Skip to main content

rudb_parse/
transform.rs

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