Skip to main content

uqa_sql/expr/range/
comparison.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Range order uses the selected subtype's bounds, independently of text spelling.
8
9use super::{
10    CanonicalMultirange, CanonicalRange, Ordering, ProductionControl, RangeSemantics, Result,
11    SQLError, Value,
12};
13
14impl CanonicalRange {
15    /// Compare admitted ranges of one subtype in `PostgreSQL` B-tree order, retaining the caller's comparison workspace and cancellation boundary.
16    pub fn cmp_with_control(
17        &self,
18        other: &Self,
19        control: &ProductionControl<'_>,
20    ) -> Result<Ordering> {
21        self.cmp_by_with_control(other, control, |left, right, control| {
22            self.semantics().compare(left, right, control)
23        })
24    }
25}
26
27impl<I: PartialEq> CanonicalRange<I> {
28    /// Compare one physical range family using its already selected endpoint operation. The caller retains operator-class, collation and invocation state; range identity never selects a native canonicalizer implicitly.
29    pub(crate) fn cmp_by_with_control(
30        &self,
31        other: &Self,
32        control: &ProductionControl<'_>,
33        mut compare: impl FnMut(&Value, &Value, &ProductionControl<'_>) -> Result<Ordering>,
34    ) -> Result<Ordering> {
35        control.check()?;
36        if self.subtype != other.subtype {
37            return Err(SQLError::Routine {
38                sqlstate: "XX000".into(),
39                message: "range types do not match".into(),
40            });
41        }
42        match (self.empty, other.empty) {
43            (true, true) => return Ok(Ordering::Equal),
44            (true, false) => return Ok(Ordering::Less),
45            (false, true) => return Ok(Ordering::Greater),
46            (false, false) => {}
47        }
48        let lower = compare_lower_bounds_by(self, other, control, &mut compare)?;
49        if !lower.is_eq() {
50            return Ok(lower);
51        }
52        Ok(match (&self.upper, &other.upper) {
53            (None, None) => Ordering::Equal,
54            (None, Some(_)) => Ordering::Greater,
55            (Some(_), None) => Ordering::Less,
56            (Some(left), Some(right)) => compare(left, right, control)?
57                .then_with(|| self.upper_inclusive.cmp(&other.upper_inclusive)),
58        })
59    }
60}
61
62impl CanonicalMultirange {
63    /// Compare normalized members in order; an equal prefix places the shorter multirange first.
64    pub fn cmp_with_control(
65        &self,
66        other: &Self,
67        control: &ProductionControl<'_>,
68    ) -> Result<Ordering> {
69        let semantics = RangeSemantics::native(self.subtype);
70        self.cmp_by_with_control(other, control, |left, right, control| {
71            semantics.compare(left, right, control)
72        })
73    }
74}
75
76impl<I: PartialEq> CanonicalMultirange<I> {
77    pub(crate) fn cmp_by_with_control(
78        &self,
79        other: &Self,
80        control: &ProductionControl<'_>,
81        mut compare: impl FnMut(&Value, &Value, &ProductionControl<'_>) -> Result<Ordering>,
82    ) -> Result<Ordering> {
83        control.check()?;
84        if self.subtype != other.subtype {
85            return Err(SQLError::Routine {
86                sqlstate: "XX000".into(),
87                message: "multirange types do not match".into(),
88            });
89        }
90        for (left, right) in self.ranges.iter().zip(&other.ranges) {
91            let order = left.cmp_by_with_control(right, control, &mut compare)?;
92            if !order.is_eq() {
93                return Ok(order);
94            }
95        }
96        Ok(self.ranges.len().cmp(&other.ranges.len()))
97    }
98}
99
100pub(super) fn compare_lower_bounds_by<I>(
101    left: &CanonicalRange<I>,
102    right: &CanonicalRange<I>,
103    control: &ProductionControl<'_>,
104    mut compare: impl FnMut(&Value, &Value, &ProductionControl<'_>) -> Result<Ordering>,
105) -> Result<Ordering> {
106    control.check()?;
107    Ok(match (left.lower(), right.lower()) {
108        (None, None) => Ordering::Equal,
109        (None, Some(_)) => Ordering::Less,
110        (Some(_), None) => Ordering::Greater,
111        (Some(left_value), Some(right_value)) => compare(left_value, right_value, control)?
112            .then_with(|| right.lower_inclusive.cmp(&left.lower_inclusive)),
113    })
114}
115
116#[cfg(test)]
117mod tests;