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