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