Skip to main content

uqa_execution/query/
set_projection.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Physical expansion of set-returning SELECT-list calls.
8
9use crate::query::routine_invocation::SQLRoutineInvoker;
10use crate::query::table_functions::{
11    registered_table_function_rows, TableFunctionCall, TableFunctionRows,
12};
13use crate::query::PhysicalProjection;
14use crate::scalar::plan::PlanSubqueryArena;
15use crate::{
16    eval_call_arguments, Batch, ExecResult, OwnedPhysicalRow, PhysicalOperator,
17    PhysicalProjectRows, PhysicalRow, Project, ProjectionTarget, RowSchema, ScalarEvalContext,
18    ScalarExpr, SharedExpressionEvaluator,
19};
20use std::sync::Arc;
21use uqa_core::Value;
22use uqa_sql::semantics::sets::{SetFunctionCall, SetFunctionCatalog, SetProjectionPlan};
23use uqa_sql::{ast::ColumnType, SQLError, SQLParam};
24
25/// Function invocation and scoped expression services consumed by a set-expansion operator.
26pub trait SetFunctionRuntime:
27    crate::query::expression::ScalarExpressionContext + SQLRoutineInvoker
28{
29    fn has_registered_table_function(&self, name: &str) -> bool;
30    fn table_function_rows(
31        &self,
32        call: TableFunctionCall<'_>,
33        params: &[SQLParam],
34        row: Option<&OwnedPhysicalRow>,
35    ) -> Result<TableFunctionRows, SQLError>;
36}
37
38enum SetFunctionState {
39    Scalar(Value),
40    Set {
41        columns: Vec<String>,
42        rows: PhysicalProjectRows,
43        exhausted: bool,
44    },
45}
46
47struct SetExpansion {
48    input: OwnedPhysicalRow,
49    calls: Vec<SetFunctionState>,
50    has_set: bool,
51    scalar_emitted: bool,
52}
53
54impl SetExpansion {
55    fn next_values(&mut self) -> ExecResult<Option<Vec<Value>>> {
56        if !self.has_set {
57            if self.scalar_emitted {
58                return Ok(None);
59            }
60            self.scalar_emitted = true;
61            return Ok(Some(
62                self.calls
63                    .iter()
64                    .map(|call| match call {
65                        SetFunctionState::Scalar(value) => value.clone(),
66                        SetFunctionState::Set { .. } => unreachable!("has_set is false"),
67                    })
68                    .collect(),
69            ));
70        }
71
72        let mut produced = false;
73        let mut values = Vec::with_capacity(self.calls.len());
74        for call in &mut self.calls {
75            match call {
76                SetFunctionState::Scalar(value) => values.push(value.clone()),
77                SetFunctionState::Set {
78                    columns,
79                    rows,
80                    exhausted,
81                } => {
82                    if *exhausted {
83                        values.push(Value::Null);
84                        continue;
85                    }
86                    if let Some(row) = rows.next() {
87                        produced = true;
88                        values.push(set_row_value(row?, columns));
89                    } else {
90                        *exhausted = true;
91                        values.push(Value::Null);
92                    }
93                }
94            }
95        }
96        Ok(produced.then_some(values))
97    }
98}
99
100fn set_row_value(row: PhysicalRow, columns: &[String]) -> Value {
101    let values = row.into_physical_values();
102    if values.len() == 1 {
103        return values.into_iter().next().unwrap_or(Value::Null);
104    }
105    debug_assert_eq!(columns.len(), values.len());
106    Value::Record(
107        columns
108            .iter()
109            .cloned()
110            .zip(values)
111            .collect::<Vec<_>>()
112            .into(),
113    )
114}
115
116/// Projection and output batching applied after set expansion.
117pub struct SetProjectionOutput {
118    pub projections: Vec<PhysicalProjection>,
119    pub pass_through: bool,
120    pub batch_size: usize,
121}
122
123pub fn build_set_projection<'a>(
124    mut operator: Box<dyn PhysicalOperator + 'a>,
125    catalog: &dyn SetFunctionCatalog,
126    runtime: Arc<dyn SetFunctionRuntime + 'a>,
127    params: &'a [SQLParam],
128    evaluator: SharedExpressionEvaluator<'a>,
129    output: SetProjectionOutput,
130) -> Result<Box<dyn PhysicalOperator + 'a>, SQLError> {
131    let SetProjectionOutput {
132        projections,
133        pass_through,
134        batch_size: output_batch_size,
135    } = output;
136    let plan = SetProjectionPlan::new(
137        catalog,
138        runtime.as_ref(),
139        projections,
140        operator.row_schema(),
141        params,
142    )?;
143    let max_level = plan.calls.iter().map(|call| call.level).max().unwrap_or(0);
144    for level in 0..=max_level {
145        let calls = plan
146            .calls
147            .iter()
148            .filter(|call| call.level == level)
149            .cloned()
150            .collect::<Vec<_>>();
151        if calls.is_empty() {
152            continue;
153        }
154        let projections = calls
155            .iter()
156            .map(|call| {
157                (
158                    ProjectionTarget::Internal(call.placeholder),
159                    ScalarExpr::InternalColumn(call.placeholder),
160                )
161            })
162            .collect();
163        operator = Box::new(SetProjection::from_plan(
164            operator,
165            Arc::clone(&runtime),
166            params,
167            evaluator.clone(),
168            SetProjectionPlan { projections, calls },
169            true,
170            output_batch_size,
171        ));
172    }
173    if pass_through {
174        Ok(Box::new(Project::appending_target_evaluator(
175            operator,
176            plan.projections,
177            evaluator,
178        )))
179    } else {
180        Ok(Box::new(Project::with_target_evaluator(
181            operator,
182            plan.projections,
183            evaluator,
184        )))
185    }
186}
187
188mod operator;
189pub use operator::SetProjection;