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