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, which is one of the thirteen the grammar names and is written back as the
2048            // one spelling that keyword has. `EXTRACT(seconds FROM t)` and `EXTRACT(SECOND FROM t)`
2049            // are both `date_part('SECOND', t)`, which was measured, and it shows up in the column
2050            // name as well as in the deparse, since an unaliased column is named after the call.
2051            "ExtractDatePartArgument" => date_part(self.text(argument)),
2052            // An identifier, taken as written. Which specifier names are legal is not a question
2053            // about syntax, so the answer to it lives with the function.
2054            "ExtractIdentifierArgument" => self.text(argument).to_string(),
2055            _ => return self.unsupported(argument),
2056        };
2057        let text = self.intern(&part);
2058        let part = self.push(Expr::Literal { kind: LiteralKind::String, text });
2059        let operand = self.expr(self.nth(arguments, 1))?;
2060        let name = self.function_name("date_part");
2061        let args = self.expr_slice(vec![part, operand]);
2062        Ok(self.push(Expr::Function { name, args, distinct: false }))
2063    }
2064
2065    /// `FunctionArgument <- NamedFunctionArgument / PositionalFunctionArgument`.
2066    fn argument(&mut self, node: u32) -> Result<ExprRef> {
2067        let inner = self.first(node);
2068        match self.name(inner) {
2069            "PositionalFunctionArgument" => self.expr(self.first(inner)),
2070            _ => self.unsupported(inner),
2071        }
2072    }
2073
2074    /// One argument of a table function, which is the same rule plus the names.
2075    ///
2076    /// `NamedParameter <- TypeFuncName Type? NamedParameterAssignment Expression` and
2077    /// `NamedParameterAssignment <- ':=' / '=>'`, so those two spellings are what the grammar has.
2078    /// The binary accepts a third, `name = value`, which the grammar has no rule for because it
2079    /// parses as an equality and is picked apart afterwards. That is what happens here too: a
2080    /// positional argument that is a comparison between a bare name and something else is a named
2081    /// parameter, which is the reading upstream's own transformer gives it. `read_parquet(f,
2082    /// binary_as_string=True)` is the query that matters and it is the spelling the ClickBench
2083    /// entry uses.
2084    ///
2085    /// The name is not resolved here and neither is the value. Which parameters a function takes
2086    /// is the binder's question, and so is whether `binary_as_string=True` means anything to the
2087    /// function it was written on.
2088    fn table_argument(&mut self, node: u32) -> Result<Target> {
2089        let inner = self.first(node);
2090        if self.name(inner) == "NamedFunctionArgument" {
2091            let named = self.first(inner);
2092            if self.count(named) != 3 {
2093                // The optional `Type` between the name and the assignment, which is a macro
2094                // parameter's declaration and not a call.
2095                return self.unsupported(named);
2096            }
2097            let alias = self.identifier(self.first(named));
2098            let expr = self.expr(self.nth(named, 2))?;
2099            return Ok(Target { expr, alias });
2100        }
2101        let expr = self.expr(self.first(inner))?;
2102        if let Expr::Binary { op: BinaryOp::Eq, left, right } = self.ast.expr(expr) {
2103            if let Expr::Column { name } = self.ast.expr(left) {
2104                if name.len == 1 {
2105                    let alias = self.ast.parts[name.start as usize];
2106                    return Ok(Target { expr: right, alias });
2107                }
2108            }
2109        }
2110        Ok(Target { expr, alias: NONE })
2111    }
2112
2113    /// `CastExpression <- CastOrTryCast Parens(CastArguments)`.
2114    fn cast(&mut self, node: u32) -> Result<ExprRef> {
2115        let try_cast = self.name(self.first(self.first(node))) == "TryCastKeyword";
2116        // `CastArguments <- Expression 'AS' Type`.
2117        let arguments = self.nth(node, 1);
2118        let operand = self.expr(self.first(arguments))?;
2119        let text = self.text(self.nth(arguments, 1)).to_string();
2120        let ty = self.intern(&text);
2121        Ok(self.push(Expr::Cast { operand, ty, try_cast }))
2122    }
2123
2124    /// `TypeLiteral <- Type StringLiteral`, which is the cast written the other way round.
2125    ///
2126    /// `DATE '1995-09-01'` and `CAST('1995-09-01' AS DATE)` are the same expression upstream, and
2127    /// the proof is the column name: the pinned binary answers both of them in a column called
2128    /// `CAST('1995-09-01' AS DATE)`. So this is the cast node and nothing else, which means every
2129    /// type the cast already takes is a typed literal for free and the two can never drift.
2130    ///
2131    /// The string is the literal the grammar matched rather than any expression, so there is no
2132    /// constant folding question here. `DATE x` does not parse in the first place.
2133    fn typed_literal(&mut self, node: u32) -> Result<ExprRef> {
2134        let text = self.text(self.first(node)).to_string();
2135        let ty = self.intern(&text);
2136        let operand = self.expr(self.nth(node, 1))?;
2137        Ok(self.push(Expr::Cast { operand, ty, try_cast: false }))
2138    }
2139
2140    /// `IntervalLiteral <- 'INTERVAL' IntervalParameter Interval?`, which is a function call.
2141    ///
2142    /// There is no interval node and there does not need to be one, because DuckDB's own
2143    /// transformer rewrites the literal into a call and the column name says so: `INTERVAL 1 DAY`
2144    /// comes back from the pinned binary in a column called
2145    /// `to_days(CAST(trunc(CAST(1 AS DOUBLE)) AS INTEGER))`. So the literal and a handwritten
2146    /// `to_days(1)` are the same expression from here on and the two cannot drift apart.
2147    ///
2148    /// Every unit goes through a DOUBLE on the way in, which is what makes `INTERVAL 1.5 DAY` one
2149    /// day rather than a day and a half: the truncation is in the rewrite and not in the function.
2150    /// The two units that can carry a fraction skip the truncation and stay a DOUBLE all the way,
2151    /// so `INTERVAL 2.7 SECOND` really is two and seven tenths of a second.
2152    ///
2153    /// A literal with no unit is the cast written the other way round, so `INTERVAL '1 day'` is
2154    /// `CAST('1 day' AS INTERVAL)`. That arm also catches a word the grammar does not read as a
2155    /// unit, since `INTERVAL 1 d` parses as this rule with no `Interval` child and a column alias
2156    /// after it, which is why upstream answers it with a cast error about an INTEGER.
2157    fn interval_literal(&mut self, node: u32) -> Result<ExprRef> {
2158        let parameter = self.find(node, "IntervalParameter");
2159        if parameter == NONE {
2160            return self.unsupported(node);
2161        }
2162        let operand = self.expr(self.first(parameter))?;
2163        let unit = self.find(node, "Interval");
2164        if unit == NONE {
2165            let ty = self.intern("INTERVAL");
2166            return Ok(self.push(Expr::Cast { operand, ty, try_cast: false }));
2167        }
2168        let spelling = self.name(self.first(unit));
2169        // The seven range forms parse and then refuse, in upstream's words, with the unit names
2170        // spelled the canonical way rather than the way they were written: `interval 1 days to
2171        // hours` is `DAY TO HOUR` there as well.
2172        if spelling == "IntervalToInterval" {
2173            let pair = self.name(self.first(self.first(unit)));
2174            return Err(Error::parser(format!("{} is not supported", worded(pair))));
2175        }
2176        let Some(&(_, function, width)) = UNITS.iter().find(|(rule, _, _)| *rule == spelling)
2177        else {
2178            return self.unsupported(unit);
2179        };
2180        let double = self.intern("DOUBLE");
2181        let mut count = self.push(Expr::Cast { operand, ty: double, try_cast: false });
2182        if let Some(width) = width {
2183            let name = self.function_name("trunc");
2184            let args = self.expr_slice(vec![count]);
2185            let whole = self.push(Expr::Function { name, args, distinct: false });
2186            let ty = self.intern(width);
2187            count = self.push(Expr::Cast { operand: whole, ty, try_cast: false });
2188        }
2189        let name = self.function_name(function);
2190        let args = self.expr_slice(vec![count]);
2191        Ok(self.push(Expr::Function { name, args, distinct: false }))
2192    }
2193
2194    /// `CaseExpression <- 'CASE' Expression? CaseWhenThen+ CaseElse? 'END'`.
2195    fn case(&mut self, node: u32) -> Result<ExprRef> {
2196        let mut operand = NONE;
2197        let mut arms = Vec::new();
2198        let mut otherwise = NONE;
2199        for kid in self.kids(node) {
2200            match self.name(kid) {
2201                // `CaseWhenThen <- 'WHEN' Expression 'THEN' Expression`.
2202                "CaseWhenThen" => {
2203                    let when = self.expr(self.first(kid))?;
2204                    let then = self.expr(self.nth(kid, 1))?;
2205                    arms.push(CaseArm { when, then });
2206                }
2207                // `CaseElse <- 'ELSE' Expression`.
2208                "CaseElse" => otherwise = self.expr(self.first(kid))?,
2209                // The bare `Expression` before the first `WHEN`, which makes it a simple case.
2210                _ => operand = self.expr(kid)?,
2211            }
2212        }
2213        let start = self.ast.case_arms.len() as u32;
2214        self.ast.case_arms.extend(arms);
2215        let arms = Slice { start, len: self.ast.case_arms.len() as u32 - start };
2216        Ok(self.push(Expr::Case { operand, arms, otherwise }))
2217    }
2218
2219    /// `ParenthesisExpression <- Parens(List(Expression)?)`, which is a row value.
2220    ///
2221    /// One item is not a row. `(a)` is `a` in every dialect and reading it as a one column row
2222    /// would change what `(a) = (b)` means.
2223    fn row(&mut self, node: u32) -> Result<ExprRef> {
2224        let mut items = Vec::new();
2225        for kid in self.kids(node) {
2226            items.push(self.expr(kid)?);
2227        }
2228        if items.len() == 1 {
2229            return Ok(items[0]);
2230        }
2231        let items = self.expr_slice(items);
2232        Ok(self.push(Expr::Row { items }))
2233    }
2234
2235    /// `Parameter <- '?' Number / '?' / '$' Number / '$' ColLabel`, a prepared statement parameter.
2236    ///
2237    /// The identifier is what follows the marker, so `?1` and `$1` are both the parameter named 1,
2238    /// and a bare `?` takes the next number by where it was written. That is what DuckDB does, which
2239    /// is why `? + $2` prints as `$1 + $2`: the counting is its own and does not skip a number
2240    /// because a later parameter claimed it.
2241    fn parameter(&mut self, node: u32) -> Result<ExprRef> {
2242        let written = self.text(node).trim();
2243        let written = written.trim_start_matches(['?', '$']).trim();
2244        let name = if written.is_empty() {
2245            self.anonymous += 1;
2246            self.anonymous.to_string()
2247        } else {
2248            written.to_string()
2249        };
2250        let name = self.intern(&name);
2251        Ok(self.push(Expr::Parameter { name }))
2252    }
2253
2254    /// `BoundedListExpression <- '[' List(Expression)? ']'`, which is a LIST value.
2255    ///
2256    /// One item is a list of one here, unlike the parenthesised form, because the brackets are what
2257    /// say list and there is nothing else `[a]` could mean.
2258    fn list(&mut self, node: u32) -> Result<ExprRef> {
2259        let mut items = Vec::new();
2260        for kid in self.kids(node) {
2261            items.push(self.expr(kid)?);
2262        }
2263        let items = self.expr_slice(items);
2264        Ok(self.push(Expr::List { items }))
2265    }
2266
2267    /// `SubqueryExpression <- SubqueryNot? SubqueryExists? SubqueryReference`.
2268    fn subquery(&mut self, node: u32) -> Result<ExprRef> {
2269        if self.find(node, "SubqueryNot") != NONE || self.find(node, "SubqueryExists") != NONE {
2270            return self.unsupported(node);
2271        }
2272        let reference = self.find(node, "SubqueryReference");
2273        let query = self.query(self.first(reference))?;
2274        Ok(self.push(Expr::Subquery { query }))
2275    }
2276
2277    /// The value of a string literal, with the quotes gone and the escapes resolved.
2278    ///
2279    /// A literal can be several tokens. `'a' 'b'` on two lines is one literal that is `ab`, which is
2280    /// the SQL standard's rule and DuckDB's, so the node is decoded token by token rather than by
2281    /// taking its text and stripping the outside.
2282    fn string_value(&self, node: u32) -> Result<String> {
2283        let span = self.tree.node(node);
2284        let mut value = String::new();
2285        for token in &self.tokens[span.start as usize..span.end as usize] {
2286            if token.kind == Kind::String {
2287                value.push_str(&string_token(token.text(self.query))?);
2288            }
2289        }
2290        Ok(value)
2291    }
2292
2293    /// The token that opens a string literal, which is the whole of it when it has a prefix.
2294    ///
2295    /// Only the first token is asked, because a prefixed literal is one token: `E'a' 'b'` is a
2296    /// syntax error upstream rather than a concatenation, so there is no second prefix to disagree
2297    /// with this one.
2298    fn first_string(&self, node: u32) -> &'a str {
2299        let span = self.tree.node(node);
2300        self.tokens[span.start as usize..span.end as usize]
2301            .iter()
2302            .find(|token| token.kind == Kind::String)
2303            .map_or("", |token| token.text(self.query))
2304    }
2305
2306    /// A string literal as an expression, which is the value plus what the prefix makes of it.
2307    ///
2308    /// `N'abc'` is a cast of the string to VARCHAR upstream and not a plain string, and the column
2309    /// name is the proof: the pinned binary answers it in a column called `CAST('abc' AS VARCHAR)`.
2310    /// So it is written here as the cast it is, and then there is nothing left to keep in step.
2311    ///
2312    /// `x'4142'` is not a string at all, it is a BLOB, so it is the one prefix that becomes a
2313    /// different kind of literal rather than a string with something done to it.
2314    fn string_literal(&mut self, node: u32) -> Result<ExprRef> {
2315        let token = self.first_string(node);
2316        let prefix = match token.as_bytes() {
2317            [prefix, b'\'', ..] => *prefix,
2318            _ => 0,
2319        };
2320        if matches!(prefix, b'X' | b'x') {
2321            if let Some(body) = token.get(1..).and_then(quoted_body) {
2322                let text = blob_text(body.as_bytes())?;
2323                let text = self.intern(&text);
2324                return Ok(self.push(Expr::Literal { kind: LiteralKind::Blob, text }));
2325            }
2326        }
2327        let value = self.string_value(node)?;
2328        let text = self.intern(&value);
2329        let literal = self.push(Expr::Literal { kind: LiteralKind::String, text });
2330        if matches!(prefix, b'N' | b'n') {
2331            let ty = self.intern("VARCHAR");
2332            return Ok(self.push(Expr::Cast { operand: literal, ty, try_cast: false }));
2333        }
2334        Ok(literal)
2335    }
2336}
2337
2338/// Each unit an interval literal can be written in, as the grammar rule that spells it, the
2339/// function it becomes, and the width the count is truncated to on the way there.
2340///
2341/// A width of `None` is the pair that keeps what is after the point. Those two stay a DOUBLE and
2342/// never see `trunc`, which is the whole of the difference between `INTERVAL 2.7 SECOND` being two
2343/// and seven tenths of a second and `INTERVAL 1.5 DAY` being one day. Every entry, both spellings
2344/// of every keyword and the width of each one was read off the pinned binary's column names.
2345const UNITS: &[(&str, &str, Option<&str>)] = &[
2346    ("YearKeyword", "to_years", Some("INTEGER")),
2347    ("MonthKeyword", "to_months", Some("INTEGER")),
2348    ("QuarterKeyword", "to_quarters", Some("INTEGER")),
2349    ("DecadeKeyword", "to_decades", Some("INTEGER")),
2350    ("CenturyKeyword", "to_centuries", Some("INTEGER")),
2351    ("MillenniumKeyword", "to_millennia", Some("INTEGER")),
2352    ("DayKeyword", "to_days", Some("INTEGER")),
2353    ("WeekKeyword", "to_weeks", Some("INTEGER")),
2354    ("HourKeyword", "to_hours", Some("BIGINT")),
2355    ("MinuteKeyword", "to_minutes", Some("BIGINT")),
2356    ("MicrosecondKeyword", "to_microseconds", Some("BIGINT")),
2357    ("SecondKeyword", "to_seconds", None),
2358    ("MillisecondKeyword", "to_milliseconds", None),
2359];
2360
2361/// The one spelling a date part keyword is written back as, which is not always the singular.
2362///
2363/// Both spellings of each of the thirteen keywords land on one name, and the name is upper case and
2364/// is plural for the two smallest parts and singular for the rest. That is not a rule, it is a list,
2365/// and it was read off the pinned binary a keyword at a time: `EXTRACT(milliseconds FROM t)` and
2366/// `EXTRACT(millisecond FROM t)` are both `date_part('MILLISECONDS', t)` while `EXTRACT(seconds FROM
2367/// t)` is `date_part('SECOND', t)`.
2368///
2369/// A word that is not a keyword never reaches here, because the grammar tells the two apart, and it
2370/// keeps whatever case it was written in. `EXTRACT(epoch FROM t)` stays lower case, measured.
2371fn date_part(written: &str) -> String {
2372    const PARTS: &[(&str, &str)] = &[
2373        ("YEAR", "YEAR"),
2374        ("YEARS", "YEAR"),
2375        ("MONTH", "MONTH"),
2376        ("MONTHS", "MONTH"),
2377        ("DAY", "DAY"),
2378        ("DAYS", "DAY"),
2379        ("HOUR", "HOUR"),
2380        ("HOURS", "HOUR"),
2381        ("MINUTE", "MINUTE"),
2382        ("MINUTES", "MINUTE"),
2383        ("SECOND", "SECOND"),
2384        ("SECONDS", "SECOND"),
2385        ("MILLISECOND", "MILLISECONDS"),
2386        ("MILLISECONDS", "MILLISECONDS"),
2387        ("MICROSECOND", "MICROSECONDS"),
2388        ("MICROSECONDS", "MICROSECONDS"),
2389        ("WEEK", "WEEK"),
2390        ("WEEKS", "WEEK"),
2391        ("QUARTER", "QUARTER"),
2392        ("QUARTERS", "QUARTER"),
2393        ("DECADE", "DECADE"),
2394        ("DECADES", "DECADE"),
2395        ("CENTURY", "CENTURY"),
2396        ("CENTURIES", "CENTURY"),
2397        ("MILLENNIUM", "MILLENNIUM"),
2398        ("MILLENNIA", "MILLENNIUM"),
2399    ];
2400    PARTS
2401        .iter()
2402        .find(|(spelling, _)| spelling.eq_ignore_ascii_case(written))
2403        .map_or_else(|| written.to_string(), |(_, name)| (*name).to_string())
2404}
2405
2406/// A grammar rule name like `DayToHour` as the words upstream puts in the message for it.
2407fn worded(rule: &str) -> String {
2408    let mut out = String::new();
2409    for character in rule.chars() {
2410        if character.is_ascii_uppercase() && !out.is_empty() {
2411            out.push(' ');
2412        }
2413        out.push(character.to_ascii_uppercase());
2414    }
2415    out
2416}
2417
2418/// The value of one string token, with the quotes gone and whatever the prefix means resolved.
2419///
2420/// There is no fall through that keeps the source text. That arm is what answered `SELECT E'a'`
2421/// with the four characters `E'a'`, and a default that silently answers with the query is a default
2422/// that will do this again with the next spelling somebody adds, so a spelling this does not know
2423/// raises instead. Per #329.
2424fn string_token(text: &str) -> Result<String> {
2425    if let Some(body) = dollar_body(text) {
2426        return Ok(body.to_string());
2427    }
2428    if let Some(body) = quoted_body(text) {
2429        return Ok(body.replace("''", "'"));
2430    }
2431    let Some(body) = text.get(1..).and_then(quoted_body) else {
2432        return Ok(text.to_string());
2433    };
2434    match text.as_bytes()[0] {
2435        b'E' | b'e' => escaped(body),
2436        // `N'abc'` is the string and nothing else. The cast that makes the name is put on outside.
2437        b'N' | b'n' => Ok(body.replace("''", "'")),
2438        // Not a bit string, whatever the spelling suggests. Upstream answers `B'101'` with the four
2439        // characters `b101` as a VARCHAR, and `B''` with the one character `b`, which is measured
2440        // and not guessed. Nothing else is done with the body.
2441        b'B' | b'b' => Ok(format!("b{}", body.replace("''", "'"))),
2442        // `x'41'` is a BLOB and a BLOB is not a string, so the places that want a string out of a
2443        // literal, which are DESCRIBE and the part in EXTRACT, do not get one from this spelling.
2444        _ => Err(Error::not_implemented(format!("the string literal {text} is not supported yet"))),
2445    }
2446}
2447
2448/// The text a blob literal's body means, which is the text a blob prints as.
2449///
2450/// `x'4142'` is two bytes and the pinned binary calls the column `'AB'::BLOB`, so what is kept here
2451/// is the printed form and not the source. The cast that reads it back gives the bytes again, which
2452/// is what makes one text enough for both the value and the name, and it is `Value` that prints it
2453/// so the two spellings of a blob cannot drift apart.
2454///
2455/// Upstream writes `\xHH` for every pair without looking at the digits and lets the cast refuse the
2456/// ones that are not hex, which is why `x'4'` is a parser error and `x'zz'` is a conversion error
2457/// one step later. Doing the same thing gives both messages in the same words. The pairs are bytes
2458/// and not characters: `x'éé'` is four bytes and so two pairs, which is how upstream counts them.
2459fn blob_text(body: &[u8]) -> Result<String> {
2460    if body.len() % 2 != 0 {
2461        return Err(Error::parser("Hex string literal must have an even number of hex digits"));
2462    }
2463    let digit = |byte: u8| (byte as char).to_digit(16).map(|digit| digit as u8);
2464    let bytes: Option<Vec<u8>> =
2465        body.chunks(2).map(|pair| Some(digit(pair[0])? * 16 + digit(pair[1])?)).collect();
2466    match bytes {
2467        Some(bytes) => Ok(Value::Blob(bytes).to_string()),
2468        None => {
2469            Ok(body.chunks(2).map(|pair| format!("\\x{}", String::from_utf8_lossy(pair))).collect())
2470        }
2471    }
2472}
2473
2474/// The body of a single quoted string, for the tokens that are one.
2475///
2476/// An unterminated token has nothing to take off the end and keeps every byte it was given, which
2477/// is why the closing quote has to be a quote that is not also the opening one.
2478fn quoted_body(text: &str) -> Option<&str> {
2479    text.strip_prefix('\'').filter(|rest| !rest.is_empty()).and_then(|rest| rest.strip_suffix('\''))
2480}
2481
2482/// The body of an `E'...'` literal, with the C style escapes resolved.
2483///
2484/// Every rule here was read off the pinned binary one at a time. The named escapes are `\n`, `\t`,
2485/// `\r`, `\b` and `\f`, and `\v` is not one of them. `\x` takes one or two hex digits and `\0`
2486/// through `\7` take one to three octal digits, both of which write a byte and not a character, so
2487/// `\xc3\xa9` is one `é` and `\377` is not a string at all. `\uHHHH` takes exactly four hex digits
2488/// and writes the character they name. Anything else, including a `\u` that is short or names a
2489/// surrogate half or a NUL, drops the backslash and keeps the character, so `\q` is `q` and `\u41`
2490/// is `u41`.
2491///
2492/// The result is bytes until the end because the escapes write bytes, and the two ways of writing
2493/// something that is not a string both raise the way upstream raises them.
2494fn escaped(body: &str) -> Result<String> {
2495    let bytes = body.as_bytes();
2496    let mut out = Vec::with_capacity(bytes.len());
2497    let mut at = 0;
2498    while at < bytes.len() {
2499        let byte = bytes[at];
2500        at += 1;
2501        if byte == b'\'' && bytes.get(at) == Some(&b'\'') {
2502            out.push(b'\'');
2503            at += 1;
2504            continue;
2505        }
2506        if byte != b'\\' || at == bytes.len() {
2507            out.push(byte);
2508            continue;
2509        }
2510        let escape = bytes[at];
2511        at += 1;
2512        match escape {
2513            b'n' => out.push(b'\n'),
2514            b't' => out.push(b'\t'),
2515            b'r' => out.push(b'\r'),
2516            b'b' => out.push(0x08),
2517            b'f' => out.push(0x0c),
2518            b'x' => match digits(bytes, &mut at, 16, 2) {
2519                Some(value) => out.push(value as u8),
2520                None => out.push(b'x'),
2521            },
2522            b'0'..=b'7' => {
2523                at -= 1;
2524                let value = digits(bytes, &mut at, 8, 3).unwrap_or(0);
2525                out.push(value as u8);
2526            }
2527            b'u' => match four_hex(bytes, at).and_then(char::from_u32).filter(|c| *c != '\0') {
2528                Some(c) => {
2529                    at += 4;
2530                    out.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes());
2531                }
2532                None => out.push(b'u'),
2533            },
2534            other => out.push(other),
2535        }
2536    }
2537    if out.contains(&0) {
2538        return Err(Error::parser("Null character not permitted in escape string literal"));
2539    }
2540    String::from_utf8(out).map_err(|error| {
2541        Error::parser(format!(
2542            "Invalid UTF-8 in escape string literal at byte offset {}: byte mismatch",
2543            error.utf8_error().valid_up_to()
2544        ))
2545    })
2546}
2547
2548/// Up to `most` digits in `radix` starting at `at`, moving `at` past the ones that were taken.
2549///
2550/// `None` means there were none at all, which is the case where the escape was not an escape:
2551/// `\x` on its own is the letter `x` upstream and not a zero byte.
2552fn digits(bytes: &[u8], at: &mut usize, radix: u32, most: usize) -> Option<u32> {
2553    let mut value = None;
2554    for _ in 0..most {
2555        let Some(digit) = bytes.get(*at).and_then(|byte| (*byte as char).to_digit(radix)) else {
2556            break;
2557        };
2558        value = Some(value.unwrap_or(0) * radix + digit);
2559        *at += 1;
2560    }
2561    value
2562}
2563
2564/// The four hex digits of a `\uHHHH`, which has to be all four of them or it is not one.
2565///
2566/// Nothing is consumed here, because the digits are only digits if the whole escape works out. A
2567/// surrogate half is not a character and upstream does not pair it up either, so `😀` is
2568/// the ten characters it was written as, which is what the caller falls back to.
2569fn four_hex(bytes: &[u8], at: usize) -> Option<u32> {
2570    let digits = bytes.get(at..at + 4)?;
2571    if !digits.iter().all(u8::is_ascii_hexdigit) {
2572        return None;
2573    }
2574    u32::from_str_radix(std::str::from_utf8(digits).ok()?, 16).ok()
2575}
2576
2577/// The body of a dollar quoted string, for the tokens that are one.
2578///
2579/// The tag is whatever sits between the opening pair of dollars and may be empty, so `$$a$$` and
2580/// `$tag$a$tag$` both arrive here, and nothing inside the body is escaped, which is the whole reason
2581/// the spelling exists. The tokenizer has already found the closing tag, which is the part that takes
2582/// work, so this says where the body starts and ends and no more. A token that is not dollar quoted
2583/// gives `None` and so does an unterminated one, which has no closing tag to take off and keeps every
2584/// byte it was given, the way the matcher already treats it. Per #276.
2585fn dollar_body(text: &str) -> Option<&str> {
2586    let rest = text.strip_prefix('$')?;
2587    let close = rest.find('$')?;
2588    let (tag, body) = (&rest[..close], &rest[close + 1..]);
2589    body.strip_suffix(&format!("${tag}$"))
2590}
2591
2592/// Strip the quoting off an identifier.
2593///
2594/// DuckDB does not fold identifier case at any point, quoted or not, so this only removes the
2595/// quotes and resolves the doubled ones. Anything else would be the parser deciding what a name is.
2596///
2597/// Single quotes are stripped too, and the only way one gets here is the file name in `FROM
2598/// 'hits.parquet'`, because the matcher takes a string for a name in that position and in `COPY t TO
2599/// '...'` and nowhere else. Leaving them on would make that name different from the one `FROM
2600/// "hits.parquet"` writes, and DuckDB reads both of those as the same file.
2601fn unquote(text: &str) -> String {
2602    if let Some(body) = text.strip_prefix('"').and_then(|rest| rest.strip_suffix('"')) {
2603        return body.replace("\"\"", "\"");
2604    }
2605    match text.strip_prefix('\'').and_then(|rest| rest.strip_suffix('\'')) {
2606        Some(body) => body.replace("''", "'"),
2607        None => text.to_string(),
2608    }
2609}
2610
2611#[cfg(test)]
2612mod tests {
2613    use super::*;
2614    use crate::corpus::CORPUS;
2615    use crate::matcher::parse;
2616
2617    /// The AST written back out as text, which is what the assertions below read.
2618    ///
2619    /// Not a SQL printer and not trying to be. It is deliberately not valid SQL: operators are
2620    /// spelled with the name of the variant and every binary node is parenthesised, so that a test
2621    /// asserting on this text is asserting on the shape of the tree and not on a formatting choice.
2622    /// `a - b - c` and `a - (b - c)` have to look different here or the test that tells them apart
2623    /// is not a test.
2624    fn show(ast: &Ast, expr: ExprRef) -> String {
2625        if expr == NONE {
2626            return "-".to_string();
2627        }
2628        let list = |slice: Slice| {
2629            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
2630        };
2631        match ast.expr(expr) {
2632            Expr::Star { qualifier, replacements } => {
2633                let star = if qualifier.is_empty() {
2634                    "*".to_string()
2635                } else {
2636                    format!("{}.*", ast.name_text(qualifier))
2637                };
2638                if replacements.is_empty() {
2639                    return star;
2640                }
2641                let entries: Vec<String> = ast
2642                    .target_list(replacements)
2643                    .iter()
2644                    .map(|target| {
2645                        format!("{} AS {}", show(ast, target.expr), ast.string(target.alias))
2646                    })
2647                    .collect();
2648                format!("{star} REPLACE ({})", entries.join(", "))
2649            }
2650            Expr::Column { name } => ast.name_text(name),
2651            Expr::Literal { kind, text } => match kind {
2652                LiteralKind::Number => ast.string(text).to_string(),
2653                LiteralKind::String => format!("'{}'", ast.string(text)),
2654                LiteralKind::Blob => format!("'{}'::BLOB", ast.string(text)),
2655                other => format!("{other:?}").to_uppercase(),
2656            },
2657            Expr::Unary { op, operand } => format!("({op:?} {})", show(ast, operand)),
2658            Expr::Binary { op, left, right } => {
2659                let op = match op {
2660                    BinaryOp::Named(name) => ast.string(name).to_string(),
2661                    other => format!("{other:?}"),
2662                };
2663                format!("({} {op} {})", show(ast, left), show(ast, right))
2664            }
2665            Expr::Function { name, args, distinct } => {
2666                let distinct = if distinct { "DISTINCT " } else { "" };
2667                format!("{}({distinct}{})", ast.name_text(name), list(args))
2668            }
2669            Expr::Cast { operand, ty, try_cast } => {
2670                let word = if try_cast { "TRY_CAST" } else { "CAST" };
2671                format!("{word}({} AS {})", show(ast, operand), ast.string(ty))
2672            }
2673            Expr::Case { operand, arms, otherwise } => {
2674                let arms = ast
2675                    .arm_list(arms)
2676                    .iter()
2677                    .map(|arm| format!("WHEN {} THEN {}", show(ast, arm.when), show(ast, arm.then)))
2678                    .collect::<Vec<_>>()
2679                    .join(" ");
2680                format!("CASE {} {arms} ELSE {} END", show(ast, operand), show(ast, otherwise))
2681            }
2682            Expr::Between { operand, low, high, negated } => {
2683                let not = if negated { "NOT " } else { "" };
2684                format!(
2685                    "({not}{} BETWEEN {} AND {})",
2686                    show(ast, operand),
2687                    show(ast, low),
2688                    show(ast, high)
2689                )
2690            }
2691            Expr::In { operand, list: items, negated } => {
2692                let not = if negated { "NOT " } else { "" };
2693                format!("({not}{} IN [{}])", show(ast, operand), list(items))
2694            }
2695            Expr::List { items } => format!("[{}]", list(items)),
2696            Expr::Parameter { name } => format!("${}", ast.string(name)),
2697            Expr::Row { items } => format!("ROW({})", list(items)),
2698            Expr::Subquery { query } => format!("({})", show_query(ast, query)),
2699        }
2700    }
2701
2702    /// One from item written back out.
2703    fn show_source(ast: &Ast, source: SourceRef) -> String {
2704        let alias = |alias: StrRef| match alias {
2705            NONE => String::new(),
2706            other => format!(" AS {}", ast.string(other)),
2707        };
2708        match ast.source(source) {
2709            Source::Table { name, alias: name_alias, .. } => {
2710                format!("{}{}", ast.name_text(name), alias(name_alias))
2711            }
2712            Source::Function { name, args, alias: call_alias, .. } => {
2713                let args = ast
2714                    .target_list(args)
2715                    .iter()
2716                    .map(|item| match item.alias {
2717                        NONE => show(ast, item.expr),
2718                        named => format!("{} := {}", ast.string(named), show(ast, item.expr)),
2719                    })
2720                    .collect::<Vec<_>>()
2721                    .join(", ");
2722                format!("{}({args}){}", ast.name_text(name), alias(call_alias))
2723            }
2724            Source::Subquery { query, alias: query_alias, .. } => {
2725                format!("({}){}", show_query(ast, query), alias(query_alias))
2726            }
2727            Source::Values { rows, alias: values_alias, .. } => {
2728                format!("{}{}", show_rows(ast, rows), alias(values_alias))
2729            }
2730            Source::Join { left, right, kind, natural, on, using } => {
2731                let natural = if natural { "NATURAL " } else { "" };
2732                let on = if on == NONE { String::new() } else { format!(" ON {}", show(ast, on)) };
2733                let using = if using.is_empty() {
2734                    String::new()
2735                } else {
2736                    format!(" USING ({})", ast.name_text(using))
2737                };
2738                format!(
2739                    "({} {natural}{kind:?} JOIN {}{on}{using})",
2740                    show_source(ast, left),
2741                    show_source(ast, right)
2742                )
2743            }
2744        }
2745    }
2746
2747    /// The rows of a `VALUES` written back out.
2748    fn show_rows(ast: &Ast, rows: Slice) -> String {
2749        let rows = ast
2750            .rows(rows)
2751            .iter()
2752            .map(|&row| {
2753                let items = ast
2754                    .expr_list(row)
2755                    .iter()
2756                    .map(|&item| show(ast, item))
2757                    .collect::<Vec<_>>()
2758                    .join(", ");
2759                format!("({items})")
2760            })
2761            .collect::<Vec<_>>()
2762            .join(", ");
2763        format!("VALUES {rows}")
2764    }
2765
2766    /// One query written back out.
2767    fn show_query(ast: &Ast, index: QueryRef) -> String {
2768        let query = ast.query(index);
2769        let list = |slice: Slice| {
2770            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
2771        };
2772        let mut out = match query.body {
2773            QueryBody::SetOp { op, quantifier, by_name, left, right } => {
2774                let by_name = if by_name { " BY NAME" } else { "" };
2775                format!(
2776                    "({} {op:?} {quantifier:?}{by_name} {})",
2777                    show_query(ast, left),
2778                    show_query(ast, right)
2779                )
2780            }
2781            QueryBody::Select(index) => {
2782                let select = ast.select(index);
2783                let distinct = match select.distinct {
2784                    Distinct::No => String::new(),
2785                    Distinct::Yes => " DISTINCT".to_string(),
2786                    Distinct::On(on) => format!(" DISTINCT ON ({})", list(on)),
2787                };
2788                let targets = ast
2789                    .target_list(select.targets)
2790                    .iter()
2791                    .map(|target| match target.alias {
2792                        NONE => show(ast, target.expr),
2793                        alias => format!("{} AS {}", show(ast, target.expr), ast.string(alias)),
2794                    })
2795                    .collect::<Vec<_>>()
2796                    .join(", ");
2797                let mut out = format!("SELECT{distinct} {targets}");
2798                if !select.from.is_empty() {
2799                    let from = ast
2800                        .source_list(select.from)
2801                        .iter()
2802                        .map(|&source| show_source(ast, source))
2803                        .collect::<Vec<_>>()
2804                        .join(", ");
2805                    out += &format!(" FROM {from}");
2806                }
2807                if select.filter != NONE {
2808                    out += &format!(" WHERE {}", show(ast, select.filter));
2809                }
2810                if select.group_by_all {
2811                    out += " GROUP BY ALL";
2812                } else if !select.group_by.is_empty() {
2813                    out += &format!(" GROUP BY {}", list(select.group_by));
2814                }
2815                if select.having != NONE {
2816                    out += &format!(" HAVING {}", show(ast, select.having));
2817                }
2818                out
2819            }
2820            QueryBody::Values(rows) => show_rows(ast, rows),
2821            QueryBody::Describe(inner) => format!("DESCRIBE {}", show_query(ast, inner)),
2822        };
2823        if query.order_by_all {
2824            out += " ORDER BY ALL";
2825        } else if !query.order_by.is_empty() {
2826            let items = ast
2827                .order_list(query.order_by)
2828                .iter()
2829                .map(|item| format!("{} {:?} {:?}", show(ast, item.expr), item.order, item.nulls))
2830                .collect::<Vec<_>>()
2831                .join(", ");
2832            out += &format!(" ORDER BY {items}");
2833        }
2834        if query.limit != NONE {
2835            let percent = if query.limit_percent { "%" } else { "" };
2836            out += &format!(" LIMIT {}{percent}", show(ast, query.limit));
2837        }
2838        if query.offset != NONE {
2839            out += &format!(" OFFSET {}", show(ast, query.offset));
2840        }
2841        out
2842    }
2843
2844    /// One statement, transformed and written back out.
2845    fn round(query: &str) -> String {
2846        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
2847        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
2848        let Statement::Query(index) = ast.statements[0] else {
2849            panic!("{query} is not a query");
2850        };
2851        show_query(&ast, index)
2852    }
2853
2854    /// One statement, transformed and written back out as the DDL and DML shape it is.
2855    fn round_statement(query: &str) -> String {
2856        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
2857        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
2858        match ast.statements[0] {
2859            Statement::Query(index) => show_query(&ast, index),
2860            Statement::CreateTable(index) => {
2861                let create = ast.create_table(index);
2862                let mut out = "CREATE".to_string();
2863                if create.or_replace {
2864                    out += " OR REPLACE";
2865                }
2866                if create.temporary {
2867                    out += " TEMPORARY";
2868                }
2869                out += " TABLE";
2870                if create.if_not_exists {
2871                    out += " IF NOT EXISTS";
2872                }
2873                out += &format!(" {}", ast.name_text(create.name));
2874                let columns = ast
2875                    .column_defs(create.columns)
2876                    .iter()
2877                    .map(|def| {
2878                        let ty = match def.ty {
2879                            NONE => String::new(),
2880                            other => format!(" {}", ast.string(other)),
2881                        };
2882                        let null = if def.not_null { " NOT NULL" } else { "" };
2883                        format!("{}{ty}{null}", ast.string(def.name))
2884                    })
2885                    .collect::<Vec<_>>()
2886                    .join(", ");
2887                if !columns.is_empty() || create.query == NONE {
2888                    out += &format!(" ({columns})");
2889                }
2890                if create.query != NONE {
2891                    out += &format!(" AS {}", show_query(&ast, create.query));
2892                }
2893                out
2894            }
2895            Statement::CreateView(index) => {
2896                let create = ast.create_view(index);
2897                let mut out = "CREATE".to_string();
2898                if create.or_replace {
2899                    out += " OR REPLACE";
2900                }
2901                if create.temporary {
2902                    out += " TEMPORARY";
2903                }
2904                out += " VIEW";
2905                if create.if_not_exists {
2906                    out += " IF NOT EXISTS";
2907                }
2908                out += &format!(" {}", ast.name_text(create.name));
2909                if !create.columns.is_empty() {
2910                    let columns = ast.name(create.columns).collect::<Vec<_>>().join(", ");
2911                    out += &format!(" ({columns})");
2912                }
2913                out + &format!(" AS {}", show_query(&ast, create.query))
2914            }
2915            Statement::DropTable(index) => {
2916                let drop = ast.drop_table(index);
2917                let mut out = if drop.view { "DROP VIEW" } else { "DROP TABLE" }.to_string();
2918                if drop.if_exists {
2919                    out += " IF EXISTS";
2920                }
2921                let names = ast
2922                    .name_list(drop.names)
2923                    .iter()
2924                    .map(|&name| ast.name_text(name))
2925                    .collect::<Vec<_>>()
2926                    .join(", ");
2927                out + &format!(" {names}")
2928            }
2929            Statement::Insert(index) => {
2930                let insert = ast.insert(index);
2931                let mut out = format!("INSERT INTO {}", ast.name_text(insert.name));
2932                if !insert.columns.is_empty() {
2933                    let columns = ast.name(insert.columns).collect::<Vec<_>>().join(", ");
2934                    out += &format!(" ({columns})");
2935                }
2936                out + &format!(" {}", show_query(&ast, insert.source))
2937            }
2938            Statement::Set(index) => {
2939                let setting = ast.setting(index);
2940                let scope = match setting.scope.keyword() {
2941                    "" => String::new(),
2942                    word => format!(" {word}"),
2943                };
2944                format!("SET{scope} {} = {}", ast.string(setting.name), show(&ast, setting.value))
2945            }
2946            Statement::Reset(index) => {
2947                let setting = ast.setting(index);
2948                let scope = match setting.scope.keyword() {
2949                    "" => String::new(),
2950                    word => format!(" {word}"),
2951                };
2952                format!("RESET{scope} {}", ast.string(setting.name))
2953            }
2954            Statement::Explain { query, analyze } => {
2955                let analyze = if analyze { "ANALYZE " } else { "" };
2956                format!("EXPLAIN {analyze}{}", show_query(&ast, query))
2957            }
2958        }
2959    }
2960
2961    #[test]
2962    fn an_explain_keeps_the_query_it_was_asked_about() {
2963        assert_eq!(
2964            round_statement("EXPLAIN SELECT a FROM t WHERE a > 1"),
2965            "EXPLAIN SELECT a FROM t WHERE (a Gt 1)"
2966        );
2967        assert_eq!(round_statement("explain select 1"), "EXPLAIN SELECT 1");
2968        assert_eq!(round_statement("explain analyze select 1"), "EXPLAIN ANALYZE SELECT 1");
2969    }
2970
2971    #[test]
2972    fn the_parts_of_an_explain_that_are_not_the_query_are_refused_by_name() {
2973        // An option list that chooses a format is a promise about the output this does not keep,
2974        // and a statement that is not a query has no plan to show.
2975        for (query, named) in [
2976            ("EXPLAIN (FORMAT JSON) SELECT 1", "ExplainOptionList"),
2977            ("EXPLAIN INSERT INTO t VALUES (1)", "InsertStatement"),
2978            ("EXPLAIN CREATE TABLE u (a INTEGER)", "CreateStatement"),
2979        ] {
2980            let error = parse_ast(query).expect_err(query).to_string();
2981            assert!(error.contains(named), "{query}: {error}");
2982        }
2983    }
2984
2985    #[test]
2986    fn a_set_keeps_its_name_its_scope_and_its_value() {
2987        assert_eq!(round_statement("SET memory_limit = '1GB'"), "SET memory_limit = '1GB'");
2988        assert_eq!(round_statement("set threads=4"), "SET threads = 4");
2989        assert_eq!(round_statement("SET GLOBAL threads = 4"), "SET GLOBAL threads = 4");
2990        assert_eq!(round_statement("SET SESSION threads = 4"), "SET SESSION threads = 4");
2991        assert_eq!(round_statement("SET LOCAL threads = 4"), "SET LOCAL threads = 4");
2992        assert_eq!(round_statement("RESET memory_limit"), "RESET memory_limit");
2993        assert_eq!(round_statement("RESET GLOBAL memory_limit"), "RESET GLOBAL memory_limit");
2994    }
2995
2996    #[test]
2997    fn the_two_other_things_the_word_set_starts_are_refused_rather_than_read_as_settings() {
2998        // `SET VARIABLE x = 1` declares a session variable and `SET SCHEMA` picks where an
2999        // unqualified name is looked up. Neither is a knob on the engine and reading either as one
3000        // would change an answer quietly.
3001        for statement in ["SET VARIABLE x = 1", "SET SCHEMA 'main'", "SET TIME ZONE 'UTC'"] {
3002            let error = parse_ast(statement).expect_err(statement);
3003            assert_eq!(error.code().duckdb_name(), "Not implemented Error", "{statement}");
3004        }
3005    }
3006
3007    #[test]
3008    fn a_setting_written_with_a_list_of_values_is_refused_rather_than_taking_the_first() {
3009        let error = parse_ast("SET search_path = a, b").expect_err("a list of two");
3010        assert_eq!(error.code().duckdb_name(), "Not implemented Error");
3011    }
3012
3013    #[test]
3014    fn the_query_m0_has_to_run_transforms() {
3015        assert_eq!(round("SELECT * FROM t WHERE x > 5"), "SELECT * FROM t WHERE (x Gt 5)");
3016    }
3017
3018    #[test]
3019    fn a_replace_list_rides_on_the_star_it_changes() {
3020        // The parentheses are optional around a single entry, which is how the clickbench load
3021        // recipe is not written but is how a lot of hand written sql is.
3022        assert_eq!(
3023            round("SELECT * REPLACE (a + 1 AS a) FROM t"),
3024            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
3025        );
3026        assert_eq!(
3027            round("SELECT * REPLACE a + 1 AS a FROM t"),
3028            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
3029        );
3030        assert_eq!(
3031            round("SELECT t.* REPLACE (make_date(a) AS a, b * 2 AS b) FROM t"),
3032            "SELECT t.* REPLACE (make_date(a) AS a, (b Multiply 2) AS b) FROM t"
3033        );
3034    }
3035
3036    #[test]
3037    fn one_column_cannot_be_replaced_twice() {
3038        // Caught here rather than in the binder because it is a mistake in what was written and
3039        // not a mistake about what is in the table, and duckdb reports it the same way.
3040        let error = parse_ast("SELECT * REPLACE (a + 1 AS a, a + 2 AS A) FROM t").unwrap_err();
3041        assert_eq!(error.to_string(), "Parser Error: Duplicate entry \"A\" in REPLACE list");
3042    }
3043
3044    #[test]
3045    fn a_table_function_argument_can_have_a_name_written_in_front_of_it() {
3046        // The grammar has `:=` and `=>`. It does not have `=`, which parses as a comparison and is
3047        // read back apart here, and that is the spelling the clickbench load recipe uses.
3048        for spelling in
3049            ["binary_as_string := True", "binary_as_string => True", "binary_as_string = True"]
3050        {
3051            assert_eq!(
3052                round(&format!("SELECT * FROM read_parquet('f.parquet', {spelling})")),
3053                "SELECT * FROM read_parquet('f.parquet', binary_as_string := TRUE)",
3054                "{spelling}"
3055            );
3056        }
3057    }
3058
3059    #[test]
3060    fn an_equality_that_is_not_a_bare_name_stays_an_argument() {
3061        // A qualified name on the left is not a parameter name, and neither is anything that is
3062        // not a name at all, so both of those stay the comparison they were written as.
3063        assert_eq!(round("SELECT * FROM f(t.a = 1)"), "SELECT * FROM f((t.a Eq 1))");
3064        assert_eq!(round("SELECT * FROM f(1 = 1)"), "SELECT * FROM f((1 Eq 1))");
3065    }
3066
3067    #[test]
3068    fn a_create_table_keeps_its_types_as_text() {
3069        assert_eq!(
3070            round_statement("CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"),
3071            "CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"
3072        );
3073        // The type is the text between the identifier and whatever follows it, parentheses and
3074        // all, because resolving `DECIMAL(18, 3)` into a width and a scale is the binder's job and
3075        // doing it here would mean two places that know the type table.
3076        assert_eq!(
3077            round_statement("CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"),
3078            "CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"
3079        );
3080    }
3081
3082    #[test]
3083    fn the_modifiers_on_a_create_table_survive() {
3084        assert_eq!(
3085            round_statement("CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"),
3086            "CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"
3087        );
3088        assert_eq!(
3089            round_statement("CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"),
3090            "CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"
3091        );
3092    }
3093
3094    #[test]
3095    fn or_replace_and_if_not_exists_in_one_statement_is_refused_here_and_not_later() {
3096        // The grammar has room for both and duckdb's has not, so its refusal is a parser error with
3097        // a caret under the `NOT` and this one is a parser error at the same stage. It is the same
3098        // sentence whatever is being created.
3099        for sql in [
3100            "CREATE OR REPLACE TABLE IF NOT EXISTS t (a INT)",
3101            "CREATE OR REPLACE VIEW IF NOT EXISTS v AS SELECT 1",
3102        ] {
3103            let error = parse_ast(sql).unwrap_err().to_string();
3104            assert_eq!(
3105                error,
3106                "Parser Error: Cannot specify both OR REPLACE and IF NOT EXISTS within single \
3107                 create statement"
3108            );
3109        }
3110    }
3111
3112    #[test]
3113    fn a_create_table_as_carries_the_query_and_not_the_types() {
3114        assert_eq!(
3115            round_statement("CREATE TABLE t AS SELECT a FROM u"),
3116            "CREATE TABLE t AS SELECT a FROM u"
3117        );
3118        // The names are the syntax's to say and the types are the query's, so the column
3119        // definitions here have names and no types.
3120        assert_eq!(
3121            round_statement("CREATE TABLE t (x, y) AS SELECT a, b FROM u"),
3122            "CREATE TABLE t (x, y) AS SELECT a, b FROM u"
3123        );
3124    }
3125
3126    #[test]
3127    fn a_create_view_carries_its_body_twice_over() {
3128        assert_eq!(
3129            round_statement("CREATE VIEW v AS SELECT a FROM u"),
3130            "CREATE VIEW v AS SELECT a FROM u"
3131        );
3132        assert_eq!(
3133            round_statement("CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"),
3134            "CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"
3135        );
3136        // The text the catalog keeps is the body and only the body, so that binding it again is
3137        // binding a query rather than a `CREATE` statement.
3138        let ast = parse_ast("CREATE VIEW v (x) AS SELECT a FROM u WHERE a > 1").expect("parses");
3139        let Statement::CreateView(index) = ast.statements[0] else {
3140            panic!("not a create view");
3141        };
3142        assert_eq!(ast.string(ast.create_view(index).sql), "SELECT a FROM u WHERE a > 1");
3143    }
3144
3145    #[test]
3146    fn a_drop_view_is_not_a_drop_table() {
3147        assert_eq!(round_statement("DROP VIEW IF EXISTS a, b"), "DROP VIEW IF EXISTS a, b");
3148        assert_eq!(round_statement("DROP TABLE a"), "DROP TABLE a");
3149    }
3150
3151    #[test]
3152    fn a_drop_table_is_a_list_of_qualified_names() {
3153        assert_eq!(round_statement("DROP TABLE t"), "DROP TABLE t");
3154        assert_eq!(round_statement("DROP TABLE IF EXISTS a, b.c"), "DROP TABLE IF EXISTS a, b.c");
3155    }
3156
3157    #[test]
3158    fn dropping_something_that_is_neither_a_table_nor_a_view_is_refused() {
3159        // `TableOrView` covers `MATERIALIZED VIEW` as well, which is not a thing this database has,
3160        // and dropping one as if it were an ordinary view is a wrong answer rather than a missing
3161        // feature.
3162        let error = parse_ast("DROP MATERIALIZED VIEW v").unwrap_err().to_string();
3163        assert!(error.starts_with("Not implemented Error"), "{error}");
3164    }
3165
3166    #[test]
3167    fn both_spellings_of_insert_arrive_at_a_query() {
3168        assert_eq!(
3169            round_statement("INSERT INTO t VALUES (1, 'a'), (2, 'b')"),
3170            "INSERT INTO t VALUES (1, 'a'), (2, 'b')"
3171        );
3172        assert_eq!(
3173            round_statement("INSERT INTO t (a, b) SELECT x, y FROM u"),
3174            "INSERT INTO t (a, b) SELECT x, y FROM u"
3175        );
3176    }
3177
3178    #[test]
3179    fn an_insert_clause_that_changes_the_answer_is_refused() {
3180        for query in [
3181            "INSERT INTO t VALUES (1) RETURNING *",
3182            "INSERT OR REPLACE INTO t VALUES (1)",
3183            "INSERT INTO t BY NAME SELECT 1 AS a",
3184            "INSERT INTO t VALUES (1) ON CONFLICT DO NOTHING",
3185            "INSERT INTO t DEFAULT VALUES",
3186        ] {
3187            let error = parse_ast(query).unwrap_err().to_string();
3188            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
3189        }
3190    }
3191
3192    #[test]
3193    fn a_column_constraint_that_is_not_not_null_is_refused() {
3194        // Nothing enforces a constraint yet. Accepting one and not enforcing it is the wrong
3195        // answer, so `NOT NULL` is kept because the column already has a nullability and the rest
3196        // are refused until there is somewhere to put them.
3197        for query in [
3198            "CREATE TABLE t (a INT PRIMARY KEY)",
3199            "CREATE TABLE t (a INT UNIQUE)",
3200            "CREATE TABLE t (a INT CHECK (a > 0))",
3201            "CREATE TABLE t (a INT DEFAULT 1)",
3202            "CREATE TABLE t (a INT REFERENCES u (b))",
3203            "CREATE TABLE t (a INT, PRIMARY KEY (a))",
3204        ] {
3205            let error = parse_ast(query).unwrap_err().to_string();
3206            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
3207        }
3208    }
3209
3210    #[test]
3211    fn values_is_a_query_on_its_own_and_in_a_from() {
3212        assert_eq!(round("VALUES (1), (2)"), "VALUES (1), (2)");
3213        // Parenthesised it is a subquery whose body is the values, and bare it is a `ValuesRef`.
3214        // Two rules and one meaning, which is the grammar's doing and not something to flatten
3215        // here, because the parenthesised form can carry an order by and the bare one cannot.
3216        assert_eq!(
3217            round("SELECT * FROM (VALUES (1, 2), (3, 4)) t(a, b)"),
3218            "SELECT * FROM (VALUES (1, 2), (3, 4)) AS t"
3219        );
3220        assert_eq!(
3221            round("SELECT * FROM VALUES (1, 2), (3, 4) AS t(a, b)"),
3222            "SELECT * FROM VALUES (1, 2), (3, 4) AS t"
3223        );
3224        // Rows of different widths parse. Saying so wants the column count, which for an insert is
3225        // the table's, so the check belongs to the binder and not here.
3226        assert_eq!(round("VALUES (1), (2, 3)"), "VALUES (1), (2, 3)");
3227    }
3228
3229    /// `DESCRIBE` is a query body, and the two spellings that name something become a star over it.
3230    ///
3231    /// Naming a table is not a shortcut for the query. On the reference binary `DESCRIBE t` and
3232    /// `DESCRIBE SELECT * FROM t` print the same six columns and the same rows, down to the `NO` on
3233    /// a column that refuses nulls, so rewriting one into the other costs nothing and leaves the
3234    /// binder with one case instead of three. A file name goes down the same path as a table name
3235    /// because a bare string in a `FROM` clause is already a name the replacement scan picks up.
3236    #[test]
3237    fn describe_rewrites_a_name_into_a_star_over_it() {
3238        assert_eq!(round("DESCRIBE SELECT 1 AS a"), "DESCRIBE SELECT 1 AS a");
3239        assert_eq!(round("DESCRIBE t"), "DESCRIBE SELECT * FROM t");
3240        assert_eq!(round("DESC t"), "DESCRIBE SELECT * FROM t");
3241        assert_eq!(round("DESCRIBE 'x.parquet'"), "DESCRIBE SELECT * FROM x.parquet");
3242        // A body and not a statement kind, so it nests both ways with no rule of its own.
3243        assert_eq!(
3244            round("SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"),
3245            "SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"
3246        );
3247        assert_eq!(round("DESCRIBE DESCRIBE SELECT 1 AS a"), "DESCRIBE DESCRIBE SELECT 1 AS a");
3248    }
3249
3250    /// `SUMMARIZE` shares both of `DESCRIBE`'s grammar rules and is a different statement.
3251    ///
3252    /// It reads every row and returns one row per column carrying the min, the max, the count and
3253    /// the approximate distinct count, so none of it falls out of the `DESCRIBE` path. The word is
3254    /// the only thing in the tree that tells the two apart, which is why the transform looks at it
3255    /// rather than trusting the rule name it arrived under.
3256    #[test]
3257    fn summarize_is_refused_even_though_it_parses_as_a_describe() {
3258        for query in ["SUMMARIZE t", "SUMMARIZE SELECT 1"] {
3259            let error = parse_ast(query).expect_err("summarize is not implemented");
3260            let message = error.to_string();
3261            assert!(message.starts_with("Not implemented Error"), "{query} failed with {message}");
3262        }
3263    }
3264
3265    #[test]
3266    fn every_statement_in_the_corpus_gets_a_defined_answer() {
3267        // The point of the test is the word defined. Half of these are statement kinds and
3268        // clauses this milestone does not cover, and the requirement is not that they work, it is
3269        // that they fail by saying so. A panic, a silently dropped clause or an internal error
3270        // would each be a different bug and all three would be invisible without this.
3271        let mut done = 0;
3272        for query in CORPUS {
3273            match parse_ast(query) {
3274                Ok(ast) => {
3275                    assert_eq!(ast.statements.len(), 1, "{query}");
3276                    done += 1;
3277                }
3278                Err(error) => {
3279                    let message = error.to_string();
3280                    assert!(
3281                        message.starts_with("Not implemented Error"),
3282                        "{query} failed with {message}, which is not a not-implemented error"
3283                    );
3284                }
3285            }
3286        }
3287        // Not an assertion about the right number. It is a ratchet: this only moves up, and the
3288        // day it moves down somebody has taken a construct out without meaning to.
3289        assert!(done >= 31, "only {done} of the corpus transforms, which is fewer than it was");
3290    }
3291
3292    #[test]
3293    fn the_ast_is_far_smaller_than_the_parse_tree() {
3294        let query = CORPUS[4];
3295        let tree = parse(query).unwrap();
3296        let ast = parse_ast(query).unwrap();
3297        // The twenty precedence levels are the difference. Every one of them is a node in the
3298        // parse tree for every expression at every depth, and none of them survives into the AST.
3299        assert!(
3300            ast.node_count() * 20 < tree.arena_len(),
3301            "{} ast nodes against {} parse nodes",
3302            ast.node_count(),
3303            tree.arena_len()
3304        );
3305    }
3306
3307    #[test]
3308    fn precedence_comes_out_of_the_chain_and_into_the_tree() {
3309        assert_eq!(round("SELECT 1 + 2 * 3"), "SELECT (1 Add (2 Multiply 3))");
3310        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
3311        assert_eq!(round("SELECT 1 + 2 + 3"), "SELECT ((1 Add 2) Add 3)");
3312        assert_eq!(round("SELECT 1 - 2 - 3"), "SELECT ((1 Subtract 2) Subtract 3)");
3313        assert_eq!(
3314            round("SELECT a OR b AND c"),
3315            "SELECT (a Or (b And c))",
3316            "and binds tighter than or"
3317        );
3318    }
3319
3320    #[test]
3321    fn a_double_negation_is_two_nodes_and_not_none() {
3322        // Folding it would be an optimizer decision and this is not the optimizer. It also would
3323        // not be safe in general: `NOT NOT x` on a null is still null and on a non boolean it is
3324        // still an error, and both of those have to survive to the binder to be reported.
3325        assert_eq!(round("SELECT NOT NOT a"), "SELECT (Not (Not a))");
3326    }
3327
3328    #[test]
3329    fn a_parenthesised_single_expression_is_not_a_row() {
3330        assert_eq!(round("SELECT (a)"), "SELECT a");
3331        assert_eq!(round("SELECT (a, b)"), "SELECT ROW(a, b)");
3332    }
3333
3334    #[test]
3335    fn a_bracketed_list_is_a_list_of_however_many_items_were_written() {
3336        // One item is a list of one, which is where this parts company with the parenthesised form
3337        // above: `(a)` is `a` and `[a]` is a list, because the brackets are what say list.
3338        assert_eq!(round("SELECT [a]"), "SELECT [a]");
3339        assert_eq!(round("SELECT [1, 2, 3]"), "SELECT [1, 2, 3]");
3340        assert_eq!(round("SELECT []"), "SELECT []");
3341        assert_eq!(round("SELECT ['a.parquet', 'b.parquet']"), "SELECT ['a.parquet', 'b.parquet']");
3342    }
3343
3344    #[test]
3345    fn a_parameter_carries_its_identifier_however_it_was_written() {
3346        assert_eq!(round("SELECT $1"), "SELECT $1");
3347        assert_eq!(round("SELECT ?1"), "SELECT $1");
3348        assert_eq!(round("SELECT $name"), "SELECT $name");
3349        // A bare question mark is numbered by where it is, and the counting is its own, so a later
3350        // `$2` does not push the first one along. This is duckdb v1.4.1, which prints `$1 + $2`.
3351        assert_eq!(round("SELECT ? + $2"), "SELECT ($1 Add $2)");
3352        assert_eq!(round("SELECT ?, ?, ?"), "SELECT $1, $2, $3");
3353    }
3354
3355    #[test]
3356    fn the_parameters_of_a_statement_are_listed_once_each_in_written_order() {
3357        let ast = parse_ast("SELECT $b, $a, $b WHERE $a").expect("parses");
3358        assert_eq!(ast.parameters(), vec!["b", "a"]);
3359        assert!(parse_ast("SELECT 1").expect("parses").parameters().is_empty());
3360    }
3361
3362    #[test]
3363    fn the_three_ways_to_write_an_alias_all_arrive() {
3364        assert_eq!(round("SELECT a AS b"), "SELECT a AS b");
3365        assert_eq!(round("SELECT a b"), "SELECT a AS b");
3366        assert_eq!(round("SELECT b: a"), "SELECT a AS b");
3367        assert_eq!(round("SELECT a"), "SELECT a", "and no alias when none was written");
3368    }
3369
3370    #[test]
3371    fn a_from_with_no_select_selects_everything() {
3372        // DuckDB's own shorthand. Inventing the star here rather than in the binder means the
3373        // binder never has to know that the clause it is looking at was the one that was missing.
3374        assert_eq!(round("FROM t"), "SELECT * FROM t");
3375        assert_eq!(round("FROM t SELECT a"), "SELECT a FROM t");
3376    }
3377
3378    #[test]
3379    fn joins_nest_to_the_left() {
3380        assert_eq!(
3381            round("SELECT * FROM a JOIN b ON a.i = b.i LEFT JOIN c USING (k)"),
3382            "SELECT * FROM ((a Inner JOIN b ON (a.i Eq b.i)) Left JOIN c USING (k))"
3383        );
3384        assert_eq!(
3385            round("SELECT * FROM a NATURAL JOIN b"),
3386            "SELECT * FROM (a NATURAL Inner JOIN b)"
3387        );
3388        assert_eq!(round("SELECT * FROM a CROSS JOIN b"), "SELECT * FROM (a Cross JOIN b)");
3389        assert_eq!(
3390            round("SELECT * FROM a POSITIONAL JOIN b"),
3391            "SELECT * FROM (a Positional JOIN b)"
3392        );
3393        assert_eq!(round("SELECT * FROM a, b"), "SELECT * FROM a, b", "a comma is not a join node");
3394    }
3395
3396    #[test]
3397    fn a_qualified_name_keeps_its_parts_however_it_was_spelled() {
3398        // Five grammar rules can produce a column reference and they disagree about which
3399        // component is a schema and which is a table. None of that is decidable without the
3400        // catalog, so the AST holds the parts and the binder decides.
3401        assert_eq!(round("SELECT a"), "SELECT a");
3402        assert_eq!(round("SELECT t.a"), "SELECT t.a");
3403        assert_eq!(round("SELECT s.t.a"), "SELECT s.t.a");
3404        assert_eq!(round("SELECT c.s.t.a"), "SELECT c.s.t.a");
3405        assert_eq!(round("SELECT * FROM s.t"), "SELECT * FROM s.t");
3406    }
3407
3408    #[test]
3409    fn a_star_can_be_qualified() {
3410        assert_eq!(round("SELECT *"), "SELECT *");
3411        assert_eq!(round("SELECT t.*"), "SELECT t.*");
3412        assert_eq!(round("SELECT s.t.*"), "SELECT s.t.*");
3413    }
3414
3415    #[test]
3416    fn a_quoted_identifier_keeps_its_case_and_loses_its_quotes() {
3417        // DuckDB does not fold identifier case at any point, quoted or not, which the tokenizer
3418        // work established by reading the source. So the only thing to do here is take the quotes
3419        // off and resolve the doubled ones.
3420        let ast = parse_ast("SELECT \"Mixed Case\", \"a\"\"b\"").unwrap();
3421        assert_eq!(ast.strings[0], "Mixed Case");
3422        assert_eq!(ast.strings[1], "a\"b");
3423    }
3424
3425    #[test]
3426    fn a_string_literal_is_decoded_and_adjacent_ones_are_joined() {
3427        assert_eq!(round("SELECT 'it''s'"), "SELECT 'it's'");
3428        assert_eq!(round("SELECT 'a'\n'b'"), "SELECT 'ab'", "the standard's adjacency rule");
3429    }
3430
3431    /// Per #276, where the tag and the dollars were coming through as part of the value.
3432    #[test]
3433    fn a_dollar_quoted_string_loses_its_dollars_and_its_tag() {
3434        assert_eq!(round("SELECT $$dollar quoted$$"), "SELECT 'dollar quoted'");
3435        assert_eq!(round("SELECT $tag$body$tag$"), "SELECT 'body'");
3436        assert_eq!(round("SELECT $$$$"), "SELECT ''", "an empty tag and an empty body");
3437        // Nothing in the body is escaped, which is what the spelling is for, so a quote is a quote
3438        // and a dollar that is not the closing tag is a dollar.
3439        assert_eq!(round("SELECT $tag$it''s $other$ fine$tag$"), "SELECT 'it''s $other$ fine'");
3440        // An unterminated one has no closing tag to take off and keeps every byte it was given.
3441        assert_eq!(round("SELECT $$open"), "SELECT '$$open'");
3442    }
3443
3444    /// Per #329, where every prefixed spelling came back as the source text it was written as.
3445    ///
3446    /// The escapes are the ones the pinned binary takes, read off it one at a time. The two that
3447    /// are easy to get wrong are `\v`, which is not an escape and is the letter, and `\u`, which
3448    /// wants all four digits and otherwise drops the backslash and keeps the letter.
3449    #[test]
3450    fn an_escape_string_resolves_its_backslashes() {
3451        assert_eq!(round("SELECT E'a\\nb'"), "SELECT 'a\nb'");
3452        assert_eq!(round("SELECT e'a\\tb'"), "SELECT 'a\tb'", "the prefix is a letter, not a name");
3453        assert_eq!(round("SELECT E'a\\rb'"), "SELECT 'a\rb'");
3454        assert_eq!(round("SELECT E'a\\bb'"), "SELECT 'a\u{8}b'");
3455        assert_eq!(round("SELECT E'a\\fb'"), "SELECT 'a\u{c}b'");
3456        assert_eq!(round("SELECT E'a\\\\b'"), "SELECT 'a\\b'");
3457        assert_eq!(round("SELECT E'a\\'b'"), "SELECT 'a'b'", "a quote, the same as ''");
3458        assert_eq!(round("SELECT E'a''b'"), "SELECT 'a'b'", "and '' still means a quote here");
3459        // A backslash in front of anything else is dropped and the character is kept, which is what
3460        // makes \v the letter v.
3461        assert_eq!(round("SELECT E'a\\vb'"), "SELECT 'avb'");
3462        assert_eq!(round("SELECT E'a\\qb'"), "SELECT 'aqb'");
3463    }
3464
3465    /// The escapes that write a byte rather than a character, and the one that writes a character.
3466    #[test]
3467    fn a_numeric_escape_writes_the_byte_or_the_character_it_names() {
3468        assert_eq!(round("SELECT E'\\x41'"), "SELECT 'A'");
3469        assert_eq!(round("SELECT E'\\x4142'"), "SELECT 'A42'", "two digits at the most");
3470        assert_eq!(
3471            round("SELECT E'a\\x'"),
3472            "SELECT 'ax'",
3473            "and one at the least, or it is a letter"
3474        );
3475        assert_eq!(round("SELECT E'\\101'"), "SELECT 'A'");
3476        assert_eq!(round("SELECT E'\\1011'"), "SELECT 'A1'", "three digits at the most");
3477        assert_eq!(round("SELECT E'\\8'"), "SELECT '8'", "8 is not an octal digit");
3478        // Bytes and not characters, so two of them make one character and one of them makes none.
3479        assert_eq!(round("SELECT E'\\xc3\\xa9'"), "SELECT 'é'");
3480        assert_eq!(round("SELECT E'\\u00e9'"), "SELECT 'é'");
3481        assert_eq!(round("SELECT E'a\\u41'"), "SELECT 'au41'", "four digits or it is a letter");
3482        assert_eq!(round("SELECT E'a\\uZZZZ'"), "SELECT 'auZZZZ'");
3483        assert_eq!(
3484            round("SELECT E'\\ud83d\\ude00'"),
3485            "SELECT 'ud83dude00'",
3486            "surrogates are not it"
3487        );
3488    }
3489
3490    /// The two ways an escape string is not a string at all, both with the message upstream gives.
3491    #[test]
3492    fn an_escape_string_that_is_not_a_string_raises() {
3493        let error = parse_ast("SELECT E'a\\x00'").unwrap_err().to_string();
3494        assert_eq!(error, "Parser Error: Null character not permitted in escape string literal");
3495        let error = parse_ast("SELECT E'a\\377'").unwrap_err().to_string();
3496        assert_eq!(
3497            error,
3498            "Parser Error: Invalid UTF-8 in escape string literal at byte offset 1: byte mismatch",
3499            "the offset is where the bytes stop being a string, not where the escape was written"
3500        );
3501    }
3502
3503    /// The other prefixes, all of them measured against the pinned binary rather than assumed.
3504    #[test]
3505    fn the_other_string_prefixes_are_what_upstream_makes_of_them() {
3506        // N is the string and a cast of it to VARCHAR, which is where the column name comes from.
3507        assert_eq!(round("SELECT N'abc'"), "SELECT CAST('abc' AS VARCHAR)");
3508        assert_eq!(round("SELECT n'abc'"), "SELECT CAST('abc' AS VARCHAR)");
3509        // B is not a bit string. It is the letter b in front of the body, untouched.
3510        assert_eq!(round("SELECT B'101'"), "SELECT 'b101'");
3511        assert_eq!(round("SELECT b'abc'"), "SELECT 'babc'");
3512        assert_eq!(round("SELECT B''"), "SELECT 'b'", "an empty one is the letter on its own");
3513    }
3514
3515    /// X is the prefix that is not a string at all, per #329.
3516    ///
3517    /// What is kept is the text the blob prints as, because that is the text the column is named
3518    /// after and the text the cast reads the bytes back from, and one text that does both is one
3519    /// text that cannot disagree with itself.
3520    #[test]
3521    fn a_hex_string_is_a_blob_and_not_a_string() {
3522        assert_eq!(round("SELECT x'4142'"), "SELECT 'AB'::BLOB");
3523        assert_eq!(round("SELECT X'4142'"), "SELECT 'AB'::BLOB");
3524        assert_eq!(round("SELECT x'ff41'"), "SELECT '\\xFFA'::BLOB", "a byte that does not print");
3525        assert_eq!(round("SELECT x''"), "SELECT ''::BLOB", "an empty one is an empty blob");
3526        // A quote and a backslash are bytes that do not print either, which is what keeps the text
3527        // something the cast can read back.
3528        assert_eq!(round("SELECT x'2741'"), "SELECT '\\x27A'::BLOB");
3529        assert_eq!(round("SELECT x'5c7834314141'"), "SELECT '\\x5Cx41AA'::BLOB");
3530        // An odd number of digits is a parser error and a digit that is not one is not, because
3531        // upstream writes the pairs out without looking at them and the cast is what looks.
3532        let error = parse_ast("SELECT x'4'").unwrap_err().to_string();
3533        assert_eq!(
3534            error,
3535            "Parser Error: Hex string literal must have an even number of hex digits"
3536        );
3537        assert_eq!(round("SELECT x'41zz'"), "SELECT '\\x41\\xzz'::BLOB");
3538    }
3539
3540    #[test]
3541    fn the_null_and_boolean_tests_are_postfix_unary_operators() {
3542        assert_eq!(round("SELECT x IS NULL"), "SELECT (IsNull x)");
3543        assert_eq!(round("SELECT x IS NOT NULL"), "SELECT (IsNotNull x)");
3544        assert_eq!(round("SELECT x ISNULL"), "SELECT (IsNull x)");
3545        assert_eq!(round("SELECT x NOTNULL"), "SELECT (IsNotNull x)");
3546        assert_eq!(round("SELECT x IS TRUE"), "SELECT (IsTrue x)");
3547        assert_eq!(round("SELECT x IS NOT FALSE"), "SELECT (IsNotFalse x)");
3548        assert_eq!(round("SELECT x IS DISTINCT FROM y"), "SELECT (x IsDistinctFrom y)");
3549        assert_eq!(round("SELECT x IS NOT DISTINCT FROM y"), "SELECT (x IsNotDistinctFrom y)");
3550    }
3551
3552    #[test]
3553    fn the_like_family_folds_its_negation_into_the_operator() {
3554        assert_eq!(round("SELECT x LIKE 'a'"), "SELECT (x Like 'a')");
3555        assert_eq!(round("SELECT x NOT LIKE 'a'"), "SELECT (x NotLike 'a')");
3556        assert_eq!(round("SELECT x ILIKE 'a'"), "SELECT (x ILike 'a')");
3557        assert_eq!(round("SELECT x ~~ 'a'"), "SELECT (x Like 'a')", "the operator spelling");
3558        assert_eq!(round("SELECT x !~~ 'a'"), "SELECT (x NotLike 'a')");
3559        assert_eq!(round("SELECT x SIMILAR TO 'a'"), "SELECT (x SimilarTo 'a')");
3560        // Glob has no negated operator to fold into, so the negation stays where it was written.
3561        assert_eq!(round("SELECT x NOT GLOB 'a'"), "SELECT (Not (x Glob 'a'))");
3562    }
3563
3564    #[test]
3565    fn between_and_in_carry_their_negation_as_a_flag() {
3566        assert_eq!(round("SELECT x BETWEEN 1 AND 2"), "SELECT (x BETWEEN 1 AND 2)");
3567        assert_eq!(round("SELECT x NOT BETWEEN 1 AND 2"), "SELECT (NOT x BETWEEN 1 AND 2)");
3568        assert_eq!(round("SELECT x IN (1, 2)"), "SELECT (x IN [1, 2])");
3569        assert_eq!(round("SELECT x NOT IN (1, 2)"), "SELECT (NOT x IN [1, 2])");
3570    }
3571
3572    #[test]
3573    fn both_spellings_of_a_cast_are_the_same_node() {
3574        assert_eq!(round("SELECT CAST(x AS BIGINT)"), "SELECT CAST(x AS BIGINT)");
3575        assert_eq!(round("SELECT x::BIGINT"), "SELECT CAST(x AS BIGINT)");
3576        assert_eq!(round("SELECT TRY_CAST(x AS BIGINT)"), "SELECT TRY_CAST(x AS BIGINT)");
3577        assert_eq!(
3578            round("SELECT x::DECIMAL(18, 3)"),
3579            "SELECT CAST(x AS DECIMAL(18, 3))",
3580            "the type is kept as text because parsing it is the type system's job"
3581        );
3582    }
3583
3584    #[test]
3585    fn a_typed_literal_is_a_third_spelling_of_the_same_cast() {
3586        assert_eq!(round("SELECT DATE '1995-09-01'"), "SELECT CAST('1995-09-01' AS DATE)");
3587        assert_eq!(
3588            round("SELECT date '1995-09-01'"),
3589            "SELECT CAST('1995-09-01' AS date)",
3590            "the type is kept as written, the same as it is in the other two spellings"
3591        );
3592        assert_eq!(
3593            round("SELECT TIMESTAMP '2020-01-01 03:04:05'"),
3594            "SELECT CAST('2020-01-01 03:04:05' AS TIMESTAMP)"
3595        );
3596        assert_eq!(
3597            round("SELECT DECIMAL(5, 2) '1.5'"),
3598            "SELECT CAST('1.5' AS DECIMAL(5, 2))",
3599            "any type the cast takes is a typed literal, parameters and all"
3600        );
3601        assert_eq!(
3602            round("SELECT VARCHAR 'hi' FROM t"),
3603            "SELECT CAST('hi' AS VARCHAR) FROM t",
3604            "including the ones where the cast has nothing to do"
3605        );
3606    }
3607
3608    #[test]
3609    fn a_case_keeps_its_arms_in_order() {
3610        assert_eq!(
3611            round("SELECT CASE WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"),
3612            "SELECT CASE - WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"
3613        );
3614        assert_eq!(
3615            round("SELECT CASE x WHEN 1 THEN 'a' END"),
3616            "SELECT CASE x WHEN 1 THEN 'a' ELSE - END",
3617            "a simple case keeps the operand and a missing else is not an implicit null yet"
3618        );
3619    }
3620
3621    #[test]
3622    fn a_field_access_and_a_method_call_are_ordinary_function_calls() {
3623        // Which is what DuckDB makes of them too. Giving each its own AST node would mean the
3624        // binder needs a rule for something the function resolver already handles.
3625        assert_eq!(round("SELECT (f(x)).y"), "SELECT struct_extract(f(x), 'y')");
3626        assert_eq!(round("SELECT a[1]"), "SELECT array_extract(a, 1)");
3627    }
3628
3629    /// The four ways of leaving a bound out, all of which upstream fills in the same way.
3630    #[test]
3631    fn a_range_gets_the_bounds_the_query_left_out() {
3632        assert_eq!(round("SELECT a[1:2]"), "SELECT array_slice(a, 1, 2)");
3633        assert_eq!(round("SELECT a[:2]"), "SELECT array_slice(a, 1, 2)");
3634        assert_eq!(round("SELECT a[2:]"), "SELECT array_slice(a, 2, -1)");
3635        assert_eq!(round("SELECT a[:]"), "SELECT array_slice(a, 1, -1)");
3636        // `EndSliceMinus`, which is a range with no end rather than a subtraction of nothing.
3637        assert_eq!(round("SELECT a[1:-]"), "SELECT array_slice(a, 1, -1)");
3638        assert_eq!(round("SELECT a[1:2:3]"), "SELECT array_slice(a, 1, 2, 3)");
3639        // A step that was written and left empty, which upstream fills with a list so that the call
3640        // fails to bind. Answering a row here would be answering where the reference refuses.
3641        assert_eq!(round("SELECT a[1:2:]"), "SELECT array_slice(a, 1, 2, [])");
3642    }
3643
3644    /// `[]` is the one subscript the parser takes and the transformer refuses, in upstream's words.
3645    #[test]
3646    fn an_empty_subscript_is_not_a_subscript() {
3647        let error = parse_ast("SELECT a[]").expect_err("an empty subscript");
3648        assert_eq!(error.message(), "Empty subscript '[]' is not allowed");
3649    }
3650
3651    /// A rule that wrote a keyword is not a rule that said nothing, however few children it has.
3652    /// Per #313.
3653    #[test]
3654    fn a_keyword_is_not_stepped_through_on_the_way_to_its_one_argument() {
3655        for (sql, rule) in [
3656            ("SELECT row(1)", "RowExpression"),
3657            ("SELECT try(1)", "TryExpression"),
3658            ("SELECT unpack([1])", "UnpackExpression"),
3659            ("SELECT columns('a')", "ColumnsExpression"),
3660        ] {
3661            let error = parse_ast(sql).expect_err(sql);
3662            assert!(error.message().ends_with(rule), "{sql}: {error}");
3663        }
3664        // Grouping brackets really do say nothing, and that is the one rule of this shape that is
3665        // stepped through rather than refused.
3666        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
3667        assert_eq!(round("SELECT -(7)"), "SELECT (Negate 7)");
3668    }
3669
3670    /// The three spellings of a null check, two of which are their own grammar rule. Per #306.
3671    #[test]
3672    fn the_null_checks_are_calls_by_the_names_duckdb_prints() {
3673        // The keyword is the name, so the call is written with the canonical spelling of it whichever
3674        // case the query used. What the column is called is the binder's to decide.
3675        assert_eq!(round("SELECT COALESCE(a, b, 1)"), "SELECT coalesce(a, b, 1)");
3676        assert_eq!(round("SELECT coalesce(a)"), "SELECT coalesce(a)");
3677        assert_eq!(round("SELECT NULLIF(a, 1)"), "SELECT nullif(a, 1)");
3678        // `IFNULL` is a plain call that upstream's parser turns into the operator, qualifier and all.
3679        assert_eq!(round("SELECT ifnull(a, 1)"), "SELECT coalesce(a, 1)");
3680        assert_eq!(round("SELECT main.ifnull(a, 1)"), "SELECT coalesce(a, 1)");
3681        let error = parse_ast("SELECT ifnull(a)").expect_err("one argument to ifnull");
3682        assert_eq!(error.message(), "Wrong number of arguments to IFNULL.");
3683        let error = parse_ast("SELECT ifnull(a, b, c)").expect_err("three arguments to ifnull");
3684        assert_eq!(error.message(), "Wrong number of arguments to IFNULL.");
3685    }
3686
3687    /// The four string functions with a grammar rule of their own, written back out as the calls
3688    /// DuckDB's parser writes them as. Per #314.
3689    #[test]
3690    fn the_string_keywords_are_the_calls_duckdb_prints() {
3691        assert_eq!(round("SELECT substring(s, 2, 3)"), "SELECT substring(s, 2, 3)");
3692        assert_eq!(round("SELECT SUBSTRING(s FROM 2 FOR 3)"), "SELECT substring(s, 2, 3)");
3693        assert_eq!(round("SELECT substring(s FROM 2)"), "SELECT substring(s, 2)");
3694        // The `FOR` on its own is three arguments and not two, with the start filled in.
3695        assert_eq!(round("SELECT substring(s FOR 3)"), "SELECT substring(s, 1, 3)");
3696        // The haystack comes first in the call and second in the query.
3697        assert_eq!(round("SELECT position('c' IN s)"), "SELECT position(s, 'c')");
3698        assert_eq!(round("SELECT trim(s)"), "SELECT trim(s)");
3699        assert_eq!(round("SELECT trim(BOTH 'x' FROM s)"), "SELECT trim(s, 'x')");
3700        assert_eq!(round("SELECT trim(BOTH FROM s)"), "SELECT trim(s)");
3701        assert_eq!(round("SELECT trim(s, 'xy')"), "SELECT trim(s, 'xy')");
3702        // A direction is a different function and not a different argument.
3703        assert_eq!(round("SELECT trim(LEADING FROM s)"), "SELECT ltrim(s)");
3704        assert_eq!(round("SELECT trim(TRAILING FROM s)"), "SELECT rtrim(s)");
3705        assert_eq!(round("SELECT trim(LEADING 'x' FROM s)"), "SELECT ltrim(s, 'x')");
3706        assert_eq!(round("SELECT trim(TRAILING 'x' FROM s)"), "SELECT rtrim(s, 'x')");
3707        assert_eq!(
3708            round("SELECT overlay(s PLACING 'X' FROM 2 FOR 1)"),
3709            "SELECT overlay(s, 'X', 2, 1)"
3710        );
3711        assert_eq!(round("SELECT overlay(s PLACING 'X' FROM 2)"), "SELECT overlay(s, 'X', 2)");
3712        assert_eq!(round("SELECT overlay(s, 'X', 2, 1)"), "SELECT overlay(s, 'X', 2, 1)");
3713    }
3714
3715    #[test]
3716    fn an_aggregate_keeps_its_distinct() {
3717        assert_eq!(round("SELECT count(*)"), "SELECT count(*)");
3718        assert_eq!(round("SELECT count(DISTINCT x)"), "SELECT count(DISTINCT x)");
3719        assert_eq!(round("SELECT count(ALL x)"), "SELECT count(x)");
3720        assert_eq!(round("SELECT main.count(x)"), "SELECT main.count(x)");
3721    }
3722
3723    #[test]
3724    fn the_modifiers_hang_off_the_query_and_not_off_the_select() {
3725        // `a UNION b ORDER BY x` sorts the union. Putting the order by on the select would have
3726        // made that unrepresentable, which is why the grammar puts it outside the chain and why
3727        // the AST follows.
3728        assert_eq!(
3729            round("SELECT 1 UNION ALL SELECT 2 ORDER BY 1"),
3730            "(SELECT 1 Union All SELECT 2) ORDER BY 1 Unstated Unstated"
3731        );
3732        assert_eq!(
3733            round("SELECT a FROM t UNION SELECT b FROM u EXCEPT SELECT c FROM v"),
3734            "((SELECT a FROM t Union Unstated SELECT b FROM u) Except Unstated SELECT c FROM v)",
3735            "set operators are left associative"
3736        );
3737        assert_eq!(
3738            round("SELECT 1 UNION SELECT 2 INTERSECT SELECT 3"),
3739            "(SELECT 1 Union Unstated (SELECT 2 Intersect Unstated SELECT 3))",
3740            "and intersect binds tighter than the other two"
3741        );
3742    }
3743
3744    #[test]
3745    fn the_sort_and_limit_clauses_keep_what_was_written() {
3746        assert_eq!(
3747            round("SELECT a FROM t ORDER BY a"),
3748            "SELECT a FROM t ORDER BY a Unstated Unstated"
3749        );
3750        assert_eq!(
3751            round("SELECT a FROM t ORDER BY a DESC NULLS LAST"),
3752            "SELECT a FROM t ORDER BY a Descending Last"
3753        );
3754        assert_eq!(round("SELECT a FROM t ORDER BY ALL"), "SELECT a FROM t ORDER BY ALL");
3755        assert_eq!(round("SELECT a FROM t GROUP BY ALL"), "SELECT a FROM t GROUP BY ALL");
3756        assert_eq!(round("SELECT a FROM t LIMIT 10 OFFSET 5"), "SELECT a FROM t LIMIT 10 OFFSET 5");
3757        assert_eq!(round("SELECT a FROM t OFFSET 5 LIMIT 10"), "SELECT a FROM t LIMIT 10 OFFSET 5");
3758        assert_eq!(round("SELECT a FROM t LIMIT 10%"), "SELECT a FROM t LIMIT 10%");
3759        assert_eq!(round("SELECT a FROM t LIMIT ALL"), "SELECT a FROM t", "which is no limit");
3760    }
3761
3762    #[test]
3763    fn a_subquery_appears_in_both_places_it_can() {
3764        assert_eq!(
3765            round("SELECT * FROM (SELECT x FROM t) AS s"),
3766            "SELECT * FROM (SELECT x FROM t) AS s"
3767        );
3768        assert_eq!(round("SELECT (SELECT 1)"), "SELECT (SELECT 1)");
3769    }
3770
3771    #[test]
3772    fn distinct_on_keeps_its_expressions() {
3773        assert_eq!(round("SELECT DISTINCT a"), "SELECT DISTINCT a");
3774        assert_eq!(round("SELECT ALL a"), "SELECT a", "which is the default written out");
3775        assert_eq!(round("SELECT DISTINCT ON (a, b) a"), "SELECT DISTINCT ON (a, b) a");
3776    }
3777
3778    #[test]
3779    fn an_operator_the_dialect_does_not_name_is_kept_by_name() {
3780        // The grammar text says `OperatorLiteral <- Identifier`, which reads as though any bare
3781        // word could be written infix. It cannot. That rule is one of the 24 the matcher overrides
3782        // and it is overridden to the bare operator matcher, so what it takes is a run of operator
3783        // characters. Believing the body here would have produced a transformer that accepted
3784        // `a foo b`, which DuckDB rejects.
3785        assert_eq!(round("SELECT a <=> b"), "SELECT (a <=> b)");
3786        assert!(parse_ast("SELECT a foo b").is_err(), "a bare word is not an operator");
3787    }
3788
3789    #[test]
3790    fn a_script_is_a_list_of_statements() {
3791        let ast = parse_ast("SELECT 1; SELECT 2;").unwrap();
3792        assert_eq!(ast.statements.len(), 2);
3793        // A trailing semicolon makes an empty top level statement in the parse tree, because the
3794        // grammar's `Statement? (';'+ / EndOfInput)` is happy with nothing on both sides. It is
3795        // dropped here rather than pretended away in the matcher.
3796        let Statement::Query(second) = ast.statements[1] else {
3797            panic!("the second statement is a query");
3798        };
3799        assert_eq!(show_query(&ast, second), "SELECT 2");
3800    }
3801
3802    #[test]
3803    fn an_unsupported_construct_names_itself_and_what_was_written() {
3804        let error = parse_ast("ALTER TABLE t ADD COLUMN a INTEGER").unwrap_err().to_string();
3805        assert!(error.starts_with("Not implemented Error"), "{error}");
3806        assert!(error.contains("ALTER TABLE t ADD COLUMN a INTEGER"), "{error}");
3807        assert!(error.contains("AlterStatement"), "{error}");
3808    }
3809
3810    #[test]
3811    fn a_long_construct_is_cut_short_in_the_message() {
3812        let query = format!("ALTER TABLE t ADD COLUMN {} INTEGER", "a".repeat(80));
3813        let error = parse_ast(&query).unwrap_err().to_string();
3814        assert!(error.contains("..."), "{error}");
3815        assert!(error.len() < 200, "{error}");
3816    }
3817
3818    #[test]
3819    fn the_transformer_never_panics_on_anything_the_matcher_accepts() {
3820        // The matcher accepts a good deal that means nothing, because the grammar does. Every one
3821        // of these parses and none of them is a statement this milestone covers, and the contract
3822        // is that the answer is an error either way.
3823        for query in [
3824            "SELECT",
3825            "FROM t SELECT",
3826            "SELECT * FROM t WHERE",
3827            "SELECT ()",
3828            "SELECT a FROM t GROUP BY ()",
3829        ] {
3830            let answer = parse_ast(query);
3831            if let Err(error) = answer {
3832                let message = error.to_string();
3833                assert!(
3834                    message.starts_with("Not implemented Error")
3835                        || message.starts_with("Parser Error"),
3836                    "{query} failed with {message}"
3837                );
3838            }
3839        }
3840    }
3841
3842    #[test]
3843    fn a_file_name_in_a_from_clause_is_a_table_name_with_the_quotes_off() {
3844        // Both spellings have to arrive as the same name, because the binder decides whether it is
3845        // a file by looking at the name, and `'hits.parquet'` with the quotes still on it is not
3846        // a path that anything can open.
3847        assert_eq!(round("SELECT * FROM 'hits.parquet'"), "SELECT * FROM hits.parquet");
3848        assert_eq!(round("SELECT * FROM \"hits.parquet\""), "SELECT * FROM hits.parquet");
3849        assert_eq!(round("SELECT * FROM 'hits.parquet' AS h"), "SELECT * FROM hits.parquet AS h");
3850    }
3851
3852    #[test]
3853    fn a_function_call_in_a_from_clause_is_a_source_and_not_an_expression() {
3854        assert_eq!(round("SELECT * FROM range(3)"), "SELECT * FROM range(3)");
3855        assert_eq!(round("SELECT * FROM range(1, 10, 2)"), "SELECT * FROM range(1, 10, 2)");
3856        assert_eq!(round("SELECT * FROM main.range(3)"), "SELECT * FROM main.range(3)");
3857        assert_eq!(round("SELECT * FROM range(3) AS t"), "SELECT * FROM range(3) AS t");
3858        // The grammar allows a call with no arguments here and the transformer keeps it, because
3859        // whether a particular function takes none is the binder's question and not this one's.
3860        assert_eq!(round("SELECT * FROM some_function()"), "SELECT * FROM some_function()");
3861    }
3862
3863    #[test]
3864    fn the_forms_of_a_table_function_this_does_not_cover_are_turned_away_by_name() {
3865        for query in [
3866            "SELECT * FROM range(3) WITH ORDINALITY",
3867            "SELECT * FROM LATERAL range(3)",
3868            "SELECT * FROM t: range(3)",
3869        ] {
3870            let error = parse_ast(query).unwrap_err().to_string();
3871            assert!(error.contains("grammar rule"), "{query} failed with {error}");
3872        }
3873    }
3874
3875    #[test]
3876    fn interning_means_a_name_written_twice_is_stored_once() {
3877        let ast = parse_ast("SELECT a, a, a FROM t WHERE a = a").unwrap();
3878        assert_eq!(ast.strings.iter().filter(|text| *text == "a").count(), 1);
3879    }
3880}