Skip to main content

uqa_sql/catalog/index/operator_classes/btree/
binding.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! A selected class retains the declaration's type while checking the class's binary input contract.
8
9use super::BuiltinBTreeOperatorClass;
10use crate::{
11    catalog::type_metadata,
12    type_resolution::{cast_catalog_entry_with_control, CastMethod},
13    ColumnType, SQLError,
14};
15use uqa_core::memory::ProductionControl;
16
17/// Checked class input compatibility, independent of comparator availability and the subtype's default class. Binding neither copies the declaration nor invokes type input or domain checks.
18#[derive(Debug, Clone, Copy)]
19pub struct BoundBTreeOperatorClass<'a> {
20    class: &'static BuiltinBTreeOperatorClass,
21    declared_type: &'a ColumnType,
22}
23
24impl BuiltinBTreeOperatorClass {
25    /// Bind an explicitly selected class as `PostgreSQL`'s `IsBinaryCoercible` check does. The caller supplies the selected name's diagnostic spelling, including any schema qualifier. Function and assignment-only casts do not make an input acceptable.
26    pub fn bind_input<'a>(
27        &'static self,
28        declared_type: &'a ColumnType,
29        selected_name: &str,
30        control: &ProductionControl<'_>,
31    ) -> Result<BoundBTreeOperatorClass<'a>, SQLError> {
32        if !accepts_input(self.input_type_oid, declared_type, control)? {
33            let diagnostic_type = declared_type.without_type_modifiers_with_control(control)?;
34            return Err(SQLError::Routine {
35                sqlstate: "42804".into(),
36                message: format!(
37                    "operator class \"{}\" does not accept data type {}",
38                    selected_name,
39                    diagnostic_type.display_name()
40                ),
41            });
42        }
43        Ok(BoundBTreeOperatorClass {
44            class: self,
45            declared_type,
46        })
47    }
48}
49
50impl<'a> BoundBTreeOperatorClass<'a> {
51    #[must_use]
52    pub const fn class(self) -> &'static BuiltinBTreeOperatorClass {
53        self.class
54    }
55
56    #[must_use]
57    pub const fn declared_type(self) -> &'a ColumnType {
58        self.declared_type
59    }
60}
61
62fn accepts_input(
63    input_oid: u32,
64    mut source: &ColumnType,
65    control: &ProductionControl<'_>,
66) -> Result<bool, SQLError> {
67    control.check()?;
68    while let ColumnType::Domain { base, .. } = source {
69        control.check()?;
70        source = base;
71    }
72    if matches!(source, ColumnType::Named(_)) {
73        return Err(SQLError::Internal(
74            "unresolved type reached operator class input binding".into(),
75        ));
76    }
77    if type_metadata::pg_type_oid(source) == i64::from(input_oid) {
78        return Ok(true);
79    }
80    if let Some(generic) = type_metadata::routine_pseudo_type_name(i64::from(input_oid)) {
81        return Ok(match generic {
82            "anyarray" => matches!(
83                source,
84                ColumnType::Array(_) | ColumnType::Int2Vector | ColumnType::OidVector
85            ),
86            "anyenum" => matches!(source, ColumnType::Enum(_)),
87            "anyrange" => matches!(source, ColumnType::Range(_) | ColumnType::DeclaredRange(_)),
88            "anymultirange" => matches!(
89                source,
90                ColumnType::Multirange(_) | ColumnType::DeclaredMultirange(_)
91            ),
92            "record" => matches!(source, ColumnType::Composite(_)),
93            _ => false,
94        });
95    }
96    let Some(target) = type_metadata::builtin_scalar_type(input_oid) else {
97        return Ok(false);
98    };
99    if matches!(target, ColumnType::Record) {
100        return Ok(matches!(source, ColumnType::Composite(_)));
101    }
102    Ok(cast_catalog_entry_with_control(source, target, control)?
103        .is_some_and(|cast| cast.context == b'i' && cast.method == CastMethod::Binary))
104}
105
106#[cfg(test)]
107mod tests;