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