Skip to main content

rudb_parse/
transform.rs

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