Skip to main content

uqa_execution/aggregation/
distinct.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bounded, ordered DISTINCT input and aggregate value comparisons.
8
9use super::{AggregateValueBuffer, SQLError, Value};
10use uqa_core::memory::ProductionControl;
11use uqa_sql::expr::enums::EnumLabelCatalog;
12
13/// Aggregate DISTINCT uses SQL ordering before eliminating adjacent equal inputs.
14/// Reusing sorted runs keeps both comparisons and spill behavior fallible.
15pub struct DistinctTracker {
16    pub(super) values: AggregateValueBuffer,
17    pub(super) argument_count: Option<usize>,
18}
19
20impl Default for DistinctTracker {
21    fn default() -> Self {
22        Self::new(32 * 1024 * 1024)
23    }
24}
25
26impl DistinctTracker {
27    pub(super) fn new(budget_bytes: usize) -> Self {
28        Self {
29            values: AggregateValueBuffer::new(budget_bytes),
30            argument_count: None,
31        }
32    }
33
34    pub(super) fn is_empty(&self) -> bool {
35        self.values.next_sequence == 0
36    }
37
38    pub(super) fn insert(
39        &mut self,
40        value: &Value,
41        mut sort_keys: Vec<super::ordering::AggregateSortKey>,
42        enums: Option<&dyn EnumLabelCatalog>,
43    ) -> Result<(), SQLError> {
44        // Explicit ORDER BY keys come first; each selected argument resolves their ties.
45        if let Some(count) = self.argument_count {
46            for value in arguments(value, count)? {
47                sort_keys.push(super::ordering::AggregateSortKey::ascending(value.clone()));
48            }
49        } else {
50            sort_keys.push(super::ordering::AggregateSortKey::ascending(value.clone()));
51        }
52        self.values.push(value.clone(), sort_keys, enums)
53    }
54
55    pub(super) fn for_each(
56        &self,
57        enums: Option<&dyn EnumLabelCatalog>,
58        mut observe: impl FnMut(&Value) -> Result<(), SQLError>,
59    ) -> Result<(), SQLError> {
60        let mut previous: Option<Value> = None;
61        self.values.for_each_ordered(enums, |record| {
62            if let Some(previous) = &previous {
63                if self.equal_inputs(previous, &record.value, enums)? {
64                    return Ok(());
65                }
66            }
67            observe(&record.value)?;
68            previous = Some(record.value);
69            Ok(())
70        })
71    }
72
73    fn equal_inputs(
74        &self,
75        left: &Value,
76        right: &Value,
77        enums: Option<&dyn EnumLabelCatalog>,
78    ) -> Result<bool, SQLError> {
79        let types = self.values.key_types.as_deref().unwrap_or_default();
80        let control = ProductionControl::uncontrolled();
81        let Some(count) = self.argument_count else {
82            return uqa_sql::expr::equal_selected_values_with_catalog(
83                left,
84                right,
85                types.last().and_then(Option::as_ref),
86                &control,
87                enums,
88            );
89        };
90        let types = types
91            .get(types.len().saturating_sub(count)..)
92            .filter(|types| types.len() == count)
93            .ok_or_else(|| SQLError::Internal("DISTINCT argument types are missing".into()))?;
94        for ((left, right), ty) in arguments(left, count)?
95            .iter()
96            .zip(arguments(right, count)?)
97            .zip(types)
98        {
99            if !uqa_sql::expr::equal_selected_values_with_catalog(
100                left,
101                right,
102                ty.as_ref(),
103                &control,
104                enums,
105            )? {
106                return Ok(false);
107            }
108        }
109        Ok(true)
110    }
111}
112
113fn arguments(value: &Value, count: usize) -> Result<&[Value], SQLError> {
114    if let Value::List(values) = value {
115        if values.len() == count {
116            return Ok(values);
117        }
118    }
119    Err(SQLError::Internal(
120        "DISTINCT input does not match its argument tuple".into(),
121    ))
122}
123
124pub fn value_as_f64(v: &Value) -> Result<f64, SQLError> {
125    match v {
126        Value::Int(n) => Ok(*n as f64),
127        Value::Float(f) => Ok(*f),
128        Value::Decimal(d) => d.to_f64().ok_or_else(|| {
129            SQLError::TypeMismatch(format!("expected number that fits float, got {v:?}"))
130        }),
131        other => Err(SQLError::TypeMismatch(format!(
132            "expected number, got {other:?}"
133        ))),
134    }
135}
136
137pub fn value_lt(a: &Value, b: &Value) -> Result<bool, SQLError> {
138    Ok(match (a, b) {
139        (
140            Value::Int(_) | Value::Float(_) | Value::Decimal(_),
141            Value::Int(_) | Value::Float(_) | Value::Decimal(_),
142        ) => a < b,
143        (Value::Str(x), Value::Str(y)) => x < y,
144        (Value::FixedChar(x), Value::FixedChar(y)) => x.trim_end() < y.trim_end(),
145        (Value::Bytes(x), Value::Bytes(y)) => x < y,
146        (Value::Temporal(x), Value::Temporal(y)) => x < y,
147        // PostgreSQL MIN/MAX bind array_smaller/array_larger for catalog vectors; OID-vector scalar operators use a different order.
148        (Value::LegacyVector(left), Value::LegacyVector(right)) => {
149            left.compare_as_array(right).is_lt()
150        }
151        (Value::Array(_), Value::Array(_))
152        | (Value::List(_), Value::List(_))
153        | (Value::Row(_), Value::Row(_))
154        | (Value::Record(_), Value::Record(_)) => super::compare_extrema(a, b)?.is_lt(),
155        (Value::Map(x), Value::Map(y)) => x < y,
156        // MIN and MAX over `anyenum` use `enum_smaller` and `enum_larger`: label keys in declaration order.
157        (Value::Enum(x), Value::Enum(y)) => {
158            if x.type_oid() != y.type_oid() {
159                return Err(SQLError::Internal(format!(
160                    "MIN/MAX received enum type OIDs {} and {}",
161                    x.type_oid(),
162                    y.type_oid()
163                )));
164            }
165            x.key() < y.key()
166        }
167        _ => false,
168    })
169}
170
171pub fn value_gt(a: &Value, b: &Value) -> Result<bool, SQLError> {
172    value_lt(b, a)
173}