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