Skip to main content

contextdb_parser/
ast.rs

1#[derive(Debug, Clone)]
2pub enum Statement {
3    CreateTable(CreateTable),
4    AlterTable(AlterTable),
5    DropTable(DropTable),
6    CreateIndex(CreateIndex),
7    DropIndex(DropIndex),
8    Insert(Insert),
9    Delete(Delete),
10    Update(Update),
11    Select(SelectStatement),
12    Begin,
13    Commit,
14    Rollback,
15    SetMemoryLimit(SetMemoryLimitValue),
16    ShowMemoryLimit,
17    SetDiskLimit(SetDiskLimitValue),
18    ShowDiskLimit,
19    SetSyncConflictPolicy(String),
20    ShowSyncConflictPolicy,
21    ShowVectorIndexes,
22}
23
24#[derive(Debug, Clone)]
25pub enum SetMemoryLimitValue {
26    Bytes(usize),
27    None,
28}
29
30#[derive(Debug, Clone)]
31pub enum SetDiskLimitValue {
32    Bytes(u64),
33    None,
34}
35
36#[derive(Debug, Clone)]
37pub struct SelectStatement {
38    pub ctes: Vec<Cte>,
39    pub body: SelectBody,
40}
41
42#[derive(Debug, Clone)]
43pub enum Cte {
44    SqlCte {
45        name: String,
46        query: SelectBody,
47    },
48    MatchCte {
49        name: String,
50        match_clause: MatchClause,
51    },
52}
53
54#[derive(Debug, Clone)]
55pub struct MatchClause {
56    pub graph_name: Option<String>,
57    pub pattern: GraphPattern,
58    pub where_clause: Option<Expr>,
59    pub return_cols: Vec<ReturnCol>,
60}
61
62#[derive(Debug, Clone)]
63pub struct ReturnCol {
64    pub expr: Expr,
65    pub alias: Option<String>,
66}
67
68#[derive(Debug, Clone)]
69pub struct GraphPattern {
70    pub start: NodePattern,
71    pub edges: Vec<EdgeStep>,
72}
73
74#[derive(Debug, Clone)]
75pub struct NodePattern {
76    pub alias: String,
77    pub label: Option<String>,
78    pub properties: Vec<(String, Expr)>,
79}
80
81#[derive(Debug, Clone)]
82pub struct EdgeStep {
83    pub direction: EdgeDirection,
84    pub edge_type: Option<String>,
85    pub min_hops: u32,
86    pub max_hops: u32,
87    pub alias: Option<String>,
88    pub target: NodePattern,
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
92pub enum EdgeDirection {
93    Outgoing,
94    Incoming,
95    Both,
96}
97
98#[derive(Debug, Clone)]
99pub struct SelectBody {
100    pub distinct: bool,
101    pub columns: Vec<SelectColumn>,
102    pub from: Vec<FromItem>,
103    pub joins: Vec<JoinClause>,
104    pub where_clause: Option<Expr>,
105    pub order_by: Vec<OrderByItem>,
106    pub use_rank: Option<String>,
107    pub limit: Option<u64>,
108}
109
110#[derive(Debug, Clone)]
111pub struct SelectColumn {
112    pub expr: Expr,
113    pub alias: Option<String>,
114}
115
116#[derive(Debug, Clone)]
117pub enum FromItem {
118    Table {
119        name: String,
120        alias: Option<String>,
121    },
122    GraphTable {
123        graph_name: String,
124        match_clause: MatchClause,
125        columns: Vec<GraphTableColumn>,
126    },
127}
128
129#[derive(Debug, Clone)]
130pub struct GraphTableColumn {
131    pub expr: Expr,
132    pub alias: String,
133}
134
135#[derive(Debug, Clone)]
136pub struct JoinClause {
137    pub join_type: JoinType,
138    pub table: String,
139    pub alias: Option<String>,
140    pub on: Expr,
141}
142
143#[derive(Debug, Clone, Copy, PartialEq, Eq)]
144pub enum JoinType {
145    Inner,
146    Left,
147}
148
149#[derive(Debug, Clone)]
150pub struct OrderByItem {
151    pub expr: Expr,
152    pub direction: SortDirection,
153}
154
155#[derive(Debug, Clone, Copy, PartialEq, Eq)]
156pub enum SortDirection {
157    Asc,
158    Desc,
159    CosineDistance,
160}
161
162#[derive(Debug, Clone)]
163pub enum Expr {
164    Column(ColumnRef),
165    Literal(Literal),
166    Parameter(String),
167    BinaryOp {
168        left: Box<Expr>,
169        op: BinOp,
170        right: Box<Expr>,
171    },
172    UnaryOp {
173        op: UnaryOp,
174        operand: Box<Expr>,
175    },
176    FunctionCall {
177        name: String,
178        args: Vec<Expr>,
179    },
180    InList {
181        expr: Box<Expr>,
182        list: Vec<Expr>,
183        negated: bool,
184    },
185    Like {
186        expr: Box<Expr>,
187        pattern: Box<Expr>,
188        negated: bool,
189    },
190    InSubquery {
191        expr: Box<Expr>,
192        subquery: Box<SelectBody>,
193        negated: bool,
194    },
195    IsNull {
196        expr: Box<Expr>,
197        negated: bool,
198    },
199    CosineDistance {
200        left: Box<Expr>,
201        right: Box<Expr>,
202    },
203}
204
205#[derive(Debug, Clone)]
206pub struct ColumnRef {
207    pub table: Option<String>,
208    pub column: String,
209}
210
211#[derive(Debug, Clone)]
212pub enum Literal {
213    Null,
214    Bool(bool),
215    Integer(i64),
216    Real(f64),
217    Text(String),
218    Vector(Vec<f32>),
219}
220
221#[derive(Debug, Clone, Copy, PartialEq, Eq)]
222pub enum BinOp {
223    Eq,
224    Neq,
225    Lt,
226    Lte,
227    Gt,
228    Gte,
229    And,
230    Or,
231}
232
233#[derive(Debug, Clone, Copy, PartialEq, Eq)]
234pub enum UnaryOp {
235    Not,
236    Neg,
237}
238
239#[derive(Debug, Clone)]
240pub struct Insert {
241    pub table: String,
242    pub columns: Vec<String>,
243    pub values: Vec<Vec<Expr>>,
244    pub on_conflict: Option<OnConflict>,
245}
246
247#[derive(Debug, Clone)]
248pub struct OnConflict {
249    pub columns: Vec<String>,
250    pub update_columns: Vec<(String, Expr)>,
251}
252
253#[derive(Debug, Clone)]
254pub struct CreateTable {
255    pub name: String,
256    pub columns: Vec<ColumnDef>,
257    pub unique_constraints: Vec<Vec<String>>,
258    pub if_not_exists: bool,
259    pub immutable: bool,
260    pub state_machine: Option<StateMachineDef>,
261    pub dag_edge_types: Vec<String>,
262    pub propagation_rules: Vec<AstPropagationRule>,
263    pub retain: Option<RetainOption>,
264}
265
266#[derive(Debug, Clone)]
267pub struct RetainOption {
268    pub duration_seconds: u64,
269    pub sync_safe: bool,
270}
271
272#[derive(Debug, Clone)]
273pub struct AlterTable {
274    pub table: String,
275    pub action: AlterAction,
276}
277
278#[derive(Debug, Clone)]
279pub enum AlterAction {
280    AddColumn(ColumnDef),
281    DropColumn {
282        column: String,
283        cascade: bool,
284    },
285    RenameColumn {
286        from: String,
287        to: String,
288    },
289    SetRetain {
290        duration_seconds: u64,
291        sync_safe: bool,
292    },
293    DropRetain,
294    SetSyncConflictPolicy(String),
295    DropSyncConflictPolicy,
296}
297
298#[derive(Debug, Clone)]
299pub struct StateMachineDef {
300    pub column: String,
301    pub transitions: Vec<(String, Vec<String>)>,
302}
303
304#[derive(Debug, Clone)]
305pub struct DropTable {
306    pub name: String,
307    pub if_exists: bool,
308}
309
310#[derive(Debug, Clone)]
311pub struct CreateIndex {
312    pub name: String,
313    pub table: String,
314    pub columns: Vec<(String, SortDirection)>,
315}
316
317#[derive(Debug, Clone)]
318pub struct DropIndex {
319    pub name: String,
320    pub table: String,
321    pub if_exists: bool,
322}
323
324#[derive(Debug, Clone)]
325pub struct ColumnDef {
326    pub name: String,
327    pub data_type: DataType,
328    pub nullable: bool,
329    pub primary_key: bool,
330    pub unique: bool,
331    pub default: Option<Expr>,
332    pub references: Option<ForeignKey>,
333    pub expires: bool,
334    pub immutable: bool,
335    pub quantization: VectorQuantization,
336    pub rank_policy: Option<Box<RankPolicyAst>>,
337}
338
339#[derive(Debug, Clone, PartialEq, Eq)]
340pub struct RankPolicyAst {
341    pub joined_table: String,
342    pub joined_column: String,
343    pub sort_key: String,
344    pub formula: String,
345}
346
347#[derive(Debug, Clone)]
348pub struct ForeignKey {
349    pub table: String,
350    pub column: String,
351    pub propagation_rules: Vec<AstPropagationRule>,
352}
353
354#[derive(Debug, Clone)]
355pub enum AstPropagationRule {
356    FkState {
357        trigger_state: String,
358        target_state: String,
359        max_depth: Option<u32>,
360        abort_on_failure: bool,
361    },
362    EdgeState {
363        edge_type: String,
364        direction: String,
365        trigger_state: String,
366        target_state: String,
367        max_depth: Option<u32>,
368        abort_on_failure: bool,
369    },
370    VectorExclusion {
371        trigger_state: String,
372    },
373}
374
375#[derive(Debug, Clone, PartialEq, Eq)]
376pub enum DataType {
377    Uuid,
378    Text,
379    Integer,
380    Real,
381    Boolean,
382    Timestamp,
383    Json,
384    Vector(u32),
385    TxId,
386}
387
388#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
389pub enum VectorQuantization {
390    #[default]
391    F32,
392    SQ8,
393    SQ4,
394}
395
396#[derive(Debug, Clone)]
397pub struct Delete {
398    pub table: String,
399    pub where_clause: Option<Expr>,
400}
401
402#[derive(Debug, Clone)]
403pub struct Update {
404    pub table: String,
405    pub assignments: Vec<(String, Expr)>,
406    pub where_clause: Option<Expr>,
407}