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