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