Skip to main content

nodedb_query/expr/
types.rs

1//! SqlExpr AST node definitions.
2
3use nodedb_types::Value;
4
5/// A serializable SQL expression that can be evaluated against a document.
6#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
7pub enum SqlExpr {
8    /// Column reference: extract field value from the document.
9    Column(String),
10    /// Literal value.
11    Literal(Value),
12    /// Binary operation: left op right.
13    BinaryOp {
14        left: Box<SqlExpr>,
15        op: BinaryOp,
16        right: Box<SqlExpr>,
17    },
18    /// Unary negation: -expr or NOT expr.
19    Negate(Box<SqlExpr>),
20    /// Scalar function call.
21    Function { name: String, args: Vec<SqlExpr> },
22    /// CAST(expr AS type).
23    Cast {
24        expr: Box<SqlExpr>,
25        to_type: CastType,
26    },
27    /// CASE WHEN cond1 THEN val1 ... ELSE default END.
28    Case {
29        operand: Option<Box<SqlExpr>>,
30        when_thens: Vec<(SqlExpr, SqlExpr)>,
31        else_expr: Option<Box<SqlExpr>>,
32    },
33    /// COALESCE(expr1, expr2, ...): first non-null value.
34    Coalesce(Vec<SqlExpr>),
35    /// NULLIF(expr1, expr2): returns NULL if expr1 = expr2, else expr1.
36    NullIf(Box<SqlExpr>, Box<SqlExpr>),
37    /// IS NULL / IS NOT NULL.
38    IsNull { expr: Box<SqlExpr>, negated: bool },
39    /// OLD column reference: extract field value from the pre-update document.
40    /// Used in TRANSITION CHECK predicates. Resolves against the OLD row
41    /// when evaluated via `eval_with_old()`. Returns NULL in normal `eval()`.
42    OldColumn(String),
43    /// `EXCLUDED.col` reference from `INSERT ... ON CONFLICT DO UPDATE`:
44    /// the column value from the row proposed for insertion that
45    /// triggered the conflict. Resolves against the incoming row when
46    /// evaluated via `eval_with_excluded()`. Returns NULL in plain
47    /// `eval()` / `eval_with_old()`.
48    ExcludedColumn(String),
49}
50
51/// Binary operators.
52#[derive(
53    Debug,
54    Clone,
55    Copy,
56    PartialEq,
57    Eq,
58    serde::Serialize,
59    serde::Deserialize,
60    zerompk::ToMessagePack,
61    zerompk::FromMessagePack,
62)]
63#[msgpack(c_enum)]
64pub enum BinaryOp {
65    Add,
66    Sub,
67    Mul,
68    Div,
69    Mod,
70    Eq,
71    NotEq,
72    Gt,
73    GtEq,
74    Lt,
75    LtEq,
76    And,
77    Or,
78    Concat,
79}
80
81/// Target types for CAST.
82#[derive(
83    Debug,
84    Clone,
85    PartialEq,
86    Eq,
87    serde::Serialize,
88    serde::Deserialize,
89    zerompk::ToMessagePack,
90    zerompk::FromMessagePack,
91)]
92#[msgpack(c_enum)]
93pub enum CastType {
94    Int,
95    Float,
96    String,
97    Bool,
98}
99
100/// A computed projection column: alias + expression.
101#[derive(
102    Debug,
103    Clone,
104    serde::Serialize,
105    serde::Deserialize,
106    zerompk::ToMessagePack,
107    zerompk::FromMessagePack,
108)]
109pub struct ComputedColumn {
110    pub alias: String,
111    pub expr: SqlExpr,
112}