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