Skip to main content

rudb_parse/
transform.rs

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