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, Value};
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            "ExplainStatement" => self.explain_statement(inner),
293            _ => self.unsupported(inner),
294        }
295    }
296
297    /// `ExplainStatement <- 'EXPLAIN' AnalyzeKeyword? ExplainOptionList? ExplainableStatements`.
298    ///
299    /// Of the twenty one explainable statements, the one that is done is the query. The other
300    /// twenty either do not exist here yet or have nothing to show: a plan is what `EXPLAIN`
301    /// prints, and a `SET` has no plan. An `INSERT` has a plan for its source and showing that
302    /// would answer a question nobody asked, since the source is not what the statement does.
303    ///
304    /// The option list is a refusal. `EXPLAIN (FORMAT JSON)` asks for the plan in a shape nothing
305    /// here writes, and answering it with the text form would be answering a different question
306    /// quietly.
307    fn explain_statement(&mut self, node: u32) -> Result<Statement> {
308        let analyze = self.find(node, "AnalyzeKeyword") != NONE;
309        let options = self.find(node, "ExplainOptionList");
310        if options != NONE {
311            return self.unsupported(options);
312        }
313        let inner = self.first(self.find(node, "ExplainableStatements"));
314        if self.name(inner) != "ExplainSelectStatement" {
315            return self.unsupported(inner);
316        }
317        let query = self.query(self.find(inner, "SelectStatementInternal"))?;
318        Ok(Statement::Explain { query, analyze })
319    }
320
321    /// `SetStatement <- 'SET' SetAssignmentOrTimeZone`.
322    ///
323    /// Of the three assignments, `StandardAssignment` is the one that is done. `SET SCHEMA` and
324    /// `SET TIME ZONE` are each a setting this database has nothing to do with yet, and they are a
325    /// refusal rather than a silent success, because a statement that says where to look for a
326    /// table and is ignored is a statement that changes an answer.
327    fn set_statement(&mut self, node: u32) -> Result<Statement> {
328        let inner = self.first(self.find(node, "SetAssignmentOrTimeZone"));
329        if self.name(inner) != "StandardAssignment" {
330            return self.unsupported(inner);
331        }
332        let (name, scope) = self.setting_name(self.find(inner, "SetVariableOrSetting"))?;
333        let assignment = self.find(inner, "SetAssignment");
334        let list = self.find(assignment, "VariableList");
335        let mut values = Vec::new();
336        for kid in self.kids(list) {
337            values.push(self.expr(kid)?);
338        }
339        // The grammar takes a list because `SET search_path = a, b` is a list in postgres. Nothing
340        // here has a setting that reads one, and taking the first of several would be worse than
341        // saying so.
342        let [value] = values[..] else {
343            return self.unsupported(list);
344        };
345        let index = self.ast.settings.len() as u32;
346        self.ast.settings.push(Setting { name, scope, value });
347        Ok(Statement::Set(index))
348    }
349
350    /// `ResetStatement <- 'RESET' SetVariableOrSetting`.
351    fn reset_statement(&mut self, node: u32) -> Result<Statement> {
352        let (name, scope) = self.setting_name(self.find(node, "SetVariableOrSetting"))?;
353        let index = self.ast.settings.len() as u32;
354        self.ast.settings.push(Setting { name, scope, value: NONE });
355        Ok(Statement::Reset(index))
356    }
357
358    /// `SetVariableOrSetting <- SetVariable / SetSetting`, where the setting carries a scope word.
359    ///
360    /// `SET VARIABLE x = 1` is the other alternative and is a different feature: a variable is a
361    /// value the session holds and `getvariable` reads back, where a setting is a knob on the
362    /// engine. Refused rather than treated as a setting of that name.
363    fn setting_name(&mut self, node: u32) -> Result<(StrRef, Scope)> {
364        let inner = self.first(node);
365        if self.name(inner) != "SetSetting" {
366            return self.unsupported(inner);
367        }
368        let written = self.find(inner, "SettingScope");
369        let scope = if written == NONE {
370            Scope::Unwritten
371        } else {
372            match self.name(self.first(written)) {
373                "GlobalScope" => Scope::Global,
374                "SessionScope" => Scope::Session,
375                "LocalScope" => Scope::Local,
376                _ => return self.unsupported(written),
377            }
378        };
379        Ok((self.identifier(self.find(inner, "SettingName")), scope))
380    }
381
382    /// `CreateStatement <- 'CREATE' OrReplace? Temporary? CreateStatementVariation`.
383    ///
384    /// Of the nine variations, `CreateTableStmt` and `CreateViewStmt` are the ones that are done.
385    /// The other seven are a macro, a sequence, a type, a schema, an index, a secret and a trigger,
386    /// and each of them is a catalog entry this database has no room for yet.
387    fn create_statement(&mut self, node: u32) -> Result<Statement> {
388        let or_replace = self.find(node, "OrReplace") != NONE;
389        let temporary = self.find(node, "Temporary") != NONE;
390        let variation = self.find(node, "CreateStatementVariation");
391        let inner = self.first(variation);
392        // duckdb refuses this pair in the parser, with a caret under the `NOT`, because none of its
393        // create rules has room for both. The vendored grammar has room for both, so the refusal is
394        // here instead, which is the same stage and therefore the same sentence.
395        if or_replace && self.find(inner, "IfNotExists") != NONE {
396            return Err(Error::parser(
397                "Cannot specify both OR REPLACE and IF NOT EXISTS within single create statement",
398            ));
399        }
400        match self.name(inner) {
401            "CreateTableStmt" => self.create_table_statement(inner, or_replace, temporary),
402            "CreateViewStmt" => self.create_view_statement(inner, or_replace, temporary),
403            _ => self.unsupported(inner),
404        }
405    }
406
407    /// `CreateTableStmt <- 'TABLE' IfNotExists? QualifiedName CreateTableDefinition`.
408    fn create_table_statement(
409        &mut self,
410        inner: u32,
411        or_replace: bool,
412        temporary: bool,
413    ) -> Result<Statement> {
414        let name = self.name_parts(self.find(inner, "QualifiedName"));
415        let if_not_exists = self.find(inner, "IfNotExists") != NONE;
416        let definition = self.find(inner, "CreateTableDefinition");
417        let body = self.first(definition);
418        let (columns, query) = match self.name(body) {
419            "CreateColumnList" => (self.column_list(body)?, NONE),
420            "CreateTableAs" => self.create_table_as(body)?,
421            _ => return self.unsupported(body),
422        };
423        let index = self.ast.create_tables.len() as u32;
424        self.ast.create_tables.push(CreateTable {
425            name,
426            columns,
427            query,
428            if_not_exists,
429            or_replace,
430            temporary,
431        });
432        Ok(Statement::CreateTable(index))
433    }
434
435    /// `CreateViewStmt <- CreateSecure? CreateRecursive? 'VIEW' IfNotExists? QualifiedName
436    /// InsertColumnList? WithList? 'AS' SelectStatementInternal`.
437    ///
438    /// The body is transformed here as well as kept as text. Transforming it is what makes a view
439    /// whose body does not parse a parse error at creation, which is where it belongs, and the text
440    /// is what the catalog keeps so that the body can be bound again at every reference.
441    fn create_view_statement(
442        &mut self,
443        inner: u32,
444        or_replace: bool,
445        temporary: bool,
446    ) -> Result<Statement> {
447        for kid in self.kids(inner) {
448            // `SECURE` is a column and row policy, `RECURSIVE` is a different shape of view
449            // entirely, and `WITH` carries options. Dropping any of the three silently would make a
450            // view that is not the view that was asked for.
451            if matches!(self.name(kid), "CreateSecure" | "CreateRecursive" | "WithList") {
452                return self.unsupported(kid);
453            }
454        }
455        let name = self.name_parts(self.find(inner, "QualifiedName"));
456        let if_not_exists = self.find(inner, "IfNotExists") != NONE;
457        let list = self.find(inner, "InsertColumnList");
458        let columns = if list == NONE {
459            Slice::default()
460        } else {
461            let mut parts = Vec::new();
462            for kid in self.kids(self.find(list, "ColumnList")) {
463                parts.push(self.identifier(kid));
464            }
465            self.part_slice(parts)
466        };
467        let body = self.find(inner, "SelectStatementInternal");
468        let sql = self.text(body).to_string();
469        let sql = self.intern(&sql);
470        let query = self.query(body)?;
471        let index = self.ast.create_views.len() as u32;
472        self.ast.create_views.push(CreateView {
473            name,
474            columns,
475            query,
476            sql,
477            if_not_exists,
478            or_replace,
479            temporary,
480        });
481        Ok(Statement::CreateView(index))
482    }
483
484    /// `CreateColumnList <- Parens(CreateTableColumnList?) PartitionSortedOptions? WithList?`.
485    fn column_list(&mut self, node: u32) -> Result<Slice> {
486        for kid in self.kids(node) {
487            if matches!(self.name(kid), "PartitionOptions" | "SortedOptions" | "WithList") {
488                return self.unsupported(kid);
489            }
490        }
491        let list = self.find(node, "CreateTableColumnList");
492        if list == NONE {
493            // `CREATE TABLE t ()` parses. It is a table of no columns, and the catalog is entitled
494            // to refuse it, but that is not this layer's refusal to make.
495            return Ok(Slice::default());
496        }
497        let mut defs = Vec::new();
498        for element in self.kids(list) {
499            let inner = self.first(element);
500            if self.name(inner) != "CreateTableColumnDefinition" {
501                // A table level `PRIMARY KEY`, `UNIQUE`, `CHECK` or `FOREIGN KEY`. Constraints are
502                // not enforced anywhere yet and silently dropping one is a wrong answer waiting to
503                // happen, so it is refused instead.
504                return self.unsupported(inner);
505            }
506            defs.push(self.column_definition(self.first(inner))?);
507        }
508        Ok(self.column_def_slice(defs))
509    }
510
511    /// `ColumnDefinition <- DottedIdentifier Type? GeneratedColumn? ConstraintNameClause?
512    /// ColumnConstraint*`.
513    fn column_definition(&mut self, node: u32) -> Result<ColumnDef> {
514        let name = self.identifier(self.find(node, "DottedIdentifier"));
515        let type_node = self.find(node, "Type");
516        let ty = if type_node == NONE {
517            NONE
518        } else {
519            let text = self.text(type_node).to_string();
520            self.intern(&text)
521        };
522        if self.find(node, "GeneratedColumn") != NONE {
523            return self.unsupported(self.find(node, "GeneratedColumn"));
524        }
525        let mut not_null = false;
526        for kid in self.kids(node) {
527            if self.name(kid) != "ColumnConstraint" {
528                continue;
529            }
530            let constraint = self.first(kid);
531            match self.name(constraint) {
532                "NotNullConstraint" => {
533                    not_null = self.name(self.first(constraint)) == "NotNullColumnConstraint";
534                }
535                _ => return self.unsupported(constraint),
536            }
537        }
538        Ok(ColumnDef { name, ty, not_null })
539    }
540
541    /// `CreateTableAs <- IdentifierList? PartitionSortedOptions? WithList? 'AS' Statement
542    /// WithData?`.
543    ///
544    /// The names in the `IdentifierList` become column definitions with no type, because the types
545    /// are the query's and only the names are the syntax's to say.
546    fn create_table_as(&mut self, node: u32) -> Result<(Slice, QueryRef)> {
547        for kid in self.kids(node) {
548            if matches!(
549                self.name(kid),
550                "PartitionOptions" | "SortedOptions" | "WithList" | "WithData"
551            ) {
552                return self.unsupported(kid);
553            }
554        }
555        let names = self.find(node, "IdentifierList");
556        let columns = if names == NONE {
557            Slice::default()
558        } else {
559            let mut defs = Vec::new();
560            for kid in self.kids(names) {
561                let name = self.identifier(kid);
562                defs.push(ColumnDef { name, ty: NONE, not_null: false });
563            }
564            self.column_def_slice(defs)
565        };
566        let statement = self.find(node, "Statement");
567        let inner = self.first(statement);
568        if self.name(inner) != "SelectStatement" {
569            return self.unsupported(inner);
570        }
571        let query = self.query(self.first(inner))?;
572        Ok((columns, query))
573    }
574
575    /// `DropStatement <- 'DROP' DropEntries DropBehavior?`.
576    ///
577    /// `DropTable <- TableOrView IfExists? List(BaseTableName)`, and `TableOrView` covers `VIEW`
578    /// and `MATERIALIZED VIEW` as well as `TABLE`, so it is checked rather than assumed. The first
579    /// two are done and a materialized view is not a thing this database has.
580    fn drop_statement(&mut self, node: u32) -> Result<Statement> {
581        if self.find(node, "DropBehavior") != NONE {
582            return self.unsupported(self.find(node, "DropBehavior"));
583        }
584        let entries = self.find(node, "DropEntries");
585        let inner = self.first(entries);
586        if self.name(inner) != "DropTable" {
587            return self.unsupported(inner);
588        }
589        let kind = self.find(inner, "TableOrView");
590        let view = match self.name(self.first(kind)) {
591            "CommentTable" => false,
592            "CommentView" => true,
593            _ => return self.unsupported(kind),
594        };
595        let if_exists = self.find(inner, "IfExists") != NONE;
596        let mut names = Vec::new();
597        for kid in self.kids(inner) {
598            if self.name(kid) == "BaseTableName" {
599                names.push(self.name_parts(kid));
600            }
601        }
602        let names = self.name_list_slice(names);
603        let index = self.ast.drop_tables.len() as u32;
604        self.ast.drop_tables.push(DropTable { names, if_exists, view });
605        Ok(Statement::DropTable(index))
606    }
607
608    /// `InsertStatement <- ... InsertTarget InsertColumnList? InsertValues ...`.
609    ///
610    /// `ON CONFLICT`, `RETURNING`, `BY NAME`, `BY POSITION`, `OR REPLACE` and the rest of the
611    /// clauses the grammar hangs off this are each a refusal, because every one of them changes
612    /// what the statement means and none of them changes it in a way anything downstream would
613    /// notice if it were dropped.
614    fn insert_statement(&mut self, node: u32) -> Result<Statement> {
615        for kid in self.kids(node) {
616            if matches!(
617                self.name(kid),
618                "InsertTarget" | "InsertColumnList" | "InsertValues" | "WithClause"
619            ) {
620                continue;
621            }
622            return self.unsupported(kid);
623        }
624        if self.find(node, "WithClause") != NONE {
625            return self.unsupported(self.find(node, "WithClause"));
626        }
627        let name = self.name_parts(self.find(self.find(node, "InsertTarget"), "BaseTableName"));
628        let list = self.find(node, "InsertColumnList");
629        let columns = if list == NONE {
630            Slice::default()
631        } else {
632            let mut parts = Vec::new();
633            for kid in self.kids(self.find(list, "ColumnList")) {
634                parts.push(self.identifier(kid));
635            }
636            self.part_slice(parts)
637        };
638        let values = self.find(node, "InsertValues");
639        let inner = self.first(values);
640        if self.name(inner) != "SelectInsertValues" {
641            return self.unsupported(inner);
642        }
643        let source = self.query(self.find(inner, "SelectStatementInternal"))?;
644        let index = self.ast.inserts.len() as u32;
645        self.ast.inserts.push(Insert { name, columns, source });
646        Ok(Statement::Insert(index))
647    }
648
649    /// `SelectStatementInternal <- WithClause? SelectSetOpChain ResultModifiers?`.
650    fn query(&mut self, node: u32) -> Result<QueryRef> {
651        if self.find(node, "WithClause") != NONE {
652            return self.unsupported(self.find(node, "WithClause"));
653        }
654        let chain = self.find(node, "SelectSetOpChain");
655        if chain == NONE {
656            return self.unsupported(node);
657        }
658        let query = self.set_op_chain(chain)?;
659        let modifiers = self.find(node, "ResultModifiers");
660        if modifiers != NONE {
661            self.result_modifiers(query, modifiers)?;
662        }
663        Ok(query)
664    }
665
666    /// `SelectSetOpChain <- IntersectChain SelectSetOpChainTail*`, left associative.
667    fn set_op_chain(&mut self, node: u32) -> Result<QueryRef> {
668        let mut kids = self.kids(node);
669        let head = kids.next().unwrap_or(NONE);
670        let mut left = self.intersect_chain(head)?;
671        for tail in kids {
672            // `SelectSetOpChainTail <- SetopClause IntersectChain`.
673            let clause = self.first(tail);
674            let (op, quantifier, by_name) = self.setop_clause(clause)?;
675            let right = self.intersect_chain(self.nth(tail, 1))?;
676            left = self.push_query(Query::bare(QueryBody::SetOp {
677                op,
678                quantifier,
679                by_name,
680                left,
681                right,
682            }));
683        }
684        Ok(left)
685    }
686
687    /// `IntersectChain <- SelectAtom IntersectChainTail*`, which binds tighter than union.
688    fn intersect_chain(&mut self, node: u32) -> Result<QueryRef> {
689        let mut kids = self.kids(node);
690        let head = kids.next().unwrap_or(NONE);
691        let mut left = self.select_atom(head)?;
692        for tail in kids {
693            // `IntersectChainTail <- SetIntersectClause SelectAtom`.
694            let clause = self.first(tail);
695            let quantifier = self.quantifier(self.find(clause, "DistinctOrAll"));
696            let right = self.select_atom(self.nth(tail, 1))?;
697            left = self.push_query(Query::bare(QueryBody::SetOp {
698                op: SetOp::Intersect,
699                quantifier,
700                by_name: false,
701                left,
702                right,
703            }));
704        }
705        Ok(left)
706    }
707
708    /// `SetopClause <- SetopType DistinctOrAll? ByName?`.
709    fn setop_clause(&mut self, node: u32) -> Result<(SetOp, Quantifier, bool)> {
710        let kind = self.find(node, "SetopType");
711        let op = match self.name(self.first(kind)) {
712            "SetopUnion" => SetOp::Union,
713            "SetopExcept" => SetOp::Except,
714            _ => return self.unsupported(kind),
715        };
716        let quantifier = self.quantifier(self.find(node, "DistinctOrAll"));
717        Ok((op, quantifier, self.find(node, "ByName") != NONE))
718    }
719
720    /// `DistinctOrAll <- DistinctKeyword / AllKeyword`, absent included.
721    fn quantifier(&self, node: u32) -> Quantifier {
722        if node == NONE {
723            return Quantifier::Unstated;
724        }
725        match self.name(self.first(node)) {
726            "DistinctKeyword" => Quantifier::Distinct,
727            "AllKeyword" => Quantifier::All,
728            _ => Quantifier::Unstated,
729        }
730    }
731
732    /// `SelectAtom <- SelectParens / SelectStatementType`.
733    fn select_atom(&mut self, node: u32) -> Result<QueryRef> {
734        let inner = self.first(node);
735        match self.name(inner) {
736            // `SelectParens <- Parens(SelectStatementInternal)`, so the parens buy a query that
737            // carries its own order by and limit and nothing else.
738            "SelectParens" => self.query(self.first(inner)),
739            "SelectStatementType" => {
740                let kind = self.first(inner);
741                match self.name(kind) {
742                    "OptionalParensSimpleSelect" => {
743                        let select = self.simple_select(self.unwrap_parens(kind))?;
744                        Ok(self.push_query(Query::bare(QueryBody::Select(select))))
745                    }
746                    "ValuesClause" => {
747                        let rows = self.values_clause(kind)?;
748                        Ok(self.push_query(Query::bare(QueryBody::Values(rows))))
749                    }
750                    "DescribeStatement" => self.describe_statement(kind),
751                    _ => self.unsupported(kind),
752                }
753            }
754            _ => self.unsupported(inner),
755        }
756    }
757
758    /// `DescribeStatement <- ShowTables / ShowDeprecatedSelect / DescribeSelect / ShowAllTables /
759    /// ShowByName / DescribeByName`.
760    ///
761    /// Two of the six are done and they are the two that describe a relation. The four that are
762    /// not are `SHOW`, which is a different statement wearing this rule: three of its four forms
763    /// list what the database has rather than what a query returns, and the fourth is `SHOW <query>`,
764    /// which upstream documents as deprecated. Describing something through a deprecated spelling
765    /// is not worth implementing before the spelling that replaced it has users.
766    ///
767    /// `SUMMARIZE` shares `DescribeByName` and `DescribeSelect` with `DESCRIBE` and is refused
768    /// here, because it returns twelve columns of statistics rather than six of schema and reading
769    /// it as a describe would answer a different question than the one that was asked.
770    fn describe_statement(&mut self, node: u32) -> Result<QueryRef> {
771        let inner = self.first(node);
772        match self.name(inner) {
773            "DescribeSelect" => {
774                self.describe_and_not_summarize(inner)?;
775                let query = self.query(self.find(inner, "SelectStatementInternal"))?;
776                Ok(self.push_query(Query::bare(QueryBody::Describe(query))))
777            }
778            "DescribeByName" => {
779                self.describe_and_not_summarize(inner)?;
780                let target = self.find(inner, "DescribeTarget");
781                if target == NONE {
782                    return self.unsupported(inner);
783                }
784                let source = self.describe_target(target)?;
785                let query = self.star_over(source);
786                Ok(self.push_query(Query::bare(QueryBody::Describe(query))))
787            }
788            _ => self.unsupported(inner),
789        }
790    }
791
792    /// `DescribeOrSummarize <- DescribeRule / Summarize`, where only the first is done.
793    fn describe_and_not_summarize(&mut self, node: u32) -> Result<()> {
794        let word = self.find(node, "DescribeOrSummarize");
795        if word == NONE || self.name(self.first(word)) != "DescribeRule" {
796            return self.unsupported(if word == NONE { node } else { word });
797        }
798        Ok(())
799    }
800
801    /// `DescribeTarget <- DescribeBaseTableName / DescribeStringLiteral`, as a source to read from.
802    ///
803    /// Both become a `FROM` item and not a lookup of their own, because the string form is the
804    /// replacement scan and the binder already knows how to turn `'hits.parquet'` into a reader.
805    /// A name that is a table, a view, a file or nothing at all then gets one answer from one place.
806    fn describe_target(&mut self, node: u32) -> Result<SourceRef> {
807        let inner = self.first(node);
808        let name = match self.name(inner) {
809            "DescribeBaseTableName" => self.name_parts(self.find(inner, "BaseTableName")),
810            "DescribeStringLiteral" => {
811                let text = self.string_value(self.find(inner, "StringLiteral"))?;
812                let part = self.intern(&text);
813                self.part_slice(vec![part])
814            }
815            _ => return self.unsupported(inner),
816        };
817        Ok(self.push_source(Source::Table { name, alias: NONE, columns: Slice::default() }))
818    }
819
820    /// `SELECT * FROM <source>`, which is what `DESCRIBE t` means.
821    fn star_over(&mut self, source: SourceRef) -> QueryRef {
822        let star =
823            self.push(Expr::Star { qualifier: Slice::default(), replacements: Slice::default() });
824        let targets = self.target_slice(vec![Target { expr: star, alias: NONE }]);
825        let start = self.ast.source_lists.len() as u32;
826        self.ast.source_lists.push(source);
827        let from = Slice { start, len: 1 };
828        let select = self.push_select(Select { targets, from, ..Select::empty() });
829        self.push_query(Query::bare(QueryBody::Select(select)))
830    }
831
832    /// `ValuesClause <- 'VALUES' List(ValuesExpressions)`, each of which is `Parens(List(Expression))`.
833    ///
834    /// The rows are not checked against each other for width here. Two rows of different widths
835    /// parse, and saying so is the binder's job, because the message wants to name the column count
836    /// it expected and the parser does not know it for `INSERT` where the table decides.
837    fn values_clause(&mut self, node: u32) -> Result<Slice> {
838        let mut rows = Vec::new();
839        for kid in self.kids(node) {
840            if self.name(kid) != "ValuesExpressions" {
841                continue;
842            }
843            let mut items = Vec::new();
844            for expr in self.kids(kid) {
845                items.push(self.expr(expr)?);
846            }
847            let slice = self.expr_slice(items);
848            rows.push(slice);
849        }
850        let start = self.ast.rows.len() as u32;
851        self.ast.rows.extend(rows);
852        Ok(Slice { start, len: self.ast.rows.len() as u32 - start })
853    }
854
855    /// `OptionalParensSimpleSelect <- SimpleSelectParens / SimpleSelect`, down to the select.
856    fn unwrap_parens(&self, node: u32) -> u32 {
857        let mut node = self.first(node);
858        while self.name(node) == "SimpleSelectParens" {
859            node = self.first(node);
860        }
861        node
862    }
863
864    /// `ResultModifiers <- OrderByClause? LimitOffset?`.
865    fn result_modifiers(&mut self, query: QueryRef, node: u32) -> Result<()> {
866        let order = self.find(node, "OrderByClause");
867        if order != NONE {
868            let (items, all) = self.order_by(order)?;
869            let start = self.ast.order_items.len() as u32;
870            self.ast.order_items.extend(items);
871            self.ast.queries[query as usize].order_by =
872                Slice { start, len: self.ast.order_items.len() as u32 - start };
873            self.ast.queries[query as usize].order_by_all = all;
874        }
875        let limit = self.find(node, "LimitOffset");
876        if limit != NONE {
877            self.limit_offset(query, self.first(limit))?;
878        }
879        Ok(())
880    }
881
882    /// The four spellings of a limit and an offset, in either order and either one alone.
883    fn limit_offset(&mut self, query: QueryRef, node: u32) -> Result<()> {
884        match self.name(node) {
885            "LimitOffsetClause" | "OffsetLimitClause" => {
886                let limit = self.find(node, "LimitClause");
887                if limit != NONE {
888                    self.limit(query, limit)?;
889                }
890                let offset = self.find(node, "OffsetClause");
891                if offset != NONE {
892                    self.offset(query, offset)?;
893                }
894                Ok(())
895            }
896            _ => self.unsupported(node),
897        }
898    }
899
900    /// `LimitClause <- 'LIMIT' LimitValue`.
901    fn limit(&mut self, query: QueryRef, node: u32) -> Result<()> {
902        let value = self.first(node);
903        let inner = self.first(value);
904        match self.name(inner) {
905            // `LIMIT ALL` is no limit at all, which is what an absent limit already means.
906            "LimitAll" => Ok(()),
907            // `LimitExpression <- Expression '%'?`. The percent sign is a terminal so it leaves no
908            // node behind, and the only thing that says it was written is the text of the rule that
909            // matched it.
910            "LimitExpression" => {
911                let expr = self.expr(self.first(inner))?;
912                self.ast.queries[query as usize].limit = expr;
913                self.ast.queries[query as usize].limit_percent = self.text(inner).ends_with('%');
914                Ok(())
915            }
916            "LimitLiteralPercent" => {
917                let expr = self.expr(self.first(inner))?;
918                self.ast.queries[query as usize].limit = expr;
919                self.ast.queries[query as usize].limit_percent = true;
920                Ok(())
921            }
922            _ => self.unsupported(inner),
923        }
924    }
925
926    /// `OffsetClause <- 'OFFSET' OffsetValue`, where `OffsetValue <- Expression RowOrRows?`.
927    fn offset(&mut self, query: QueryRef, node: u32) -> Result<()> {
928        let value = self.first(node);
929        let expr = self.expr(self.first(value))?;
930        self.ast.queries[query as usize].offset = expr;
931        Ok(())
932    }
933
934    /// `SimpleSelect <- SelectFrom WhereClause? GroupByClause? HavingClause? WindowClause?
935    /// QualifyClause? SampleClause?`.
936    fn simple_select(&mut self, node: u32) -> Result<SelectRef> {
937        for name in ["WindowClause", "QualifyClause", "SampleClause"] {
938            let clause = self.find(node, name);
939            if clause != NONE {
940                return self.unsupported(clause);
941            }
942        }
943        let mut select = Select::empty();
944        self.select_from(&mut select, self.first(node))?;
945        let filter = self.find(node, "WhereClause");
946        if filter != NONE {
947            select.filter = self.expr(self.first(filter))?;
948        }
949        let group = self.find(node, "GroupByClause");
950        if group != NONE {
951            self.group_by(&mut select, self.first(group))?;
952        }
953        let having = self.find(node, "HavingClause");
954        if having != NONE {
955            select.having = self.expr(self.first(having))?;
956        }
957        Ok(self.push_select(select))
958    }
959
960    /// `SelectFrom <- SelectFromClause / FromSelectClause`, which is `SELECT ... FROM ...` and
961    /// DuckDB's `FROM ... SELECT ...` written the other way round.
962    fn select_from(&mut self, select: &mut Select, node: u32) -> Result<()> {
963        let clause = self.first(node);
964        let targets = self.find(clause, "SelectClause");
965        let from = self.find(clause, "FromClause");
966        if from != NONE {
967            select.from = self.sources(from)?;
968        }
969        if targets == NONE {
970            // `FROM t` on its own. DuckDB reads it as `SELECT * FROM t`, and inventing the star
971            // here rather than in the binder keeps the binder from having to know the shape of the
972            // clause that was missing.
973            let star = self
974                .push(Expr::Star { qualifier: Slice::default(), replacements: Slice::default() });
975            let start = self.ast.targets.len() as u32;
976            self.ast.targets.push(Target { expr: star, alias: NONE });
977            select.targets = Slice { start, len: 1 };
978            return Ok(());
979        }
980        self.select_clause(select, targets)
981    }
982
983    /// `SelectClause <- 'SELECT' DistinctClause? TargetList?`.
984    fn select_clause(&mut self, select: &mut Select, node: u32) -> Result<()> {
985        let distinct = self.find(node, "DistinctClause");
986        if distinct != NONE {
987            let inner = self.first(distinct);
988            select.distinct = match self.name(inner) {
989                // `SELECT ALL` is the default spelled out.
990                "DistinctAll" => Distinct::No,
991                "DistinctOn" => {
992                    let on = self.find(inner, "DistinctOnTargets");
993                    if on == NONE {
994                        Distinct::Yes
995                    } else {
996                        let mut items = Vec::new();
997                        for kid in self.kids(on) {
998                            items.push(self.expr(kid)?);
999                        }
1000                        Distinct::On(self.expr_slice(items))
1001                    }
1002                }
1003                _ => return self.unsupported(inner),
1004            };
1005        }
1006        let list = self.find(node, "TargetList");
1007        if list == NONE {
1008            return Ok(());
1009        }
1010        let mut targets = Vec::new();
1011        for kid in self.kids(list) {
1012            targets.push(self.target(kid)?);
1013        }
1014        select.targets = self.target_slice(targets);
1015        Ok(())
1016    }
1017
1018    /// `AliasedExpression <- ColIdExpression / ExpressionAsCollabel / ExpressionOptIdentifier`.
1019    fn target(&mut self, node: u32) -> Result<Target> {
1020        let inner = self.first(node);
1021        match self.name(inner) {
1022            // `ColIdExpression <- ColId ':' Expression`, the alias written first.
1023            "ColIdExpression" => {
1024                let alias = self.identifier(self.first(inner));
1025                let expr = self.expr(self.nth(inner, 1))?;
1026                Ok(Target { expr, alias })
1027            }
1028            "ExpressionAsCollabel" => {
1029                let expr = self.expr(self.first(inner))?;
1030                let alias = self.identifier(self.nth(inner, 1));
1031                Ok(Target { expr, alias })
1032            }
1033            "ExpressionOptIdentifier" => {
1034                let expr = self.expr(self.first(inner))?;
1035                let alias =
1036                    if self.count(inner) > 1 { self.identifier(self.nth(inner, 1)) } else { NONE };
1037                Ok(Target { expr, alias })
1038            }
1039            _ => self.unsupported(inner),
1040        }
1041    }
1042
1043    /// `GroupByClause <- 'GROUP' 'BY' GroupByExpressions`.
1044    fn group_by(&mut self, select: &mut Select, node: u32) -> Result<()> {
1045        let inner = self.first(node);
1046        match self.name(inner) {
1047            "GroupByAll" => {
1048                select.group_by_all = true;
1049                Ok(())
1050            }
1051            "GroupByList" => {
1052                let mut items = Vec::new();
1053                for kid in self.kids(inner) {
1054                    // `GroupByExpression <- EmptyGroupingItem / CubeOrRollupClause /
1055                    // GroupingSetsClause / GroupByBaseExpression`.
1056                    let expression = self.first(kid);
1057                    if self.name(expression) != "GroupByBaseExpression" {
1058                        return self.unsupported(expression);
1059                    }
1060                    items.push(self.expr(self.first(expression))?);
1061                }
1062                select.group_by = self.expr_slice(items);
1063                Ok(())
1064            }
1065            _ => self.unsupported(inner),
1066        }
1067    }
1068
1069    /// `OrderByClause <- 'ORDER' 'BY' OrderByExpressions`, where `OrderByExpressions <- OrderByAll
1070    /// / OrderByExpressionList`.
1071    fn order_by(&mut self, node: u32) -> Result<(Vec<OrderItem>, bool)> {
1072        let inner = self.first(self.first(node));
1073        match self.name(inner) {
1074            "OrderByAll" => {
1075                let (order, nulls) = self.sort_options(inner);
1076                Ok((vec![OrderItem { expr: NONE, order, nulls }], true))
1077            }
1078            "OrderByExpressionList" => {
1079                let mut items = Vec::new();
1080                for kid in self.kids(inner) {
1081                    // `OrderByExpression <- Expression DescOrAsc? NullsFirstOrLast?`.
1082                    let expr = self.expr(self.first(kid))?;
1083                    let (order, nulls) = self.sort_options(kid);
1084                    items.push(OrderItem { expr, order, nulls });
1085                }
1086                Ok((items, false))
1087            }
1088            _ => self.unsupported(inner),
1089        }
1090    }
1091
1092    /// The direction and the null placement of one sort key, either of which may be unwritten.
1093    fn sort_options(&self, node: u32) -> (Order, Nulls) {
1094        let direction = self.find(node, "DescOrAsc");
1095        let order = if direction == NONE {
1096            Order::Unstated
1097        } else if self.name(self.first(direction)) == "DescendingOrder" {
1098            Order::Descending
1099        } else {
1100            Order::Ascending
1101        };
1102        let placement = self.find(node, "NullsFirstOrLast");
1103        let nulls = if placement == NONE {
1104            Nulls::Unstated
1105        } else if self.name(self.first(placement)) == "NullsFirst" {
1106            Nulls::First
1107        } else {
1108            Nulls::Last
1109        };
1110        (order, nulls)
1111    }
1112
1113    // From clauses.
1114
1115    /// `FromClause <- 'FROM' List(TableRef)`.
1116    fn sources(&mut self, node: u32) -> Result<Slice> {
1117        let mut items = Vec::new();
1118        for kid in self.kids(node) {
1119            items.push(self.table_ref(kid)?);
1120        }
1121        let start = self.ast.source_lists.len() as u32;
1122        self.ast.source_lists.extend(items);
1123        Ok(Slice { start, len: self.ast.source_lists.len() as u32 - start })
1124    }
1125
1126    /// `TableRef <- InnerTableRef JoinOrPivot*`, left associative like the set operators.
1127    fn table_ref(&mut self, node: u32) -> Result<SourceRef> {
1128        let mut kids = self.kids(node);
1129        let head = kids.next().unwrap_or(NONE);
1130        let mut left = self.inner_table_ref(head)?;
1131        for tail in kids {
1132            let clause = self.first(tail);
1133            if self.name(clause) != "JoinClause" {
1134                return self.unsupported(clause);
1135            }
1136            left = self.join(left, self.first(clause))?;
1137        }
1138        Ok(left)
1139    }
1140
1141    /// `InnerTableRef <- ValuesRef / TableFunction / TableSubquery / BaseTableRef / ParensTableRef`.
1142    fn inner_table_ref(&mut self, node: u32) -> Result<SourceRef> {
1143        let inner = if self.name(node) == "InnerTableRef" { self.first(node) } else { node };
1144        match self.name(inner) {
1145            "BaseTableRef" => {
1146                if self.find(inner, "TableAliasColon") != NONE {
1147                    return self.unsupported(inner);
1148                }
1149                for name in ["AtClause", "SampleClause"] {
1150                    let clause = self.find(inner, name);
1151                    if clause != NONE {
1152                        return self.unsupported(clause);
1153                    }
1154                }
1155                let name = self.name_parts(self.find(inner, "BaseTableName"));
1156                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
1157                Ok(self.push_source(Source::Table { name, alias, columns }))
1158            }
1159            "TableSubquery" => {
1160                if self.find(inner, "TableAliasColon") != NONE
1161                    || self.find(inner, "Lateral") != NONE
1162                {
1163                    return self.unsupported(inner);
1164                }
1165                // `SubqueryReference <- Parens(SelectStatementInternal)`.
1166                let reference = self.find(inner, "SubqueryReference");
1167                let query = self.query(self.first(reference))?;
1168                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
1169                Ok(self.push_source(Source::Subquery { query, alias, columns }))
1170            }
1171            // `TableFunction <- TableFunctionLateralOpt / TableFunctionAliasColon`, and
1172            // `TableFunctionLateralOpt <- Lateral? QualifiedTableFunction TableFunctionArguments
1173            // WithOrdinality? TableAlias?`. The colon form and `LATERAL` are their own work, and
1174            // `WITH ORDINALITY` adds a column, so all three are turned away rather than dropped.
1175            "TableFunction" => {
1176                let form = self.first(inner);
1177                for name in ["TableAliasColon", "Lateral", "WithOrdinality", "SampleClause"] {
1178                    let clause = self.find(form, name);
1179                    if clause != NONE {
1180                        return self.unsupported(clause);
1181                    }
1182                }
1183                let name = self.name_parts(self.find(form, "QualifiedTableFunction"));
1184                let mut args = Vec::new();
1185                // `TableFunctionArguments <- Parens(List(FunctionArgument)?)`, so a call with no
1186                // arguments has the wrapper and no list under it.
1187                let list = self.find(form, "TableFunctionArguments");
1188                for kid in self.kids(list) {
1189                    args.push(self.table_argument(kid)?);
1190                }
1191                let args = self.target_slice(args);
1192                let (alias, columns) = self.table_alias(self.find(form, "TableAlias"));
1193                Ok(self.push_source(Source::Function { name, args, alias, columns }))
1194            }
1195            "ValuesRef" => {
1196                if self.find(inner, "TableAliasColon") != NONE {
1197                    return self.unsupported(inner);
1198                }
1199                let rows = self.values_clause(self.find(inner, "ValuesClause"))?;
1200                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
1201                Ok(self.push_source(Source::Values { rows, alias, columns }))
1202            }
1203            "ParensTableRef" => {
1204                if self.find(inner, "TableAliasColon") != NONE
1205                    || self.find(inner, "SampleClause") != NONE
1206                    || self.find(inner, "TableAlias") != NONE
1207                {
1208                    return self.unsupported(inner);
1209                }
1210                self.table_ref(self.find(inner, "TableRef"))
1211            }
1212            _ => self.unsupported(inner),
1213        }
1214    }
1215
1216    /// `TableAlias <- TableAliasAs / TableAliasWithoutAs`, either with a column alias list.
1217    fn table_alias(&mut self, node: u32) -> (StrRef, Slice) {
1218        if node == NONE {
1219            return (NONE, Slice::default());
1220        }
1221        let inner = self.first(node);
1222        let alias = self.identifier(self.first(inner));
1223        let list = self.find(inner, "ColumnAliases");
1224        if list == NONE {
1225            return (alias, Slice::default());
1226        }
1227        let mut columns = Vec::new();
1228        for kid in self.kids(list) {
1229            let name = self.identifier(kid);
1230            columns.push(name);
1231        }
1232        (alias, self.part_slice(columns))
1233    }
1234
1235    /// `JoinClause <- JoinByClause / RegularJoinClause / JoinWithoutOnClause / NearestJoinClause`.
1236    fn join(&mut self, left: SourceRef, node: u32) -> Result<SourceRef> {
1237        match self.name(node) {
1238            // `RegularJoinClause <- Asof? JoinType? 'JOIN' TableRef JoinQualifier`.
1239            "RegularJoinClause" => {
1240                if self.find(node, "Asof") != NONE {
1241                    return self.unsupported(node);
1242                }
1243                let kind = self.join_type(self.find(node, "JoinType"));
1244                let right = self.table_ref(self.find(node, "TableRef"))?;
1245                let (on, using) = self.join_qualifier(self.find(node, "JoinQualifier"))?;
1246                Ok(self.push_source(Source::Join { left, right, kind, natural: false, on, using }))
1247            }
1248            // `JoinWithoutOnClause <- JoinPrefix 'JOIN' InnerTableRef`, which is cross, natural and
1249            // positional. Those three are exactly the joins that carry no condition.
1250            "JoinWithoutOnClause" => {
1251                let prefix = self.first(self.find(node, "JoinPrefix"));
1252                let (kind, natural) = match self.name(prefix) {
1253                    "CrossJoinPrefix" => (JoinKind::Cross, false),
1254                    "PositionalJoinPrefix" => (JoinKind::Positional, false),
1255                    "NaturalJoinPrefix" => (self.join_type(self.find(prefix, "JoinType")), true),
1256                    _ => return self.unsupported(prefix),
1257                };
1258                let right = self.inner_table_ref(self.find(node, "InnerTableRef"))?;
1259                Ok(self.push_source(Source::Join {
1260                    left,
1261                    right,
1262                    kind,
1263                    natural,
1264                    on: NONE,
1265                    using: Slice::default(),
1266                }))
1267            }
1268            _ => self.unsupported(node),
1269        }
1270    }
1271
1272    /// `JoinType <- FullJoin / LeftJoin / RightJoin / SemiJoin / AntiJoin / InnerJoin`, absent
1273    /// meaning inner, which is what SQL has always meant by a bare `JOIN`.
1274    fn join_type(&self, node: u32) -> JoinKind {
1275        if node == NONE {
1276            return JoinKind::Inner;
1277        }
1278        match self.name(self.first(node)) {
1279            "FullJoin" => JoinKind::Full,
1280            "LeftJoin" => JoinKind::Left,
1281            "RightJoin" => JoinKind::Right,
1282            "SemiJoin" => JoinKind::Semi,
1283            "AntiJoin" => JoinKind::Anti,
1284            _ => JoinKind::Inner,
1285        }
1286    }
1287
1288    /// `JoinQualifier <- OnClause / UsingClause`.
1289    fn join_qualifier(&mut self, node: u32) -> Result<(ExprRef, Slice)> {
1290        let inner = self.first(node);
1291        match self.name(inner) {
1292            "OnClause" => Ok((self.expr(self.first(inner))?, Slice::default())),
1293            "UsingClause" => {
1294                let mut columns = Vec::new();
1295                for kid in self.kids(inner) {
1296                    let name = self.identifier(kid);
1297                    columns.push(name);
1298                }
1299                Ok((NONE, self.part_slice(columns)))
1300            }
1301            _ => self.unsupported(inner),
1302        }
1303    }
1304
1305    // Expressions.
1306
1307    /// One expression, from wherever in the precedence chain it starts.
1308    ///
1309    /// The loop is the whole design. A rule that says something gets an arm, a rule with exactly
1310    /// one child that said nothing is stepped through, and anything else is an error naming itself.
1311    /// The chain rules never get an arm for their one child case, which is why adding a precedence
1312    /// level upstream costs nothing here.
1313    ///
1314    /// Said nothing means covered no text of its own. A keyword is not a child of the node that
1315    /// spells it, so `TRIM(x)` is a rule with one child and that child is `x`, and stepping through
1316    /// on the child count alone threw the `TRIM` away and answered the untrimmed string. Comparing
1317    /// the two spans is what tells the two cases apart: a precedence rule with one child covers
1318    /// exactly what its child covers, and a rule that wrote a keyword or a bracket covers more.
1319    /// That is the rule rather than a list of the names it happened to be wrong about, because the
1320    /// grammar has eleven hundred rules and the ones with a keyword and one child are not enumerable
1321    /// by reading the ones that are wrong today.
1322    fn expr(&mut self, node: u32) -> Result<ExprRef> {
1323        let mut node = node;
1324        loop {
1325            let count = self.count(node);
1326            let name = self.name(node);
1327            match name {
1328                "LogicalOrExpression" if count > 1 => return self.logical(node, BinaryOp::Or),
1329                "LogicalAndExpression" if count > 1 => return self.logical(node, BinaryOp::And),
1330                "LogicalNotExpression" if count > 1 => return self.logical_not(node),
1331                "IsExpression" if count > 1 => return self.is_expression(node),
1332                "BetweenInLikeExpression" if count > 1 => return self.between_in_like(node),
1333                "PrefixExpression" if count > 1 => return self.prefix(node),
1334                "BaseExpression" if count > 1 => return self.indirection(node),
1335                "LambdaArrowExpression"
1336                | "IsDistinctFromExpression"
1337                | "ComparisonExpression"
1338                | "OtherOperatorExpression"
1339                | "BitwiseExpression"
1340                | "AdditiveExpression"
1341                | "MultiplicativeExpression"
1342                | "ExponentiationExpression"
1343                | "CollateExpression"
1344                | "AtTimeZoneExpression"
1345                    if count > 1 =>
1346                {
1347                    return self.tail_chain(node);
1348                }
1349                "ColumnReference" => {
1350                    let name = self.name_parts(node);
1351                    return Ok(self.push(Expr::Column { name }));
1352                }
1353                "StarExpression" => return self.star(node),
1354                "NumberLiteral" => {
1355                    let text = self.text(node).to_string();
1356                    let text = self.intern(&text);
1357                    return Ok(self.push(Expr::Literal { kind: LiteralKind::Number, text }));
1358                }
1359                "StringLiteral" => return self.string_literal(node),
1360                "NullLiteral" | "TrueLiteral" | "FalseLiteral" => {
1361                    let kind = match name {
1362                        "NullLiteral" => LiteralKind::Null,
1363                        "TrueLiteral" => LiteralKind::True,
1364                        _ => LiteralKind::False,
1365                    };
1366                    return Ok(self.push(Expr::Literal { kind, text: NONE }));
1367                }
1368                "FunctionExpression" => return self.function(node),
1369                "CoalesceExpression" => return self.coalesce(node),
1370                "NullIfExpression" => return self.null_if(node),
1371                "SubstringExpression" => return self.substring(node),
1372                "PositionExpression" => return self.position(node),
1373                "TrimExpression" => return self.trim(node),
1374                "OverlayExpression" => return self.overlay(node),
1375                "ExtractExpression" => return self.extract(node),
1376                "CastExpression" => return self.cast(node),
1377                "TypeLiteral" => return self.typed_literal(node),
1378                "IntervalLiteral" => return self.interval_literal(node),
1379                "CaseExpression" => return self.case(node),
1380                "ParenthesisExpression" => return self.row(node),
1381                // `ParensExpression <- Parens(Expression)` covers more text than its child and
1382                // still says nothing about the value, because the brackets are grouping. It is the
1383                // one rule of that shape, which is why it is an arm rather than a second rule in
1384                // the step below. `ParenthesisExpression` is not this: it holds a list, and a list
1385                // of more than one is a row.
1386                "ParensExpression" if count == 1 => node = self.first(node),
1387                "BoundedListExpression" => return self.list(node),
1388                "QuestionMarkNumberedParameter"
1389                | "AnonymousParameter"
1390                | "NumberedParameter"
1391                | "ColLabelParameter" => return self.parameter(node),
1392                "SubqueryExpression" => return self.subquery(node),
1393                _ if count == 1 && self.text(self.first(node)) == self.text(node) => {
1394                    node = self.first(node);
1395                }
1396                _ => return self.unsupported(node),
1397            }
1398        }
1399    }
1400
1401    /// `X <- Y XTail*` where `XTail <- Operator Y`, the shape ten precedence levels share.
1402    fn tail_chain(&mut self, node: u32) -> Result<ExprRef> {
1403        let mut kids = self.kids(node);
1404        let head = kids.next().unwrap_or(NONE);
1405        let mut left = self.expr(head)?;
1406        for tail in kids {
1407            let operator = self.first(tail);
1408            let op = self.binary_op(operator)?;
1409            // `ComparisonExpressionTail <- ComparisonOperator NotExpression? BetweenInLikeExpression`
1410            // is the one tail with an optional middle, so the operand is the last child and not the
1411            // second one. Taking the last is right for every tail and wrong for none.
1412            let operand = self.kids(tail).last().unwrap_or(NONE);
1413            if self.count(tail) > 2 {
1414                return self.unsupported(tail);
1415            }
1416            let right = self.expr(operand)?;
1417            left = self.push(Expr::Binary { op, left, right });
1418        }
1419        Ok(left)
1420    }
1421
1422    /// Which infix operator a tail's operator node is.
1423    fn binary_op(&mut self, node: u32) -> Result<BinaryOp> {
1424        // The operator rules nest: `ComparisonOperator` over `OperatorGreaterThan` over the symbol
1425        // itself. Every one of them covers the same tokens, so the text is the same at every level
1426        // and reading it once at the top is enough. The name is not, which is why the bottom of the
1427        // chain is walked to as well: `OtherOperator` says nothing and `OperatorLiteral` says
1428        // everything, and they are three levels apart.
1429        let mut leaf = node;
1430        while self.count(leaf) == 1 {
1431            leaf = self.first(leaf);
1432        }
1433        let text = self.text(node);
1434        let upper = text.to_ascii_uppercase();
1435        let op = match upper.as_str() {
1436            "OR" => BinaryOp::Or,
1437            "AND" => BinaryOp::And,
1438            "=" | "==" => BinaryOp::Eq,
1439            "!=" | "<>" => BinaryOp::NotEq,
1440            "<" => BinaryOp::Lt,
1441            ">" => BinaryOp::Gt,
1442            "<=" => BinaryOp::LtEq,
1443            ">=" => BinaryOp::GtEq,
1444            "+" => BinaryOp::Add,
1445            "-" => BinaryOp::Subtract,
1446            "*" => BinaryOp::Multiply,
1447            "/" => BinaryOp::Divide,
1448            "//" => BinaryOp::IntegerDivide,
1449            "%" => BinaryOp::Modulo,
1450            "^" | "**" => BinaryOp::Power,
1451            "&" => BinaryOp::BitAnd,
1452            "|" => BinaryOp::BitOr,
1453            "<<" => BinaryOp::ShiftLeft,
1454            ">>" => BinaryOp::ShiftRight,
1455            "||" => BinaryOp::Concat,
1456            "COLLATE" => BinaryOp::Collate,
1457            "->" => BinaryOp::Arrow,
1458            "->>" => BinaryOp::LongArrow,
1459            "@>" => BinaryOp::Contains,
1460            "<@" => BinaryOp::ContainedBy,
1461            "&&" => BinaryOp::Overlaps,
1462            "^@" => BinaryOp::StartsWith,
1463            "<<=" => BinaryOp::InetContainedByOrEq,
1464            ">>=" => BinaryOp::InetContainsOrEq,
1465            _ if self.name(leaf) == "AtTimeZoneOperator" => BinaryOp::AtTimeZone,
1466            // `IsDistinctFromOp <- 'IS' 'NOT'? 'DISTINCT' 'FROM'`, told apart by the middle word,
1467            // which is not in the tree because keywords are terminals.
1468            _ if self.name(leaf) == "IsDistinctFromOp" => {
1469                if upper.split_whitespace().any(|word| word == "NOT") {
1470                    BinaryOp::IsNotDistinctFrom
1471                } else {
1472                    BinaryOp::IsDistinctFrom
1473                }
1474            }
1475            // `OperatorLiteral` is the open end of the operator set. Its body in the grammar text
1476            // says `Identifier`, but it is one of the 24 rules whose body the matcher does not
1477            // walk and the matcher it is overridden to is the bare operator one, so what it
1478            // actually accepts is any run of operator characters that is not already a token.
1479            // `a <=> b` is such a run, DuckDB resolves it as a two argument function of that name,
1480            // and rejecting it here would reject SQL DuckDB accepts.
1481            _ if self.name(leaf) == "OperatorLiteral" => {
1482                let interned = self.intern(text);
1483                BinaryOp::Named(interned)
1484            }
1485            _ => return self.unsupported(node),
1486        };
1487        Ok(op)
1488    }
1489
1490    /// `LogicalOrExpression <- LogicalAndExpression LogicalOrExpressionTail*`, and the `AND` twin.
1491    ///
1492    /// Separate from the other tails because the tail here is `'OR' LogicalAndExpression` with the
1493    /// keyword as a terminal, so there is no operator node to read and the operator is the rule.
1494    fn logical(&mut self, node: u32, op: BinaryOp) -> Result<ExprRef> {
1495        let mut kids = self.kids(node);
1496        let head = kids.next().unwrap_or(NONE);
1497        let mut left = self.expr(head)?;
1498        for tail in kids {
1499            let right = self.expr(self.first(tail))?;
1500            left = self.push(Expr::Binary { op, left, right });
1501        }
1502        Ok(left)
1503    }
1504
1505    /// `LogicalNotExpression <- NotExpression? IsExpression`, where `NotExpression <- NotKeyword+`.
1506    ///
1507    /// The plus matters. `NOT NOT x` is two nodes in the parse tree and two negations in the AST,
1508    /// and folding them here would be an optimizer decision taken in the parser.
1509    fn logical_not(&mut self, node: u32) -> Result<ExprRef> {
1510        let negations = self.count(self.first(node));
1511        let mut expr = self.expr(self.nth(node, 1))?;
1512        for _ in 0..negations {
1513            expr = self.push(Expr::Unary { op: UnaryOp::Not, operand: expr });
1514        }
1515        Ok(expr)
1516    }
1517
1518    /// `IsExpression <- IsDistinctFromExpression IsTest*`, the postfix null and boolean tests.
1519    fn is_expression(&mut self, node: u32) -> Result<ExprRef> {
1520        let mut kids = self.kids(node);
1521        let head = kids.next().unwrap_or(NONE);
1522        let mut expr = self.expr(head)?;
1523        for test in kids {
1524            let inner = self.first(test);
1525            let negated = self.text(inner).to_ascii_uppercase().contains("NOT");
1526            let op = match self.name(inner) {
1527                "NotNull" => UnaryOp::IsNotNull,
1528                "IsNull" => UnaryOp::IsNull,
1529                // `IsLiteral <- 'IS' 'NOT'? IsLiteralValue`, and the value rule is one more level
1530                // down again because it is a choice of four and not four alternatives inlined.
1531                "IsLiteral" => match self.name(self.first(self.first(inner))) {
1532                    "NullLiteral" if negated => UnaryOp::IsNotNull,
1533                    "NullLiteral" => UnaryOp::IsNull,
1534                    "TrueLiteral" if negated => UnaryOp::IsNotTrue,
1535                    "TrueLiteral" => UnaryOp::IsTrue,
1536                    "FalseLiteral" if negated => UnaryOp::IsNotFalse,
1537                    "FalseLiteral" => UnaryOp::IsFalse,
1538                    "UnknownLiteral" if negated => UnaryOp::IsNotUnknown,
1539                    "UnknownLiteral" => UnaryOp::IsUnknown,
1540                    _ => return self.unsupported(inner),
1541                },
1542                _ => return self.unsupported(inner),
1543            };
1544            expr = self.push(Expr::Unary { op, operand: expr });
1545        }
1546        Ok(expr)
1547    }
1548
1549    /// `BetweenInLikeExpression <- OtherOperatorExpression BetweenInLikeOp?`.
1550    fn between_in_like(&mut self, node: u32) -> Result<ExprRef> {
1551        let operand = self.expr(self.first(node))?;
1552        // `BetweenInLikeOp <- 'NOT'? BetweenInLikeOpExpression`. The `NOT` is a terminal, so what
1553        // says it was written is that the op node covers a token the inner node does not.
1554        let op = self.nth(node, 1);
1555        let negated = self.text(op).to_ascii_uppercase().starts_with("NOT");
1556        let inner = self.first(self.first(op));
1557        match self.name(inner) {
1558            // `BetweenClause <- 'BETWEEN' x 'AND' y`.
1559            "BetweenClause" => {
1560                let low = self.expr(self.first(inner))?;
1561                let high = self.expr(self.nth(inner, 1))?;
1562                Ok(self.push(Expr::Between { operand, low, high, negated }))
1563            }
1564            // `InClause <- 'IN' InExpression`.
1565            "InClause" => {
1566                let expression = self.first(self.first(inner));
1567                match self.name(expression) {
1568                    "InExpressionList" => {
1569                        let mut items = Vec::new();
1570                        for kid in self.kids(expression) {
1571                            items.push(self.expr(kid)?);
1572                        }
1573                        let list = self.expr_slice(items);
1574                        Ok(self.push(Expr::In { operand, list, negated }))
1575                    }
1576                    _ => self.unsupported(expression),
1577                }
1578            }
1579            // `LikeClause <- LikeVariations x EscapeClause?`.
1580            "LikeClause" => {
1581                if self.find(inner, "EscapeClause") != NONE {
1582                    return self.unsupported(inner);
1583                }
1584                let variation = self.name(self.first(self.first(inner)));
1585                let op = match (variation, negated) {
1586                    ("LikeToken", false) | ("NotLikeOp", true) => BinaryOp::Like,
1587                    ("LikeToken", true) | ("NotLikeOp", false) => BinaryOp::NotLike,
1588                    ("ILikeToken", false) | ("NotILikeOp", true) => BinaryOp::ILike,
1589                    ("ILikeToken", true) | ("NotILikeOp", false) => BinaryOp::NotILike,
1590                    // Glob and the bare regex match have no negated spelling of their own in
1591                    // `LikeVariations`, so a `NOT` in front of either stays an explicit negation.
1592                    ("GlobToken", _) => BinaryOp::Glob,
1593                    ("RegexMatchToken", _) => BinaryOp::Regex,
1594                    ("SimilarToToken", false) | ("NotSimilarToOp", true) => BinaryOp::SimilarTo,
1595                    ("SimilarToToken", true) | ("NotSimilarToOp", false) => BinaryOp::NotSimilarTo,
1596                    ("RegexInsensitiveMatchToken", false)
1597                    | ("NotRegexInsensitiveMatchOp", true) => BinaryOp::RegexInsensitive,
1598                    ("RegexInsensitiveMatchToken", true)
1599                    | ("NotRegexInsensitiveMatchOp", false) => BinaryOp::NotRegexInsensitive,
1600                    _ => return self.unsupported(inner),
1601                };
1602                let right = self.expr(self.nth(inner, 1))?;
1603                let expr = self.push(Expr::Binary { op, left: operand, right });
1604                // The like family folds its negation into the operator because it has a spelling
1605                // for the negated form. Glob and regex do not, so theirs stays where it was.
1606                if negated && matches!(op, BinaryOp::Glob | BinaryOp::Regex) {
1607                    return Ok(self.push(Expr::Unary { op: UnaryOp::Not, operand: expr }));
1608                }
1609                Ok(expr)
1610            }
1611            _ => self.unsupported(inner),
1612        }
1613    }
1614
1615    /// `PrefixExpression <- PrefixOperator* BaseExpression`, applied right to left.
1616    fn prefix(&mut self, node: u32) -> Result<ExprRef> {
1617        let kids: Vec<u32> = self.kids(node).collect();
1618        let mut expr = self.expr(kids[kids.len() - 1])?;
1619        for &operator in kids[..kids.len() - 1].iter().rev() {
1620            let op = match self.name(self.first(operator)) {
1621                "MinusPrefixOperator" => UnaryOp::Negate,
1622                "PlusPrefixOperator" => UnaryOp::Plus,
1623                "TildePrefixOperator" => UnaryOp::BitNot,
1624                _ => return self.unsupported(operator),
1625            };
1626            expr = self.push(Expr::Unary { op, operand: expr });
1627        }
1628        Ok(expr)
1629    }
1630
1631    /// `BaseExpression <- SingleExpression IndirectionList?`, the postfix chain.
1632    fn indirection(&mut self, node: u32) -> Result<ExprRef> {
1633        let mut expr = self.expr(self.first(node))?;
1634        for step in self.kids(self.nth(node, 1)) {
1635            let inner = self.first(step);
1636            expr = match self.name(inner) {
1637                // `CastOperator <- '::' Type`.
1638                "CastOperator" => {
1639                    let text = self.text(self.first(inner)).to_string();
1640                    let ty = self.intern(&text);
1641                    self.push(Expr::Cast { operand: expr, ty, try_cast: false })
1642                }
1643                "DotOperator" => {
1644                    let dot = self.first(inner);
1645                    match self.name(dot) {
1646                        // `DotColumnOperator <- '.' ColLabel`, which DuckDB resolves as a call of
1647                        // `struct_extract`. Writing it as that call rather than as its own node
1648                        // keeps the binder from needing a rule for a thing that is already a
1649                        // function.
1650                        "DotColumnOperator" => {
1651                            let field = self.identifier(self.first(dot));
1652                            let text = self.ast.string(field).to_string();
1653                            let literal = self.intern(&text);
1654                            let key = self
1655                                .push(Expr::Literal { kind: LiteralKind::String, text: literal });
1656                            let name = self.function_name("struct_extract");
1657                            let args = self.expr_slice(vec![expr, key]);
1658                            self.push(Expr::Function { name, args, distinct: false })
1659                        }
1660                        // `DotMethodOperator <- '.' MethodExpression`, where `x.f(a)` is `f(x, a)`.
1661                        "DotMethodOperator" => {
1662                            let method = self.first(dot);
1663                            let text = self.text(self.first(method)).to_string();
1664                            let text = unquote(&text);
1665                            let name = self.function_name(&text);
1666                            let mut args = vec![expr];
1667                            let list = self.find(method, "MethodExpressionArguments");
1668                            if list != NONE {
1669                                let inner = self.first(list);
1670                                let arguments = self.find(inner, "MethodFunctionArguments");
1671                                if arguments != NONE {
1672                                    for kid in self.kids(arguments) {
1673                                        args.push(self.argument(kid)?);
1674                                    }
1675                                }
1676                            }
1677                            let args = self.expr_slice(args);
1678                            self.push(Expr::Function { name, args, distinct: false })
1679                        }
1680                        _ => return self.unsupported(dot),
1681                    }
1682                }
1683                // `SliceExpression <- '[' SliceBound ']'` over
1684                // `SliceBound <- Expression? EndSliceBound? StepSliceBound?`, so a subscript is one
1685                // index when neither colon is there and a range when either of them is. Both become
1686                // a call, the same two calls DuckDB's own transformer writes.
1687                "SliceExpression" => self.subscript(inner, expr)?,
1688                // `PostfixOperator <- '!'`.
1689                "PostfixOperator" => {
1690                    self.push(Expr::Unary { op: UnaryOp::Factorial, operand: expr })
1691                }
1692                _ => return self.unsupported(inner),
1693            };
1694        }
1695        Ok(expr)
1696    }
1697
1698    /// `SliceExpression <- '[' SliceBound ']'`, which is `array_extract` or `array_slice`.
1699    ///
1700    /// The three parts of the bound are all optional and any of the eight combinations parses, so
1701    /// which call this is comes from which parts are there rather than from how many children the
1702    /// bound has. One expression and no colon is an index. Anything with a colon in it is a range,
1703    /// and a range the query did not write both ends of gets the ends DuckDB's transformer gives it:
1704    /// a missing begin is 1 and a missing end is -1, which is the last element, so `x[:]` is the
1705    /// whole of `x` and `array_slice(x, 1, -1)` answers the same thing.
1706    ///
1707    /// `EndSliceMinus` is the `-` in `x[1:-]`, which upstream reads as a range with no end rather
1708    /// than as a subtraction of nothing, and it answers `x[1:]`. So it is the missing end too.
1709    ///
1710    /// The step is the odd one. `x[1:2:]` is a step that is written and empty, and what upstream
1711    /// does with it is pass a list where the step goes, which then fails to bind because the fourth
1712    /// parameter is a BIGINT. The empty list here is that, measured off the pinned binary: it says
1713    /// `array_slice(INTEGER[], INTEGER_LITERAL, INTEGER_LITERAL, INTEGER[])` has no match, and the
1714    /// fourth type in that sentence is the list. Writing a 1 there instead would answer a row where
1715    /// the reference refuses.
1716    fn subscript(&mut self, node: u32, target: ExprRef) -> Result<ExprRef> {
1717        let bound = self.first(node);
1718        let (mut begin, mut end, mut step) = (NONE, NONE, NONE);
1719        for kid in self.kids(bound) {
1720            match self.name(kid) {
1721                "EndSliceBound" => end = kid,
1722                "StepSliceBound" => step = kid,
1723                _ => begin = kid,
1724            }
1725        }
1726        if end == NONE && step == NONE {
1727            if begin == NONE {
1728                return Err(Error::parser("Empty subscript '[]' is not allowed"));
1729            }
1730            let index = self.expr(begin)?;
1731            let name = self.function_name("array_extract");
1732            let args = self.expr_slice(vec![target, index]);
1733            return Ok(self.push(Expr::Function { name, args, distinct: false }));
1734        }
1735        let first = if begin == NONE { self.literal_number("1") } else { self.expr(begin)? };
1736        // `EndSliceBound <- ':' EndSliceValue?` and `EndSliceValue <- Expression / EndSliceMinus`,
1737        // so the end is written only when the value is there and is not the lone hyphen.
1738        let value = if end == NONE { NONE } else { self.find(end, "EndSliceValue") };
1739        let written = if value == NONE { NONE } else { self.first(value) };
1740        let last = if written == NONE || self.name(written) == "EndSliceMinus" {
1741            self.literal_number("-1")
1742        } else {
1743            self.expr(written)?
1744        };
1745        let mut args = vec![target, first, last];
1746        if step != NONE {
1747            let by = self.first(step);
1748            args.push(if by == NONE {
1749                self.push(Expr::List { items: Slice::default() })
1750            } else {
1751                self.expr(by)?
1752            });
1753        }
1754        let name = self.function_name("array_slice");
1755        let args = self.expr_slice(args);
1756        Ok(self.push(Expr::Function { name, args, distinct: false }))
1757    }
1758
1759    /// A number literal the transformer writes rather than reads, for a bound a range left out.
1760    fn literal_number(&mut self, digits: &str) -> ExprRef {
1761        let text = self.intern(digits);
1762        self.push(Expr::Literal { kind: LiteralKind::Number, text })
1763    }
1764
1765    /// A one part function name, for the calls the transformer invents rather than reads.
1766    fn function_name(&mut self, name: &str) -> Slice {
1767        let interned = self.intern(name);
1768        self.part_slice(vec![interned])
1769    }
1770
1771    /// `StarExpression <- StarQualifierList? '*' ExcludeList? ReplaceList? RenameList?`.
1772    fn star(&mut self, node: u32) -> Result<ExprRef> {
1773        for name in ["ExcludeList", "RenameList"] {
1774            let list = self.find(node, name);
1775            if list != NONE {
1776                return self.unsupported(list);
1777            }
1778        }
1779        let replace = self.find(node, "ReplaceList");
1780        let replacements =
1781            if replace == NONE { Slice::default() } else { self.replacements(replace)? };
1782        let qualifier = self.find(node, "StarQualifierList");
1783        let qualifier =
1784            if qualifier == NONE { Slice::default() } else { self.name_parts(qualifier) };
1785        Ok(self.push(Expr::Star { qualifier, replacements }))
1786    }
1787
1788    /// `ReplaceList <- 'REPLACE' ReplaceEntries`, where an entry is `Expression 'AS'
1789    /// ColumnReference` and the entries are one bare entry or a parenthesized list of them.
1790    ///
1791    /// The duplicate check is here rather than in the binder because that is where DuckDB does it:
1792    /// naming the same column twice is a Parser Error there, and it is one of the few things about
1793    /// a star that can be decided without knowing what the star stands for.
1794    fn replacements(&mut self, node: u32) -> Result<Slice> {
1795        // `ReplaceEntries <- ReplaceEntrySingle / ReplaceEntryList` and both of those hold the
1796        // entries as their own children, so the same walk reads either shape.
1797        let entries = self.first(self.first(node));
1798        let listed: Vec<u32> =
1799            self.kids(entries).filter(|&kid| self.name(kid) == "ReplaceEntry").collect();
1800        let mut replacements = Vec::with_capacity(listed.len());
1801        for entry in listed {
1802            let expr = self.expr(self.first(entry))?;
1803            let alias = self.identifier(self.nth(entry, 1));
1804            let written = self.ast.string(alias).to_string();
1805            if replacements
1806                .iter()
1807                .any(|held: &Target| self.ast.string(held.alias).eq_ignore_ascii_case(&written))
1808            {
1809                return Err(Error::parser(format!(
1810                    "Duplicate entry \"{written}\" in REPLACE list"
1811                )));
1812            }
1813            replacements.push(Target { expr, alias });
1814        }
1815        Ok(self.target_slice(replacements))
1816    }
1817
1818    /// `FunctionExpression <- FunctionIdentifier FunctionExpressionArguments WithinGroupClause?
1819    /// FilterClause? ExportClause? OverClause?`.
1820    fn function(&mut self, node: u32) -> Result<ExprRef> {
1821        for name in ["WithinGroupClause", "FilterClause", "ExportClause", "OverClause"] {
1822            let clause = self.find(node, name);
1823            if clause != NONE {
1824                return self.unsupported(clause);
1825            }
1826        }
1827        let name = self.name_parts(self.first(node));
1828        // `FunctionExpressionArguments <- Parens(FunctionExpressionArgumentList)` and
1829        // `FunctionExpressionArgumentList <- DistinctOrAll? FunctionArgumentList? OrderByClause?
1830        // IgnoreOrRespectNulls?`, so a call with no arguments still has both wrappers.
1831        let list = self.first(self.nth(node, 1));
1832        for name in ["OrderByClause", "IgnoreOrRespectNulls"] {
1833            let clause = self.find(list, name);
1834            if clause != NONE {
1835                return self.unsupported(clause);
1836            }
1837        }
1838        let distinct = self.quantifier(self.find(list, "DistinctOrAll")) == Quantifier::Distinct;
1839        let mut args = Vec::new();
1840        let arguments = self.find(list, "FunctionArgumentList");
1841        if arguments != NONE {
1842            for kid in self.kids(arguments) {
1843                args.push(self.argument(kid)?);
1844            }
1845        }
1846        // `IFNULL` is an ordinary call in the grammar and is not one by the time DuckDB's parser is
1847        // done with it: `ifnull(NULL, 3)` comes back named `COALESCE(NULL, 3)` there, and so does
1848        // `main.ifnull(NULL, 3)`, so the qualifier goes with the rewrite. The count is checked here
1849        // because that is where upstream checks it, with the sentence below rather than the binder's
1850        // arity error, and it is checked before the two arguments are looked at.
1851        if self.ast.name(name).last().is_some_and(|part| part.eq_ignore_ascii_case("ifnull")) {
1852            if args.len() != 2 {
1853                return Err(Error::parser("Wrong number of arguments to IFNULL."));
1854            }
1855            let args = self.expr_slice(args);
1856            let name = self.function_name("coalesce");
1857            return Ok(self.push(Expr::Function { name, args, distinct }));
1858        }
1859        let args = self.expr_slice(args);
1860        Ok(self.push(Expr::Function { name, args, distinct }))
1861    }
1862
1863    /// `CoalesceExpression <- 'COALESCE' Parens(List(Expression))`.
1864    ///
1865    /// A keyword is not a child and the two wrappers are transparent, so the children are the
1866    /// arguments. One of them is enough for the grammar and none of them is a syntax error, which is
1867    /// why there is no count checked here.
1868    ///
1869    /// The call is written with the canonical name rather than the one the query used, since there is
1870    /// nothing else to keep: the keyword is the name. Upstream prints the column in capitals whatever
1871    /// case was written, because `COALESCE` is an operator there and not a function name that its
1872    /// parser folded, and the binder is where that is decided here.
1873    fn coalesce(&mut self, node: u32) -> Result<ExprRef> {
1874        let mut args = Vec::new();
1875        for kid in self.kids(node) {
1876            args.push(self.expr(kid)?);
1877        }
1878        let args = self.expr_slice(args);
1879        let name = self.function_name("coalesce");
1880        Ok(self.push(Expr::Function { name, args, distinct: false }))
1881    }
1882
1883    /// `NullIfExpression <- 'NULLIF' Parens(NullIfArguments)` and
1884    /// `NullIfArguments <- Expression ',' Expression`.
1885    ///
1886    /// Exactly two arguments, because the rule says so: `nullif(1)` and `nullif(1, 2, 3)` are syntax
1887    /// errors upstream and are syntax errors here for the same reason, so there is no arity to check
1888    /// after the parse.
1889    ///
1890    /// It stays a function called `nullif` rather than becoming the `CASE` upstream's macro expands
1891    /// to, since the column it produces is named after the call and not after the expansion.
1892    fn null_if(&mut self, node: u32) -> Result<ExprRef> {
1893        let arguments = self.find(node, "NullIfArguments");
1894        if arguments == NONE {
1895            return self.unsupported(node);
1896        }
1897        let mut args = Vec::new();
1898        for kid in self.kids(arguments) {
1899            args.push(self.expr(kid)?);
1900        }
1901        let args = self.expr_slice(args);
1902        let name = self.function_name("nullif");
1903        Ok(self.push(Expr::Function { name, args, distinct: false }))
1904    }
1905
1906    /// `SubstringExpression <- 'SUBSTRING' Parens(SubstringArguments)` and
1907    /// `SubstringArguments <- SubstringParameters / SubstringExpressionList`.
1908    ///
1909    /// Both spellings are the same call and DuckDB's parser writes both of them back out as one:
1910    /// `substring(s FROM a FOR b)` comes back as the column `"substring"(s, a, b)` there, and so does
1911    /// `substring(s, a, b)`. The `FOR` on its own is the one worth pointing at, since it is not the
1912    /// two argument call it looks like. `substring('abcdef' FOR 3)` is `"substring"('abcdef', 1, 3)`
1913    /// upstream, so the start is filled in with a literal 1 here rather than left out.
1914    fn substring(&mut self, node: u32) -> Result<ExprRef> {
1915        let shape = self.first(self.first(node));
1916        let mut args = Vec::new();
1917        match self.name(shape) {
1918            "SubstringExpressionList" => {
1919                for kid in self.kids(shape) {
1920                    args.push(self.expr(kid)?);
1921                }
1922            }
1923            "SubstringParameters" => {
1924                args.push(self.expr(self.first(shape))?);
1925                // `SubstringFromFor <- SubstringFromOptionalFor / SubstringFor`, and both of those
1926                // hold the bounds as `FromExpression` and `ForExpression`, so finding them by name
1927                // reads either shape and neither one has to be told apart from the other.
1928                let bounds = self.first(self.nth(shape, 1));
1929                let from = self.find(bounds, "FromExpression");
1930                let start =
1931                    if from == NONE { self.number("1") } else { self.expr(self.first(from))? };
1932                args.push(start);
1933                let count = self.find(bounds, "ForExpression");
1934                if count != NONE {
1935                    args.push(self.expr(self.first(count))?);
1936                }
1937            }
1938            _ => return self.unsupported(shape),
1939        }
1940        let args = self.expr_slice(args);
1941        let name = self.function_name("substring");
1942        Ok(self.push(Expr::Function { name, args, distinct: false }))
1943    }
1944
1945    /// `PositionExpression <- 'POSITION' Parens(PositionArguments)` and
1946    /// `PositionArguments <- OtherOperatorExpression 'IN' Expression`.
1947    ///
1948    /// The two arguments swap. `position('c' IN 'abcdef')` is `"position"('abcdef', 'c')` upstream,
1949    /// which is the same order `strpos` and `instr` are written in, so the haystack comes first in
1950    /// the call and second in the query.
1951    fn position(&mut self, node: u32) -> Result<ExprRef> {
1952        let arguments = self.first(node);
1953        if self.count(arguments) != 2 {
1954            return self.unsupported(arguments);
1955        }
1956        let needle = self.expr(self.first(arguments))?;
1957        let haystack = self.expr(self.nth(arguments, 1))?;
1958        let args = self.expr_slice(vec![haystack, needle]);
1959        let name = self.function_name("position");
1960        Ok(self.push(Expr::Function { name, args, distinct: false }))
1961    }
1962
1963    /// `TrimExpression <- 'TRIM' Parens(TrimArguments)` and
1964    /// `TrimArguments <- TrimDirection? TrimSource? List(Expression)`.
1965    ///
1966    /// The direction is not an argument, it is the function: `LEADING` is `ltrim` upstream and
1967    /// `TRAILING` is `rtrim`, while `BOTH` and the bare form are both `trim`. The characters to strip
1968    /// are the last argument whichever way they were written, so `trim(BOTH 'x' FROM 'xxaxx')` and
1969    /// `trim('xxaxx', 'x')` are the same call, which is why the source goes on the end of the list
1970    /// rather than in front of it.
1971    fn trim(&mut self, node: u32) -> Result<ExprRef> {
1972        let arguments = self.first(node);
1973        let direction = self.find(arguments, "TrimDirection");
1974        let name = match direction {
1975            NONE => "trim",
1976            held => match self.name(self.first(held)) {
1977                "TrimLeading" => "ltrim",
1978                "TrimTrailing" => "rtrim",
1979                _ => "trim",
1980            },
1981        };
1982        let mut args = Vec::new();
1983        for kid in self.kids(arguments) {
1984            if matches!(self.name(kid), "TrimDirection" | "TrimSource") {
1985                continue;
1986            }
1987            args.push(self.expr(kid)?);
1988        }
1989        // `TrimSource <- Expression? 'FROM'`, so `trim(LEADING FROM s)` has the node with nothing
1990        // under it and there is no second argument to add.
1991        let source = self.find(arguments, "TrimSource");
1992        if source != NONE && self.count(source) == 1 {
1993            args.push(self.expr(self.first(source))?);
1994        }
1995        let args = self.expr_slice(args);
1996        let name = self.function_name(name);
1997        Ok(self.push(Expr::Function { name, args, distinct: false }))
1998    }
1999
2000    /// `OverlayExpression <- 'OVERLAY' Parens(OverlayArguments)` and
2001    /// `OverlayArguments <- OverlayParameters / OverlayExpressionList`, where
2002    /// `OverlayParameters <- Expression 'PLACING' Expression FromExpression ForExpression?`.
2003    ///
2004    /// The arguments are already in the order the call takes them, so the keyword spelling is the
2005    /// list spelling with `PLACING`, `FROM` and `FOR` where the commas would be:
2006    /// `overlay('abcdef' PLACING 'X' FROM 2 FOR 1)` is `"overlay"('abcdef', 'X', 2, 1)` upstream.
2007    fn overlay(&mut self, node: u32) -> Result<ExprRef> {
2008        let shape = self.first(self.first(node));
2009        if !matches!(self.name(shape), "OverlayParameters" | "OverlayExpressionList") {
2010            return self.unsupported(shape);
2011        }
2012        let mut args = Vec::new();
2013        for kid in self.kids(shape) {
2014            let kid = match self.name(kid) {
2015                "FromExpression" | "ForExpression" => self.first(kid),
2016                _ => kid,
2017            };
2018            args.push(self.expr(kid)?);
2019        }
2020        let args = self.expr_slice(args);
2021        let name = self.function_name("overlay");
2022        Ok(self.push(Expr::Function { name, args, distinct: false }))
2023    }
2024
2025    /// A number literal the query did not write, for the one place a lowering has to supply one.
2026    fn number(&mut self, text: &str) -> ExprRef {
2027        let text = self.intern(text);
2028        self.push(Expr::Literal { kind: LiteralKind::Number, text })
2029    }
2030
2031    /// `ExtractExpression <- 'EXTRACT' Parens(ExtractArguments)` and
2032    /// `ExtractArguments <- ExtractArgument 'FROM' Expression`.
2033    ///
2034    /// `EXTRACT` is not a function in the grammar because its argument list is not an argument
2035    /// list, and it is a function everywhere after here because DuckDB's parser does the same
2036    /// rewrite: `EXTRACT(minute FROM t)` is `date_part('minute', t)` and there is no separate
2037    /// implementation of one of them. The part is a keyword, an identifier or a string in the
2038    /// grammar, and all three become the string, which is why this is a rewrite and not a node.
2039    fn extract(&mut self, node: u32) -> Result<ExprRef> {
2040        let arguments = self.find(node, "ExtractArguments");
2041        if arguments == NONE {
2042            return self.unsupported(node);
2043        }
2044        let argument = self.first(self.first(arguments));
2045        let part = match self.name(argument) {
2046            "ExtractStringArgument" => self.string_value(argument)?,
2047            // A keyword or an identifier, both taken as written. Which specifier names are legal is
2048            // not a question about syntax, so the answer to it lives with the function.
2049            "ExtractDatePartArgument" | "ExtractIdentifierArgument" => {
2050                self.text(argument).to_string()
2051            }
2052            _ => return self.unsupported(argument),
2053        };
2054        let text = self.intern(&part);
2055        let part = self.push(Expr::Literal { kind: LiteralKind::String, text });
2056        let operand = self.expr(self.nth(arguments, 1))?;
2057        let name = self.function_name("date_part");
2058        let args = self.expr_slice(vec![part, operand]);
2059        Ok(self.push(Expr::Function { name, args, distinct: false }))
2060    }
2061
2062    /// `FunctionArgument <- NamedFunctionArgument / PositionalFunctionArgument`.
2063    fn argument(&mut self, node: u32) -> Result<ExprRef> {
2064        let inner = self.first(node);
2065        match self.name(inner) {
2066            "PositionalFunctionArgument" => self.expr(self.first(inner)),
2067            _ => self.unsupported(inner),
2068        }
2069    }
2070
2071    /// One argument of a table function, which is the same rule plus the names.
2072    ///
2073    /// `NamedParameter <- TypeFuncName Type? NamedParameterAssignment Expression` and
2074    /// `NamedParameterAssignment <- ':=' / '=>'`, so those two spellings are what the grammar has.
2075    /// The binary accepts a third, `name = value`, which the grammar has no rule for because it
2076    /// parses as an equality and is picked apart afterwards. That is what happens here too: a
2077    /// positional argument that is a comparison between a bare name and something else is a named
2078    /// parameter, which is the reading upstream's own transformer gives it. `read_parquet(f,
2079    /// binary_as_string=True)` is the query that matters and it is the spelling the ClickBench
2080    /// entry uses.
2081    ///
2082    /// The name is not resolved here and neither is the value. Which parameters a function takes
2083    /// is the binder's question, and so is whether `binary_as_string=True` means anything to the
2084    /// function it was written on.
2085    fn table_argument(&mut self, node: u32) -> Result<Target> {
2086        let inner = self.first(node);
2087        if self.name(inner) == "NamedFunctionArgument" {
2088            let named = self.first(inner);
2089            if self.count(named) != 3 {
2090                // The optional `Type` between the name and the assignment, which is a macro
2091                // parameter's declaration and not a call.
2092                return self.unsupported(named);
2093            }
2094            let alias = self.identifier(self.first(named));
2095            let expr = self.expr(self.nth(named, 2))?;
2096            return Ok(Target { expr, alias });
2097        }
2098        let expr = self.expr(self.first(inner))?;
2099        if let Expr::Binary { op: BinaryOp::Eq, left, right } = self.ast.expr(expr) {
2100            if let Expr::Column { name } = self.ast.expr(left) {
2101                if name.len == 1 {
2102                    let alias = self.ast.parts[name.start as usize];
2103                    return Ok(Target { expr: right, alias });
2104                }
2105            }
2106        }
2107        Ok(Target { expr, alias: NONE })
2108    }
2109
2110    /// `CastExpression <- CastOrTryCast Parens(CastArguments)`.
2111    fn cast(&mut self, node: u32) -> Result<ExprRef> {
2112        let try_cast = self.name(self.first(self.first(node))) == "TryCastKeyword";
2113        // `CastArguments <- Expression 'AS' Type`.
2114        let arguments = self.nth(node, 1);
2115        let operand = self.expr(self.first(arguments))?;
2116        let text = self.text(self.nth(arguments, 1)).to_string();
2117        let ty = self.intern(&text);
2118        Ok(self.push(Expr::Cast { operand, ty, try_cast }))
2119    }
2120
2121    /// `TypeLiteral <- Type StringLiteral`, which is the cast written the other way round.
2122    ///
2123    /// `DATE '1995-09-01'` and `CAST('1995-09-01' AS DATE)` are the same expression upstream, and
2124    /// the proof is the column name: the pinned binary answers both of them in a column called
2125    /// `CAST('1995-09-01' AS DATE)`. So this is the cast node and nothing else, which means every
2126    /// type the cast already takes is a typed literal for free and the two can never drift.
2127    ///
2128    /// The string is the literal the grammar matched rather than any expression, so there is no
2129    /// constant folding question here. `DATE x` does not parse in the first place.
2130    fn typed_literal(&mut self, node: u32) -> Result<ExprRef> {
2131        let text = self.text(self.first(node)).to_string();
2132        let ty = self.intern(&text);
2133        let operand = self.expr(self.nth(node, 1))?;
2134        Ok(self.push(Expr::Cast { operand, ty, try_cast: false }))
2135    }
2136
2137    /// `IntervalLiteral <- 'INTERVAL' IntervalParameter Interval?`, which is a function call.
2138    ///
2139    /// There is no interval node and there does not need to be one, because DuckDB's own
2140    /// transformer rewrites the literal into a call and the column name says so: `INTERVAL 1 DAY`
2141    /// comes back from the pinned binary in a column called
2142    /// `to_days(CAST(trunc(CAST(1 AS DOUBLE)) AS INTEGER))`. So the literal and a handwritten
2143    /// `to_days(1)` are the same expression from here on and the two cannot drift apart.
2144    ///
2145    /// Every unit goes through a DOUBLE on the way in, which is what makes `INTERVAL 1.5 DAY` one
2146    /// day rather than a day and a half: the truncation is in the rewrite and not in the function.
2147    /// The two units that can carry a fraction skip the truncation and stay a DOUBLE all the way,
2148    /// so `INTERVAL 2.7 SECOND` really is two and seven tenths of a second.
2149    ///
2150    /// A literal with no unit is the cast written the other way round, so `INTERVAL '1 day'` is
2151    /// `CAST('1 day' AS INTERVAL)`. That arm also catches a word the grammar does not read as a
2152    /// unit, since `INTERVAL 1 d` parses as this rule with no `Interval` child and a column alias
2153    /// after it, which is why upstream answers it with a cast error about an INTEGER.
2154    fn interval_literal(&mut self, node: u32) -> Result<ExprRef> {
2155        let parameter = self.find(node, "IntervalParameter");
2156        if parameter == NONE {
2157            return self.unsupported(node);
2158        }
2159        let operand = self.expr(self.first(parameter))?;
2160        let unit = self.find(node, "Interval");
2161        if unit == NONE {
2162            let ty = self.intern("INTERVAL");
2163            return Ok(self.push(Expr::Cast { operand, ty, try_cast: false }));
2164        }
2165        let spelling = self.name(self.first(unit));
2166        // The seven range forms parse and then refuse, in upstream's words, with the unit names
2167        // spelled the canonical way rather than the way they were written: `interval 1 days to
2168        // hours` is `DAY TO HOUR` there as well.
2169        if spelling == "IntervalToInterval" {
2170            let pair = self.name(self.first(self.first(unit)));
2171            return Err(Error::parser(format!("{} is not supported", worded(pair))));
2172        }
2173        let Some(&(_, function, width)) = UNITS.iter().find(|(rule, _, _)| *rule == spelling)
2174        else {
2175            return self.unsupported(unit);
2176        };
2177        let double = self.intern("DOUBLE");
2178        let mut count = self.push(Expr::Cast { operand, ty: double, try_cast: false });
2179        if let Some(width) = width {
2180            let name = self.function_name("trunc");
2181            let args = self.expr_slice(vec![count]);
2182            let whole = self.push(Expr::Function { name, args, distinct: false });
2183            let ty = self.intern(width);
2184            count = self.push(Expr::Cast { operand: whole, ty, try_cast: false });
2185        }
2186        let name = self.function_name(function);
2187        let args = self.expr_slice(vec![count]);
2188        Ok(self.push(Expr::Function { name, args, distinct: false }))
2189    }
2190
2191    /// `CaseExpression <- 'CASE' Expression? CaseWhenThen+ CaseElse? 'END'`.
2192    fn case(&mut self, node: u32) -> Result<ExprRef> {
2193        let mut operand = NONE;
2194        let mut arms = Vec::new();
2195        let mut otherwise = NONE;
2196        for kid in self.kids(node) {
2197            match self.name(kid) {
2198                // `CaseWhenThen <- 'WHEN' Expression 'THEN' Expression`.
2199                "CaseWhenThen" => {
2200                    let when = self.expr(self.first(kid))?;
2201                    let then = self.expr(self.nth(kid, 1))?;
2202                    arms.push(CaseArm { when, then });
2203                }
2204                // `CaseElse <- 'ELSE' Expression`.
2205                "CaseElse" => otherwise = self.expr(self.first(kid))?,
2206                // The bare `Expression` before the first `WHEN`, which makes it a simple case.
2207                _ => operand = self.expr(kid)?,
2208            }
2209        }
2210        let start = self.ast.case_arms.len() as u32;
2211        self.ast.case_arms.extend(arms);
2212        let arms = Slice { start, len: self.ast.case_arms.len() as u32 - start };
2213        Ok(self.push(Expr::Case { operand, arms, otherwise }))
2214    }
2215
2216    /// `ParenthesisExpression <- Parens(List(Expression)?)`, which is a row value.
2217    ///
2218    /// One item is not a row. `(a)` is `a` in every dialect and reading it as a one column row
2219    /// would change what `(a) = (b)` means.
2220    fn row(&mut self, node: u32) -> Result<ExprRef> {
2221        let mut items = Vec::new();
2222        for kid in self.kids(node) {
2223            items.push(self.expr(kid)?);
2224        }
2225        if items.len() == 1 {
2226            return Ok(items[0]);
2227        }
2228        let items = self.expr_slice(items);
2229        Ok(self.push(Expr::Row { items }))
2230    }
2231
2232    /// `Parameter <- '?' Number / '?' / '$' Number / '$' ColLabel`, a prepared statement parameter.
2233    ///
2234    /// The identifier is what follows the marker, so `?1` and `$1` are both the parameter named 1,
2235    /// and a bare `?` takes the next number by where it was written. That is what DuckDB does, which
2236    /// is why `? + $2` prints as `$1 + $2`: the counting is its own and does not skip a number
2237    /// because a later parameter claimed it.
2238    fn parameter(&mut self, node: u32) -> Result<ExprRef> {
2239        let written = self.text(node).trim();
2240        let written = written.trim_start_matches(['?', '$']).trim();
2241        let name = if written.is_empty() {
2242            self.anonymous += 1;
2243            self.anonymous.to_string()
2244        } else {
2245            written.to_string()
2246        };
2247        let name = self.intern(&name);
2248        Ok(self.push(Expr::Parameter { name }))
2249    }
2250
2251    /// `BoundedListExpression <- '[' List(Expression)? ']'`, which is a LIST value.
2252    ///
2253    /// One item is a list of one here, unlike the parenthesised form, because the brackets are what
2254    /// say list and there is nothing else `[a]` could mean.
2255    fn list(&mut self, node: u32) -> Result<ExprRef> {
2256        let mut items = Vec::new();
2257        for kid in self.kids(node) {
2258            items.push(self.expr(kid)?);
2259        }
2260        let items = self.expr_slice(items);
2261        Ok(self.push(Expr::List { items }))
2262    }
2263
2264    /// `SubqueryExpression <- SubqueryNot? SubqueryExists? SubqueryReference`.
2265    fn subquery(&mut self, node: u32) -> Result<ExprRef> {
2266        if self.find(node, "SubqueryNot") != NONE || self.find(node, "SubqueryExists") != NONE {
2267            return self.unsupported(node);
2268        }
2269        let reference = self.find(node, "SubqueryReference");
2270        let query = self.query(self.first(reference))?;
2271        Ok(self.push(Expr::Subquery { query }))
2272    }
2273
2274    /// The value of a string literal, with the quotes gone and the escapes resolved.
2275    ///
2276    /// A literal can be several tokens. `'a' 'b'` on two lines is one literal that is `ab`, which is
2277    /// the SQL standard's rule and DuckDB's, so the node is decoded token by token rather than by
2278    /// taking its text and stripping the outside.
2279    fn string_value(&self, node: u32) -> Result<String> {
2280        let span = self.tree.node(node);
2281        let mut value = String::new();
2282        for token in &self.tokens[span.start as usize..span.end as usize] {
2283            if token.kind == Kind::String {
2284                value.push_str(&string_token(token.text(self.query))?);
2285            }
2286        }
2287        Ok(value)
2288    }
2289
2290    /// The token that opens a string literal, which is the whole of it when it has a prefix.
2291    ///
2292    /// Only the first token is asked, because a prefixed literal is one token: `E'a' 'b'` is a
2293    /// syntax error upstream rather than a concatenation, so there is no second prefix to disagree
2294    /// with this one.
2295    fn first_string(&self, node: u32) -> &'a str {
2296        let span = self.tree.node(node);
2297        self.tokens[span.start as usize..span.end as usize]
2298            .iter()
2299            .find(|token| token.kind == Kind::String)
2300            .map_or("", |token| token.text(self.query))
2301    }
2302
2303    /// A string literal as an expression, which is the value plus what the prefix makes of it.
2304    ///
2305    /// `N'abc'` is a cast of the string to VARCHAR upstream and not a plain string, and the column
2306    /// name is the proof: the pinned binary answers it in a column called `CAST('abc' AS VARCHAR)`.
2307    /// So it is written here as the cast it is, and then there is nothing left to keep in step.
2308    ///
2309    /// `x'4142'` is not a string at all, it is a BLOB, so it is the one prefix that becomes a
2310    /// different kind of literal rather than a string with something done to it.
2311    fn string_literal(&mut self, node: u32) -> Result<ExprRef> {
2312        let token = self.first_string(node);
2313        let prefix = match token.as_bytes() {
2314            [prefix, b'\'', ..] => *prefix,
2315            _ => 0,
2316        };
2317        if matches!(prefix, b'X' | b'x') {
2318            if let Some(body) = token.get(1..).and_then(quoted_body) {
2319                let text = blob_text(body.as_bytes())?;
2320                let text = self.intern(&text);
2321                return Ok(self.push(Expr::Literal { kind: LiteralKind::Blob, text }));
2322            }
2323        }
2324        let value = self.string_value(node)?;
2325        let text = self.intern(&value);
2326        let literal = self.push(Expr::Literal { kind: LiteralKind::String, text });
2327        if matches!(prefix, b'N' | b'n') {
2328            let ty = self.intern("VARCHAR");
2329            return Ok(self.push(Expr::Cast { operand: literal, ty, try_cast: false }));
2330        }
2331        Ok(literal)
2332    }
2333}
2334
2335/// Each unit an interval literal can be written in, as the grammar rule that spells it, the
2336/// function it becomes, and the width the count is truncated to on the way there.
2337///
2338/// A width of `None` is the pair that keeps what is after the point. Those two stay a DOUBLE and
2339/// never see `trunc`, which is the whole of the difference between `INTERVAL 2.7 SECOND` being two
2340/// and seven tenths of a second and `INTERVAL 1.5 DAY` being one day. Every entry, both spellings
2341/// of every keyword and the width of each one was read off the pinned binary's column names.
2342const UNITS: &[(&str, &str, Option<&str>)] = &[
2343    ("YearKeyword", "to_years", Some("INTEGER")),
2344    ("MonthKeyword", "to_months", Some("INTEGER")),
2345    ("QuarterKeyword", "to_quarters", Some("INTEGER")),
2346    ("DecadeKeyword", "to_decades", Some("INTEGER")),
2347    ("CenturyKeyword", "to_centuries", Some("INTEGER")),
2348    ("MillenniumKeyword", "to_millennia", Some("INTEGER")),
2349    ("DayKeyword", "to_days", Some("INTEGER")),
2350    ("WeekKeyword", "to_weeks", Some("INTEGER")),
2351    ("HourKeyword", "to_hours", Some("BIGINT")),
2352    ("MinuteKeyword", "to_minutes", Some("BIGINT")),
2353    ("MicrosecondKeyword", "to_microseconds", Some("BIGINT")),
2354    ("SecondKeyword", "to_seconds", None),
2355    ("MillisecondKeyword", "to_milliseconds", None),
2356];
2357
2358/// A grammar rule name like `DayToHour` as the words upstream puts in the message for it.
2359fn worded(rule: &str) -> String {
2360    let mut out = String::new();
2361    for character in rule.chars() {
2362        if character.is_ascii_uppercase() && !out.is_empty() {
2363            out.push(' ');
2364        }
2365        out.push(character.to_ascii_uppercase());
2366    }
2367    out
2368}
2369
2370/// The value of one string token, with the quotes gone and whatever the prefix means resolved.
2371///
2372/// There is no fall through that keeps the source text. That arm is what answered `SELECT E'a'`
2373/// with the four characters `E'a'`, and a default that silently answers with the query is a default
2374/// that will do this again with the next spelling somebody adds, so a spelling this does not know
2375/// raises instead. Per #329.
2376fn string_token(text: &str) -> Result<String> {
2377    if let Some(body) = dollar_body(text) {
2378        return Ok(body.to_string());
2379    }
2380    if let Some(body) = quoted_body(text) {
2381        return Ok(body.replace("''", "'"));
2382    }
2383    let Some(body) = text.get(1..).and_then(quoted_body) else {
2384        return Ok(text.to_string());
2385    };
2386    match text.as_bytes()[0] {
2387        b'E' | b'e' => escaped(body),
2388        // `N'abc'` is the string and nothing else. The cast that makes the name is put on outside.
2389        b'N' | b'n' => Ok(body.replace("''", "'")),
2390        // Not a bit string, whatever the spelling suggests. Upstream answers `B'101'` with the four
2391        // characters `b101` as a VARCHAR, and `B''` with the one character `b`, which is measured
2392        // and not guessed. Nothing else is done with the body.
2393        b'B' | b'b' => Ok(format!("b{}", body.replace("''", "'"))),
2394        // `x'41'` is a BLOB and a BLOB is not a string, so the places that want a string out of a
2395        // literal, which are DESCRIBE and the part in EXTRACT, do not get one from this spelling.
2396        _ => Err(Error::not_implemented(format!("the string literal {text} is not supported yet"))),
2397    }
2398}
2399
2400/// The text a blob literal's body means, which is the text a blob prints as.
2401///
2402/// `x'4142'` is two bytes and the pinned binary calls the column `'AB'::BLOB`, so what is kept here
2403/// is the printed form and not the source. The cast that reads it back gives the bytes again, which
2404/// is what makes one text enough for both the value and the name, and it is `Value` that prints it
2405/// so the two spellings of a blob cannot drift apart.
2406///
2407/// Upstream writes `\xHH` for every pair without looking at the digits and lets the cast refuse the
2408/// ones that are not hex, which is why `x'4'` is a parser error and `x'zz'` is a conversion error
2409/// one step later. Doing the same thing gives both messages in the same words. The pairs are bytes
2410/// and not characters: `x'éé'` is four bytes and so two pairs, which is how upstream counts them.
2411fn blob_text(body: &[u8]) -> Result<String> {
2412    if body.len() % 2 != 0 {
2413        return Err(Error::parser("Hex string literal must have an even number of hex digits"));
2414    }
2415    let digit = |byte: u8| (byte as char).to_digit(16).map(|digit| digit as u8);
2416    let bytes: Option<Vec<u8>> =
2417        body.chunks(2).map(|pair| Some(digit(pair[0])? * 16 + digit(pair[1])?)).collect();
2418    match bytes {
2419        Some(bytes) => Ok(Value::Blob(bytes).to_string()),
2420        None => {
2421            Ok(body.chunks(2).map(|pair| format!("\\x{}", String::from_utf8_lossy(pair))).collect())
2422        }
2423    }
2424}
2425
2426/// The body of a single quoted string, for the tokens that are one.
2427///
2428/// An unterminated token has nothing to take off the end and keeps every byte it was given, which
2429/// is why the closing quote has to be a quote that is not also the opening one.
2430fn quoted_body(text: &str) -> Option<&str> {
2431    text.strip_prefix('\'').filter(|rest| !rest.is_empty()).and_then(|rest| rest.strip_suffix('\''))
2432}
2433
2434/// The body of an `E'...'` literal, with the C style escapes resolved.
2435///
2436/// Every rule here was read off the pinned binary one at a time. The named escapes are `\n`, `\t`,
2437/// `\r`, `\b` and `\f`, and `\v` is not one of them. `\x` takes one or two hex digits and `\0`
2438/// through `\7` take one to three octal digits, both of which write a byte and not a character, so
2439/// `\xc3\xa9` is one `é` and `\377` is not a string at all. `\uHHHH` takes exactly four hex digits
2440/// and writes the character they name. Anything else, including a `\u` that is short or names a
2441/// surrogate half or a NUL, drops the backslash and keeps the character, so `\q` is `q` and `\u41`
2442/// is `u41`.
2443///
2444/// The result is bytes until the end because the escapes write bytes, and the two ways of writing
2445/// something that is not a string both raise the way upstream raises them.
2446fn escaped(body: &str) -> Result<String> {
2447    let bytes = body.as_bytes();
2448    let mut out = Vec::with_capacity(bytes.len());
2449    let mut at = 0;
2450    while at < bytes.len() {
2451        let byte = bytes[at];
2452        at += 1;
2453        if byte == b'\'' && bytes.get(at) == Some(&b'\'') {
2454            out.push(b'\'');
2455            at += 1;
2456            continue;
2457        }
2458        if byte != b'\\' || at == bytes.len() {
2459            out.push(byte);
2460            continue;
2461        }
2462        let escape = bytes[at];
2463        at += 1;
2464        match escape {
2465            b'n' => out.push(b'\n'),
2466            b't' => out.push(b'\t'),
2467            b'r' => out.push(b'\r'),
2468            b'b' => out.push(0x08),
2469            b'f' => out.push(0x0c),
2470            b'x' => match digits(bytes, &mut at, 16, 2) {
2471                Some(value) => out.push(value as u8),
2472                None => out.push(b'x'),
2473            },
2474            b'0'..=b'7' => {
2475                at -= 1;
2476                let value = digits(bytes, &mut at, 8, 3).unwrap_or(0);
2477                out.push(value as u8);
2478            }
2479            b'u' => match four_hex(bytes, at).and_then(char::from_u32).filter(|c| *c != '\0') {
2480                Some(c) => {
2481                    at += 4;
2482                    out.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes());
2483                }
2484                None => out.push(b'u'),
2485            },
2486            other => out.push(other),
2487        }
2488    }
2489    if out.contains(&0) {
2490        return Err(Error::parser("Null character not permitted in escape string literal"));
2491    }
2492    String::from_utf8(out).map_err(|error| {
2493        Error::parser(format!(
2494            "Invalid UTF-8 in escape string literal at byte offset {}: byte mismatch",
2495            error.utf8_error().valid_up_to()
2496        ))
2497    })
2498}
2499
2500/// Up to `most` digits in `radix` starting at `at`, moving `at` past the ones that were taken.
2501///
2502/// `None` means there were none at all, which is the case where the escape was not an escape:
2503/// `\x` on its own is the letter `x` upstream and not a zero byte.
2504fn digits(bytes: &[u8], at: &mut usize, radix: u32, most: usize) -> Option<u32> {
2505    let mut value = None;
2506    for _ in 0..most {
2507        let Some(digit) = bytes.get(*at).and_then(|byte| (*byte as char).to_digit(radix)) else {
2508            break;
2509        };
2510        value = Some(value.unwrap_or(0) * radix + digit);
2511        *at += 1;
2512    }
2513    value
2514}
2515
2516/// The four hex digits of a `\uHHHH`, which has to be all four of them or it is not one.
2517///
2518/// Nothing is consumed here, because the digits are only digits if the whole escape works out. A
2519/// surrogate half is not a character and upstream does not pair it up either, so `😀` is
2520/// the ten characters it was written as, which is what the caller falls back to.
2521fn four_hex(bytes: &[u8], at: usize) -> Option<u32> {
2522    let digits = bytes.get(at..at + 4)?;
2523    if !digits.iter().all(u8::is_ascii_hexdigit) {
2524        return None;
2525    }
2526    u32::from_str_radix(std::str::from_utf8(digits).ok()?, 16).ok()
2527}
2528
2529/// The body of a dollar quoted string, for the tokens that are one.
2530///
2531/// The tag is whatever sits between the opening pair of dollars and may be empty, so `$$a$$` and
2532/// `$tag$a$tag$` both arrive here, and nothing inside the body is escaped, which is the whole reason
2533/// the spelling exists. The tokenizer has already found the closing tag, which is the part that takes
2534/// work, so this says where the body starts and ends and no more. A token that is not dollar quoted
2535/// gives `None` and so does an unterminated one, which has no closing tag to take off and keeps every
2536/// byte it was given, the way the matcher already treats it. Per #276.
2537fn dollar_body(text: &str) -> Option<&str> {
2538    let rest = text.strip_prefix('$')?;
2539    let close = rest.find('$')?;
2540    let (tag, body) = (&rest[..close], &rest[close + 1..]);
2541    body.strip_suffix(&format!("${tag}$"))
2542}
2543
2544/// Strip the quoting off an identifier.
2545///
2546/// DuckDB does not fold identifier case at any point, quoted or not, so this only removes the
2547/// quotes and resolves the doubled ones. Anything else would be the parser deciding what a name is.
2548///
2549/// Single quotes are stripped too, and the only way one gets here is the file name in `FROM
2550/// 'hits.parquet'`, because the matcher takes a string for a name in that position and in `COPY t TO
2551/// '...'` and nowhere else. Leaving them on would make that name different from the one `FROM
2552/// "hits.parquet"` writes, and DuckDB reads both of those as the same file.
2553fn unquote(text: &str) -> String {
2554    if let Some(body) = text.strip_prefix('"').and_then(|rest| rest.strip_suffix('"')) {
2555        return body.replace("\"\"", "\"");
2556    }
2557    match text.strip_prefix('\'').and_then(|rest| rest.strip_suffix('\'')) {
2558        Some(body) => body.replace("''", "'"),
2559        None => text.to_string(),
2560    }
2561}
2562
2563#[cfg(test)]
2564mod tests {
2565    use super::*;
2566    use crate::corpus::CORPUS;
2567    use crate::matcher::parse;
2568
2569    /// The AST written back out as text, which is what the assertions below read.
2570    ///
2571    /// Not a SQL printer and not trying to be. It is deliberately not valid SQL: operators are
2572    /// spelled with the name of the variant and every binary node is parenthesised, so that a test
2573    /// asserting on this text is asserting on the shape of the tree and not on a formatting choice.
2574    /// `a - b - c` and `a - (b - c)` have to look different here or the test that tells them apart
2575    /// is not a test.
2576    fn show(ast: &Ast, expr: ExprRef) -> String {
2577        if expr == NONE {
2578            return "-".to_string();
2579        }
2580        let list = |slice: Slice| {
2581            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
2582        };
2583        match ast.expr(expr) {
2584            Expr::Star { qualifier, replacements } => {
2585                let star = if qualifier.is_empty() {
2586                    "*".to_string()
2587                } else {
2588                    format!("{}.*", ast.name_text(qualifier))
2589                };
2590                if replacements.is_empty() {
2591                    return star;
2592                }
2593                let entries: Vec<String> = ast
2594                    .target_list(replacements)
2595                    .iter()
2596                    .map(|target| {
2597                        format!("{} AS {}", show(ast, target.expr), ast.string(target.alias))
2598                    })
2599                    .collect();
2600                format!("{star} REPLACE ({})", entries.join(", "))
2601            }
2602            Expr::Column { name } => ast.name_text(name),
2603            Expr::Literal { kind, text } => match kind {
2604                LiteralKind::Number => ast.string(text).to_string(),
2605                LiteralKind::String => format!("'{}'", ast.string(text)),
2606                LiteralKind::Blob => format!("'{}'::BLOB", ast.string(text)),
2607                other => format!("{other:?}").to_uppercase(),
2608            },
2609            Expr::Unary { op, operand } => format!("({op:?} {})", show(ast, operand)),
2610            Expr::Binary { op, left, right } => {
2611                let op = match op {
2612                    BinaryOp::Named(name) => ast.string(name).to_string(),
2613                    other => format!("{other:?}"),
2614                };
2615                format!("({} {op} {})", show(ast, left), show(ast, right))
2616            }
2617            Expr::Function { name, args, distinct } => {
2618                let distinct = if distinct { "DISTINCT " } else { "" };
2619                format!("{}({distinct}{})", ast.name_text(name), list(args))
2620            }
2621            Expr::Cast { operand, ty, try_cast } => {
2622                let word = if try_cast { "TRY_CAST" } else { "CAST" };
2623                format!("{word}({} AS {})", show(ast, operand), ast.string(ty))
2624            }
2625            Expr::Case { operand, arms, otherwise } => {
2626                let arms = ast
2627                    .arm_list(arms)
2628                    .iter()
2629                    .map(|arm| format!("WHEN {} THEN {}", show(ast, arm.when), show(ast, arm.then)))
2630                    .collect::<Vec<_>>()
2631                    .join(" ");
2632                format!("CASE {} {arms} ELSE {} END", show(ast, operand), show(ast, otherwise))
2633            }
2634            Expr::Between { operand, low, high, negated } => {
2635                let not = if negated { "NOT " } else { "" };
2636                format!(
2637                    "({not}{} BETWEEN {} AND {})",
2638                    show(ast, operand),
2639                    show(ast, low),
2640                    show(ast, high)
2641                )
2642            }
2643            Expr::In { operand, list: items, negated } => {
2644                let not = if negated { "NOT " } else { "" };
2645                format!("({not}{} IN [{}])", show(ast, operand), list(items))
2646            }
2647            Expr::List { items } => format!("[{}]", list(items)),
2648            Expr::Parameter { name } => format!("${}", ast.string(name)),
2649            Expr::Row { items } => format!("ROW({})", list(items)),
2650            Expr::Subquery { query } => format!("({})", show_query(ast, query)),
2651        }
2652    }
2653
2654    /// One from item written back out.
2655    fn show_source(ast: &Ast, source: SourceRef) -> String {
2656        let alias = |alias: StrRef| match alias {
2657            NONE => String::new(),
2658            other => format!(" AS {}", ast.string(other)),
2659        };
2660        match ast.source(source) {
2661            Source::Table { name, alias: name_alias, .. } => {
2662                format!("{}{}", ast.name_text(name), alias(name_alias))
2663            }
2664            Source::Function { name, args, alias: call_alias, .. } => {
2665                let args = ast
2666                    .target_list(args)
2667                    .iter()
2668                    .map(|item| match item.alias {
2669                        NONE => show(ast, item.expr),
2670                        named => format!("{} := {}", ast.string(named), show(ast, item.expr)),
2671                    })
2672                    .collect::<Vec<_>>()
2673                    .join(", ");
2674                format!("{}({args}){}", ast.name_text(name), alias(call_alias))
2675            }
2676            Source::Subquery { query, alias: query_alias, .. } => {
2677                format!("({}){}", show_query(ast, query), alias(query_alias))
2678            }
2679            Source::Values { rows, alias: values_alias, .. } => {
2680                format!("{}{}", show_rows(ast, rows), alias(values_alias))
2681            }
2682            Source::Join { left, right, kind, natural, on, using } => {
2683                let natural = if natural { "NATURAL " } else { "" };
2684                let on = if on == NONE { String::new() } else { format!(" ON {}", show(ast, on)) };
2685                let using = if using.is_empty() {
2686                    String::new()
2687                } else {
2688                    format!(" USING ({})", ast.name_text(using))
2689                };
2690                format!(
2691                    "({} {natural}{kind:?} JOIN {}{on}{using})",
2692                    show_source(ast, left),
2693                    show_source(ast, right)
2694                )
2695            }
2696        }
2697    }
2698
2699    /// The rows of a `VALUES` written back out.
2700    fn show_rows(ast: &Ast, rows: Slice) -> String {
2701        let rows = ast
2702            .rows(rows)
2703            .iter()
2704            .map(|&row| {
2705                let items = ast
2706                    .expr_list(row)
2707                    .iter()
2708                    .map(|&item| show(ast, item))
2709                    .collect::<Vec<_>>()
2710                    .join(", ");
2711                format!("({items})")
2712            })
2713            .collect::<Vec<_>>()
2714            .join(", ");
2715        format!("VALUES {rows}")
2716    }
2717
2718    /// One query written back out.
2719    fn show_query(ast: &Ast, index: QueryRef) -> String {
2720        let query = ast.query(index);
2721        let list = |slice: Slice| {
2722            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
2723        };
2724        let mut out = match query.body {
2725            QueryBody::SetOp { op, quantifier, by_name, left, right } => {
2726                let by_name = if by_name { " BY NAME" } else { "" };
2727                format!(
2728                    "({} {op:?} {quantifier:?}{by_name} {})",
2729                    show_query(ast, left),
2730                    show_query(ast, right)
2731                )
2732            }
2733            QueryBody::Select(index) => {
2734                let select = ast.select(index);
2735                let distinct = match select.distinct {
2736                    Distinct::No => String::new(),
2737                    Distinct::Yes => " DISTINCT".to_string(),
2738                    Distinct::On(on) => format!(" DISTINCT ON ({})", list(on)),
2739                };
2740                let targets = ast
2741                    .target_list(select.targets)
2742                    .iter()
2743                    .map(|target| match target.alias {
2744                        NONE => show(ast, target.expr),
2745                        alias => format!("{} AS {}", show(ast, target.expr), ast.string(alias)),
2746                    })
2747                    .collect::<Vec<_>>()
2748                    .join(", ");
2749                let mut out = format!("SELECT{distinct} {targets}");
2750                if !select.from.is_empty() {
2751                    let from = ast
2752                        .source_list(select.from)
2753                        .iter()
2754                        .map(|&source| show_source(ast, source))
2755                        .collect::<Vec<_>>()
2756                        .join(", ");
2757                    out += &format!(" FROM {from}");
2758                }
2759                if select.filter != NONE {
2760                    out += &format!(" WHERE {}", show(ast, select.filter));
2761                }
2762                if select.group_by_all {
2763                    out += " GROUP BY ALL";
2764                } else if !select.group_by.is_empty() {
2765                    out += &format!(" GROUP BY {}", list(select.group_by));
2766                }
2767                if select.having != NONE {
2768                    out += &format!(" HAVING {}", show(ast, select.having));
2769                }
2770                out
2771            }
2772            QueryBody::Values(rows) => show_rows(ast, rows),
2773            QueryBody::Describe(inner) => format!("DESCRIBE {}", show_query(ast, inner)),
2774        };
2775        if query.order_by_all {
2776            out += " ORDER BY ALL";
2777        } else if !query.order_by.is_empty() {
2778            let items = ast
2779                .order_list(query.order_by)
2780                .iter()
2781                .map(|item| format!("{} {:?} {:?}", show(ast, item.expr), item.order, item.nulls))
2782                .collect::<Vec<_>>()
2783                .join(", ");
2784            out += &format!(" ORDER BY {items}");
2785        }
2786        if query.limit != NONE {
2787            let percent = if query.limit_percent { "%" } else { "" };
2788            out += &format!(" LIMIT {}{percent}", show(ast, query.limit));
2789        }
2790        if query.offset != NONE {
2791            out += &format!(" OFFSET {}", show(ast, query.offset));
2792        }
2793        out
2794    }
2795
2796    /// One statement, transformed and written back out.
2797    fn round(query: &str) -> String {
2798        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
2799        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
2800        let Statement::Query(index) = ast.statements[0] else {
2801            panic!("{query} is not a query");
2802        };
2803        show_query(&ast, index)
2804    }
2805
2806    /// One statement, transformed and written back out as the DDL and DML shape it is.
2807    fn round_statement(query: &str) -> String {
2808        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
2809        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
2810        match ast.statements[0] {
2811            Statement::Query(index) => show_query(&ast, index),
2812            Statement::CreateTable(index) => {
2813                let create = ast.create_table(index);
2814                let mut out = "CREATE".to_string();
2815                if create.or_replace {
2816                    out += " OR REPLACE";
2817                }
2818                if create.temporary {
2819                    out += " TEMPORARY";
2820                }
2821                out += " TABLE";
2822                if create.if_not_exists {
2823                    out += " IF NOT EXISTS";
2824                }
2825                out += &format!(" {}", ast.name_text(create.name));
2826                let columns = ast
2827                    .column_defs(create.columns)
2828                    .iter()
2829                    .map(|def| {
2830                        let ty = match def.ty {
2831                            NONE => String::new(),
2832                            other => format!(" {}", ast.string(other)),
2833                        };
2834                        let null = if def.not_null { " NOT NULL" } else { "" };
2835                        format!("{}{ty}{null}", ast.string(def.name))
2836                    })
2837                    .collect::<Vec<_>>()
2838                    .join(", ");
2839                if !columns.is_empty() || create.query == NONE {
2840                    out += &format!(" ({columns})");
2841                }
2842                if create.query != NONE {
2843                    out += &format!(" AS {}", show_query(&ast, create.query));
2844                }
2845                out
2846            }
2847            Statement::CreateView(index) => {
2848                let create = ast.create_view(index);
2849                let mut out = "CREATE".to_string();
2850                if create.or_replace {
2851                    out += " OR REPLACE";
2852                }
2853                if create.temporary {
2854                    out += " TEMPORARY";
2855                }
2856                out += " VIEW";
2857                if create.if_not_exists {
2858                    out += " IF NOT EXISTS";
2859                }
2860                out += &format!(" {}", ast.name_text(create.name));
2861                if !create.columns.is_empty() {
2862                    let columns = ast.name(create.columns).collect::<Vec<_>>().join(", ");
2863                    out += &format!(" ({columns})");
2864                }
2865                out + &format!(" AS {}", show_query(&ast, create.query))
2866            }
2867            Statement::DropTable(index) => {
2868                let drop = ast.drop_table(index);
2869                let mut out = if drop.view { "DROP VIEW" } else { "DROP TABLE" }.to_string();
2870                if drop.if_exists {
2871                    out += " IF EXISTS";
2872                }
2873                let names = ast
2874                    .name_list(drop.names)
2875                    .iter()
2876                    .map(|&name| ast.name_text(name))
2877                    .collect::<Vec<_>>()
2878                    .join(", ");
2879                out + &format!(" {names}")
2880            }
2881            Statement::Insert(index) => {
2882                let insert = ast.insert(index);
2883                let mut out = format!("INSERT INTO {}", ast.name_text(insert.name));
2884                if !insert.columns.is_empty() {
2885                    let columns = ast.name(insert.columns).collect::<Vec<_>>().join(", ");
2886                    out += &format!(" ({columns})");
2887                }
2888                out + &format!(" {}", show_query(&ast, insert.source))
2889            }
2890            Statement::Set(index) => {
2891                let setting = ast.setting(index);
2892                let scope = match setting.scope.keyword() {
2893                    "" => String::new(),
2894                    word => format!(" {word}"),
2895                };
2896                format!("SET{scope} {} = {}", ast.string(setting.name), show(&ast, setting.value))
2897            }
2898            Statement::Reset(index) => {
2899                let setting = ast.setting(index);
2900                let scope = match setting.scope.keyword() {
2901                    "" => String::new(),
2902                    word => format!(" {word}"),
2903                };
2904                format!("RESET{scope} {}", ast.string(setting.name))
2905            }
2906            Statement::Explain { query, analyze } => {
2907                let analyze = if analyze { "ANALYZE " } else { "" };
2908                format!("EXPLAIN {analyze}{}", show_query(&ast, query))
2909            }
2910        }
2911    }
2912
2913    #[test]
2914    fn an_explain_keeps_the_query_it_was_asked_about() {
2915        assert_eq!(
2916            round_statement("EXPLAIN SELECT a FROM t WHERE a > 1"),
2917            "EXPLAIN SELECT a FROM t WHERE (a Gt 1)"
2918        );
2919        assert_eq!(round_statement("explain select 1"), "EXPLAIN SELECT 1");
2920        assert_eq!(round_statement("explain analyze select 1"), "EXPLAIN ANALYZE SELECT 1");
2921    }
2922
2923    #[test]
2924    fn the_parts_of_an_explain_that_are_not_the_query_are_refused_by_name() {
2925        // An option list that chooses a format is a promise about the output this does not keep,
2926        // and a statement that is not a query has no plan to show.
2927        for (query, named) in [
2928            ("EXPLAIN (FORMAT JSON) SELECT 1", "ExplainOptionList"),
2929            ("EXPLAIN INSERT INTO t VALUES (1)", "InsertStatement"),
2930            ("EXPLAIN CREATE TABLE u (a INTEGER)", "CreateStatement"),
2931        ] {
2932            let error = parse_ast(query).expect_err(query).to_string();
2933            assert!(error.contains(named), "{query}: {error}");
2934        }
2935    }
2936
2937    #[test]
2938    fn a_set_keeps_its_name_its_scope_and_its_value() {
2939        assert_eq!(round_statement("SET memory_limit = '1GB'"), "SET memory_limit = '1GB'");
2940        assert_eq!(round_statement("set threads=4"), "SET threads = 4");
2941        assert_eq!(round_statement("SET GLOBAL threads = 4"), "SET GLOBAL threads = 4");
2942        assert_eq!(round_statement("SET SESSION threads = 4"), "SET SESSION threads = 4");
2943        assert_eq!(round_statement("SET LOCAL threads = 4"), "SET LOCAL threads = 4");
2944        assert_eq!(round_statement("RESET memory_limit"), "RESET memory_limit");
2945        assert_eq!(round_statement("RESET GLOBAL memory_limit"), "RESET GLOBAL memory_limit");
2946    }
2947
2948    #[test]
2949    fn the_two_other_things_the_word_set_starts_are_refused_rather_than_read_as_settings() {
2950        // `SET VARIABLE x = 1` declares a session variable and `SET SCHEMA` picks where an
2951        // unqualified name is looked up. Neither is a knob on the engine and reading either as one
2952        // would change an answer quietly.
2953        for statement in ["SET VARIABLE x = 1", "SET SCHEMA 'main'", "SET TIME ZONE 'UTC'"] {
2954            let error = parse_ast(statement).expect_err(statement);
2955            assert_eq!(error.code().duckdb_name(), "Not implemented Error", "{statement}");
2956        }
2957    }
2958
2959    #[test]
2960    fn a_setting_written_with_a_list_of_values_is_refused_rather_than_taking_the_first() {
2961        let error = parse_ast("SET search_path = a, b").expect_err("a list of two");
2962        assert_eq!(error.code().duckdb_name(), "Not implemented Error");
2963    }
2964
2965    #[test]
2966    fn the_query_m0_has_to_run_transforms() {
2967        assert_eq!(round("SELECT * FROM t WHERE x > 5"), "SELECT * FROM t WHERE (x Gt 5)");
2968    }
2969
2970    #[test]
2971    fn a_replace_list_rides_on_the_star_it_changes() {
2972        // The parentheses are optional around a single entry, which is how the clickbench load
2973        // recipe is not written but is how a lot of hand written sql is.
2974        assert_eq!(
2975            round("SELECT * REPLACE (a + 1 AS a) FROM t"),
2976            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
2977        );
2978        assert_eq!(
2979            round("SELECT * REPLACE a + 1 AS a FROM t"),
2980            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
2981        );
2982        assert_eq!(
2983            round("SELECT t.* REPLACE (make_date(a) AS a, b * 2 AS b) FROM t"),
2984            "SELECT t.* REPLACE (make_date(a) AS a, (b Multiply 2) AS b) FROM t"
2985        );
2986    }
2987
2988    #[test]
2989    fn one_column_cannot_be_replaced_twice() {
2990        // Caught here rather than in the binder because it is a mistake in what was written and
2991        // not a mistake about what is in the table, and duckdb reports it the same way.
2992        let error = parse_ast("SELECT * REPLACE (a + 1 AS a, a + 2 AS A) FROM t").unwrap_err();
2993        assert_eq!(error.to_string(), "Parser Error: Duplicate entry \"A\" in REPLACE list");
2994    }
2995
2996    #[test]
2997    fn a_table_function_argument_can_have_a_name_written_in_front_of_it() {
2998        // The grammar has `:=` and `=>`. It does not have `=`, which parses as a comparison and is
2999        // read back apart here, and that is the spelling the clickbench load recipe uses.
3000        for spelling in
3001            ["binary_as_string := True", "binary_as_string => True", "binary_as_string = True"]
3002        {
3003            assert_eq!(
3004                round(&format!("SELECT * FROM read_parquet('f.parquet', {spelling})")),
3005                "SELECT * FROM read_parquet('f.parquet', binary_as_string := TRUE)",
3006                "{spelling}"
3007            );
3008        }
3009    }
3010
3011    #[test]
3012    fn an_equality_that_is_not_a_bare_name_stays_an_argument() {
3013        // A qualified name on the left is not a parameter name, and neither is anything that is
3014        // not a name at all, so both of those stay the comparison they were written as.
3015        assert_eq!(round("SELECT * FROM f(t.a = 1)"), "SELECT * FROM f((t.a Eq 1))");
3016        assert_eq!(round("SELECT * FROM f(1 = 1)"), "SELECT * FROM f((1 Eq 1))");
3017    }
3018
3019    #[test]
3020    fn a_create_table_keeps_its_types_as_text() {
3021        assert_eq!(
3022            round_statement("CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"),
3023            "CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"
3024        );
3025        // The type is the text between the identifier and whatever follows it, parentheses and
3026        // all, because resolving `DECIMAL(18, 3)` into a width and a scale is the binder's job and
3027        // doing it here would mean two places that know the type table.
3028        assert_eq!(
3029            round_statement("CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"),
3030            "CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"
3031        );
3032    }
3033
3034    #[test]
3035    fn the_modifiers_on_a_create_table_survive() {
3036        assert_eq!(
3037            round_statement("CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"),
3038            "CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"
3039        );
3040        assert_eq!(
3041            round_statement("CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"),
3042            "CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"
3043        );
3044    }
3045
3046    #[test]
3047    fn or_replace_and_if_not_exists_in_one_statement_is_refused_here_and_not_later() {
3048        // The grammar has room for both and duckdb's has not, so its refusal is a parser error with
3049        // a caret under the `NOT` and this one is a parser error at the same stage. It is the same
3050        // sentence whatever is being created.
3051        for sql in [
3052            "CREATE OR REPLACE TABLE IF NOT EXISTS t (a INT)",
3053            "CREATE OR REPLACE VIEW IF NOT EXISTS v AS SELECT 1",
3054        ] {
3055            let error = parse_ast(sql).unwrap_err().to_string();
3056            assert_eq!(
3057                error,
3058                "Parser Error: Cannot specify both OR REPLACE and IF NOT EXISTS within single \
3059                 create statement"
3060            );
3061        }
3062    }
3063
3064    #[test]
3065    fn a_create_table_as_carries_the_query_and_not_the_types() {
3066        assert_eq!(
3067            round_statement("CREATE TABLE t AS SELECT a FROM u"),
3068            "CREATE TABLE t AS SELECT a FROM u"
3069        );
3070        // The names are the syntax's to say and the types are the query's, so the column
3071        // definitions here have names and no types.
3072        assert_eq!(
3073            round_statement("CREATE TABLE t (x, y) AS SELECT a, b FROM u"),
3074            "CREATE TABLE t (x, y) AS SELECT a, b FROM u"
3075        );
3076    }
3077
3078    #[test]
3079    fn a_create_view_carries_its_body_twice_over() {
3080        assert_eq!(
3081            round_statement("CREATE VIEW v AS SELECT a FROM u"),
3082            "CREATE VIEW v AS SELECT a FROM u"
3083        );
3084        assert_eq!(
3085            round_statement("CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"),
3086            "CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"
3087        );
3088        // The text the catalog keeps is the body and only the body, so that binding it again is
3089        // binding a query rather than a `CREATE` statement.
3090        let ast = parse_ast("CREATE VIEW v (x) AS SELECT a FROM u WHERE a > 1").expect("parses");
3091        let Statement::CreateView(index) = ast.statements[0] else {
3092            panic!("not a create view");
3093        };
3094        assert_eq!(ast.string(ast.create_view(index).sql), "SELECT a FROM u WHERE a > 1");
3095    }
3096
3097    #[test]
3098    fn a_drop_view_is_not_a_drop_table() {
3099        assert_eq!(round_statement("DROP VIEW IF EXISTS a, b"), "DROP VIEW IF EXISTS a, b");
3100        assert_eq!(round_statement("DROP TABLE a"), "DROP TABLE a");
3101    }
3102
3103    #[test]
3104    fn a_drop_table_is_a_list_of_qualified_names() {
3105        assert_eq!(round_statement("DROP TABLE t"), "DROP TABLE t");
3106        assert_eq!(round_statement("DROP TABLE IF EXISTS a, b.c"), "DROP TABLE IF EXISTS a, b.c");
3107    }
3108
3109    #[test]
3110    fn dropping_something_that_is_neither_a_table_nor_a_view_is_refused() {
3111        // `TableOrView` covers `MATERIALIZED VIEW` as well, which is not a thing this database has,
3112        // and dropping one as if it were an ordinary view is a wrong answer rather than a missing
3113        // feature.
3114        let error = parse_ast("DROP MATERIALIZED VIEW v").unwrap_err().to_string();
3115        assert!(error.starts_with("Not implemented Error"), "{error}");
3116    }
3117
3118    #[test]
3119    fn both_spellings_of_insert_arrive_at_a_query() {
3120        assert_eq!(
3121            round_statement("INSERT INTO t VALUES (1, 'a'), (2, 'b')"),
3122            "INSERT INTO t VALUES (1, 'a'), (2, 'b')"
3123        );
3124        assert_eq!(
3125            round_statement("INSERT INTO t (a, b) SELECT x, y FROM u"),
3126            "INSERT INTO t (a, b) SELECT x, y FROM u"
3127        );
3128    }
3129
3130    #[test]
3131    fn an_insert_clause_that_changes_the_answer_is_refused() {
3132        for query in [
3133            "INSERT INTO t VALUES (1) RETURNING *",
3134            "INSERT OR REPLACE INTO t VALUES (1)",
3135            "INSERT INTO t BY NAME SELECT 1 AS a",
3136            "INSERT INTO t VALUES (1) ON CONFLICT DO NOTHING",
3137            "INSERT INTO t DEFAULT VALUES",
3138        ] {
3139            let error = parse_ast(query).unwrap_err().to_string();
3140            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
3141        }
3142    }
3143
3144    #[test]
3145    fn a_column_constraint_that_is_not_not_null_is_refused() {
3146        // Nothing enforces a constraint yet. Accepting one and not enforcing it is the wrong
3147        // answer, so `NOT NULL` is kept because the column already has a nullability and the rest
3148        // are refused until there is somewhere to put them.
3149        for query in [
3150            "CREATE TABLE t (a INT PRIMARY KEY)",
3151            "CREATE TABLE t (a INT UNIQUE)",
3152            "CREATE TABLE t (a INT CHECK (a > 0))",
3153            "CREATE TABLE t (a INT DEFAULT 1)",
3154            "CREATE TABLE t (a INT REFERENCES u (b))",
3155            "CREATE TABLE t (a INT, PRIMARY KEY (a))",
3156        ] {
3157            let error = parse_ast(query).unwrap_err().to_string();
3158            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
3159        }
3160    }
3161
3162    #[test]
3163    fn values_is_a_query_on_its_own_and_in_a_from() {
3164        assert_eq!(round("VALUES (1), (2)"), "VALUES (1), (2)");
3165        // Parenthesised it is a subquery whose body is the values, and bare it is a `ValuesRef`.
3166        // Two rules and one meaning, which is the grammar's doing and not something to flatten
3167        // here, because the parenthesised form can carry an order by and the bare one cannot.
3168        assert_eq!(
3169            round("SELECT * FROM (VALUES (1, 2), (3, 4)) t(a, b)"),
3170            "SELECT * FROM (VALUES (1, 2), (3, 4)) AS t"
3171        );
3172        assert_eq!(
3173            round("SELECT * FROM VALUES (1, 2), (3, 4) AS t(a, b)"),
3174            "SELECT * FROM VALUES (1, 2), (3, 4) AS t"
3175        );
3176        // Rows of different widths parse. Saying so wants the column count, which for an insert is
3177        // the table's, so the check belongs to the binder and not here.
3178        assert_eq!(round("VALUES (1), (2, 3)"), "VALUES (1), (2, 3)");
3179    }
3180
3181    /// `DESCRIBE` is a query body, and the two spellings that name something become a star over it.
3182    ///
3183    /// Naming a table is not a shortcut for the query. On the reference binary `DESCRIBE t` and
3184    /// `DESCRIBE SELECT * FROM t` print the same six columns and the same rows, down to the `NO` on
3185    /// a column that refuses nulls, so rewriting one into the other costs nothing and leaves the
3186    /// binder with one case instead of three. A file name goes down the same path as a table name
3187    /// because a bare string in a `FROM` clause is already a name the replacement scan picks up.
3188    #[test]
3189    fn describe_rewrites_a_name_into_a_star_over_it() {
3190        assert_eq!(round("DESCRIBE SELECT 1 AS a"), "DESCRIBE SELECT 1 AS a");
3191        assert_eq!(round("DESCRIBE t"), "DESCRIBE SELECT * FROM t");
3192        assert_eq!(round("DESC t"), "DESCRIBE SELECT * FROM t");
3193        assert_eq!(round("DESCRIBE 'x.parquet'"), "DESCRIBE SELECT * FROM x.parquet");
3194        // A body and not a statement kind, so it nests both ways with no rule of its own.
3195        assert_eq!(
3196            round("SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"),
3197            "SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"
3198        );
3199        assert_eq!(round("DESCRIBE DESCRIBE SELECT 1 AS a"), "DESCRIBE DESCRIBE SELECT 1 AS a");
3200    }
3201
3202    /// `SUMMARIZE` shares both of `DESCRIBE`'s grammar rules and is a different statement.
3203    ///
3204    /// It reads every row and returns one row per column carrying the min, the max, the count and
3205    /// the approximate distinct count, so none of it falls out of the `DESCRIBE` path. The word is
3206    /// the only thing in the tree that tells the two apart, which is why the transform looks at it
3207    /// rather than trusting the rule name it arrived under.
3208    #[test]
3209    fn summarize_is_refused_even_though_it_parses_as_a_describe() {
3210        for query in ["SUMMARIZE t", "SUMMARIZE SELECT 1"] {
3211            let error = parse_ast(query).expect_err("summarize is not implemented");
3212            let message = error.to_string();
3213            assert!(message.starts_with("Not implemented Error"), "{query} failed with {message}");
3214        }
3215    }
3216
3217    #[test]
3218    fn every_statement_in_the_corpus_gets_a_defined_answer() {
3219        // The point of the test is the word defined. Half of these are statement kinds and
3220        // clauses this milestone does not cover, and the requirement is not that they work, it is
3221        // that they fail by saying so. A panic, a silently dropped clause or an internal error
3222        // would each be a different bug and all three would be invisible without this.
3223        let mut done = 0;
3224        for query in CORPUS {
3225            match parse_ast(query) {
3226                Ok(ast) => {
3227                    assert_eq!(ast.statements.len(), 1, "{query}");
3228                    done += 1;
3229                }
3230                Err(error) => {
3231                    let message = error.to_string();
3232                    assert!(
3233                        message.starts_with("Not implemented Error"),
3234                        "{query} failed with {message}, which is not a not-implemented error"
3235                    );
3236                }
3237            }
3238        }
3239        // Not an assertion about the right number. It is a ratchet: this only moves up, and the
3240        // day it moves down somebody has taken a construct out without meaning to.
3241        assert!(done >= 31, "only {done} of the corpus transforms, which is fewer than it was");
3242    }
3243
3244    #[test]
3245    fn the_ast_is_far_smaller_than_the_parse_tree() {
3246        let query = CORPUS[4];
3247        let tree = parse(query).unwrap();
3248        let ast = parse_ast(query).unwrap();
3249        // The twenty precedence levels are the difference. Every one of them is a node in the
3250        // parse tree for every expression at every depth, and none of them survives into the AST.
3251        assert!(
3252            ast.node_count() * 20 < tree.arena_len(),
3253            "{} ast nodes against {} parse nodes",
3254            ast.node_count(),
3255            tree.arena_len()
3256        );
3257    }
3258
3259    #[test]
3260    fn precedence_comes_out_of_the_chain_and_into_the_tree() {
3261        assert_eq!(round("SELECT 1 + 2 * 3"), "SELECT (1 Add (2 Multiply 3))");
3262        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
3263        assert_eq!(round("SELECT 1 + 2 + 3"), "SELECT ((1 Add 2) Add 3)");
3264        assert_eq!(round("SELECT 1 - 2 - 3"), "SELECT ((1 Subtract 2) Subtract 3)");
3265        assert_eq!(
3266            round("SELECT a OR b AND c"),
3267            "SELECT (a Or (b And c))",
3268            "and binds tighter than or"
3269        );
3270    }
3271
3272    #[test]
3273    fn a_double_negation_is_two_nodes_and_not_none() {
3274        // Folding it would be an optimizer decision and this is not the optimizer. It also would
3275        // not be safe in general: `NOT NOT x` on a null is still null and on a non boolean it is
3276        // still an error, and both of those have to survive to the binder to be reported.
3277        assert_eq!(round("SELECT NOT NOT a"), "SELECT (Not (Not a))");
3278    }
3279
3280    #[test]
3281    fn a_parenthesised_single_expression_is_not_a_row() {
3282        assert_eq!(round("SELECT (a)"), "SELECT a");
3283        assert_eq!(round("SELECT (a, b)"), "SELECT ROW(a, b)");
3284    }
3285
3286    #[test]
3287    fn a_bracketed_list_is_a_list_of_however_many_items_were_written() {
3288        // One item is a list of one, which is where this parts company with the parenthesised form
3289        // above: `(a)` is `a` and `[a]` is a list, because the brackets are what say list.
3290        assert_eq!(round("SELECT [a]"), "SELECT [a]");
3291        assert_eq!(round("SELECT [1, 2, 3]"), "SELECT [1, 2, 3]");
3292        assert_eq!(round("SELECT []"), "SELECT []");
3293        assert_eq!(round("SELECT ['a.parquet', 'b.parquet']"), "SELECT ['a.parquet', 'b.parquet']");
3294    }
3295
3296    #[test]
3297    fn a_parameter_carries_its_identifier_however_it_was_written() {
3298        assert_eq!(round("SELECT $1"), "SELECT $1");
3299        assert_eq!(round("SELECT ?1"), "SELECT $1");
3300        assert_eq!(round("SELECT $name"), "SELECT $name");
3301        // A bare question mark is numbered by where it is, and the counting is its own, so a later
3302        // `$2` does not push the first one along. This is duckdb v1.4.1, which prints `$1 + $2`.
3303        assert_eq!(round("SELECT ? + $2"), "SELECT ($1 Add $2)");
3304        assert_eq!(round("SELECT ?, ?, ?"), "SELECT $1, $2, $3");
3305    }
3306
3307    #[test]
3308    fn the_parameters_of_a_statement_are_listed_once_each_in_written_order() {
3309        let ast = parse_ast("SELECT $b, $a, $b WHERE $a").expect("parses");
3310        assert_eq!(ast.parameters(), vec!["b", "a"]);
3311        assert!(parse_ast("SELECT 1").expect("parses").parameters().is_empty());
3312    }
3313
3314    #[test]
3315    fn the_three_ways_to_write_an_alias_all_arrive() {
3316        assert_eq!(round("SELECT a AS b"), "SELECT a AS b");
3317        assert_eq!(round("SELECT a b"), "SELECT a AS b");
3318        assert_eq!(round("SELECT b: a"), "SELECT a AS b");
3319        assert_eq!(round("SELECT a"), "SELECT a", "and no alias when none was written");
3320    }
3321
3322    #[test]
3323    fn a_from_with_no_select_selects_everything() {
3324        // DuckDB's own shorthand. Inventing the star here rather than in the binder means the
3325        // binder never has to know that the clause it is looking at was the one that was missing.
3326        assert_eq!(round("FROM t"), "SELECT * FROM t");
3327        assert_eq!(round("FROM t SELECT a"), "SELECT a FROM t");
3328    }
3329
3330    #[test]
3331    fn joins_nest_to_the_left() {
3332        assert_eq!(
3333            round("SELECT * FROM a JOIN b ON a.i = b.i LEFT JOIN c USING (k)"),
3334            "SELECT * FROM ((a Inner JOIN b ON (a.i Eq b.i)) Left JOIN c USING (k))"
3335        );
3336        assert_eq!(
3337            round("SELECT * FROM a NATURAL JOIN b"),
3338            "SELECT * FROM (a NATURAL Inner JOIN b)"
3339        );
3340        assert_eq!(round("SELECT * FROM a CROSS JOIN b"), "SELECT * FROM (a Cross JOIN b)");
3341        assert_eq!(
3342            round("SELECT * FROM a POSITIONAL JOIN b"),
3343            "SELECT * FROM (a Positional JOIN b)"
3344        );
3345        assert_eq!(round("SELECT * FROM a, b"), "SELECT * FROM a, b", "a comma is not a join node");
3346    }
3347
3348    #[test]
3349    fn a_qualified_name_keeps_its_parts_however_it_was_spelled() {
3350        // Five grammar rules can produce a column reference and they disagree about which
3351        // component is a schema and which is a table. None of that is decidable without the
3352        // catalog, so the AST holds the parts and the binder decides.
3353        assert_eq!(round("SELECT a"), "SELECT a");
3354        assert_eq!(round("SELECT t.a"), "SELECT t.a");
3355        assert_eq!(round("SELECT s.t.a"), "SELECT s.t.a");
3356        assert_eq!(round("SELECT c.s.t.a"), "SELECT c.s.t.a");
3357        assert_eq!(round("SELECT * FROM s.t"), "SELECT * FROM s.t");
3358    }
3359
3360    #[test]
3361    fn a_star_can_be_qualified() {
3362        assert_eq!(round("SELECT *"), "SELECT *");
3363        assert_eq!(round("SELECT t.*"), "SELECT t.*");
3364        assert_eq!(round("SELECT s.t.*"), "SELECT s.t.*");
3365    }
3366
3367    #[test]
3368    fn a_quoted_identifier_keeps_its_case_and_loses_its_quotes() {
3369        // DuckDB does not fold identifier case at any point, quoted or not, which the tokenizer
3370        // work established by reading the source. So the only thing to do here is take the quotes
3371        // off and resolve the doubled ones.
3372        let ast = parse_ast("SELECT \"Mixed Case\", \"a\"\"b\"").unwrap();
3373        assert_eq!(ast.strings[0], "Mixed Case");
3374        assert_eq!(ast.strings[1], "a\"b");
3375    }
3376
3377    #[test]
3378    fn a_string_literal_is_decoded_and_adjacent_ones_are_joined() {
3379        assert_eq!(round("SELECT 'it''s'"), "SELECT 'it's'");
3380        assert_eq!(round("SELECT 'a'\n'b'"), "SELECT 'ab'", "the standard's adjacency rule");
3381    }
3382
3383    /// Per #276, where the tag and the dollars were coming through as part of the value.
3384    #[test]
3385    fn a_dollar_quoted_string_loses_its_dollars_and_its_tag() {
3386        assert_eq!(round("SELECT $$dollar quoted$$"), "SELECT 'dollar quoted'");
3387        assert_eq!(round("SELECT $tag$body$tag$"), "SELECT 'body'");
3388        assert_eq!(round("SELECT $$$$"), "SELECT ''", "an empty tag and an empty body");
3389        // Nothing in the body is escaped, which is what the spelling is for, so a quote is a quote
3390        // and a dollar that is not the closing tag is a dollar.
3391        assert_eq!(round("SELECT $tag$it''s $other$ fine$tag$"), "SELECT 'it''s $other$ fine'");
3392        // An unterminated one has no closing tag to take off and keeps every byte it was given.
3393        assert_eq!(round("SELECT $$open"), "SELECT '$$open'");
3394    }
3395
3396    /// Per #329, where every prefixed spelling came back as the source text it was written as.
3397    ///
3398    /// The escapes are the ones the pinned binary takes, read off it one at a time. The two that
3399    /// are easy to get wrong are `\v`, which is not an escape and is the letter, and `\u`, which
3400    /// wants all four digits and otherwise drops the backslash and keeps the letter.
3401    #[test]
3402    fn an_escape_string_resolves_its_backslashes() {
3403        assert_eq!(round("SELECT E'a\\nb'"), "SELECT 'a\nb'");
3404        assert_eq!(round("SELECT e'a\\tb'"), "SELECT 'a\tb'", "the prefix is a letter, not a name");
3405        assert_eq!(round("SELECT E'a\\rb'"), "SELECT 'a\rb'");
3406        assert_eq!(round("SELECT E'a\\bb'"), "SELECT 'a\u{8}b'");
3407        assert_eq!(round("SELECT E'a\\fb'"), "SELECT 'a\u{c}b'");
3408        assert_eq!(round("SELECT E'a\\\\b'"), "SELECT 'a\\b'");
3409        assert_eq!(round("SELECT E'a\\'b'"), "SELECT 'a'b'", "a quote, the same as ''");
3410        assert_eq!(round("SELECT E'a''b'"), "SELECT 'a'b'", "and '' still means a quote here");
3411        // A backslash in front of anything else is dropped and the character is kept, which is what
3412        // makes \v the letter v.
3413        assert_eq!(round("SELECT E'a\\vb'"), "SELECT 'avb'");
3414        assert_eq!(round("SELECT E'a\\qb'"), "SELECT 'aqb'");
3415    }
3416
3417    /// The escapes that write a byte rather than a character, and the one that writes a character.
3418    #[test]
3419    fn a_numeric_escape_writes_the_byte_or_the_character_it_names() {
3420        assert_eq!(round("SELECT E'\\x41'"), "SELECT 'A'");
3421        assert_eq!(round("SELECT E'\\x4142'"), "SELECT 'A42'", "two digits at the most");
3422        assert_eq!(
3423            round("SELECT E'a\\x'"),
3424            "SELECT 'ax'",
3425            "and one at the least, or it is a letter"
3426        );
3427        assert_eq!(round("SELECT E'\\101'"), "SELECT 'A'");
3428        assert_eq!(round("SELECT E'\\1011'"), "SELECT 'A1'", "three digits at the most");
3429        assert_eq!(round("SELECT E'\\8'"), "SELECT '8'", "8 is not an octal digit");
3430        // Bytes and not characters, so two of them make one character and one of them makes none.
3431        assert_eq!(round("SELECT E'\\xc3\\xa9'"), "SELECT 'é'");
3432        assert_eq!(round("SELECT E'\\u00e9'"), "SELECT 'é'");
3433        assert_eq!(round("SELECT E'a\\u41'"), "SELECT 'au41'", "four digits or it is a letter");
3434        assert_eq!(round("SELECT E'a\\uZZZZ'"), "SELECT 'auZZZZ'");
3435        assert_eq!(
3436            round("SELECT E'\\ud83d\\ude00'"),
3437            "SELECT 'ud83dude00'",
3438            "surrogates are not it"
3439        );
3440    }
3441
3442    /// The two ways an escape string is not a string at all, both with the message upstream gives.
3443    #[test]
3444    fn an_escape_string_that_is_not_a_string_raises() {
3445        let error = parse_ast("SELECT E'a\\x00'").unwrap_err().to_string();
3446        assert_eq!(error, "Parser Error: Null character not permitted in escape string literal");
3447        let error = parse_ast("SELECT E'a\\377'").unwrap_err().to_string();
3448        assert_eq!(
3449            error,
3450            "Parser Error: Invalid UTF-8 in escape string literal at byte offset 1: byte mismatch",
3451            "the offset is where the bytes stop being a string, not where the escape was written"
3452        );
3453    }
3454
3455    /// The other prefixes, all of them measured against the pinned binary rather than assumed.
3456    #[test]
3457    fn the_other_string_prefixes_are_what_upstream_makes_of_them() {
3458        // N is the string and a cast of it to VARCHAR, which is where the column name comes from.
3459        assert_eq!(round("SELECT N'abc'"), "SELECT CAST('abc' AS VARCHAR)");
3460        assert_eq!(round("SELECT n'abc'"), "SELECT CAST('abc' AS VARCHAR)");
3461        // B is not a bit string. It is the letter b in front of the body, untouched.
3462        assert_eq!(round("SELECT B'101'"), "SELECT 'b101'");
3463        assert_eq!(round("SELECT b'abc'"), "SELECT 'babc'");
3464        assert_eq!(round("SELECT B''"), "SELECT 'b'", "an empty one is the letter on its own");
3465    }
3466
3467    /// X is the prefix that is not a string at all, per #329.
3468    ///
3469    /// What is kept is the text the blob prints as, because that is the text the column is named
3470    /// after and the text the cast reads the bytes back from, and one text that does both is one
3471    /// text that cannot disagree with itself.
3472    #[test]
3473    fn a_hex_string_is_a_blob_and_not_a_string() {
3474        assert_eq!(round("SELECT x'4142'"), "SELECT 'AB'::BLOB");
3475        assert_eq!(round("SELECT X'4142'"), "SELECT 'AB'::BLOB");
3476        assert_eq!(round("SELECT x'ff41'"), "SELECT '\\xFFA'::BLOB", "a byte that does not print");
3477        assert_eq!(round("SELECT x''"), "SELECT ''::BLOB", "an empty one is an empty blob");
3478        // A quote and a backslash are bytes that do not print either, which is what keeps the text
3479        // something the cast can read back.
3480        assert_eq!(round("SELECT x'2741'"), "SELECT '\\x27A'::BLOB");
3481        assert_eq!(round("SELECT x'5c7834314141'"), "SELECT '\\x5Cx41AA'::BLOB");
3482        // An odd number of digits is a parser error and a digit that is not one is not, because
3483        // upstream writes the pairs out without looking at them and the cast is what looks.
3484        let error = parse_ast("SELECT x'4'").unwrap_err().to_string();
3485        assert_eq!(
3486            error,
3487            "Parser Error: Hex string literal must have an even number of hex digits"
3488        );
3489        assert_eq!(round("SELECT x'41zz'"), "SELECT '\\x41\\xzz'::BLOB");
3490    }
3491
3492    #[test]
3493    fn the_null_and_boolean_tests_are_postfix_unary_operators() {
3494        assert_eq!(round("SELECT x IS NULL"), "SELECT (IsNull x)");
3495        assert_eq!(round("SELECT x IS NOT NULL"), "SELECT (IsNotNull x)");
3496        assert_eq!(round("SELECT x ISNULL"), "SELECT (IsNull x)");
3497        assert_eq!(round("SELECT x NOTNULL"), "SELECT (IsNotNull x)");
3498        assert_eq!(round("SELECT x IS TRUE"), "SELECT (IsTrue x)");
3499        assert_eq!(round("SELECT x IS NOT FALSE"), "SELECT (IsNotFalse x)");
3500        assert_eq!(round("SELECT x IS DISTINCT FROM y"), "SELECT (x IsDistinctFrom y)");
3501        assert_eq!(round("SELECT x IS NOT DISTINCT FROM y"), "SELECT (x IsNotDistinctFrom y)");
3502    }
3503
3504    #[test]
3505    fn the_like_family_folds_its_negation_into_the_operator() {
3506        assert_eq!(round("SELECT x LIKE 'a'"), "SELECT (x Like 'a')");
3507        assert_eq!(round("SELECT x NOT LIKE 'a'"), "SELECT (x NotLike 'a')");
3508        assert_eq!(round("SELECT x ILIKE 'a'"), "SELECT (x ILike 'a')");
3509        assert_eq!(round("SELECT x ~~ 'a'"), "SELECT (x Like 'a')", "the operator spelling");
3510        assert_eq!(round("SELECT x !~~ 'a'"), "SELECT (x NotLike 'a')");
3511        assert_eq!(round("SELECT x SIMILAR TO 'a'"), "SELECT (x SimilarTo 'a')");
3512        // Glob has no negated operator to fold into, so the negation stays where it was written.
3513        assert_eq!(round("SELECT x NOT GLOB 'a'"), "SELECT (Not (x Glob 'a'))");
3514    }
3515
3516    #[test]
3517    fn between_and_in_carry_their_negation_as_a_flag() {
3518        assert_eq!(round("SELECT x BETWEEN 1 AND 2"), "SELECT (x BETWEEN 1 AND 2)");
3519        assert_eq!(round("SELECT x NOT BETWEEN 1 AND 2"), "SELECT (NOT x BETWEEN 1 AND 2)");
3520        assert_eq!(round("SELECT x IN (1, 2)"), "SELECT (x IN [1, 2])");
3521        assert_eq!(round("SELECT x NOT IN (1, 2)"), "SELECT (NOT x IN [1, 2])");
3522    }
3523
3524    #[test]
3525    fn both_spellings_of_a_cast_are_the_same_node() {
3526        assert_eq!(round("SELECT CAST(x AS BIGINT)"), "SELECT CAST(x AS BIGINT)");
3527        assert_eq!(round("SELECT x::BIGINT"), "SELECT CAST(x AS BIGINT)");
3528        assert_eq!(round("SELECT TRY_CAST(x AS BIGINT)"), "SELECT TRY_CAST(x AS BIGINT)");
3529        assert_eq!(
3530            round("SELECT x::DECIMAL(18, 3)"),
3531            "SELECT CAST(x AS DECIMAL(18, 3))",
3532            "the type is kept as text because parsing it is the type system's job"
3533        );
3534    }
3535
3536    #[test]
3537    fn a_typed_literal_is_a_third_spelling_of_the_same_cast() {
3538        assert_eq!(round("SELECT DATE '1995-09-01'"), "SELECT CAST('1995-09-01' AS DATE)");
3539        assert_eq!(
3540            round("SELECT date '1995-09-01'"),
3541            "SELECT CAST('1995-09-01' AS date)",
3542            "the type is kept as written, the same as it is in the other two spellings"
3543        );
3544        assert_eq!(
3545            round("SELECT TIMESTAMP '2020-01-01 03:04:05'"),
3546            "SELECT CAST('2020-01-01 03:04:05' AS TIMESTAMP)"
3547        );
3548        assert_eq!(
3549            round("SELECT DECIMAL(5, 2) '1.5'"),
3550            "SELECT CAST('1.5' AS DECIMAL(5, 2))",
3551            "any type the cast takes is a typed literal, parameters and all"
3552        );
3553        assert_eq!(
3554            round("SELECT VARCHAR 'hi' FROM t"),
3555            "SELECT CAST('hi' AS VARCHAR) FROM t",
3556            "including the ones where the cast has nothing to do"
3557        );
3558    }
3559
3560    #[test]
3561    fn a_case_keeps_its_arms_in_order() {
3562        assert_eq!(
3563            round("SELECT CASE WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"),
3564            "SELECT CASE - WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"
3565        );
3566        assert_eq!(
3567            round("SELECT CASE x WHEN 1 THEN 'a' END"),
3568            "SELECT CASE x WHEN 1 THEN 'a' ELSE - END",
3569            "a simple case keeps the operand and a missing else is not an implicit null yet"
3570        );
3571    }
3572
3573    #[test]
3574    fn a_field_access_and_a_method_call_are_ordinary_function_calls() {
3575        // Which is what DuckDB makes of them too. Giving each its own AST node would mean the
3576        // binder needs a rule for something the function resolver already handles.
3577        assert_eq!(round("SELECT (f(x)).y"), "SELECT struct_extract(f(x), 'y')");
3578        assert_eq!(round("SELECT a[1]"), "SELECT array_extract(a, 1)");
3579    }
3580
3581    /// The four ways of leaving a bound out, all of which upstream fills in the same way.
3582    #[test]
3583    fn a_range_gets_the_bounds_the_query_left_out() {
3584        assert_eq!(round("SELECT a[1:2]"), "SELECT array_slice(a, 1, 2)");
3585        assert_eq!(round("SELECT a[:2]"), "SELECT array_slice(a, 1, 2)");
3586        assert_eq!(round("SELECT a[2:]"), "SELECT array_slice(a, 2, -1)");
3587        assert_eq!(round("SELECT a[:]"), "SELECT array_slice(a, 1, -1)");
3588        // `EndSliceMinus`, which is a range with no end rather than a subtraction of nothing.
3589        assert_eq!(round("SELECT a[1:-]"), "SELECT array_slice(a, 1, -1)");
3590        assert_eq!(round("SELECT a[1:2:3]"), "SELECT array_slice(a, 1, 2, 3)");
3591        // A step that was written and left empty, which upstream fills with a list so that the call
3592        // fails to bind. Answering a row here would be answering where the reference refuses.
3593        assert_eq!(round("SELECT a[1:2:]"), "SELECT array_slice(a, 1, 2, [])");
3594    }
3595
3596    /// `[]` is the one subscript the parser takes and the transformer refuses, in upstream's words.
3597    #[test]
3598    fn an_empty_subscript_is_not_a_subscript() {
3599        let error = parse_ast("SELECT a[]").expect_err("an empty subscript");
3600        assert_eq!(error.message(), "Empty subscript '[]' is not allowed");
3601    }
3602
3603    /// A rule that wrote a keyword is not a rule that said nothing, however few children it has.
3604    /// Per #313.
3605    #[test]
3606    fn a_keyword_is_not_stepped_through_on_the_way_to_its_one_argument() {
3607        for (sql, rule) in [
3608            ("SELECT row(1)", "RowExpression"),
3609            ("SELECT try(1)", "TryExpression"),
3610            ("SELECT unpack([1])", "UnpackExpression"),
3611            ("SELECT columns('a')", "ColumnsExpression"),
3612        ] {
3613            let error = parse_ast(sql).expect_err(sql);
3614            assert!(error.message().ends_with(rule), "{sql}: {error}");
3615        }
3616        // Grouping brackets really do say nothing, and that is the one rule of this shape that is
3617        // stepped through rather than refused.
3618        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
3619        assert_eq!(round("SELECT -(7)"), "SELECT (Negate 7)");
3620    }
3621
3622    /// The three spellings of a null check, two of which are their own grammar rule. Per #306.
3623    #[test]
3624    fn the_null_checks_are_calls_by_the_names_duckdb_prints() {
3625        // The keyword is the name, so the call is written with the canonical spelling of it whichever
3626        // case the query used. What the column is called is the binder's to decide.
3627        assert_eq!(round("SELECT COALESCE(a, b, 1)"), "SELECT coalesce(a, b, 1)");
3628        assert_eq!(round("SELECT coalesce(a)"), "SELECT coalesce(a)");
3629        assert_eq!(round("SELECT NULLIF(a, 1)"), "SELECT nullif(a, 1)");
3630        // `IFNULL` is a plain call that upstream's parser turns into the operator, qualifier and all.
3631        assert_eq!(round("SELECT ifnull(a, 1)"), "SELECT coalesce(a, 1)");
3632        assert_eq!(round("SELECT main.ifnull(a, 1)"), "SELECT coalesce(a, 1)");
3633        let error = parse_ast("SELECT ifnull(a)").expect_err("one argument to ifnull");
3634        assert_eq!(error.message(), "Wrong number of arguments to IFNULL.");
3635        let error = parse_ast("SELECT ifnull(a, b, c)").expect_err("three arguments to ifnull");
3636        assert_eq!(error.message(), "Wrong number of arguments to IFNULL.");
3637    }
3638
3639    /// The four string functions with a grammar rule of their own, written back out as the calls
3640    /// DuckDB's parser writes them as. Per #314.
3641    #[test]
3642    fn the_string_keywords_are_the_calls_duckdb_prints() {
3643        assert_eq!(round("SELECT substring(s, 2, 3)"), "SELECT substring(s, 2, 3)");
3644        assert_eq!(round("SELECT SUBSTRING(s FROM 2 FOR 3)"), "SELECT substring(s, 2, 3)");
3645        assert_eq!(round("SELECT substring(s FROM 2)"), "SELECT substring(s, 2)");
3646        // The `FOR` on its own is three arguments and not two, with the start filled in.
3647        assert_eq!(round("SELECT substring(s FOR 3)"), "SELECT substring(s, 1, 3)");
3648        // The haystack comes first in the call and second in the query.
3649        assert_eq!(round("SELECT position('c' IN s)"), "SELECT position(s, 'c')");
3650        assert_eq!(round("SELECT trim(s)"), "SELECT trim(s)");
3651        assert_eq!(round("SELECT trim(BOTH 'x' FROM s)"), "SELECT trim(s, 'x')");
3652        assert_eq!(round("SELECT trim(BOTH FROM s)"), "SELECT trim(s)");
3653        assert_eq!(round("SELECT trim(s, 'xy')"), "SELECT trim(s, 'xy')");
3654        // A direction is a different function and not a different argument.
3655        assert_eq!(round("SELECT trim(LEADING FROM s)"), "SELECT ltrim(s)");
3656        assert_eq!(round("SELECT trim(TRAILING FROM s)"), "SELECT rtrim(s)");
3657        assert_eq!(round("SELECT trim(LEADING 'x' FROM s)"), "SELECT ltrim(s, 'x')");
3658        assert_eq!(round("SELECT trim(TRAILING 'x' FROM s)"), "SELECT rtrim(s, 'x')");
3659        assert_eq!(
3660            round("SELECT overlay(s PLACING 'X' FROM 2 FOR 1)"),
3661            "SELECT overlay(s, 'X', 2, 1)"
3662        );
3663        assert_eq!(round("SELECT overlay(s PLACING 'X' FROM 2)"), "SELECT overlay(s, 'X', 2)");
3664        assert_eq!(round("SELECT overlay(s, 'X', 2, 1)"), "SELECT overlay(s, 'X', 2, 1)");
3665    }
3666
3667    #[test]
3668    fn an_aggregate_keeps_its_distinct() {
3669        assert_eq!(round("SELECT count(*)"), "SELECT count(*)");
3670        assert_eq!(round("SELECT count(DISTINCT x)"), "SELECT count(DISTINCT x)");
3671        assert_eq!(round("SELECT count(ALL x)"), "SELECT count(x)");
3672        assert_eq!(round("SELECT main.count(x)"), "SELECT main.count(x)");
3673    }
3674
3675    #[test]
3676    fn the_modifiers_hang_off_the_query_and_not_off_the_select() {
3677        // `a UNION b ORDER BY x` sorts the union. Putting the order by on the select would have
3678        // made that unrepresentable, which is why the grammar puts it outside the chain and why
3679        // the AST follows.
3680        assert_eq!(
3681            round("SELECT 1 UNION ALL SELECT 2 ORDER BY 1"),
3682            "(SELECT 1 Union All SELECT 2) ORDER BY 1 Unstated Unstated"
3683        );
3684        assert_eq!(
3685            round("SELECT a FROM t UNION SELECT b FROM u EXCEPT SELECT c FROM v"),
3686            "((SELECT a FROM t Union Unstated SELECT b FROM u) Except Unstated SELECT c FROM v)",
3687            "set operators are left associative"
3688        );
3689        assert_eq!(
3690            round("SELECT 1 UNION SELECT 2 INTERSECT SELECT 3"),
3691            "(SELECT 1 Union Unstated (SELECT 2 Intersect Unstated SELECT 3))",
3692            "and intersect binds tighter than the other two"
3693        );
3694    }
3695
3696    #[test]
3697    fn the_sort_and_limit_clauses_keep_what_was_written() {
3698        assert_eq!(
3699            round("SELECT a FROM t ORDER BY a"),
3700            "SELECT a FROM t ORDER BY a Unstated Unstated"
3701        );
3702        assert_eq!(
3703            round("SELECT a FROM t ORDER BY a DESC NULLS LAST"),
3704            "SELECT a FROM t ORDER BY a Descending Last"
3705        );
3706        assert_eq!(round("SELECT a FROM t ORDER BY ALL"), "SELECT a FROM t ORDER BY ALL");
3707        assert_eq!(round("SELECT a FROM t GROUP BY ALL"), "SELECT a FROM t GROUP BY ALL");
3708        assert_eq!(round("SELECT a FROM t LIMIT 10 OFFSET 5"), "SELECT a FROM t LIMIT 10 OFFSET 5");
3709        assert_eq!(round("SELECT a FROM t OFFSET 5 LIMIT 10"), "SELECT a FROM t LIMIT 10 OFFSET 5");
3710        assert_eq!(round("SELECT a FROM t LIMIT 10%"), "SELECT a FROM t LIMIT 10%");
3711        assert_eq!(round("SELECT a FROM t LIMIT ALL"), "SELECT a FROM t", "which is no limit");
3712    }
3713
3714    #[test]
3715    fn a_subquery_appears_in_both_places_it_can() {
3716        assert_eq!(
3717            round("SELECT * FROM (SELECT x FROM t) AS s"),
3718            "SELECT * FROM (SELECT x FROM t) AS s"
3719        );
3720        assert_eq!(round("SELECT (SELECT 1)"), "SELECT (SELECT 1)");
3721    }
3722
3723    #[test]
3724    fn distinct_on_keeps_its_expressions() {
3725        assert_eq!(round("SELECT DISTINCT a"), "SELECT DISTINCT a");
3726        assert_eq!(round("SELECT ALL a"), "SELECT a", "which is the default written out");
3727        assert_eq!(round("SELECT DISTINCT ON (a, b) a"), "SELECT DISTINCT ON (a, b) a");
3728    }
3729
3730    #[test]
3731    fn an_operator_the_dialect_does_not_name_is_kept_by_name() {
3732        // The grammar text says `OperatorLiteral <- Identifier`, which reads as though any bare
3733        // word could be written infix. It cannot. That rule is one of the 24 the matcher overrides
3734        // and it is overridden to the bare operator matcher, so what it takes is a run of operator
3735        // characters. Believing the body here would have produced a transformer that accepted
3736        // `a foo b`, which DuckDB rejects.
3737        assert_eq!(round("SELECT a <=> b"), "SELECT (a <=> b)");
3738        assert!(parse_ast("SELECT a foo b").is_err(), "a bare word is not an operator");
3739    }
3740
3741    #[test]
3742    fn a_script_is_a_list_of_statements() {
3743        let ast = parse_ast("SELECT 1; SELECT 2;").unwrap();
3744        assert_eq!(ast.statements.len(), 2);
3745        // A trailing semicolon makes an empty top level statement in the parse tree, because the
3746        // grammar's `Statement? (';'+ / EndOfInput)` is happy with nothing on both sides. It is
3747        // dropped here rather than pretended away in the matcher.
3748        let Statement::Query(second) = ast.statements[1] else {
3749            panic!("the second statement is a query");
3750        };
3751        assert_eq!(show_query(&ast, second), "SELECT 2");
3752    }
3753
3754    #[test]
3755    fn an_unsupported_construct_names_itself_and_what_was_written() {
3756        let error = parse_ast("ALTER TABLE t ADD COLUMN a INTEGER").unwrap_err().to_string();
3757        assert!(error.starts_with("Not implemented Error"), "{error}");
3758        assert!(error.contains("ALTER TABLE t ADD COLUMN a INTEGER"), "{error}");
3759        assert!(error.contains("AlterStatement"), "{error}");
3760    }
3761
3762    #[test]
3763    fn a_long_construct_is_cut_short_in_the_message() {
3764        let query = format!("ALTER TABLE t ADD COLUMN {} INTEGER", "a".repeat(80));
3765        let error = parse_ast(&query).unwrap_err().to_string();
3766        assert!(error.contains("..."), "{error}");
3767        assert!(error.len() < 200, "{error}");
3768    }
3769
3770    #[test]
3771    fn the_transformer_never_panics_on_anything_the_matcher_accepts() {
3772        // The matcher accepts a good deal that means nothing, because the grammar does. Every one
3773        // of these parses and none of them is a statement this milestone covers, and the contract
3774        // is that the answer is an error either way.
3775        for query in [
3776            "SELECT",
3777            "FROM t SELECT",
3778            "SELECT * FROM t WHERE",
3779            "SELECT ()",
3780            "SELECT a FROM t GROUP BY ()",
3781        ] {
3782            let answer = parse_ast(query);
3783            if let Err(error) = answer {
3784                let message = error.to_string();
3785                assert!(
3786                    message.starts_with("Not implemented Error")
3787                        || message.starts_with("Parser Error"),
3788                    "{query} failed with {message}"
3789                );
3790            }
3791        }
3792    }
3793
3794    #[test]
3795    fn a_file_name_in_a_from_clause_is_a_table_name_with_the_quotes_off() {
3796        // Both spellings have to arrive as the same name, because the binder decides whether it is
3797        // a file by looking at the name, and `'hits.parquet'` with the quotes still on it is not
3798        // a path that anything can open.
3799        assert_eq!(round("SELECT * FROM 'hits.parquet'"), "SELECT * FROM hits.parquet");
3800        assert_eq!(round("SELECT * FROM \"hits.parquet\""), "SELECT * FROM hits.parquet");
3801        assert_eq!(round("SELECT * FROM 'hits.parquet' AS h"), "SELECT * FROM hits.parquet AS h");
3802    }
3803
3804    #[test]
3805    fn a_function_call_in_a_from_clause_is_a_source_and_not_an_expression() {
3806        assert_eq!(round("SELECT * FROM range(3)"), "SELECT * FROM range(3)");
3807        assert_eq!(round("SELECT * FROM range(1, 10, 2)"), "SELECT * FROM range(1, 10, 2)");
3808        assert_eq!(round("SELECT * FROM main.range(3)"), "SELECT * FROM main.range(3)");
3809        assert_eq!(round("SELECT * FROM range(3) AS t"), "SELECT * FROM range(3) AS t");
3810        // The grammar allows a call with no arguments here and the transformer keeps it, because
3811        // whether a particular function takes none is the binder's question and not this one's.
3812        assert_eq!(round("SELECT * FROM some_function()"), "SELECT * FROM some_function()");
3813    }
3814
3815    #[test]
3816    fn the_forms_of_a_table_function_this_does_not_cover_are_turned_away_by_name() {
3817        for query in [
3818            "SELECT * FROM range(3) WITH ORDINALITY",
3819            "SELECT * FROM LATERAL range(3)",
3820            "SELECT * FROM t: range(3)",
3821        ] {
3822            let error = parse_ast(query).unwrap_err().to_string();
3823            assert!(error.contains("grammar rule"), "{query} failed with {error}");
3824        }
3825    }
3826
3827    #[test]
3828    fn interning_means_a_name_written_twice_is_stored_once() {
3829        let ast = parse_ast("SELECT a, a, a FROM t WHERE a = a").unwrap();
3830        assert_eq!(ast.strings.iter().filter(|text| *text == "a").count(), 1);
3831    }
3832}