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