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 macro table names derived from the FTS index.
217    pub docs_table: String,
218    pub terms_table: String,
219    pub posting_table: String,
220    /// The source node table/column the index was created on (P52.39), used
221    /// to keep the derived macro tables in sync with live DML.
222    pub table_name: String,
223    pub column_name: String,
224}
225
226#[derive(Debug, Clone)]
227pub struct BoundPattern {
228    pub node_variable: Option<String>,
229    pub node_label: Option<String>,
230    pub node_table_id: Option<u64>,
231    pub properties: Vec<(String, akar_parser::ast::Expression)>,
232    pub edge: Option<BoundEdgePattern>,
233}
234
235#[derive(Debug, Clone)]
236pub struct BoundEdgePattern {
237    pub variable: Option<String>,
238    pub label: Option<String>,
239    pub rel_table_id: Option<u64>,
240    pub direction: akar_parser::ast::EdgeDirection,
241    pub properties: Vec<(String, akar_parser::ast::Expression)>,
242    pub lower_bound: Option<u64>,
243    pub upper_bound: Option<u64>,
244}
245
246/// A bound expression with resolved type information.
247#[derive(Debug, Clone)]
248pub struct BoundExpression {
249    pub expression: Expression,
250    pub resolved_type: LogicalTypeID,
251    pub is_constant: bool,
252    /// Output column name override from `AS alias` in RETURN/WITH (P53.16).
253    pub alias: Option<String>,
254}
255
256#[derive(Debug, Clone)]
257pub struct BoundReturnClause {
258    pub expressions: Vec<BoundExpression>,
259    pub distinct: bool,
260    /// Bound ORDER BY items.
261    pub order_by: Option<Vec<BoundOrderByItem>>,
262    /// Bound LIMIT.
263    pub limit: Option<u64>,
264    /// Bound SKIP.
265    pub skip: Option<u64>,
266}
267
268/// A bound sort item for ORDER BY.
269#[derive(Debug, Clone)]
270pub struct BoundOrderByItem {
271    pub expression: BoundExpression,
272    pub ascending: bool,
273}
274
275#[derive(Debug, Clone)]
276pub struct BoundWhereClause {
277    pub expression: BoundExpression,
278}
279
280// DDL
281#[derive(Debug, Clone)]
282pub struct BoundCreateNodeTable {
283    pub name: String,
284    pub columns: Vec<akar_catalog::CatalogColumn>,
285    pub primary_key: String,
286}
287
288#[derive(Debug, Clone)]
289pub struct BoundCreateRelTable {
290    pub name: String,
291    pub from: String,
292    pub to: String,
293    pub src_table_id: u64,
294    pub dst_table_id: u64,
295    pub columns: Vec<akar_catalog::CatalogColumn>,
296}
297
298#[derive(Debug, Clone)]
299pub struct BoundDropTable {
300    pub name: String,
301}
302
303#[derive(Debug, Clone)]
304pub struct BoundAlterTable {
305    pub table_name: String,
306    pub action: akar_parser::ast::AlterAction,
307}
308
309#[derive(Debug, Clone)]
310pub struct BoundUnion {
311    pub left: Box<BoundQuery>,
312    pub right: Box<BoundQuery>,
313    pub all: bool,
314}
315
316/// Bound CREATE VECTOR INDEX statement.
317#[derive(Debug, Clone)]
318pub struct BoundCreateVectorIndex {
319    pub index_name: String,
320    pub table_name: String,
321    pub column_name: String,
322    pub metric: String,
323    pub dimensions: u64,
324}
325
326/// Bound `CREATE [ART|HASH] INDEX` statement.
327#[derive(Debug, Clone)]
328pub struct BoundCreateIndex {
329    pub index_type: IndexType,
330    pub index_name: String,
331    pub table_name: String,
332    pub column_name: String,
333}
334
335/// Bound `DROP INDEX` statement.
336#[derive(Debug, Clone)]
337pub struct BoundDropIndex {
338    pub index_name: String,
339    pub table_name: String,
340}
341
342/// Bound CALL statement — invoke a table function.
343#[derive(Debug, Clone)]
344pub struct BoundStandaloneCall {
345    pub function_name: String,
346    pub args: Vec<akar_parser::ast::Expression>,
347}
348
349/// Bound EXPORT DATABASE statement.
350#[derive(Debug, Clone)]
351pub struct BoundExportDatabase {
352    pub file_path: String,
353    pub file_type: String,
354    pub schema_only: bool,
355    pub options: std::collections::HashMap<String, String>,
356}
357
358/// Bound IMPORT DATABASE statement.
359#[derive(Debug, Clone)]
360pub struct BoundImportDatabase {
361    pub file_path: String,
362    pub query: String,
363    pub index_query: String,
364}
365
366/// Bound CREATE SEQUENCE statement.
367#[derive(Debug, Clone)]
368pub struct BoundCreateSequence {
369    pub name: String,
370    pub if_not_exists: bool,
371    pub or_replace: bool,
372    pub start_with: i64,
373    pub increment: i64,
374    pub min_value: i64,
375    pub max_value: i64,
376    pub cycle: bool,
377}
378
379/// Bound DROP SEQUENCE statement.
380#[derive(Debug, Clone)]
381pub struct BoundDropSequence {
382    pub name: String,
383    pub if_exists: bool,
384}
385
386/// Bound CREATE MACRO statement.
387#[derive(Debug, Clone)]
388pub struct BoundCreateMacro {
389    pub name: String,
390    pub positional_args: Vec<String>,
391    pub default_args: Vec<(String, String)>,
392    pub expression: String,
393}
394
395/// A node element of a CREATE/MERGE pattern.
396#[derive(Debug, Clone)]
397pub struct BoundNodeCreate {
398    pub variable: Option<String>,
399    pub table_name: String,
400    pub table_id: u64,
401    pub properties: Vec<(String, akar_parser::ast::Expression)>,
402}
403
404/// An edge element of a CREATE/MERGE pattern with resolved endpoints.
405#[derive(Debug, Clone)]
406pub struct BoundEdgeCreate {
407    pub variable: Option<String>,
408    pub table_name: String,
409    pub table_id: u64,
410    pub src_var: String,
411    pub dst_var: String,
412    pub properties: Vec<(String, akar_parser::ast::Expression)>,
413}
414
415/// A single element of a CREATE/MERGE pattern path (node and/or edge).
416#[derive(Debug, Clone)]
417pub struct BoundCreatePattern {
418    pub node: Option<BoundNodeCreate>,
419    pub edge: Option<BoundEdgeCreate>,
420}
421
422/// Bound CREATE DML statement — create nodes and/or relationships.
423#[derive(Debug, Clone)]
424pub struct BoundCreateDml {
425    pub patterns: Vec<BoundCreatePattern>,
426}
427
428/// Bound MERGE statement — match or create a node/relationship pattern.
429#[derive(Debug, Clone)]
430pub struct BoundMerge {
431    pub table_name: String,
432    pub table_id: u64,
433    /// Properties from the primary (first) MERGE pattern node (used for matching and creation).
434    pub properties: Vec<(String, akar_parser::ast::Expression)>,
435    /// All bound patterns (nodes + edges) of the MERGE statement.
436    pub patterns: Vec<BoundCreatePattern>,
437    /// ON CREATE SET items: resolved column info.
438    pub on_create: Vec<BoundSetItem>,
439    /// ON MATCH SET items: resolved column info.
440    pub on_match: Vec<BoundSetItem>,
441}
442
443/// Bound CREATE FTS INDEX statement.
444#[derive(Debug, Clone)]
445pub struct BoundCreateFtsIndex {
446    pub index_name: String,
447    pub table_name: String,
448    pub column_name: String,
449    pub if_not_exists: bool,
450    /// The macro table names that will be created.
451    pub docs_table: String,
452    pub terms_table: String,
453    pub posting_table: String,
454}
455
456/// Bound CREATE TYPE — user-defined type alias.
457#[derive(Debug, Clone)]
458pub struct BoundCreateType {
459    pub name: String,
460    pub type_name: String,
461}
462
463/// Bound COMMENT ON TABLE — table comment.
464#[derive(Debug, Clone)]
465pub struct BoundCommentOnTable {
466    pub table_name: String,
467    pub comment: String,
468}
469
470/// Bound CREATE GRAPH — projected graph.
471#[derive(Debug, Clone)]
472pub struct BoundCreateGraph {
473    pub name: String,
474    pub is_any: bool,
475}
476
477/// Bound USE GRAPH — set graph context.
478#[derive(Debug, Clone)]
479pub struct BoundUseGraph {
480    pub name: String,
481}
482
483/// Bound DROP GRAPH — remove projected graph.
484#[derive(Debug, Clone)]
485pub struct BoundDropGraph {
486    pub name: String,
487}