1use super::{
10 parse_multirange_with_control, parse_range_with_control, CanonicalMultirange, CanonicalRange,
11 Ordering, Produced, ProductionControl, Result, SQLError, Value,
12};
13use crate::{expr::SQLValueCatalog, ColumnType};
14
15pub enum RangeComparisonValue {
17 Range(Produced<CanonicalRange>),
18 Multirange(Produced<CanonicalMultirange>),
19 DeclaredRange(Produced<CanonicalRange<u32>>),
20 DeclaredMultirange(Produced<CanonicalMultirange<u32>>),
21}
22
23impl RangeComparisonValue {
24 pub fn declared_physical_oid(
26 value: &Value,
27 catalog: Option<&dyn SQLValueCatalog>,
28 ) -> Result<Option<u32>> {
29 let Value::Datum(datum) = value else {
30 return Ok(None);
31 };
32 let oid = datum.type_oid();
33 if crate::catalog::type_metadata::builtin_scalar_type(oid).is_some() {
34 return Ok(None);
35 }
36 Ok(catalog
37 .map(|catalog| catalog.value_range_type(oid))
38 .transpose()?
39 .flatten()
40 .map(|_| oid))
41 }
42
43 pub fn from_declared_with_control(
45 value: &Value,
46 oid: u32,
47 catalog: Option<&dyn SQLValueCatalog>,
48 control: &ProductionControl<'_>,
49 ) -> Result<Option<Self>> {
50 control.check()?;
51 if matches!(value, Value::Null) {
52 return Ok(None);
53 }
54 if let Some(ty) = crate::catalog::type_metadata::builtin_scalar_type(oid) {
55 return Self::from_type(value, ty, catalog, control);
56 }
57 let ty = catalog
58 .map(|catalog| catalog.value_type_by_oid_with_control(oid, control))
59 .transpose()?
60 .flatten();
61 match ty {
62 Some(ty) => Self::from_type(value, &ty, catalog, control),
63 None => Ok(None),
64 }
65 }
66
67 pub fn from_type_with_control(
69 value: &Value,
70 ty: &ColumnType,
71 catalog: Option<&dyn SQLValueCatalog>,
72 control: &ProductionControl<'_>,
73 ) -> Result<Option<Self>> {
74 control.check()?;
75 if matches!(value, Value::Null) {
76 return Ok(None);
77 }
78 Self::from_type(value, ty, catalog, control)
79 }
80
81 fn from_type(
82 value: &Value,
83 ty: &ColumnType,
84 catalog: Option<&dyn SQLValueCatalog>,
85 control: &ProductionControl<'_>,
86 ) -> Result<Option<Self>> {
87 control.check()?;
88 if matches!(
89 ty,
90 ColumnType::DeclaredRange(_) | ColumnType::DeclaredMultirange(_)
91 ) {
92 return match value {
93 Value::Datum(datum) => {
94 super::super::datums::range_comparison(datum, ty, catalog, control).map(Some)
95 }
96 _ => Err(SQLError::Internal(
97 "declared range comparison requires admitted physical bounds".into(),
98 )),
99 };
100 }
101 let (subtype, multirange) = match ty {
102 ColumnType::Range(subtype) => (*subtype, false),
103 ColumnType::Multirange(subtype) => (*subtype, true),
104 ColumnType::Domain { base, .. } => {
105 return Self::from_type(value, base, catalog, control)
106 }
107 _ => return Ok(None),
108 };
109 if let Value::Datum(datum) = value {
110 return super::super::datums::range_comparison(datum, ty, catalog, control).map(Some);
111 }
112 let Value::Str(text) = value else {
113 return Err(SQLError::Internal(
114 "selected range comparison requires an admitted range value".into(),
115 ));
116 };
117 Ok(Some(if multirange {
118 Self::Multirange(parse_multirange_with_control(text, subtype, control)?)
119 } else {
120 Self::Range(parse_range_with_control(text, subtype, control)?)
121 }))
122 }
123
124 pub fn cmp_with_control(
126 &self,
127 other: &Self,
128 control: &ProductionControl<'_>,
129 ) -> Result<Ordering> {
130 self.cmp_with_catalog_and_control(other, None, control)
131 }
132
133 pub fn cmp_with_catalog_and_control(
134 &self,
135 other: &Self,
136 catalog: Option<&dyn SQLValueCatalog>,
137 control: &ProductionControl<'_>,
138 ) -> Result<Ordering> {
139 let compare = |oid, left: &Value, right: &Value, control: &ProductionControl<'_>| {
140 catalog
141 .ok_or_else(|| {
142 SQLError::Internal("selected range comparison support is unavailable".into())
143 })?
144 .compare_range_bounds(oid, left, right, control)
145 };
146 match (self, other) {
147 (Self::Range(left), Self::Range(right)) => left.cmp_with_control(right, control),
148 (Self::Multirange(left), Self::Multirange(right)) => {
149 left.cmp_with_control(right, control)
150 }
151 (Self::DeclaredRange(left), Self::DeclaredRange(right)) => {
152 left.cmp_by_with_control(right, control, |a, b, control| {
153 compare(*left.identity(), a, b, control)
154 })
155 }
156 (Self::DeclaredMultirange(left), Self::DeclaredMultirange(right)) => left
157 .cmp_by_with_control(right, control, |a, b, control| {
158 compare(*left.identity(), a, b, control)
159 }),
160 _ => Err(SQLError::Routine {
161 sqlstate: "XX000".into(),
162 message: "range types do not match".into(),
163 }),
164 }
165 }
166}