Skip to main content

uqa_execution/relational/
sort.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! External sort wrapper and SQL key comparison.
8
9use super::{
10    Batch, DefaultExpressionEvaluator, ExecResult, PhysicalOperator, SQLParam, ScalarExpr,
11    SharedExpressionEvaluator, Value,
12};
13
14#[derive(Debug, Clone)]
15pub struct SortKey {
16    pub expr: ScalarExpr,
17    pub descending: bool,
18    /// `Some(true)` forces NULLS FIRST, `Some(false)` forces NULLS
19    /// LAST. `None` falls back to the SQL-standard default - NULLS
20    /// LAST for ASC and NULLS FIRST for DESC.
21    pub nulls_first: Option<bool>,
22}
23
24/// Byte-bounded blocking sort backed by the external merge-sort implementation.
25/// Compatibility constructors use a 64 MiB budget; engine callers should pass
26/// the active session's `work_mem` through [`Self::with_evaluator_and_work_mem`].
27const DEFAULT_SORT_WORK_MEM_BYTES: usize = 64 * 1024 * 1024;
28
29pub struct Sort<'a> {
30    inner: crate::external_sort::ExternalSort<'a>,
31}
32
33impl Sort<'static> {
34    pub fn with_work_mem(
35        child: Box<dyn PhysicalOperator>,
36        keys: Vec<SortKey>,
37        params: Vec<SQLParam>,
38        work_mem_bytes: usize,
39    ) -> Self {
40        Self::with_evaluator_and_work_mem(
41            child,
42            keys,
43            DefaultExpressionEvaluator::shared(params),
44            work_mem_bytes,
45        )
46    }
47
48    pub fn new(
49        child: Box<dyn PhysicalOperator>,
50        keys: Vec<SortKey>,
51        params: Vec<SQLParam>,
52    ) -> Self {
53        Self::with_evaluator(child, keys, DefaultExpressionEvaluator::shared(params))
54    }
55
56    /// Top-K variant: retain only the first `keep` rows of the sorted
57    /// order. Uses a partial selection, so the cost is `O(n + k log k)`
58    /// instead of `O(n log n)`.
59    pub fn with_keep(
60        child: Box<dyn PhysicalOperator>,
61        keys: Vec<SortKey>,
62        params: Vec<SQLParam>,
63        keep: usize,
64    ) -> Self {
65        Self::with_evaluator_and_keep(
66            child,
67            keys,
68            DefaultExpressionEvaluator::shared(params),
69            keep,
70        )
71    }
72}
73
74impl<'a> Sort<'a> {
75    pub fn with_evaluator(
76        child: Box<dyn PhysicalOperator + 'a>,
77        keys: Vec<SortKey>,
78        evaluator: SharedExpressionEvaluator<'a>,
79    ) -> Self {
80        Self::with_evaluator_and_work_mem(child, keys, evaluator, DEFAULT_SORT_WORK_MEM_BYTES)
81    }
82
83    pub fn with_evaluator_and_work_mem(
84        child: Box<dyn PhysicalOperator + 'a>,
85        keys: Vec<SortKey>,
86        evaluator: SharedExpressionEvaluator<'a>,
87        work_mem_bytes: usize,
88    ) -> Self {
89        Self {
90            inner: crate::external_sort::ExternalSort::new(
91                child,
92                keys,
93                evaluator,
94                None,
95                work_mem_bytes,
96            ),
97        }
98    }
99
100    pub fn with_evaluator_and_keep(
101        child: Box<dyn PhysicalOperator + 'a>,
102        keys: Vec<SortKey>,
103        evaluator: SharedExpressionEvaluator<'a>,
104        keep: usize,
105    ) -> Self {
106        Self {
107            inner: crate::external_sort::ExternalSort::new(
108                child,
109                keys,
110                evaluator,
111                Some(keep),
112                DEFAULT_SORT_WORK_MEM_BYTES,
113            ),
114        }
115    }
116}
117
118/// Compare two pre-computed sort-key vectors under `keys` semantics:
119/// per-key direction plus `PostgreSQL` NULLS placement (default NULLS
120/// LAST for ascending, NULLS FIRST for descending).
121pub fn compare_sort_key_values(
122    keys: &[SortKey],
123    av: &[Value],
124    bv: &[Value],
125) -> ExecResult<std::cmp::Ordering> {
126    compare_sort_key_values_by(keys, |index| (&av[index], &bv[index]))
127}
128
129pub(crate) fn compare_sort_key_values_by<'a>(
130    keys: &[SortKey],
131    values: impl FnMut(usize) -> (&'a Value, &'a Value),
132) -> ExecResult<std::cmp::Ordering> {
133    SortComparison {
134        keys,
135        types: &[],
136        enums: None,
137        states: &[],
138        equality_keys: false,
139    }
140    .compare_by(values)
141}
142
143/// One ordering support call per key, retained across run generation, top-K selection and merge passes.
144#[derive(Clone, Copy)]
145pub(crate) struct SortComparison<'a> {
146    pub(crate) keys: &'a [SortKey],
147    pub(crate) types: &'a [Option<uqa_sql::ColumnType>],
148    pub(crate) enums: Option<&'a dyn uqa_sql::expr::enums::EnumLabelCatalog>,
149    pub(crate) states: &'a [uqa_sql::expr::enums::EnumComparisonState],
150    pub(crate) equality_keys: bool,
151}
152
153impl SortComparison<'_> {
154    pub(crate) fn compare_by<'v>(
155        &self,
156        mut values: impl FnMut(usize) -> (&'v Value, &'v Value),
157    ) -> ExecResult<std::cmp::Ordering> {
158        use std::cmp::Ordering;
159        for (i, k) in self.keys.iter().enumerate() {
160            let (a, b) = values(i);
161            let a_null = matches!(a, Value::Null);
162            let b_null = matches!(b, Value::Null);
163            let nulls_first = k.nulls_first.unwrap_or(k.descending);
164            if a_null || b_null {
165                let null_cmp = if a_null == b_null {
166                    Ordering::Equal
167                } else if a_null {
168                    if nulls_first {
169                        Ordering::Less
170                    } else {
171                        Ordering::Greater
172                    }
173                } else if nulls_first {
174                    Ordering::Greater
175                } else {
176                    Ordering::Less
177                };
178                if null_cmp != Ordering::Equal {
179                    return Ok(null_cmp);
180                }
181                continue;
182            }
183            let ord = if self.equality_keys {
184                uqa_sql::expr::compare_selected_grouping_values_with_catalog(
185                    a,
186                    b,
187                    self.types.get(i).and_then(Option::as_ref),
188                    &uqa_core::memory::ProductionControl::uncontrolled(),
189                    self.enums,
190                )?
191            } else {
192                uqa_sql::expr::compare_selected_values_with_catalog(
193                    a,
194                    b,
195                    self.types.get(i).and_then(Option::as_ref),
196                    &uqa_core::memory::ProductionControl::uncontrolled(),
197                    self.enums,
198                    self.states.get(i),
199                )?
200            };
201            let ord = if k.descending { ord.reverse() } else { ord };
202            if ord != Ordering::Equal {
203                return Ok(ord);
204            }
205        }
206        Ok(Ordering::Equal)
207    }
208}
209
210/// DISTINCT and WITH TIES use equality, without invoking a scalar ordering support function.
211pub(crate) fn equal_typed_sort_key_values(
212    left: &[Value],
213    right: &[Value],
214    types: &[Option<uqa_sql::ColumnType>],
215    enums: Option<&dyn uqa_sql::expr::SQLValueCatalog>,
216) -> ExecResult<bool> {
217    if left.len() != right.len() {
218        return Ok(false);
219    }
220    for (index, (left, right)) in left.iter().zip(right).enumerate() {
221        if !uqa_sql::expr::equal_selected_values_with_catalog(
222            left,
223            right,
224            types.get(index).and_then(Option::as_ref),
225            &uqa_core::memory::ProductionControl::uncontrolled(),
226            enums,
227        )? {
228            return Ok(false);
229        }
230    }
231    Ok(true)
232}
233
234pub(super) fn compare_values(
235    a: &Value,
236    b: &Value,
237) -> Result<std::cmp::Ordering, uqa_sql::SQLError> {
238    use std::cmp::Ordering::*;
239    Ok(match (a, b) {
240        (Value::Null, Value::Null) => Equal,
241        (Value::Null, _) => Less,
242        (_, Value::Null) => Greater,
243        (Value::Temporal(x), Value::Str(y)) => x
244            .parse_same_kind(y)
245            .map_or_else(|| a.cmp(b), |parsed| x.cmp(&parsed)),
246        (Value::Str(x), Value::Temporal(y)) => y
247            .parse_same_kind(x)
248            .map_or_else(|| a.cmp(b), |parsed| parsed.cmp(y)),
249        _ => crate::aggregation::compare_extrema(a, b)?,
250    })
251}
252
253impl PhysicalOperator for Sort<'_> {
254    fn row_schema(&self) -> &super::RowSchema {
255        self.inner.row_schema()
256    }
257
258    fn output_ordering(&self) -> &[crate::PhysicalOrder] {
259        self.inner.output_ordering()
260    }
261
262    fn backward_scan_support(&self) -> crate::BackwardScanSupport {
263        crate::BackwardScanSupport::Materialize
264    }
265
266    fn open(&mut self) -> ExecResult<()> {
267        self.inner.open()
268    }
269
270    fn next(&mut self) -> ExecResult<Option<Batch>> {
271        self.inner.next()
272    }
273
274    fn close(&mut self) -> ExecResult<()> {
275        self.inner.close()
276    }
277}
278
279// -------------------------------------------------------------------------
280// Limit / Offset
281// -------------------------------------------------------------------------