Skip to main content

uqa_execution/query/relational/
aggregation.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Aggregate, DISTINCT, and ordering-key preparation.
8
9use crate::{FunctionTypeResolver, ProjectionTarget, RowSchema, ScalarExpr};
10use uqa_sql::ast::InternalColumnRef;
11use uqa_sql::plan::{ProjectionPlan, QueryBlockPlan};
12use uqa_sql::{SQLError, SQLParam};
13
14use super::ordering::{
15    distinct_output_target_position, identity_order_columns, one_based_output_position,
16    output_target_position, prior_distinct_key_index, resolve_order_expression,
17};
18use crate::query::projection::projection_target_expression;
19use crate::query::{OutputColumnMapping, PhysicalProjection};
20use uqa_sql::semantics::{
21    projection_columns,
22    sets::{validation::expression_may_return_set, SetFunctionCatalog},
23};
24
25#[cfg(test)]
26mod tests;
27
28type PreparedOrderSetProjections = (Option<QueryBlockPlan>, Vec<(usize, InternalColumnRef)>);
29
30pub fn prepare_order_set_projections(
31    engine: &dyn SetFunctionCatalog,
32    type_resolver: &dyn FunctionTypeResolver,
33    statement: &QueryBlockPlan,
34    output_columns: &[OutputColumnMapping],
35    projections: &mut Vec<PhysicalProjection>,
36    schema: &RowSchema,
37    params: &[SQLParam],
38) -> Result<PreparedOrderSetProjections, SQLError> {
39    let mut prepared: Option<QueryBlockPlan> = None;
40    let relation = uqa_sql::ast::InternalRelationId::allocate();
41    let mut resjunk = Vec::new();
42    for (index, order) in statement.order_by.iter().enumerate() {
43        if let Some(target) = output_target_position(statement, &order.expr, output_columns)? {
44            if !target.direct {
45                prepared.get_or_insert_with(|| statement.clone()).order_by[index].expr =
46                    one_based_output_position(target.position)?;
47            }
48            continue;
49        }
50        let expression = resolve_order_expression(&order.expr, output_columns)?;
51        if statement.with_ties {
52            let column = relation.column(resjunk.len());
53            projections.push((ProjectionTarget::Internal(column), expression));
54            resjunk.push((index, column));
55            prepared.get_or_insert_with(|| statement.clone()).order_by[index].expr =
56                ScalarExpr::InternalColumn(column);
57            continue;
58        }
59        if let Some((target, _)) = projections.iter().find(|(_, projected)| {
60            uqa_sql::semantics::aggregates::exprs_match(projected, &expression)
61        }) {
62            prepared.get_or_insert_with(|| statement.clone()).order_by[index].expr =
63                projection_target_expression(target);
64        } else if matches!(statement.compute, uqa_sql::plan::ComputePlan::Window)
65            || expression_may_return_set(engine, type_resolver, &expression, schema, params)?
66        {
67            // Window output projection replaces the source schema, so an unselected sort expression must survive as an internal attribute until ordering consumes it.
68            let column = relation.column(resjunk.len());
69            projections.push((ProjectionTarget::Internal(column), expression));
70            resjunk.push((index, column));
71            prepared.get_or_insert_with(|| statement.clone()).order_by[index].expr =
72                ScalarExpr::InternalColumn(column);
73        }
74    }
75    Ok((prepared, resjunk))
76}
77
78pub fn append_distinct_set_projections(
79    statement: &QueryBlockPlan,
80    output_columns: &[OutputColumnMapping],
81    projections: &mut Vec<PhysicalProjection>,
82) -> Result<Vec<(usize, InternalColumnRef)>, SQLError> {
83    let mut columns = Vec::new();
84    visit_distinct_set_projections(statement, output_columns, |index, column, expression| {
85        projections.push((ProjectionTarget::Internal(column), expression.clone()));
86        columns.push((index, column));
87        Ok(())
88    })?;
89    Ok(columns)
90}
91
92pub(super) fn visit_distinct_set_projections<'a>(
93    statement: &'a QueryBlockPlan,
94    output_columns: &'a [OutputColumnMapping],
95    mut observe: impl FnMut(usize, InternalColumnRef, &'a ScalarExpr) -> Result<(), SQLError>,
96) -> Result<(), SQLError> {
97    let relation = uqa_sql::ast::InternalRelationId::allocate();
98    let mut count = 0;
99    for (index, expression) in statement.distinct_on.iter().enumerate() {
100        if prior_distinct_key_index(statement, index, expression, output_columns)?.is_some() {
101            continue;
102        }
103        if distinct_output_target_position(statement, expression, output_columns)?.is_some() {
104            continue;
105        }
106        let expression =
107            crate::query::ordering::resolve_order_expression_ref(expression, output_columns)?;
108        observe(index, relation.column(count), expression)?;
109        count += 1;
110    }
111    Ok(())
112}
113
114pub struct AggregateKeyStatement {
115    pub statement: QueryBlockPlan,
116    pub targets: Vec<(usize, InternalColumnRef)>,
117    pub distinct_on: Vec<(usize, InternalColumnRef)>,
118    pub order_by: Vec<(usize, InternalColumnRef)>,
119}
120
121pub fn prepare_aggregate_key_statement(
122    statement: &QueryBlockPlan,
123) -> Result<Option<AggregateKeyStatement>, SQLError> {
124    let output = identity_order_columns(&projection_columns(&statement.projections));
125    let mut prepared: Option<QueryBlockPlan> = None;
126    let relation = uqa_sql::ast::InternalRelationId::allocate();
127    let mut targets = Vec::new();
128    let mut distinct_on = Vec::new();
129    let mut order_by = Vec::new();
130    for (index, expression) in statement.distinct_on.iter().enumerate() {
131        if prior_distinct_key_index(statement, index, expression, &output)?.is_some() {
132            continue;
133        }
134        if distinct_output_target_position(statement, expression, &output)?.is_some() {
135            continue;
136        }
137        let expression = resolve_order_expression(expression, &output)?;
138        let prepared = prepared.get_or_insert_with(|| statement.clone());
139        let position = prepared.projections.len();
140        let column = relation.column(targets.len());
141        prepared.projections.push(ProjectionPlan {
142            expr: expression,
143            alias: None,
144        });
145        targets.push((position, column));
146        distinct_on.push((index, column));
147    }
148    for (index, order) in statement.order_by.iter().enumerate() {
149        if let Some(target) = output_target_position(statement, &order.expr, &output)? {
150            if !target.direct {
151                prepared.get_or_insert_with(|| statement.clone()).order_by[index].expr =
152                    one_based_output_position(target.position)?;
153            }
154            continue;
155        }
156        let expression = resolve_order_expression(&order.expr, &output)?;
157        let current = prepared.as_ref().unwrap_or(statement);
158        let existing = current
159            .projections
160            .iter()
161            .enumerate()
162            .find(|(_, projection)| {
163                uqa_sql::semantics::aggregates::exprs_match(&projection.expr, &expression)
164            })
165            .map(|(position, _)| position);
166        let prepared = prepared.get_or_insert_with(|| statement.clone());
167        let key = if let Some(position) = existing {
168            if let Some((_, column)) = targets
169                .iter()
170                .find(|(target_position, _)| *target_position == position)
171            {
172                order_by.push((index, *column));
173                ScalarExpr::InternalColumn(*column)
174            } else {
175                ScalarExpr::Position(position)
176            }
177        } else {
178            let position = prepared.projections.len();
179            let column = relation.column(targets.len());
180            prepared.projections.push(ProjectionPlan {
181                expr: expression,
182                alias: None,
183            });
184            targets.push((position, column));
185            order_by.push((index, column));
186            ScalarExpr::InternalColumn(column)
187        };
188        prepared.order_by[index].expr = key;
189    }
190    Ok(prepared.map(|statement| AggregateKeyStatement {
191        statement,
192        targets,
193        distinct_on,
194        order_by,
195    }))
196}