rudb_parse/ast.rs
1//! rudb's abstract syntax tree.
2//!
3//! The parse tree the matcher produces is DuckDB's grammar, faithfully. That is the point of it and
4//! it is also why nothing downstream should read it: a bump of the vendored grammar is allowed to
5//! rename `BetweenInLikeExpression`, and if the binder is matching on that name then the bump is a
6//! rewrite. This module is the boundary. It is ours, it changes when we decide it changes, and
7//! `transform` is the one place that knows both shapes.
8//!
9//! Everything is an arena with `u32` indices, per `spec/04-architecture.md` section 4.5. There is
10//! no `Box` and no `Vec` inside a node. A list of children is a [`Slice`] into a side vector, which
11//! means a node is a fixed size, the whole tree is a handful of allocations, and walking it is a
12//! sequential read rather than a pointer chase per node. It also means an `Ast` is `Clone` and
13//! `Send` without any thought, and that a subtree can be addressed by a `u32` in a plan or an
14//! error without borrowing anything.
15//!
16//! The one cost is that you cannot hold a reference to a node and index the arena at the same time,
17//! so the code reads a node out by value first. Nodes are small and `Copy`, so that is a register
18//! move.
19
20use rudb_common::Span;
21
22use crate::matcher::NONE;
23
24/// A run of items in one of the side vectors.
25///
26/// Empty is `len == 0`, and `start` is then meaningless rather than wrong. There is no `Option`
27/// wrapper because an absent list and an empty list are the same thing everywhere this is used.
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
29pub struct Slice {
30 /// The first item.
31 pub start: u32,
32 /// How many items.
33 pub len: u32,
34}
35
36impl Slice {
37 /// Whether the run is empty.
38 pub const fn is_empty(self) -> bool {
39 self.len == 0
40 }
41
42 /// The run as a range, for indexing the backing vector.
43 pub const fn range(self) -> std::ops::Range<usize> {
44 self.start as usize..(self.start + self.len) as usize
45 }
46}
47
48/// An index into `Ast::strings`.
49pub type StrRef = u32;
50/// An index into `Ast::exprs`.
51pub type ExprRef = u32;
52/// An index into `Ast::sources`.
53pub type SourceRef = u32;
54/// An index into `Ast::queries`.
55pub type QueryRef = u32;
56/// An index into `Ast::selects`.
57pub type SelectRef = u32;
58/// An index into `Ast::create_tables`.
59pub type CreateTableRef = u32;
60/// An index into `Ast::create_views`.
61pub type CreateViewRef = u32;
62/// An index into `Ast::drop_tables`.
63pub type DropTableRef = u32;
64/// Index into [`Ast::schemas`].
65pub type SchemaRef = u32;
66/// Index into [`Ast::sequences`].
67pub type SequenceRef = u32;
68/// Index into [`Ast::alters`].
69pub type AlterRef = u32;
70/// An index into `Ast::inserts`.
71pub type InsertRef = u32;
72/// An index into `Ast::settings`.
73pub type SettingRef = u32;
74/// An index into `Ast::windows`.
75pub type WindowRef = u32;
76
77/// One statement.
78///
79/// Seven of the twenty seven the grammar reaches. The rest are a transform error naming the rule
80/// rather than a variant that nothing fills in, so that adding one is a compile error somewhere
81/// useful rather than a silent `todo!()`.
82#[derive(Debug, Clone, Copy, PartialEq, Eq)]
83pub enum Statement {
84 /// A query, meaning a `SELECT` or a set operation over two of them.
85 Query(QueryRef),
86 /// `CREATE TABLE`.
87 CreateTable(CreateTableRef),
88 /// `CREATE VIEW`.
89 CreateView(CreateViewRef),
90 /// `DROP TABLE` or `DROP VIEW`, which are one rule in the grammar and one statement here.
91 DropTable(DropTableRef),
92 /// `CREATE SCHEMA` or `DROP SCHEMA`.
93 Schema(SchemaRef),
94 /// `CREATE SEQUENCE` or `DROP SEQUENCE`.
95 Sequence(SequenceRef),
96 /// `ALTER TABLE` or `ALTER VIEW`.
97 Alter(AlterRef),
98 /// `INSERT INTO`.
99 Insert(InsertRef),
100 /// `UPDATE`, held as an [`Insert`] whose columns are the ones `SET` names and whose source is
101 /// `SELECT *, condition, value, ... FROM table`, one value per named column.
102 ///
103 /// The binder knows how wide the table is and the transform does not, so the source carries
104 /// the table's columns, whether the row matched, and the new values side by side, and the
105 /// binder picks each column's new value or its old one out of them.
106 Update(InsertRef),
107 /// `DELETE FROM` and `TRUNCATE`, held the same way as [`Statement::Update`] with no columns.
108 Delete(InsertRef),
109 /// `SET name = value`.
110 Set(SettingRef),
111 /// `RESET name`, which is the same shape with nothing on the right of it.
112 Reset(SettingRef),
113 /// `CHECKPOINT` or `FORCE CHECKPOINT`.
114 Checkpoint,
115 /// `BEGIN`, `COMMIT` or `ROLLBACK`, under any of the spellings the grammar takes for each.
116 Transaction(Transaction),
117 /// `EXPLAIN` over a query, and whether `ANALYZE` was asked for.
118 ///
119 /// The query rather than a statement, because the grammar lets every statement be explained
120 /// and a plan is the only thing there is to show. `EXPLAIN INSERT` is a refusal rather than a
121 /// plan of the source, since the source is not what the statement does.
122 ///
123 /// `ANALYZE` means the query is run and the plan is printed with what happened on it, so it is
124 /// a flag on the same statement rather than a statement of its own. Everything between the
125 /// parser and the printer is the same either way, which is the point: the analyzed plan has to
126 /// be the plan that ran.
127 ///
128 /// `STATISTICS` asks for the section that says what the planner knew, which is what
129 /// `spec/stats/05-every-query.md` section 5.1.1 asks `EXPLAIN` to print. It is a flag for the
130 /// same reason `ANALYZE` is: it changes what goes on the end of the output and nothing before
131 /// it.
132 Explain { query: QueryRef, analyze: bool, statistics: bool },
133}
134
135/// `SET name = value` and `RESET name`.
136///
137/// One struct for the two, because `RESET name` is `SET name` with no value and giving it its own
138/// arena would mean two of everything to say the same thing twice.
139#[derive(Debug, Clone, Copy, PartialEq, Eq)]
140pub struct Setting {
141 /// The setting name, as written.
142 pub name: StrRef,
143 /// The scope word, if one was written.
144 pub scope: Scope,
145 /// The value, or `NONE` for a `RESET`.
146 ///
147 /// An expression rather than text. `SET memory_limit = '1GB'` writes a string and `SET threads
148 /// = 4` writes a number, and what a setting does with either is the setting's business.
149 pub value: ExprRef,
150 /// Whether the statement was written as a bare `PRAGMA name`.
151 ///
152 /// `PRAGMA disable_optimizer` is a `SET` with the name and the value both folded into one word,
153 /// and which word means what is the catalog's business rather than the parser's, so it arrives
154 /// here as a name with no value and this flag to say that no value is not a `RESET`.
155 pub pragma: bool,
156}
157
158/// Which copy of a setting a statement means.
159#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
160pub enum Scope {
161 /// No scope word, which every setting reads as the one it has.
162 #[default]
163 Unwritten,
164 /// `GLOBAL`.
165 Global,
166 /// `SESSION`.
167 Session,
168 /// `LOCAL`.
169 Local,
170}
171
172impl Scope {
173 /// The word that was written, for the sentence an error prints.
174 #[must_use]
175 pub const fn keyword(self) -> &'static str {
176 match self {
177 Self::Unwritten => "",
178 Self::Global => "GLOBAL",
179 Self::Session => "SESSION",
180 Self::Local => "LOCAL",
181 }
182 }
183}
184
185/// `CREATE TABLE name (columns)` or `CREATE TABLE name AS query`.
186///
187/// Exactly one of `columns` and `query` says what the table is. A column list is the ordinary form
188/// and `query` is `CREATE TABLE AS`, where the columns come from what the query produced and the
189/// only thing the syntax contributes is optionally renaming them, which is `columns` with the types
190/// left as `NONE`.
191#[derive(Debug, Clone, Copy, PartialEq, Eq)]
192pub struct CreateTable {
193 /// The table name, as a run of [`Slice`] parts, outermost first.
194 pub name: Slice,
195 /// The column definitions, as a run of [`ColumnDef`].
196 pub columns: Slice,
197 /// The `AS` query, or `NONE`.
198 pub query: QueryRef,
199 /// Whether `IF NOT EXISTS` was written.
200 pub if_not_exists: bool,
201 /// Whether `OR REPLACE` was written.
202 pub or_replace: bool,
203 /// Whether `TEMP` or `TEMPORARY` was written.
204 pub temporary: bool,
205 /// The column names of each `PRIMARY KEY` and `UNIQUE`, as a run of name lists in the order
206 /// they were written, whether on a column or on the table.
207 pub keys: Slice,
208 /// Which of `keys` is the primary key, or `NONE`.
209 pub primary: u32,
210 /// Every `CHECK` expression, as a run of expressions in the order they were written, whether on
211 /// a column or on the table.
212 pub checks: Slice,
213 /// The columns of each `FOREIGN KEY`, as a run of name lists in the order written, whether on
214 /// a column or on the table.
215 pub foreign: Slice,
216 /// The table each of `foreign` references, as a run of name lists of its parts.
217 pub foreign_tables: Slice,
218 /// The referenced columns of each of `foreign`, as a run of name lists, an empty one when the
219 /// constraint named none and so means the referenced table's primary key.
220 pub foreign_referenced: Slice,
221}
222
223/// One column of a `CREATE TABLE`.
224///
225/// The type is the text as written rather than a resolved type, because resolving a type is the
226/// binder's job and this crate is syntax. `VARCHAR(10)` and `STRUCT(a INTEGER)` reach the binder
227/// as themselves.
228#[derive(Debug, Clone, Copy, PartialEq, Eq)]
229pub struct ColumnDef {
230 /// The column name.
231 pub name: StrRef,
232 /// The type as written, or `NONE` when the definition had none, which only `CREATE TABLE AS`
233 /// allows.
234 pub ty: StrRef,
235 /// Whether `NOT NULL` was written.
236 pub not_null: bool,
237 /// The `DEFAULT` expression, or `NONE` when the definition had none.
238 pub default: ExprRef,
239}
240
241/// `CREATE VIEW name (columns) AS query`.
242///
243/// The body is kept twice over, as a bound reference into this same arena and as the text that was
244/// written. Both are needed and they are needed for different things. The reference is what binds
245/// the body at creation, which is where a view over a table that is not there is refused. The text
246/// is what the catalog keeps, because a view is bound again at every reference rather than frozen
247/// at creation: a view over `SELECT * FROM t` follows `t` when a column is added to it, which was
248/// measured, and the only way to follow it is to have the query to bind again.
249#[derive(Debug, Clone, Copy, PartialEq, Eq)]
250pub struct CreateView {
251 /// The view name, as a run of [`Slice`] parts, outermost first.
252 pub name: Slice,
253 /// The column aliases, as a run of parts, empty when the statement wrote no list.
254 pub columns: Slice,
255 /// The body.
256 pub query: QueryRef,
257 /// The body as it was written, which is what the catalog keeps.
258 pub sql: StrRef,
259 /// Whether `IF NOT EXISTS` was written.
260 pub if_not_exists: bool,
261 /// Whether `OR REPLACE` was written.
262 pub or_replace: bool,
263 /// Whether `TEMP` or `TEMPORARY` was written.
264 pub temporary: bool,
265}
266
267/// `DROP TABLE a, b` or `DROP VIEW a, b`.
268#[derive(Debug, Clone, Copy, PartialEq, Eq)]
269pub struct DropTable {
270 /// The names, as a run of [`Slice`] into `Ast::name_lists`, each of which is a run of parts.
271 pub names: Slice,
272 /// Whether `IF EXISTS` was written.
273 pub if_exists: bool,
274 /// Whether `VIEW` was written where `TABLE` could have been. Dropping one as the other is an
275 /// error rather than a synonym, so which word was written has to survive the transform.
276 pub view: bool,
277}
278
279/// `CREATE SCHEMA name` or `DROP SCHEMA name`.
280#[derive(Debug, Clone, Copy, PartialEq, Eq)]
281pub struct Schema {
282 /// The name, as a run of parts, outermost first.
283 pub name: Slice,
284 /// Whether this is a `DROP` rather than a `CREATE`.
285 pub drop: bool,
286 /// Whether `IF NOT EXISTS` was written on a create or `IF EXISTS` on a drop.
287 pub quiet: bool,
288 /// Whether `OR REPLACE` was written, which only a create can have.
289 pub or_replace: bool,
290 /// Whether `TEMP` or `TEMPORARY` was written, which only a create can have.
291 pub temporary: bool,
292 /// Whether `CASCADE` was written, which only a drop can have.
293 pub cascade: bool,
294}
295
296/// `CREATE SEQUENCE name options` or `DROP SEQUENCE name`.
297///
298/// The options are settled here rather than in the binder, defaults and all, because that is where
299/// the pin settles them and every refusal of a bad combination is a parser error there.
300#[derive(Debug, Clone, Copy, PartialEq, Eq)]
301pub struct Sequence {
302 /// The name, as a run of parts, outermost first.
303 pub name: Slice,
304 /// Whether this is a `DROP` rather than a `CREATE`.
305 pub drop: bool,
306 /// Whether `IF NOT EXISTS` was written on a create or `IF EXISTS` on a drop.
307 pub quiet: bool,
308 /// Whether `OR REPLACE` was written, which only a create can have.
309 pub or_replace: bool,
310 /// Whether `TEMP` or `TEMPORARY` was written, which only a create can have.
311 pub temporary: bool,
312 /// Whether `CASCADE` was written, which only a drop can have.
313 pub cascade: bool,
314 /// What a create settled, and the defaults on a drop.
315 pub options: rudb_common::sequence::Options,
316 /// The table or view an `ALTER SEQUENCE ... OWNED BY` names, as a run of parts, and empty for
317 /// anything else. An alter is a statement that is neither a drop nor has this empty.
318 pub owner: Slice,
319}
320
321/// `ALTER TABLE name action` or `ALTER VIEW name RENAME TO other`.
322///
323/// One action a statement, because the pin refuses a list of them in the parser.
324#[derive(Debug, Clone, Copy, PartialEq, Eq)]
325pub struct Alter {
326 /// The table or view, as a run of parts, outermost first.
327 pub name: Slice,
328 /// Whether `IF EXISTS` was written, which makes a missing table no error.
329 pub quiet: bool,
330 /// Whether this is `ALTER VIEW` rather than `ALTER TABLE`.
331 pub view: bool,
332 /// What it does.
333 pub action: AlterAction,
334}
335
336/// What one `ALTER TABLE` does. A column is named as written.
337#[derive(Debug, Clone, Copy, PartialEq, Eq)]
338pub enum AlterAction {
339 /// `RENAME TO name`.
340 Rename {
341 /// The new name.
342 to: StrRef,
343 },
344 /// `RENAME COLUMN column TO name`.
345 RenameColumn {
346 /// The column.
347 column: StrRef,
348 /// The new name.
349 to: StrRef,
350 },
351 /// `ADD COLUMN definition`, where only the type, `NOT NULL` and `DEFAULT` count, since the pin
352 /// drops every other constraint written on an added column.
353 AddColumn {
354 /// The column as written.
355 column: ColumnDef,
356 /// Whether `IF NOT EXISTS` was written.
357 quiet: bool,
358 },
359 /// `DROP COLUMN column`.
360 DropColumn {
361 /// The column.
362 column: StrRef,
363 /// Whether `IF EXISTS` was written.
364 quiet: bool,
365 },
366 /// `ALTER COLUMN column SET DEFAULT expression`, or `DROP DEFAULT` when the expression is
367 /// `NONE`.
368 Default {
369 /// The column.
370 column: StrRef,
371 /// The new default.
372 default: ExprRef,
373 },
374 /// `ALTER COLUMN column SET NOT NULL` or `DROP NOT NULL`.
375 NotNull {
376 /// The column.
377 column: StrRef,
378 /// Whether it is `SET`.
379 set: bool,
380 },
381 /// `ALTER COLUMN column SET DATA TYPE type USING expression`, either of which can be left out,
382 /// though not both. `NONE` for a missing one.
383 Type {
384 /// The column.
385 column: StrRef,
386 /// The type as written.
387 ty: StrRef,
388 /// The expression the new values are worked out by.
389 using: ExprRef,
390 },
391}
392
393/// `INSERT INTO name (columns) query`.
394#[derive(Debug, Clone, Copy, PartialEq, Eq)]
395pub struct Insert {
396 /// The table name, as a run of parts, outermost first.
397 pub name: Slice,
398 /// The column list, as a run of parts, empty when the statement did not write one.
399 pub columns: Slice,
400 /// What produces the rows, which is a `VALUES` clause or any other query, or `NONE` for
401 /// `DEFAULT VALUES`, which is one row of every column's default.
402 pub source: QueryRef,
403 /// The `RETURNING` list, held as `SELECT list FROM table [AS alias]` and run over the rows the
404 /// statement wrote rather than over the table.
405 pub returning: Option<QueryRef>,
406 /// What an `INSERT` does with a row whose key the table already holds, when it said.
407 pub conflict: Option<Conflict>,
408}
409
410/// `ON CONFLICT`, `INSERT OR REPLACE` or `INSERT OR IGNORE`.
411#[derive(Debug, Clone, Copy, PartialEq, Eq)]
412pub struct Conflict {
413 /// The columns of the key the statement named, as a run of parts, empty when it named none.
414 pub target: Slice,
415 /// What happens to a row that clashes.
416 pub action: ConflictAction,
417}
418
419/// What happens to a row whose key is already held.
420#[derive(Debug, Clone, Copy, PartialEq, Eq)]
421pub enum ConflictAction {
422 /// `DO NOTHING` or `OR IGNORE`: the row is dropped.
423 Nothing,
424 /// `OR REPLACE`: the held row takes the new row's values in the columns the statement wrote.
425 Replace,
426 /// `DO UPDATE SET`, held as `SELECT values..., condition FROM table AS alias POSITIONAL JOIN
427 /// table AS excluded`, which the write runs with the held rows on the left and the new rows on
428 /// the right.
429 Update {
430 /// The columns that are set, as a run of parts, one for each value.
431 columns: Slice,
432 /// The query that works out the values and whether the row is updated at all.
433 query: QueryRef,
434 },
435}
436
437/// A `WITH name AS MATERIALIZED (query)`, which is run once and read wherever it is named.
438///
439/// Only the materialised ones are here. A plain `WITH` and a `NOT MATERIALIZED` one are put into
440/// every place they are named while the tree is being built, the way the reference binary does it,
441/// so by the time anything reads an [`Ast`] there is no name left to resolve.
442#[derive(Debug, Clone, Copy, PartialEq, Eq)]
443pub struct Cte {
444 /// The name it was written with.
445 pub name: StrRef,
446 /// What produces its rows.
447 pub query: QueryRef,
448 /// The column names from `AS name(a, b)`, as a run of [`StrRef`], empty when there were none.
449 pub columns: Slice,
450}
451
452/// A query: a body, plus the modifiers that apply to whatever the body produced.
453///
454/// The split is the grammar's, not an invention. `SelectStatementInternal <- WithClause?
455/// SelectSetOpChain ResultModifiers?` puts `ORDER BY` and `LIMIT` outside the set operator chain,
456/// which is the only place they can go and be right: `a UNION b ORDER BY x` sorts the union and not
457/// the second half of it. Hanging them off `Select` instead would have made that unrepresentable.
458#[derive(Debug, Clone, Copy, PartialEq, Eq)]
459pub struct Query {
460 /// The materialised `WITH` definitions this query introduces, as a run of indexes into
461 /// `Ast::ctes` held in `Ast::cte_lists`, outermost first.
462 ///
463 /// A list of indexes rather than a run of the arena itself, because a materialised `WITH`
464 /// inside another one is pushed while the outer one is still being built, so what one query
465 /// owns is not a contiguous stretch of the arena.
466 pub ctes: Slice,
467 /// What produces the rows.
468 pub body: QueryBody,
469 /// The `ORDER BY` list, as a run of [`OrderItem`].
470 pub order_by: Slice,
471 /// Whether the clause was `ORDER BY ALL`.
472 pub order_by_all: bool,
473 /// The `LIMIT` expression, or `NONE`.
474 pub limit: ExprRef,
475 /// Whether the limit was a percentage rather than a row count.
476 pub limit_percent: bool,
477 /// The `OFFSET` expression, or `NONE`.
478 pub offset: ExprRef,
479}
480
481impl Query {
482 /// A query with no modifiers on it.
483 pub const fn bare(body: QueryBody) -> Self {
484 Self {
485 ctes: Slice { start: 0, len: 0 },
486 body,
487 order_by: Slice { start: 0, len: 0 },
488 order_by_all: false,
489 limit: NONE,
490 limit_percent: false,
491 offset: NONE,
492 }
493 }
494}
495
496/// What produces the rows of a query.
497#[derive(Debug, Clone, Copy, PartialEq, Eq)]
498pub enum QueryBody {
499 /// One `SELECT ... FROM ... WHERE ...` block.
500 Select(SelectRef),
501 /// `UNION`, `EXCEPT` or `INTERSECT` over two queries.
502 SetOp {
503 /// Which operator.
504 op: SetOp,
505 /// Whether duplicates survive.
506 quantifier: Quantifier,
507 /// Whether the columns are matched up by name rather than by position.
508 by_name: bool,
509 /// The query on the left.
510 left: QueryRef,
511 /// The query on the right.
512 right: QueryRef,
513 },
514 /// `VALUES (1, 'a'), (2, 'b')`, as a run of [`Slice`] in `Ast::rows`.
515 ///
516 /// A row count and a column count and nothing else, so it is a query body rather than a
517 /// statement of its own. That is also what makes `INSERT INTO t VALUES (1)` and
518 /// `INSERT INTO t SELECT 1` the same shape by the time anything downstream sees them, which is
519 /// the reason the insert walker does not have two arms.
520 Values(Slice),
521 /// `DESCRIBE SELECT ...`, `DESCRIBE t` and `DESCRIBE 'file.parquet'`.
522 ///
523 /// A query body rather than a statement, because that is where the grammar puts it:
524 /// `SelectStatementType <- ... / DescribeStatement / ...`, so `FROM (DESCRIBE SELECT 1)` is a
525 /// subquery over one and needs no rule of its own. The two spellings that name something
526 /// instead of writing a query arrive here as `DESCRIBE SELECT * FROM that`, which is not a
527 /// shortcut: on the reference binary `DESCRIBE t` and `DESCRIBE SELECT * FROM t` produce the
528 /// same six columns and the same rows, down to the primary key and the default.
529 Describe(QueryRef),
530 /// `SHOW name`, resolved as a setting or a deprecated table description while binding.
531 Show { name: Slice, relation: QueryRef },
532}
533
534/// Which set operator.
535#[derive(Debug, Clone, Copy, PartialEq, Eq)]
536pub enum SetOp {
537 /// `UNION`.
538 Union,
539 /// `EXCEPT`.
540 Except,
541 /// `INTERSECT`.
542 Intersect,
543}
544
545/// Whether a set operator or an aggregate keeps duplicates.
546///
547/// `Unstated` is not the same as `All` even though the two agree for `UNION`, because they disagree
548/// for `INTERSECT` in some dialects and because an error message that says what was written is
549/// better than one that says what it was taken to mean.
550#[derive(Debug, Clone, Copy, PartialEq, Eq)]
551pub enum Quantifier {
552 /// Neither word was written.
553 Unstated,
554 /// `ALL`.
555 All,
556 /// `DISTINCT`.
557 Distinct,
558}
559
560/// What the `DISTINCT` clause of a select said.
561#[derive(Debug, Clone, Copy, PartialEq, Eq)]
562pub enum Distinct {
563 /// No clause, or the no-op `SELECT ALL`.
564 No,
565 /// `SELECT DISTINCT`.
566 Yes,
567 /// `SELECT DISTINCT ON (a, b)`, holding the expressions in the parentheses.
568 On(Slice),
569}
570
571/// One select block.
572///
573/// Every optional expression is `NONE` when it is absent rather than an `Option<u32>`, which keeps
574/// the struct at forty bytes and keeps the absent case spelled the same way it is spelled in the
575/// parse tree arena.
576#[derive(Debug, Clone, Copy, PartialEq, Eq)]
577pub struct Select {
578 /// The `DISTINCT` clause.
579 pub distinct: Distinct,
580 /// The target list, as a run of [`Target`].
581 pub targets: Slice,
582 /// The `FROM` list, as a run of [`SourceRef`]. Several entries mean a cross product.
583 pub from: Slice,
584 /// The `WHERE` expression, or `NONE`.
585 pub filter: ExprRef,
586 /// The `GROUP BY` list, as a run of [`ExprRef`].
587 pub group_by: Slice,
588 /// Whether the clause was `GROUP BY ALL`.
589 pub group_by_all: bool,
590 /// The `HAVING` expression, or `NONE`.
591 pub having: ExprRef,
592}
593
594impl Select {
595 /// An empty select, which is what the transformer fills in from.
596 pub const fn empty() -> Self {
597 Self {
598 distinct: Distinct::No,
599 targets: Slice { start: 0, len: 0 },
600 from: Slice { start: 0, len: 0 },
601 filter: NONE,
602 group_by: Slice { start: 0, len: 0 },
603 group_by_all: false,
604 having: NONE,
605 }
606 }
607}
608
609/// One entry of a target list.
610#[derive(Debug, Clone, Copy, PartialEq, Eq)]
611pub struct Target {
612 /// What is being selected.
613 pub expr: ExprRef,
614 /// The alias, or `NONE`. The binder invents one when there is none, because what it invents
615 /// depends on the expression and that is a binder question rather than a parser question.
616 pub alias: StrRef,
617}
618
619/// One entry of an order by list.
620#[derive(Debug, Clone, Copy, PartialEq, Eq)]
621pub struct OrderItem {
622 /// What to sort on.
623 pub expr: ExprRef,
624 /// The direction.
625 pub order: Order,
626 /// Where nulls go.
627 pub nulls: Nulls,
628}
629
630/// Sort direction, with the unwritten case kept apart from the default it resolves to.
631#[derive(Debug, Clone, Copy, PartialEq, Eq)]
632pub enum Order {
633 /// Nothing was written.
634 Unstated,
635 /// `ASC` or `ASCENDING`.
636 Ascending,
637 /// `DESC` or `DESCENDING`.
638 Descending,
639}
640
641/// Null placement in a sort.
642#[derive(Debug, Clone, Copy, PartialEq, Eq)]
643pub enum Nulls {
644 /// Nothing was written, so the session default applies.
645 Unstated,
646 /// `NULLS FIRST`.
647 First,
648 /// `NULLS LAST`.
649 Last,
650}
651
652/// How a window frame measures the distance to its bounds.
653#[derive(Debug, Clone, Copy, PartialEq, Eq)]
654pub enum WindowUnit {
655 /// `ROWS`, so a bound counts rows.
656 Rows,
657 /// `RANGE`, so a bound is a value offset from the current row's sort key.
658 Range,
659 /// `GROUPS`, so a bound counts runs of rows that tie on the sort key.
660 Groups,
661}
662
663/// One end of a window frame.
664#[derive(Debug, Clone, Copy, PartialEq, Eq)]
665pub enum WindowBound {
666 /// `UNBOUNDED PRECEDING`, the first row of the partition.
667 UnboundedPreceding,
668 /// `n PRECEDING`, holding the offset expression.
669 Preceding(ExprRef),
670 /// `CURRENT ROW`.
671 CurrentRow,
672 /// `n FOLLOWING`, holding the offset expression.
673 Following(ExprRef),
674 /// `UNBOUNDED FOLLOWING`, the last row of the partition.
675 UnboundedFollowing,
676}
677
678/// Which peers of the current row the frame drops once its bounds have been applied.
679#[derive(Debug, Clone, Copy, PartialEq, Eq)]
680pub enum WindowExclude {
681 /// `EXCLUDE NO OTHERS`, which is also what an unwritten clause means.
682 NoOthers,
683 /// `EXCLUDE CURRENT ROW`.
684 CurrentRow,
685 /// `EXCLUDE GROUP`, dropping the current row and everything that ties with it.
686 Group,
687 /// `EXCLUDE TIES`, dropping everything that ties with the current row but keeping it.
688 Ties,
689}
690
691/// Everything inside the parentheses of an `OVER`.
692///
693/// A named window is resolved here rather than downstream, because the resolution is a parser
694/// question on the reference binary: a reference to a window nobody defined is a `Parser Error`
695/// there, and a view written with `OVER w` comes back out of the catalog with the definition
696/// inlined. So nothing after the transform ever sees a name, and there is no window clause on
697/// [`Select`] for it to see one in.
698#[derive(Debug, Clone, Copy, PartialEq, Eq)]
699pub struct WindowSpec {
700 /// The `PARTITION BY` list, as a run of [`ExprRef`], empty when there was no clause.
701 pub partition: Slice,
702 /// The `ORDER BY` list, as a run of [`OrderItem`], empty when there was no clause.
703 pub order: Slice,
704 /// Which of the three units the bounds are measured in.
705 pub unit: WindowUnit,
706 /// Where the frame starts.
707 pub start: WindowBound,
708 /// Where the frame ends.
709 pub end: WindowBound,
710 /// Which peers the frame drops.
711 pub exclude: WindowExclude,
712}
713
714impl WindowSpec {
715 /// The frame a window with no frame clause gets, which the standard fixes and DuckDB follows.
716 pub const DEFAULT_UNIT: WindowUnit = WindowUnit::Range;
717 /// The start a window with no frame clause gets.
718 pub const DEFAULT_START: WindowBound = WindowBound::UnboundedPreceding;
719 /// The end a window with no frame clause gets.
720 pub const DEFAULT_END: WindowBound = WindowBound::CurrentRow;
721
722 /// A window with no clauses at all, which is what `OVER ()` means.
723 pub const fn empty() -> Self {
724 Self {
725 partition: Slice { start: 0, len: 0 },
726 order: Slice { start: 0, len: 0 },
727 unit: Self::DEFAULT_UNIT,
728 start: Self::DEFAULT_START,
729 end: Self::DEFAULT_END,
730 exclude: WindowExclude::NoOthers,
731 }
732 }
733
734 /// Whether the frame is the one an unwritten frame clause means.
735 ///
736 /// This is what decides whether the frame is printed, which is not a matter of taste: the
737 /// printed form is the column name a window target gets when the query wrote no alias, so
738 /// `SELECT sum(x) OVER (ORDER BY x)` has to be named without a frame in it to agree with the
739 /// reference binary.
740 pub fn frame_is_default(&self) -> bool {
741 self.unit == Self::DEFAULT_UNIT
742 && self.start == Self::DEFAULT_START
743 && self.end == Self::DEFAULT_END
744 && self.exclude == WindowExclude::NoOthers
745 }
746}
747
748/// One entry in a `FROM` clause, which is a tree because joins nest.
749#[derive(Debug, Clone, Copy, PartialEq, Eq)]
750pub enum Source {
751 /// A named table, possibly qualified by schema and catalog.
752 Table {
753 /// The name, as a run of [`StrRef`] in `Ast::parts`, outermost first.
754 name: Slice,
755 /// The alias, or `NONE`.
756 alias: StrRef,
757 /// Column aliases from `AS t(a, b)`, as a run of [`StrRef`].
758 columns: Slice,
759 },
760 /// A materialised `WITH` named where a table goes.
761 ///
762 /// Which definition it reads is settled here rather than left as a name, because shadowing is
763 /// a question about where the name was written and this is the only place that still knows.
764 Cte {
765 /// Which definition, as an index into `Ast::ctes`.
766 cte: u32,
767 /// The alias, or `NONE`, which for a bare name is the name itself.
768 alias: StrRef,
769 /// Column aliases from `AS c(a, b)`, as a run of [`StrRef`].
770 columns: Slice,
771 },
772 /// A parenthesised query in the `FROM` clause.
773 Subquery {
774 /// The query.
775 query: QueryRef,
776 /// The alias, or `NONE`.
777 alias: StrRef,
778 /// Column aliases, as a run of [`StrRef`].
779 columns: Slice,
780 },
781 /// A function call where a table goes, such as `range(10)`.
782 ///
783 /// Held with the name as a qualified run rather than a single string, because `main.range(10)`
784 /// is legal and a function in a schema that does not exist has to say so rather than being
785 /// looked up unqualified and found.
786 Function {
787 /// The name, as a run of [`StrRef`] in `Ast::parts`, outermost first.
788 name: Slice,
789 /// The arguments, as a run of [`Target`] where the alias is the parameter name and is
790 /// `NONE` for a positional one.
791 args: Slice,
792 /// The alias, or `NONE`.
793 alias: StrRef,
794 /// Column aliases from `AS t(a, b)`, as a run of [`StrRef`].
795 columns: Slice,
796 /// Whether the call was written as `PRAGMA name` rather than as a function call.
797 ///
798 /// The two are the same query, because `PRAGMA table_info('t')` is rewritten to
799 /// `SELECT * FROM pragma_table_info('t')` here the way upstream rewrites it, and the
800 /// rewritten form is what the plan and the deparser see. What the flag is for is the two
801 /// messages a bad call produces, which upstream writes in the spelling the user used:
802 /// `table_info()` rather than `pragma_table_info()`, and a candidate line reading
803 /// `PRAGMA "table_info"(VARCHAR)`. A user who wrote a pragma and is told about a function
804 /// they did not name has been handed the rewrite to debug rather than their own statement.
805 pragma: bool,
806 },
807 /// A `VALUES` in the `FROM` clause.
808 Values {
809 /// The rows, as a run of [`Slice`] in `Ast::rows`.
810 rows: Slice,
811 /// The alias, or `NONE`.
812 alias: StrRef,
813 /// Column aliases, as a run of [`StrRef`].
814 columns: Slice,
815 },
816 /// Two sources joined.
817 Join {
818 /// The left side.
819 left: SourceRef,
820 /// The right side.
821 right: SourceRef,
822 /// Which join.
823 kind: JoinKind,
824 /// Whether it was written `NATURAL`.
825 natural: bool,
826 /// The `ON` expression, or `NONE`.
827 on: ExprRef,
828 /// The `USING` column list, as a run of [`StrRef`].
829 using: Slice,
830 },
831}
832
833/// Which join.
834#[derive(Debug, Clone, Copy, PartialEq, Eq)]
835pub enum JoinKind {
836 /// `[INNER] JOIN`.
837 Inner,
838 /// `LEFT [OUTER] JOIN`.
839 Left,
840 /// `RIGHT [OUTER] JOIN`.
841 Right,
842 /// `FULL [OUTER] JOIN`.
843 Full,
844 /// `SEMI JOIN`.
845 Semi,
846 /// `ANTI JOIN`.
847 Anti,
848 /// `CROSS JOIN`.
849 Cross,
850 /// `POSITIONAL JOIN`, which is DuckDB's own and pairs rows by ordinal.
851 Positional,
852}
853
854/// One expression.
855///
856/// Twenty four bytes, which is the widest variant rounded up. The precedence chain in the grammar
857/// does not survive into here: twenty levels of `X <- Y Tail*` become one [`Expr::Binary`] tree,
858/// because the levels exist to make the grammar unambiguous and mean nothing afterwards.
859#[derive(Debug, Clone, Copy, PartialEq, Eq)]
860pub enum Expr {
861 /// `*`, or `t.*` with a qualifier.
862 Star {
863 /// The qualifier, as a run of [`StrRef`], empty for a bare star.
864 qualifier: Slice,
865 /// `REPLACE (expression AS column)`, as a run of [`Target`] where the alias is the column
866 /// being replaced, empty for a star with no replace list.
867 ///
868 /// A [`Target`] rather than a type of its own because a replacement is an expression and a
869 /// name, which is exactly what a target is, and because that puts it in the arena every
870 /// other expression and name pair already lives in.
871 replacements: Slice,
872 },
873 /// A column reference, qualified or not.
874 Column {
875 /// The name, as a run of [`StrRef`], outermost first, so `s.t.a` is three parts.
876 name: Slice,
877 },
878 /// A literal, kept as the text that was written.
879 Literal {
880 /// Which kind.
881 kind: LiteralKind,
882 /// The text, with quotes stripped and escapes resolved for a string, `NONE` for a keyword
883 /// literal like `NULL` where the kind already says everything.
884 text: StrRef,
885 },
886 /// A prefix or postfix operator.
887 Unary {
888 /// Which operator.
889 op: UnaryOp,
890 /// What it applies to.
891 operand: ExprRef,
892 },
893 /// An infix operator.
894 Binary {
895 /// Which operator.
896 op: BinaryOp,
897 /// The left operand.
898 left: ExprRef,
899 /// The right operand.
900 right: ExprRef,
901 },
902 /// A function call.
903 Function {
904 /// The name, as a run of [`StrRef`], so `main.count` is two parts.
905 name: Slice,
906 /// The arguments, as a run of [`ExprRef`].
907 args: Slice,
908 /// Whether the call said `DISTINCT`.
909 distinct: bool,
910 /// The `FILTER (WHERE ...)` predicate, or `NONE`. Kept on every call and not only on the
911 /// ones that can carry it, because which names can carry it is a question about the
912 /// function catalog and the parser does not have one.
913 filter: ExprRef,
914 },
915 /// A function call with an `OVER` on the end of it.
916 ///
917 /// Kept apart from [`Expr::Function`] rather than given an optional window, because the two
918 /// are different things by every rule that applies to them: a window call is refused in a
919 /// `WHERE` and in a `HAVING`, it may not appear inside an aggregate, and it resolves against a
920 /// different set of names. A variant that only some of the code has to remember to look at is
921 /// a variant the rest of the code gets wrong.
922 Window {
923 /// The name, as a run of [`StrRef`], so `main.sum` is two parts.
924 name: Slice,
925 /// The arguments, as a run of [`ExprRef`].
926 args: Slice,
927 /// Whether the call said `DISTINCT`.
928 distinct: bool,
929 /// The `FILTER (WHERE ...)` predicate, or `NONE`. It is written before the `OVER` and not
930 /// after it, which is a rule of the grammar rather than of the binder.
931 filter: ExprRef,
932 /// Whether the call said `IGNORE NULLS`. `RESPECT NULLS` is the default and is not kept,
933 /// because the reference binary drops it: a view written with it comes back without it.
934 ignore_nulls: bool,
935 /// The `ORDER BY` written inside the brackets, as a run of [`OrderItem`], empty when there
936 /// was none. This is the order the call reads the rows of its frame in, and it has nothing
937 /// to do with the `ORDER BY` in the `OVER`, which lays the partition out.
938 order: Slice,
939 /// The window itself, into `Ast::windows`.
940 spec: WindowRef,
941 },
942 /// `CAST(x AS t)` or `TRY_CAST(x AS t)`.
943 Cast {
944 /// What is being cast.
945 operand: ExprRef,
946 /// The target type, as the text it was written with. Parsing it is `rudb-common`'s job and
947 /// doing it here would put the type system in the parser.
948 ty: StrRef,
949 /// Whether a failure yields null rather than an error.
950 try_cast: bool,
951 },
952 /// `CASE`, searched or simple.
953 Case {
954 /// The operand of a simple `CASE x WHEN`, or `NONE` for a searched one.
955 operand: ExprRef,
956 /// The arms, as a run of [`CaseArm`].
957 arms: Slice,
958 /// The `ELSE`, or `NONE`.
959 otherwise: ExprRef,
960 },
961 /// `x BETWEEN a AND b`.
962 Between {
963 /// What is being tested.
964 operand: ExprRef,
965 /// The lower bound.
966 low: ExprRef,
967 /// The upper bound.
968 high: ExprRef,
969 /// Whether it was written `NOT BETWEEN`.
970 negated: bool,
971 },
972 /// `x IN (a, b, c)`.
973 In {
974 /// What is being tested.
975 operand: ExprRef,
976 /// The list, as a run of [`ExprRef`].
977 list: Slice,
978 /// Whether it was written `NOT IN`.
979 negated: bool,
980 },
981 /// `x IN (SELECT ...)` or its negation.
982 InSubquery {
983 /// What is being tested.
984 operand: ExprRef,
985 /// The query producing the candidates.
986 query: QueryRef,
987 /// Whether it was written `NOT IN`.
988 negated: bool,
989 },
990 /// `x op ANY (SELECT ...)` or `x op ALL (SELECT ...)`.
991 QuantifiedSubquery {
992 /// The value on the left of the comparison.
993 operand: ExprRef,
994 /// The comparison applied to each candidate.
995 op: BinaryOp,
996 /// The query producing the candidates.
997 query: QueryRef,
998 /// Whether the quantifier was `ALL` rather than `ANY`.
999 all: bool,
1000 },
1001 /// `DEFAULT` where a value is written, which is the column's default and only means something
1002 /// as a whole item of an `INSERT`'s `VALUES` row.
1003 Default,
1004 /// A prepared statement parameter, written `?`, `?1`, `$1` or `$name`.
1005 Parameter {
1006 /// The identifier, which is the number for a positional one and the word for a named one.
1007 /// A bare `?` is numbered by where it was written, so the identifier is there either way.
1008 name: StrRef,
1009 },
1010 /// A bracketed list of expressions, `[a, b, c]`, which is a LIST value.
1011 List {
1012 /// The items, as a run of [`ExprRef`], in the order they were written.
1013 items: Slice,
1014 },
1015 /// `LAMBDA x, i: body`, a function written inline as the argument of one that takes it.
1016 ///
1017 /// It is an expression only so that it can sit in an argument list. Anywhere else it means
1018 /// nothing, and the binder says so in upstream's words rather than the parser refusing it,
1019 /// because upstream's parser accepts it anywhere too.
1020 Lambda {
1021 /// The parameter names, as a run of [`StrRef`], in the order they were written.
1022 params: Slice,
1023 /// What the function computes from them.
1024 body: ExprRef,
1025 },
1026 /// A braced struct, `{'a': 1, b: 2}`, which is a STRUCT value with the field names written.
1027 Struct {
1028 /// The field names, as a run of [`StrRef`], in the order they were written.
1029 names: Slice,
1030 /// The values, as a run of [`ExprRef`], one for each name.
1031 values: Slice,
1032 },
1033 /// A parenthesised list of more than one expression, which is a row value.
1034 Row {
1035 /// The items, as a run of [`ExprRef`].
1036 items: Slice,
1037 },
1038 /// A scalar subquery, `(SELECT ...)` where an expression is expected.
1039 Subquery {
1040 /// The query.
1041 query: QueryRef,
1042 },
1043 /// `EXISTS (SELECT ...)` or its negation.
1044 Exists {
1045 /// The query whose cardinality is tested.
1046 query: QueryRef,
1047 /// Whether `NOT` was written before `EXISTS`.
1048 negated: bool,
1049 },
1050}
1051
1052/// One `WHEN a THEN b`.
1053#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1054pub struct CaseArm {
1055 /// The `WHEN`.
1056 pub when: ExprRef,
1057 /// The `THEN`.
1058 pub then: ExprRef,
1059}
1060
1061/// What a transaction statement asks for.
1062#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1063pub enum Transaction {
1064 /// `BEGIN` or `START TRANSACTION`, and whether `READ ONLY` was written after it.
1065 Begin {
1066 /// Whether the transaction may not write.
1067 read_only: bool,
1068 },
1069 /// `COMMIT` or `END`.
1070 Commit,
1071 /// `ROLLBACK` or `ABORT`.
1072 Rollback,
1073}
1074
1075/// Which literal.
1076#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1077pub enum LiteralKind {
1078 /// A number, kept as text because the width it wants depends on where it lands.
1079 Number,
1080 /// A string.
1081 String,
1082 /// A blob, kept as the text a blob prints as, which is the text a cast reads it back from.
1083 Blob,
1084 /// `NULL`.
1085 Null,
1086 /// `TRUE`.
1087 True,
1088 /// `FALSE`.
1089 False,
1090}
1091
1092/// A prefix or postfix operator.
1093#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1094pub enum UnaryOp {
1095 /// `NOT x`.
1096 Not,
1097 /// `-x`.
1098 Negate,
1099 /// `+x`, which is a no-op that still has to survive to the binder so that `+'a'` errors.
1100 Plus,
1101 /// `~x`.
1102 BitNot,
1103 /// `x!`.
1104 Factorial,
1105 /// `x IS NULL` or `x ISNULL`.
1106 IsNull,
1107 /// `x IS NOT NULL` or `x NOTNULL`.
1108 IsNotNull,
1109 /// `x IS TRUE`.
1110 IsTrue,
1111 /// `x IS NOT TRUE`.
1112 IsNotTrue,
1113 /// `x IS FALSE`.
1114 IsFalse,
1115 /// `x IS NOT FALSE`.
1116 IsNotFalse,
1117 /// `x IS UNKNOWN`.
1118 IsUnknown,
1119 /// `x IS NOT UNKNOWN`.
1120 IsNotUnknown,
1121}
1122
1123/// An infix operator.
1124///
1125/// The list is the dialect and not a general idea of what operators are. `Named` is the one open
1126/// door, because `OperatorLiteral` in the grammar takes any run of operator characters that is not
1127/// already a token, and rejecting that here would reject SQL DuckDB accepts.
1128#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1129pub enum BinaryOp {
1130 /// `OR`.
1131 Or,
1132 /// `AND`.
1133 And,
1134 /// `=` or `==`.
1135 Eq,
1136 /// `!=` or `<>`.
1137 NotEq,
1138 /// `<`.
1139 Lt,
1140 /// `>`.
1141 Gt,
1142 /// `<=`.
1143 LtEq,
1144 /// `>=`.
1145 GtEq,
1146 /// `IS DISTINCT FROM`.
1147 IsDistinctFrom,
1148 /// `IS NOT DISTINCT FROM`.
1149 IsNotDistinctFrom,
1150 /// `+`.
1151 Add,
1152 /// `-`.
1153 Subtract,
1154 /// `*`.
1155 Multiply,
1156 /// `/`.
1157 Divide,
1158 /// `//`, integer division.
1159 IntegerDivide,
1160 /// `%`.
1161 Modulo,
1162 /// `^` or `**`.
1163 Power,
1164 /// `&`.
1165 BitAnd,
1166 /// `|`.
1167 BitOr,
1168 /// `<<`.
1169 ShiftLeft,
1170 /// `>>`.
1171 ShiftRight,
1172 /// `||`.
1173 Concat,
1174 /// `LIKE` or `~~`.
1175 Like,
1176 /// `NOT LIKE` or `!~~`.
1177 NotLike,
1178 /// `ILIKE` or `~~*`.
1179 ILike,
1180 /// `NOT ILIKE` or `!~~*`.
1181 NotILike,
1182 /// `GLOB` or `~~~`.
1183 Glob,
1184 /// `SIMILAR TO`.
1185 SimilarTo,
1186 /// `NOT SIMILAR TO`.
1187 NotSimilarTo,
1188 /// `~`, a regex match.
1189 Regex,
1190 /// `!~`, a negated regex match.
1191 NotRegex,
1192 /// `~*`, a case insensitive regex match.
1193 RegexInsensitive,
1194 /// `!~*`, a negated case insensitive regex match.
1195 NotRegexInsensitive,
1196 /// `COLLATE`.
1197 Collate,
1198 /// `AT TIME ZONE`.
1199 AtTimeZone,
1200 /// `->`.
1201 Arrow,
1202 /// `->>`.
1203 LongArrow,
1204 /// `@>`, contains.
1205 Contains,
1206 /// `<@`, contained by.
1207 ContainedBy,
1208 /// `&&`, overlaps.
1209 Overlaps,
1210 /// `^@`, starts with.
1211 StartsWith,
1212 /// `<<=`, an inet operator.
1213 InetContainedByOrEq,
1214 /// `>>=`, an inet operator.
1215 InetContainsOrEq,
1216 /// An operator the dialect does not name, which DuckDB resolves as a binary function of that
1217 /// name. `a <=> b` is the shape.
1218 Named(StrRef),
1219}
1220
1221/// A parsed statement or script, with every arena it points into.
1222///
1223/// Cheap to clone, cheap to send, and self contained: no index in here refers to anything outside
1224/// it, and nothing in here borrows the query text. The text is copied into `strings` on the way in,
1225/// which costs one allocation per distinct identifier and buys an `Ast` that outlives the string it
1226/// came from.
1227#[derive(Debug, Clone, Default, PartialEq, Eq)]
1228pub struct Ast {
1229 /// The statements in the script, in order.
1230 pub statements: Vec<Statement>,
1231 /// The query arena.
1232 pub queries: Vec<Query>,
1233 /// Source ranges parallel to `queries`.
1234 pub query_spans: Vec<Span>,
1235 /// The select arena.
1236 pub selects: Vec<Select>,
1237 /// The expression arena.
1238 pub exprs: Vec<Expr>,
1239 /// Source ranges parallel to `exprs`.
1240 pub expr_spans: Vec<Span>,
1241 /// The from-item arena.
1242 pub sources: Vec<Source>,
1243 /// Interned text. Identifiers keep the case they were written in, because DuckDB does not fold
1244 /// it at any point, including for quoted identifiers.
1245 pub strings: Vec<String>,
1246 /// Backing store for every [`Slice`] of names.
1247 pub parts: Vec<StrRef>,
1248 /// Backing store for every [`Slice`] of expressions.
1249 pub expr_lists: Vec<ExprRef>,
1250 /// Backing store for every [`Slice`] of from items.
1251 pub source_lists: Vec<SourceRef>,
1252 /// Backing store for every [`Slice`] of target list entries.
1253 pub targets: Vec<Target>,
1254 /// Backing store for every [`Slice`] of order by entries.
1255 pub order_items: Vec<OrderItem>,
1256 /// Backing store for every [`Slice`] of case arms.
1257 pub case_arms: Vec<CaseArm>,
1258 /// The `CREATE TABLE` arena.
1259 pub create_tables: Vec<CreateTable>,
1260 /// The `CREATE VIEW` arena.
1261 pub create_views: Vec<CreateView>,
1262 /// The `DROP TABLE` arena.
1263 pub drop_tables: Vec<DropTable>,
1264 /// The `CREATE SCHEMA` and `DROP SCHEMA` arena.
1265 pub schemas: Vec<Schema>,
1266 /// The `CREATE SEQUENCE` and `DROP SEQUENCE` arena.
1267 pub sequences: Vec<Sequence>,
1268 /// The `ALTER TABLE` and `ALTER VIEW` arena.
1269 pub alters: Vec<Alter>,
1270 /// The `INSERT` arena.
1271 pub inserts: Vec<Insert>,
1272 /// The `SET` and `RESET` arena.
1273 pub settings: Vec<Setting>,
1274 /// Backing store for every [`Slice`] of column definitions.
1275 pub column_defs: Vec<ColumnDef>,
1276 /// Backing store for every [`Slice`] of names, which is a name list rather than a name.
1277 pub name_lists: Vec<Slice>,
1278 /// Backing store for the rows of a `VALUES`, each of which is a run of expressions.
1279 pub rows: Vec<Slice>,
1280 /// The window arena, holding what was inside the parentheses of every `OVER`.
1281 pub windows: Vec<WindowSpec>,
1282 /// The materialised `WITH` arena.
1283 pub ctes: Vec<Cte>,
1284 /// Backing store for every [`Slice`] of materialised `WITH` indexes.
1285 pub cte_lists: Vec<u32>,
1286}
1287
1288impl Ast {
1289 /// The source range of an expression.
1290 pub fn expr_span(&self, expr: ExprRef) -> Span {
1291 self.expr_spans[expr as usize]
1292 }
1293
1294 /// The source range of a query.
1295 pub fn query_span(&self, query: QueryRef) -> Span {
1296 self.query_spans[query as usize]
1297 }
1298
1299 /// The text behind a [`StrRef`], or the empty string for `NONE`.
1300 pub fn string(&self, index: StrRef) -> &str {
1301 if index == NONE { "" } else { &self.strings[index as usize] }
1302 }
1303
1304 /// Every parameter identifier the statement uses, once each, in the order they were written.
1305 ///
1306 /// The arena is built as the walk goes, so its order is the written order, and a parameter used
1307 /// twice is one identifier here because it is one value to provide.
1308 pub fn parameters(&self) -> Vec<&str> {
1309 let mut found: Vec<&str> = Vec::new();
1310 for expr in &self.exprs {
1311 if let Expr::Parameter { name } = *expr {
1312 let name = self.string(name);
1313 if !found.contains(&name) {
1314 found.push(name);
1315 }
1316 }
1317 }
1318 found
1319 }
1320
1321 /// The parts of a name, outermost first.
1322 pub fn name(&self, slice: Slice) -> impl Iterator<Item = &str> {
1323 self.parts[slice.range()].iter().map(|&part| self.string(part))
1324 }
1325
1326 /// A name written back out with dots between the parts, for error messages and tests.
1327 pub fn name_text(&self, slice: Slice) -> String {
1328 self.name(slice).collect::<Vec<_>>().join(".")
1329 }
1330
1331 /// One expression.
1332 pub fn expr(&self, index: ExprRef) -> Expr {
1333 self.exprs[index as usize]
1334 }
1335
1336 /// One from item.
1337 pub fn source(&self, index: SourceRef) -> Source {
1338 self.sources[index as usize]
1339 }
1340
1341 /// One query.
1342 pub fn query(&self, index: QueryRef) -> Query {
1343 self.queries[index as usize]
1344 }
1345
1346 /// One select block.
1347 pub fn select(&self, index: SelectRef) -> Select {
1348 self.selects[index as usize]
1349 }
1350
1351 /// One window.
1352 pub fn window(&self, index: WindowRef) -> WindowSpec {
1353 self.windows[index as usize]
1354 }
1355
1356 /// One materialised `WITH` definition.
1357 pub fn cte(&self, index: u32) -> Cte {
1358 self.ctes[index as usize]
1359 }
1360
1361 /// The materialised `WITH` definitions a query introduces, outermost first.
1362 pub fn cte_list(&self, slice: Slice) -> &[u32] {
1363 &self.cte_lists[slice.range()]
1364 }
1365
1366 /// The expressions of a list.
1367 pub fn expr_list(&self, slice: Slice) -> &[ExprRef] {
1368 &self.expr_lists[slice.range()]
1369 }
1370
1371 /// The from items of a list.
1372 pub fn source_list(&self, slice: Slice) -> &[SourceRef] {
1373 &self.source_lists[slice.range()]
1374 }
1375
1376 /// The entries of a target list.
1377 pub fn target_list(&self, slice: Slice) -> &[Target] {
1378 &self.targets[slice.range()]
1379 }
1380
1381 /// The entries of an order by list.
1382 pub fn order_list(&self, slice: Slice) -> &[OrderItem] {
1383 &self.order_items[slice.range()]
1384 }
1385
1386 /// The arms of a case.
1387 pub fn arm_list(&self, slice: Slice) -> &[CaseArm] {
1388 &self.case_arms[slice.range()]
1389 }
1390
1391 /// One `CREATE TABLE`.
1392 pub fn create_table(&self, index: CreateTableRef) -> CreateTable {
1393 self.create_tables[index as usize]
1394 }
1395
1396 /// One `CREATE VIEW`.
1397 pub fn create_view(&self, index: CreateViewRef) -> CreateView {
1398 self.create_views[index as usize]
1399 }
1400
1401 /// One `DROP TABLE`.
1402 pub fn drop_table(&self, index: DropTableRef) -> DropTable {
1403 self.drop_tables[index as usize]
1404 }
1405
1406 /// One `CREATE SCHEMA` or `DROP SCHEMA`.
1407 pub fn schema(&self, index: SchemaRef) -> Schema {
1408 self.schemas[index as usize]
1409 }
1410
1411 /// One `CREATE SEQUENCE` or `DROP SEQUENCE`.
1412 pub fn sequence(&self, index: SequenceRef) -> Sequence {
1413 self.sequences[index as usize]
1414 }
1415
1416 /// One `ALTER TABLE` or `ALTER VIEW`.
1417 pub fn alter(&self, index: AlterRef) -> Alter {
1418 self.alters[index as usize]
1419 }
1420
1421 /// One `INSERT`.
1422 pub fn insert(&self, index: InsertRef) -> Insert {
1423 self.inserts[index as usize]
1424 }
1425
1426 /// One `SET` or `RESET`.
1427 pub fn setting(&self, index: SettingRef) -> Setting {
1428 self.settings[index as usize]
1429 }
1430
1431 /// The column definitions of a `CREATE TABLE`.
1432 pub fn column_defs(&self, slice: Slice) -> &[ColumnDef] {
1433 &self.column_defs[slice.range()]
1434 }
1435
1436 /// The names of a name list, each of which is itself a run of parts.
1437 pub fn name_list(&self, slice: Slice) -> &[Slice] {
1438 &self.name_lists[slice.range()]
1439 }
1440
1441 /// The rows of a `VALUES`, each of which is itself a run of expressions.
1442 pub fn rows(&self, slice: Slice) -> &[Slice] {
1443 &self.rows[slice.range()]
1444 }
1445
1446 /// How many nodes the whole tree is, across every arena.
1447 ///
1448 /// The number to watch when the transformer changes. A parse tree of five thousand nodes that
1449 /// becomes an AST of thirty is the twenty precedence levels being thrown away, which is the
1450 /// whole reason this module exists.
1451 pub fn node_count(&self) -> usize {
1452 self.queries.len() + self.selects.len() + self.exprs.len() + self.sources.len()
1453 }
1454}