Skip to main content

alopex_sql/ast/
expr.rs

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