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 crate::matcher::NONE;
21
22/// A run of items in one of the side vectors.
23///
24/// Empty is `len == 0`, and `start` is then meaningless rather than wrong. There is no `Option`
25/// wrapper because an absent list and an empty list are the same thing everywhere this is used.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
27pub struct Slice {
28 /// The first item.
29 pub start: u32,
30 /// How many items.
31 pub len: u32,
32}
33
34impl Slice {
35 /// Whether the run is empty.
36 pub const fn is_empty(self) -> bool {
37 self.len == 0
38 }
39
40 /// The run as a range, for indexing the backing vector.
41 pub const fn range(self) -> std::ops::Range<usize> {
42 self.start as usize..(self.start + self.len) as usize
43 }
44}
45
46/// An index into `Ast::strings`.
47pub type StrRef = u32;
48/// An index into `Ast::exprs`.
49pub type ExprRef = u32;
50/// An index into `Ast::sources`.
51pub type SourceRef = u32;
52/// An index into `Ast::queries`.
53pub type QueryRef = u32;
54/// An index into `Ast::selects`.
55pub type SelectRef = u32;
56/// An index into `Ast::create_tables`.
57pub type CreateTableRef = u32;
58/// An index into `Ast::create_views`.
59pub type CreateViewRef = u32;
60/// An index into `Ast::drop_tables`.
61pub type DropTableRef = u32;
62/// An index into `Ast::inserts`.
63pub type InsertRef = u32;
64
65/// One statement.
66///
67/// Five of the twenty seven the grammar reaches. The rest are a transform error naming the rule
68/// rather than a variant that nothing fills in, so that adding one is a compile error somewhere
69/// useful rather than a silent `todo!()`.
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub enum Statement {
72 /// A query, meaning a `SELECT` or a set operation over two of them.
73 Query(QueryRef),
74 /// `CREATE TABLE`.
75 CreateTable(CreateTableRef),
76 /// `CREATE VIEW`.
77 CreateView(CreateViewRef),
78 /// `DROP TABLE` or `DROP VIEW`, which are one rule in the grammar and one statement here.
79 DropTable(DropTableRef),
80 /// `INSERT INTO`.
81 Insert(InsertRef),
82}
83
84/// `CREATE TABLE name (columns)` or `CREATE TABLE name AS query`.
85///
86/// Exactly one of `columns` and `query` says what the table is. A column list is the ordinary form
87/// and `query` is `CREATE TABLE AS`, where the columns come from what the query produced and the
88/// only thing the syntax contributes is optionally renaming them, which is `columns` with the types
89/// left as `NONE`.
90#[derive(Debug, Clone, Copy, PartialEq, Eq)]
91pub struct CreateTable {
92 /// The table name, as a run of [`Slice`] parts, outermost first.
93 pub name: Slice,
94 /// The column definitions, as a run of [`ColumnDef`].
95 pub columns: Slice,
96 /// The `AS` query, or `NONE`.
97 pub query: QueryRef,
98 /// Whether `IF NOT EXISTS` was written.
99 pub if_not_exists: bool,
100 /// Whether `OR REPLACE` was written.
101 pub or_replace: bool,
102 /// Whether `TEMP` or `TEMPORARY` was written.
103 pub temporary: bool,
104}
105
106/// One column of a `CREATE TABLE`.
107///
108/// The type is the text as written rather than a resolved type, because resolving a type is the
109/// binder's job and this crate is syntax. `VARCHAR(10)` and `STRUCT(a INTEGER)` reach the binder
110/// as themselves.
111#[derive(Debug, Clone, Copy, PartialEq, Eq)]
112pub struct ColumnDef {
113 /// The column name.
114 pub name: StrRef,
115 /// The type as written, or `NONE` when the definition had none, which only `CREATE TABLE AS`
116 /// allows.
117 pub ty: StrRef,
118 /// Whether `NOT NULL` was written.
119 pub not_null: bool,
120}
121
122/// `CREATE VIEW name (columns) AS query`.
123///
124/// The body is kept twice over, as a bound reference into this same arena and as the text that was
125/// written. Both are needed and they are needed for different things. The reference is what binds
126/// the body at creation, which is where a view over a table that is not there is refused. The text
127/// is what the catalog keeps, because a view is bound again at every reference rather than frozen
128/// at creation: a view over `SELECT * FROM t` follows `t` when a column is added to it, which was
129/// measured, and the only way to follow it is to have the query to bind again.
130#[derive(Debug, Clone, Copy, PartialEq, Eq)]
131pub struct CreateView {
132 /// The view name, as a run of [`Slice`] parts, outermost first.
133 pub name: Slice,
134 /// The column aliases, as a run of parts, empty when the statement wrote no list.
135 pub columns: Slice,
136 /// The body.
137 pub query: QueryRef,
138 /// The body as it was written, which is what the catalog keeps.
139 pub sql: StrRef,
140 /// Whether `IF NOT EXISTS` was written.
141 pub if_not_exists: bool,
142 /// Whether `OR REPLACE` was written.
143 pub or_replace: bool,
144 /// Whether `TEMP` or `TEMPORARY` was written.
145 pub temporary: bool,
146}
147
148/// `DROP TABLE a, b` or `DROP VIEW a, b`.
149#[derive(Debug, Clone, Copy, PartialEq, Eq)]
150pub struct DropTable {
151 /// The names, as a run of [`Slice`] into `Ast::name_lists`, each of which is a run of parts.
152 pub names: Slice,
153 /// Whether `IF EXISTS` was written.
154 pub if_exists: bool,
155 /// Whether `VIEW` was written where `TABLE` could have been. Dropping one as the other is an
156 /// error rather than a synonym, so which word was written has to survive the transform.
157 pub view: bool,
158}
159
160/// `INSERT INTO name (columns) query`.
161#[derive(Debug, Clone, Copy, PartialEq, Eq)]
162pub struct Insert {
163 /// The table name, as a run of parts, outermost first.
164 pub name: Slice,
165 /// The column list, as a run of parts, empty when the statement did not write one.
166 pub columns: Slice,
167 /// What produces the rows, which is a `VALUES` clause or any other query.
168 pub source: QueryRef,
169}
170
171/// A query: a body, plus the modifiers that apply to whatever the body produced.
172///
173/// The split is the grammar's, not an invention. `SelectStatementInternal <- WithClause?
174/// SelectSetOpChain ResultModifiers?` puts `ORDER BY` and `LIMIT` outside the set operator chain,
175/// which is the only place they can go and be right: `a UNION b ORDER BY x` sorts the union and not
176/// the second half of it. Hanging them off `Select` instead would have made that unrepresentable.
177#[derive(Debug, Clone, Copy, PartialEq, Eq)]
178pub struct Query {
179 /// What produces the rows.
180 pub body: QueryBody,
181 /// The `ORDER BY` list, as a run of [`OrderItem`].
182 pub order_by: Slice,
183 /// Whether the clause was `ORDER BY ALL`.
184 pub order_by_all: bool,
185 /// The `LIMIT` expression, or `NONE`.
186 pub limit: ExprRef,
187 /// Whether the limit was a percentage rather than a row count.
188 pub limit_percent: bool,
189 /// The `OFFSET` expression, or `NONE`.
190 pub offset: ExprRef,
191}
192
193impl Query {
194 /// A query with no modifiers on it.
195 pub const fn bare(body: QueryBody) -> Self {
196 Self {
197 body,
198 order_by: Slice { start: 0, len: 0 },
199 order_by_all: false,
200 limit: NONE,
201 limit_percent: false,
202 offset: NONE,
203 }
204 }
205}
206
207/// What produces the rows of a query.
208#[derive(Debug, Clone, Copy, PartialEq, Eq)]
209pub enum QueryBody {
210 /// One `SELECT ... FROM ... WHERE ...` block.
211 Select(SelectRef),
212 /// `UNION`, `EXCEPT` or `INTERSECT` over two queries.
213 SetOp {
214 /// Which operator.
215 op: SetOp,
216 /// Whether duplicates survive.
217 quantifier: Quantifier,
218 /// Whether the columns are matched up by name rather than by position.
219 by_name: bool,
220 /// The query on the left.
221 left: QueryRef,
222 /// The query on the right.
223 right: QueryRef,
224 },
225 /// `VALUES (1, 'a'), (2, 'b')`, as a run of [`Slice`] in `Ast::rows`.
226 ///
227 /// A row count and a column count and nothing else, so it is a query body rather than a
228 /// statement of its own. That is also what makes `INSERT INTO t VALUES (1)` and
229 /// `INSERT INTO t SELECT 1` the same shape by the time anything downstream sees them, which is
230 /// the reason the insert walker does not have two arms.
231 Values(Slice),
232}
233
234/// Which set operator.
235#[derive(Debug, Clone, Copy, PartialEq, Eq)]
236pub enum SetOp {
237 /// `UNION`.
238 Union,
239 /// `EXCEPT`.
240 Except,
241 /// `INTERSECT`.
242 Intersect,
243}
244
245/// Whether a set operator or an aggregate keeps duplicates.
246///
247/// `Unstated` is not the same as `All` even though the two agree for `UNION`, because they disagree
248/// for `INTERSECT` in some dialects and because an error message that says what was written is
249/// better than one that says what it was taken to mean.
250#[derive(Debug, Clone, Copy, PartialEq, Eq)]
251pub enum Quantifier {
252 /// Neither word was written.
253 Unstated,
254 /// `ALL`.
255 All,
256 /// `DISTINCT`.
257 Distinct,
258}
259
260/// What the `DISTINCT` clause of a select said.
261#[derive(Debug, Clone, Copy, PartialEq, Eq)]
262pub enum Distinct {
263 /// No clause, or the no-op `SELECT ALL`.
264 No,
265 /// `SELECT DISTINCT`.
266 Yes,
267 /// `SELECT DISTINCT ON (a, b)`, holding the expressions in the parentheses.
268 On(Slice),
269}
270
271/// One select block.
272///
273/// Every optional expression is `NONE` when it is absent rather than an `Option<u32>`, which keeps
274/// the struct at forty bytes and keeps the absent case spelled the same way it is spelled in the
275/// parse tree arena.
276#[derive(Debug, Clone, Copy, PartialEq, Eq)]
277pub struct Select {
278 /// The `DISTINCT` clause.
279 pub distinct: Distinct,
280 /// The target list, as a run of [`Target`].
281 pub targets: Slice,
282 /// The `FROM` list, as a run of [`SourceRef`]. Several entries mean a cross product.
283 pub from: Slice,
284 /// The `WHERE` expression, or `NONE`.
285 pub filter: ExprRef,
286 /// The `GROUP BY` list, as a run of [`ExprRef`].
287 pub group_by: Slice,
288 /// Whether the clause was `GROUP BY ALL`.
289 pub group_by_all: bool,
290 /// The `HAVING` expression, or `NONE`.
291 pub having: ExprRef,
292}
293
294impl Select {
295 /// An empty select, which is what the transformer fills in from.
296 pub const fn empty() -> Self {
297 Self {
298 distinct: Distinct::No,
299 targets: Slice { start: 0, len: 0 },
300 from: Slice { start: 0, len: 0 },
301 filter: NONE,
302 group_by: Slice { start: 0, len: 0 },
303 group_by_all: false,
304 having: NONE,
305 }
306 }
307}
308
309/// One entry of a target list.
310#[derive(Debug, Clone, Copy, PartialEq, Eq)]
311pub struct Target {
312 /// What is being selected.
313 pub expr: ExprRef,
314 /// The alias, or `NONE`. The binder invents one when there is none, because what it invents
315 /// depends on the expression and that is a binder question rather than a parser question.
316 pub alias: StrRef,
317}
318
319/// One entry of an order by list.
320#[derive(Debug, Clone, Copy, PartialEq, Eq)]
321pub struct OrderItem {
322 /// What to sort on.
323 pub expr: ExprRef,
324 /// The direction.
325 pub order: Order,
326 /// Where nulls go.
327 pub nulls: Nulls,
328}
329
330/// Sort direction, with the unwritten case kept apart from the default it resolves to.
331#[derive(Debug, Clone, Copy, PartialEq, Eq)]
332pub enum Order {
333 /// Nothing was written.
334 Unstated,
335 /// `ASC` or `ASCENDING`.
336 Ascending,
337 /// `DESC` or `DESCENDING`.
338 Descending,
339}
340
341/// Null placement in a sort.
342#[derive(Debug, Clone, Copy, PartialEq, Eq)]
343pub enum Nulls {
344 /// Nothing was written, so the session default applies.
345 Unstated,
346 /// `NULLS FIRST`.
347 First,
348 /// `NULLS LAST`.
349 Last,
350}
351
352/// One entry in a `FROM` clause, which is a tree because joins nest.
353#[derive(Debug, Clone, Copy, PartialEq, Eq)]
354pub enum Source {
355 /// A named table, possibly qualified by schema and catalog.
356 Table {
357 /// The name, as a run of [`StrRef`] in `Ast::parts`, outermost first.
358 name: Slice,
359 /// The alias, or `NONE`.
360 alias: StrRef,
361 /// Column aliases from `AS t(a, b)`, as a run of [`StrRef`].
362 columns: Slice,
363 },
364 /// A parenthesised query in the `FROM` clause.
365 Subquery {
366 /// The query.
367 query: QueryRef,
368 /// The alias, or `NONE`.
369 alias: StrRef,
370 /// Column aliases, as a run of [`StrRef`].
371 columns: Slice,
372 },
373 /// A function call where a table goes, such as `range(10)`.
374 ///
375 /// Held with the name as a qualified run rather than a single string, because `main.range(10)`
376 /// is legal and a function in a schema that does not exist has to say so rather than being
377 /// looked up unqualified and found.
378 Function {
379 /// The name, as a run of [`StrRef`] in `Ast::parts`, outermost first.
380 name: Slice,
381 /// The arguments, as a run of [`ExprRef`] in `Ast::expr_lists`.
382 args: Slice,
383 /// The alias, or `NONE`.
384 alias: StrRef,
385 /// Column aliases from `AS t(a, b)`, as a run of [`StrRef`].
386 columns: Slice,
387 },
388 /// A `VALUES` in the `FROM` clause.
389 Values {
390 /// The rows, as a run of [`Slice`] in `Ast::rows`.
391 rows: Slice,
392 /// The alias, or `NONE`.
393 alias: StrRef,
394 /// Column aliases, as a run of [`StrRef`].
395 columns: Slice,
396 },
397 /// Two sources joined.
398 Join {
399 /// The left side.
400 left: SourceRef,
401 /// The right side.
402 right: SourceRef,
403 /// Which join.
404 kind: JoinKind,
405 /// Whether it was written `NATURAL`.
406 natural: bool,
407 /// The `ON` expression, or `NONE`.
408 on: ExprRef,
409 /// The `USING` column list, as a run of [`StrRef`].
410 using: Slice,
411 },
412}
413
414/// Which join.
415#[derive(Debug, Clone, Copy, PartialEq, Eq)]
416pub enum JoinKind {
417 /// `[INNER] JOIN`.
418 Inner,
419 /// `LEFT [OUTER] JOIN`.
420 Left,
421 /// `RIGHT [OUTER] JOIN`.
422 Right,
423 /// `FULL [OUTER] JOIN`.
424 Full,
425 /// `SEMI JOIN`.
426 Semi,
427 /// `ANTI JOIN`.
428 Anti,
429 /// `CROSS JOIN`.
430 Cross,
431 /// `POSITIONAL JOIN`, which is DuckDB's own and pairs rows by ordinal.
432 Positional,
433}
434
435/// One expression.
436///
437/// Twenty four bytes, which is the widest variant rounded up. The precedence chain in the grammar
438/// does not survive into here: twenty levels of `X <- Y Tail*` become one [`Expr::Binary`] tree,
439/// because the levels exist to make the grammar unambiguous and mean nothing afterwards.
440#[derive(Debug, Clone, Copy, PartialEq, Eq)]
441pub enum Expr {
442 /// `*`, or `t.*` with a qualifier.
443 Star {
444 /// The qualifier, as a run of [`StrRef`], empty for a bare star.
445 qualifier: Slice,
446 },
447 /// A column reference, qualified or not.
448 Column {
449 /// The name, as a run of [`StrRef`], outermost first, so `s.t.a` is three parts.
450 name: Slice,
451 },
452 /// A literal, kept as the text that was written.
453 Literal {
454 /// Which kind.
455 kind: LiteralKind,
456 /// The text, with quotes stripped and escapes resolved for a string, `NONE` for a keyword
457 /// literal like `NULL` where the kind already says everything.
458 text: StrRef,
459 },
460 /// A prefix or postfix operator.
461 Unary {
462 /// Which operator.
463 op: UnaryOp,
464 /// What it applies to.
465 operand: ExprRef,
466 },
467 /// An infix operator.
468 Binary {
469 /// Which operator.
470 op: BinaryOp,
471 /// The left operand.
472 left: ExprRef,
473 /// The right operand.
474 right: ExprRef,
475 },
476 /// A function call.
477 Function {
478 /// The name, as a run of [`StrRef`], so `main.count` is two parts.
479 name: Slice,
480 /// The arguments, as a run of [`ExprRef`].
481 args: Slice,
482 /// Whether the call said `DISTINCT`.
483 distinct: bool,
484 },
485 /// `CAST(x AS t)` or `TRY_CAST(x AS t)`.
486 Cast {
487 /// What is being cast.
488 operand: ExprRef,
489 /// The target type, as the text it was written with. Parsing it is `rudb-common`'s job and
490 /// doing it here would put the type system in the parser.
491 ty: StrRef,
492 /// Whether a failure yields null rather than an error.
493 try_cast: bool,
494 },
495 /// `CASE`, searched or simple.
496 Case {
497 /// The operand of a simple `CASE x WHEN`, or `NONE` for a searched one.
498 operand: ExprRef,
499 /// The arms, as a run of [`CaseArm`].
500 arms: Slice,
501 /// The `ELSE`, or `NONE`.
502 otherwise: ExprRef,
503 },
504 /// `x BETWEEN a AND b`.
505 Between {
506 /// What is being tested.
507 operand: ExprRef,
508 /// The lower bound.
509 low: ExprRef,
510 /// The upper bound.
511 high: ExprRef,
512 /// Whether it was written `NOT BETWEEN`.
513 negated: bool,
514 },
515 /// `x IN (a, b, c)`.
516 In {
517 /// What is being tested.
518 operand: ExprRef,
519 /// The list, as a run of [`ExprRef`].
520 list: Slice,
521 /// Whether it was written `NOT IN`.
522 negated: bool,
523 },
524 /// A prepared statement parameter, written `?`, `?1`, `$1` or `$name`.
525 Parameter {
526 /// The identifier, which is the number for a positional one and the word for a named one.
527 /// A bare `?` is numbered by where it was written, so the identifier is there either way.
528 name: StrRef,
529 },
530 /// A bracketed list of expressions, `[a, b, c]`, which is a LIST value.
531 List {
532 /// The items, as a run of [`ExprRef`], in the order they were written.
533 items: Slice,
534 },
535 /// A parenthesised list of more than one expression, which is a row value.
536 Row {
537 /// The items, as a run of [`ExprRef`].
538 items: Slice,
539 },
540 /// A scalar subquery, `(SELECT ...)` where an expression is expected.
541 Subquery {
542 /// The query.
543 query: QueryRef,
544 },
545}
546
547/// One `WHEN a THEN b`.
548#[derive(Debug, Clone, Copy, PartialEq, Eq)]
549pub struct CaseArm {
550 /// The `WHEN`.
551 pub when: ExprRef,
552 /// The `THEN`.
553 pub then: ExprRef,
554}
555
556/// Which literal.
557#[derive(Debug, Clone, Copy, PartialEq, Eq)]
558pub enum LiteralKind {
559 /// A number, kept as text because the width it wants depends on where it lands.
560 Number,
561 /// A string.
562 String,
563 /// `NULL`.
564 Null,
565 /// `TRUE`.
566 True,
567 /// `FALSE`.
568 False,
569}
570
571/// A prefix or postfix operator.
572#[derive(Debug, Clone, Copy, PartialEq, Eq)]
573pub enum UnaryOp {
574 /// `NOT x`.
575 Not,
576 /// `-x`.
577 Negate,
578 /// `+x`, which is a no-op that still has to survive to the binder so that `+'a'` errors.
579 Plus,
580 /// `~x`.
581 BitNot,
582 /// `x!`.
583 Factorial,
584 /// `x IS NULL` or `x ISNULL`.
585 IsNull,
586 /// `x IS NOT NULL` or `x NOTNULL`.
587 IsNotNull,
588 /// `x IS TRUE`.
589 IsTrue,
590 /// `x IS NOT TRUE`.
591 IsNotTrue,
592 /// `x IS FALSE`.
593 IsFalse,
594 /// `x IS NOT FALSE`.
595 IsNotFalse,
596 /// `x IS UNKNOWN`.
597 IsUnknown,
598 /// `x IS NOT UNKNOWN`.
599 IsNotUnknown,
600}
601
602/// An infix operator.
603///
604/// The list is the dialect and not a general idea of what operators are. `Named` is the one open
605/// door, because `OperatorLiteral` in the grammar takes any run of operator characters that is not
606/// already a token, and rejecting that here would reject SQL DuckDB accepts.
607#[derive(Debug, Clone, Copy, PartialEq, Eq)]
608pub enum BinaryOp {
609 /// `OR`.
610 Or,
611 /// `AND`.
612 And,
613 /// `=` or `==`.
614 Eq,
615 /// `!=` or `<>`.
616 NotEq,
617 /// `<`.
618 Lt,
619 /// `>`.
620 Gt,
621 /// `<=`.
622 LtEq,
623 /// `>=`.
624 GtEq,
625 /// `IS DISTINCT FROM`.
626 IsDistinctFrom,
627 /// `IS NOT DISTINCT FROM`.
628 IsNotDistinctFrom,
629 /// `+`.
630 Add,
631 /// `-`.
632 Subtract,
633 /// `*`.
634 Multiply,
635 /// `/`.
636 Divide,
637 /// `//`, integer division.
638 IntegerDivide,
639 /// `%`.
640 Modulo,
641 /// `^` or `**`.
642 Power,
643 /// `&`.
644 BitAnd,
645 /// `|`.
646 BitOr,
647 /// `<<`.
648 ShiftLeft,
649 /// `>>`.
650 ShiftRight,
651 /// `||`.
652 Concat,
653 /// `LIKE` or `~~`.
654 Like,
655 /// `NOT LIKE` or `!~~`.
656 NotLike,
657 /// `ILIKE` or `~~*`.
658 ILike,
659 /// `NOT ILIKE` or `!~~*`.
660 NotILike,
661 /// `GLOB` or `~~~`.
662 Glob,
663 /// `SIMILAR TO`.
664 SimilarTo,
665 /// `!~`, which the grammar calls the not-similar-to operator.
666 NotSimilarTo,
667 /// `~`, a regex match.
668 Regex,
669 /// `~*`, a case insensitive regex match.
670 RegexInsensitive,
671 /// `!~*`, a negated case insensitive regex match.
672 NotRegexInsensitive,
673 /// `COLLATE`.
674 Collate,
675 /// `AT TIME ZONE`.
676 AtTimeZone,
677 /// `->`.
678 Arrow,
679 /// `->>`.
680 LongArrow,
681 /// `@>`, contains.
682 Contains,
683 /// `<@`, contained by.
684 ContainedBy,
685 /// `&&`, overlaps.
686 Overlaps,
687 /// `^@`, starts with.
688 StartsWith,
689 /// `<<=`, an inet operator.
690 InetContainedByOrEq,
691 /// `>>=`, an inet operator.
692 InetContainsOrEq,
693 /// An operator the dialect does not name, which DuckDB resolves as a binary function of that
694 /// name. `a <=> b` is the shape.
695 Named(StrRef),
696}
697
698/// A parsed statement or script, with every arena it points into.
699///
700/// Cheap to clone, cheap to send, and self contained: no index in here refers to anything outside
701/// it, and nothing in here borrows the query text. The text is copied into `strings` on the way in,
702/// which costs one allocation per distinct identifier and buys an `Ast` that outlives the string it
703/// came from.
704#[derive(Debug, Clone, Default, PartialEq, Eq)]
705pub struct Ast {
706 /// The statements in the script, in order.
707 pub statements: Vec<Statement>,
708 /// The query arena.
709 pub queries: Vec<Query>,
710 /// The select arena.
711 pub selects: Vec<Select>,
712 /// The expression arena.
713 pub exprs: Vec<Expr>,
714 /// The from-item arena.
715 pub sources: Vec<Source>,
716 /// Interned text. Identifiers keep the case they were written in, because DuckDB does not fold
717 /// it at any point, including for quoted identifiers.
718 pub strings: Vec<String>,
719 /// Backing store for every [`Slice`] of names.
720 pub parts: Vec<StrRef>,
721 /// Backing store for every [`Slice`] of expressions.
722 pub expr_lists: Vec<ExprRef>,
723 /// Backing store for every [`Slice`] of from items.
724 pub source_lists: Vec<SourceRef>,
725 /// Backing store for every [`Slice`] of target list entries.
726 pub targets: Vec<Target>,
727 /// Backing store for every [`Slice`] of order by entries.
728 pub order_items: Vec<OrderItem>,
729 /// Backing store for every [`Slice`] of case arms.
730 pub case_arms: Vec<CaseArm>,
731 /// The `CREATE TABLE` arena.
732 pub create_tables: Vec<CreateTable>,
733 /// The `CREATE VIEW` arena.
734 pub create_views: Vec<CreateView>,
735 /// The `DROP TABLE` arena.
736 pub drop_tables: Vec<DropTable>,
737 /// The `INSERT` arena.
738 pub inserts: Vec<Insert>,
739 /// Backing store for every [`Slice`] of column definitions.
740 pub column_defs: Vec<ColumnDef>,
741 /// Backing store for every [`Slice`] of names, which is a name list rather than a name.
742 pub name_lists: Vec<Slice>,
743 /// Backing store for the rows of a `VALUES`, each of which is a run of expressions.
744 pub rows: Vec<Slice>,
745}
746
747impl Ast {
748 /// The text behind a [`StrRef`], or the empty string for `NONE`.
749 pub fn string(&self, index: StrRef) -> &str {
750 if index == NONE { "" } else { &self.strings[index as usize] }
751 }
752
753 /// Every parameter identifier the statement uses, once each, in the order they were written.
754 ///
755 /// The arena is built as the walk goes, so its order is the written order, and a parameter used
756 /// twice is one identifier here because it is one value to provide.
757 pub fn parameters(&self) -> Vec<&str> {
758 let mut found: Vec<&str> = Vec::new();
759 for expr in &self.exprs {
760 if let Expr::Parameter { name } = *expr {
761 let name = self.string(name);
762 if !found.contains(&name) {
763 found.push(name);
764 }
765 }
766 }
767 found
768 }
769
770 /// The parts of a name, outermost first.
771 pub fn name(&self, slice: Slice) -> impl Iterator<Item = &str> {
772 self.parts[slice.range()].iter().map(|&part| self.string(part))
773 }
774
775 /// A name written back out with dots between the parts, for error messages and tests.
776 pub fn name_text(&self, slice: Slice) -> String {
777 self.name(slice).collect::<Vec<_>>().join(".")
778 }
779
780 /// One expression.
781 pub fn expr(&self, index: ExprRef) -> Expr {
782 self.exprs[index as usize]
783 }
784
785 /// One from item.
786 pub fn source(&self, index: SourceRef) -> Source {
787 self.sources[index as usize]
788 }
789
790 /// One query.
791 pub fn query(&self, index: QueryRef) -> Query {
792 self.queries[index as usize]
793 }
794
795 /// One select block.
796 pub fn select(&self, index: SelectRef) -> Select {
797 self.selects[index as usize]
798 }
799
800 /// The expressions of a list.
801 pub fn expr_list(&self, slice: Slice) -> &[ExprRef] {
802 &self.expr_lists[slice.range()]
803 }
804
805 /// The from items of a list.
806 pub fn source_list(&self, slice: Slice) -> &[SourceRef] {
807 &self.source_lists[slice.range()]
808 }
809
810 /// The entries of a target list.
811 pub fn target_list(&self, slice: Slice) -> &[Target] {
812 &self.targets[slice.range()]
813 }
814
815 /// The entries of an order by list.
816 pub fn order_list(&self, slice: Slice) -> &[OrderItem] {
817 &self.order_items[slice.range()]
818 }
819
820 /// The arms of a case.
821 pub fn arm_list(&self, slice: Slice) -> &[CaseArm] {
822 &self.case_arms[slice.range()]
823 }
824
825 /// One `CREATE TABLE`.
826 pub fn create_table(&self, index: CreateTableRef) -> CreateTable {
827 self.create_tables[index as usize]
828 }
829
830 /// One `CREATE VIEW`.
831 pub fn create_view(&self, index: CreateViewRef) -> CreateView {
832 self.create_views[index as usize]
833 }
834
835 /// One `DROP TABLE`.
836 pub fn drop_table(&self, index: DropTableRef) -> DropTable {
837 self.drop_tables[index as usize]
838 }
839
840 /// One `INSERT`.
841 pub fn insert(&self, index: InsertRef) -> Insert {
842 self.inserts[index as usize]
843 }
844
845 /// The column definitions of a `CREATE TABLE`.
846 pub fn column_defs(&self, slice: Slice) -> &[ColumnDef] {
847 &self.column_defs[slice.range()]
848 }
849
850 /// The names of a name list, each of which is itself a run of parts.
851 pub fn name_list(&self, slice: Slice) -> &[Slice] {
852 &self.name_lists[slice.range()]
853 }
854
855 /// The rows of a `VALUES`, each of which is itself a run of expressions.
856 pub fn rows(&self, slice: Slice) -> &[Slice] {
857 &self.rows[slice.range()]
858 }
859
860 /// How many nodes the whole tree is, across every arena.
861 ///
862 /// The number to watch when the transformer changes. A parse tree of five thousand nodes that
863 /// becomes an AST of thirty is the twenty precedence levels being thrown away, which is the
864 /// whole reason this module exists.
865 pub fn node_count(&self) -> usize {
866 self.queries.len() + self.selects.len() + self.exprs.len() + self.sources.len()
867 }
868}