Skip to main content

uqa_execution/set_operation/
construction.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Set-operation input validation and physical construction.
8
9use super::{
10    set_operation_types, AlignSchema, ColumnEvaluator, DirectionalAppendPosition,
11    ExternalSetOperation, RowCursor,
12};
13use crate::physical::construction::OperatorBuild;
14use crate::{
15    ExecError, ExecResult, ExternalSort, PhysicalOperator, RowSchema, ScalarExpr,
16    SharedExpressionEvaluator, SortKey,
17};
18use std::sync::Arc;
19use uqa_sql::ast::{ColumnType, SetOpKind};
20
21impl<'a> AlignSchema<'a> {
22    pub(super) fn new(
23        child: Box<dyn PhysicalOperator + 'a>,
24        output: Vec<String>,
25        output_types: &[Option<ColumnType>],
26    ) -> ExecResult<Self> {
27        let (child, (schema, coercions)) = OperatorBuild::run(child, |child| {
28            let source = child.schema().to_vec();
29            if source.len() != output.len() {
30                return Err(crate::physical::physical_exec_error(ExecError::Other(
31                    format!(
32                        "set-operation inputs have different widths: {} and {}",
33                        output.len(),
34                        source.len()
35                    ),
36                )));
37            }
38            if output.len() != output_types.len() {
39                return Err(crate::physical::physical_exec_error(ExecError::Other(
40                    format!(
41                        "set-operation output type width {} does not match input width {}",
42                        output_types.len(),
43                        output.len()
44                    ),
45                )));
46            }
47            let coercions = child
48                .row_schema()
49                .column_types()
50                .iter()
51                .zip(output_types)
52                .map(|(source, target)| {
53                    target
54                        .as_ref()
55                        .filter(|target| source.as_ref() != Some(*target))
56                        .cloned()
57                })
58                .collect();
59            let schema = RowSchema::with_types(output, output_types.to_vec());
60            Ok((schema, coercions))
61        })?;
62        Ok(Self {
63            input_pending: crate::physical::demand::PendingRows::default(),
64            child,
65            schema,
66            coercions,
67        })
68    }
69}
70
71impl<'a> ExternalSetOperation<'a> {
72    pub fn new(
73        left: Box<dyn PhysicalOperator + 'a>,
74        right: Box<dyn PhysicalOperator + 'a>,
75        kind: SetOpKind,
76        all: bool,
77        work_mem_bytes: usize,
78    ) -> ExecResult<Self> {
79        let (left, right, output_types) = OperatorBuild::pair(left, right, |left, right| {
80            set_operation_types(left.row_schema(), right.row_schema())
81                .map_err(crate::physical::physical_exec_error)
82        })?;
83        Self::new_with_types(left, right, kind, all, output_types, work_mem_bytes)
84    }
85
86    pub fn new_with_evaluator(
87        left: Box<dyn PhysicalOperator + 'a>,
88        right: Box<dyn PhysicalOperator + 'a>,
89        kind: SetOpKind,
90        all: bool,
91        work_mem_bytes: usize,
92        evaluator: SharedExpressionEvaluator<'a>,
93    ) -> ExecResult<Self> {
94        let (left, right, output_types) = OperatorBuild::pair(left, right, |left, right| {
95            set_operation_types(left.row_schema(), right.row_schema())
96                .map_err(crate::physical::physical_exec_error)
97        })?;
98        Self::new_with_types_and_evaluator(
99            left,
100            right,
101            kind,
102            all,
103            output_types,
104            work_mem_bytes,
105            evaluator,
106        )
107    }
108
109    pub fn new_with_types(
110        left: Box<dyn PhysicalOperator + 'a>,
111        right: Box<dyn PhysicalOperator + 'a>,
112        kind: SetOpKind,
113        all: bool,
114        output_types: Vec<Option<ColumnType>>,
115        work_mem_bytes: usize,
116    ) -> ExecResult<Self> {
117        Self::new_with_types_and_evaluator(
118            left,
119            right,
120            kind,
121            all,
122            output_types,
123            work_mem_bytes,
124            Arc::new(ColumnEvaluator),
125        )
126    }
127
128    /// Construct a set operation whose ordinary forward pulls remain one-row incremental when a scroll materialization boundary wraps the complete operation.
129    pub fn new_directional_with_types(
130        left: Box<dyn PhysicalOperator + 'a>,
131        right: Box<dyn PhysicalOperator + 'a>,
132        kind: SetOpKind,
133        all: bool,
134        output_types: Vec<Option<ColumnType>>,
135        work_mem_bytes: usize,
136    ) -> ExecResult<Self> {
137        let mut operation =
138            Self::new_with_types(left, right, kind, all, output_types, work_mem_bytes)?;
139        operation.incremental_union_all = true;
140        Ok(operation)
141    }
142
143    pub fn new_with_types_and_evaluator(
144        left: Box<dyn PhysicalOperator + 'a>,
145        right: Box<dyn PhysicalOperator + 'a>,
146        kind: SetOpKind,
147        all: bool,
148        output_types: Vec<Option<ColumnType>>,
149        work_mem_bytes: usize,
150        evaluator: SharedExpressionEvaluator<'a>,
151    ) -> ExecResult<Self> {
152        let output = left.schema().to_vec();
153        let (left, right, ()) = OperatorBuild::pair(left, right, |left, right| {
154            left.attach(|input| {
155                AlignSchema::new(input, output.clone(), &output_types)
156                    .map(|input| Box::new(input) as Box<dyn PhysicalOperator + 'a>)
157                    .map_err(crate::physical::physical_exec_error)
158            })?;
159            right.attach(|input| {
160                AlignSchema::new(input, output.clone(), &output_types)
161                    .map(|input| Box::new(input) as Box<dyn PhysicalOperator + 'a>)
162                    .map_err(crate::physical::physical_exec_error)
163            })
164        })?;
165        let (left, right) = if matches!((kind, all), (SetOpKind::Union, true)) {
166            (left, right)
167        } else {
168            let keys = output
169                .iter()
170                .map(|column| SortKey {
171                    expr: ScalarExpr::Column(column.clone().into()),
172                    descending: false,
173                    nulls_first: Some(true),
174                })
175                .collect::<Vec<_>>();
176            // Both merge inputs are live concurrently. Split the configured
177            // budget so their encoded in-memory runs cannot each claim it.
178            let per_input = (work_mem_bytes / 2).max(1);
179            (
180                Box::new(
181                    ExternalSort::new(left, keys.clone(), evaluator.clone(), None, per_input)
182                        .with_equality_keys(),
183                ) as Box<dyn PhysicalOperator + 'a>,
184                Box::new(
185                    ExternalSort::new(right, keys, Arc::clone(&evaluator), None, per_input)
186                        .with_equality_keys(),
187                ) as Box<dyn PhysicalOperator + 'a>,
188            )
189        };
190        Ok(Self {
191            left: RowCursor::new(left),
192            right: RowCursor::new(right),
193            evaluator,
194            kind,
195            all,
196            schema: RowSchema::with_types(output, output_types),
197            left_group: None,
198            right_group: None,
199            pending_row: None,
200            pending_count: 0,
201            output_bytes: work_mem_bytes,
202            union_all_left_done: false,
203            incremental_union_all: false,
204            directional_position: DirectionalAppendPosition::BeforeFirst,
205        })
206    }
207}