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