Skip to main content

uqa_execution/query/relational/
ordering.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Projection placement around blocking ordering operators.
8
9use super::limit::attach_order_limit;
10use super::{build_set_projection, RelationalContext};
11use crate::query::projection::projection_set_batch_size;
12use crate::query::row_at_a_time::RowAtATime;
13use crate::query::runtime::QueryRuntimeView;
14use crate::query::{CteScope, OutputColumnMapping, PhysicalProjection};
15use crate::{ProjectionTarget, SharedExpressionEvaluator};
16use std::sync::Arc;
17use uqa_core::Value;
18use uqa_sql::semantics::sets::validation::projections_may_return_set;
19use uqa_sql::{plan::QueryBlockPlan, SQLError, SQLParam, ScalarExpr};
20
21pub struct FinalProjectionExecution<'context, 'scope, S: Clone + 'static> {
22    pub context: RelationalContext<'context, S>,
23    pub params: &'context [SQLParam],
24    pub ctes: &'scope CteScope<S>,
25    pub runtime: QueryRuntimeView<'context>,
26    pub evaluator: SharedExpressionEvaluator<'context>,
27}
28
29pub use crate::query::ordering::*;
30mod streaming;
31pub(super) use streaming::attach_streaming_order_projection;
32
33pub fn attach_final_projection_order<'a, S: Clone + 'static>(
34    mut operator: Box<dyn crate::PhysicalOperator + 'a>,
35    ordering: (&QueryBlockPlan, &[OutputColumnMapping]),
36    projections: Vec<PhysicalProjection>,
37    execution: FinalProjectionExecution<'a, '_, S>,
38) -> Result<Box<dyn crate::PhysicalOperator + 'a>, SQLError> {
39    let (statement, output) = ordering;
40    let type_resolver = execution.context.expression_scope(execution.ctes.clone());
41    let returns_set = projections_may_return_set(
42        execution.context.catalog,
43        type_resolver.as_ref(),
44        &projections,
45        operator.row_schema(),
46        execution.params,
47    )?;
48    if execution.ctes.streams_command_progress() && !statement.order_by.is_empty() && !returns_set {
49        return attach_deferred_order_projection(
50            operator,
51            statement,
52            output,
53            projections,
54            execution,
55        );
56    }
57    let FinalProjectionExecution {
58        context,
59        params,
60        ctes,
61        runtime,
62        evaluator,
63    } = execution;
64    let batch_size = projection_set_batch_size(statement, ctes);
65    operator = if returns_set {
66        build_set_projection(
67            operator,
68            context,
69            params,
70            ctes,
71            Arc::clone(&evaluator),
72            crate::query::set_projection::SetProjectionOutput {
73                projections,
74                pass_through: false,
75                batch_size,
76            },
77        )?
78    } else {
79        if batch_size == 1 {
80            operator = Box::new(RowAtATime::new(operator));
81        }
82        Box::new(crate::Project::with_target_evaluator(
83            operator,
84            projections,
85            Arc::clone(&evaluator),
86        ))
87    };
88    attach_order_limit(
89        operator, statement, output, context, params, ctes, runtime, evaluator, None,
90    )
91}
92
93fn attach_deferred_order_projection<'a, S: Clone + 'static>(
94    mut operator: Box<dyn crate::PhysicalOperator + 'a>,
95    statement: &QueryBlockPlan,
96    output: &[OutputColumnMapping],
97    projections: Vec<PhysicalProjection>,
98    execution: FinalProjectionExecution<'a, '_, S>,
99) -> Result<Box<dyn crate::PhysicalOperator + 'a>, SQLError> {
100    let FinalProjectionExecution {
101        context,
102        params,
103        ctes,
104        runtime,
105        evaluator,
106    } = execution;
107    let (sort_statement, sort_projections, projections) =
108        prepare_deferred_order_projection(statement, output, projections)?;
109    operator = Box::new(crate::Project::appending_target_evaluator(
110        operator,
111        sort_projections,
112        Arc::clone(&evaluator),
113    ));
114    operator = attach_order_limit(
115        operator,
116        &sort_statement,
117        &[],
118        context,
119        params,
120        ctes,
121        runtime,
122        Arc::clone(&evaluator),
123        None,
124    )?;
125    operator = Box::new(RowAtATime::new(operator));
126    Ok(Box::new(crate::Project::with_target_evaluator(
127        operator,
128        projections,
129        evaluator,
130    )))
131}
132
133type PreparedDeferredOrderProjection = (
134    QueryBlockPlan,
135    Vec<PhysicalProjection>,
136    Vec<PhysicalProjection>,
137);
138
139pub(super) fn prepare_deferred_order_projection(
140    statement: &QueryBlockPlan,
141    output: &[OutputColumnMapping],
142    mut projections: Vec<PhysicalProjection>,
143) -> Result<PreparedDeferredOrderProjection, SQLError> {
144    let mut sort_statement = statement.clone();
145    let sort_relation = uqa_sql::ast::InternalRelationId::allocate();
146    let mut sort_projections =
147        Vec::<(Option<usize>, ScalarExpr, uqa_sql::ast::InternalColumnRef)>::new();
148    for (order_index, order) in statement.order_by.iter().enumerate() {
149        let resolved = resolve_order_expression(&order.expr, output)?;
150        let direct_target = match &order.expr {
151            ScalarExpr::Literal(Value::Int(position)) => usize::try_from(*position)
152                .ok()
153                .and_then(|position| position.checked_sub(1)),
154            ScalarExpr::Column(name) => output.iter().position(|(label, _)| label == name),
155            _ => None,
156        };
157        let target = direct_target
158            .or_else(|| {
159                projections.iter().position(|(target, projected)| {
160                    matches!(
161                        (target, &resolved),
162                        (ProjectionTarget::Internal(column), ScalarExpr::InternalColumn(required))
163                            if column == required
164                    ) || uqa_sql::semantics::aggregates::exprs_match(projected, &resolved)
165                })
166            })
167            .filter(|position| *position < projections.len());
168        let expression = target
169            .map(|position| projections[position].1.clone())
170            .unwrap_or(resolved);
171        let existing_column = sort_projections
172            .iter()
173            .find(|(existing_target, existing, _)| {
174                target == *existing_target
175                    && (target.is_some()
176                        || uqa_sql::semantics::aggregates::exprs_match(existing, &expression))
177            })
178            .map(|(_, _, column)| *column);
179        let column = if let Some(column) = existing_column {
180            column
181        } else {
182            let column = sort_relation.column(sort_projections.len());
183            sort_projections.push((target, expression, column));
184            column
185        };
186        sort_statement.order_by[order_index].expr = ScalarExpr::InternalColumn(column);
187        if let Some(target) = target {
188            projections[target].1 = ScalarExpr::InternalColumn(column);
189        }
190    }
191    let sort_projections = sort_projections
192        .into_iter()
193        .map(|(_, expression, column)| (ProjectionTarget::Internal(column), expression))
194        .collect();
195    Ok((sort_statement, sort_projections, projections))
196}