Skip to main content

nodedb_query/scan_filter/
op.rs

1//! `FilterOp` enum and its string/serde conversions.
2//!
3//! `FilterOp` is an O(1)-dispatch discriminant used by the scan filter
4//! evaluator. On-wire it travels as a lowercase string tag so physical
5//! plans remain debuggable by hand.
6
7/// Filter operator enum for O(1) dispatch instead of string comparison.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum FilterOp {
10    Eq,
11    Ne,
12    Gt,
13    Gte,
14    Lt,
15    Lte,
16    Contains,
17    Like,
18    NotLike,
19    Ilike,
20    NotIlike,
21    In,
22    NotIn,
23    IsNull,
24    IsNotNull,
25    ArrayContains,
26    ArrayContainsAll,
27    ArrayOverlap,
28    #[default]
29    MatchAll,
30    Exists,
31    NotExists,
32    Or,
33    /// Arbitrary expression predicate: the filter's `expr` field holds a
34    /// `nodedb_query::expr::SqlExpr`. The scan evaluator runs the expression
35    /// against the full row and treats truthy results as a match. Used when
36    /// the planner cannot reduce the WHERE clause to a simple `(field, op, value)`
37    /// — e.g. `LOWER(col) = 'x'`, `qty + 1 = 5`, `NOT (col = 'x')`.
38    Expr,
39    /// Column-vs-column comparison: `field` op `value` where `value` is a
40    /// `Value::String` containing the name of the other column. The comparison
41    /// reads both fields from the same document row.
42    GtColumn,
43    GteColumn,
44    LtColumn,
45    LteColumn,
46    EqColumn,
47    NeColumn,
48}
49
50impl FilterOp {
51    pub fn parse_op(s: &str) -> Self {
52        match s {
53            "eq" => Self::Eq,
54            "ne" | "neq" => Self::Ne,
55            "gt" => Self::Gt,
56            "gte" | "ge" => Self::Gte,
57            "lt" => Self::Lt,
58            "lte" | "le" => Self::Lte,
59            "contains" => Self::Contains,
60            "like" => Self::Like,
61            "not_like" => Self::NotLike,
62            "ilike" => Self::Ilike,
63            "not_ilike" => Self::NotIlike,
64            "in" => Self::In,
65            "not_in" => Self::NotIn,
66            "is_null" => Self::IsNull,
67            "is_not_null" => Self::IsNotNull,
68            "array_contains" => Self::ArrayContains,
69            "array_contains_all" => Self::ArrayContainsAll,
70            "array_overlap" => Self::ArrayOverlap,
71            "match_all" => Self::MatchAll,
72            "exists" => Self::Exists,
73            "not_exists" => Self::NotExists,
74            "or" => Self::Or,
75            "expr" => Self::Expr,
76            "gt_col" => Self::GtColumn,
77            "gte_col" => Self::GteColumn,
78            "lt_col" => Self::LtColumn,
79            "lte_col" => Self::LteColumn,
80            "eq_col" => Self::EqColumn,
81            "ne_col" => Self::NeColumn,
82            _ => Self::MatchAll,
83        }
84    }
85
86    pub fn as_str(&self) -> &'static str {
87        match self {
88            Self::Eq => "eq",
89            Self::Ne => "ne",
90            Self::Gt => "gt",
91            Self::Gte => "gte",
92            Self::Lt => "lt",
93            Self::Lte => "lte",
94            Self::Contains => "contains",
95            Self::Like => "like",
96            Self::NotLike => "not_like",
97            Self::Ilike => "ilike",
98            Self::NotIlike => "not_ilike",
99            Self::In => "in",
100            Self::NotIn => "not_in",
101            Self::IsNull => "is_null",
102            Self::IsNotNull => "is_not_null",
103            Self::ArrayContains => "array_contains",
104            Self::ArrayContainsAll => "array_contains_all",
105            Self::ArrayOverlap => "array_overlap",
106            Self::MatchAll => "match_all",
107            Self::Exists => "exists",
108            Self::NotExists => "not_exists",
109            Self::Or => "or",
110            Self::Expr => "expr",
111            Self::GtColumn => "gt_col",
112            Self::GteColumn => "gte_col",
113            Self::LtColumn => "lt_col",
114            Self::LteColumn => "lte_col",
115            Self::EqColumn => "eq_col",
116            Self::NeColumn => "ne_col",
117        }
118    }
119}
120
121impl From<&str> for FilterOp {
122    fn from(s: &str) -> Self {
123        Self::parse_op(s)
124    }
125}
126
127impl From<String> for FilterOp {
128    fn from(s: String) -> Self {
129        Self::parse_op(&s)
130    }
131}
132
133impl serde::Serialize for FilterOp {
134    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
135        serializer.serialize_str(self.as_str())
136    }
137}
138
139impl<'de> serde::Deserialize<'de> for FilterOp {
140    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
141        let s = String::deserialize(deserializer)?;
142        Ok(FilterOp::parse_op(&s))
143    }
144}