Skip to main content

icydb_core/db/predicate/
coercion.rs

1//! Module: predicate::coercion
2//! Responsibility: coercion identifiers/specs and family support matching.
3//! Does not own: predicate AST evaluation or schema literal validation.
4//! Boundary: consumed by predicate schema/semantics/runtime layers.
5
6use crate::value::CoercionFamily;
7use std::fmt;
8
9///
10/// CoercionId
11///
12/// Identifier for an explicit comparison coercion policy.
13///
14/// Coercions express *how* values may be compared, not whether a comparison
15/// is valid for a given field. Validation and planning enforce legality.
16///
17
18#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
19pub enum CoercionId {
20    Strict,
21    NumericWiden,
22    TextCasefold,
23    CollectionElement,
24}
25
26impl CoercionId {
27    /// Stable tag used by plan hash encodings (fingerprint/continuation).
28    #[must_use]
29    pub const fn plan_hash_tag(self) -> u8 {
30        match self {
31            Self::Strict => 0x01,
32            Self::NumericWiden => 0x02,
33            Self::TextCasefold => 0x04,
34            Self::CollectionElement => 0x05,
35        }
36    }
37}
38
39///
40/// CoercionSpec
41///
42/// Fully-specified coercion policy for predicate comparisons.
43///
44
45#[derive(Clone, Eq, PartialEq)]
46pub struct CoercionSpec {
47    pub(crate) id: CoercionId,
48    pub(crate) params: Vec<(String, String)>,
49}
50
51impl CoercionSpec {
52    #[must_use]
53    pub const fn new(id: CoercionId) -> Self {
54        Self {
55            id,
56            params: Vec::new(),
57        }
58    }
59
60    /// Return the canonical coercion identifier.
61    #[must_use]
62    pub const fn id(&self) -> CoercionId {
63        self.id
64    }
65
66    /// Borrow any attached coercion parameters.
67    #[must_use]
68    pub const fn params(&self) -> &[(String, String)] {
69        self.params.as_slice()
70    }
71}
72
73impl fmt::Debug for CoercionSpec {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        f.debug_struct("CoercionSpec")
76            .field("id", &self.id)
77            .field("params", &CoercionParamsDebug(&self.params))
78            .finish()
79    }
80}
81
82impl Default for CoercionSpec {
83    fn default() -> Self {
84        Self::new(CoercionId::Strict)
85    }
86}
87
88// Keep verbose predicate diagnostics stable even though coercion params no
89// longer retain tree-map machinery internally.
90struct CoercionParamsDebug<'a>(&'a [(String, String)]);
91
92impl fmt::Debug for CoercionParamsDebug<'_> {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        let mut debug = f.debug_map();
95        for (key, value) in self.0 {
96            debug.entry(key, value);
97        }
98
99        debug.finish()
100    }
101}
102
103///
104/// CoercionRuleFamily
105///
106/// Rule-side matcher for coercion routing families.
107///
108
109#[derive(Clone, Copy, Debug, Eq, PartialEq)]
110pub(crate) enum CoercionRuleFamily {
111    Any,
112    Family(CoercionFamily),
113}
114
115///
116/// CoercionRule
117///
118/// Declarative coercion routing rule between value families.
119///
120
121#[derive(Clone, Copy, Debug, Eq, PartialEq)]
122pub(crate) struct CoercionRule {
123    left: CoercionRuleFamily,
124    right: CoercionRuleFamily,
125    id: CoercionId,
126}
127
128pub(crate) const COERCION_TABLE: &[CoercionRule] = &[
129    CoercionRule {
130        left: CoercionRuleFamily::Any,
131        right: CoercionRuleFamily::Any,
132        id: CoercionId::Strict,
133    },
134    CoercionRule {
135        left: CoercionRuleFamily::Family(CoercionFamily::Numeric),
136        right: CoercionRuleFamily::Family(CoercionFamily::Numeric),
137        id: CoercionId::NumericWiden,
138    },
139    CoercionRule {
140        left: CoercionRuleFamily::Family(CoercionFamily::Textual),
141        right: CoercionRuleFamily::Family(CoercionFamily::Textual),
142        id: CoercionId::TextCasefold,
143    },
144    CoercionRule {
145        left: CoercionRuleFamily::Any,
146        right: CoercionRuleFamily::Any,
147        id: CoercionId::CollectionElement,
148    },
149];
150
151/// Returns whether a coercion rule exists for the provided routing families.
152#[must_use]
153pub(in crate::db) fn supports_coercion(
154    left: CoercionFamily,
155    right: CoercionFamily,
156    id: CoercionId,
157) -> bool {
158    COERCION_TABLE.iter().any(|rule| {
159        rule.id == id && family_matches(rule.left, left) && family_matches(rule.right, right)
160    })
161}
162
163fn family_matches(rule: CoercionRuleFamily, value: CoercionFamily) -> bool {
164    match rule {
165        CoercionRuleFamily::Any => true,
166        CoercionRuleFamily::Family(expected) => expected == value,
167    }
168}