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        enums: None,
136        states: &[],
137        equality_keys: false,
138    }
139    .compare_by(values)
140}
141
142/// One ordering support call per key, retained across run generation, top-K selection and merge passes.
143#[derive(Clone, Copy)]
144pub(crate) struct SortComparison<'a> {
145    pub(crate) keys: &'a [SortKey],
146    pub(crate) enums: Option<&'a dyn uqa_sql::expr::enums::EnumLabelCatalog>,
147    pub(crate) states: &'a [uqa_sql::expr::enums::EnumComparisonState],
148    pub(crate) equality_keys: bool,
149}
150
151impl SortComparison<'_> {
152    pub(crate) fn compare_by<'v>(
153        &self,
154        mut values: impl FnMut(usize) -> (&'v Value, &'v Value),
155    ) -> ExecResult<std::cmp::Ordering> {
156        use std::cmp::Ordering;
157        for (i, k) in self.keys.iter().enumerate() {
158            let (a, b) = values(i);
159            let a_null = matches!(a, Value::Null);
160            let b_null = matches!(b, Value::Null);
161            let nulls_first = k.nulls_first.unwrap_or(k.descending);
162            if a_null || b_null {
163                let null_cmp = if a_null == b_null {
164                    Ordering::Equal
165                } else if a_null {
166                    if nulls_first {
167                        Ordering::Less
168                    } else {
169                        Ordering::Greater
170                    }
171                } else if nulls_first {
172                    Ordering::Greater
173                } else {
174                    Ordering::Less
175                };
176                if null_cmp != Ordering::Equal {
177                    return Ok(null_cmp);
178                }
179                continue;
180            }
181            let ord = if self.equality_keys {
182                uqa_sql::expr::compare_grouping_values_with_enum_catalog(
183                    a,
184                    b,
185                    &uqa_core::memory::ProductionControl::uncontrolled(),
186                    self.enums,
187                )?
188            } else {
189                uqa_sql::expr::compare_typed_values_with_enum_catalog(
190                    a,
191                    b,
192                    &uqa_core::memory::ProductionControl::uncontrolled(),
193                    self.enums,
194                    self.states.get(i),
195                )?
196            };
197            let ord = if k.descending { ord.reverse() } else { ord };
198            if ord != Ordering::Equal {
199                return Ok(ord);
200            }
201        }
202        Ok(Ordering::Equal)
203    }
204}
205
206/// DISTINCT and WITH TIES use equality, without invoking a scalar ordering support function.
207pub(crate) fn equal_sort_key_values(
208    left: &[Value],
209    right: &[Value],
210    enums: Option<&dyn uqa_sql::expr::enums::EnumLabelCatalog>,
211) -> ExecResult<bool> {
212    if left.len() != right.len() {
213        return Ok(false);
214    }
215    for (left, right) in left.iter().zip(right) {
216        if !uqa_sql::expr::equal_typed_values_with_enum_catalog(
217            left,
218            right,
219            &uqa_core::memory::ProductionControl::uncontrolled(),
220            enums,
221        )? {
222            return Ok(false);
223        }
224    }
225    Ok(true)
226}
227
228pub(super) fn compare_values(
229    a: &Value,
230    b: &Value,
231) -> Result<std::cmp::Ordering, uqa_sql::SQLError> {
232    use std::cmp::Ordering::*;
233    Ok(match (a, b) {
234        (Value::Null, Value::Null) => Equal,
235        (Value::Null, _) => Less,
236        (_, Value::Null) => Greater,
237        (Value::Temporal(x), Value::Str(y)) => x
238            .parse_same_kind(y)
239            .map_or_else(|| a.cmp(b), |parsed| x.cmp(&parsed)),
240        (Value::Str(x), Value::Temporal(y)) => y
241            .parse_same_kind(x)
242            .map_or_else(|| a.cmp(b), |parsed| parsed.cmp(y)),
243        _ => crate::aggregation::compare_extrema(a, b)?,
244    })
245}
246
247impl PhysicalOperator for Sort<'_> {
248    fn row_schema(&self) -> &super::RowSchema {
249        self.inner.row_schema()
250    }
251
252    fn output_ordering(&self) -> &[crate::PhysicalOrder] {
253        self.inner.output_ordering()
254    }
255
256    fn backward_scan_support(&self) -> crate::BackwardScanSupport {
257        crate::BackwardScanSupport::Materialize
258    }
259
260    fn open(&mut self) -> ExecResult<()> {
261        self.inner.open()
262    }
263
264    fn next(&mut self) -> ExecResult<Option<Batch>> {
265        self.inner.next()
266    }
267
268    fn close(&mut self) -> ExecResult<()> {
269        self.inner.close()
270    }
271}
272
273// -------------------------------------------------------------------------
274// Limit / Offset
275// -------------------------------------------------------------------------