Skip to main content

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