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