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