Skip to main content

alopex_sql/ast/
expr.rs

1use super::ddl::DataType;
2use super::span::{Span, Spanned};
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Expr {
7    pub kind: ExprKind,
8    pub span: Span,
9}
10
11impl Expr {
12    pub fn new(kind: ExprKind, span: Span) -> Self {
13        Self { kind, span }
14    }
15}
16
17impl Spanned for Expr {
18    fn span(&self) -> Span {
19        self.span
20    }
21}
22
23#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(tag = "variant")]
25#[allow(clippy::large_enum_variant)]
26pub enum ExprKind {
27    Literal {
28        literal: Literal,
29    },
30    ColumnRef {
31        table: Option<String>,
32        column: String,
33    },
34    BinaryOp {
35        left: Box<Expr>,
36        op: BinaryOp,
37        right: Box<Expr>,
38    },
39    UnaryOp {
40        op: UnaryOp,
41        operand: Box<Expr>,
42    },
43    FunctionCall {
44        name: String,
45        args: Vec<Expr>,
46        distinct: bool,
47        star: bool,
48    },
49    Cast {
50        expr: Box<Expr>,
51        target_type: DataType,
52    },
53    Between {
54        expr: Box<Expr>,
55        low: Box<Expr>,
56        high: Box<Expr>,
57        negated: bool,
58    },
59    Like {
60        expr: Box<Expr>,
61        pattern: Box<Expr>,
62        escape: Option<Box<Expr>>,
63        negated: bool,
64        #[serde(default)]
65        kind: PatternMatchKind,
66    },
67    InList {
68        expr: Box<Expr>,
69        list: Vec<Expr>,
70        negated: bool,
71    },
72    IsNull {
73        expr: Box<Expr>,
74        negated: bool,
75    },
76    VectorLiteral {
77        values: Vec<f64>,
78    },
79    ScalarSubquery {
80        subquery: Box<super::Statement>,
81    },
82    InSubquery {
83        expr: Box<Expr>,
84        subquery: Box<super::Statement>,
85        negated: bool,
86    },
87    Exists {
88        subquery: Box<super::Statement>,
89        negated: bool,
90    },
91    Quantified {
92        expr: Box<Expr>,
93        op: BinaryOp,
94        quantifier: Quantifier,
95        subquery: Box<super::Statement>,
96    },
97}
98
99#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
100pub enum PatternMatchKind {
101    #[default]
102    Like,
103    ILike,
104    Glob,
105    SimilarTo,
106}
107
108#[derive(Debug, Clone, Serialize, Deserialize)]
109#[serde(tag = "variant", content = "value")]
110pub enum Literal {
111    Number(String),
112    String(String),
113    Boolean(bool),
114    Null,
115    /// SQL-TS interval text, preserved for a downstream semantic layer.
116    ///
117    /// Appended to preserve existing bincode discriminants.
118    Interval(String),
119}
120
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
122pub enum BinaryOp {
123    Add,
124    Sub,
125    Mul,
126    Div,
127    Mod,
128    Eq,
129    Neq,
130    Lt,
131    Gt,
132    LtEq,
133    GtEq,
134    And,
135    Or,
136    StringConcat,
137}
138
139#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
140pub enum UnaryOp {
141    Not,
142    Minus,
143}
144
145#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
146pub enum Quantifier {
147    Any,
148    All,
149}