Skip to main content

rudb_parse/
transform.rs

1//! From the parse tree to the AST.
2//!
3//! This is the one module that reads rule names out of the vendored grammar, and that is deliberate
4//! containment: an upstream bump that renames a rule breaks a match arm here and nothing else in
5//! the repository. `spec/04-architecture.md` section 4.5 says this transformer is ours and has to
6//! be total over the rule table, and total is the load bearing word. Every rule reaches a defined
7//! answer. For the ones this milestone covers that answer is an AST node, and for the rest it is a
8//! `Not implemented` error naming the construct, which is what DuckDB itself answers for syntax it
9//! parses and does not support. There is no arm that panics and none that silently drops a clause,
10//! because a dropped clause is a wrong answer and a wrong answer is worse than an error.
11//!
12//! The mechanism that makes it tractable is the default arm. Two thirds of the parse tree is the
13//! expression precedence chain, twenty rules of the form `X <- Y Tail*` that exist to make the
14//! grammar unambiguous and that carry no meaning once it has been parsed. Rather than name all
15//! twenty, the expression walker handles the case where a rule matched something interesting and
16//! otherwise descends through any node with exactly one child. That is not a shortcut. It is the
17//! statement that a rule with one child said nothing, which is true of every chain link, and it
18//! means the twenty first precedence level upstream adds costs us nothing.
19
20use std::collections::HashMap;
21
22use rudb_common::{Error, Result};
23
24use crate::ast::{
25    Ast, BinaryOp, CaseArm, ColumnDef, CreateTable, CreateView, Distinct, DropTable, Expr, ExprRef,
26    Insert, JoinKind, LiteralKind, Nulls, Order, OrderItem, Quantifier, Query, QueryBody, QueryRef,
27    Scope, Select, SelectRef, SetOp, Setting, Slice, Source, SourceRef, Statement, StrRef, Target,
28    UnaryOp,
29};
30use crate::generated::rules::PROGRAM;
31use crate::matcher::{NONE, Tree, parse_tokens};
32use crate::token::{Kind, Token};
33use crate::tokenize::tokenize;
34
35/// Parse a script and transform it into the AST.
36///
37/// The tokens are produced once and handed to both halves. Calling [`crate::parse`] here instead
38/// would be shorter and would tokenize the query a second time, which `cargo xtask bench` prices
39/// at about a tenth of the whole front end.
40pub fn parse_ast(query: &str) -> Result<Ast> {
41    let tokens = tokenize(query)?;
42    let tree = parse_tokens(query, &tokens, PROGRAM, true)?;
43    transform(query, &tokens, &tree)
44}
45
46/// Transform a parse tree that has already been produced.
47pub fn transform(query: &str, tokens: &[Token], tree: &Tree) -> Result<Ast> {
48    let mut transform = Transform {
49        query,
50        tokens,
51        tree,
52        ast: Ast::default(),
53        interned: HashMap::new(),
54        anonymous: 0,
55    };
56    transform.program(tree.root())?;
57    Ok(transform.ast)
58}
59
60struct Transform<'a> {
61    query: &'a str,
62    tokens: &'a [Token],
63    tree: &'a Tree,
64    ast: Ast,
65    interned: HashMap<String, StrRef>,
66    /// How many bare `?` parameters have been seen, which is what numbers the next one.
67    anonymous: u32,
68}
69
70impl<'a> Transform<'a> {
71    // The parts that walk the parse tree without caring what it says.
72
73    /// The text a node covers.
74    fn text(&self, node: u32) -> &'a str {
75        self.tree.text(node, self.query, self.tokens)
76    }
77
78    /// The name of the rule a node is.
79    fn name(&self, node: u32) -> &'static str {
80        self.tree.name(node)
81    }
82
83    /// The children of a node.
84    ///
85    /// Returned with the tree's lifetime rather than the borrow of `self`, so that the caller can
86    /// iterate it while calling the `&mut self` methods that build the arena. Copying the `&Tree`
87    /// out first is what buys that, and it is why every walker here starts by doing so.
88    fn kids(&self, node: u32) -> impl Iterator<Item = u32> + use<'a> {
89        let tree = self.tree;
90        tree.children(node)
91    }
92
93    /// How many children a node has.
94    fn count(&self, node: u32) -> usize {
95        self.kids(node).count()
96    }
97
98    /// The n'th child, or `NONE`.
99    fn nth(&self, node: u32, n: usize) -> u32 {
100        self.kids(node).nth(n).unwrap_or(NONE)
101    }
102
103    /// The first child, or `NONE`.
104    fn first(&self, node: u32) -> u32 {
105        self.nth(node, 0)
106    }
107
108    /// The first child named `name`, or `NONE`.
109    ///
110    /// Optional parts of a sequence do not leave a placeholder behind, so `SimpleSelect` with a
111    /// `WHERE` and no `GROUP BY` has the where clause as its second child and a `SimpleSelect` with
112    /// neither has something else there. Positional indexing into an optional sequence is the
113    /// single easiest way to write a transformer that is subtly wrong, so nothing here does it.
114    fn find(&self, node: u32, name: &str) -> u32 {
115        self.kids(node).find(|&kid| self.name(kid) == name).unwrap_or(NONE)
116    }
117
118    /// Every leaf of a subtree, in order.
119    ///
120    /// A leaf is a rule that matched only terminals, which for a name is the identifier itself. It
121    /// is how all thirty odd spellings of a qualified name collapse into one walk: whether the
122    /// parse said `SchemaQualification ReservedTableQualification ReservedColumnName` or
123    /// `IdentifierDot IdentifierDot ColumnName`, the leaves are the parts in order.
124    fn leaves(&self, node: u32, out: &mut Vec<u32>) {
125        let mut any = false;
126        for kid in self.kids(node) {
127            any = true;
128            self.leaves(kid, &mut *out);
129        }
130        if !any {
131            out.push(node);
132        }
133    }
134
135    // The parts that build the arena.
136
137    /// Intern a string, returning its index.
138    fn intern(&mut self, text: &str) -> StrRef {
139        if let Some(&index) = self.interned.get(text) {
140            return index;
141        }
142        let index = u32::try_from(self.ast.strings.len())
143            .map_err(|_| Error::internal("more than four billion strings in one query"))
144            .unwrap_or(NONE);
145        self.ast.strings.push(text.to_string());
146        self.interned.insert(text.to_string(), index);
147        index
148    }
149
150    /// Push an expression and return its index.
151    fn push(&mut self, expr: Expr) -> ExprRef {
152        let index = self.ast.exprs.len() as u32;
153        self.ast.exprs.push(expr);
154        index
155    }
156
157    /// Push a from item and return its index.
158    fn push_source(&mut self, source: Source) -> SourceRef {
159        let index = self.ast.sources.len() as u32;
160        self.ast.sources.push(source);
161        index
162    }
163
164    /// Push a query and return its index.
165    fn push_query(&mut self, query: Query) -> QueryRef {
166        let index = self.ast.queries.len() as u32;
167        self.ast.queries.push(query);
168        index
169    }
170
171    /// Push a select and return its index.
172    fn push_select(&mut self, select: Select) -> SelectRef {
173        let index = self.ast.selects.len() as u32;
174        self.ast.selects.push(select);
175        index
176    }
177
178    /// Turn a vector of expressions into a slice of the expression list arena.
179    fn expr_slice(&mut self, items: Vec<ExprRef>) -> Slice {
180        let start = self.ast.expr_lists.len() as u32;
181        self.ast.expr_lists.extend(items);
182        Slice { start, len: self.ast.expr_lists.len() as u32 - start }
183    }
184
185    /// Turn a vector of strings into a slice of the name arena.
186    fn part_slice(&mut self, items: Vec<StrRef>) -> Slice {
187        let start = self.ast.parts.len() as u32;
188        self.ast.parts.extend(items);
189        Slice { start, len: self.ast.parts.len() as u32 - start }
190    }
191
192    /// Turn a vector of column definitions into a slice of the column arena.
193    fn column_def_slice(&mut self, items: Vec<ColumnDef>) -> Slice {
194        let start = self.ast.column_defs.len() as u32;
195        self.ast.column_defs.extend(items);
196        Slice { start, len: self.ast.column_defs.len() as u32 - start }
197    }
198
199    /// Turn a vector of targets into a slice of the target arena.
200    fn target_slice(&mut self, items: Vec<Target>) -> Slice {
201        let start = self.ast.targets.len() as u32;
202        self.ast.targets.extend(items);
203        Slice { start, len: self.ast.targets.len() as u32 - start }
204    }
205
206    /// Turn a vector of qualified names into a slice of the name list arena.
207    fn name_list_slice(&mut self, items: Vec<Slice>) -> Slice {
208        let start = self.ast.name_lists.len() as u32;
209        self.ast.name_lists.extend(items);
210        Slice { start, len: self.ast.name_lists.len() as u32 - start }
211    }
212
213    /// The error for a construct the transformer does not cover yet.
214    ///
215    /// Both halves matter. The text is what the user wrote, which is the only part they can act on,
216    /// and the rule name is what we act on, because it is the exact grammar rule to go implement.
217    fn unsupported<T>(&self, node: u32) -> Result<T> {
218        let text = self.text(node);
219        let text = if text.chars().count() > 60 {
220            let cut = text.char_indices().nth(60).map_or(text.len(), |(at, _)| at);
221            format!("{}...", &text[..cut])
222        } else {
223            text.to_string()
224        };
225        Err(Error::not_implemented(format!(
226            "{text} is not supported yet, the grammar rule is {}",
227            self.name(node)
228        )))
229    }
230
231    // Names.
232
233    /// One identifier out of a subtree, with the quoting and any trailing dot removed.
234    fn identifier(&mut self, node: u32) -> StrRef {
235        let mut leaves = Vec::new();
236        self.leaves(node, &mut leaves);
237        let text = leaves.last().map_or("", |&leaf| self.text(leaf));
238        let text = unquote(text.strip_suffix('.').unwrap_or(text));
239        self.intern(&text)
240    }
241
242    /// Every part of a qualified name, outermost first.
243    fn name_parts(&mut self, node: u32) -> Slice {
244        let mut leaves = Vec::new();
245        self.leaves(node, &mut leaves);
246        let mut parts = Vec::with_capacity(leaves.len());
247        for leaf in leaves {
248            let text = self.text(leaf);
249            // A node that covers no tokens is an optional part that was not written, and a bare
250            // `*` is the star and not a name part. Neither is a component of anything.
251            if text.is_empty() || text == "*" {
252                continue;
253            }
254            let text = unquote(text.strip_suffix('.').unwrap_or(text));
255            let interned = self.intern(&text);
256            parts.push(interned);
257        }
258        self.part_slice(parts)
259    }
260
261    // Statements.
262
263    /// `Program <- TopLevelStatement*`.
264    fn program(&mut self, node: u32) -> Result<()> {
265        for top in self.kids(node) {
266            // A script that ends in a semicolon produces a last `TopLevelStatement` whose only
267            // child is the end of input, because the grammar says `Statement? (';'+ / EndOfInput)`
268            // and both halves of that are happy to match nothing. It is a real node and it is not a
269            // statement, so it is dropped here rather than pretended away in the matcher.
270            let Some(statement) = self.kids(top).find(|&kid| self.name(kid) == "Statement") else {
271                continue;
272            };
273            let statement = self.statement(statement)?;
274            self.ast.statements.push(statement);
275        }
276        Ok(())
277    }
278
279    /// `Statement <- SelectStatement / ...`, twenty seven alternatives of which four are done.
280    fn statement(&mut self, node: u32) -> Result<Statement> {
281        let inner = self.first(node);
282        match self.name(inner) {
283            "SelectStatement" => {
284                let query = self.query(self.first(inner))?;
285                Ok(Statement::Query(query))
286            }
287            "CreateStatement" => self.create_statement(inner),
288            "DropStatement" => self.drop_statement(inner),
289            "InsertStatement" => self.insert_statement(inner),
290            "SetStatement" => self.set_statement(inner),
291            "ResetStatement" => self.reset_statement(inner),
292            _ => self.unsupported(inner),
293        }
294    }
295
296    /// `SetStatement <- 'SET' SetAssignmentOrTimeZone`.
297    ///
298    /// Of the three assignments, `StandardAssignment` is the one that is done. `SET SCHEMA` and
299    /// `SET TIME ZONE` are each a setting this database has nothing to do with yet, and they are a
300    /// refusal rather than a silent success, because a statement that says where to look for a
301    /// table and is ignored is a statement that changes an answer.
302    fn set_statement(&mut self, node: u32) -> Result<Statement> {
303        let inner = self.first(self.find(node, "SetAssignmentOrTimeZone"));
304        if self.name(inner) != "StandardAssignment" {
305            return self.unsupported(inner);
306        }
307        let (name, scope) = self.setting_name(self.find(inner, "SetVariableOrSetting"))?;
308        let assignment = self.find(inner, "SetAssignment");
309        let list = self.find(assignment, "VariableList");
310        let mut values = Vec::new();
311        for kid in self.kids(list) {
312            values.push(self.expr(kid)?);
313        }
314        // The grammar takes a list because `SET search_path = a, b` is a list in postgres. Nothing
315        // here has a setting that reads one, and taking the first of several would be worse than
316        // saying so.
317        let [value] = values[..] else {
318            return self.unsupported(list);
319        };
320        let index = self.ast.settings.len() as u32;
321        self.ast.settings.push(Setting { name, scope, value });
322        Ok(Statement::Set(index))
323    }
324
325    /// `ResetStatement <- 'RESET' SetVariableOrSetting`.
326    fn reset_statement(&mut self, node: u32) -> Result<Statement> {
327        let (name, scope) = self.setting_name(self.find(node, "SetVariableOrSetting"))?;
328        let index = self.ast.settings.len() as u32;
329        self.ast.settings.push(Setting { name, scope, value: NONE });
330        Ok(Statement::Reset(index))
331    }
332
333    /// `SetVariableOrSetting <- SetVariable / SetSetting`, where the setting carries a scope word.
334    ///
335    /// `SET VARIABLE x = 1` is the other alternative and is a different feature: a variable is a
336    /// value the session holds and `getvariable` reads back, where a setting is a knob on the
337    /// engine. Refused rather than treated as a setting of that name.
338    fn setting_name(&mut self, node: u32) -> Result<(StrRef, Scope)> {
339        let inner = self.first(node);
340        if self.name(inner) != "SetSetting" {
341            return self.unsupported(inner);
342        }
343        let written = self.find(inner, "SettingScope");
344        let scope = if written == NONE {
345            Scope::Unwritten
346        } else {
347            match self.name(self.first(written)) {
348                "GlobalScope" => Scope::Global,
349                "SessionScope" => Scope::Session,
350                "LocalScope" => Scope::Local,
351                _ => return self.unsupported(written),
352            }
353        };
354        Ok((self.identifier(self.find(inner, "SettingName")), scope))
355    }
356
357    /// `CreateStatement <- 'CREATE' OrReplace? Temporary? CreateStatementVariation`.
358    ///
359    /// Of the nine variations, `CreateTableStmt` and `CreateViewStmt` are the ones that are done.
360    /// The other seven are a macro, a sequence, a type, a schema, an index, a secret and a trigger,
361    /// and each of them is a catalog entry this database has no room for yet.
362    fn create_statement(&mut self, node: u32) -> Result<Statement> {
363        let or_replace = self.find(node, "OrReplace") != NONE;
364        let temporary = self.find(node, "Temporary") != NONE;
365        let variation = self.find(node, "CreateStatementVariation");
366        let inner = self.first(variation);
367        // duckdb refuses this pair in the parser, with a caret under the `NOT`, because none of its
368        // create rules has room for both. The vendored grammar has room for both, so the refusal is
369        // here instead, which is the same stage and therefore the same sentence.
370        if or_replace && self.find(inner, "IfNotExists") != NONE {
371            return Err(Error::parser(
372                "Cannot specify both OR REPLACE and IF NOT EXISTS within single create statement",
373            ));
374        }
375        match self.name(inner) {
376            "CreateTableStmt" => self.create_table_statement(inner, or_replace, temporary),
377            "CreateViewStmt" => self.create_view_statement(inner, or_replace, temporary),
378            _ => self.unsupported(inner),
379        }
380    }
381
382    /// `CreateTableStmt <- 'TABLE' IfNotExists? QualifiedName CreateTableDefinition`.
383    fn create_table_statement(
384        &mut self,
385        inner: u32,
386        or_replace: bool,
387        temporary: bool,
388    ) -> Result<Statement> {
389        let name = self.name_parts(self.find(inner, "QualifiedName"));
390        let if_not_exists = self.find(inner, "IfNotExists") != NONE;
391        let definition = self.find(inner, "CreateTableDefinition");
392        let body = self.first(definition);
393        let (columns, query) = match self.name(body) {
394            "CreateColumnList" => (self.column_list(body)?, NONE),
395            "CreateTableAs" => self.create_table_as(body)?,
396            _ => return self.unsupported(body),
397        };
398        let index = self.ast.create_tables.len() as u32;
399        self.ast.create_tables.push(CreateTable {
400            name,
401            columns,
402            query,
403            if_not_exists,
404            or_replace,
405            temporary,
406        });
407        Ok(Statement::CreateTable(index))
408    }
409
410    /// `CreateViewStmt <- CreateSecure? CreateRecursive? 'VIEW' IfNotExists? QualifiedName
411    /// InsertColumnList? WithList? 'AS' SelectStatementInternal`.
412    ///
413    /// The body is transformed here as well as kept as text. Transforming it is what makes a view
414    /// whose body does not parse a parse error at creation, which is where it belongs, and the text
415    /// is what the catalog keeps so that the body can be bound again at every reference.
416    fn create_view_statement(
417        &mut self,
418        inner: u32,
419        or_replace: bool,
420        temporary: bool,
421    ) -> Result<Statement> {
422        for kid in self.kids(inner) {
423            // `SECURE` is a column and row policy, `RECURSIVE` is a different shape of view
424            // entirely, and `WITH` carries options. Dropping any of the three silently would make a
425            // view that is not the view that was asked for.
426            if matches!(self.name(kid), "CreateSecure" | "CreateRecursive" | "WithList") {
427                return self.unsupported(kid);
428            }
429        }
430        let name = self.name_parts(self.find(inner, "QualifiedName"));
431        let if_not_exists = self.find(inner, "IfNotExists") != NONE;
432        let list = self.find(inner, "InsertColumnList");
433        let columns = if list == NONE {
434            Slice::default()
435        } else {
436            let mut parts = Vec::new();
437            for kid in self.kids(self.find(list, "ColumnList")) {
438                parts.push(self.identifier(kid));
439            }
440            self.part_slice(parts)
441        };
442        let body = self.find(inner, "SelectStatementInternal");
443        let sql = self.text(body).to_string();
444        let sql = self.intern(&sql);
445        let query = self.query(body)?;
446        let index = self.ast.create_views.len() as u32;
447        self.ast.create_views.push(CreateView {
448            name,
449            columns,
450            query,
451            sql,
452            if_not_exists,
453            or_replace,
454            temporary,
455        });
456        Ok(Statement::CreateView(index))
457    }
458
459    /// `CreateColumnList <- Parens(CreateTableColumnList?) PartitionSortedOptions? WithList?`.
460    fn column_list(&mut self, node: u32) -> Result<Slice> {
461        for kid in self.kids(node) {
462            if matches!(self.name(kid), "PartitionOptions" | "SortedOptions" | "WithList") {
463                return self.unsupported(kid);
464            }
465        }
466        let list = self.find(node, "CreateTableColumnList");
467        if list == NONE {
468            // `CREATE TABLE t ()` parses. It is a table of no columns, and the catalog is entitled
469            // to refuse it, but that is not this layer's refusal to make.
470            return Ok(Slice::default());
471        }
472        let mut defs = Vec::new();
473        for element in self.kids(list) {
474            let inner = self.first(element);
475            if self.name(inner) != "CreateTableColumnDefinition" {
476                // A table level `PRIMARY KEY`, `UNIQUE`, `CHECK` or `FOREIGN KEY`. Constraints are
477                // not enforced anywhere yet and silently dropping one is a wrong answer waiting to
478                // happen, so it is refused instead.
479                return self.unsupported(inner);
480            }
481            defs.push(self.column_definition(self.first(inner))?);
482        }
483        Ok(self.column_def_slice(defs))
484    }
485
486    /// `ColumnDefinition <- DottedIdentifier Type? GeneratedColumn? ConstraintNameClause?
487    /// ColumnConstraint*`.
488    fn column_definition(&mut self, node: u32) -> Result<ColumnDef> {
489        let name = self.identifier(self.find(node, "DottedIdentifier"));
490        let type_node = self.find(node, "Type");
491        let ty = if type_node == NONE {
492            NONE
493        } else {
494            let text = self.text(type_node).to_string();
495            self.intern(&text)
496        };
497        if self.find(node, "GeneratedColumn") != NONE {
498            return self.unsupported(self.find(node, "GeneratedColumn"));
499        }
500        let mut not_null = false;
501        for kid in self.kids(node) {
502            if self.name(kid) != "ColumnConstraint" {
503                continue;
504            }
505            let constraint = self.first(kid);
506            match self.name(constraint) {
507                "NotNullConstraint" => {
508                    not_null = self.name(self.first(constraint)) == "NotNullColumnConstraint";
509                }
510                _ => return self.unsupported(constraint),
511            }
512        }
513        Ok(ColumnDef { name, ty, not_null })
514    }
515
516    /// `CreateTableAs <- IdentifierList? PartitionSortedOptions? WithList? 'AS' Statement
517    /// WithData?`.
518    ///
519    /// The names in the `IdentifierList` become column definitions with no type, because the types
520    /// are the query's and only the names are the syntax's to say.
521    fn create_table_as(&mut self, node: u32) -> Result<(Slice, QueryRef)> {
522        for kid in self.kids(node) {
523            if matches!(
524                self.name(kid),
525                "PartitionOptions" | "SortedOptions" | "WithList" | "WithData"
526            ) {
527                return self.unsupported(kid);
528            }
529        }
530        let names = self.find(node, "IdentifierList");
531        let columns = if names == NONE {
532            Slice::default()
533        } else {
534            let mut defs = Vec::new();
535            for kid in self.kids(names) {
536                let name = self.identifier(kid);
537                defs.push(ColumnDef { name, ty: NONE, not_null: false });
538            }
539            self.column_def_slice(defs)
540        };
541        let statement = self.find(node, "Statement");
542        let inner = self.first(statement);
543        if self.name(inner) != "SelectStatement" {
544            return self.unsupported(inner);
545        }
546        let query = self.query(self.first(inner))?;
547        Ok((columns, query))
548    }
549
550    /// `DropStatement <- 'DROP' DropEntries DropBehavior?`.
551    ///
552    /// `DropTable <- TableOrView IfExists? List(BaseTableName)`, and `TableOrView` covers `VIEW`
553    /// and `MATERIALIZED VIEW` as well as `TABLE`, so it is checked rather than assumed. The first
554    /// two are done and a materialized view is not a thing this database has.
555    fn drop_statement(&mut self, node: u32) -> Result<Statement> {
556        if self.find(node, "DropBehavior") != NONE {
557            return self.unsupported(self.find(node, "DropBehavior"));
558        }
559        let entries = self.find(node, "DropEntries");
560        let inner = self.first(entries);
561        if self.name(inner) != "DropTable" {
562            return self.unsupported(inner);
563        }
564        let kind = self.find(inner, "TableOrView");
565        let view = match self.name(self.first(kind)) {
566            "CommentTable" => false,
567            "CommentView" => true,
568            _ => return self.unsupported(kind),
569        };
570        let if_exists = self.find(inner, "IfExists") != NONE;
571        let mut names = Vec::new();
572        for kid in self.kids(inner) {
573            if self.name(kid) == "BaseTableName" {
574                names.push(self.name_parts(kid));
575            }
576        }
577        let names = self.name_list_slice(names);
578        let index = self.ast.drop_tables.len() as u32;
579        self.ast.drop_tables.push(DropTable { names, if_exists, view });
580        Ok(Statement::DropTable(index))
581    }
582
583    /// `InsertStatement <- ... InsertTarget InsertColumnList? InsertValues ...`.
584    ///
585    /// `ON CONFLICT`, `RETURNING`, `BY NAME`, `BY POSITION`, `OR REPLACE` and the rest of the
586    /// clauses the grammar hangs off this are each a refusal, because every one of them changes
587    /// what the statement means and none of them changes it in a way anything downstream would
588    /// notice if it were dropped.
589    fn insert_statement(&mut self, node: u32) -> Result<Statement> {
590        for kid in self.kids(node) {
591            if matches!(
592                self.name(kid),
593                "InsertTarget" | "InsertColumnList" | "InsertValues" | "WithClause"
594            ) {
595                continue;
596            }
597            return self.unsupported(kid);
598        }
599        if self.find(node, "WithClause") != NONE {
600            return self.unsupported(self.find(node, "WithClause"));
601        }
602        let name = self.name_parts(self.find(self.find(node, "InsertTarget"), "BaseTableName"));
603        let list = self.find(node, "InsertColumnList");
604        let columns = if list == NONE {
605            Slice::default()
606        } else {
607            let mut parts = Vec::new();
608            for kid in self.kids(self.find(list, "ColumnList")) {
609                parts.push(self.identifier(kid));
610            }
611            self.part_slice(parts)
612        };
613        let values = self.find(node, "InsertValues");
614        let inner = self.first(values);
615        if self.name(inner) != "SelectInsertValues" {
616            return self.unsupported(inner);
617        }
618        let source = self.query(self.find(inner, "SelectStatementInternal"))?;
619        let index = self.ast.inserts.len() as u32;
620        self.ast.inserts.push(Insert { name, columns, source });
621        Ok(Statement::Insert(index))
622    }
623
624    /// `SelectStatementInternal <- WithClause? SelectSetOpChain ResultModifiers?`.
625    fn query(&mut self, node: u32) -> Result<QueryRef> {
626        if self.find(node, "WithClause") != NONE {
627            return self.unsupported(self.find(node, "WithClause"));
628        }
629        let chain = self.find(node, "SelectSetOpChain");
630        if chain == NONE {
631            return self.unsupported(node);
632        }
633        let query = self.set_op_chain(chain)?;
634        let modifiers = self.find(node, "ResultModifiers");
635        if modifiers != NONE {
636            self.result_modifiers(query, modifiers)?;
637        }
638        Ok(query)
639    }
640
641    /// `SelectSetOpChain <- IntersectChain SelectSetOpChainTail*`, left associative.
642    fn set_op_chain(&mut self, node: u32) -> Result<QueryRef> {
643        let mut kids = self.kids(node);
644        let head = kids.next().unwrap_or(NONE);
645        let mut left = self.intersect_chain(head)?;
646        for tail in kids {
647            // `SelectSetOpChainTail <- SetopClause IntersectChain`.
648            let clause = self.first(tail);
649            let (op, quantifier, by_name) = self.setop_clause(clause)?;
650            let right = self.intersect_chain(self.nth(tail, 1))?;
651            left = self.push_query(Query::bare(QueryBody::SetOp {
652                op,
653                quantifier,
654                by_name,
655                left,
656                right,
657            }));
658        }
659        Ok(left)
660    }
661
662    /// `IntersectChain <- SelectAtom IntersectChainTail*`, which binds tighter than union.
663    fn intersect_chain(&mut self, node: u32) -> Result<QueryRef> {
664        let mut kids = self.kids(node);
665        let head = kids.next().unwrap_or(NONE);
666        let mut left = self.select_atom(head)?;
667        for tail in kids {
668            // `IntersectChainTail <- SetIntersectClause SelectAtom`.
669            let clause = self.first(tail);
670            let quantifier = self.quantifier(self.find(clause, "DistinctOrAll"));
671            let right = self.select_atom(self.nth(tail, 1))?;
672            left = self.push_query(Query::bare(QueryBody::SetOp {
673                op: SetOp::Intersect,
674                quantifier,
675                by_name: false,
676                left,
677                right,
678            }));
679        }
680        Ok(left)
681    }
682
683    /// `SetopClause <- SetopType DistinctOrAll? ByName?`.
684    fn setop_clause(&mut self, node: u32) -> Result<(SetOp, Quantifier, bool)> {
685        let kind = self.find(node, "SetopType");
686        let op = match self.name(self.first(kind)) {
687            "SetopUnion" => SetOp::Union,
688            "SetopExcept" => SetOp::Except,
689            _ => return self.unsupported(kind),
690        };
691        let quantifier = self.quantifier(self.find(node, "DistinctOrAll"));
692        Ok((op, quantifier, self.find(node, "ByName") != NONE))
693    }
694
695    /// `DistinctOrAll <- DistinctKeyword / AllKeyword`, absent included.
696    fn quantifier(&self, node: u32) -> Quantifier {
697        if node == NONE {
698            return Quantifier::Unstated;
699        }
700        match self.name(self.first(node)) {
701            "DistinctKeyword" => Quantifier::Distinct,
702            "AllKeyword" => Quantifier::All,
703            _ => Quantifier::Unstated,
704        }
705    }
706
707    /// `SelectAtom <- SelectParens / SelectStatementType`.
708    fn select_atom(&mut self, node: u32) -> Result<QueryRef> {
709        let inner = self.first(node);
710        match self.name(inner) {
711            // `SelectParens <- Parens(SelectStatementInternal)`, so the parens buy a query that
712            // carries its own order by and limit and nothing else.
713            "SelectParens" => self.query(self.first(inner)),
714            "SelectStatementType" => {
715                let kind = self.first(inner);
716                match self.name(kind) {
717                    "OptionalParensSimpleSelect" => {
718                        let select = self.simple_select(self.unwrap_parens(kind))?;
719                        Ok(self.push_query(Query::bare(QueryBody::Select(select))))
720                    }
721                    "ValuesClause" => {
722                        let rows = self.values_clause(kind)?;
723                        Ok(self.push_query(Query::bare(QueryBody::Values(rows))))
724                    }
725                    "DescribeStatement" => self.describe_statement(kind),
726                    _ => self.unsupported(kind),
727                }
728            }
729            _ => self.unsupported(inner),
730        }
731    }
732
733    /// `DescribeStatement <- ShowTables / ShowDeprecatedSelect / DescribeSelect / ShowAllTables /
734    /// ShowByName / DescribeByName`.
735    ///
736    /// Two of the six are done and they are the two that describe a relation. The four that are
737    /// not are `SHOW`, which is a different statement wearing this rule: three of its four forms
738    /// list what the database has rather than what a query returns, and the fourth is `SHOW <query>`,
739    /// which upstream documents as deprecated. Describing something through a deprecated spelling
740    /// is not worth implementing before the spelling that replaced it has users.
741    ///
742    /// `SUMMARIZE` shares `DescribeByName` and `DescribeSelect` with `DESCRIBE` and is refused
743    /// here, because it returns twelve columns of statistics rather than six of schema and reading
744    /// it as a describe would answer a different question than the one that was asked.
745    fn describe_statement(&mut self, node: u32) -> Result<QueryRef> {
746        let inner = self.first(node);
747        match self.name(inner) {
748            "DescribeSelect" => {
749                self.describe_and_not_summarize(inner)?;
750                let query = self.query(self.find(inner, "SelectStatementInternal"))?;
751                Ok(self.push_query(Query::bare(QueryBody::Describe(query))))
752            }
753            "DescribeByName" => {
754                self.describe_and_not_summarize(inner)?;
755                let target = self.find(inner, "DescribeTarget");
756                if target == NONE {
757                    return self.unsupported(inner);
758                }
759                let source = self.describe_target(target)?;
760                let query = self.star_over(source);
761                Ok(self.push_query(Query::bare(QueryBody::Describe(query))))
762            }
763            _ => self.unsupported(inner),
764        }
765    }
766
767    /// `DescribeOrSummarize <- DescribeRule / Summarize`, where only the first is done.
768    fn describe_and_not_summarize(&mut self, node: u32) -> Result<()> {
769        let word = self.find(node, "DescribeOrSummarize");
770        if word == NONE || self.name(self.first(word)) != "DescribeRule" {
771            return self.unsupported(if word == NONE { node } else { word });
772        }
773        Ok(())
774    }
775
776    /// `DescribeTarget <- DescribeBaseTableName / DescribeStringLiteral`, as a source to read from.
777    ///
778    /// Both become a `FROM` item and not a lookup of their own, because the string form is the
779    /// replacement scan and the binder already knows how to turn `'hits.parquet'` into a reader.
780    /// A name that is a table, a view, a file or nothing at all then gets one answer from one place.
781    fn describe_target(&mut self, node: u32) -> Result<SourceRef> {
782        let inner = self.first(node);
783        let name = match self.name(inner) {
784            "DescribeBaseTableName" => self.name_parts(self.find(inner, "BaseTableName")),
785            "DescribeStringLiteral" => {
786                let text = self.string_value(self.find(inner, "StringLiteral"));
787                let part = self.intern(&text);
788                self.part_slice(vec![part])
789            }
790            _ => return self.unsupported(inner),
791        };
792        Ok(self.push_source(Source::Table { name, alias: NONE, columns: Slice::default() }))
793    }
794
795    /// `SELECT * FROM <source>`, which is what `DESCRIBE t` means.
796    fn star_over(&mut self, source: SourceRef) -> QueryRef {
797        let star =
798            self.push(Expr::Star { qualifier: Slice::default(), replacements: Slice::default() });
799        let targets = self.target_slice(vec![Target { expr: star, alias: NONE }]);
800        let start = self.ast.source_lists.len() as u32;
801        self.ast.source_lists.push(source);
802        let from = Slice { start, len: 1 };
803        let select = self.push_select(Select { targets, from, ..Select::empty() });
804        self.push_query(Query::bare(QueryBody::Select(select)))
805    }
806
807    /// `ValuesClause <- 'VALUES' List(ValuesExpressions)`, each of which is `Parens(List(Expression))`.
808    ///
809    /// The rows are not checked against each other for width here. Two rows of different widths
810    /// parse, and saying so is the binder's job, because the message wants to name the column count
811    /// it expected and the parser does not know it for `INSERT` where the table decides.
812    fn values_clause(&mut self, node: u32) -> Result<Slice> {
813        let mut rows = Vec::new();
814        for kid in self.kids(node) {
815            if self.name(kid) != "ValuesExpressions" {
816                continue;
817            }
818            let mut items = Vec::new();
819            for expr in self.kids(kid) {
820                items.push(self.expr(expr)?);
821            }
822            let slice = self.expr_slice(items);
823            rows.push(slice);
824        }
825        let start = self.ast.rows.len() as u32;
826        self.ast.rows.extend(rows);
827        Ok(Slice { start, len: self.ast.rows.len() as u32 - start })
828    }
829
830    /// `OptionalParensSimpleSelect <- SimpleSelectParens / SimpleSelect`, down to the select.
831    fn unwrap_parens(&self, node: u32) -> u32 {
832        let mut node = self.first(node);
833        while self.name(node) == "SimpleSelectParens" {
834            node = self.first(node);
835        }
836        node
837    }
838
839    /// `ResultModifiers <- OrderByClause? LimitOffset?`.
840    fn result_modifiers(&mut self, query: QueryRef, node: u32) -> Result<()> {
841        let order = self.find(node, "OrderByClause");
842        if order != NONE {
843            let (items, all) = self.order_by(order)?;
844            let start = self.ast.order_items.len() as u32;
845            self.ast.order_items.extend(items);
846            self.ast.queries[query as usize].order_by =
847                Slice { start, len: self.ast.order_items.len() as u32 - start };
848            self.ast.queries[query as usize].order_by_all = all;
849        }
850        let limit = self.find(node, "LimitOffset");
851        if limit != NONE {
852            self.limit_offset(query, self.first(limit))?;
853        }
854        Ok(())
855    }
856
857    /// The four spellings of a limit and an offset, in either order and either one alone.
858    fn limit_offset(&mut self, query: QueryRef, node: u32) -> Result<()> {
859        match self.name(node) {
860            "LimitOffsetClause" | "OffsetLimitClause" => {
861                let limit = self.find(node, "LimitClause");
862                if limit != NONE {
863                    self.limit(query, limit)?;
864                }
865                let offset = self.find(node, "OffsetClause");
866                if offset != NONE {
867                    self.offset(query, offset)?;
868                }
869                Ok(())
870            }
871            _ => self.unsupported(node),
872        }
873    }
874
875    /// `LimitClause <- 'LIMIT' LimitValue`.
876    fn limit(&mut self, query: QueryRef, node: u32) -> Result<()> {
877        let value = self.first(node);
878        let inner = self.first(value);
879        match self.name(inner) {
880            // `LIMIT ALL` is no limit at all, which is what an absent limit already means.
881            "LimitAll" => Ok(()),
882            // `LimitExpression <- Expression '%'?`. The percent sign is a terminal so it leaves no
883            // node behind, and the only thing that says it was written is the text of the rule that
884            // matched it.
885            "LimitExpression" => {
886                let expr = self.expr(self.first(inner))?;
887                self.ast.queries[query as usize].limit = expr;
888                self.ast.queries[query as usize].limit_percent = self.text(inner).ends_with('%');
889                Ok(())
890            }
891            "LimitLiteralPercent" => {
892                let expr = self.expr(self.first(inner))?;
893                self.ast.queries[query as usize].limit = expr;
894                self.ast.queries[query as usize].limit_percent = true;
895                Ok(())
896            }
897            _ => self.unsupported(inner),
898        }
899    }
900
901    /// `OffsetClause <- 'OFFSET' OffsetValue`, where `OffsetValue <- Expression RowOrRows?`.
902    fn offset(&mut self, query: QueryRef, node: u32) -> Result<()> {
903        let value = self.first(node);
904        let expr = self.expr(self.first(value))?;
905        self.ast.queries[query as usize].offset = expr;
906        Ok(())
907    }
908
909    /// `SimpleSelect <- SelectFrom WhereClause? GroupByClause? HavingClause? WindowClause?
910    /// QualifyClause? SampleClause?`.
911    fn simple_select(&mut self, node: u32) -> Result<SelectRef> {
912        for name in ["WindowClause", "QualifyClause", "SampleClause"] {
913            let clause = self.find(node, name);
914            if clause != NONE {
915                return self.unsupported(clause);
916            }
917        }
918        let mut select = Select::empty();
919        self.select_from(&mut select, self.first(node))?;
920        let filter = self.find(node, "WhereClause");
921        if filter != NONE {
922            select.filter = self.expr(self.first(filter))?;
923        }
924        let group = self.find(node, "GroupByClause");
925        if group != NONE {
926            self.group_by(&mut select, self.first(group))?;
927        }
928        let having = self.find(node, "HavingClause");
929        if having != NONE {
930            select.having = self.expr(self.first(having))?;
931        }
932        Ok(self.push_select(select))
933    }
934
935    /// `SelectFrom <- SelectFromClause / FromSelectClause`, which is `SELECT ... FROM ...` and
936    /// DuckDB's `FROM ... SELECT ...` written the other way round.
937    fn select_from(&mut self, select: &mut Select, node: u32) -> Result<()> {
938        let clause = self.first(node);
939        let targets = self.find(clause, "SelectClause");
940        let from = self.find(clause, "FromClause");
941        if from != NONE {
942            select.from = self.sources(from)?;
943        }
944        if targets == NONE {
945            // `FROM t` on its own. DuckDB reads it as `SELECT * FROM t`, and inventing the star
946            // here rather than in the binder keeps the binder from having to know the shape of the
947            // clause that was missing.
948            let star = self
949                .push(Expr::Star { qualifier: Slice::default(), replacements: Slice::default() });
950            let start = self.ast.targets.len() as u32;
951            self.ast.targets.push(Target { expr: star, alias: NONE });
952            select.targets = Slice { start, len: 1 };
953            return Ok(());
954        }
955        self.select_clause(select, targets)
956    }
957
958    /// `SelectClause <- 'SELECT' DistinctClause? TargetList?`.
959    fn select_clause(&mut self, select: &mut Select, node: u32) -> Result<()> {
960        let distinct = self.find(node, "DistinctClause");
961        if distinct != NONE {
962            let inner = self.first(distinct);
963            select.distinct = match self.name(inner) {
964                // `SELECT ALL` is the default spelled out.
965                "DistinctAll" => Distinct::No,
966                "DistinctOn" => {
967                    let on = self.find(inner, "DistinctOnTargets");
968                    if on == NONE {
969                        Distinct::Yes
970                    } else {
971                        let mut items = Vec::new();
972                        for kid in self.kids(on) {
973                            items.push(self.expr(kid)?);
974                        }
975                        Distinct::On(self.expr_slice(items))
976                    }
977                }
978                _ => return self.unsupported(inner),
979            };
980        }
981        let list = self.find(node, "TargetList");
982        if list == NONE {
983            return Ok(());
984        }
985        let mut targets = Vec::new();
986        for kid in self.kids(list) {
987            targets.push(self.target(kid)?);
988        }
989        select.targets = self.target_slice(targets);
990        Ok(())
991    }
992
993    /// `AliasedExpression <- ColIdExpression / ExpressionAsCollabel / ExpressionOptIdentifier`.
994    fn target(&mut self, node: u32) -> Result<Target> {
995        let inner = self.first(node);
996        match self.name(inner) {
997            // `ColIdExpression <- ColId ':' Expression`, the alias written first.
998            "ColIdExpression" => {
999                let alias = self.identifier(self.first(inner));
1000                let expr = self.expr(self.nth(inner, 1))?;
1001                Ok(Target { expr, alias })
1002            }
1003            "ExpressionAsCollabel" => {
1004                let expr = self.expr(self.first(inner))?;
1005                let alias = self.identifier(self.nth(inner, 1));
1006                Ok(Target { expr, alias })
1007            }
1008            "ExpressionOptIdentifier" => {
1009                let expr = self.expr(self.first(inner))?;
1010                let alias =
1011                    if self.count(inner) > 1 { self.identifier(self.nth(inner, 1)) } else { NONE };
1012                Ok(Target { expr, alias })
1013            }
1014            _ => self.unsupported(inner),
1015        }
1016    }
1017
1018    /// `GroupByClause <- 'GROUP' 'BY' GroupByExpressions`.
1019    fn group_by(&mut self, select: &mut Select, node: u32) -> Result<()> {
1020        let inner = self.first(node);
1021        match self.name(inner) {
1022            "GroupByAll" => {
1023                select.group_by_all = true;
1024                Ok(())
1025            }
1026            "GroupByList" => {
1027                let mut items = Vec::new();
1028                for kid in self.kids(inner) {
1029                    // `GroupByExpression <- EmptyGroupingItem / CubeOrRollupClause /
1030                    // GroupingSetsClause / GroupByBaseExpression`.
1031                    let expression = self.first(kid);
1032                    if self.name(expression) != "GroupByBaseExpression" {
1033                        return self.unsupported(expression);
1034                    }
1035                    items.push(self.expr(self.first(expression))?);
1036                }
1037                select.group_by = self.expr_slice(items);
1038                Ok(())
1039            }
1040            _ => self.unsupported(inner),
1041        }
1042    }
1043
1044    /// `OrderByClause <- 'ORDER' 'BY' OrderByExpressions`, where `OrderByExpressions <- OrderByAll
1045    /// / OrderByExpressionList`.
1046    fn order_by(&mut self, node: u32) -> Result<(Vec<OrderItem>, bool)> {
1047        let inner = self.first(self.first(node));
1048        match self.name(inner) {
1049            "OrderByAll" => {
1050                let (order, nulls) = self.sort_options(inner);
1051                Ok((vec![OrderItem { expr: NONE, order, nulls }], true))
1052            }
1053            "OrderByExpressionList" => {
1054                let mut items = Vec::new();
1055                for kid in self.kids(inner) {
1056                    // `OrderByExpression <- Expression DescOrAsc? NullsFirstOrLast?`.
1057                    let expr = self.expr(self.first(kid))?;
1058                    let (order, nulls) = self.sort_options(kid);
1059                    items.push(OrderItem { expr, order, nulls });
1060                }
1061                Ok((items, false))
1062            }
1063            _ => self.unsupported(inner),
1064        }
1065    }
1066
1067    /// The direction and the null placement of one sort key, either of which may be unwritten.
1068    fn sort_options(&self, node: u32) -> (Order, Nulls) {
1069        let direction = self.find(node, "DescOrAsc");
1070        let order = if direction == NONE {
1071            Order::Unstated
1072        } else if self.name(self.first(direction)) == "DescendingOrder" {
1073            Order::Descending
1074        } else {
1075            Order::Ascending
1076        };
1077        let placement = self.find(node, "NullsFirstOrLast");
1078        let nulls = if placement == NONE {
1079            Nulls::Unstated
1080        } else if self.name(self.first(placement)) == "NullsFirst" {
1081            Nulls::First
1082        } else {
1083            Nulls::Last
1084        };
1085        (order, nulls)
1086    }
1087
1088    // From clauses.
1089
1090    /// `FromClause <- 'FROM' List(TableRef)`.
1091    fn sources(&mut self, node: u32) -> Result<Slice> {
1092        let mut items = Vec::new();
1093        for kid in self.kids(node) {
1094            items.push(self.table_ref(kid)?);
1095        }
1096        let start = self.ast.source_lists.len() as u32;
1097        self.ast.source_lists.extend(items);
1098        Ok(Slice { start, len: self.ast.source_lists.len() as u32 - start })
1099    }
1100
1101    /// `TableRef <- InnerTableRef JoinOrPivot*`, left associative like the set operators.
1102    fn table_ref(&mut self, node: u32) -> Result<SourceRef> {
1103        let mut kids = self.kids(node);
1104        let head = kids.next().unwrap_or(NONE);
1105        let mut left = self.inner_table_ref(head)?;
1106        for tail in kids {
1107            let clause = self.first(tail);
1108            if self.name(clause) != "JoinClause" {
1109                return self.unsupported(clause);
1110            }
1111            left = self.join(left, self.first(clause))?;
1112        }
1113        Ok(left)
1114    }
1115
1116    /// `InnerTableRef <- ValuesRef / TableFunction / TableSubquery / BaseTableRef / ParensTableRef`.
1117    fn inner_table_ref(&mut self, node: u32) -> Result<SourceRef> {
1118        let inner = if self.name(node) == "InnerTableRef" { self.first(node) } else { node };
1119        match self.name(inner) {
1120            "BaseTableRef" => {
1121                if self.find(inner, "TableAliasColon") != NONE {
1122                    return self.unsupported(inner);
1123                }
1124                for name in ["AtClause", "SampleClause"] {
1125                    let clause = self.find(inner, name);
1126                    if clause != NONE {
1127                        return self.unsupported(clause);
1128                    }
1129                }
1130                let name = self.name_parts(self.find(inner, "BaseTableName"));
1131                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
1132                Ok(self.push_source(Source::Table { name, alias, columns }))
1133            }
1134            "TableSubquery" => {
1135                if self.find(inner, "TableAliasColon") != NONE
1136                    || self.find(inner, "Lateral") != NONE
1137                {
1138                    return self.unsupported(inner);
1139                }
1140                // `SubqueryReference <- Parens(SelectStatementInternal)`.
1141                let reference = self.find(inner, "SubqueryReference");
1142                let query = self.query(self.first(reference))?;
1143                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
1144                Ok(self.push_source(Source::Subquery { query, alias, columns }))
1145            }
1146            // `TableFunction <- TableFunctionLateralOpt / TableFunctionAliasColon`, and
1147            // `TableFunctionLateralOpt <- Lateral? QualifiedTableFunction TableFunctionArguments
1148            // WithOrdinality? TableAlias?`. The colon form and `LATERAL` are their own work, and
1149            // `WITH ORDINALITY` adds a column, so all three are turned away rather than dropped.
1150            "TableFunction" => {
1151                let form = self.first(inner);
1152                for name in ["TableAliasColon", "Lateral", "WithOrdinality", "SampleClause"] {
1153                    let clause = self.find(form, name);
1154                    if clause != NONE {
1155                        return self.unsupported(clause);
1156                    }
1157                }
1158                let name = self.name_parts(self.find(form, "QualifiedTableFunction"));
1159                let mut args = Vec::new();
1160                // `TableFunctionArguments <- Parens(List(FunctionArgument)?)`, so a call with no
1161                // arguments has the wrapper and no list under it.
1162                let list = self.find(form, "TableFunctionArguments");
1163                for kid in self.kids(list) {
1164                    args.push(self.table_argument(kid)?);
1165                }
1166                let args = self.target_slice(args);
1167                let (alias, columns) = self.table_alias(self.find(form, "TableAlias"));
1168                Ok(self.push_source(Source::Function { name, args, alias, columns }))
1169            }
1170            "ValuesRef" => {
1171                if self.find(inner, "TableAliasColon") != NONE {
1172                    return self.unsupported(inner);
1173                }
1174                let rows = self.values_clause(self.find(inner, "ValuesClause"))?;
1175                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
1176                Ok(self.push_source(Source::Values { rows, alias, columns }))
1177            }
1178            "ParensTableRef" => {
1179                if self.find(inner, "TableAliasColon") != NONE
1180                    || self.find(inner, "SampleClause") != NONE
1181                    || self.find(inner, "TableAlias") != NONE
1182                {
1183                    return self.unsupported(inner);
1184                }
1185                self.table_ref(self.find(inner, "TableRef"))
1186            }
1187            _ => self.unsupported(inner),
1188        }
1189    }
1190
1191    /// `TableAlias <- TableAliasAs / TableAliasWithoutAs`, either with a column alias list.
1192    fn table_alias(&mut self, node: u32) -> (StrRef, Slice) {
1193        if node == NONE {
1194            return (NONE, Slice::default());
1195        }
1196        let inner = self.first(node);
1197        let alias = self.identifier(self.first(inner));
1198        let list = self.find(inner, "ColumnAliases");
1199        if list == NONE {
1200            return (alias, Slice::default());
1201        }
1202        let mut columns = Vec::new();
1203        for kid in self.kids(list) {
1204            let name = self.identifier(kid);
1205            columns.push(name);
1206        }
1207        (alias, self.part_slice(columns))
1208    }
1209
1210    /// `JoinClause <- JoinByClause / RegularJoinClause / JoinWithoutOnClause / NearestJoinClause`.
1211    fn join(&mut self, left: SourceRef, node: u32) -> Result<SourceRef> {
1212        match self.name(node) {
1213            // `RegularJoinClause <- Asof? JoinType? 'JOIN' TableRef JoinQualifier`.
1214            "RegularJoinClause" => {
1215                if self.find(node, "Asof") != NONE {
1216                    return self.unsupported(node);
1217                }
1218                let kind = self.join_type(self.find(node, "JoinType"));
1219                let right = self.table_ref(self.find(node, "TableRef"))?;
1220                let (on, using) = self.join_qualifier(self.find(node, "JoinQualifier"))?;
1221                Ok(self.push_source(Source::Join { left, right, kind, natural: false, on, using }))
1222            }
1223            // `JoinWithoutOnClause <- JoinPrefix 'JOIN' InnerTableRef`, which is cross, natural and
1224            // positional. Those three are exactly the joins that carry no condition.
1225            "JoinWithoutOnClause" => {
1226                let prefix = self.first(self.find(node, "JoinPrefix"));
1227                let (kind, natural) = match self.name(prefix) {
1228                    "CrossJoinPrefix" => (JoinKind::Cross, false),
1229                    "PositionalJoinPrefix" => (JoinKind::Positional, false),
1230                    "NaturalJoinPrefix" => (self.join_type(self.find(prefix, "JoinType")), true),
1231                    _ => return self.unsupported(prefix),
1232                };
1233                let right = self.inner_table_ref(self.find(node, "InnerTableRef"))?;
1234                Ok(self.push_source(Source::Join {
1235                    left,
1236                    right,
1237                    kind,
1238                    natural,
1239                    on: NONE,
1240                    using: Slice::default(),
1241                }))
1242            }
1243            _ => self.unsupported(node),
1244        }
1245    }
1246
1247    /// `JoinType <- FullJoin / LeftJoin / RightJoin / SemiJoin / AntiJoin / InnerJoin`, absent
1248    /// meaning inner, which is what SQL has always meant by a bare `JOIN`.
1249    fn join_type(&self, node: u32) -> JoinKind {
1250        if node == NONE {
1251            return JoinKind::Inner;
1252        }
1253        match self.name(self.first(node)) {
1254            "FullJoin" => JoinKind::Full,
1255            "LeftJoin" => JoinKind::Left,
1256            "RightJoin" => JoinKind::Right,
1257            "SemiJoin" => JoinKind::Semi,
1258            "AntiJoin" => JoinKind::Anti,
1259            _ => JoinKind::Inner,
1260        }
1261    }
1262
1263    /// `JoinQualifier <- OnClause / UsingClause`.
1264    fn join_qualifier(&mut self, node: u32) -> Result<(ExprRef, Slice)> {
1265        let inner = self.first(node);
1266        match self.name(inner) {
1267            "OnClause" => Ok((self.expr(self.first(inner))?, Slice::default())),
1268            "UsingClause" => {
1269                let mut columns = Vec::new();
1270                for kid in self.kids(inner) {
1271                    let name = self.identifier(kid);
1272                    columns.push(name);
1273                }
1274                Ok((NONE, self.part_slice(columns)))
1275            }
1276            _ => self.unsupported(inner),
1277        }
1278    }
1279
1280    // Expressions.
1281
1282    /// One expression, from wherever in the precedence chain it starts.
1283    ///
1284    /// The loop is the whole design. A rule that says something gets an arm, a rule with exactly
1285    /// one child said nothing and is stepped through, and anything else is an error naming itself.
1286    /// The chain rules never get an arm for their one child case, which is why adding a precedence
1287    /// level upstream costs nothing here.
1288    fn expr(&mut self, node: u32) -> Result<ExprRef> {
1289        let mut node = node;
1290        loop {
1291            let count = self.count(node);
1292            let name = self.name(node);
1293            match name {
1294                "LogicalOrExpression" if count > 1 => return self.logical(node, BinaryOp::Or),
1295                "LogicalAndExpression" if count > 1 => return self.logical(node, BinaryOp::And),
1296                "LogicalNotExpression" if count > 1 => return self.logical_not(node),
1297                "IsExpression" if count > 1 => return self.is_expression(node),
1298                "BetweenInLikeExpression" if count > 1 => return self.between_in_like(node),
1299                "PrefixExpression" if count > 1 => return self.prefix(node),
1300                "BaseExpression" if count > 1 => return self.indirection(node),
1301                "LambdaArrowExpression"
1302                | "IsDistinctFromExpression"
1303                | "ComparisonExpression"
1304                | "OtherOperatorExpression"
1305                | "BitwiseExpression"
1306                | "AdditiveExpression"
1307                | "MultiplicativeExpression"
1308                | "ExponentiationExpression"
1309                | "CollateExpression"
1310                | "AtTimeZoneExpression"
1311                    if count > 1 =>
1312                {
1313                    return self.tail_chain(node);
1314                }
1315                "ColumnReference" => {
1316                    let name = self.name_parts(node);
1317                    return Ok(self.push(Expr::Column { name }));
1318                }
1319                "StarExpression" => return self.star(node),
1320                "NumberLiteral" => {
1321                    let text = self.text(node).to_string();
1322                    let text = self.intern(&text);
1323                    return Ok(self.push(Expr::Literal { kind: LiteralKind::Number, text }));
1324                }
1325                "StringLiteral" => {
1326                    let text = self.string_value(node);
1327                    let text = self.intern(&text);
1328                    return Ok(self.push(Expr::Literal { kind: LiteralKind::String, text }));
1329                }
1330                "NullLiteral" | "TrueLiteral" | "FalseLiteral" => {
1331                    let kind = match name {
1332                        "NullLiteral" => LiteralKind::Null,
1333                        "TrueLiteral" => LiteralKind::True,
1334                        _ => LiteralKind::False,
1335                    };
1336                    return Ok(self.push(Expr::Literal { kind, text: NONE }));
1337                }
1338                "FunctionExpression" => return self.function(node),
1339                "CoalesceExpression" => return self.coalesce(node),
1340                "NullIfExpression" => return self.null_if(node),
1341                "ExtractExpression" => return self.extract(node),
1342                "CastExpression" => return self.cast(node),
1343                "CaseExpression" => return self.case(node),
1344                "ParenthesisExpression" => return self.row(node),
1345                "BoundedListExpression" => return self.list(node),
1346                "QuestionMarkNumberedParameter"
1347                | "AnonymousParameter"
1348                | "NumberedParameter"
1349                | "ColLabelParameter" => return self.parameter(node),
1350                "SubqueryExpression" => return self.subquery(node),
1351                _ if count == 1 => node = self.first(node),
1352                _ => return self.unsupported(node),
1353            }
1354        }
1355    }
1356
1357    /// `X <- Y XTail*` where `XTail <- Operator Y`, the shape ten precedence levels share.
1358    fn tail_chain(&mut self, node: u32) -> Result<ExprRef> {
1359        let mut kids = self.kids(node);
1360        let head = kids.next().unwrap_or(NONE);
1361        let mut left = self.expr(head)?;
1362        for tail in kids {
1363            let operator = self.first(tail);
1364            let op = self.binary_op(operator)?;
1365            // `ComparisonExpressionTail <- ComparisonOperator NotExpression? BetweenInLikeExpression`
1366            // is the one tail with an optional middle, so the operand is the last child and not the
1367            // second one. Taking the last is right for every tail and wrong for none.
1368            let operand = self.kids(tail).last().unwrap_or(NONE);
1369            if self.count(tail) > 2 {
1370                return self.unsupported(tail);
1371            }
1372            let right = self.expr(operand)?;
1373            left = self.push(Expr::Binary { op, left, right });
1374        }
1375        Ok(left)
1376    }
1377
1378    /// Which infix operator a tail's operator node is.
1379    fn binary_op(&mut self, node: u32) -> Result<BinaryOp> {
1380        // The operator rules nest: `ComparisonOperator` over `OperatorGreaterThan` over the symbol
1381        // itself. Every one of them covers the same tokens, so the text is the same at every level
1382        // and reading it once at the top is enough. The name is not, which is why the bottom of the
1383        // chain is walked to as well: `OtherOperator` says nothing and `OperatorLiteral` says
1384        // everything, and they are three levels apart.
1385        let mut leaf = node;
1386        while self.count(leaf) == 1 {
1387            leaf = self.first(leaf);
1388        }
1389        let text = self.text(node);
1390        let upper = text.to_ascii_uppercase();
1391        let op = match upper.as_str() {
1392            "OR" => BinaryOp::Or,
1393            "AND" => BinaryOp::And,
1394            "=" | "==" => BinaryOp::Eq,
1395            "!=" | "<>" => BinaryOp::NotEq,
1396            "<" => BinaryOp::Lt,
1397            ">" => BinaryOp::Gt,
1398            "<=" => BinaryOp::LtEq,
1399            ">=" => BinaryOp::GtEq,
1400            "+" => BinaryOp::Add,
1401            "-" => BinaryOp::Subtract,
1402            "*" => BinaryOp::Multiply,
1403            "/" => BinaryOp::Divide,
1404            "//" => BinaryOp::IntegerDivide,
1405            "%" => BinaryOp::Modulo,
1406            "^" | "**" => BinaryOp::Power,
1407            "&" => BinaryOp::BitAnd,
1408            "|" => BinaryOp::BitOr,
1409            "<<" => BinaryOp::ShiftLeft,
1410            ">>" => BinaryOp::ShiftRight,
1411            "||" => BinaryOp::Concat,
1412            "COLLATE" => BinaryOp::Collate,
1413            "->" => BinaryOp::Arrow,
1414            "->>" => BinaryOp::LongArrow,
1415            "@>" => BinaryOp::Contains,
1416            "<@" => BinaryOp::ContainedBy,
1417            "&&" => BinaryOp::Overlaps,
1418            "^@" => BinaryOp::StartsWith,
1419            "<<=" => BinaryOp::InetContainedByOrEq,
1420            ">>=" => BinaryOp::InetContainsOrEq,
1421            _ if self.name(leaf) == "AtTimeZoneOperator" => BinaryOp::AtTimeZone,
1422            // `IsDistinctFromOp <- 'IS' 'NOT'? 'DISTINCT' 'FROM'`, told apart by the middle word,
1423            // which is not in the tree because keywords are terminals.
1424            _ if self.name(leaf) == "IsDistinctFromOp" => {
1425                if upper.split_whitespace().any(|word| word == "NOT") {
1426                    BinaryOp::IsNotDistinctFrom
1427                } else {
1428                    BinaryOp::IsDistinctFrom
1429                }
1430            }
1431            // `OperatorLiteral` is the open end of the operator set. Its body in the grammar text
1432            // says `Identifier`, but it is one of the 24 rules whose body the matcher does not
1433            // walk and the matcher it is overridden to is the bare operator one, so what it
1434            // actually accepts is any run of operator characters that is not already a token.
1435            // `a <=> b` is such a run, DuckDB resolves it as a two argument function of that name,
1436            // and rejecting it here would reject SQL DuckDB accepts.
1437            _ if self.name(leaf) == "OperatorLiteral" => {
1438                let interned = self.intern(text);
1439                BinaryOp::Named(interned)
1440            }
1441            _ => return self.unsupported(node),
1442        };
1443        Ok(op)
1444    }
1445
1446    /// `LogicalOrExpression <- LogicalAndExpression LogicalOrExpressionTail*`, and the `AND` twin.
1447    ///
1448    /// Separate from the other tails because the tail here is `'OR' LogicalAndExpression` with the
1449    /// keyword as a terminal, so there is no operator node to read and the operator is the rule.
1450    fn logical(&mut self, node: u32, op: BinaryOp) -> Result<ExprRef> {
1451        let mut kids = self.kids(node);
1452        let head = kids.next().unwrap_or(NONE);
1453        let mut left = self.expr(head)?;
1454        for tail in kids {
1455            let right = self.expr(self.first(tail))?;
1456            left = self.push(Expr::Binary { op, left, right });
1457        }
1458        Ok(left)
1459    }
1460
1461    /// `LogicalNotExpression <- NotExpression? IsExpression`, where `NotExpression <- NotKeyword+`.
1462    ///
1463    /// The plus matters. `NOT NOT x` is two nodes in the parse tree and two negations in the AST,
1464    /// and folding them here would be an optimizer decision taken in the parser.
1465    fn logical_not(&mut self, node: u32) -> Result<ExprRef> {
1466        let negations = self.count(self.first(node));
1467        let mut expr = self.expr(self.nth(node, 1))?;
1468        for _ in 0..negations {
1469            expr = self.push(Expr::Unary { op: UnaryOp::Not, operand: expr });
1470        }
1471        Ok(expr)
1472    }
1473
1474    /// `IsExpression <- IsDistinctFromExpression IsTest*`, the postfix null and boolean tests.
1475    fn is_expression(&mut self, node: u32) -> Result<ExprRef> {
1476        let mut kids = self.kids(node);
1477        let head = kids.next().unwrap_or(NONE);
1478        let mut expr = self.expr(head)?;
1479        for test in kids {
1480            let inner = self.first(test);
1481            let negated = self.text(inner).to_ascii_uppercase().contains("NOT");
1482            let op = match self.name(inner) {
1483                "NotNull" => UnaryOp::IsNotNull,
1484                "IsNull" => UnaryOp::IsNull,
1485                // `IsLiteral <- 'IS' 'NOT'? IsLiteralValue`, and the value rule is one more level
1486                // down again because it is a choice of four and not four alternatives inlined.
1487                "IsLiteral" => match self.name(self.first(self.first(inner))) {
1488                    "NullLiteral" if negated => UnaryOp::IsNotNull,
1489                    "NullLiteral" => UnaryOp::IsNull,
1490                    "TrueLiteral" if negated => UnaryOp::IsNotTrue,
1491                    "TrueLiteral" => UnaryOp::IsTrue,
1492                    "FalseLiteral" if negated => UnaryOp::IsNotFalse,
1493                    "FalseLiteral" => UnaryOp::IsFalse,
1494                    "UnknownLiteral" if negated => UnaryOp::IsNotUnknown,
1495                    "UnknownLiteral" => UnaryOp::IsUnknown,
1496                    _ => return self.unsupported(inner),
1497                },
1498                _ => return self.unsupported(inner),
1499            };
1500            expr = self.push(Expr::Unary { op, operand: expr });
1501        }
1502        Ok(expr)
1503    }
1504
1505    /// `BetweenInLikeExpression <- OtherOperatorExpression BetweenInLikeOp?`.
1506    fn between_in_like(&mut self, node: u32) -> Result<ExprRef> {
1507        let operand = self.expr(self.first(node))?;
1508        // `BetweenInLikeOp <- 'NOT'? BetweenInLikeOpExpression`. The `NOT` is a terminal, so what
1509        // says it was written is that the op node covers a token the inner node does not.
1510        let op = self.nth(node, 1);
1511        let negated = self.text(op).to_ascii_uppercase().starts_with("NOT");
1512        let inner = self.first(self.first(op));
1513        match self.name(inner) {
1514            // `BetweenClause <- 'BETWEEN' x 'AND' y`.
1515            "BetweenClause" => {
1516                let low = self.expr(self.first(inner))?;
1517                let high = self.expr(self.nth(inner, 1))?;
1518                Ok(self.push(Expr::Between { operand, low, high, negated }))
1519            }
1520            // `InClause <- 'IN' InExpression`.
1521            "InClause" => {
1522                let expression = self.first(self.first(inner));
1523                match self.name(expression) {
1524                    "InExpressionList" => {
1525                        let mut items = Vec::new();
1526                        for kid in self.kids(expression) {
1527                            items.push(self.expr(kid)?);
1528                        }
1529                        let list = self.expr_slice(items);
1530                        Ok(self.push(Expr::In { operand, list, negated }))
1531                    }
1532                    _ => self.unsupported(expression),
1533                }
1534            }
1535            // `LikeClause <- LikeVariations x EscapeClause?`.
1536            "LikeClause" => {
1537                if self.find(inner, "EscapeClause") != NONE {
1538                    return self.unsupported(inner);
1539                }
1540                let variation = self.name(self.first(self.first(inner)));
1541                let op = match (variation, negated) {
1542                    ("LikeToken", false) | ("NotLikeOp", true) => BinaryOp::Like,
1543                    ("LikeToken", true) | ("NotLikeOp", false) => BinaryOp::NotLike,
1544                    ("ILikeToken", false) | ("NotILikeOp", true) => BinaryOp::ILike,
1545                    ("ILikeToken", true) | ("NotILikeOp", false) => BinaryOp::NotILike,
1546                    // Glob and the bare regex match have no negated spelling of their own in
1547                    // `LikeVariations`, so a `NOT` in front of either stays an explicit negation.
1548                    ("GlobToken", _) => BinaryOp::Glob,
1549                    ("RegexMatchToken", _) => BinaryOp::Regex,
1550                    ("SimilarToToken", false) | ("NotSimilarToOp", true) => BinaryOp::SimilarTo,
1551                    ("SimilarToToken", true) | ("NotSimilarToOp", false) => BinaryOp::NotSimilarTo,
1552                    ("RegexInsensitiveMatchToken", false)
1553                    | ("NotRegexInsensitiveMatchOp", true) => BinaryOp::RegexInsensitive,
1554                    ("RegexInsensitiveMatchToken", true)
1555                    | ("NotRegexInsensitiveMatchOp", false) => BinaryOp::NotRegexInsensitive,
1556                    _ => return self.unsupported(inner),
1557                };
1558                let right = self.expr(self.nth(inner, 1))?;
1559                let expr = self.push(Expr::Binary { op, left: operand, right });
1560                // The like family folds its negation into the operator because it has a spelling
1561                // for the negated form. Glob and regex do not, so theirs stays where it was.
1562                if negated && matches!(op, BinaryOp::Glob | BinaryOp::Regex) {
1563                    return Ok(self.push(Expr::Unary { op: UnaryOp::Not, operand: expr }));
1564                }
1565                Ok(expr)
1566            }
1567            _ => self.unsupported(inner),
1568        }
1569    }
1570
1571    /// `PrefixExpression <- PrefixOperator* BaseExpression`, applied right to left.
1572    fn prefix(&mut self, node: u32) -> Result<ExprRef> {
1573        let kids: Vec<u32> = self.kids(node).collect();
1574        let mut expr = self.expr(kids[kids.len() - 1])?;
1575        for &operator in kids[..kids.len() - 1].iter().rev() {
1576            let op = match self.name(self.first(operator)) {
1577                "MinusPrefixOperator" => UnaryOp::Negate,
1578                "PlusPrefixOperator" => UnaryOp::Plus,
1579                "TildePrefixOperator" => UnaryOp::BitNot,
1580                _ => return self.unsupported(operator),
1581            };
1582            expr = self.push(Expr::Unary { op, operand: expr });
1583        }
1584        Ok(expr)
1585    }
1586
1587    /// `BaseExpression <- SingleExpression IndirectionList?`, the postfix chain.
1588    fn indirection(&mut self, node: u32) -> Result<ExprRef> {
1589        let mut expr = self.expr(self.first(node))?;
1590        for step in self.kids(self.nth(node, 1)) {
1591            let inner = self.first(step);
1592            expr = match self.name(inner) {
1593                // `CastOperator <- '::' Type`.
1594                "CastOperator" => {
1595                    let text = self.text(self.first(inner)).to_string();
1596                    let ty = self.intern(&text);
1597                    self.push(Expr::Cast { operand: expr, ty, try_cast: false })
1598                }
1599                "DotOperator" => {
1600                    let dot = self.first(inner);
1601                    match self.name(dot) {
1602                        // `DotColumnOperator <- '.' ColLabel`, which DuckDB resolves as a call of
1603                        // `struct_extract`. Writing it as that call rather than as its own node
1604                        // keeps the binder from needing a rule for a thing that is already a
1605                        // function.
1606                        "DotColumnOperator" => {
1607                            let field = self.identifier(self.first(dot));
1608                            let text = self.ast.string(field).to_string();
1609                            let literal = self.intern(&text);
1610                            let key = self
1611                                .push(Expr::Literal { kind: LiteralKind::String, text: literal });
1612                            let name = self.function_name("struct_extract");
1613                            let args = self.expr_slice(vec![expr, key]);
1614                            self.push(Expr::Function { name, args, distinct: false })
1615                        }
1616                        // `DotMethodOperator <- '.' MethodExpression`, where `x.f(a)` is `f(x, a)`.
1617                        "DotMethodOperator" => {
1618                            let method = self.first(dot);
1619                            let text = self.text(self.first(method)).to_string();
1620                            let text = unquote(&text);
1621                            let name = self.function_name(&text);
1622                            let mut args = vec![expr];
1623                            let list = self.find(method, "MethodExpressionArguments");
1624                            if list != NONE {
1625                                let inner = self.first(list);
1626                                let arguments = self.find(inner, "MethodFunctionArguments");
1627                                if arguments != NONE {
1628                                    for kid in self.kids(arguments) {
1629                                        args.push(self.argument(kid)?);
1630                                    }
1631                                }
1632                            }
1633                            let args = self.expr_slice(args);
1634                            self.push(Expr::Function { name, args, distinct: false })
1635                        }
1636                        _ => return self.unsupported(dot),
1637                    }
1638                }
1639                // `SliceExpression <- '[' SliceBound ']'` over
1640                // `SliceBound <- Expression? EndSliceBound? StepSliceBound?`, so a subscript is one
1641                // index when neither colon is there and a range when either of them is. Both become
1642                // a call, the same two calls DuckDB's own transformer writes.
1643                "SliceExpression" => self.subscript(inner, expr)?,
1644                // `PostfixOperator <- '!'`.
1645                "PostfixOperator" => {
1646                    self.push(Expr::Unary { op: UnaryOp::Factorial, operand: expr })
1647                }
1648                _ => return self.unsupported(inner),
1649            };
1650        }
1651        Ok(expr)
1652    }
1653
1654    /// `SliceExpression <- '[' SliceBound ']'`, which is `array_extract` or `array_slice`.
1655    ///
1656    /// The three parts of the bound are all optional and any of the eight combinations parses, so
1657    /// which call this is comes from which parts are there rather than from how many children the
1658    /// bound has. One expression and no colon is an index. Anything with a colon in it is a range,
1659    /// and a range the query did not write both ends of gets the ends DuckDB's transformer gives it:
1660    /// a missing begin is 1 and a missing end is -1, which is the last element, so `x[:]` is the
1661    /// whole of `x` and `array_slice(x, 1, -1)` answers the same thing.
1662    ///
1663    /// `EndSliceMinus` is the `-` in `x[1:-]`, which upstream reads as a range with no end rather
1664    /// than as a subtraction of nothing, and it answers `x[1:]`. So it is the missing end too.
1665    ///
1666    /// The step is the odd one. `x[1:2:]` is a step that is written and empty, and what upstream
1667    /// does with it is pass a list where the step goes, which then fails to bind because the fourth
1668    /// parameter is a BIGINT. The empty list here is that, measured off the pinned binary: it says
1669    /// `array_slice(INTEGER[], INTEGER_LITERAL, INTEGER_LITERAL, INTEGER[])` has no match, and the
1670    /// fourth type in that sentence is the list. Writing a 1 there instead would answer a row where
1671    /// the reference refuses.
1672    fn subscript(&mut self, node: u32, target: ExprRef) -> Result<ExprRef> {
1673        let bound = self.first(node);
1674        let (mut begin, mut end, mut step) = (NONE, NONE, NONE);
1675        for kid in self.kids(bound) {
1676            match self.name(kid) {
1677                "EndSliceBound" => end = kid,
1678                "StepSliceBound" => step = kid,
1679                _ => begin = kid,
1680            }
1681        }
1682        if end == NONE && step == NONE {
1683            if begin == NONE {
1684                return Err(Error::parser("Empty subscript '[]' is not allowed"));
1685            }
1686            let index = self.expr(begin)?;
1687            let name = self.function_name("array_extract");
1688            let args = self.expr_slice(vec![target, index]);
1689            return Ok(self.push(Expr::Function { name, args, distinct: false }));
1690        }
1691        let first = if begin == NONE { self.literal_number("1") } else { self.expr(begin)? };
1692        // `EndSliceBound <- ':' EndSliceValue?` and `EndSliceValue <- Expression / EndSliceMinus`,
1693        // so the end is written only when the value is there and is not the lone hyphen.
1694        let value = if end == NONE { NONE } else { self.find(end, "EndSliceValue") };
1695        let written = if value == NONE { NONE } else { self.first(value) };
1696        let last = if written == NONE || self.name(written) == "EndSliceMinus" {
1697            self.literal_number("-1")
1698        } else {
1699            self.expr(written)?
1700        };
1701        let mut args = vec![target, first, last];
1702        if step != NONE {
1703            let by = self.first(step);
1704            args.push(if by == NONE {
1705                self.push(Expr::List { items: Slice::default() })
1706            } else {
1707                self.expr(by)?
1708            });
1709        }
1710        let name = self.function_name("array_slice");
1711        let args = self.expr_slice(args);
1712        Ok(self.push(Expr::Function { name, args, distinct: false }))
1713    }
1714
1715    /// A number literal the transformer writes rather than reads, for a bound a range left out.
1716    fn literal_number(&mut self, digits: &str) -> ExprRef {
1717        let text = self.intern(digits);
1718        self.push(Expr::Literal { kind: LiteralKind::Number, text })
1719    }
1720
1721    /// A one part function name, for the calls the transformer invents rather than reads.
1722    fn function_name(&mut self, name: &str) -> Slice {
1723        let interned = self.intern(name);
1724        self.part_slice(vec![interned])
1725    }
1726
1727    /// `StarExpression <- StarQualifierList? '*' ExcludeList? ReplaceList? RenameList?`.
1728    fn star(&mut self, node: u32) -> Result<ExprRef> {
1729        for name in ["ExcludeList", "RenameList"] {
1730            let list = self.find(node, name);
1731            if list != NONE {
1732                return self.unsupported(list);
1733            }
1734        }
1735        let replace = self.find(node, "ReplaceList");
1736        let replacements =
1737            if replace == NONE { Slice::default() } else { self.replacements(replace)? };
1738        let qualifier = self.find(node, "StarQualifierList");
1739        let qualifier =
1740            if qualifier == NONE { Slice::default() } else { self.name_parts(qualifier) };
1741        Ok(self.push(Expr::Star { qualifier, replacements }))
1742    }
1743
1744    /// `ReplaceList <- 'REPLACE' ReplaceEntries`, where an entry is `Expression 'AS'
1745    /// ColumnReference` and the entries are one bare entry or a parenthesized list of them.
1746    ///
1747    /// The duplicate check is here rather than in the binder because that is where DuckDB does it:
1748    /// naming the same column twice is a Parser Error there, and it is one of the few things about
1749    /// a star that can be decided without knowing what the star stands for.
1750    fn replacements(&mut self, node: u32) -> Result<Slice> {
1751        // `ReplaceEntries <- ReplaceEntrySingle / ReplaceEntryList` and both of those hold the
1752        // entries as their own children, so the same walk reads either shape.
1753        let entries = self.first(self.first(node));
1754        let listed: Vec<u32> =
1755            self.kids(entries).filter(|&kid| self.name(kid) == "ReplaceEntry").collect();
1756        let mut replacements = Vec::with_capacity(listed.len());
1757        for entry in listed {
1758            let expr = self.expr(self.first(entry))?;
1759            let alias = self.identifier(self.nth(entry, 1));
1760            let written = self.ast.string(alias).to_string();
1761            if replacements
1762                .iter()
1763                .any(|held: &Target| self.ast.string(held.alias).eq_ignore_ascii_case(&written))
1764            {
1765                return Err(Error::parser(format!(
1766                    "Duplicate entry \"{written}\" in REPLACE list"
1767                )));
1768            }
1769            replacements.push(Target { expr, alias });
1770        }
1771        Ok(self.target_slice(replacements))
1772    }
1773
1774    /// `FunctionExpression <- FunctionIdentifier FunctionExpressionArguments WithinGroupClause?
1775    /// FilterClause? ExportClause? OverClause?`.
1776    fn function(&mut self, node: u32) -> Result<ExprRef> {
1777        for name in ["WithinGroupClause", "FilterClause", "ExportClause", "OverClause"] {
1778            let clause = self.find(node, name);
1779            if clause != NONE {
1780                return self.unsupported(clause);
1781            }
1782        }
1783        let name = self.name_parts(self.first(node));
1784        // `FunctionExpressionArguments <- Parens(FunctionExpressionArgumentList)` and
1785        // `FunctionExpressionArgumentList <- DistinctOrAll? FunctionArgumentList? OrderByClause?
1786        // IgnoreOrRespectNulls?`, so a call with no arguments still has both wrappers.
1787        let list = self.first(self.nth(node, 1));
1788        for name in ["OrderByClause", "IgnoreOrRespectNulls"] {
1789            let clause = self.find(list, name);
1790            if clause != NONE {
1791                return self.unsupported(clause);
1792            }
1793        }
1794        let distinct = self.quantifier(self.find(list, "DistinctOrAll")) == Quantifier::Distinct;
1795        let mut args = Vec::new();
1796        let arguments = self.find(list, "FunctionArgumentList");
1797        if arguments != NONE {
1798            for kid in self.kids(arguments) {
1799                args.push(self.argument(kid)?);
1800            }
1801        }
1802        // `IFNULL` is an ordinary call in the grammar and is not one by the time DuckDB's parser is
1803        // done with it: `ifnull(NULL, 3)` comes back named `COALESCE(NULL, 3)` there, and so does
1804        // `main.ifnull(NULL, 3)`, so the qualifier goes with the rewrite. The count is checked here
1805        // because that is where upstream checks it, with the sentence below rather than the binder's
1806        // arity error, and it is checked before the two arguments are looked at.
1807        if self.ast.name(name).last().is_some_and(|part| part.eq_ignore_ascii_case("ifnull")) {
1808            if args.len() != 2 {
1809                return Err(Error::parser("Wrong number of arguments to IFNULL."));
1810            }
1811            let args = self.expr_slice(args);
1812            let name = self.function_name("coalesce");
1813            return Ok(self.push(Expr::Function { name, args, distinct }));
1814        }
1815        let args = self.expr_slice(args);
1816        Ok(self.push(Expr::Function { name, args, distinct }))
1817    }
1818
1819    /// `CoalesceExpression <- 'COALESCE' Parens(List(Expression))`.
1820    ///
1821    /// A keyword is not a child and the two wrappers are transparent, so the children are the
1822    /// arguments. One of them is enough for the grammar and none of them is a syntax error, which is
1823    /// why there is no count checked here.
1824    ///
1825    /// The call is written with the canonical name rather than the one the query used, since there is
1826    /// nothing else to keep: the keyword is the name. Upstream prints the column in capitals whatever
1827    /// case was written, because `COALESCE` is an operator there and not a function name that its
1828    /// parser folded, and the binder is where that is decided here.
1829    fn coalesce(&mut self, node: u32) -> Result<ExprRef> {
1830        let mut args = Vec::new();
1831        for kid in self.kids(node) {
1832            args.push(self.expr(kid)?);
1833        }
1834        let args = self.expr_slice(args);
1835        let name = self.function_name("coalesce");
1836        Ok(self.push(Expr::Function { name, args, distinct: false }))
1837    }
1838
1839    /// `NullIfExpression <- 'NULLIF' Parens(NullIfArguments)` and
1840    /// `NullIfArguments <- Expression ',' Expression`.
1841    ///
1842    /// Exactly two arguments, because the rule says so: `nullif(1)` and `nullif(1, 2, 3)` are syntax
1843    /// errors upstream and are syntax errors here for the same reason, so there is no arity to check
1844    /// after the parse.
1845    ///
1846    /// It stays a function called `nullif` rather than becoming the `CASE` upstream's macro expands
1847    /// to, since the column it produces is named after the call and not after the expansion.
1848    fn null_if(&mut self, node: u32) -> Result<ExprRef> {
1849        let arguments = self.find(node, "NullIfArguments");
1850        if arguments == NONE {
1851            return self.unsupported(node);
1852        }
1853        let mut args = Vec::new();
1854        for kid in self.kids(arguments) {
1855            args.push(self.expr(kid)?);
1856        }
1857        let args = self.expr_slice(args);
1858        let name = self.function_name("nullif");
1859        Ok(self.push(Expr::Function { name, args, distinct: false }))
1860    }
1861
1862    /// `ExtractExpression <- 'EXTRACT' Parens(ExtractArguments)` and
1863    /// `ExtractArguments <- ExtractArgument 'FROM' Expression`.
1864    ///
1865    /// `EXTRACT` is not a function in the grammar because its argument list is not an argument
1866    /// list, and it is a function everywhere after here because DuckDB's parser does the same
1867    /// rewrite: `EXTRACT(minute FROM t)` is `date_part('minute', t)` and there is no separate
1868    /// implementation of one of them. The part is a keyword, an identifier or a string in the
1869    /// grammar, and all three become the string, which is why this is a rewrite and not a node.
1870    fn extract(&mut self, node: u32) -> Result<ExprRef> {
1871        let arguments = self.find(node, "ExtractArguments");
1872        if arguments == NONE {
1873            return self.unsupported(node);
1874        }
1875        let argument = self.first(self.first(arguments));
1876        let part = match self.name(argument) {
1877            "ExtractStringArgument" => self.string_value(argument),
1878            // A keyword or an identifier, both taken as written. Which specifier names are legal is
1879            // not a question about syntax, so the answer to it lives with the function.
1880            "ExtractDatePartArgument" | "ExtractIdentifierArgument" => {
1881                self.text(argument).to_string()
1882            }
1883            _ => return self.unsupported(argument),
1884        };
1885        let text = self.intern(&part);
1886        let part = self.push(Expr::Literal { kind: LiteralKind::String, text });
1887        let operand = self.expr(self.nth(arguments, 1))?;
1888        let name = self.function_name("date_part");
1889        let args = self.expr_slice(vec![part, operand]);
1890        Ok(self.push(Expr::Function { name, args, distinct: false }))
1891    }
1892
1893    /// `FunctionArgument <- NamedFunctionArgument / PositionalFunctionArgument`.
1894    fn argument(&mut self, node: u32) -> Result<ExprRef> {
1895        let inner = self.first(node);
1896        match self.name(inner) {
1897            "PositionalFunctionArgument" => self.expr(self.first(inner)),
1898            _ => self.unsupported(inner),
1899        }
1900    }
1901
1902    /// One argument of a table function, which is the same rule plus the names.
1903    ///
1904    /// `NamedParameter <- TypeFuncName Type? NamedParameterAssignment Expression` and
1905    /// `NamedParameterAssignment <- ':=' / '=>'`, so those two spellings are what the grammar has.
1906    /// The binary accepts a third, `name = value`, which the grammar has no rule for because it
1907    /// parses as an equality and is picked apart afterwards. That is what happens here too: a
1908    /// positional argument that is a comparison between a bare name and something else is a named
1909    /// parameter, which is the reading upstream's own transformer gives it. `read_parquet(f,
1910    /// binary_as_string=True)` is the query that matters and it is the spelling the ClickBench
1911    /// entry uses.
1912    ///
1913    /// The name is not resolved here and neither is the value. Which parameters a function takes
1914    /// is the binder's question, and so is whether `binary_as_string=True` means anything to the
1915    /// function it was written on.
1916    fn table_argument(&mut self, node: u32) -> Result<Target> {
1917        let inner = self.first(node);
1918        if self.name(inner) == "NamedFunctionArgument" {
1919            let named = self.first(inner);
1920            if self.count(named) != 3 {
1921                // The optional `Type` between the name and the assignment, which is a macro
1922                // parameter's declaration and not a call.
1923                return self.unsupported(named);
1924            }
1925            let alias = self.identifier(self.first(named));
1926            let expr = self.expr(self.nth(named, 2))?;
1927            return Ok(Target { expr, alias });
1928        }
1929        let expr = self.expr(self.first(inner))?;
1930        if let Expr::Binary { op: BinaryOp::Eq, left, right } = self.ast.expr(expr) {
1931            if let Expr::Column { name } = self.ast.expr(left) {
1932                if name.len == 1 {
1933                    let alias = self.ast.parts[name.start as usize];
1934                    return Ok(Target { expr: right, alias });
1935                }
1936            }
1937        }
1938        Ok(Target { expr, alias: NONE })
1939    }
1940
1941    /// `CastExpression <- CastOrTryCast Parens(CastArguments)`.
1942    fn cast(&mut self, node: u32) -> Result<ExprRef> {
1943        let try_cast = self.name(self.first(self.first(node))) == "TryCastKeyword";
1944        // `CastArguments <- Expression 'AS' Type`.
1945        let arguments = self.nth(node, 1);
1946        let operand = self.expr(self.first(arguments))?;
1947        let text = self.text(self.nth(arguments, 1)).to_string();
1948        let ty = self.intern(&text);
1949        Ok(self.push(Expr::Cast { operand, ty, try_cast }))
1950    }
1951
1952    /// `CaseExpression <- 'CASE' Expression? CaseWhenThen+ CaseElse? 'END'`.
1953    fn case(&mut self, node: u32) -> Result<ExprRef> {
1954        let mut operand = NONE;
1955        let mut arms = Vec::new();
1956        let mut otherwise = NONE;
1957        for kid in self.kids(node) {
1958            match self.name(kid) {
1959                // `CaseWhenThen <- 'WHEN' Expression 'THEN' Expression`.
1960                "CaseWhenThen" => {
1961                    let when = self.expr(self.first(kid))?;
1962                    let then = self.expr(self.nth(kid, 1))?;
1963                    arms.push(CaseArm { when, then });
1964                }
1965                // `CaseElse <- 'ELSE' Expression`.
1966                "CaseElse" => otherwise = self.expr(self.first(kid))?,
1967                // The bare `Expression` before the first `WHEN`, which makes it a simple case.
1968                _ => operand = self.expr(kid)?,
1969            }
1970        }
1971        let start = self.ast.case_arms.len() as u32;
1972        self.ast.case_arms.extend(arms);
1973        let arms = Slice { start, len: self.ast.case_arms.len() as u32 - start };
1974        Ok(self.push(Expr::Case { operand, arms, otherwise }))
1975    }
1976
1977    /// `ParenthesisExpression <- Parens(List(Expression)?)`, which is a row value.
1978    ///
1979    /// One item is not a row. `(a)` is `a` in every dialect and reading it as a one column row
1980    /// would change what `(a) = (b)` means.
1981    fn row(&mut self, node: u32) -> Result<ExprRef> {
1982        let mut items = Vec::new();
1983        for kid in self.kids(node) {
1984            items.push(self.expr(kid)?);
1985        }
1986        if items.len() == 1 {
1987            return Ok(items[0]);
1988        }
1989        let items = self.expr_slice(items);
1990        Ok(self.push(Expr::Row { items }))
1991    }
1992
1993    /// `Parameter <- '?' Number / '?' / '$' Number / '$' ColLabel`, a prepared statement parameter.
1994    ///
1995    /// The identifier is what follows the marker, so `?1` and `$1` are both the parameter named 1,
1996    /// and a bare `?` takes the next number by where it was written. That is what DuckDB does, which
1997    /// is why `? + $2` prints as `$1 + $2`: the counting is its own and does not skip a number
1998    /// because a later parameter claimed it.
1999    fn parameter(&mut self, node: u32) -> Result<ExprRef> {
2000        let written = self.text(node).trim();
2001        let written = written.trim_start_matches(['?', '$']).trim();
2002        let name = if written.is_empty() {
2003            self.anonymous += 1;
2004            self.anonymous.to_string()
2005        } else {
2006            written.to_string()
2007        };
2008        let name = self.intern(&name);
2009        Ok(self.push(Expr::Parameter { name }))
2010    }
2011
2012    /// `BoundedListExpression <- '[' List(Expression)? ']'`, which is a LIST value.
2013    ///
2014    /// One item is a list of one here, unlike the parenthesised form, because the brackets are what
2015    /// say list and there is nothing else `[a]` could mean.
2016    fn list(&mut self, node: u32) -> Result<ExprRef> {
2017        let mut items = Vec::new();
2018        for kid in self.kids(node) {
2019            items.push(self.expr(kid)?);
2020        }
2021        let items = self.expr_slice(items);
2022        Ok(self.push(Expr::List { items }))
2023    }
2024
2025    /// `SubqueryExpression <- SubqueryNot? SubqueryExists? SubqueryReference`.
2026    fn subquery(&mut self, node: u32) -> Result<ExprRef> {
2027        if self.find(node, "SubqueryNot") != NONE || self.find(node, "SubqueryExists") != NONE {
2028            return self.unsupported(node);
2029        }
2030        let reference = self.find(node, "SubqueryReference");
2031        let query = self.query(self.first(reference))?;
2032        Ok(self.push(Expr::Subquery { query }))
2033    }
2034
2035    /// The value of a string literal, with the quotes gone and the escapes resolved.
2036    ///
2037    /// A literal can be several tokens. `'a' 'b'` on two lines is one literal that is `ab`, which is
2038    /// the SQL standard's rule and DuckDB's, so the node is decoded token by token rather than by
2039    /// taking its text and stripping the outside.
2040    fn string_value(&self, node: u32) -> String {
2041        let span = self.tree.node(node);
2042        let mut value = String::new();
2043        for token in &self.tokens[span.start as usize..span.end as usize] {
2044            if token.kind != Kind::String {
2045                continue;
2046            }
2047            let text = token.text(self.query);
2048            if let Some(body) = dollar_body(text) {
2049                value.push_str(body);
2050                continue;
2051            }
2052            match text.strip_prefix('\'').and_then(|rest| rest.strip_suffix('\'')) {
2053                Some(body) => value.push_str(&body.replace("''", "'")),
2054                None => value.push_str(text),
2055            }
2056        }
2057        value
2058    }
2059}
2060
2061/// The body of a dollar quoted string, for the tokens that are one.
2062///
2063/// The tag is whatever sits between the opening pair of dollars and may be empty, so `$$a$$` and
2064/// `$tag$a$tag$` both arrive here, and nothing inside the body is escaped, which is the whole reason
2065/// the spelling exists. The tokenizer has already found the closing tag, which is the part that takes
2066/// work, so this says where the body starts and ends and no more. A token that is not dollar quoted
2067/// gives `None` and so does an unterminated one, which has no closing tag to take off and keeps every
2068/// byte it was given, the way the matcher already treats it. Per #276.
2069fn dollar_body(text: &str) -> Option<&str> {
2070    let rest = text.strip_prefix('$')?;
2071    let close = rest.find('$')?;
2072    let (tag, body) = (&rest[..close], &rest[close + 1..]);
2073    body.strip_suffix(&format!("${tag}$"))
2074}
2075
2076/// Strip the quoting off an identifier.
2077///
2078/// DuckDB does not fold identifier case at any point, quoted or not, so this only removes the
2079/// quotes and resolves the doubled ones. Anything else would be the parser deciding what a name is.
2080///
2081/// Single quotes are stripped too, and the only way one gets here is the file name in `FROM
2082/// 'hits.parquet'`, because the matcher takes a string for a name in that position and in `COPY t TO
2083/// '...'` and nowhere else. Leaving them on would make that name different from the one `FROM
2084/// "hits.parquet"` writes, and DuckDB reads both of those as the same file.
2085fn unquote(text: &str) -> String {
2086    if let Some(body) = text.strip_prefix('"').and_then(|rest| rest.strip_suffix('"')) {
2087        return body.replace("\"\"", "\"");
2088    }
2089    match text.strip_prefix('\'').and_then(|rest| rest.strip_suffix('\'')) {
2090        Some(body) => body.replace("''", "'"),
2091        None => text.to_string(),
2092    }
2093}
2094
2095#[cfg(test)]
2096mod tests {
2097    use super::*;
2098    use crate::corpus::CORPUS;
2099    use crate::matcher::parse;
2100
2101    /// The AST written back out as text, which is what the assertions below read.
2102    ///
2103    /// Not a SQL printer and not trying to be. It is deliberately not valid SQL: operators are
2104    /// spelled with the name of the variant and every binary node is parenthesised, so that a test
2105    /// asserting on this text is asserting on the shape of the tree and not on a formatting choice.
2106    /// `a - b - c` and `a - (b - c)` have to look different here or the test that tells them apart
2107    /// is not a test.
2108    fn show(ast: &Ast, expr: ExprRef) -> String {
2109        if expr == NONE {
2110            return "-".to_string();
2111        }
2112        let list = |slice: Slice| {
2113            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
2114        };
2115        match ast.expr(expr) {
2116            Expr::Star { qualifier, replacements } => {
2117                let star = if qualifier.is_empty() {
2118                    "*".to_string()
2119                } else {
2120                    format!("{}.*", ast.name_text(qualifier))
2121                };
2122                if replacements.is_empty() {
2123                    return star;
2124                }
2125                let entries: Vec<String> = ast
2126                    .target_list(replacements)
2127                    .iter()
2128                    .map(|target| {
2129                        format!("{} AS {}", show(ast, target.expr), ast.string(target.alias))
2130                    })
2131                    .collect();
2132                format!("{star} REPLACE ({})", entries.join(", "))
2133            }
2134            Expr::Column { name } => ast.name_text(name),
2135            Expr::Literal { kind, text } => match kind {
2136                LiteralKind::Number => ast.string(text).to_string(),
2137                LiteralKind::String => format!("'{}'", ast.string(text)),
2138                other => format!("{other:?}").to_uppercase(),
2139            },
2140            Expr::Unary { op, operand } => format!("({op:?} {})", show(ast, operand)),
2141            Expr::Binary { op, left, right } => {
2142                let op = match op {
2143                    BinaryOp::Named(name) => ast.string(name).to_string(),
2144                    other => format!("{other:?}"),
2145                };
2146                format!("({} {op} {})", show(ast, left), show(ast, right))
2147            }
2148            Expr::Function { name, args, distinct } => {
2149                let distinct = if distinct { "DISTINCT " } else { "" };
2150                format!("{}({distinct}{})", ast.name_text(name), list(args))
2151            }
2152            Expr::Cast { operand, ty, try_cast } => {
2153                let word = if try_cast { "TRY_CAST" } else { "CAST" };
2154                format!("{word}({} AS {})", show(ast, operand), ast.string(ty))
2155            }
2156            Expr::Case { operand, arms, otherwise } => {
2157                let arms = ast
2158                    .arm_list(arms)
2159                    .iter()
2160                    .map(|arm| format!("WHEN {} THEN {}", show(ast, arm.when), show(ast, arm.then)))
2161                    .collect::<Vec<_>>()
2162                    .join(" ");
2163                format!("CASE {} {arms} ELSE {} END", show(ast, operand), show(ast, otherwise))
2164            }
2165            Expr::Between { operand, low, high, negated } => {
2166                let not = if negated { "NOT " } else { "" };
2167                format!(
2168                    "({not}{} BETWEEN {} AND {})",
2169                    show(ast, operand),
2170                    show(ast, low),
2171                    show(ast, high)
2172                )
2173            }
2174            Expr::In { operand, list: items, negated } => {
2175                let not = if negated { "NOT " } else { "" };
2176                format!("({not}{} IN [{}])", show(ast, operand), list(items))
2177            }
2178            Expr::List { items } => format!("[{}]", list(items)),
2179            Expr::Parameter { name } => format!("${}", ast.string(name)),
2180            Expr::Row { items } => format!("ROW({})", list(items)),
2181            Expr::Subquery { query } => format!("({})", show_query(ast, query)),
2182        }
2183    }
2184
2185    /// One from item written back out.
2186    fn show_source(ast: &Ast, source: SourceRef) -> String {
2187        let alias = |alias: StrRef| match alias {
2188            NONE => String::new(),
2189            other => format!(" AS {}", ast.string(other)),
2190        };
2191        match ast.source(source) {
2192            Source::Table { name, alias: name_alias, .. } => {
2193                format!("{}{}", ast.name_text(name), alias(name_alias))
2194            }
2195            Source::Function { name, args, alias: call_alias, .. } => {
2196                let args = ast
2197                    .target_list(args)
2198                    .iter()
2199                    .map(|item| match item.alias {
2200                        NONE => show(ast, item.expr),
2201                        named => format!("{} := {}", ast.string(named), show(ast, item.expr)),
2202                    })
2203                    .collect::<Vec<_>>()
2204                    .join(", ");
2205                format!("{}({args}){}", ast.name_text(name), alias(call_alias))
2206            }
2207            Source::Subquery { query, alias: query_alias, .. } => {
2208                format!("({}){}", show_query(ast, query), alias(query_alias))
2209            }
2210            Source::Values { rows, alias: values_alias, .. } => {
2211                format!("{}{}", show_rows(ast, rows), alias(values_alias))
2212            }
2213            Source::Join { left, right, kind, natural, on, using } => {
2214                let natural = if natural { "NATURAL " } else { "" };
2215                let on = if on == NONE { String::new() } else { format!(" ON {}", show(ast, on)) };
2216                let using = if using.is_empty() {
2217                    String::new()
2218                } else {
2219                    format!(" USING ({})", ast.name_text(using))
2220                };
2221                format!(
2222                    "({} {natural}{kind:?} JOIN {}{on}{using})",
2223                    show_source(ast, left),
2224                    show_source(ast, right)
2225                )
2226            }
2227        }
2228    }
2229
2230    /// The rows of a `VALUES` written back out.
2231    fn show_rows(ast: &Ast, rows: Slice) -> String {
2232        let rows = ast
2233            .rows(rows)
2234            .iter()
2235            .map(|&row| {
2236                let items = ast
2237                    .expr_list(row)
2238                    .iter()
2239                    .map(|&item| show(ast, item))
2240                    .collect::<Vec<_>>()
2241                    .join(", ");
2242                format!("({items})")
2243            })
2244            .collect::<Vec<_>>()
2245            .join(", ");
2246        format!("VALUES {rows}")
2247    }
2248
2249    /// One query written back out.
2250    fn show_query(ast: &Ast, index: QueryRef) -> String {
2251        let query = ast.query(index);
2252        let list = |slice: Slice| {
2253            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
2254        };
2255        let mut out = match query.body {
2256            QueryBody::SetOp { op, quantifier, by_name, left, right } => {
2257                let by_name = if by_name { " BY NAME" } else { "" };
2258                format!(
2259                    "({} {op:?} {quantifier:?}{by_name} {})",
2260                    show_query(ast, left),
2261                    show_query(ast, right)
2262                )
2263            }
2264            QueryBody::Select(index) => {
2265                let select = ast.select(index);
2266                let distinct = match select.distinct {
2267                    Distinct::No => String::new(),
2268                    Distinct::Yes => " DISTINCT".to_string(),
2269                    Distinct::On(on) => format!(" DISTINCT ON ({})", list(on)),
2270                };
2271                let targets = ast
2272                    .target_list(select.targets)
2273                    .iter()
2274                    .map(|target| match target.alias {
2275                        NONE => show(ast, target.expr),
2276                        alias => format!("{} AS {}", show(ast, target.expr), ast.string(alias)),
2277                    })
2278                    .collect::<Vec<_>>()
2279                    .join(", ");
2280                let mut out = format!("SELECT{distinct} {targets}");
2281                if !select.from.is_empty() {
2282                    let from = ast
2283                        .source_list(select.from)
2284                        .iter()
2285                        .map(|&source| show_source(ast, source))
2286                        .collect::<Vec<_>>()
2287                        .join(", ");
2288                    out += &format!(" FROM {from}");
2289                }
2290                if select.filter != NONE {
2291                    out += &format!(" WHERE {}", show(ast, select.filter));
2292                }
2293                if select.group_by_all {
2294                    out += " GROUP BY ALL";
2295                } else if !select.group_by.is_empty() {
2296                    out += &format!(" GROUP BY {}", list(select.group_by));
2297                }
2298                if select.having != NONE {
2299                    out += &format!(" HAVING {}", show(ast, select.having));
2300                }
2301                out
2302            }
2303            QueryBody::Values(rows) => show_rows(ast, rows),
2304            QueryBody::Describe(inner) => format!("DESCRIBE {}", show_query(ast, inner)),
2305        };
2306        if query.order_by_all {
2307            out += " ORDER BY ALL";
2308        } else if !query.order_by.is_empty() {
2309            let items = ast
2310                .order_list(query.order_by)
2311                .iter()
2312                .map(|item| format!("{} {:?} {:?}", show(ast, item.expr), item.order, item.nulls))
2313                .collect::<Vec<_>>()
2314                .join(", ");
2315            out += &format!(" ORDER BY {items}");
2316        }
2317        if query.limit != NONE {
2318            let percent = if query.limit_percent { "%" } else { "" };
2319            out += &format!(" LIMIT {}{percent}", show(ast, query.limit));
2320        }
2321        if query.offset != NONE {
2322            out += &format!(" OFFSET {}", show(ast, query.offset));
2323        }
2324        out
2325    }
2326
2327    /// One statement, transformed and written back out.
2328    fn round(query: &str) -> String {
2329        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
2330        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
2331        let Statement::Query(index) = ast.statements[0] else {
2332            panic!("{query} is not a query");
2333        };
2334        show_query(&ast, index)
2335    }
2336
2337    /// One statement, transformed and written back out as the DDL and DML shape it is.
2338    fn round_statement(query: &str) -> String {
2339        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
2340        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
2341        match ast.statements[0] {
2342            Statement::Query(index) => show_query(&ast, index),
2343            Statement::CreateTable(index) => {
2344                let create = ast.create_table(index);
2345                let mut out = "CREATE".to_string();
2346                if create.or_replace {
2347                    out += " OR REPLACE";
2348                }
2349                if create.temporary {
2350                    out += " TEMPORARY";
2351                }
2352                out += " TABLE";
2353                if create.if_not_exists {
2354                    out += " IF NOT EXISTS";
2355                }
2356                out += &format!(" {}", ast.name_text(create.name));
2357                let columns = ast
2358                    .column_defs(create.columns)
2359                    .iter()
2360                    .map(|def| {
2361                        let ty = match def.ty {
2362                            NONE => String::new(),
2363                            other => format!(" {}", ast.string(other)),
2364                        };
2365                        let null = if def.not_null { " NOT NULL" } else { "" };
2366                        format!("{}{ty}{null}", ast.string(def.name))
2367                    })
2368                    .collect::<Vec<_>>()
2369                    .join(", ");
2370                if !columns.is_empty() || create.query == NONE {
2371                    out += &format!(" ({columns})");
2372                }
2373                if create.query != NONE {
2374                    out += &format!(" AS {}", show_query(&ast, create.query));
2375                }
2376                out
2377            }
2378            Statement::CreateView(index) => {
2379                let create = ast.create_view(index);
2380                let mut out = "CREATE".to_string();
2381                if create.or_replace {
2382                    out += " OR REPLACE";
2383                }
2384                if create.temporary {
2385                    out += " TEMPORARY";
2386                }
2387                out += " VIEW";
2388                if create.if_not_exists {
2389                    out += " IF NOT EXISTS";
2390                }
2391                out += &format!(" {}", ast.name_text(create.name));
2392                if !create.columns.is_empty() {
2393                    let columns = ast.name(create.columns).collect::<Vec<_>>().join(", ");
2394                    out += &format!(" ({columns})");
2395                }
2396                out + &format!(" AS {}", show_query(&ast, create.query))
2397            }
2398            Statement::DropTable(index) => {
2399                let drop = ast.drop_table(index);
2400                let mut out = if drop.view { "DROP VIEW" } else { "DROP TABLE" }.to_string();
2401                if drop.if_exists {
2402                    out += " IF EXISTS";
2403                }
2404                let names = ast
2405                    .name_list(drop.names)
2406                    .iter()
2407                    .map(|&name| ast.name_text(name))
2408                    .collect::<Vec<_>>()
2409                    .join(", ");
2410                out + &format!(" {names}")
2411            }
2412            Statement::Insert(index) => {
2413                let insert = ast.insert(index);
2414                let mut out = format!("INSERT INTO {}", ast.name_text(insert.name));
2415                if !insert.columns.is_empty() {
2416                    let columns = ast.name(insert.columns).collect::<Vec<_>>().join(", ");
2417                    out += &format!(" ({columns})");
2418                }
2419                out + &format!(" {}", show_query(&ast, insert.source))
2420            }
2421            Statement::Set(index) => {
2422                let setting = ast.setting(index);
2423                let scope = match setting.scope.keyword() {
2424                    "" => String::new(),
2425                    word => format!(" {word}"),
2426                };
2427                format!("SET{scope} {} = {}", ast.string(setting.name), show(&ast, setting.value))
2428            }
2429            Statement::Reset(index) => {
2430                let setting = ast.setting(index);
2431                let scope = match setting.scope.keyword() {
2432                    "" => String::new(),
2433                    word => format!(" {word}"),
2434                };
2435                format!("RESET{scope} {}", ast.string(setting.name))
2436            }
2437        }
2438    }
2439
2440    #[test]
2441    fn a_set_keeps_its_name_its_scope_and_its_value() {
2442        assert_eq!(round_statement("SET memory_limit = '1GB'"), "SET memory_limit = '1GB'");
2443        assert_eq!(round_statement("set threads=4"), "SET threads = 4");
2444        assert_eq!(round_statement("SET GLOBAL threads = 4"), "SET GLOBAL threads = 4");
2445        assert_eq!(round_statement("SET SESSION threads = 4"), "SET SESSION threads = 4");
2446        assert_eq!(round_statement("SET LOCAL threads = 4"), "SET LOCAL threads = 4");
2447        assert_eq!(round_statement("RESET memory_limit"), "RESET memory_limit");
2448        assert_eq!(round_statement("RESET GLOBAL memory_limit"), "RESET GLOBAL memory_limit");
2449    }
2450
2451    #[test]
2452    fn the_two_other_things_the_word_set_starts_are_refused_rather_than_read_as_settings() {
2453        // `SET VARIABLE x = 1` declares a session variable and `SET SCHEMA` picks where an
2454        // unqualified name is looked up. Neither is a knob on the engine and reading either as one
2455        // would change an answer quietly.
2456        for statement in ["SET VARIABLE x = 1", "SET SCHEMA 'main'", "SET TIME ZONE 'UTC'"] {
2457            let error = parse_ast(statement).expect_err(statement);
2458            assert_eq!(error.code().duckdb_name(), "Not implemented Error", "{statement}");
2459        }
2460    }
2461
2462    #[test]
2463    fn a_setting_written_with_a_list_of_values_is_refused_rather_than_taking_the_first() {
2464        let error = parse_ast("SET search_path = a, b").expect_err("a list of two");
2465        assert_eq!(error.code().duckdb_name(), "Not implemented Error");
2466    }
2467
2468    #[test]
2469    fn the_query_m0_has_to_run_transforms() {
2470        assert_eq!(round("SELECT * FROM t WHERE x > 5"), "SELECT * FROM t WHERE (x Gt 5)");
2471    }
2472
2473    #[test]
2474    fn a_replace_list_rides_on_the_star_it_changes() {
2475        // The parentheses are optional around a single entry, which is how the clickbench load
2476        // recipe is not written but is how a lot of hand written sql is.
2477        assert_eq!(
2478            round("SELECT * REPLACE (a + 1 AS a) FROM t"),
2479            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
2480        );
2481        assert_eq!(
2482            round("SELECT * REPLACE a + 1 AS a FROM t"),
2483            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
2484        );
2485        assert_eq!(
2486            round("SELECT t.* REPLACE (make_date(a) AS a, b * 2 AS b) FROM t"),
2487            "SELECT t.* REPLACE (make_date(a) AS a, (b Multiply 2) AS b) FROM t"
2488        );
2489    }
2490
2491    #[test]
2492    fn one_column_cannot_be_replaced_twice() {
2493        // Caught here rather than in the binder because it is a mistake in what was written and
2494        // not a mistake about what is in the table, and duckdb reports it the same way.
2495        let error = parse_ast("SELECT * REPLACE (a + 1 AS a, a + 2 AS A) FROM t").unwrap_err();
2496        assert_eq!(error.to_string(), "Parser Error: Duplicate entry \"A\" in REPLACE list");
2497    }
2498
2499    #[test]
2500    fn a_table_function_argument_can_have_a_name_written_in_front_of_it() {
2501        // The grammar has `:=` and `=>`. It does not have `=`, which parses as a comparison and is
2502        // read back apart here, and that is the spelling the clickbench load recipe uses.
2503        for spelling in
2504            ["binary_as_string := True", "binary_as_string => True", "binary_as_string = True"]
2505        {
2506            assert_eq!(
2507                round(&format!("SELECT * FROM read_parquet('f.parquet', {spelling})")),
2508                "SELECT * FROM read_parquet('f.parquet', binary_as_string := TRUE)",
2509                "{spelling}"
2510            );
2511        }
2512    }
2513
2514    #[test]
2515    fn an_equality_that_is_not_a_bare_name_stays_an_argument() {
2516        // A qualified name on the left is not a parameter name, and neither is anything that is
2517        // not a name at all, so both of those stay the comparison they were written as.
2518        assert_eq!(round("SELECT * FROM f(t.a = 1)"), "SELECT * FROM f((t.a Eq 1))");
2519        assert_eq!(round("SELECT * FROM f(1 = 1)"), "SELECT * FROM f((1 Eq 1))");
2520    }
2521
2522    #[test]
2523    fn a_create_table_keeps_its_types_as_text() {
2524        assert_eq!(
2525            round_statement("CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"),
2526            "CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"
2527        );
2528        // The type is the text between the identifier and whatever follows it, parentheses and
2529        // all, because resolving `DECIMAL(18, 3)` into a width and a scale is the binder's job and
2530        // doing it here would mean two places that know the type table.
2531        assert_eq!(
2532            round_statement("CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"),
2533            "CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"
2534        );
2535    }
2536
2537    #[test]
2538    fn the_modifiers_on_a_create_table_survive() {
2539        assert_eq!(
2540            round_statement("CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"),
2541            "CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"
2542        );
2543        assert_eq!(
2544            round_statement("CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"),
2545            "CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"
2546        );
2547    }
2548
2549    #[test]
2550    fn or_replace_and_if_not_exists_in_one_statement_is_refused_here_and_not_later() {
2551        // The grammar has room for both and duckdb's has not, so its refusal is a parser error with
2552        // a caret under the `NOT` and this one is a parser error at the same stage. It is the same
2553        // sentence whatever is being created.
2554        for sql in [
2555            "CREATE OR REPLACE TABLE IF NOT EXISTS t (a INT)",
2556            "CREATE OR REPLACE VIEW IF NOT EXISTS v AS SELECT 1",
2557        ] {
2558            let error = parse_ast(sql).unwrap_err().to_string();
2559            assert_eq!(
2560                error,
2561                "Parser Error: Cannot specify both OR REPLACE and IF NOT EXISTS within single \
2562                 create statement"
2563            );
2564        }
2565    }
2566
2567    #[test]
2568    fn a_create_table_as_carries_the_query_and_not_the_types() {
2569        assert_eq!(
2570            round_statement("CREATE TABLE t AS SELECT a FROM u"),
2571            "CREATE TABLE t AS SELECT a FROM u"
2572        );
2573        // The names are the syntax's to say and the types are the query's, so the column
2574        // definitions here have names and no types.
2575        assert_eq!(
2576            round_statement("CREATE TABLE t (x, y) AS SELECT a, b FROM u"),
2577            "CREATE TABLE t (x, y) AS SELECT a, b FROM u"
2578        );
2579    }
2580
2581    #[test]
2582    fn a_create_view_carries_its_body_twice_over() {
2583        assert_eq!(
2584            round_statement("CREATE VIEW v AS SELECT a FROM u"),
2585            "CREATE VIEW v AS SELECT a FROM u"
2586        );
2587        assert_eq!(
2588            round_statement("CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"),
2589            "CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"
2590        );
2591        // The text the catalog keeps is the body and only the body, so that binding it again is
2592        // binding a query rather than a `CREATE` statement.
2593        let ast = parse_ast("CREATE VIEW v (x) AS SELECT a FROM u WHERE a > 1").expect("parses");
2594        let Statement::CreateView(index) = ast.statements[0] else {
2595            panic!("not a create view");
2596        };
2597        assert_eq!(ast.string(ast.create_view(index).sql), "SELECT a FROM u WHERE a > 1");
2598    }
2599
2600    #[test]
2601    fn a_drop_view_is_not_a_drop_table() {
2602        assert_eq!(round_statement("DROP VIEW IF EXISTS a, b"), "DROP VIEW IF EXISTS a, b");
2603        assert_eq!(round_statement("DROP TABLE a"), "DROP TABLE a");
2604    }
2605
2606    #[test]
2607    fn a_drop_table_is_a_list_of_qualified_names() {
2608        assert_eq!(round_statement("DROP TABLE t"), "DROP TABLE t");
2609        assert_eq!(round_statement("DROP TABLE IF EXISTS a, b.c"), "DROP TABLE IF EXISTS a, b.c");
2610    }
2611
2612    #[test]
2613    fn dropping_something_that_is_neither_a_table_nor_a_view_is_refused() {
2614        // `TableOrView` covers `MATERIALIZED VIEW` as well, which is not a thing this database has,
2615        // and dropping one as if it were an ordinary view is a wrong answer rather than a missing
2616        // feature.
2617        let error = parse_ast("DROP MATERIALIZED VIEW v").unwrap_err().to_string();
2618        assert!(error.starts_with("Not implemented Error"), "{error}");
2619    }
2620
2621    #[test]
2622    fn both_spellings_of_insert_arrive_at_a_query() {
2623        assert_eq!(
2624            round_statement("INSERT INTO t VALUES (1, 'a'), (2, 'b')"),
2625            "INSERT INTO t VALUES (1, 'a'), (2, 'b')"
2626        );
2627        assert_eq!(
2628            round_statement("INSERT INTO t (a, b) SELECT x, y FROM u"),
2629            "INSERT INTO t (a, b) SELECT x, y FROM u"
2630        );
2631    }
2632
2633    #[test]
2634    fn an_insert_clause_that_changes_the_answer_is_refused() {
2635        for query in [
2636            "INSERT INTO t VALUES (1) RETURNING *",
2637            "INSERT OR REPLACE INTO t VALUES (1)",
2638            "INSERT INTO t BY NAME SELECT 1 AS a",
2639            "INSERT INTO t VALUES (1) ON CONFLICT DO NOTHING",
2640            "INSERT INTO t DEFAULT VALUES",
2641        ] {
2642            let error = parse_ast(query).unwrap_err().to_string();
2643            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
2644        }
2645    }
2646
2647    #[test]
2648    fn a_column_constraint_that_is_not_not_null_is_refused() {
2649        // Nothing enforces a constraint yet. Accepting one and not enforcing it is the wrong
2650        // answer, so `NOT NULL` is kept because the column already has a nullability and the rest
2651        // are refused until there is somewhere to put them.
2652        for query in [
2653            "CREATE TABLE t (a INT PRIMARY KEY)",
2654            "CREATE TABLE t (a INT UNIQUE)",
2655            "CREATE TABLE t (a INT CHECK (a > 0))",
2656            "CREATE TABLE t (a INT DEFAULT 1)",
2657            "CREATE TABLE t (a INT REFERENCES u (b))",
2658            "CREATE TABLE t (a INT, PRIMARY KEY (a))",
2659        ] {
2660            let error = parse_ast(query).unwrap_err().to_string();
2661            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
2662        }
2663    }
2664
2665    #[test]
2666    fn values_is_a_query_on_its_own_and_in_a_from() {
2667        assert_eq!(round("VALUES (1), (2)"), "VALUES (1), (2)");
2668        // Parenthesised it is a subquery whose body is the values, and bare it is a `ValuesRef`.
2669        // Two rules and one meaning, which is the grammar's doing and not something to flatten
2670        // here, because the parenthesised form can carry an order by and the bare one cannot.
2671        assert_eq!(
2672            round("SELECT * FROM (VALUES (1, 2), (3, 4)) t(a, b)"),
2673            "SELECT * FROM (VALUES (1, 2), (3, 4)) AS t"
2674        );
2675        assert_eq!(
2676            round("SELECT * FROM VALUES (1, 2), (3, 4) AS t(a, b)"),
2677            "SELECT * FROM VALUES (1, 2), (3, 4) AS t"
2678        );
2679        // Rows of different widths parse. Saying so wants the column count, which for an insert is
2680        // the table's, so the check belongs to the binder and not here.
2681        assert_eq!(round("VALUES (1), (2, 3)"), "VALUES (1), (2, 3)");
2682    }
2683
2684    /// `DESCRIBE` is a query body, and the two spellings that name something become a star over it.
2685    ///
2686    /// Naming a table is not a shortcut for the query. On the reference binary `DESCRIBE t` and
2687    /// `DESCRIBE SELECT * FROM t` print the same six columns and the same rows, down to the `NO` on
2688    /// a column that refuses nulls, so rewriting one into the other costs nothing and leaves the
2689    /// binder with one case instead of three. A file name goes down the same path as a table name
2690    /// because a bare string in a `FROM` clause is already a name the replacement scan picks up.
2691    #[test]
2692    fn describe_rewrites_a_name_into_a_star_over_it() {
2693        assert_eq!(round("DESCRIBE SELECT 1 AS a"), "DESCRIBE SELECT 1 AS a");
2694        assert_eq!(round("DESCRIBE t"), "DESCRIBE SELECT * FROM t");
2695        assert_eq!(round("DESC t"), "DESCRIBE SELECT * FROM t");
2696        assert_eq!(round("DESCRIBE 'x.parquet'"), "DESCRIBE SELECT * FROM x.parquet");
2697        // A body and not a statement kind, so it nests both ways with no rule of its own.
2698        assert_eq!(
2699            round("SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"),
2700            "SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"
2701        );
2702        assert_eq!(round("DESCRIBE DESCRIBE SELECT 1 AS a"), "DESCRIBE DESCRIBE SELECT 1 AS a");
2703    }
2704
2705    /// `SUMMARIZE` shares both of `DESCRIBE`'s grammar rules and is a different statement.
2706    ///
2707    /// It reads every row and returns one row per column carrying the min, the max, the count and
2708    /// the approximate distinct count, so none of it falls out of the `DESCRIBE` path. The word is
2709    /// the only thing in the tree that tells the two apart, which is why the transform looks at it
2710    /// rather than trusting the rule name it arrived under.
2711    #[test]
2712    fn summarize_is_refused_even_though_it_parses_as_a_describe() {
2713        for query in ["SUMMARIZE t", "SUMMARIZE SELECT 1"] {
2714            let error = parse_ast(query).expect_err("summarize is not implemented");
2715            let message = error.to_string();
2716            assert!(message.starts_with("Not implemented Error"), "{query} failed with {message}");
2717        }
2718    }
2719
2720    #[test]
2721    fn every_statement_in_the_corpus_gets_a_defined_answer() {
2722        // The point of the test is the word defined. Forty of these are statement kinds and
2723        // clauses this milestone does not cover, and the requirement is not that they work, it is
2724        // that they fail by saying so. A panic, a silently dropped clause or an internal error
2725        // would each be a different bug and all three would be invisible without this.
2726        let mut done = 0;
2727        for query in CORPUS {
2728            match parse_ast(query) {
2729                Ok(ast) => {
2730                    assert_eq!(ast.statements.len(), 1, "{query}");
2731                    done += 1;
2732                }
2733                Err(error) => {
2734                    let message = error.to_string();
2735                    assert!(
2736                        message.starts_with("Not implemented Error"),
2737                        "{query} failed with {message}, which is not a not-implemented error"
2738                    );
2739                }
2740            }
2741        }
2742        // Not an assertion about the right number. It is a ratchet: this only moves up, and the
2743        // day it moves down somebody has taken a construct out without meaning to.
2744        assert!(done >= 23, "only {done} of the corpus transforms, which is fewer than it was");
2745    }
2746
2747    #[test]
2748    fn the_ast_is_far_smaller_than_the_parse_tree() {
2749        let query = CORPUS[4];
2750        let tree = parse(query).unwrap();
2751        let ast = parse_ast(query).unwrap();
2752        // The twenty precedence levels are the difference. Every one of them is a node in the
2753        // parse tree for every expression at every depth, and none of them survives into the AST.
2754        assert!(
2755            ast.node_count() * 20 < tree.arena_len(),
2756            "{} ast nodes against {} parse nodes",
2757            ast.node_count(),
2758            tree.arena_len()
2759        );
2760    }
2761
2762    #[test]
2763    fn precedence_comes_out_of_the_chain_and_into_the_tree() {
2764        assert_eq!(round("SELECT 1 + 2 * 3"), "SELECT (1 Add (2 Multiply 3))");
2765        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
2766        assert_eq!(round("SELECT 1 + 2 + 3"), "SELECT ((1 Add 2) Add 3)");
2767        assert_eq!(round("SELECT 1 - 2 - 3"), "SELECT ((1 Subtract 2) Subtract 3)");
2768        assert_eq!(
2769            round("SELECT a OR b AND c"),
2770            "SELECT (a Or (b And c))",
2771            "and binds tighter than or"
2772        );
2773    }
2774
2775    #[test]
2776    fn a_double_negation_is_two_nodes_and_not_none() {
2777        // Folding it would be an optimizer decision and this is not the optimizer. It also would
2778        // not be safe in general: `NOT NOT x` on a null is still null and on a non boolean it is
2779        // still an error, and both of those have to survive to the binder to be reported.
2780        assert_eq!(round("SELECT NOT NOT a"), "SELECT (Not (Not a))");
2781    }
2782
2783    #[test]
2784    fn a_parenthesised_single_expression_is_not_a_row() {
2785        assert_eq!(round("SELECT (a)"), "SELECT a");
2786        assert_eq!(round("SELECT (a, b)"), "SELECT ROW(a, b)");
2787    }
2788
2789    #[test]
2790    fn a_bracketed_list_is_a_list_of_however_many_items_were_written() {
2791        // One item is a list of one, which is where this parts company with the parenthesised form
2792        // above: `(a)` is `a` and `[a]` is a list, because the brackets are what say list.
2793        assert_eq!(round("SELECT [a]"), "SELECT [a]");
2794        assert_eq!(round("SELECT [1, 2, 3]"), "SELECT [1, 2, 3]");
2795        assert_eq!(round("SELECT []"), "SELECT []");
2796        assert_eq!(round("SELECT ['a.parquet', 'b.parquet']"), "SELECT ['a.parquet', 'b.parquet']");
2797    }
2798
2799    #[test]
2800    fn a_parameter_carries_its_identifier_however_it_was_written() {
2801        assert_eq!(round("SELECT $1"), "SELECT $1");
2802        assert_eq!(round("SELECT ?1"), "SELECT $1");
2803        assert_eq!(round("SELECT $name"), "SELECT $name");
2804        // A bare question mark is numbered by where it is, and the counting is its own, so a later
2805        // `$2` does not push the first one along. This is duckdb v1.4.1, which prints `$1 + $2`.
2806        assert_eq!(round("SELECT ? + $2"), "SELECT ($1 Add $2)");
2807        assert_eq!(round("SELECT ?, ?, ?"), "SELECT $1, $2, $3");
2808    }
2809
2810    #[test]
2811    fn the_parameters_of_a_statement_are_listed_once_each_in_written_order() {
2812        let ast = parse_ast("SELECT $b, $a, $b WHERE $a").expect("parses");
2813        assert_eq!(ast.parameters(), vec!["b", "a"]);
2814        assert!(parse_ast("SELECT 1").expect("parses").parameters().is_empty());
2815    }
2816
2817    #[test]
2818    fn the_three_ways_to_write_an_alias_all_arrive() {
2819        assert_eq!(round("SELECT a AS b"), "SELECT a AS b");
2820        assert_eq!(round("SELECT a b"), "SELECT a AS b");
2821        assert_eq!(round("SELECT b: a"), "SELECT a AS b");
2822        assert_eq!(round("SELECT a"), "SELECT a", "and no alias when none was written");
2823    }
2824
2825    #[test]
2826    fn a_from_with_no_select_selects_everything() {
2827        // DuckDB's own shorthand. Inventing the star here rather than in the binder means the
2828        // binder never has to know that the clause it is looking at was the one that was missing.
2829        assert_eq!(round("FROM t"), "SELECT * FROM t");
2830        assert_eq!(round("FROM t SELECT a"), "SELECT a FROM t");
2831    }
2832
2833    #[test]
2834    fn joins_nest_to_the_left() {
2835        assert_eq!(
2836            round("SELECT * FROM a JOIN b ON a.i = b.i LEFT JOIN c USING (k)"),
2837            "SELECT * FROM ((a Inner JOIN b ON (a.i Eq b.i)) Left JOIN c USING (k))"
2838        );
2839        assert_eq!(
2840            round("SELECT * FROM a NATURAL JOIN b"),
2841            "SELECT * FROM (a NATURAL Inner JOIN b)"
2842        );
2843        assert_eq!(round("SELECT * FROM a CROSS JOIN b"), "SELECT * FROM (a Cross JOIN b)");
2844        assert_eq!(
2845            round("SELECT * FROM a POSITIONAL JOIN b"),
2846            "SELECT * FROM (a Positional JOIN b)"
2847        );
2848        assert_eq!(round("SELECT * FROM a, b"), "SELECT * FROM a, b", "a comma is not a join node");
2849    }
2850
2851    #[test]
2852    fn a_qualified_name_keeps_its_parts_however_it_was_spelled() {
2853        // Five grammar rules can produce a column reference and they disagree about which
2854        // component is a schema and which is a table. None of that is decidable without the
2855        // catalog, so the AST holds the parts and the binder decides.
2856        assert_eq!(round("SELECT a"), "SELECT a");
2857        assert_eq!(round("SELECT t.a"), "SELECT t.a");
2858        assert_eq!(round("SELECT s.t.a"), "SELECT s.t.a");
2859        assert_eq!(round("SELECT c.s.t.a"), "SELECT c.s.t.a");
2860        assert_eq!(round("SELECT * FROM s.t"), "SELECT * FROM s.t");
2861    }
2862
2863    #[test]
2864    fn a_star_can_be_qualified() {
2865        assert_eq!(round("SELECT *"), "SELECT *");
2866        assert_eq!(round("SELECT t.*"), "SELECT t.*");
2867        assert_eq!(round("SELECT s.t.*"), "SELECT s.t.*");
2868    }
2869
2870    #[test]
2871    fn a_quoted_identifier_keeps_its_case_and_loses_its_quotes() {
2872        // DuckDB does not fold identifier case at any point, quoted or not, which the tokenizer
2873        // work established by reading the source. So the only thing to do here is take the quotes
2874        // off and resolve the doubled ones.
2875        let ast = parse_ast("SELECT \"Mixed Case\", \"a\"\"b\"").unwrap();
2876        assert_eq!(ast.strings[0], "Mixed Case");
2877        assert_eq!(ast.strings[1], "a\"b");
2878    }
2879
2880    #[test]
2881    fn a_string_literal_is_decoded_and_adjacent_ones_are_joined() {
2882        assert_eq!(round("SELECT 'it''s'"), "SELECT 'it's'");
2883        assert_eq!(round("SELECT 'a'\n'b'"), "SELECT 'ab'", "the standard's adjacency rule");
2884    }
2885
2886    /// Per #276, where the tag and the dollars were coming through as part of the value.
2887    #[test]
2888    fn a_dollar_quoted_string_loses_its_dollars_and_its_tag() {
2889        assert_eq!(round("SELECT $$dollar quoted$$"), "SELECT 'dollar quoted'");
2890        assert_eq!(round("SELECT $tag$body$tag$"), "SELECT 'body'");
2891        assert_eq!(round("SELECT $$$$"), "SELECT ''", "an empty tag and an empty body");
2892        // Nothing in the body is escaped, which is what the spelling is for, so a quote is a quote
2893        // and a dollar that is not the closing tag is a dollar.
2894        assert_eq!(round("SELECT $tag$it''s $other$ fine$tag$"), "SELECT 'it''s $other$ fine'");
2895        // An unterminated one has no closing tag to take off and keeps every byte it was given.
2896        assert_eq!(round("SELECT $$open"), "SELECT '$$open'");
2897    }
2898
2899    #[test]
2900    fn the_null_and_boolean_tests_are_postfix_unary_operators() {
2901        assert_eq!(round("SELECT x IS NULL"), "SELECT (IsNull x)");
2902        assert_eq!(round("SELECT x IS NOT NULL"), "SELECT (IsNotNull x)");
2903        assert_eq!(round("SELECT x ISNULL"), "SELECT (IsNull x)");
2904        assert_eq!(round("SELECT x NOTNULL"), "SELECT (IsNotNull x)");
2905        assert_eq!(round("SELECT x IS TRUE"), "SELECT (IsTrue x)");
2906        assert_eq!(round("SELECT x IS NOT FALSE"), "SELECT (IsNotFalse x)");
2907        assert_eq!(round("SELECT x IS DISTINCT FROM y"), "SELECT (x IsDistinctFrom y)");
2908        assert_eq!(round("SELECT x IS NOT DISTINCT FROM y"), "SELECT (x IsNotDistinctFrom y)");
2909    }
2910
2911    #[test]
2912    fn the_like_family_folds_its_negation_into_the_operator() {
2913        assert_eq!(round("SELECT x LIKE 'a'"), "SELECT (x Like 'a')");
2914        assert_eq!(round("SELECT x NOT LIKE 'a'"), "SELECT (x NotLike 'a')");
2915        assert_eq!(round("SELECT x ILIKE 'a'"), "SELECT (x ILike 'a')");
2916        assert_eq!(round("SELECT x ~~ 'a'"), "SELECT (x Like 'a')", "the operator spelling");
2917        assert_eq!(round("SELECT x !~~ 'a'"), "SELECT (x NotLike 'a')");
2918        assert_eq!(round("SELECT x SIMILAR TO 'a'"), "SELECT (x SimilarTo 'a')");
2919        // Glob has no negated operator to fold into, so the negation stays where it was written.
2920        assert_eq!(round("SELECT x NOT GLOB 'a'"), "SELECT (Not (x Glob 'a'))");
2921    }
2922
2923    #[test]
2924    fn between_and_in_carry_their_negation_as_a_flag() {
2925        assert_eq!(round("SELECT x BETWEEN 1 AND 2"), "SELECT (x BETWEEN 1 AND 2)");
2926        assert_eq!(round("SELECT x NOT BETWEEN 1 AND 2"), "SELECT (NOT x BETWEEN 1 AND 2)");
2927        assert_eq!(round("SELECT x IN (1, 2)"), "SELECT (x IN [1, 2])");
2928        assert_eq!(round("SELECT x NOT IN (1, 2)"), "SELECT (NOT x IN [1, 2])");
2929    }
2930
2931    #[test]
2932    fn both_spellings_of_a_cast_are_the_same_node() {
2933        assert_eq!(round("SELECT CAST(x AS BIGINT)"), "SELECT CAST(x AS BIGINT)");
2934        assert_eq!(round("SELECT x::BIGINT"), "SELECT CAST(x AS BIGINT)");
2935        assert_eq!(round("SELECT TRY_CAST(x AS BIGINT)"), "SELECT TRY_CAST(x AS BIGINT)");
2936        assert_eq!(
2937            round("SELECT x::DECIMAL(18, 3)"),
2938            "SELECT CAST(x AS DECIMAL(18, 3))",
2939            "the type is kept as text because parsing it is the type system's job"
2940        );
2941    }
2942
2943    #[test]
2944    fn a_case_keeps_its_arms_in_order() {
2945        assert_eq!(
2946            round("SELECT CASE WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"),
2947            "SELECT CASE - WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"
2948        );
2949        assert_eq!(
2950            round("SELECT CASE x WHEN 1 THEN 'a' END"),
2951            "SELECT CASE x WHEN 1 THEN 'a' ELSE - END",
2952            "a simple case keeps the operand and a missing else is not an implicit null yet"
2953        );
2954    }
2955
2956    #[test]
2957    fn a_field_access_and_a_method_call_are_ordinary_function_calls() {
2958        // Which is what DuckDB makes of them too. Giving each its own AST node would mean the
2959        // binder needs a rule for something the function resolver already handles.
2960        assert_eq!(round("SELECT (f(x)).y"), "SELECT struct_extract(f(x), 'y')");
2961        assert_eq!(round("SELECT a[1]"), "SELECT array_extract(a, 1)");
2962    }
2963
2964    /// The four ways of leaving a bound out, all of which upstream fills in the same way.
2965    #[test]
2966    fn a_range_gets_the_bounds_the_query_left_out() {
2967        assert_eq!(round("SELECT a[1:2]"), "SELECT array_slice(a, 1, 2)");
2968        assert_eq!(round("SELECT a[:2]"), "SELECT array_slice(a, 1, 2)");
2969        assert_eq!(round("SELECT a[2:]"), "SELECT array_slice(a, 2, -1)");
2970        assert_eq!(round("SELECT a[:]"), "SELECT array_slice(a, 1, -1)");
2971        // `EndSliceMinus`, which is a range with no end rather than a subtraction of nothing.
2972        assert_eq!(round("SELECT a[1:-]"), "SELECT array_slice(a, 1, -1)");
2973        assert_eq!(round("SELECT a[1:2:3]"), "SELECT array_slice(a, 1, 2, 3)");
2974        // A step that was written and left empty, which upstream fills with a list so that the call
2975        // fails to bind. Answering a row here would be answering where the reference refuses.
2976        assert_eq!(round("SELECT a[1:2:]"), "SELECT array_slice(a, 1, 2, [])");
2977    }
2978
2979    /// `[]` is the one subscript the parser takes and the transformer refuses, in upstream's words.
2980    #[test]
2981    fn an_empty_subscript_is_not_a_subscript() {
2982        let error = parse_ast("SELECT a[]").expect_err("an empty subscript");
2983        assert_eq!(error.message(), "Empty subscript '[]' is not allowed");
2984    }
2985
2986    /// The three spellings of a null check, two of which are their own grammar rule. Per #306.
2987    #[test]
2988    fn the_null_checks_are_calls_by_the_names_duckdb_prints() {
2989        // The keyword is the name, so the call is written with the canonical spelling of it whichever
2990        // case the query used. What the column is called is the binder's to decide.
2991        assert_eq!(round("SELECT COALESCE(a, b, 1)"), "SELECT coalesce(a, b, 1)");
2992        assert_eq!(round("SELECT coalesce(a)"), "SELECT coalesce(a)");
2993        assert_eq!(round("SELECT NULLIF(a, 1)"), "SELECT nullif(a, 1)");
2994        // `IFNULL` is a plain call that upstream's parser turns into the operator, qualifier and all.
2995        assert_eq!(round("SELECT ifnull(a, 1)"), "SELECT coalesce(a, 1)");
2996        assert_eq!(round("SELECT main.ifnull(a, 1)"), "SELECT coalesce(a, 1)");
2997        let error = parse_ast("SELECT ifnull(a)").expect_err("one argument to ifnull");
2998        assert_eq!(error.message(), "Wrong number of arguments to IFNULL.");
2999        let error = parse_ast("SELECT ifnull(a, b, c)").expect_err("three arguments to ifnull");
3000        assert_eq!(error.message(), "Wrong number of arguments to IFNULL.");
3001    }
3002
3003    #[test]
3004    fn an_aggregate_keeps_its_distinct() {
3005        assert_eq!(round("SELECT count(*)"), "SELECT count(*)");
3006        assert_eq!(round("SELECT count(DISTINCT x)"), "SELECT count(DISTINCT x)");
3007        assert_eq!(round("SELECT count(ALL x)"), "SELECT count(x)");
3008        assert_eq!(round("SELECT main.count(x)"), "SELECT main.count(x)");
3009    }
3010
3011    #[test]
3012    fn the_modifiers_hang_off_the_query_and_not_off_the_select() {
3013        // `a UNION b ORDER BY x` sorts the union. Putting the order by on the select would have
3014        // made that unrepresentable, which is why the grammar puts it outside the chain and why
3015        // the AST follows.
3016        assert_eq!(
3017            round("SELECT 1 UNION ALL SELECT 2 ORDER BY 1"),
3018            "(SELECT 1 Union All SELECT 2) ORDER BY 1 Unstated Unstated"
3019        );
3020        assert_eq!(
3021            round("SELECT a FROM t UNION SELECT b FROM u EXCEPT SELECT c FROM v"),
3022            "((SELECT a FROM t Union Unstated SELECT b FROM u) Except Unstated SELECT c FROM v)",
3023            "set operators are left associative"
3024        );
3025        assert_eq!(
3026            round("SELECT 1 UNION SELECT 2 INTERSECT SELECT 3"),
3027            "(SELECT 1 Union Unstated (SELECT 2 Intersect Unstated SELECT 3))",
3028            "and intersect binds tighter than the other two"
3029        );
3030    }
3031
3032    #[test]
3033    fn the_sort_and_limit_clauses_keep_what_was_written() {
3034        assert_eq!(
3035            round("SELECT a FROM t ORDER BY a"),
3036            "SELECT a FROM t ORDER BY a Unstated Unstated"
3037        );
3038        assert_eq!(
3039            round("SELECT a FROM t ORDER BY a DESC NULLS LAST"),
3040            "SELECT a FROM t ORDER BY a Descending Last"
3041        );
3042        assert_eq!(round("SELECT a FROM t ORDER BY ALL"), "SELECT a FROM t ORDER BY ALL");
3043        assert_eq!(round("SELECT a FROM t GROUP BY ALL"), "SELECT a FROM t GROUP BY ALL");
3044        assert_eq!(round("SELECT a FROM t LIMIT 10 OFFSET 5"), "SELECT a FROM t LIMIT 10 OFFSET 5");
3045        assert_eq!(round("SELECT a FROM t OFFSET 5 LIMIT 10"), "SELECT a FROM t LIMIT 10 OFFSET 5");
3046        assert_eq!(round("SELECT a FROM t LIMIT 10%"), "SELECT a FROM t LIMIT 10%");
3047        assert_eq!(round("SELECT a FROM t LIMIT ALL"), "SELECT a FROM t", "which is no limit");
3048    }
3049
3050    #[test]
3051    fn a_subquery_appears_in_both_places_it_can() {
3052        assert_eq!(
3053            round("SELECT * FROM (SELECT x FROM t) AS s"),
3054            "SELECT * FROM (SELECT x FROM t) AS s"
3055        );
3056        assert_eq!(round("SELECT (SELECT 1)"), "SELECT (SELECT 1)");
3057    }
3058
3059    #[test]
3060    fn distinct_on_keeps_its_expressions() {
3061        assert_eq!(round("SELECT DISTINCT a"), "SELECT DISTINCT a");
3062        assert_eq!(round("SELECT ALL a"), "SELECT a", "which is the default written out");
3063        assert_eq!(round("SELECT DISTINCT ON (a, b) a"), "SELECT DISTINCT ON (a, b) a");
3064    }
3065
3066    #[test]
3067    fn an_operator_the_dialect_does_not_name_is_kept_by_name() {
3068        // The grammar text says `OperatorLiteral <- Identifier`, which reads as though any bare
3069        // word could be written infix. It cannot. That rule is one of the 24 the matcher overrides
3070        // and it is overridden to the bare operator matcher, so what it takes is a run of operator
3071        // characters. Believing the body here would have produced a transformer that accepted
3072        // `a foo b`, which DuckDB rejects.
3073        assert_eq!(round("SELECT a <=> b"), "SELECT (a <=> b)");
3074        assert!(parse_ast("SELECT a foo b").is_err(), "a bare word is not an operator");
3075    }
3076
3077    #[test]
3078    fn a_script_is_a_list_of_statements() {
3079        let ast = parse_ast("SELECT 1; SELECT 2;").unwrap();
3080        assert_eq!(ast.statements.len(), 2);
3081        // A trailing semicolon makes an empty top level statement in the parse tree, because the
3082        // grammar's `Statement? (';'+ / EndOfInput)` is happy with nothing on both sides. It is
3083        // dropped here rather than pretended away in the matcher.
3084        let Statement::Query(second) = ast.statements[1] else {
3085            panic!("the second statement is a query");
3086        };
3087        assert_eq!(show_query(&ast, second), "SELECT 2");
3088    }
3089
3090    #[test]
3091    fn an_unsupported_construct_names_itself_and_what_was_written() {
3092        let error = parse_ast("ALTER TABLE t ADD COLUMN a INTEGER").unwrap_err().to_string();
3093        assert!(error.starts_with("Not implemented Error"), "{error}");
3094        assert!(error.contains("ALTER TABLE t ADD COLUMN a INTEGER"), "{error}");
3095        assert!(error.contains("AlterStatement"), "{error}");
3096    }
3097
3098    #[test]
3099    fn a_long_construct_is_cut_short_in_the_message() {
3100        let query = format!("ALTER TABLE t ADD COLUMN {} INTEGER", "a".repeat(80));
3101        let error = parse_ast(&query).unwrap_err().to_string();
3102        assert!(error.contains("..."), "{error}");
3103        assert!(error.len() < 200, "{error}");
3104    }
3105
3106    #[test]
3107    fn the_transformer_never_panics_on_anything_the_matcher_accepts() {
3108        // The matcher accepts a good deal that means nothing, because the grammar does. Every one
3109        // of these parses and none of them is a statement this milestone covers, and the contract
3110        // is that the answer is an error either way.
3111        for query in [
3112            "SELECT",
3113            "FROM t SELECT",
3114            "SELECT * FROM t WHERE",
3115            "SELECT ()",
3116            "SELECT a FROM t GROUP BY ()",
3117        ] {
3118            let answer = parse_ast(query);
3119            if let Err(error) = answer {
3120                let message = error.to_string();
3121                assert!(
3122                    message.starts_with("Not implemented Error")
3123                        || message.starts_with("Parser Error"),
3124                    "{query} failed with {message}"
3125                );
3126            }
3127        }
3128    }
3129
3130    #[test]
3131    fn a_file_name_in_a_from_clause_is_a_table_name_with_the_quotes_off() {
3132        // Both spellings have to arrive as the same name, because the binder decides whether it is
3133        // a file by looking at the name, and `'hits.parquet'` with the quotes still on it is not
3134        // a path that anything can open.
3135        assert_eq!(round("SELECT * FROM 'hits.parquet'"), "SELECT * FROM hits.parquet");
3136        assert_eq!(round("SELECT * FROM \"hits.parquet\""), "SELECT * FROM hits.parquet");
3137        assert_eq!(round("SELECT * FROM 'hits.parquet' AS h"), "SELECT * FROM hits.parquet AS h");
3138    }
3139
3140    #[test]
3141    fn a_function_call_in_a_from_clause_is_a_source_and_not_an_expression() {
3142        assert_eq!(round("SELECT * FROM range(3)"), "SELECT * FROM range(3)");
3143        assert_eq!(round("SELECT * FROM range(1, 10, 2)"), "SELECT * FROM range(1, 10, 2)");
3144        assert_eq!(round("SELECT * FROM main.range(3)"), "SELECT * FROM main.range(3)");
3145        assert_eq!(round("SELECT * FROM range(3) AS t"), "SELECT * FROM range(3) AS t");
3146        // The grammar allows a call with no arguments here and the transformer keeps it, because
3147        // whether a particular function takes none is the binder's question and not this one's.
3148        assert_eq!(round("SELECT * FROM some_function()"), "SELECT * FROM some_function()");
3149    }
3150
3151    #[test]
3152    fn the_forms_of_a_table_function_this_does_not_cover_are_turned_away_by_name() {
3153        for query in [
3154            "SELECT * FROM range(3) WITH ORDINALITY",
3155            "SELECT * FROM LATERAL range(3)",
3156            "SELECT * FROM t: range(3)",
3157        ] {
3158            let error = parse_ast(query).unwrap_err().to_string();
3159            assert!(error.contains("grammar rule"), "{query} failed with {error}");
3160        }
3161    }
3162
3163    #[test]
3164    fn interning_means_a_name_written_twice_is_stored_once() {
3165        let ast = parse_ast("SELECT a, a, a FROM t WHERE a = a").unwrap();
3166        assert_eq!(ast.strings.iter().filter(|text| *text == "a").count(), 1);
3167    }
3168}