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