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 unique_constraints: Vec<Vec<String>>,
255    pub if_not_exists: bool,
256    pub immutable: bool,
257    pub state_machine: Option<StateMachineDef>,
258    pub dag_edge_types: Vec<String>,
259    pub propagation_rules: Vec<AstPropagationRule>,
260    pub retain: Option<RetainOption>,
261}
262
263#[derive(Debug, Clone)]
264pub struct RetainOption {
265    pub duration_seconds: u64,
266    pub sync_safe: bool,
267}
268
269#[derive(Debug, Clone)]
270pub struct AlterTable {
271    pub table: String,
272    pub action: AlterAction,
273}
274
275#[derive(Debug, Clone)]
276pub enum AlterAction {
277    AddColumn(ColumnDef),
278    DropColumn(String),
279    RenameColumn {
280        from: String,
281        to: String,
282    },
283    SetRetain {
284        duration_seconds: u64,
285        sync_safe: bool,
286    },
287    DropRetain,
288    SetSyncConflictPolicy(String),
289    DropSyncConflictPolicy,
290}
291
292#[derive(Debug, Clone)]
293pub struct StateMachineDef {
294    pub column: String,
295    pub transitions: Vec<(String, Vec<String>)>,
296}
297
298#[derive(Debug, Clone)]
299pub struct DropTable {
300    pub name: String,
301    pub if_exists: bool,
302}
303
304#[derive(Debug, Clone)]
305pub struct CreateIndex {
306    pub name: String,
307    pub table: String,
308    pub columns: Vec<String>,
309}
310
311#[derive(Debug, Clone)]
312pub struct ColumnDef {
313    pub name: String,
314    pub data_type: DataType,
315    pub nullable: bool,
316    pub primary_key: bool,
317    pub unique: bool,
318    pub default: Option<Expr>,
319    pub references: Option<ForeignKey>,
320    pub expires: bool,
321}
322
323#[derive(Debug, Clone)]
324pub struct ForeignKey {
325    pub table: String,
326    pub column: String,
327    pub propagation_rules: Vec<AstPropagationRule>,
328}
329
330#[derive(Debug, Clone)]
331pub enum AstPropagationRule {
332    FkState {
333        trigger_state: String,
334        target_state: String,
335        max_depth: Option<u32>,
336        abort_on_failure: bool,
337    },
338    EdgeState {
339        edge_type: String,
340        direction: String,
341        trigger_state: String,
342        target_state: String,
343        max_depth: Option<u32>,
344        abort_on_failure: bool,
345    },
346    VectorExclusion {
347        trigger_state: String,
348    },
349}
350
351#[derive(Debug, Clone)]
352pub enum DataType {
353    Uuid,
354    Text,
355    Integer,
356    Real,
357    Boolean,
358    Timestamp,
359    Json,
360    Vector(u32),
361}
362
363#[derive(Debug, Clone)]
364pub struct Delete {
365    pub table: String,
366    pub where_clause: Option<Expr>,
367}
368
369#[derive(Debug, Clone)]
370pub struct Update {
371    pub table: String,
372    pub assignments: Vec<(String, Expr)>,
373    pub where_clause: Option<Expr>,
374}