Skip to main content

akar_binder/
bound_statement.rs

1//! Bound statement types — AST nodes after semantic analysis.
2
3use akar_catalog::IndexType;
4use akar_common::types::LogicalTypeID;
5use akar_parser::ast::{Expression, ExtensionAction, TransactionAction};
6use std::collections::HashMap;
7
8/// A bound statement after semantic analysis.
9#[derive(Debug, Clone)]
10pub enum BoundStatement {
11    BoundQuery(BoundQuery),
12    BoundStandaloneCall(BoundStandaloneCall),
13    BoundCreateNodeTable(BoundCreateNodeTable),
14    BoundCreateRelTable(BoundCreateRelTable),
15    BoundDropTable(BoundDropTable),
16    BoundCopyFrom(BoundCopyFrom),
17    BoundCopyTo(BoundCopyTo),
18    BoundAlterTable(BoundAlterTable),
19    BoundCreateVectorIndex(BoundCreateVectorIndex),
20    BoundCreateIndex(BoundCreateIndex),
21    BoundDropIndex(BoundDropIndex),
22    BoundUnion(BoundUnion),
23    BoundMerge(BoundMerge),
24    BoundCreateDml(BoundCreateDml),
25    BoundExplain(BoundExplain),
26    BoundCreateSequence(BoundCreateSequence),
27    BoundDropSequence(BoundDropSequence),
28    BoundCreateMacro(BoundCreateMacro),
29    BoundExportDatabase(BoundExportDatabase),
30    BoundImportDatabase(BoundImportDatabase),
31    BoundCreateFtsIndex(BoundCreateFtsIndex),
32    BoundAnalyze(BoundAnalyze),
33    BoundTransaction(BoundTransaction),
34    BoundExtension(BoundExtension),
35    BoundAttachDatabase(BoundAttachDatabase),
36    BoundDetachDatabase(BoundDetachDatabase),
37    BoundUseDatabase(BoundUseDatabase),
38    BoundLoadFrom(BoundLoadFrom),
39    BoundCreateType(BoundCreateType),
40    BoundCommentOnTable(BoundCommentOnTable),
41    BoundCreateGraph(BoundCreateGraph),
42    BoundUseGraph(BoundUseGraph),
43    BoundDropGraph(BoundDropGraph),
44}
45
46/// Bound TRANSACTION statement.
47#[derive(Debug, Clone)]
48pub struct BoundTransaction {
49    pub action: TransactionAction,
50}
51
52/// Bound EXTENSION management statement.
53#[derive(Debug, Clone)]
54pub struct BoundExtension {
55    pub action: ExtensionAction,
56    pub name: String,
57}
58
59/// Bound ATTACH DATABASE statement.
60#[derive(Debug, Clone)]
61pub struct BoundAttachDatabase {
62    pub path: String,
63    pub alias: String,
64    pub options: HashMap<String, String>,
65}
66
67/// Bound DETACH DATABASE statement.
68#[derive(Debug, Clone)]
69pub struct BoundDetachDatabase {
70    pub alias: String,
71}
72
73/// Bound USE DATABASE statement.
74#[derive(Debug, Clone)]
75pub struct BoundUseDatabase {
76    pub alias: String,
77}
78
79/// Bound LOAD FROM statement.
80#[derive(Debug, Clone)]
81pub struct BoundLoadFrom {
82    pub path: String,
83    pub options: HashMap<String, String>,
84}
85
86/// Bound ANALYZE statement.
87#[derive(Debug, Clone)]
88pub struct BoundAnalyze {
89    /// Table name to analyze, or None for all tables.
90    pub table_name: Option<String>,
91    /// Resolved table IDs for the tables to analyze.
92    pub table_ids: Vec<u64>,
93}
94
95/// COPY FROM statement — load data from a file into a table.
96#[derive(Debug, Clone)]
97pub struct BoundCopyFrom {
98    pub table_name: String,
99    pub table_id: u64,
100    pub file_path: String,
101    pub options: HashMap<String, String>,
102    /// Resolved column schema from the catalog (column names + types).
103    pub columns: Vec<akar_catalog::CatalogColumn>,
104}
105
106/// COPY TO statement — export query results to a file.
107#[derive(Debug, Clone)]
108pub struct BoundCopyTo {
109    pub file_path: String,
110    pub format: akar_parser::ast::CopyToFormat,
111    pub header: bool,
112    /// The inner query to execute.
113    pub query: BoundQuery,
114}
115
116/// A resolved variable in scope (from MATCH patterns).
117#[derive(Debug, Clone)]
118pub struct BoundVariable {
119    pub name: String,
120    pub table_id: u64,
121    pub label: Option<String>,
122    pub is_node: bool,
123}
124
125#[derive(Debug, Clone)]
126pub struct BoundQuery {
127    pub clauses: Vec<BoundClause>,
128    /// Variables in scope (accumulated across clauses).
129    pub variables: Vec<BoundVariable>,
130}
131
132#[derive(Debug, Clone)]
133pub enum BoundClause {
134    BoundMatch(BoundMatchClause),
135    BoundReturn(BoundReturnClause),
136    BoundWhere(BoundWhereClause),
137    BoundDelete(BoundDeleteClause),
138    BoundSet(BoundSetClause),
139    BoundOptionalMatch(BoundMatchClause),
140    BoundWith(BoundReturnClause),
141    BoundUnwind(BoundUnwindClause),
142    BoundForeach(BoundForeachClause),
143    BoundCreate(BoundMatchClause),
144    BoundMerge(BoundMerge),
145}
146
147#[derive(Debug, Clone)]
148pub struct BoundSetItem {
149    pub property: akar_parser::ast::Expression,
150    pub value: akar_parser::ast::Expression,
151    pub column_name: String,
152    pub column_idx: usize,
153    pub table_name: String,
154    pub table_id: u64,
155    pub is_node: bool,
156}
157
158#[derive(Debug, Clone)]
159pub struct BoundSetClause {
160    pub items: Vec<BoundSetItem>,
161}
162
163#[derive(Debug, Clone)]
164pub struct BoundDeleteItem {
165    pub expression: Expression,
166    pub table_name: String,
167    pub table_id: u64,
168    pub primary_key_column: String,
169    pub is_node: bool,
170}
171
172#[derive(Debug, Clone)]
173pub struct BoundDeleteClause {
174    pub detach: bool,
175    pub items: Vec<BoundDeleteItem>,
176}
177
178#[derive(Debug, Clone)]
179pub struct BoundUnwindClause {
180    pub expression: akar_parser::ast::Expression,
181    pub variable: String,
182}
183
184/// Bound EXPLAIN statement — wraps an inner bound statement.
185#[derive(Debug, Clone)]
186pub struct BoundExplain {
187    /// The inner bound statement to explain.
188    pub inner: Box<BoundStatement>,
189    /// The type of explain output.
190    pub explain_type: akar_parser::ast::ExplainType,
191}
192
193/// Bound FOREACH clause — iterate over list and execute sub-statements.
194#[derive(Debug, Clone)]
195pub struct BoundForeachClause {
196    pub variable: String,
197    pub expression: akar_parser::ast::Expression,
198    /// Bound sub-statements (CREATE, SET, DELETE) produced by bind_foreach.
199    pub sub_statements: Vec<BoundStatement>,
200}
201
202#[derive(Debug, Clone)]
203pub struct BoundMatchClause {
204    pub patterns: Vec<BoundPattern>,
205    /// New variables introduced by this MATCH clause.
206    pub new_variables: Vec<BoundVariable>,
207    /// Optional FTS query (from USING FTS INDEX clause).
208    pub fts_query: Option<BoundFtsQuery>,
209}
210
211/// A bound USING FTS INDEX query.
212#[derive(Debug, Clone)]
213pub struct BoundFtsQuery {
214    pub index_name: String,
215    pub query_string: String,
216    /// The source node table/column the index was created on (P52.39), used
217    /// to keep the Tantivy index in sync with live DML (catch-up + soft-delete
218    /// filtering, P104.2/P105.3).
219    pub table_name: String,
220    pub column_name: String,
221}
222
223#[derive(Debug, Clone)]
224pub struct BoundPattern {
225    pub node_variable: Option<String>,
226    pub node_label: Option<String>,
227    pub node_table_id: Option<u64>,
228    pub properties: Vec<(String, akar_parser::ast::Expression)>,
229    pub edge: Option<BoundEdgePattern>,
230}
231
232#[derive(Debug, Clone)]
233pub struct BoundEdgePattern {
234    pub variable: Option<String>,
235    pub label: Option<String>,
236    pub rel_table_id: Option<u64>,
237    pub direction: akar_parser::ast::EdgeDirection,
238    pub properties: Vec<(String, akar_parser::ast::Expression)>,
239    pub lower_bound: Option<u64>,
240    pub upper_bound: Option<u64>,
241}
242
243/// A bound expression with resolved type information.
244#[derive(Debug, Clone)]
245pub struct BoundExpression {
246    pub expression: Expression,
247    pub resolved_type: LogicalTypeID,
248    pub is_constant: bool,
249    /// Output column name override from `AS alias` in RETURN/WITH (P53.16).
250    pub alias: Option<String>,
251}
252
253#[derive(Debug, Clone)]
254pub struct BoundReturnClause {
255    pub expressions: Vec<BoundExpression>,
256    pub distinct: bool,
257    /// Bound ORDER BY items.
258    pub order_by: Option<Vec<BoundOrderByItem>>,
259    /// Bound LIMIT.
260    pub limit: Option<u64>,
261    /// Bound SKIP.
262    pub skip: Option<u64>,
263    /// Parameter name referenced by LIMIT (resolved to `limit` at substitution).
264    pub limit_param: Option<String>,
265    /// Parameter name referenced by SKIP (resolved to `skip` at substitution).
266    pub skip_param: Option<String>,
267}
268
269/// A bound sort item for ORDER BY.
270#[derive(Debug, Clone)]
271pub struct BoundOrderByItem {
272    pub expression: BoundExpression,
273    pub ascending: bool,
274}
275
276#[derive(Debug, Clone)]
277pub struct BoundWhereClause {
278    pub expression: BoundExpression,
279}
280
281// DDL
282#[derive(Debug, Clone)]
283pub struct BoundCreateNodeTable {
284    pub name: String,
285    pub columns: Vec<akar_catalog::CatalogColumn>,
286    pub primary_key: String,
287    pub if_not_exists: bool,
288}
289
290#[derive(Debug, Clone)]
291pub struct BoundCreateRelTable {
292    pub name: String,
293    pub from: String,
294    pub to: String,
295    pub src_table_id: u64,
296    pub dst_table_id: u64,
297    pub columns: Vec<akar_catalog::CatalogColumn>,
298    pub if_not_exists: bool,
299}
300
301#[derive(Debug, Clone)]
302pub struct BoundDropTable {
303    pub name: String,
304}
305
306#[derive(Debug, Clone)]
307pub struct BoundAlterTable {
308    pub table_name: String,
309    pub action: akar_parser::ast::AlterAction,
310}
311
312#[derive(Debug, Clone)]
313pub struct BoundUnion {
314    pub left: Box<BoundQuery>,
315    pub right: Box<BoundQuery>,
316    pub all: bool,
317}
318
319/// Bound CREATE VECTOR INDEX statement.
320#[derive(Debug, Clone)]
321pub struct BoundCreateVectorIndex {
322    pub index_name: String,
323    pub table_name: String,
324    pub column_name: String,
325    pub metric: String,
326    pub dimensions: u64,
327}
328
329/// Bound `CREATE [ART|HASH] INDEX` statement.
330#[derive(Debug, Clone)]
331pub struct BoundCreateIndex {
332    pub index_type: IndexType,
333    pub index_name: String,
334    pub table_name: String,
335    pub column_name: String,
336}
337
338/// Bound `DROP INDEX` statement.
339#[derive(Debug, Clone)]
340pub struct BoundDropIndex {
341    pub index_name: String,
342    pub table_name: String,
343}
344
345/// Bound CALL statement — invoke a table function.
346#[derive(Debug, Clone)]
347pub struct BoundStandaloneCall {
348    pub function_name: String,
349    pub args: Vec<akar_parser::ast::Expression>,
350}
351
352/// Bound EXPORT DATABASE statement.
353#[derive(Debug, Clone)]
354pub struct BoundExportDatabase {
355    pub file_path: String,
356    pub file_type: String,
357    pub schema_only: bool,
358    pub options: std::collections::HashMap<String, String>,
359}
360
361/// Bound IMPORT DATABASE statement.
362#[derive(Debug, Clone)]
363pub struct BoundImportDatabase {
364    pub file_path: String,
365    pub query: String,
366    pub index_query: String,
367}
368
369/// Bound CREATE SEQUENCE statement.
370#[derive(Debug, Clone)]
371pub struct BoundCreateSequence {
372    pub name: String,
373    pub if_not_exists: bool,
374    pub or_replace: bool,
375    pub start_with: i64,
376    pub increment: i64,
377    pub min_value: i64,
378    pub max_value: i64,
379    pub cycle: bool,
380}
381
382/// Bound DROP SEQUENCE statement.
383#[derive(Debug, Clone)]
384pub struct BoundDropSequence {
385    pub name: String,
386    pub if_exists: bool,
387}
388
389/// Bound CREATE MACRO statement.
390#[derive(Debug, Clone)]
391pub struct BoundCreateMacro {
392    pub name: String,
393    pub positional_args: Vec<String>,
394    pub default_args: Vec<(String, String)>,
395    pub expression: String,
396}
397
398/// A node element of a CREATE/MERGE pattern.
399#[derive(Debug, Clone)]
400pub struct BoundNodeCreate {
401    pub variable: Option<String>,
402    pub table_name: String,
403    pub table_id: u64,
404    pub properties: Vec<(String, akar_parser::ast::Expression)>,
405}
406
407/// An edge element of a CREATE/MERGE pattern with resolved endpoints.
408#[derive(Debug, Clone)]
409pub struct BoundEdgeCreate {
410    pub variable: Option<String>,
411    pub table_name: String,
412    pub table_id: u64,
413    pub src_var: String,
414    pub dst_var: String,
415    pub properties: Vec<(String, akar_parser::ast::Expression)>,
416}
417
418/// A single element of a CREATE/MERGE pattern path (node and/or edge).
419#[derive(Debug, Clone)]
420pub struct BoundCreatePattern {
421    pub node: Option<BoundNodeCreate>,
422    pub edge: Option<BoundEdgeCreate>,
423}
424
425/// Bound CREATE DML statement — create nodes and/or relationships.
426#[derive(Debug, Clone)]
427pub struct BoundCreateDml {
428    pub patterns: Vec<BoundCreatePattern>,
429}
430
431/// Bound MERGE statement — match or create a node/relationship pattern.
432#[derive(Debug, Clone)]
433pub struct BoundMerge {
434    pub table_name: String,
435    pub table_id: u64,
436    /// Properties from the primary (first) MERGE pattern node (used for matching and creation).
437    pub properties: Vec<(String, akar_parser::ast::Expression)>,
438    /// All bound patterns (nodes + edges) of the MERGE statement.
439    pub patterns: Vec<BoundCreatePattern>,
440    /// ON CREATE SET items: resolved column info.
441    pub on_create: Vec<BoundSetItem>,
442    /// ON MATCH SET items: resolved column info.
443    pub on_match: Vec<BoundSetItem>,
444}
445
446/// Bound CREATE FTS INDEX statement.
447#[derive(Debug, Clone)]
448pub struct BoundCreateFtsIndex {
449    pub index_name: String,
450    pub table_name: String,
451    pub column_name: String,
452    /// Tokenizer name from `WITH TOKENIZER('...')` (P109.1); `None` = callers
453    /// use the `en_stem` default.
454    pub tokenizer: Option<String>,
455    pub if_not_exists: bool,
456}
457
458/// Bound CREATE TYPE — user-defined type alias.
459#[derive(Debug, Clone)]
460pub struct BoundCreateType {
461    pub name: String,
462    pub type_name: String,
463}
464
465/// Bound COMMENT ON TABLE — table comment.
466#[derive(Debug, Clone)]
467pub struct BoundCommentOnTable {
468    pub table_name: String,
469    pub comment: String,
470}
471
472/// Bound CREATE GRAPH — projected graph.
473#[derive(Debug, Clone)]
474pub struct BoundCreateGraph {
475    pub name: String,
476    pub is_any: bool,
477}
478
479/// Bound USE GRAPH — set graph context.
480#[derive(Debug, Clone)]
481pub struct BoundUseGraph {
482    pub name: String,
483}
484
485/// Bound DROP GRAPH — remove projected graph.
486#[derive(Debug, Clone)]
487pub struct BoundDropGraph {
488    pub name: String,
489}