Skip to main content

uqa_sql/expr/
collation.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Deterministic built-in Unicode comparison owns decomposition and admitted scratch in SQL.
8
9mod combining;
10mod data;
11mod elements;
12mod mapping;
13mod stream;
14
15use crate::SQLError;
16use elements::{
17    CollationElement, CE_BUFFER_SIZE, NO_CE_PRIMARY, NO_CE_SECONDARY, NO_CE_TERTIARY, TERTIARY_MASK,
18};
19use icu_collator::provider::{Baked, CollationDiacriticsV1};
20use icu_provider::{DataProvider, DataRequest};
21use std::cmp::Ordering;
22use stream::CollationElements;
23use uqa_core::{
24    compare_bytes_with_control,
25    memory::{ProductionControl, ProductionSmallVec},
26};
27
28/// Compare the deterministic `PostgreSQL` `unicode` built-in (root ICU, tertiary strength, normalization checking off). Byte-distinct strings retain distinct equality classes even when their Unicode weights match.
29pub fn compare_unicode_with_control(
30    left: &str,
31    right: &str,
32    control: &ProductionControl<'_>,
33) -> Result<Ordering, SQLError> {
34    let byte_order = compare_bytes_with_control(left.as_bytes(), right.as_bytes(), control)?;
35    if byte_order.is_eq() {
36        return Ok(byte_order);
37    }
38    let diacritics =
39        <Baked as DataProvider<CollationDiacriticsV1>>::load(&Baked, DataRequest::default())
40            .map_err(|error| {
41                SQLError::Internal(format!("built-in Unicode collation data: {error}"))
42            })?
43            .payload;
44    let root = Baked::SINGLETON_COLLATION_ROOT_V1;
45    let jamo = Baked::SINGLETON_COLLATION_JAMO_V1
46        .ce32s
47        .as_ule_slice()
48        .try_into()
49        .map_err(|_| SQLError::Internal("built-in Unicode Jamo width".into()))?;
50    let decompositions = icu_normalizer::provider::Baked::SINGLETON_NORMALIZER_NFD_DATA_V1;
51    let tables = icu_normalizer::provider::Baked::SINGLETON_NORMALIZER_NFD_TABLES_V1;
52    let mut left = CollationElements::new(
53        left.chars(),
54        *control,
55        root,
56        jamo,
57        &diacritics.get().secondaries,
58        decompositions,
59        tables,
60    );
61    let mut right = CollationElements::new(
62        right.chars(),
63        *control,
64        root,
65        jamo,
66        &diacritics.get().secondaries,
67        decompositions,
68        tables,
69    );
70    left.init()?;
71    right.init()?;
72    let mut left_ces = ProductionSmallVec::<[CollationElement; CE_BUFFER_SIZE]>::new(*control);
73    let mut right_ces = ProductionSmallVec::<[CollationElement; CE_BUFFER_SIZE]>::new(*control);
74    loop {
75        control.check()?;
76        let left_primary = loop {
77            let ce = left.next()?;
78            left_ces.push(ce)?;
79            if ce.primary() != 0 {
80                break ce.primary();
81            }
82        };
83        let right_primary = loop {
84            let ce = right.next()?;
85            right_ces.push(ce)?;
86            if ce.primary() != 0 {
87                break ce.primary();
88            }
89        };
90        let order = left_primary.cmp(&right_primary);
91        if !order.is_eq() {
92            return Ok(order);
93        }
94        if left_primary == NO_CE_PRIMARY {
95            break;
96        }
97    }
98    let order = compare_secondary_and_tertiary(&left_ces, &right_ces, control)?;
99    control.check()?;
100    Ok(order.then(byte_order))
101}
102
103fn compare_secondary_and_tertiary(
104    left_ces: &[CollationElement],
105    right_ces: &[CollationElement],
106    control: &ProductionControl<'_>,
107) -> Result<Ordering, SQLError> {
108    for (weight, sentinel) in [
109        (
110            CollationElement::secondary as fn(CollationElement) -> u16,
111            NO_CE_SECONDARY,
112        ),
113        (tertiary, NO_CE_TERTIARY),
114    ] {
115        let mut left = left_ces.iter();
116        let mut right = right_ces.iter();
117        loop {
118            control.check()?;
119            let l = loop {
120                control.check()?;
121                let value = weight(*left.next().unwrap_or_default());
122                if value != 0 {
123                    break value;
124                }
125            };
126            let r = loop {
127                control.check()?;
128                let value = weight(*right.next().unwrap_or_default());
129                if value != 0 {
130                    break value;
131                }
132            };
133            let order = l.cmp(&r);
134            if !order.is_eq() {
135                return Ok(order);
136            }
137            if l == sentinel {
138                break;
139            }
140        }
141    }
142    control.check()?;
143    Ok(Ordering::Equal)
144}
145
146fn tertiary(ce: CollationElement) -> u16 {
147    ce.non_primary().tertiary_case_quarternary(TERTIARY_MASK)
148}
149
150#[cfg(test)]
151mod tests;