Skip to main content

rudb_parse/
transform.rs

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