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        for name in ["WithinGroupClause", "ExportClause"] {
4023            let clause = self.find(node, name);
4024            if clause != NONE {
4025                return self.unsupported(clause);
4026            }
4027        }
4028        // `FilterClauseContents <- 'WHERE'? Expression`, so the word is optional and the predicate
4029        // is the last thing under it either way. Whether the call is allowed to carry one at all is
4030        // the binder's question, because it is a question about what the name resolves to.
4031        let clause = self.find(node, "FilterClause");
4032        let written =
4033            if clause == NONE { NONE } else { self.descendant(clause, "FilterClauseContents") };
4034        let filter = if written == NONE {
4035            NONE
4036        } else {
4037            let predicate = self.kids(written).last().unwrap_or(NONE);
4038            self.expr(predicate)?
4039        };
4040        let over = self.find(node, "OverClause");
4041        let name = self.name_parts(self.first(node));
4042        // `FunctionExpressionArguments <- Parens(FunctionExpressionArgumentList)` and
4043        // `FunctionExpressionArgumentList <- DistinctOrAll? FunctionArgumentList? OrderByClause?
4044        // IgnoreOrRespectNulls?`, so a call with no arguments still has both wrappers.
4045        let list = self.first(self.nth(node, 1));
4046        // An `ORDER BY` written inside the brackets is the order the call reads its rows in, which
4047        // is a different thing from the `ORDER BY` in an `OVER` and is written in a different place.
4048        // On a call without an `OVER` it is kept beside the call, for the binder to decide whether
4049        // the aggregate it names cares about the order.
4050        let inside = self.find(list, "OrderByClause");
4051        let inner = if inside == NONE {
4052            Slice { start: 0, len: 0 }
4053        } else {
4054            // `ORDER BY ALL` names the call's own arguments rather than a list of keys, and what the
4055            // reference binary does with it in here is not the ordinary reading of the words, so it
4056            // is turned down rather than guessed at.
4057            let (items, all) = self.order_by(inside)?;
4058            if all {
4059                return self.unsupported(inside);
4060            }
4061            self.order_slice(items)
4062        };
4063        // Either word is a window modifier and nothing else carries one, so an ordinary call that
4064        // writes one is turned down here, in the sentence the pin turns it down with.
4065        let nulls = self.find(list, "IgnoreOrRespectNulls");
4066        if nulls != NONE && over == NONE {
4067            return Err(Error::parser(
4068                "RESPECT/IGNORE NULLS is not supported for non-window functions",
4069            ));
4070        }
4071        let ignore_nulls = nulls != NONE && self.name(self.first(nulls)) == "IgnoreNulls";
4072        let distinct = self.quantifier(self.find(list, "DistinctOrAll")) == Quantifier::Distinct;
4073        let mut args = Vec::new();
4074        let mut names = Vec::new();
4075        let mut first_named = NONE;
4076        let arguments = self.find(list, "FunctionArgumentList");
4077        if arguments != NONE {
4078            for kid in self.kids(arguments) {
4079                let (name, arg) = self.argument(kid)?;
4080                if name == NONE && !names.is_empty() {
4081                    return Err(Error::binder(format!(
4082                        "Positional argument '{}' cannot follow named arguments in function call.",
4083                        self.text(kid)
4084                    )));
4085                }
4086                if name != NONE {
4087                    if names.is_empty() {
4088                        first_named = kid;
4089                    }
4090                    names.push(name);
4091                }
4092                args.push(arg);
4093            }
4094        }
4095        // `struct_pack(a := 1)` is the one call whose names are part of its value, and it is the
4096        // same struct `{'a': 1}` is, so it becomes that. `struct_pack()` is the empty struct and a
4097        // call with any positional argument stays a call, for the binder to turn down in the pin's
4098        // words. `struct_insert(s, b := 2)` and `struct_update` take the named arguments as the
4099        // fields to add or replace, so those are gathered into one struct handed over as the last
4100        // argument. A name on any other call is a parameter the binder does not have yet.
4101        let called = if name.len == 1 {
4102            self.ast.name(name).last().map(str::to_ascii_lowercase).unwrap_or_default()
4103        } else {
4104            String::new()
4105        };
4106        let packs = called == "struct_pack";
4107        if packs && over == NONE && names.len() == args.len() {
4108            let names = self.part_slice(names);
4109            let values = self.expr_slice(args);
4110            return Ok(self.push(Expr::Struct { names, values }));
4111        }
4112        let merges = matches!(called.as_str(), "struct_insert" | "struct_update");
4113        let rewritten = packs || merges || called == "unnest" || called == "ifnull";
4114        if inside != NONE && over == NONE && rewritten {
4115            return self.unsupported(inside);
4116        }
4117        if merges && over == NONE && !names.is_empty() && names.len() + 1 == args.len() {
4118            let names = self.part_slice(names);
4119            let values = self.expr_slice(args.split_off(1));
4120            args.push(self.push(Expr::Struct { names, values }));
4121        } else if called == "unnest" && over == NONE && !names.is_empty() {
4122            // `unnest(l, recursive := true)` keeps its options beside the call, where the binder
4123            // looks for them, and the call itself is the positional arguments alone.
4124            let values = args.split_off(args.len() - names.len());
4125            let named: Vec<Target> =
4126                names.into_iter().zip(values).map(|(alias, expr)| Target { expr, alias }).collect();
4127            let named = self.target_slice(named);
4128            let args = self.expr_slice(args);
4129            let call = self.push(Expr::Function { name, args, distinct, filter });
4130            self.ast.named_args.push((call, named));
4131            return Ok(call);
4132        } else if !names.is_empty() && (!packs || names.len() == args.len()) {
4133            return self.unsupported(first_named);
4134        }
4135        // A call with an `OVER` on it is a window call and none of the rewrites below apply to it.
4136        // The reference binary agrees on the one case where that is visible: `ifnull(1) OVER ()`
4137        // keeps its name and its one argument and is turned down for not naming an aggregate,
4138        // where the same call without the `OVER` is a rewrite and an arity error.
4139        if over != NONE {
4140            let args = self.expr_slice(args);
4141            let spec = self.over(over)?;
4142            return Ok(self.push(Expr::Window {
4143                name,
4144                args,
4145                distinct,
4146                filter,
4147                ignore_nulls,
4148                order: inner,
4149                spec,
4150            }));
4151        }
4152        // `IFNULL` is an ordinary call in the grammar and is not one by the time DuckDB's parser is
4153        // done with it: `ifnull(NULL, 3)` comes back named `COALESCE(NULL, 3)` there, and so does
4154        // `main.ifnull(NULL, 3)`, so the qualifier goes with the rewrite. The count is checked here
4155        // because that is where upstream checks it, with the sentence below rather than the binder's
4156        // arity error, and it is checked before the two arguments are looked at.
4157        if self.ast.name(name).last().is_some_and(|part| part.eq_ignore_ascii_case("ifnull")) {
4158            if args.len() != 2 {
4159                return Err(Error::parser("Wrong number of arguments to IFNULL."));
4160            }
4161            let args = self.expr_slice(args);
4162            let name = self.function_name("coalesce");
4163            return Ok(self.push(Expr::Function { name, args, distinct, filter }));
4164        }
4165        let args = self.expr_slice(args);
4166        let call = self.push(Expr::Function { name, args, distinct, filter });
4167        if inside != NONE {
4168            self.ast.aggregate_orders.push((call, inner));
4169        }
4170        Ok(call)
4171    }
4172
4173    // Windows.
4174
4175    /// `WindowClause <- 'WINDOW' List(WindowDefinition)` and
4176    /// `WindowDefinition <- Identifier 'AS' WindowFrameDefinition`.
4177    ///
4178    /// The definitions are read in the order they were written and each one can see the ones before
4179    /// it, so `WINDOW w AS (ORDER BY i), v AS (w)` defines two windows that order the same way.
4180    fn window_clause(&mut self, node: u32) -> Result<()> {
4181        for kid in self.kids(node) {
4182            if self.name(kid) != "WindowDefinition" {
4183                continue;
4184            }
4185            let name = self.identifier(self.first(kid));
4186            let definition = self.find(kid, "WindowFrameDefinition");
4187            if definition == NONE {
4188                return self.unsupported(kid);
4189            }
4190            let (spec, framed) = self.window_definition(definition)?;
4191            let spec = self.push_window(spec);
4192            self.named_windows.push((name, spec, framed));
4193        }
4194        Ok(())
4195    }
4196
4197    /// `OverClause <- 'OVER' WindowFrame` and
4198    /// `WindowFrame <- ParensIdentifier / WindowFrameDefinition / IdentifierWindowFrame`.
4199    ///
4200    /// The first and the third spelling are a bare reference, written `OVER (w)` and `OVER w`, and
4201    /// both resolve to the window that name was given. A reference is resolved here rather than
4202    /// carried, because that is where the reference binary resolves it: a name nobody defined is a
4203    /// `Parser Error` there, and a view written with one comes back out of the catalog with the
4204    /// definition written in its place.
4205    fn over(&mut self, node: u32) -> Result<WindowRef> {
4206        let mut frame = self.first(node);
4207        if self.name(frame) == "WindowFrame" {
4208            frame = self.first(frame);
4209        }
4210        match self.name(frame) {
4211            "ParensIdentifier" | "IdentifierWindowFrame" => {
4212                let name = self.identifier(self.first(frame));
4213                let (spec, _) = self.named_window(name)?;
4214                Ok(spec)
4215            }
4216            "WindowFrameDefinition" => {
4217                let (spec, _) = self.window_definition(frame)?;
4218                Ok(self.push_window(spec))
4219            }
4220            _ => self.unsupported(frame),
4221        }
4222    }
4223
4224    /// The window a name stands for, and whether its definition wrote a frame clause.
4225    fn named_window(&self, name: StrRef) -> Result<(WindowRef, bool)> {
4226        let written = self.ast.string(name);
4227        let found = self
4228            .named_windows
4229            .iter()
4230            .rev()
4231            .find(|&&(defined, _, _)| self.ast.string(defined).eq_ignore_ascii_case(written));
4232        match found {
4233            Some(&(_, spec, framed)) => Ok((spec, framed)),
4234            // The doubled quotes are upstream's and not a slip here. It writes the name with the
4235            // quoting a printed identifier gets and then writes quotes around that as well, so a
4236            // window called `w` is reported as `""w""`.
4237            None => Err(Error::parser(format!("window \"\"{written}\"\" does not exist"))),
4238        }
4239    }
4240
4241    /// `WindowFrameDefinition <- WindowFrameNameContentsParens / WindowFrameContentsParens`,
4242    /// `WindowFrameNameContents <- BaseWindowName? WindowFrameContents` and
4243    /// `WindowFrameContents <- WindowPartition? OrderByClause? FrameClause?`.
4244    ///
4245    /// Returns the window and whether a frame clause was written, which the caller needs because a
4246    /// definition that wrote one cannot be used as the base of another.
4247    fn window_definition(&mut self, node: u32) -> Result<(WindowSpec, bool)> {
4248        let held = self.first(self.first(node));
4249        let (base, contents) = match self.name(held) {
4250            "WindowFrameNameContents" => {
4251                (self.find(held, "BaseWindowName"), self.find(held, "WindowFrameContents"))
4252            }
4253            "WindowFrameContents" => (NONE, held),
4254            _ => return self.unsupported(held),
4255        };
4256        if contents == NONE {
4257            return self.unsupported(node);
4258        }
4259        let partition = self.find(contents, "WindowPartition");
4260        let order = self.find(contents, "OrderByClause");
4261        let frame = self.find(contents, "FrameClause");
4262        let mut spec = WindowSpec::empty();
4263        if base != NONE {
4264            let name = self.identifier(self.first(base));
4265            let written = self.ast.string(name).to_string();
4266            let (found, framed) = self.named_window(name)?;
4267            // The three refusals are upstream's, in its words. What they have in common is that a
4268            // base window is copied and not merged, so anything the copy would have to combine with
4269            // something the base already said is turned down rather than guessed at.
4270            if framed {
4271                return Err(Error::parser(format!(
4272                    "cannot copy window \"{written}\" because it has a frame clause"
4273                )));
4274            }
4275            spec = self.ast.window(found);
4276            if partition != NONE && !spec.partition.is_empty() {
4277                return Err(Error::parser(format!(
4278                    "Cannot override PARTITION BY clause of window \"{written}\""
4279                )));
4280            }
4281            if order != NONE && !spec.order.is_empty() {
4282                return Err(Error::parser(format!(
4283                    "Cannot override ORDER BY clause of window \"{written}\""
4284                )));
4285            }
4286        }
4287        if partition != NONE {
4288            let mut items = Vec::new();
4289            for kid in self.kids(partition) {
4290                items.push(self.expr(kid)?);
4291            }
4292            spec.partition = self.expr_slice(items);
4293        }
4294        if order != NONE {
4295            let (items, all) = self.order_by(order)?;
4296            if all {
4297                return self.unsupported(order);
4298            }
4299            spec.order = self.order_slice(items);
4300        }
4301        if frame != NONE {
4302            self.frame_clause(&mut spec, frame)?;
4303        }
4304        Ok((spec, frame != NONE))
4305    }
4306
4307    /// `FrameClause <- Framing FrameExtent WindowExcludeClause?`.
4308    ///
4309    /// One normalisation happens here and it is the reference binary's. A frame that runs from the
4310    /// first row of the partition to the last says the same thing however it is measured, so
4311    /// `RANGE` and `GROUPS` become `ROWS` when both ends are unbounded. It matters because the
4312    /// printed form of a window is the column name a target with no alias gets, and upstream prints
4313    /// `ROWS` for all three spellings.
4314    fn frame_clause(&mut self, spec: &mut WindowSpec, node: u32) -> Result<()> {
4315        let framing = self.first(self.find(node, "Framing"));
4316        spec.unit = match self.name(framing) {
4317            "RowsFraming" => WindowUnit::Rows,
4318            "RangeFraming" => WindowUnit::Range,
4319            "GroupsFraming" => WindowUnit::Groups,
4320            _ => return self.unsupported(framing),
4321        };
4322        let extent = self.first(self.find(node, "FrameExtent"));
4323        match self.name(extent) {
4324            // `SingleFrameExtent <- FrameBound`, which names the start and leaves the end at the
4325            // current row.
4326            "SingleFrameExtent" => {
4327                spec.start = self.frame_bound(self.first(extent))?;
4328                spec.end = WindowBound::CurrentRow;
4329            }
4330            // `BetweenFrameExtent <- 'BETWEEN' FrameBound 'AND' FrameBound`.
4331            "BetweenFrameExtent" => {
4332                spec.start = self.frame_bound(self.first(extent))?;
4333                spec.end = self.frame_bound(self.nth(extent, 1))?;
4334            }
4335            _ => return self.unsupported(extent),
4336        }
4337        let exclude = self.find(node, "WindowExcludeClause");
4338        if exclude != NONE {
4339            let element = self.first(self.first(exclude));
4340            spec.exclude = match self.name(element) {
4341                "ExcludeCurrentRow" => WindowExclude::CurrentRow,
4342                "ExcludeGroup" => WindowExclude::Group,
4343                "ExcludeTies" => WindowExclude::Ties,
4344                "ExcludeNoOthers" => WindowExclude::NoOthers,
4345                _ => return self.unsupported(element),
4346            };
4347        }
4348        if spec.start == WindowBound::UnboundedPreceding
4349            && spec.end == WindowBound::UnboundedFollowing
4350        {
4351            spec.unit = WindowUnit::Rows;
4352        }
4353        Ok(())
4354    }
4355
4356    /// `FrameBound <- FrameUnbounded / FrameCurrentRow / FrameExpression`.
4357    fn frame_bound(&mut self, node: u32) -> Result<WindowBound> {
4358        let inner = if self.name(node) == "FrameBound" { self.first(node) } else { node };
4359        match self.name(inner) {
4360            "FrameCurrentRow" => Ok(WindowBound::CurrentRow),
4361            // `FrameUnbounded <- 'UNBOUNDED' PrecedingOrFollowing`.
4362            "FrameUnbounded" => {
4363                if self.preceding(self.first(inner)) {
4364                    Ok(WindowBound::UnboundedPreceding)
4365                } else {
4366                    Ok(WindowBound::UnboundedFollowing)
4367                }
4368            }
4369            // `FrameExpression <- Expression PrecedingOrFollowing`.
4370            "FrameExpression" => {
4371                let offset = self.expr(self.first(inner))?;
4372                if self.preceding(self.nth(inner, 1)) {
4373                    Ok(WindowBound::Preceding(offset))
4374                } else {
4375                    Ok(WindowBound::Following(offset))
4376                }
4377            }
4378            _ => self.unsupported(inner),
4379        }
4380    }
4381
4382    /// `PrecedingOrFollowing <- PrecedingFrame / FollowingFrame`, which of the two it was.
4383    fn preceding(&self, node: u32) -> bool {
4384        self.name(self.first(node)) == "PrecedingFrame"
4385    }
4386
4387    /// `CoalesceExpression <- 'COALESCE' Parens(List(Expression))`.
4388    ///
4389    /// A keyword is not a child and the two wrappers are transparent, so the children are the
4390    /// arguments. One of them is enough for the grammar and none of them is a syntax error, which is
4391    /// why there is no count checked here.
4392    ///
4393    /// The call is written with the canonical name rather than the one the query used, since there is
4394    /// nothing else to keep: the keyword is the name. Upstream prints the column in capitals whatever
4395    /// case was written, because `COALESCE` is an operator there and not a function name that its
4396    /// parser folded, and the binder is where that is decided here.
4397    fn coalesce(&mut self, node: u32) -> Result<ExprRef> {
4398        let mut args = Vec::new();
4399        for kid in self.kids(node) {
4400            args.push(self.expr(kid)?);
4401        }
4402        let args = self.expr_slice(args);
4403        let name = self.function_name("coalesce");
4404        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
4405    }
4406
4407    /// `LambdaExpression <- 'LAMBDA' List(ColIdOrString) ':' Expression`.
4408    ///
4409    /// Every child but the last is a parameter, since the list and the keyword leave no node of
4410    /// their own behind, and the last one is the body. A parameter is a name and is read the way a
4411    /// column name is, so `lambda "x": x` is the parameter `x` and `lambda X: x` keeps its case for
4412    /// the column heading and still answers to `x`, which the binder matches without case.
4413    fn lambda(&mut self, node: u32) -> Result<ExprRef> {
4414        let kids: Vec<u32> = self.kids(node).collect();
4415        let Some((&body, params)) = kids.split_last() else {
4416            return self.unsupported(node);
4417        };
4418        if params.is_empty() {
4419            return self.unsupported(node);
4420        }
4421        let mut names = Vec::with_capacity(params.len());
4422        for &param in params {
4423            names.push(self.identifier(param));
4424        }
4425        let params = self.part_slice(names);
4426        let body = self.expr(body)?;
4427        Ok(self.push(Expr::Lambda { params, body }))
4428    }
4429
4430    /// `NullIfExpression <- 'NULLIF' Parens(NullIfArguments)` and
4431    /// `NullIfArguments <- Expression ',' Expression`.
4432    ///
4433    /// Exactly two arguments, because the rule says so: `nullif(1)` and `nullif(1, 2, 3)` are syntax
4434    /// errors upstream and are syntax errors here for the same reason, so there is no arity to check
4435    /// after the parse.
4436    ///
4437    /// It stays a function called `nullif` rather than becoming the `CASE` upstream's macro expands
4438    /// to, since the column it produces is named after the call and not after the expansion.
4439    /// `TryExpression <- 'TRY' Parens(Expression)`, kept as a call to `try` that the binder and the
4440    /// executor know is not a function. The pin prints it as `TRY(...)` and so does this.
4441    fn try_expression(&mut self, node: u32) -> Result<ExprRef> {
4442        let kids: Vec<u32> = self.kids(node).collect();
4443        let [only] = kids[..] else {
4444            return self.unsupported(node);
4445        };
4446        let inner = self.expr(only)?;
4447        let args = self.expr_slice(vec![inner]);
4448        let name = self.function_name("try");
4449        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
4450    }
4451
4452    fn null_if(&mut self, node: u32) -> Result<ExprRef> {
4453        let arguments = self.find(node, "NullIfArguments");
4454        if arguments == NONE {
4455            return self.unsupported(node);
4456        }
4457        let mut args = Vec::new();
4458        for kid in self.kids(arguments) {
4459            args.push(self.expr(kid)?);
4460        }
4461        let args = self.expr_slice(args);
4462        let name = self.function_name("nullif");
4463        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
4464    }
4465
4466    /// `SubstringExpression <- 'SUBSTRING' Parens(SubstringArguments)` and
4467    /// `SubstringArguments <- SubstringParameters / SubstringExpressionList`.
4468    ///
4469    /// Both spellings are the same call and DuckDB's parser writes both of them back out as one:
4470    /// `substring(s FROM a FOR b)` comes back as the column `"substring"(s, a, b)` there, and so does
4471    /// `substring(s, a, b)`. The `FOR` on its own is the one worth pointing at, since it is not the
4472    /// two argument call it looks like. `substring('abcdef' FOR 3)` is `"substring"('abcdef', 1, 3)`
4473    /// upstream, so the start is filled in with a literal 1 here rather than left out.
4474    fn substring(&mut self, node: u32) -> Result<ExprRef> {
4475        let shape = self.first(self.first(node));
4476        let mut args = Vec::new();
4477        match self.name(shape) {
4478            "SubstringExpressionList" => {
4479                for kid in self.kids(shape) {
4480                    args.push(self.expr(kid)?);
4481                }
4482            }
4483            "SubstringParameters" => {
4484                args.push(self.expr(self.first(shape))?);
4485                // `SubstringFromFor <- SubstringFromOptionalFor / SubstringFor`, and both of those
4486                // hold the bounds as `FromExpression` and `ForExpression`, so finding them by name
4487                // reads either shape and neither one has to be told apart from the other.
4488                let bounds = self.first(self.nth(shape, 1));
4489                let from = self.find(bounds, "FromExpression");
4490                let start =
4491                    if from == NONE { self.number("1") } else { self.expr(self.first(from))? };
4492                args.push(start);
4493                let count = self.find(bounds, "ForExpression");
4494                if count != NONE {
4495                    args.push(self.expr(self.first(count))?);
4496                }
4497            }
4498            _ => return self.unsupported(shape),
4499        }
4500        let args = self.expr_slice(args);
4501        let name = self.function_name("substring");
4502        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
4503    }
4504
4505    /// `PositionExpression <- 'POSITION' Parens(PositionArguments)` and
4506    /// `PositionArguments <- OtherOperatorExpression 'IN' Expression`.
4507    ///
4508    /// The two arguments swap. `position('c' IN 'abcdef')` is `"position"('abcdef', 'c')` upstream,
4509    /// which is the same order `strpos` and `instr` are written in, so the haystack comes first in
4510    /// the call and second in the query.
4511    fn position(&mut self, node: u32) -> Result<ExprRef> {
4512        let arguments = self.first(node);
4513        if self.count(arguments) != 2 {
4514            return self.unsupported(arguments);
4515        }
4516        let needle = self.expr(self.first(arguments))?;
4517        let haystack = self.expr(self.nth(arguments, 1))?;
4518        let args = self.expr_slice(vec![haystack, needle]);
4519        let name = self.function_name("position");
4520        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
4521    }
4522
4523    /// `TrimExpression <- 'TRIM' Parens(TrimArguments)` and
4524    /// `TrimArguments <- TrimDirection? TrimSource? List(Expression)`.
4525    ///
4526    /// The direction is not an argument, it is the function: `LEADING` is `ltrim` upstream and
4527    /// `TRAILING` is `rtrim`, while `BOTH` and the bare form are both `trim`. The characters to strip
4528    /// are the last argument whichever way they were written, so `trim(BOTH 'x' FROM 'xxaxx')` and
4529    /// `trim('xxaxx', 'x')` are the same call, which is why the source goes on the end of the list
4530    /// rather than in front of it.
4531    fn trim(&mut self, node: u32) -> Result<ExprRef> {
4532        let arguments = self.first(node);
4533        let direction = self.find(arguments, "TrimDirection");
4534        let name = match direction {
4535            NONE => "trim",
4536            held => match self.name(self.first(held)) {
4537                "TrimLeading" => "ltrim",
4538                "TrimTrailing" => "rtrim",
4539                _ => "trim",
4540            },
4541        };
4542        let mut args = Vec::new();
4543        for kid in self.kids(arguments) {
4544            if matches!(self.name(kid), "TrimDirection" | "TrimSource") {
4545                continue;
4546            }
4547            args.push(self.expr(kid)?);
4548        }
4549        // `TrimSource <- Expression? 'FROM'`, so `trim(LEADING FROM s)` has the node with nothing
4550        // under it and there is no second argument to add.
4551        let source = self.find(arguments, "TrimSource");
4552        if source != NONE && self.count(source) == 1 {
4553            args.push(self.expr(self.first(source))?);
4554        }
4555        let args = self.expr_slice(args);
4556        let name = self.function_name(name);
4557        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
4558    }
4559
4560    /// `OverlayExpression <- 'OVERLAY' Parens(OverlayArguments)` and
4561    /// `OverlayArguments <- OverlayParameters / OverlayExpressionList`, where
4562    /// `OverlayParameters <- Expression 'PLACING' Expression FromExpression ForExpression?`.
4563    ///
4564    /// The arguments are already in the order the call takes them, so the keyword spelling is the
4565    /// list spelling with `PLACING`, `FROM` and `FOR` where the commas would be:
4566    /// `overlay('abcdef' PLACING 'X' FROM 2 FOR 1)` is `"overlay"('abcdef', 'X', 2, 1)` upstream.
4567    fn overlay(&mut self, node: u32) -> Result<ExprRef> {
4568        let shape = self.first(self.first(node));
4569        if !matches!(self.name(shape), "OverlayParameters" | "OverlayExpressionList") {
4570            return self.unsupported(shape);
4571        }
4572        let mut args = Vec::new();
4573        for kid in self.kids(shape) {
4574            let kid = match self.name(kid) {
4575                "FromExpression" | "ForExpression" => self.first(kid),
4576                _ => kid,
4577            };
4578            args.push(self.expr(kid)?);
4579        }
4580        let args = self.expr_slice(args);
4581        let name = self.function_name("overlay");
4582        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
4583    }
4584
4585    /// A number literal the query did not write, for the one place a lowering has to supply one.
4586    fn number(&mut self, text: &str) -> ExprRef {
4587        let text = self.intern(text);
4588        self.push(Expr::Literal { kind: LiteralKind::Number, text })
4589    }
4590
4591    /// `ExtractExpression <- 'EXTRACT' Parens(ExtractArguments)` and
4592    /// `ExtractArguments <- ExtractArgument 'FROM' Expression`.
4593    ///
4594    /// `EXTRACT` is not a function in the grammar because its argument list is not an argument
4595    /// list, and it is a function everywhere after here because DuckDB's parser does the same
4596    /// rewrite: `EXTRACT(minute FROM t)` is `date_part('minute', t)` and there is no separate
4597    /// implementation of one of them. The part is a keyword, an identifier or a string in the
4598    /// grammar, and all three become the string, which is why this is a rewrite and not a node.
4599    fn extract(&mut self, node: u32) -> Result<ExprRef> {
4600        let arguments = self.find(node, "ExtractArguments");
4601        if arguments == NONE {
4602            return self.unsupported(node);
4603        }
4604        let argument = self.first(self.first(arguments));
4605        let part = match self.name(argument) {
4606            "ExtractStringArgument" => self.string_value(argument)?,
4607            // A keyword, which is one of the thirteen the grammar names and is written back as the
4608            // one spelling that keyword has. `EXTRACT(seconds FROM t)` and `EXTRACT(SECOND FROM t)`
4609            // are both `date_part('SECOND', t)`, which was measured, and it shows up in the column
4610            // name as well as in the deparse, since an unaliased column is named after the call.
4611            "ExtractDatePartArgument" => date_part(self.text(argument)),
4612            // An identifier, taken as written. Which specifier names are legal is not a question
4613            // about syntax, so the answer to it lives with the function.
4614            "ExtractIdentifierArgument" => self.text(argument).to_string(),
4615            _ => return self.unsupported(argument),
4616        };
4617        let text = self.intern(&part);
4618        let part = self.push(Expr::Literal { kind: LiteralKind::String, text });
4619        let operand = self.expr(self.nth(arguments, 1))?;
4620        let name = self.function_name("date_part");
4621        let args = self.expr_slice(vec![part, operand]);
4622        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
4623    }
4624
4625    /// `FunctionArgument <- NamedFunctionArgument / PositionalFunctionArgument`, with the name the
4626    /// argument was given or `NONE` for a positional one.
4627    fn argument(&mut self, node: u32) -> Result<(u32, ExprRef)> {
4628        let inner = self.first(node);
4629        match self.name(inner) {
4630            "PositionalFunctionArgument" => Ok((NONE, self.expr(self.first(inner))?)),
4631            "NamedFunctionArgument" => {
4632                let named = self.first(inner);
4633                if self.count(named) != 3 {
4634                    return self.unsupported(named);
4635                }
4636                let name = self.identifier(self.first(named));
4637                Ok((name, self.expr(self.nth(named, 2))?))
4638            }
4639            _ => self.unsupported(inner),
4640        }
4641    }
4642
4643    /// One argument of a table function, which is the same rule plus the names.
4644    ///
4645    /// `NamedParameter <- TypeFuncName Type? NamedParameterAssignment Expression` and
4646    /// `NamedParameterAssignment <- ':=' / '=>'`, so those two spellings are what the grammar has.
4647    /// The binary accepts a third, `name = value`, which the grammar has no rule for because it
4648    /// parses as an equality and is picked apart afterwards. That is what happens here too: a
4649    /// positional argument that is a comparison between a bare name and something else is a named
4650    /// parameter, which is the reading upstream's own transformer gives it. `read_parquet(f,
4651    /// binary_as_string=True)` is the query that matters and it is the spelling the ClickBench
4652    /// entry uses.
4653    ///
4654    /// The name is not resolved here and neither is the value. Which parameters a function takes
4655    /// is the binder's question, and so is whether `binary_as_string=True` means anything to the
4656    /// function it was written on.
4657    fn table_argument(&mut self, node: u32) -> Result<Target> {
4658        let inner = self.first(node);
4659        if self.name(inner) == "NamedFunctionArgument" {
4660            let named = self.first(inner);
4661            if self.count(named) != 3 {
4662                // The optional `Type` between the name and the assignment, which is a macro
4663                // parameter's declaration and not a call.
4664                return self.unsupported(named);
4665            }
4666            let alias = self.identifier(self.first(named));
4667            let expr = self.expr(self.nth(named, 2))?;
4668            return Ok(Target { expr, alias });
4669        }
4670        let expr = self.expr(self.first(inner))?;
4671        if let Expr::Binary { op: BinaryOp::Eq, left, right } = self.ast.expr(expr)
4672            && let Expr::Column { name } = self.ast.expr(left)
4673            && name.len == 1
4674        {
4675            let alias = self.ast.parts[name.start as usize];
4676            return Ok(Target { expr: right, alias });
4677        }
4678        Ok(Target { expr, alias: NONE })
4679    }
4680
4681    /// `CastExpression <- CastOrTryCast Parens(CastArguments)`.
4682    fn cast(&mut self, node: u32) -> Result<ExprRef> {
4683        let try_cast = self.name(self.first(self.first(node))) == "TryCastKeyword";
4684        // `CastArguments <- Expression 'AS' Type`.
4685        let arguments = self.nth(node, 1);
4686        let operand = self.expr(self.first(arguments))?;
4687        let text = self.text(self.nth(arguments, 1)).to_string();
4688        let ty = self.intern(&text);
4689        Ok(self.push(Expr::Cast { operand, ty, try_cast }))
4690    }
4691
4692    /// `TypeLiteral <- Type StringLiteral`, which is the cast written the other way round.
4693    ///
4694    /// `DATE '1995-09-01'` and `CAST('1995-09-01' AS DATE)` are the same expression upstream, and
4695    /// the proof is the column name: the pinned binary answers both of them in a column called
4696    /// `CAST('1995-09-01' AS DATE)`. So this is the cast node and nothing else, which means every
4697    /// type the cast already takes is a typed literal for free and the two can never drift.
4698    ///
4699    /// The string is the literal the grammar matched rather than any expression, so there is no
4700    /// constant folding question here. `DATE x` does not parse in the first place.
4701    fn typed_literal(&mut self, node: u32) -> Result<ExprRef> {
4702        let text = self.text(self.first(node)).to_string();
4703        let ty = self.intern(&text);
4704        let operand = self.expr(self.nth(node, 1))?;
4705        Ok(self.push(Expr::Cast { operand, ty, try_cast: false }))
4706    }
4707
4708    /// `IntervalLiteral <- 'INTERVAL' IntervalParameter Interval?`, which is a function call.
4709    ///
4710    /// There is no interval node and there does not need to be one, because DuckDB's own
4711    /// transformer rewrites the literal into a call and the column name says so: `INTERVAL 1 DAY`
4712    /// comes back from the pinned binary in a column called
4713    /// `to_days(CAST(trunc(CAST(1 AS DOUBLE)) AS INTEGER))`. So the literal and a handwritten
4714    /// `to_days(1)` are the same expression from here on and the two cannot drift apart.
4715    ///
4716    /// Every unit goes through a DOUBLE on the way in, which is what makes `INTERVAL 1.5 DAY` one
4717    /// day rather than a day and a half: the truncation is in the rewrite and not in the function.
4718    /// The two units that can carry a fraction skip the truncation and stay a DOUBLE all the way,
4719    /// so `INTERVAL 2.7 SECOND` really is two and seven tenths of a second.
4720    ///
4721    /// A literal with no unit is the cast written the other way round, so `INTERVAL '1 day'` is
4722    /// `CAST('1 day' AS INTERVAL)`. That arm also catches a word the grammar does not read as a
4723    /// unit, since `INTERVAL 1 d` parses as this rule with no `Interval` child and a column alias
4724    /// after it, which is why upstream answers it with a cast error about an INTEGER.
4725    fn interval_literal(&mut self, node: u32) -> Result<ExprRef> {
4726        let parameter = self.find(node, "IntervalParameter");
4727        if parameter == NONE {
4728            return self.unsupported(node);
4729        }
4730        let operand = self.expr(self.first(parameter))?;
4731        let unit = self.find(node, "Interval");
4732        if unit == NONE {
4733            let ty = self.intern("INTERVAL");
4734            return Ok(self.push(Expr::Cast { operand, ty, try_cast: false }));
4735        }
4736        let spelling = self.name(self.first(unit));
4737        // The seven range forms parse and then refuse, in upstream's words, with the unit names
4738        // spelled the canonical way rather than the way they were written: `interval 1 days to
4739        // hours` is `DAY TO HOUR` there as well.
4740        if spelling == "IntervalToInterval" {
4741            let pair = self.name(self.first(self.first(unit)));
4742            return Err(Error::parser(format!("{} is not supported", worded(pair))));
4743        }
4744        let Some(&(_, function, width)) = UNITS.iter().find(|(rule, _, _)| *rule == spelling)
4745        else {
4746            return self.unsupported(unit);
4747        };
4748        let double = self.intern("DOUBLE");
4749        let mut count = self.push(Expr::Cast { operand, ty: double, try_cast: false });
4750        if let Some(width) = width {
4751            let name = self.function_name("trunc");
4752            let args = self.expr_slice(vec![count]);
4753            let whole = self.push(Expr::Function { name, args, distinct: false, filter: NONE });
4754            let ty = self.intern(width);
4755            count = self.push(Expr::Cast { operand: whole, ty, try_cast: false });
4756        }
4757        let name = self.function_name(function);
4758        let args = self.expr_slice(vec![count]);
4759        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
4760    }
4761
4762    /// `CaseExpression <- 'CASE' Expression? CaseWhenThen+ CaseElse? 'END'`.
4763    fn case(&mut self, node: u32) -> Result<ExprRef> {
4764        let mut operand = NONE;
4765        let mut arms = Vec::new();
4766        let mut otherwise = NONE;
4767        for kid in self.kids(node) {
4768            match self.name(kid) {
4769                // `CaseWhenThen <- 'WHEN' Expression 'THEN' Expression`.
4770                "CaseWhenThen" => {
4771                    let when = self.expr(self.first(kid))?;
4772                    let then = self.expr(self.nth(kid, 1))?;
4773                    arms.push(CaseArm { when, then });
4774                }
4775                // `CaseElse <- 'ELSE' Expression`.
4776                "CaseElse" => otherwise = self.expr(self.first(kid))?,
4777                // The bare `Expression` before the first `WHEN`, which makes it a simple case.
4778                _ => operand = self.expr(kid)?,
4779            }
4780        }
4781        let start = self.ast.case_arms.len() as u32;
4782        self.ast.case_arms.extend(arms);
4783        let arms = Slice { start, len: self.ast.case_arms.len() as u32 - start };
4784        Ok(self.push(Expr::Case { operand, arms, otherwise }))
4785    }
4786
4787    /// `ParenthesisExpression <- Parens(List(Expression)?)`, which is a row value.
4788    ///
4789    /// One item is not a row. `(a)` is `a` in every dialect and reading it as a one column row
4790    /// would change what `(a) = (b)` means.
4791    fn row(&mut self, node: u32) -> Result<ExprRef> {
4792        let mut items = Vec::new();
4793        for kid in self.kids(node) {
4794            items.push(self.expr(kid)?);
4795        }
4796        if items.len() == 1 {
4797            return Ok(items[0]);
4798        }
4799        let items = self.expr_slice(items);
4800        Ok(self.push(Expr::Row { items }))
4801    }
4802
4803    /// `RowExpression <- 'ROW' Parens(List(Expression)?)`, which is a row whatever its length, so
4804    /// `row(1)` is a row of one where `(1)` is the number.
4805    fn row_expression(&mut self, node: u32) -> Result<ExprRef> {
4806        let mut items = Vec::new();
4807        for kid in self.kids(node) {
4808            items.push(self.expr(kid)?);
4809        }
4810        let items = self.expr_slice(items);
4811        Ok(self.push(Expr::Row { items }))
4812    }
4813
4814    /// `Parameter <- '?' Number / '?' / '$' Number / '$' ColLabel`, a prepared statement parameter.
4815    ///
4816    /// The identifier is what follows the marker, so `?1` and `$1` are both the parameter named 1,
4817    /// and a bare `?` takes the next number by where it was written. That is what DuckDB does, which
4818    /// is why `? + $2` prints as `$1 + $2`: the counting is its own and does not skip a number
4819    /// because a later parameter claimed it.
4820    fn parameter(&mut self, node: u32) -> Result<ExprRef> {
4821        let written = self.text(node).trim();
4822        let written = written.trim_start_matches(['?', '$']).trim();
4823        let name = if written.is_empty() {
4824            self.anonymous += 1;
4825            self.anonymous.to_string()
4826        } else {
4827            written.to_string()
4828        };
4829        let name = self.intern(&name);
4830        Ok(self.push(Expr::Parameter { name }))
4831    }
4832
4833    /// `BoundedListExpression <- '[' List(Expression)? ']'`, which is a LIST value.
4834    ///
4835    /// One item is a list of one here, unlike the parenthesised form, because the brackets are what
4836    /// say list and there is nothing else `[a]` could mean.
4837    fn list(&mut self, node: u32) -> Result<ExprRef> {
4838        let mut items = Vec::new();
4839        for kid in self.kids(node) {
4840            items.push(self.expr(kid)?);
4841        }
4842        let items = self.expr_slice(items);
4843        Ok(self.push(Expr::List { items }))
4844    }
4845
4846    /// `MapExpression <- 'MAP' MapStructExpression`, `MapStructExpression <- '{'
4847    /// List(MapStructField)? '}'` and `MapStructField <- Expression ':' Expression`.
4848    ///
4849    /// `MAP {1: 'a'}` is `map([1], ['a'])` on the pin, down to the column heading, so it becomes that
4850    /// call with the keys in one list and the values in the other.
4851    fn map(&mut self, node: u32) -> Result<ExprRef> {
4852        let mut keys = Vec::new();
4853        let mut values = Vec::new();
4854        let fields = self.find(node, "MapStructExpression");
4855        if fields != NONE {
4856            for field in self.kids(fields).collect::<Vec<_>>() {
4857                let kids: Vec<u32> = self.kids(field).collect();
4858                let [key, value] = kids[..] else {
4859                    return self.unsupported(field);
4860                };
4861                keys.push(self.expr(key)?);
4862                values.push(self.expr(value)?);
4863            }
4864        }
4865        let keys = self.expr_slice(keys);
4866        let keys = self.push(Expr::List { items: keys });
4867        let values = self.expr_slice(values);
4868        let values = self.push(Expr::List { items: values });
4869        let args = self.expr_slice(vec![keys, values]);
4870        let name = self.function_name("map");
4871        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
4872    }
4873
4874    /// `StructExpression <- '{' List(StructField)? '}'` and
4875    /// `StructField <- ColIdOrString ':' Expression`.
4876    ///
4877    /// A field name is read the way a column name is, so `{a: 1}`, `{"a": 1}` and `{'a': 1}` are
4878    /// the same struct, and a name written twice is left for the binder to refuse in the pin's words.
4879    fn structure(&mut self, node: u32) -> Result<ExprRef> {
4880        let mut names = Vec::new();
4881        let mut values = Vec::new();
4882        for field in self.kids(node).collect::<Vec<_>>() {
4883            let kids: Vec<u32> = self.kids(field).collect();
4884            let [name, value] = kids[..] else {
4885                return self.unsupported(field);
4886            };
4887            names.push(self.identifier(name));
4888            values.push(self.expr(value)?);
4889        }
4890        let names = self.part_slice(names);
4891        let values = self.expr_slice(values);
4892        Ok(self.push(Expr::Struct { names, values }))
4893    }
4894
4895    /// `SubqueryExpression <- SubqueryNot? SubqueryExists? SubqueryReference`.
4896    fn subquery(&mut self, node: u32) -> Result<ExprRef> {
4897        let negated = self.find(node, "SubqueryNot") != NONE;
4898        let exists = self.find(node, "SubqueryExists") != NONE;
4899        let reference = self.find(node, "SubqueryReference");
4900        let query = self.query(self.first(reference))?;
4901        Ok(if exists {
4902            self.push(Expr::Exists { query, negated })
4903        } else if negated {
4904            return self.unsupported(node);
4905        } else {
4906            self.push(Expr::Subquery { query, array: false })
4907        })
4908    }
4909
4910    /// The value of a string literal, with the quotes gone and the escapes resolved.
4911    ///
4912    /// A literal can be several tokens. `'a' 'b'` on two lines is one literal that is `ab`, which is
4913    /// the SQL standard's rule and DuckDB's, so the node is decoded token by token rather than by
4914    /// taking its text and stripping the outside.
4915    fn string_value(&self, node: u32) -> Result<String> {
4916        let span = self.tree.node(node);
4917        let mut value = String::new();
4918        for token in &self.tokens[span.start as usize..span.end as usize] {
4919            if token.kind == Kind::String {
4920                value.push_str(&string_token(token.text(self.query))?);
4921            }
4922        }
4923        Ok(value)
4924    }
4925
4926    /// The token that opens a string literal, which is the whole of it when it has a prefix.
4927    ///
4928    /// Only the first token is asked, because a prefixed literal is one token: `E'a' 'b'` is a
4929    /// syntax error upstream rather than a concatenation, so there is no second prefix to disagree
4930    /// with this one.
4931    fn first_string(&self, node: u32) -> &'a str {
4932        let span = self.tree.node(node);
4933        self.tokens[span.start as usize..span.end as usize]
4934            .iter()
4935            .find(|token| token.kind == Kind::String)
4936            .map_or("", |token| token.text(self.query))
4937    }
4938
4939    /// A string literal as an expression, which is the value plus what the prefix makes of it.
4940    ///
4941    /// `N'abc'` is a cast of the string to VARCHAR upstream and not a plain string, and the column
4942    /// name is the proof: the pinned binary answers it in a column called `CAST('abc' AS VARCHAR)`.
4943    /// So it is written here as the cast it is, and then there is nothing left to keep in step.
4944    ///
4945    /// `x'4142'` is not a string at all, it is a BLOB, so it is the one prefix that becomes a
4946    /// different kind of literal rather than a string with something done to it.
4947    fn string_literal(&mut self, node: u32) -> Result<ExprRef> {
4948        let token = self.first_string(node);
4949        let prefix = match token.as_bytes() {
4950            [prefix, b'\'', ..] => *prefix,
4951            _ => 0,
4952        };
4953        if matches!(prefix, b'X' | b'x')
4954            && let Some(body) = token.get(1..).and_then(quoted_body)
4955        {
4956            let text = blob_text(body.as_bytes())?;
4957            let text = self.intern(&text);
4958            return Ok(self.push(Expr::Literal { kind: LiteralKind::Blob, text }));
4959        }
4960        let value = self.string_value(node)?;
4961        let text = self.intern(&value);
4962        let literal = self.push(Expr::Literal { kind: LiteralKind::String, text });
4963        if matches!(prefix, b'N' | b'n') {
4964            let ty = self.intern("VARCHAR");
4965            return Ok(self.push(Expr::Cast { operand: literal, ty, try_cast: false }));
4966        }
4967        Ok(literal)
4968    }
4969}
4970
4971/// The options of a `COPY` as the `read_csv` parameters they become, each with its value.
4972type CopyOptions = Vec<(&'static str, ExprRef)>;
4973
4974/// The `read_csv` parameter a `COPY` option is, by the name it was written with in lower case.
4975///
4976/// `DELIMITER` and `NULL` are the `COPY` names for what `read_csv` calls `delim` and `nullstr`, and
4977/// DuckDB takes the `read_csv` names in a `COPY` too, so both are here.
4978fn copy_parameter(name: &str) -> Result<&'static str> {
4979    Ok(match name {
4980        "header" => "header",
4981        "delimiter" | "delim" | "sep" => "delim",
4982        "quote" => "quote",
4983        "escape" => "escape",
4984        "null" | "nullstr" => "nullstr",
4985        "all_varchar" | "allow_quoted_nulls" | "auto_detect" | "columns" | "comment"
4986        | "compression" | "dateformat" | "date_format" | "decimal_separator" | "encoding"
4987        | "force_not_null" | "force_quote" | "ignore_errors" | "max_line_size" | "names"
4988        | "new_line" | "null_padding" | "sample_size" | "skip" | "strict_mode"
4989        | "timestampformat" | "timestamp_format" | "types" | "dtypes" => {
4990            return Err(Error::not_implemented(format!(
4991                "COPY FROM with the option {name} is not supported yet"
4992            )));
4993        }
4994        _ => {
4995            return Err(Error::not_implemented(format!("Unrecognized option \"{name}\" for csv")));
4996        }
4997    })
4998}
4999
5000/// Each unit an interval literal can be written in, as the grammar rule that spells it, the
5001/// function it becomes, and the width the count is truncated to on the way there.
5002///
5003/// A width of `None` is the pair that keeps what is after the point. Those two stay a DOUBLE and
5004/// never see `trunc`, which is the whole of the difference between `INTERVAL 2.7 SECOND` being two
5005/// and seven tenths of a second and `INTERVAL 1.5 DAY` being one day. Every entry, both spellings
5006/// of every keyword and the width of each one was read off the pinned binary's column names.
5007const UNITS: &[(&str, &str, Option<&str>)] = &[
5008    ("YearKeyword", "to_years", Some("INTEGER")),
5009    ("MonthKeyword", "to_months", Some("INTEGER")),
5010    ("QuarterKeyword", "to_quarters", Some("INTEGER")),
5011    ("DecadeKeyword", "to_decades", Some("INTEGER")),
5012    ("CenturyKeyword", "to_centuries", Some("INTEGER")),
5013    ("MillenniumKeyword", "to_millennia", Some("INTEGER")),
5014    ("DayKeyword", "to_days", Some("INTEGER")),
5015    ("WeekKeyword", "to_weeks", Some("INTEGER")),
5016    ("HourKeyword", "to_hours", Some("BIGINT")),
5017    ("MinuteKeyword", "to_minutes", Some("BIGINT")),
5018    ("MicrosecondKeyword", "to_microseconds", Some("BIGINT")),
5019    ("SecondKeyword", "to_seconds", None),
5020    ("MillisecondKeyword", "to_milliseconds", None),
5021];
5022
5023/// The one spelling a date part keyword is written back as, which is not always the singular.
5024///
5025/// Both spellings of each of the thirteen keywords land on one name, and the name is upper case and
5026/// is plural for the two smallest parts and singular for the rest. That is not a rule, it is a list,
5027/// and it was read off the pinned binary a keyword at a time: `EXTRACT(milliseconds FROM t)` and
5028/// `EXTRACT(millisecond FROM t)` are both `date_part('MILLISECONDS', t)` while `EXTRACT(seconds FROM
5029/// t)` is `date_part('SECOND', t)`.
5030///
5031/// A word that is not a keyword never reaches here, because the grammar tells the two apart, and it
5032/// keeps whatever case it was written in. `EXTRACT(epoch FROM t)` stays lower case, measured.
5033fn date_part(written: &str) -> String {
5034    const PARTS: &[(&str, &str)] = &[
5035        ("YEAR", "YEAR"),
5036        ("YEARS", "YEAR"),
5037        ("MONTH", "MONTH"),
5038        ("MONTHS", "MONTH"),
5039        ("DAY", "DAY"),
5040        ("DAYS", "DAY"),
5041        ("HOUR", "HOUR"),
5042        ("HOURS", "HOUR"),
5043        ("MINUTE", "MINUTE"),
5044        ("MINUTES", "MINUTE"),
5045        ("SECOND", "SECOND"),
5046        ("SECONDS", "SECOND"),
5047        ("MILLISECOND", "MILLISECONDS"),
5048        ("MILLISECONDS", "MILLISECONDS"),
5049        ("MICROSECOND", "MICROSECONDS"),
5050        ("MICROSECONDS", "MICROSECONDS"),
5051        ("WEEK", "WEEK"),
5052        ("WEEKS", "WEEK"),
5053        ("QUARTER", "QUARTER"),
5054        ("QUARTERS", "QUARTER"),
5055        ("DECADE", "DECADE"),
5056        ("DECADES", "DECADE"),
5057        ("CENTURY", "CENTURY"),
5058        ("CENTURIES", "CENTURY"),
5059        ("MILLENNIUM", "MILLENNIUM"),
5060        ("MILLENNIA", "MILLENNIUM"),
5061    ];
5062    PARTS
5063        .iter()
5064        .find(|(spelling, _)| spelling.eq_ignore_ascii_case(written))
5065        .map_or_else(|| written.to_string(), |(_, name)| (*name).to_string())
5066}
5067
5068/// A grammar rule name like `DayToHour` as the words upstream puts in the message for it.
5069fn worded(rule: &str) -> String {
5070    let mut out = String::new();
5071    for character in rule.chars() {
5072        if character.is_ascii_uppercase() && !out.is_empty() {
5073            out.push(' ');
5074        }
5075        out.push(character.to_ascii_uppercase());
5076    }
5077    out
5078}
5079
5080/// The value of one string token, with the quotes gone and whatever the prefix means resolved.
5081///
5082/// There is no fall through that keeps the source text. That arm is what answered `SELECT E'a'`
5083/// with the four characters `E'a'`, and a default that silently answers with the query is a default
5084/// that will do this again with the next spelling somebody adds, so a spelling this does not know
5085/// raises instead. Per #329.
5086fn string_token(text: &str) -> Result<String> {
5087    if let Some(body) = dollar_body(text) {
5088        return Ok(body.to_string());
5089    }
5090    if let Some(body) = quoted_body(text) {
5091        return Ok(body.replace("''", "'"));
5092    }
5093    let Some(body) = text.get(1..).and_then(quoted_body) else {
5094        return Ok(text.to_string());
5095    };
5096    match text.as_bytes()[0] {
5097        b'E' | b'e' => escaped(body),
5098        // `N'abc'` is the string and nothing else. The cast that makes the name is put on outside.
5099        b'N' | b'n' => Ok(body.replace("''", "'")),
5100        // Not a bit string, whatever the spelling suggests. Upstream answers `B'101'` with the four
5101        // characters `b101` as a VARCHAR, and `B''` with the one character `b`, which is measured
5102        // and not guessed. Nothing else is done with the body.
5103        b'B' | b'b' => Ok(format!("b{}", body.replace("''", "'"))),
5104        // `x'41'` is a BLOB and a BLOB is not a string, so the places that want a string out of a
5105        // literal, which are DESCRIBE and the part in EXTRACT, do not get one from this spelling.
5106        _ => Err(Error::not_implemented(format!("the string literal {text} is not supported yet"))),
5107    }
5108}
5109
5110/// The text a blob literal's body means, which is the text a blob prints as.
5111///
5112/// `x'4142'` is two bytes and the pinned binary calls the column `'AB'::BLOB`, so what is kept here
5113/// is the printed form and not the source. The cast that reads it back gives the bytes again, which
5114/// is what makes one text enough for both the value and the name, and it is `Value` that prints it
5115/// so the two spellings of a blob cannot drift apart.
5116///
5117/// Upstream writes `\xHH` for every pair without looking at the digits and lets the cast refuse the
5118/// ones that are not hex, which is why `x'4'` is a parser error and `x'zz'` is a conversion error
5119/// one step later. Doing the same thing gives both messages in the same words. The pairs are bytes
5120/// and not characters: `x'éé'` is four bytes and so two pairs, which is how upstream counts them.
5121fn blob_text(body: &[u8]) -> Result<String> {
5122    if !body.len().is_multiple_of(2) {
5123        return Err(Error::parser("Hex string literal must have an even number of hex digits"));
5124    }
5125    let digit = |byte: u8| (byte as char).to_digit(16).map(|digit| digit as u8);
5126    let bytes: Option<Vec<u8>> =
5127        body.chunks(2).map(|pair| Some(digit(pair[0])? * 16 + digit(pair[1])?)).collect();
5128    match bytes {
5129        Some(bytes) => Ok(Value::Blob(bytes).to_string()),
5130        None => {
5131            Ok(body.chunks(2).map(|pair| format!("\\x{}", String::from_utf8_lossy(pair))).collect())
5132        }
5133    }
5134}
5135
5136/// The body of a single quoted string, for the tokens that are one.
5137///
5138/// An unterminated token has nothing to take off the end and keeps every byte it was given, which
5139/// is why the closing quote has to be a quote that is not also the opening one.
5140fn quoted_body(text: &str) -> Option<&str> {
5141    text.strip_prefix('\'').filter(|rest| !rest.is_empty()).and_then(|rest| rest.strip_suffix('\''))
5142}
5143
5144/// The body of an `E'...'` literal, with the C style escapes resolved.
5145///
5146/// Every rule here was read off the pinned binary one at a time. The named escapes are `\n`, `\t`,
5147/// `\r`, `\b` and `\f`, and `\v` is not one of them. `\x` takes one or two hex digits and `\0`
5148/// through `\7` take one to three octal digits, both of which write a byte and not a character, so
5149/// `\xc3\xa9` is one `é` and `\377` is not a string at all. `\uHHHH` takes exactly four hex digits
5150/// and writes the character they name. Anything else, including a `\u` that is short or names a
5151/// surrogate half or a NUL, drops the backslash and keeps the character, so `\q` is `q` and `\u41`
5152/// is `u41`.
5153///
5154/// The result is bytes until the end because the escapes write bytes, and the two ways of writing
5155/// something that is not a string both raise the way upstream raises them.
5156fn escaped(body: &str) -> Result<String> {
5157    let bytes = body.as_bytes();
5158    let mut out = Vec::with_capacity(bytes.len());
5159    let mut at = 0;
5160    while at < bytes.len() {
5161        let byte = bytes[at];
5162        at += 1;
5163        if byte == b'\'' && bytes.get(at) == Some(&b'\'') {
5164            out.push(b'\'');
5165            at += 1;
5166            continue;
5167        }
5168        if byte != b'\\' || at == bytes.len() {
5169            out.push(byte);
5170            continue;
5171        }
5172        let escape = bytes[at];
5173        at += 1;
5174        match escape {
5175            b'n' => out.push(b'\n'),
5176            b't' => out.push(b'\t'),
5177            b'r' => out.push(b'\r'),
5178            b'b' => out.push(0x08),
5179            b'f' => out.push(0x0c),
5180            b'x' => match digits(bytes, &mut at, 16, 2) {
5181                Some(value) => out.push(value as u8),
5182                None => out.push(b'x'),
5183            },
5184            b'0'..=b'7' => {
5185                at -= 1;
5186                let value = digits(bytes, &mut at, 8, 3).unwrap_or(0);
5187                out.push(value as u8);
5188            }
5189            b'u' => match four_hex(bytes, at).and_then(char::from_u32).filter(|c| *c != '\0') {
5190                Some(c) => {
5191                    at += 4;
5192                    out.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes());
5193                }
5194                None => out.push(b'u'),
5195            },
5196            other => out.push(other),
5197        }
5198    }
5199    if out.contains(&0) {
5200        return Err(Error::parser("Null character not permitted in escape string literal"));
5201    }
5202    String::from_utf8(out).map_err(|error| {
5203        Error::parser(format!(
5204            "Invalid UTF-8 in escape string literal at byte offset {}: byte mismatch",
5205            error.utf8_error().valid_up_to()
5206        ))
5207    })
5208}
5209
5210/// Up to `most` digits in `radix` starting at `at`, moving `at` past the ones that were taken.
5211///
5212/// `None` means there were none at all, which is the case where the escape was not an escape:
5213/// `\x` on its own is the letter `x` upstream and not a zero byte.
5214fn digits(bytes: &[u8], at: &mut usize, radix: u32, most: usize) -> Option<u32> {
5215    let mut value = None;
5216    for _ in 0..most {
5217        let Some(digit) = bytes.get(*at).and_then(|byte| (*byte as char).to_digit(radix)) else {
5218            break;
5219        };
5220        value = Some(value.unwrap_or(0) * radix + digit);
5221        *at += 1;
5222    }
5223    value
5224}
5225
5226/// The four hex digits of a `\uHHHH`, which has to be all four of them or it is not one.
5227///
5228/// Nothing is consumed here, because the digits are only digits if the whole escape works out. A
5229/// surrogate half is not a character and upstream does not pair it up either, so `😀` is
5230/// the ten characters it was written as, which is what the caller falls back to.
5231fn four_hex(bytes: &[u8], at: usize) -> Option<u32> {
5232    let digits = bytes.get(at..at + 4)?;
5233    if !digits.iter().all(u8::is_ascii_hexdigit) {
5234        return None;
5235    }
5236    u32::from_str_radix(std::str::from_utf8(digits).ok()?, 16).ok()
5237}
5238
5239/// The body of a dollar quoted string, for the tokens that are one.
5240///
5241/// The tag is whatever sits between the opening pair of dollars and may be empty, so `$$a$$` and
5242/// `$tag$a$tag$` both arrive here, and nothing inside the body is escaped, which is the whole reason
5243/// the spelling exists. The tokenizer has already found the closing tag, which is the part that takes
5244/// work, so this says where the body starts and ends and no more. A token that is not dollar quoted
5245/// gives `None` and so does an unterminated one, which has no closing tag to take off and keeps every
5246/// byte it was given, the way the matcher already treats it. Per #276.
5247fn dollar_body(text: &str) -> Option<&str> {
5248    let rest = text.strip_prefix('$')?;
5249    let close = rest.find('$')?;
5250    let (tag, body) = (&rest[..close], &rest[close + 1..]);
5251    body.strip_suffix(&format!("${tag}$"))
5252}
5253
5254/// Strip the quoting off an identifier.
5255///
5256/// DuckDB does not fold identifier case at any point, quoted or not, so this only removes the
5257/// quotes and resolves the doubled ones. Anything else would be the parser deciding what a name is.
5258///
5259/// Single quotes are stripped too, and the only way one gets here is the file name in `FROM
5260/// 'hits.parquet'`, because the matcher takes a string for a name in that position and in `COPY t TO
5261/// '...'` and nowhere else. Leaving them on would make that name different from the one `FROM
5262/// "hits.parquet"` writes, and DuckDB reads both of those as the same file.
5263fn unquote(text: &str) -> String {
5264    if let Some(body) = text.strip_prefix('"').and_then(|rest| rest.strip_suffix('"')) {
5265        return body.replace("\"\"", "\"");
5266    }
5267    match text.strip_prefix('\'').and_then(|rest| rest.strip_suffix('\'')) {
5268        Some(body) => body.replace("''", "'"),
5269        None => text.to_string(),
5270    }
5271}
5272
5273#[cfg(test)]
5274mod tests {
5275    use super::*;
5276    use crate::corpus::CORPUS;
5277    use crate::matcher::parse;
5278
5279    /// The AST written back out as text, which is what the assertions below read.
5280    ///
5281    /// Not a SQL printer and not trying to be. It is deliberately not valid SQL: operators are
5282    /// spelled with the name of the variant and every binary node is parenthesised, so that a test
5283    /// asserting on this text is asserting on the shape of the tree and not on a formatting choice.
5284    /// `a - b - c` and `a - (b - c)` have to look different here or the test that tells them apart
5285    /// is not a test.
5286    fn show(ast: &Ast, expr: ExprRef) -> String {
5287        if expr == NONE {
5288            return "-".to_string();
5289        }
5290        /// The `FILTER` on a call, which is nothing at all when there is none.
5291        fn shown_filter(ast: &Ast, filter: ExprRef) -> String {
5292            if filter == NONE { String::new() } else { format!(" FILTER [{}]", show(ast, filter)) }
5293        }
5294        /// A run of sort keys, which a window call has two of and in two different places.
5295        fn keys(ast: &Ast, slice: Slice) -> String {
5296            ast.order_list(slice)
5297                .iter()
5298                .map(|item| format!("{} {:?} {:?}", show(ast, item.expr), item.order, item.nulls))
5299                .collect::<Vec<_>>()
5300                .join(", ")
5301        }
5302        let list = |slice: Slice| {
5303            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
5304        };
5305        match ast.expr(expr) {
5306            Expr::Star { qualifier, replacements } => {
5307                let star = if qualifier.is_empty() {
5308                    "*".to_string()
5309                } else {
5310                    format!("{}.*", ast.name_text(qualifier))
5311                };
5312                if replacements.is_empty() {
5313                    return star;
5314                }
5315                let entries: Vec<String> = ast
5316                    .target_list(replacements)
5317                    .iter()
5318                    .map(|target| {
5319                        format!("{} AS {}", show(ast, target.expr), ast.string(target.alias))
5320                    })
5321                    .collect();
5322                format!("{star} REPLACE ({})", entries.join(", "))
5323            }
5324            Expr::Column { name } => ast.name_text(name),
5325            Expr::Literal { kind, text } => match kind {
5326                LiteralKind::Number => ast.string(text).to_string(),
5327                LiteralKind::String => format!("'{}'", ast.string(text)),
5328                LiteralKind::Blob => format!("'{}'::BLOB", ast.string(text)),
5329                other => format!("{other:?}").to_uppercase(),
5330            },
5331            Expr::Unary { op, operand } => format!("({op:?} {})", show(ast, operand)),
5332            Expr::Binary { op, left, right } => {
5333                let op = match op {
5334                    BinaryOp::Named(name) => ast.string(name).to_string(),
5335                    other => format!("{other:?}"),
5336                };
5337                format!("({} {op} {})", show(ast, left), show(ast, right))
5338            }
5339            Expr::Function { name, args, distinct, filter } => {
5340                let distinct = if distinct { "DISTINCT " } else { "" };
5341                let filter = shown_filter(ast, filter);
5342                format!("{}({distinct}{}){filter}", ast.name_text(name), list(args))
5343            }
5344            Expr::Window { name, args, distinct, filter, ignore_nulls, order: inner, spec } => {
5345                let distinct = if distinct { "DISTINCT " } else { "" };
5346                let filter = shown_filter(ast, filter);
5347                let nulls = if ignore_nulls { " IGNORE NULLS" } else { "" };
5348                let inner = keys(ast, inner);
5349                let inner = if inner.is_empty() { inner } else { format!(" ORDER BY {inner}") };
5350                let held = ast.window(spec);
5351                let order = keys(ast, held.order);
5352                let bound = |end: WindowBound| match end {
5353                    WindowBound::Preceding(offset) => format!("Preceding({})", show(ast, offset)),
5354                    WindowBound::Following(offset) => format!("Following({})", show(ast, offset)),
5355                    other => format!("{other:?}"),
5356                };
5357                format!(
5358                    "{}({distinct}{}{inner}{nulls}){filter} OVER [{}] [{order}] [{:?} {} {} {:?}]",
5359                    ast.name_text(name),
5360                    list(args),
5361                    list(held.partition),
5362                    held.unit,
5363                    bound(held.start),
5364                    bound(held.end),
5365                    held.exclude
5366                )
5367            }
5368            Expr::Cast { operand, ty, try_cast } => {
5369                let word = if try_cast { "TRY_CAST" } else { "CAST" };
5370                format!("{word}({} AS {})", show(ast, operand), ast.string(ty))
5371            }
5372            Expr::Case { operand, arms, otherwise } => {
5373                let arms = ast
5374                    .arm_list(arms)
5375                    .iter()
5376                    .map(|arm| format!("WHEN {} THEN {}", show(ast, arm.when), show(ast, arm.then)))
5377                    .collect::<Vec<_>>()
5378                    .join(" ");
5379                format!("CASE {} {arms} ELSE {} END", show(ast, operand), show(ast, otherwise))
5380            }
5381            Expr::Between { operand, low, high, negated } => {
5382                let not = if negated { "NOT " } else { "" };
5383                format!(
5384                    "({not}{} BETWEEN {} AND {})",
5385                    show(ast, operand),
5386                    show(ast, low),
5387                    show(ast, high)
5388                )
5389            }
5390            Expr::In { operand, list: items, negated } => {
5391                let not = if negated { "NOT " } else { "" };
5392                format!("({not}{} IN [{}])", show(ast, operand), list(items))
5393            }
5394            Expr::List { items } => format!("[{}]", list(items)),
5395            Expr::Lambda { params, body } => {
5396                let params: Vec<&str> = ast.name(params).collect();
5397                format!("(lambda {}: {})", params.join(", "), show(ast, body))
5398            }
5399            Expr::Parameter { name } => format!("${}", ast.string(name)),
5400            Expr::Default => "DEFAULT".to_string(),
5401            Expr::Row { items } => format!("ROW({})", list(items)),
5402            Expr::Struct { names, values } => {
5403                let fields: Vec<String> = ast
5404                    .name(names)
5405                    .zip(ast.expr_list(values))
5406                    .map(|(name, &value)| format!("{name}: {}", show(ast, value)))
5407                    .collect();
5408                format!("{{{}}}", fields.join(", "))
5409            }
5410            Expr::Subquery { query, array: false } => format!("({})", show_query(ast, query)),
5411            Expr::Subquery { query, array: true } => format!("ARRAY({})", show_query(ast, query)),
5412            Expr::Exists { query, negated } => {
5413                let exists = format!("EXISTS ({})", show_query(ast, query));
5414                if negated { format!("NOT {exists}") } else { exists }
5415            }
5416            Expr::InSubquery { operand, query, negated } => {
5417                let written = format!("{} IN ({})", show(ast, operand), show_query(ast, query));
5418                if negated { format!("NOT {written}") } else { written }
5419            }
5420            Expr::QuantifiedSubquery { operand, op, query, all } => {
5421                let quantifier = if all { "ALL" } else { "ANY" };
5422                format!("{} {op:?} {quantifier} ({})", show(ast, operand), show_query(ast, query))
5423            }
5424        }
5425    }
5426
5427    /// One from item written back out.
5428    fn show_source(ast: &Ast, source: SourceRef) -> String {
5429        let alias = |alias: StrRef| match alias {
5430            NONE => String::new(),
5431            other => format!(" AS {}", ast.string(other)),
5432        };
5433        match ast.source(source) {
5434            Source::Table { name, alias: name_alias, .. } => {
5435                format!("{}{}", ast.name_text(name), alias(name_alias))
5436            }
5437            Source::Function { name, args, alias: call_alias, .. } => {
5438                let args = ast
5439                    .target_list(args)
5440                    .iter()
5441                    .map(|item| match item.alias {
5442                        NONE => show(ast, item.expr),
5443                        named => format!("{} := {}", ast.string(named), show(ast, item.expr)),
5444                    })
5445                    .collect::<Vec<_>>()
5446                    .join(", ");
5447                format!("{}({args}){}", ast.name_text(name), alias(call_alias))
5448            }
5449            Source::Subquery { query, alias: query_alias, .. } => {
5450                format!("({}){}", show_query(ast, query), alias(query_alias))
5451            }
5452            Source::Cte { cte, alias: cte_alias, .. } => {
5453                format!("{}{}", ast.string(ast.cte(cte).name), alias(cte_alias))
5454            }
5455            Source::Values { rows, alias: values_alias, .. } => {
5456                format!("{}{}", show_rows(ast, rows), alias(values_alias))
5457            }
5458            Source::Join { left, right, kind, natural, on, using } => {
5459                let natural = if natural { "NATURAL " } else { "" };
5460                let on = if on == NONE { String::new() } else { format!(" ON {}", show(ast, on)) };
5461                let using = if using.is_empty() {
5462                    String::new()
5463                } else {
5464                    format!(" USING ({})", ast.name_text(using))
5465                };
5466                format!(
5467                    "({} {natural}{kind:?} JOIN {}{on}{using})",
5468                    show_source(ast, left),
5469                    show_source(ast, right)
5470                )
5471            }
5472        }
5473    }
5474
5475    /// The rows of a `VALUES` written back out.
5476    fn show_rows(ast: &Ast, rows: Slice) -> String {
5477        let rows = ast
5478            .rows(rows)
5479            .iter()
5480            .map(|&row| {
5481                let items = ast
5482                    .expr_list(row)
5483                    .iter()
5484                    .map(|&item| show(ast, item))
5485                    .collect::<Vec<_>>()
5486                    .join(", ");
5487                format!("({items})")
5488            })
5489            .collect::<Vec<_>>()
5490            .join(", ");
5491        format!("VALUES {rows}")
5492    }
5493
5494    /// A writing statement written back out with its `RETURNING` query after it, if it has one.
5495    fn show_returning(ast: &Ast, insert: &Insert, out: String) -> String {
5496        match insert.returning {
5497            Some(returning) => out + &format!(" RETURNING {}", show_query(ast, returning)),
5498            None => out,
5499        }
5500    }
5501
5502    /// One query written back out.
5503    fn show_query(ast: &Ast, index: QueryRef) -> String {
5504        let query = ast.query(index);
5505        let list = |slice: Slice| {
5506            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
5507        };
5508        let mut out = String::new();
5509        for &index in ast.cte_list(query.ctes) {
5510            let cte = ast.cte(index);
5511            let columns = ast.name(cte.columns).collect::<Vec<_>>().join(", ");
5512            let columns = if columns.is_empty() { columns } else { format!("({columns})") };
5513            out += &format!(
5514                "WITH {}{columns} AS MATERIALIZED ({}) ",
5515                ast.string(cte.name),
5516                show_query(ast, cte.query)
5517            );
5518        }
5519        out += &match query.body {
5520            QueryBody::SetOp { op, quantifier, by_name, left, right } => {
5521                let by_name = if by_name { " BY NAME" } else { "" };
5522                format!(
5523                    "({} {op:?} {quantifier:?}{by_name} {})",
5524                    show_query(ast, left),
5525                    show_query(ast, right)
5526                )
5527            }
5528            QueryBody::Select(index) => {
5529                let select = ast.select(index);
5530                let distinct = match select.distinct {
5531                    Distinct::No => String::new(),
5532                    Distinct::Yes => " DISTINCT".to_string(),
5533                    Distinct::On(on) => format!(" DISTINCT ON ({})", list(on)),
5534                };
5535                let targets = ast
5536                    .target_list(select.targets)
5537                    .iter()
5538                    .map(|target| match target.alias {
5539                        NONE => show(ast, target.expr),
5540                        alias => format!("{} AS {}", show(ast, target.expr), ast.string(alias)),
5541                    })
5542                    .collect::<Vec<_>>()
5543                    .join(", ");
5544                let mut out = format!("SELECT{distinct} {targets}");
5545                if !select.from.is_empty() {
5546                    let from = ast
5547                        .source_list(select.from)
5548                        .iter()
5549                        .map(|&source| show_source(ast, source))
5550                        .collect::<Vec<_>>()
5551                        .join(", ");
5552                    out += &format!(" FROM {from}");
5553                }
5554                if select.filter != NONE {
5555                    out += &format!(" WHERE {}", show(ast, select.filter));
5556                }
5557                if select.group_by_all {
5558                    out += " GROUP BY ALL";
5559                } else if !select.group_by.is_empty() {
5560                    out += &format!(" GROUP BY {}", list(select.group_by));
5561                }
5562                if select.having != NONE {
5563                    out += &format!(" HAVING {}", show(ast, select.having));
5564                }
5565                out
5566            }
5567            QueryBody::Values(rows) => show_rows(ast, rows),
5568            QueryBody::Describe(inner) => format!("DESCRIBE {}", show_query(ast, inner)),
5569            QueryBody::Show { name, .. } => format!("SHOW {}", ast.name_text(name)),
5570        };
5571        if query.order_by_all {
5572            out += " ORDER BY ALL";
5573        } else if !query.order_by.is_empty() {
5574            let items = ast
5575                .order_list(query.order_by)
5576                .iter()
5577                .map(|item| format!("{} {:?} {:?}", show(ast, item.expr), item.order, item.nulls))
5578                .collect::<Vec<_>>()
5579                .join(", ");
5580            out += &format!(" ORDER BY {items}");
5581        }
5582        if query.limit != NONE {
5583            let percent = if query.limit_percent { "%" } else { "" };
5584            out += &format!(" LIMIT {}{percent}", show(ast, query.limit));
5585        }
5586        if query.offset != NONE {
5587            out += &format!(" OFFSET {}", show(ast, query.offset));
5588        }
5589        out
5590    }
5591
5592    /// One statement, transformed and written back out.
5593    fn round(query: &str) -> String {
5594        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
5595        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
5596        let Statement::Query(index) = ast.statements[0] else {
5597            panic!("{query} is not a query");
5598        };
5599        show_query(&ast, index)
5600    }
5601
5602    fn round_with_case(query: &str, case: IdentifierCase) -> String {
5603        let ast =
5604            parse_ast_with_case(query, case).unwrap_or_else(|error| panic!("{query}: {error}"));
5605        let Statement::Query(index) = ast.statements[0] else {
5606            panic!("{query} is not a query");
5607        };
5608        show_query(&ast, index)
5609    }
5610
5611    /// One statement, transformed and written back out as the DDL and DML shape it is.
5612    fn round_statement(query: &str) -> String {
5613        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
5614        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
5615        match ast.statements[0] {
5616            Statement::Query(index) => show_query(&ast, index),
5617            Statement::CreateTable(index) => {
5618                let create = ast.create_table(index);
5619                let mut out = "CREATE".to_string();
5620                if create.or_replace {
5621                    out += " OR REPLACE";
5622                }
5623                if create.temporary {
5624                    out += " TEMPORARY";
5625                }
5626                out += " TABLE";
5627                if create.if_not_exists {
5628                    out += " IF NOT EXISTS";
5629                }
5630                out += &format!(" {}", ast.name_text(create.name));
5631                let columns = ast
5632                    .column_defs(create.columns)
5633                    .iter()
5634                    .map(|def| {
5635                        let ty = match def.ty {
5636                            NONE => String::new(),
5637                            other => format!(" {}", ast.string(other)),
5638                        };
5639                        let null = if def.not_null { " NOT NULL" } else { "" };
5640                        format!("{}{ty}{null}", ast.string(def.name))
5641                    })
5642                    .collect::<Vec<_>>()
5643                    .join(", ");
5644                if !columns.is_empty() || create.query == NONE {
5645                    out += &format!(" ({columns})");
5646                }
5647                if create.query != NONE {
5648                    out += &format!(" AS {}", show_query(&ast, create.query));
5649                }
5650                out
5651            }
5652            Statement::CreateView(index) => {
5653                let create = ast.create_view(index);
5654                let mut out = "CREATE".to_string();
5655                if create.or_replace {
5656                    out += " OR REPLACE";
5657                }
5658                if create.temporary {
5659                    out += " TEMPORARY";
5660                }
5661                out += " VIEW";
5662                if create.if_not_exists {
5663                    out += " IF NOT EXISTS";
5664                }
5665                out += &format!(" {}", ast.name_text(create.name));
5666                if !create.columns.is_empty() {
5667                    let columns = ast.name(create.columns).collect::<Vec<_>>().join(", ");
5668                    out += &format!(" ({columns})");
5669                }
5670                out + &format!(" AS {}", show_query(&ast, create.query))
5671            }
5672            Statement::DropTable(index) => {
5673                let drop = ast.drop_table(index);
5674                let mut out = if drop.view { "DROP VIEW" } else { "DROP TABLE" }.to_string();
5675                if drop.if_exists {
5676                    out += " IF EXISTS";
5677                }
5678                let names = ast
5679                    .name_list(drop.names)
5680                    .iter()
5681                    .map(|&name| ast.name_text(name))
5682                    .collect::<Vec<_>>()
5683                    .join(", ");
5684                out + &format!(" {names}")
5685            }
5686            Statement::Schema(index) => {
5687                let schema = ast.schema(index);
5688                let mut out = if schema.drop { "DROP" } else { "CREATE" }.to_string();
5689                if schema.or_replace {
5690                    out += " OR REPLACE";
5691                }
5692                if schema.temporary {
5693                    out += " TEMPORARY";
5694                }
5695                out += " SCHEMA";
5696                if schema.quiet {
5697                    out += if schema.drop { " IF EXISTS" } else { " IF NOT EXISTS" };
5698                }
5699                out += &format!(" {}", ast.name_text(schema.name));
5700                if schema.cascade {
5701                    out += " CASCADE";
5702                }
5703                out
5704            }
5705            Statement::Alter(index) => {
5706                let alter = ast.alter(index);
5707                let mut out = if alter.view { "ALTER VIEW" } else { "ALTER TABLE" }.to_string();
5708                if alter.quiet {
5709                    out += " IF EXISTS";
5710                }
5711                out += &format!(" {} ", ast.name_text(alter.name));
5712                let expr = |expr| show(&ast, expr);
5713                out + &match alter.action {
5714                    AlterAction::Rename { to } => format!("RENAME TO {}", ast.string(to)),
5715                    AlterAction::RenameColumn { column, to } => {
5716                        format!("RENAME COLUMN {} TO {}", ast.string(column), ast.string(to))
5717                    }
5718                    AlterAction::AddColumn { column, quiet } => {
5719                        let mut out = "ADD COLUMN ".to_string();
5720                        if quiet {
5721                            out += "IF NOT EXISTS ";
5722                        }
5723                        out += &format!("{} {}", ast.string(column.name), ast.string(column.ty));
5724                        if column.default != NONE {
5725                            out += &format!(" DEFAULT {}", expr(column.default));
5726                        }
5727                        if column.not_null {
5728                            out += " NOT NULL";
5729                        }
5730                        out
5731                    }
5732                    AlterAction::DropColumn { column, quiet } => {
5733                        let quiet = if quiet { "IF EXISTS " } else { "" };
5734                        format!("DROP COLUMN {quiet}{}", ast.string(column))
5735                    }
5736                    AlterAction::Default { column, default } if default == NONE => {
5737                        format!("ALTER COLUMN {} DROP DEFAULT", ast.string(column))
5738                    }
5739                    AlterAction::Default { column, default } => {
5740                        format!("ALTER COLUMN {} SET DEFAULT {}", ast.string(column), expr(default))
5741                    }
5742                    AlterAction::NotNull { column, set } => {
5743                        let which = if set { "SET" } else { "DROP" };
5744                        format!("ALTER COLUMN {} {which} NOT NULL", ast.string(column))
5745                    }
5746                    AlterAction::Type { column, ty, using } => {
5747                        let mut out = format!("ALTER COLUMN {} SET DATA TYPE", ast.string(column));
5748                        if ty != NONE {
5749                            out += &format!(" {}", ast.string(ty));
5750                        }
5751                        if using != NONE {
5752                            out += &format!(" USING {}", expr(using));
5753                        }
5754                        out
5755                    }
5756                }
5757            }
5758            Statement::Index(index) => {
5759                let index = ast.index(index);
5760                if index.drop {
5761                    return format!("DROP INDEX {}", ast.name_text(index.name));
5762                }
5763                let unique = if index.unique { " UNIQUE" } else { "" };
5764                format!(
5765                    "CREATE{unique} INDEX {} ON {} ({})",
5766                    ast.name_text(index.name),
5767                    ast.name_text(index.table),
5768                    index.elements.len
5769                )
5770            }
5771            Statement::Type(index) => {
5772                let made = ast.type_def(index);
5773                let mut out = if made.drop { "DROP" } else { "CREATE" }.to_string();
5774                if made.or_replace {
5775                    out += " OR REPLACE";
5776                }
5777                if made.temporary {
5778                    out += " TEMPORARY";
5779                }
5780                out += " TYPE";
5781                if made.quiet {
5782                    out += if made.drop { " IF EXISTS" } else { " IF NOT EXISTS" };
5783                }
5784                out += &format!(" {}", ast.name_text(made.name));
5785                if !made.drop {
5786                    out += &format!(" AS {}", ast.string(made.ty));
5787                }
5788                if made.cascade {
5789                    out += " CASCADE";
5790                }
5791                out
5792            }
5793            Statement::Sequence(index) => {
5794                let sequence = ast.sequence(index);
5795                if !sequence.owner.is_empty() {
5796                    let mut out = "ALTER SEQUENCE".to_string();
5797                    if sequence.quiet {
5798                        out += " IF EXISTS";
5799                    }
5800                    return format!(
5801                        "{out} {} OWNED BY {}",
5802                        ast.name_text(sequence.name),
5803                        ast.name_text(sequence.owner)
5804                    );
5805                }
5806                let mut out = if sequence.drop { "DROP" } else { "CREATE" }.to_string();
5807                if sequence.or_replace {
5808                    out += " OR REPLACE";
5809                }
5810                if sequence.temporary {
5811                    out += " TEMPORARY";
5812                }
5813                out += " SEQUENCE";
5814                if sequence.quiet {
5815                    out += if sequence.drop { " IF EXISTS" } else { " IF NOT EXISTS" };
5816                }
5817                out += &format!(" {}", ast.name_text(sequence.name));
5818                if !sequence.drop {
5819                    let options = sequence.options;
5820                    out += &format!(
5821                        " INCREMENT BY {} MINVALUE {} MAXVALUE {} START {}{}",
5822                        options.increment,
5823                        options.min,
5824                        options.max,
5825                        options.start,
5826                        if options.cycle { " CYCLE" } else { " NO CYCLE" }
5827                    );
5828                }
5829                if sequence.cascade {
5830                    out += " CASCADE";
5831                }
5832                out
5833            }
5834            Statement::Insert(index) => {
5835                let insert = ast.insert(index);
5836                let mut out = format!("INSERT INTO {}", ast.name_text(insert.name));
5837                if !insert.columns.is_empty() {
5838                    let columns = ast.name(insert.columns).collect::<Vec<_>>().join(", ");
5839                    out += &format!(" ({columns})");
5840                }
5841                out += &format!(" {}", show_query(&ast, insert.source));
5842                show_returning(&ast, &insert, out)
5843            }
5844            Statement::Update(index) | Statement::Delete(index) => {
5845                let change = ast.insert(index);
5846                let columns = ast.name(change.columns).collect::<Vec<_>>().join(", ");
5847                let out = format!(
5848                    "{} {} ({columns}) {}",
5849                    if matches!(ast.statements[0], Statement::Update(_)) {
5850                        "UPDATE"
5851                    } else {
5852                        "DELETE"
5853                    },
5854                    ast.name_text(change.name),
5855                    show_query(&ast, change.source)
5856                );
5857                show_returning(&ast, &change, out)
5858            }
5859            Statement::Set(index) if ast.setting(index).pragma => {
5860                format!("PRAGMA {}", ast.string(ast.setting(index).name))
5861            }
5862            Statement::Set(index) => {
5863                let setting = ast.setting(index);
5864                let scope = match setting.scope.keyword() {
5865                    "" => String::new(),
5866                    word => format!(" {word}"),
5867                };
5868                format!("SET{scope} {} = {}", ast.string(setting.name), show(&ast, setting.value))
5869            }
5870            Statement::Reset(index) => {
5871                let setting = ast.setting(index);
5872                let scope = match setting.scope.keyword() {
5873                    "" => String::new(),
5874                    word => format!(" {word}"),
5875                };
5876                format!("RESET{scope} {}", ast.string(setting.name))
5877            }
5878            Statement::Checkpoint(name) if name == NONE => "CHECKPOINT".to_string(),
5879            Statement::Checkpoint(name) => format!("CHECKPOINT {}", ast.string(name)),
5880            Statement::Attach(index) => {
5881                let attach = ast.attach(index);
5882                let alias = if attach.alias == NONE {
5883                    String::new()
5884                } else {
5885                    format!(" AS {}", ast.string(attach.alias))
5886                };
5887                format!("ATTACH {}{alias}", show(&ast, attach.path))
5888            }
5889            Statement::Detach { name, if_exists } => {
5890                let exists = if if_exists { "IF EXISTS " } else { "" };
5891                format!("DETACH {exists}{}", ast.string(name))
5892            }
5893            Statement::Transaction(Transaction::Begin { read_only: false }) => "BEGIN".to_string(),
5894            Statement::Transaction(Transaction::Begin { read_only: true }) => {
5895                "BEGIN READ ONLY".to_string()
5896            }
5897            Statement::Transaction(Transaction::Commit) => "COMMIT".to_string(),
5898            Statement::Transaction(Transaction::Rollback) => "ROLLBACK".to_string(),
5899            Statement::Explain { query, analyze, statistics, codegen } => {
5900                let analyze = if analyze { "ANALYZE " } else { "" };
5901                let statistics = if statistics { "(STATISTICS) " } else { "" };
5902                let codegen = if codegen { "(CODEGEN) " } else { "" };
5903                format!("EXPLAIN {analyze}{statistics}{codegen}{}", show_query(&ast, query))
5904            }
5905        }
5906    }
5907
5908    #[test]
5909    fn expressions_and_queries_keep_their_source_ranges() {
5910        let sql = "SELECT 1 + 22";
5911        let ast = parse_ast(sql).expect("the query parses");
5912        let Statement::Query(query) = ast.statements[0] else { panic!("a query") };
5913        assert_eq!(ast.query_span(query), Span::new(0, sql.len() as u32));
5914        let twenty_two = ast
5915            .exprs
5916            .iter()
5917            .enumerate()
5918            .find_map(|(at, expr)| match *expr {
5919                Expr::Literal { kind: LiteralKind::Number, text } if ast.string(text) == "22" => {
5920                    Some(at as u32)
5921                }
5922                _ => None,
5923            })
5924            .expect("the literal is in the arena");
5925        assert_eq!(ast.expr_span(twenty_two), Span::new(11, 13));
5926    }
5927
5928    #[test]
5929    fn an_explain_keeps_the_query_it_was_asked_about() {
5930        assert_eq!(
5931            round_statement("EXPLAIN SELECT a FROM t WHERE a > 1"),
5932            "EXPLAIN SELECT a FROM t WHERE (a Gt 1)"
5933        );
5934        assert_eq!(round_statement("explain select 1"), "EXPLAIN SELECT 1");
5935        assert_eq!(round_statement("explain analyze select 1"), "EXPLAIN ANALYZE SELECT 1");
5936    }
5937
5938    #[test]
5939    fn the_three_explain_options_this_answers_mean_what_their_names_say() {
5940        // `ANALYZE` in the list is the keyword written the other way, so the two spellings have to
5941        // land on the same statement rather than on two that happen to print alike.
5942        assert_eq!(round_statement("EXPLAIN (ANALYZE) SELECT 1"), "EXPLAIN ANALYZE SELECT 1");
5943        assert_eq!(
5944            round_statement("explain (analyze) select 1"),
5945            round_statement("explain analyze select 1")
5946        );
5947        // `LOGICAL` names the plan this already prints, so asking for it changes nothing.
5948        assert_eq!(round_statement("EXPLAIN (LOGICAL) SELECT 1"), "EXPLAIN SELECT 1");
5949        assert_eq!(
5950            round_statement("EXPLAIN (STATISTICS) SELECT 1"),
5951            "EXPLAIN (STATISTICS) SELECT 1"
5952        );
5953        assert_eq!(
5954            round_statement("EXPLAIN (ANALYZE, STATISTICS) SELECT 1"),
5955            "EXPLAIN ANALYZE (STATISTICS) SELECT 1"
5956        );
5957        assert_eq!(
5958            round_statement("EXPLAIN ANALYZE (STATISTICS) SELECT 1"),
5959            "EXPLAIN ANALYZE (STATISTICS) SELECT 1"
5960        );
5961        assert_eq!(round_statement("explain (codegen) select 1"), "EXPLAIN (CODEGEN) SELECT 1");
5962    }
5963
5964    #[test]
5965    fn the_parts_of_an_explain_that_are_not_the_query_are_refused_by_name() {
5966        // An option name this does not answer is refused in DuckDB's own words, an option that
5967        // carries a value is refused by its grammar rule because none of the three takes one, and a
5968        // statement that is not a query has no plan to show.
5969        for (query, named) in [
5970            ("EXPLAIN (FORMAT JSON) SELECT 1", "Unimplemented explain type: format"),
5971            ("EXPLAIN (NONSENSE) SELECT 1", "Unimplemented explain type: nonsense"),
5972            ("EXPLAIN (CODEGEN, ANALYZE) SELECT 1", "cannot be combined"),
5973            ("EXPLAIN (ANALYZE false) SELECT 1", "ExplainOption"),
5974            ("EXPLAIN INSERT INTO t VALUES (1)", "InsertStatement"),
5975            ("EXPLAIN CREATE TABLE u (a INTEGER)", "CreateStatement"),
5976        ] {
5977            let error = parse_ast(query).expect_err(query).to_string();
5978            assert!(error.contains(named), "{query}: {error}");
5979        }
5980    }
5981
5982    #[test]
5983    fn a_set_keeps_its_name_its_scope_and_its_value() {
5984        assert_eq!(round_statement("SET memory_limit = '1GB'"), "SET memory_limit = '1GB'");
5985        assert_eq!(round_statement("set threads=4"), "SET threads = 4");
5986        assert_eq!(round_statement("SET GLOBAL threads = 4"), "SET GLOBAL threads = 4");
5987        assert_eq!(round_statement("SET SESSION threads = 4"), "SET SESSION threads = 4");
5988        assert_eq!(round_statement("SET LOCAL threads = 4"), "SET LOCAL threads = 4");
5989        assert_eq!(round_statement("RESET memory_limit"), "RESET memory_limit");
5990        assert_eq!(round_statement("RESET GLOBAL memory_limit"), "RESET GLOBAL memory_limit");
5991        assert_eq!(
5992            round_statement("SET TIME ZONE 'Asia/Kathmandu'"),
5993            "SET TimeZone = 'Asia/Kathmandu'"
5994        );
5995        assert_eq!(round_statement("SET TIME ZONE UTC"), "SET TimeZone = 'UTC'");
5996        assert_eq!(round_statement("SET TIME ZONE DEFAULT"), "RESET TimeZone");
5997        assert_eq!(round_statement("SET TIME ZONE LOCAL"), "RESET TimeZone");
5998    }
5999
6000    #[test]
6001    fn a_session_variable_is_refused_rather_than_read_as_a_setting() {
6002        // `SET VARIABLE x = 1` declares a session variable, which is not a knob on the engine, and
6003        // reading it as one would change an answer quietly.
6004        let error = parse_ast("SET VARIABLE x = 1").expect_err("a variable");
6005        assert_eq!(error.code().duckdb_name(), "Not implemented Error");
6006    }
6007
6008    #[test]
6009    fn set_schema_and_use_are_the_schema_setting() {
6010        assert_eq!(round_statement("SET SCHEMA 'main'"), "SET schema = 'main'");
6011        assert_eq!(round_statement("USE s1"), "SET schema = 's1'");
6012        assert_eq!(round_statement("USE memory.s1"), "SET schema = 'memory.s1'");
6013        assert_eq!(round_statement("USE \"a.b\""), "SET schema = '\"a.b\"'");
6014        let error = parse_ast("USE a.b.c").expect_err("three parts");
6015        assert_eq!(error.message(), "Expected \"USE database\" or \"USE database.schema\"");
6016    }
6017
6018    #[test]
6019    fn a_setting_written_with_a_list_of_values_is_refused_rather_than_taking_the_first() {
6020        let error = parse_ast("SET search_path = a, b").expect_err("a list of two");
6021        assert_eq!(error.message(), "SET can only contain a single value");
6022    }
6023
6024    #[test]
6025    fn the_query_m0_has_to_run_transforms() {
6026        assert_eq!(round("SELECT * FROM t WHERE x > 5"), "SELECT * FROM t WHERE (x Gt 5)");
6027    }
6028
6029    #[test]
6030    fn a_replace_list_rides_on_the_star_it_changes() {
6031        // The parentheses are optional around a single entry, which is how the clickbench load
6032        // recipe is not written but is how a lot of hand written sql is.
6033        assert_eq!(
6034            round("SELECT * REPLACE (a + 1 AS a) FROM t"),
6035            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
6036        );
6037        assert_eq!(
6038            round("SELECT * REPLACE a + 1 AS a FROM t"),
6039            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
6040        );
6041        assert_eq!(
6042            round("SELECT t.* REPLACE (make_date(a) AS a, b * 2 AS b) FROM t"),
6043            "SELECT t.* REPLACE (make_date(a) AS a, (b Multiply 2) AS b) FROM t"
6044        );
6045    }
6046
6047    #[test]
6048    fn one_column_cannot_be_replaced_twice() {
6049        // Caught here rather than in the binder because it is a mistake in what was written and
6050        // not a mistake about what is in the table, and duckdb reports it the same way.
6051        let error = parse_ast("SELECT * REPLACE (a + 1 AS a, a + 2 AS A) FROM t").unwrap_err();
6052        assert_eq!(error.to_string(), "Parser Error: Duplicate entry \"A\" in REPLACE list");
6053    }
6054
6055    #[test]
6056    fn a_table_function_argument_can_have_a_name_written_in_front_of_it() {
6057        // The grammar has `:=` and `=>`. It does not have `=`, which parses as a comparison and is
6058        // read back apart here, and that is the spelling the clickbench load recipe uses.
6059        for spelling in
6060            ["binary_as_string := True", "binary_as_string => True", "binary_as_string = True"]
6061        {
6062            assert_eq!(
6063                round(&format!("SELECT * FROM read_parquet('f.parquet', {spelling})")),
6064                "SELECT * FROM read_parquet('f.parquet', binary_as_string := TRUE)",
6065                "{spelling}"
6066            );
6067        }
6068    }
6069
6070    #[test]
6071    fn an_equality_that_is_not_a_bare_name_stays_an_argument() {
6072        // A qualified name on the left is not a parameter name, and neither is anything that is
6073        // not a name at all, so both of those stay the comparison they were written as.
6074        assert_eq!(round("SELECT * FROM f(t.a = 1)"), "SELECT * FROM f((t.a Eq 1))");
6075        assert_eq!(round("SELECT * FROM f(1 = 1)"), "SELECT * FROM f((1 Eq 1))");
6076    }
6077
6078    #[test]
6079    fn a_create_table_keeps_its_types_as_text() {
6080        assert_eq!(
6081            round_statement("CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"),
6082            "CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"
6083        );
6084        // The type is the text between the identifier and whatever follows it, parentheses and
6085        // all, because resolving `DECIMAL(18, 3)` into a width and a scale is the binder's job and
6086        // doing it here would mean two places that know the type table.
6087        assert_eq!(
6088            round_statement("CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"),
6089            "CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"
6090        );
6091    }
6092
6093    #[test]
6094    fn the_modifiers_on_a_create_table_survive() {
6095        assert_eq!(
6096            round_statement("CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"),
6097            "CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"
6098        );
6099        assert_eq!(
6100            round_statement("CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"),
6101            "CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"
6102        );
6103    }
6104
6105    #[test]
6106    fn or_replace_and_if_not_exists_in_one_statement_is_refused_here_and_not_later() {
6107        // The grammar has room for both and duckdb's has not, so its refusal is a parser error with
6108        // a caret under the `NOT` and this one is a parser error at the same stage. It is the same
6109        // sentence whatever is being created.
6110        for sql in [
6111            "CREATE OR REPLACE TABLE IF NOT EXISTS t (a INT)",
6112            "CREATE OR REPLACE VIEW IF NOT EXISTS v AS SELECT 1",
6113        ] {
6114            let error = parse_ast(sql).unwrap_err().to_string();
6115            assert_eq!(
6116                error,
6117                "Parser Error: Cannot specify both OR REPLACE and IF NOT EXISTS within single \
6118                 create statement"
6119            );
6120        }
6121    }
6122
6123    #[test]
6124    fn a_create_table_as_carries_the_query_and_not_the_types() {
6125        assert_eq!(
6126            round_statement("CREATE TABLE t AS SELECT a FROM u"),
6127            "CREATE TABLE t AS SELECT a FROM u"
6128        );
6129        // The names are the syntax's to say and the types are the query's, so the column
6130        // definitions here have names and no types.
6131        assert_eq!(
6132            round_statement("CREATE TABLE t (x, y) AS SELECT a, b FROM u"),
6133            "CREATE TABLE t (x, y) AS SELECT a, b FROM u"
6134        );
6135    }
6136
6137    #[test]
6138    fn a_create_view_carries_its_body_twice_over() {
6139        assert_eq!(
6140            round_statement("CREATE VIEW v AS SELECT a FROM u"),
6141            "CREATE VIEW v AS SELECT a FROM u"
6142        );
6143        assert_eq!(
6144            round_statement("CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"),
6145            "CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"
6146        );
6147        // The text the catalog keeps is the body and only the body, so that binding it again is
6148        // binding a query rather than a `CREATE` statement.
6149        let ast = parse_ast("CREATE VIEW v (x) AS SELECT a FROM u WHERE a > 1").expect("parses");
6150        let Statement::CreateView(index) = ast.statements[0] else {
6151            panic!("not a create view");
6152        };
6153        assert_eq!(ast.string(ast.create_view(index).sql), "SELECT a FROM u WHERE a > 1");
6154    }
6155
6156    #[test]
6157    fn a_drop_view_is_not_a_drop_table() {
6158        assert_eq!(round_statement("DROP VIEW IF EXISTS a, b"), "DROP VIEW IF EXISTS a, b");
6159        assert_eq!(round_statement("DROP TABLE a"), "DROP TABLE a");
6160    }
6161
6162    #[test]
6163    fn a_drop_table_is_a_list_of_qualified_names() {
6164        assert_eq!(round_statement("DROP TABLE t"), "DROP TABLE t");
6165        assert_eq!(round_statement("DROP TABLE IF EXISTS a, b.c"), "DROP TABLE IF EXISTS a, b.c");
6166    }
6167
6168    #[test]
6169    fn dropping_something_that_is_neither_a_table_nor_a_view_is_refused() {
6170        // `TableOrView` covers `MATERIALIZED VIEW` as well, which is not a thing this database has,
6171        // and dropping one as if it were an ordinary view is a wrong answer rather than a missing
6172        // feature.
6173        let error = parse_ast("DROP MATERIALIZED VIEW v").unwrap_err().to_string();
6174        assert!(error.starts_with("Not implemented Error"), "{error}");
6175    }
6176
6177    #[test]
6178    fn both_spellings_of_insert_arrive_at_a_query() {
6179        assert_eq!(
6180            round_statement("INSERT INTO t VALUES (1, 'a'), (2, 'b')"),
6181            "INSERT INTO t VALUES (1, 'a'), (2, 'b')"
6182        );
6183        assert_eq!(
6184            round_statement("INSERT INTO t (a, b) SELECT x, y FROM u"),
6185            "INSERT INTO t (a, b) SELECT x, y FROM u"
6186        );
6187    }
6188
6189    #[test]
6190    fn a_returning_list_is_held_as_a_query_over_the_table_it_writes() {
6191        assert_eq!(
6192            round_statement("INSERT INTO t AS x VALUES (1) RETURNING x.a, a + 1 AS b"),
6193            "INSERT INTO t VALUES (1) RETURNING SELECT x.a, (a Add 1) AS b FROM t AS x"
6194        );
6195        let deleted = round_statement("DELETE FROM t WHERE a = 1 RETURNING *");
6196        assert!(deleted.ends_with(" RETURNING SELECT * FROM t"), "{deleted}");
6197        let updated = round_statement("UPDATE t SET a = 2 RETURNING a");
6198        assert!(updated.ends_with(" RETURNING SELECT a FROM t"), "{updated}");
6199    }
6200
6201    #[test]
6202    fn an_insert_clause_that_changes_the_answer_is_refused() {
6203        for query in [
6204            "INSERT INTO t BY NAME SELECT 1 AS a",
6205            "INSERT INTO t VALUES (1) ON CONFLICT ON CONSTRAINT c DO NOTHING",
6206        ] {
6207            let error = parse_ast(query).unwrap_err().to_string();
6208            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
6209        }
6210    }
6211
6212    #[test]
6213    fn a_copy_from_a_file_is_an_insert_from_read_csv() {
6214        let ast = parse_ast(
6215            "COPY name FROM '/data/name.csv' (FORMAT csv, HEADER false, ESCAPE '\\', QUOTE '\"', NULL '')",
6216        )
6217        .unwrap();
6218        let Statement::Insert(index) = ast.statements[0] else { panic!("not an insert") };
6219        assert!(ast.insert(index).copy);
6220        assert_eq!(
6221            round_statement(
6222                "COPY name FROM '/data/name.csv' (FORMAT csv, HEADER false, ESCAPE '\\', QUOTE '\"', NULL '')"
6223            ),
6224            "INSERT INTO name SELECT * FROM read_csv('/data/name.csv', header := FALSE, \
6225             escape := '\\', quote := '\"', nullstr := '')"
6226        );
6227        assert_eq!(
6228            round_statement("COPY t (a, b) FROM 'in.csv' (HEADER, DELIMITER '|')"),
6229            "INSERT INTO t (a, b) SELECT * FROM read_csv('in.csv', header := TRUE, delim := '|')"
6230        );
6231        assert_eq!(
6232            round_statement("COPY t FROM 'in.csv' WITH DELIMITER AS ';' NULL 'NA' CSV HEADER"),
6233            "INSERT INTO t SELECT * FROM read_csv('in.csv', delim := ';', nullstr := 'NA', \
6234             header := TRUE)"
6235        );
6236        assert_eq!(
6237            round_statement("COPY t FROM 'x.parquet'"),
6238            "INSERT INTO t SELECT * FROM read_parquet('x.parquet')"
6239        );
6240        assert!(!{
6241            let ast = parse_ast("INSERT INTO t VALUES (1)").unwrap();
6242            let Statement::Insert(index) = ast.statements[0] else { panic!("not an insert") };
6243            ast.insert(index).copy
6244        });
6245    }
6246
6247    #[test]
6248    fn a_copy_this_does_not_read_is_refused_by_name() {
6249        for (query, message) in [
6250            ("COPY t FROM 'in.csv' (FOO 1)", "Unrecognized option \"foo\" for csv"),
6251            ("COPY t FROM 'in.csv' (SKIP 1)", "the option skip is not supported yet"),
6252            ("COPY t FROM 'in.json' (FORMAT json)", "FORMAT json is not supported yet"),
6253        ] {
6254            let error = parse_ast(query).unwrap_err().to_string();
6255            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
6256            assert!(error.contains(message), "{query} gave {error}");
6257        }
6258        for query in ["COPY t TO 'out.csv'", "COPY (SELECT 1) TO 'out.csv'"] {
6259            let error = parse_ast(query).unwrap_err().to_string();
6260            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
6261        }
6262    }
6263
6264    #[test]
6265    fn a_foreign_key_the_pin_refuses_is_refused_with_its_sentence() {
6266        for (query, message) in [
6267            (
6268                "CREATE TABLE t (a INT REFERENCES u (b) ON DELETE CASCADE)",
6269                "FOREIGN KEY constraints cannot use CASCADE, SET NULL or SET DEFAULT",
6270            ),
6271            (
6272                "CREATE TABLE t (a INT, FOREIGN KEY (a) REFERENCES u (b, c))",
6273                "The number of referencing and referenced columns for foreign keys must be the same",
6274            ),
6275        ] {
6276            let error = parse_ast(query).unwrap_err().to_string();
6277            assert!(error.ends_with(message), "{query} gave {error}");
6278        }
6279        let ast = parse_ast(
6280            "CREATE TABLE t (a INT REFERENCES u, b INT, FOREIGN KEY (b) REFERENCES s.v (c))",
6281        )
6282        .unwrap();
6283        let Statement::CreateTable(index) = ast.statements[0] else { panic!("not a create") };
6284        let create = ast.create_table(index);
6285        let lists = |slice| {
6286            ast.name_list(slice)
6287                .iter()
6288                .map(|&names| ast.name(names).collect::<Vec<_>>().join("."))
6289                .collect::<Vec<_>>()
6290        };
6291        assert_eq!(lists(create.foreign), ["a", "b"]);
6292        assert_eq!(lists(create.foreign_tables), ["u", "s.v"]);
6293        assert_eq!(lists(create.foreign_referenced), ["", "c"]);
6294    }
6295
6296    #[test]
6297    fn keys_are_held_in_the_order_written_with_the_primary_one_marked() {
6298        let ast = parse_ast(
6299            "CREATE TABLE t (a INT UNIQUE, b INT PRIMARY KEY, c INT, CONSTRAINT k UNIQUE (c, \"A\"))",
6300        )
6301        .unwrap();
6302        let Statement::CreateTable(index) = ast.statements[0] else { panic!() };
6303        let create = ast.create_table(index);
6304        let keys: Vec<Vec<&str>> =
6305            ast.name_list(create.keys).iter().map(|&names| ast.name(names).collect()).collect();
6306        assert_eq!(keys, [vec!["a"], vec!["b"], vec!["c", "A"]]);
6307        assert_eq!(create.primary, 1);
6308        for (query, message) in [
6309            (
6310                "CREATE TABLE t (i INT PRIMARY KEY, PRIMARY KEY (i))",
6311                "Parser Error: table \"t\" has more than one primary key",
6312            ),
6313            (
6314                "CREATE TABLE t (i INT, UNIQUE (i, I))",
6315                "Parser Error: column \"\"I\"\" appears twice in primary key constraint",
6316            ),
6317        ] {
6318            assert_eq!(parse_ast(query).unwrap_err().to_string(), message);
6319        }
6320    }
6321
6322    #[test]
6323    fn values_is_a_query_on_its_own_and_in_a_from() {
6324        assert_eq!(round("VALUES (1), (2)"), "VALUES (1), (2)");
6325        // Parenthesised it is a subquery whose body is the values, and bare it is a `ValuesRef`.
6326        // Two rules and one meaning, which is the grammar's doing and not something to flatten
6327        // here, because the parenthesised form can carry an order by and the bare one cannot.
6328        assert_eq!(
6329            round("SELECT * FROM (VALUES (1, 2), (3, 4)) t(a, b)"),
6330            "SELECT * FROM (VALUES (1, 2), (3, 4)) AS t"
6331        );
6332        assert_eq!(
6333            round("SELECT * FROM VALUES (1, 2), (3, 4) AS t(a, b)"),
6334            "SELECT * FROM VALUES (1, 2), (3, 4) AS t"
6335        );
6336        // Rows of different widths parse. Saying so wants the column count, which for an insert is
6337        // the table's, so the check belongs to the binder and not here.
6338        assert_eq!(round("VALUES (1), (2, 3)"), "VALUES (1), (2, 3)");
6339    }
6340
6341    #[test]
6342    fn non_recursive_ctes_inline_and_semantic_variants_are_explicit() {
6343        assert_eq!(
6344            round("WITH t AS (SELECT 1 AS x) SELECT x FROM t"),
6345            "SELECT x FROM (SELECT 1 AS x) AS t"
6346        );
6347        assert_eq!(
6348            round("WITH t(x) AS NOT MATERIALIZED (SELECT 1) SELECT x FROM t"),
6349            "SELECT x FROM (SELECT 1) AS t"
6350        );
6351        let query = "WITH RECURSIVE t(x) AS (SELECT 1) SELECT x FROM t";
6352        let error = parse_ast(query).expect_err("the unsupported CTE shape is refused");
6353        assert!(error.to_string().starts_with("Not implemented Error"), "{query}: {error}");
6354    }
6355
6356    /// A plain definition named twice is held, and the same one named once is not.
6357    ///
6358    /// Inlining a definition that two places read means running it twice, so the rule is the count
6359    /// of reads and the word written only settles the cases where somebody wrote one. `NOT
6360    /// MATERIALIZED` is the one that says inline it anyway, and it says so however many times the
6361    /// name is read.
6362    #[test]
6363    fn a_plain_cte_read_twice_is_held_and_one_read_once_is_inlined() {
6364        assert_eq!(
6365            round("WITH t AS (SELECT 1 AS x) SELECT * FROM t a, t b"),
6366            "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT * FROM t AS a, t AS b"
6367        );
6368        assert_eq!(
6369            round("WITH t AS (SELECT 1 AS x) SELECT x FROM t"),
6370            "SELECT x FROM (SELECT 1 AS x) AS t"
6371        );
6372        assert_eq!(
6373            round("WITH t AS NOT MATERIALIZED (SELECT 1 AS x) SELECT * FROM t a, t b"),
6374            "SELECT * FROM (SELECT 1 AS x) AS a, (SELECT 1 AS x) AS b"
6375        );
6376        // A name a later definition reads is read, since that definition runs too.
6377        assert_eq!(
6378            round("WITH t AS (SELECT 1 AS x), u AS (SELECT x FROM t) SELECT x FROM t"),
6379            "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT x FROM t"
6380        );
6381        // Qualified, so it is not a read of the definition and there is only the one.
6382        assert_eq!(
6383            round("WITH t AS (SELECT 1 AS x) SELECT * FROM t a, main.t b"),
6384            "SELECT * FROM (SELECT 1 AS x) AS a, main.t AS b"
6385        );
6386    }
6387
6388    /// A definition written inside a subquery is inlined however many times it is read.
6389    ///
6390    /// The rows of a held definition are produced once for the whole statement, and a definition
6391    /// written inside a subquery can name a column of the query around it, which is an answer per
6392    /// outer row. Telling the two apart is a question about resolved columns, so what is asked here
6393    /// is the question this pass can answer: whether there is any query around it at all.
6394    #[test]
6395    fn a_cte_written_inside_a_subquery_is_inlined_however_often_it_is_read() {
6396        assert_eq!(
6397            round("SELECT * FROM (WITH t AS (SELECT 1 AS x) SELECT * FROM t a, t b) c"),
6398            "SELECT * FROM (SELECT * FROM (SELECT 1 AS x) AS a, (SELECT 1 AS x) AS b) AS c"
6399        );
6400        assert_eq!(
6401            round("WITH o AS (WITH i AS (SELECT 1 AS x) SELECT * FROM i a, i b) SELECT * FROM o"),
6402            "SELECT * FROM (SELECT * FROM (SELECT 1 AS x) AS a, (SELECT 1 AS x) AS b) AS o"
6403        );
6404    }
6405
6406    /// A name a definition further in takes over is left alone.
6407    ///
6408    /// Which of the two definitions a read means is a question about scopes, and the count here is
6409    /// a count of spellings, so a query that writes the name twice gets what every query got before
6410    /// the count existed.
6411    #[test]
6412    fn a_plain_cte_whose_name_is_written_again_further_in_is_inlined() {
6413        assert_eq!(
6414            round(
6415                "WITH t AS (SELECT 1 AS x) SELECT * FROM t a, \
6416                 (WITH t AS (SELECT 2 AS x) SELECT x FROM t) b"
6417            ),
6418            "SELECT * FROM (SELECT 1 AS x) AS a, (SELECT x FROM (SELECT 2 AS x) AS t) AS b"
6419        );
6420    }
6421
6422    /// A materialised one keeps its definition, because putting it in two places runs it twice.
6423    #[test]
6424    fn a_materialized_cte_stays_a_definition_and_its_references_stay_references() {
6425        assert_eq!(
6426            round("WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT x FROM t"),
6427            "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT x FROM t"
6428        );
6429        assert_eq!(
6430            round("WITH t(y) AS MATERIALIZED (SELECT 1) SELECT y FROM t"),
6431            "WITH t(y) AS MATERIALIZED (SELECT 1) SELECT y FROM t"
6432        );
6433        // Two references are two sources naming one definition, which is the whole point of the
6434        // word: the inlined form above would be two copies of the query.
6435        assert_eq!(
6436            round("WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT * FROM t a, t b"),
6437            "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT * FROM t AS a, t AS b"
6438        );
6439        // The inner name shadows the outer one, which is decided here and nowhere later.
6440        assert_eq!(
6441            round(
6442                "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT (WITH t AS (SELECT 2 AS x) \
6443                 SELECT x FROM t) AS inner"
6444            ),
6445            "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT (SELECT x FROM (SELECT 2 AS x) AS t) \
6446             AS inner"
6447        );
6448        // A definition may read one written before it, and it is the definition that is read
6449        // rather than a second copy of the query behind it.
6450        assert_eq!(
6451            round(
6452                "WITH a AS MATERIALIZED (SELECT 1 AS x), b AS MATERIALIZED (SELECT x + 1 AS y \
6453                 FROM a) SELECT y FROM b"
6454            ),
6455            "WITH a AS MATERIALIZED (SELECT 1 AS x) WITH b AS MATERIALIZED (SELECT (x Add 1) \
6456             AS y FROM a) SELECT y FROM b"
6457        );
6458    }
6459
6460    /// `DESCRIBE` is a query body, and the two spellings that name something become a star over it.
6461    ///
6462    /// Naming a table is not a shortcut for the query. On the reference binary `DESCRIBE t` and
6463    /// `DESCRIBE SELECT * FROM t` print the same six columns and the same rows, down to the `NO` on
6464    /// a column that refuses nulls, so rewriting one into the other costs nothing and leaves the
6465    /// binder with one case instead of three. A file name goes down the same path as a table name
6466    /// because a bare string in a `FROM` clause is already a name the replacement scan picks up.
6467    #[test]
6468    fn describe_rewrites_a_name_into_a_star_over_it() {
6469        assert_eq!(round("DESCRIBE SELECT 1 AS a"), "DESCRIBE SELECT 1 AS a");
6470        assert_eq!(round("DESCRIBE t"), "DESCRIBE SELECT * FROM t");
6471        assert_eq!(round("DESC t"), "DESCRIBE SELECT * FROM t");
6472        assert_eq!(round("DESCRIBE 'x.parquet'"), "DESCRIBE SELECT * FROM x.parquet");
6473        // A body and not a statement kind, so it nests both ways with no rule of its own.
6474        assert_eq!(
6475            round("SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"),
6476            "SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"
6477        );
6478        assert_eq!(round("DESCRIBE DESCRIBE SELECT 1 AS a"), "DESCRIBE DESCRIBE SELECT 1 AS a");
6479    }
6480
6481    /// `SUMMARIZE` shares both of `DESCRIBE`'s grammar rules and is a different statement.
6482    ///
6483    /// It reads every row and returns one row per column carrying the min, the max, the count and
6484    /// the approximate distinct count, so none of it falls out of the `DESCRIBE` path. The word is
6485    /// the only thing in the tree that tells the two apart, which is why the transform looks at it
6486    /// rather than trusting the rule name it arrived under.
6487    #[test]
6488    fn summarize_is_refused_even_though_it_parses_as_a_describe() {
6489        for query in ["SUMMARIZE t", "SUMMARIZE SELECT 1"] {
6490            let error = parse_ast(query).expect_err("summarize is not implemented");
6491            let message = error.to_string();
6492            assert!(message.starts_with("Not implemented Error"), "{query} failed with {message}");
6493        }
6494    }
6495
6496    #[test]
6497    fn every_statement_in_the_corpus_gets_a_defined_answer() {
6498        // The point of the test is the word defined. Half of these are statement kinds and
6499        // clauses this milestone does not cover, and the requirement is not that they work, it is
6500        // that they fail by saying so. A panic, a silently dropped clause or an internal error
6501        // would each be a different bug and all three would be invisible without this.
6502        let mut done = 0;
6503        for query in CORPUS {
6504            match parse_ast(query) {
6505                Ok(ast) => {
6506                    assert_eq!(ast.statements.len(), 1, "{query}");
6507                    done += 1;
6508                }
6509                Err(error) => {
6510                    let message = error.to_string();
6511                    assert!(
6512                        message.starts_with("Not implemented Error"),
6513                        "{query} failed with {message}, which is not a not-implemented error"
6514                    );
6515                }
6516            }
6517        }
6518        // Not an assertion about the right number. It is a ratchet: this only moves up, and the
6519        // day it moves down somebody has taken a construct out without meaning to.
6520        assert!(done >= 31, "only {done} of the corpus transforms, which is fewer than it was");
6521    }
6522
6523    #[test]
6524    fn the_ast_is_far_smaller_than_the_parse_tree() {
6525        let query = CORPUS[4];
6526        let tree = parse(query).unwrap();
6527        let ast = parse_ast(query).unwrap();
6528        // The twenty precedence levels are the difference. Every one of them is a node in the
6529        // parse tree for every expression at every depth, and none of them survives into the AST.
6530        assert!(
6531            ast.node_count() * 20 < tree.arena_len(),
6532            "{} ast nodes against {} parse nodes",
6533            ast.node_count(),
6534            tree.arena_len()
6535        );
6536    }
6537
6538    #[test]
6539    fn precedence_comes_out_of_the_chain_and_into_the_tree() {
6540        assert_eq!(round("SELECT 1 + 2 * 3"), "SELECT (1 Add (2 Multiply 3))");
6541        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
6542        assert_eq!(round("SELECT 1 + 2 + 3"), "SELECT ((1 Add 2) Add 3)");
6543        assert_eq!(round("SELECT 1 - 2 - 3"), "SELECT ((1 Subtract 2) Subtract 3)");
6544        assert_eq!(
6545            round("SELECT a OR b AND c"),
6546            "SELECT (a Or (b And c))",
6547            "and binds tighter than or"
6548        );
6549    }
6550
6551    #[test]
6552    fn a_double_negation_is_two_nodes_and_not_none() {
6553        // Folding it would be an optimizer decision and this is not the optimizer. It also would
6554        // not be safe in general: `NOT NOT x` on a null is still null and on a non boolean it is
6555        // still an error, and both of those have to survive to the binder to be reported.
6556        assert_eq!(round("SELECT NOT NOT a"), "SELECT (Not (Not a))");
6557    }
6558
6559    #[test]
6560    fn a_parenthesised_single_expression_is_not_a_row() {
6561        assert_eq!(round("SELECT (a)"), "SELECT a");
6562        assert_eq!(round("SELECT (a, b)"), "SELECT ROW(a, b)");
6563    }
6564
6565    #[test]
6566    fn a_bracketed_list_is_a_list_of_however_many_items_were_written() {
6567        // One item is a list of one, which is where this parts company with the parenthesised form
6568        // above: `(a)` is `a` and `[a]` is a list, because the brackets are what say list.
6569        assert_eq!(round("SELECT [a]"), "SELECT [a]");
6570        assert_eq!(round("SELECT [1, 2, 3]"), "SELECT [1, 2, 3]");
6571        assert_eq!(round("SELECT []"), "SELECT []");
6572        assert_eq!(round("SELECT ['a.parquet', 'b.parquet']"), "SELECT ['a.parquet', 'b.parquet']");
6573    }
6574
6575    #[test]
6576    fn a_parameter_carries_its_identifier_however_it_was_written() {
6577        assert_eq!(round("SELECT $1"), "SELECT $1");
6578        assert_eq!(round("SELECT ?1"), "SELECT $1");
6579        assert_eq!(round("SELECT $name"), "SELECT $name");
6580        // A bare question mark is numbered by where it is, and the counting is its own, so a later
6581        // `$2` does not push the first one along. This is duckdb v1.4.1, which prints `$1 + $2`.
6582        assert_eq!(round("SELECT ? + $2"), "SELECT ($1 Add $2)");
6583        assert_eq!(round("SELECT ?, ?, ?"), "SELECT $1, $2, $3");
6584    }
6585
6586    #[test]
6587    fn the_parameters_of_a_statement_are_listed_once_each_in_written_order() {
6588        let ast = parse_ast("SELECT $b, $a, $b WHERE $a").expect("parses");
6589        assert_eq!(ast.parameters(), vec!["b", "a"]);
6590        assert!(parse_ast("SELECT 1").expect("parses").parameters().is_empty());
6591    }
6592
6593    #[test]
6594    fn the_three_ways_to_write_an_alias_all_arrive() {
6595        assert_eq!(round("SELECT a AS b"), "SELECT a AS b");
6596        assert_eq!(round("SELECT a b"), "SELECT a AS b");
6597        assert_eq!(round("SELECT b: a"), "SELECT a AS b");
6598        assert_eq!(round("SELECT a"), "SELECT a", "and no alias when none was written");
6599    }
6600
6601    #[test]
6602    fn a_from_with_no_select_selects_everything() {
6603        // DuckDB's own shorthand. Inventing the star here rather than in the binder means the
6604        // binder never has to know that the clause it is looking at was the one that was missing.
6605        assert_eq!(round("FROM t"), "SELECT * FROM t");
6606        assert_eq!(round("FROM t SELECT a"), "SELECT a FROM t");
6607    }
6608
6609    #[test]
6610    fn joins_nest_to_the_left() {
6611        assert_eq!(
6612            round("SELECT * FROM a JOIN b ON a.i = b.i LEFT JOIN c USING (k)"),
6613            "SELECT * FROM ((a Inner JOIN b ON (a.i Eq b.i)) Left JOIN c USING (k))"
6614        );
6615        assert_eq!(
6616            round("SELECT * FROM a NATURAL JOIN b"),
6617            "SELECT * FROM (a NATURAL Inner JOIN b)"
6618        );
6619        assert_eq!(round("SELECT * FROM a CROSS JOIN b"), "SELECT * FROM (a Cross JOIN b)");
6620        assert_eq!(
6621            round("SELECT * FROM a POSITIONAL JOIN b"),
6622            "SELECT * FROM (a Positional JOIN b)"
6623        );
6624        assert_eq!(round("SELECT * FROM a, b"), "SELECT * FROM a, b", "a comma is not a join node");
6625    }
6626
6627    #[test]
6628    fn a_qualified_name_keeps_its_parts_however_it_was_spelled() {
6629        // Five grammar rules can produce a column reference and they disagree about which
6630        // component is a schema and which is a table. None of that is decidable without the
6631        // catalog, so the AST holds the parts and the binder decides.
6632        assert_eq!(round("SELECT a"), "SELECT a");
6633        assert_eq!(round("SELECT t.a"), "SELECT t.a");
6634        assert_eq!(round("SELECT s.t.a"), "SELECT s.t.a");
6635        assert_eq!(round("SELECT c.s.t.a"), "SELECT c.s.t.a");
6636        assert_eq!(round("SELECT * FROM s.t"), "SELECT * FROM s.t");
6637    }
6638
6639    #[test]
6640    fn a_star_can_be_qualified() {
6641        assert_eq!(round("SELECT *"), "SELECT *");
6642        assert_eq!(round("SELECT t.*"), "SELECT t.*");
6643        assert_eq!(round("SELECT s.t.*"), "SELECT s.t.*");
6644    }
6645
6646    #[test]
6647    fn a_quoted_identifier_keeps_its_case_and_loses_its_quotes() {
6648        // DuckDB does not fold identifier case at any point, quoted or not, which the tokenizer
6649        // work established by reading the source. So the only thing to do here is take the quotes
6650        // off and resolve the doubled ones.
6651        let ast = parse_ast("SELECT \"Mixed Case\", \"a\"\"b\"").unwrap();
6652        assert_eq!(ast.strings[0], "Mixed Case");
6653        assert_eq!(ast.strings[1], "a\"b");
6654    }
6655
6656    #[test]
6657    fn a_string_literal_is_decoded_and_adjacent_ones_are_joined() {
6658        assert_eq!(round("SELECT 'it''s'"), "SELECT 'it's'");
6659        assert_eq!(round("SELECT 'a'\n'b'"), "SELECT 'ab'", "the standard's adjacency rule");
6660    }
6661
6662    /// Per #276, where the tag and the dollars were coming through as part of the value.
6663    #[test]
6664    fn a_dollar_quoted_string_loses_its_dollars_and_its_tag() {
6665        assert_eq!(round("SELECT $$dollar quoted$$"), "SELECT 'dollar quoted'");
6666        assert_eq!(round("SELECT $tag$body$tag$"), "SELECT 'body'");
6667        assert_eq!(round("SELECT $$$$"), "SELECT ''", "an empty tag and an empty body");
6668        // Nothing in the body is escaped, which is what the spelling is for, so a quote is a quote
6669        // and a dollar that is not the closing tag is a dollar.
6670        assert_eq!(round("SELECT $tag$it''s $other$ fine$tag$"), "SELECT 'it''s $other$ fine'");
6671        // An unterminated one has no closing tag to take off and keeps every byte it was given.
6672        assert_eq!(round("SELECT $$open"), "SELECT '$$open'");
6673    }
6674
6675    /// Per #329, where every prefixed spelling came back as the source text it was written as.
6676    ///
6677    /// The escapes are the ones the pinned binary takes, read off it one at a time. The two that
6678    /// are easy to get wrong are `\v`, which is not an escape and is the letter, and `\u`, which
6679    /// wants all four digits and otherwise drops the backslash and keeps the letter.
6680    #[test]
6681    fn an_escape_string_resolves_its_backslashes() {
6682        assert_eq!(round("SELECT E'a\\nb'"), "SELECT 'a\nb'");
6683        assert_eq!(round("SELECT e'a\\tb'"), "SELECT 'a\tb'", "the prefix is a letter, not a name");
6684        assert_eq!(round("SELECT E'a\\rb'"), "SELECT 'a\rb'");
6685        assert_eq!(round("SELECT E'a\\bb'"), "SELECT 'a\u{8}b'");
6686        assert_eq!(round("SELECT E'a\\fb'"), "SELECT 'a\u{c}b'");
6687        assert_eq!(round("SELECT E'a\\\\b'"), "SELECT 'a\\b'");
6688        assert_eq!(round("SELECT E'a\\'b'"), "SELECT 'a'b'", "a quote, the same as ''");
6689        assert_eq!(round("SELECT E'a''b'"), "SELECT 'a'b'", "and '' still means a quote here");
6690        // A backslash in front of anything else is dropped and the character is kept, which is what
6691        // makes \v the letter v.
6692        assert_eq!(round("SELECT E'a\\vb'"), "SELECT 'avb'");
6693        assert_eq!(round("SELECT E'a\\qb'"), "SELECT 'aqb'");
6694    }
6695
6696    /// The escapes that write a byte rather than a character, and the one that writes a character.
6697    #[test]
6698    fn a_numeric_escape_writes_the_byte_or_the_character_it_names() {
6699        assert_eq!(round("SELECT E'\\x41'"), "SELECT 'A'");
6700        assert_eq!(round("SELECT E'\\x4142'"), "SELECT 'A42'", "two digits at the most");
6701        assert_eq!(
6702            round("SELECT E'a\\x'"),
6703            "SELECT 'ax'",
6704            "and one at the least, or it is a letter"
6705        );
6706        assert_eq!(round("SELECT E'\\101'"), "SELECT 'A'");
6707        assert_eq!(round("SELECT E'\\1011'"), "SELECT 'A1'", "three digits at the most");
6708        assert_eq!(round("SELECT E'\\8'"), "SELECT '8'", "8 is not an octal digit");
6709        // Bytes and not characters, so two of them make one character and one of them makes none.
6710        assert_eq!(round("SELECT E'\\xc3\\xa9'"), "SELECT 'é'");
6711        assert_eq!(round("SELECT E'\\u00e9'"), "SELECT 'é'");
6712        assert_eq!(round("SELECT E'a\\u41'"), "SELECT 'au41'", "four digits or it is a letter");
6713        assert_eq!(round("SELECT E'a\\uZZZZ'"), "SELECT 'auZZZZ'");
6714        assert_eq!(
6715            round("SELECT E'\\ud83d\\ude00'"),
6716            "SELECT 'ud83dude00'",
6717            "surrogates are not it"
6718        );
6719    }
6720
6721    /// The two ways an escape string is not a string at all, both with the message upstream gives.
6722    #[test]
6723    fn an_escape_string_that_is_not_a_string_raises() {
6724        let error = parse_ast("SELECT E'a\\x00'").unwrap_err().to_string();
6725        assert_eq!(error, "Parser Error: Null character not permitted in escape string literal");
6726        let error = parse_ast("SELECT E'a\\377'").unwrap_err().to_string();
6727        assert_eq!(
6728            error,
6729            "Parser Error: Invalid UTF-8 in escape string literal at byte offset 1: byte mismatch",
6730            "the offset is where the bytes stop being a string, not where the escape was written"
6731        );
6732    }
6733
6734    /// The other prefixes, all of them measured against the pinned binary rather than assumed.
6735    #[test]
6736    fn the_other_string_prefixes_are_what_upstream_makes_of_them() {
6737        // N is the string and a cast of it to VARCHAR, which is where the column name comes from.
6738        assert_eq!(round("SELECT N'abc'"), "SELECT CAST('abc' AS VARCHAR)");
6739        assert_eq!(round("SELECT n'abc'"), "SELECT CAST('abc' AS VARCHAR)");
6740        // B is not a bit string. It is the letter b in front of the body, untouched.
6741        assert_eq!(round("SELECT B'101'"), "SELECT 'b101'");
6742        assert_eq!(round("SELECT b'abc'"), "SELECT 'babc'");
6743        assert_eq!(round("SELECT B''"), "SELECT 'b'", "an empty one is the letter on its own");
6744    }
6745
6746    /// X is the prefix that is not a string at all, per #329.
6747    ///
6748    /// What is kept is the text the blob prints as, because that is the text the column is named
6749    /// after and the text the cast reads the bytes back from, and one text that does both is one
6750    /// text that cannot disagree with itself.
6751    #[test]
6752    fn a_hex_string_is_a_blob_and_not_a_string() {
6753        assert_eq!(round("SELECT x'4142'"), "SELECT 'AB'::BLOB");
6754        assert_eq!(round("SELECT X'4142'"), "SELECT 'AB'::BLOB");
6755        assert_eq!(round("SELECT x'ff41'"), "SELECT '\\xFFA'::BLOB", "a byte that does not print");
6756        assert_eq!(round("SELECT x''"), "SELECT ''::BLOB", "an empty one is an empty blob");
6757        // A quote and a backslash are bytes that do not print either, which is what keeps the text
6758        // something the cast can read back.
6759        assert_eq!(round("SELECT x'2741'"), "SELECT '\\x27A'::BLOB");
6760        assert_eq!(round("SELECT x'5c7834314141'"), "SELECT '\\x5Cx41AA'::BLOB");
6761        // An odd number of digits is a parser error and a digit that is not one is not, because
6762        // upstream writes the pairs out without looking at them and the cast is what looks.
6763        let error = parse_ast("SELECT x'4'").unwrap_err().to_string();
6764        assert_eq!(
6765            error,
6766            "Parser Error: Hex string literal must have an even number of hex digits"
6767        );
6768        assert_eq!(round("SELECT x'41zz'"), "SELECT '\\x41\\xzz'::BLOB");
6769    }
6770
6771    #[test]
6772    fn the_null_and_boolean_tests_are_postfix_unary_operators() {
6773        assert_eq!(round("SELECT x IS NULL"), "SELECT (IsNull x)");
6774        assert_eq!(round("SELECT x IS NOT NULL"), "SELECT (IsNotNull x)");
6775        assert_eq!(round("SELECT x ISNULL"), "SELECT (IsNull x)");
6776        assert_eq!(round("SELECT x NOTNULL"), "SELECT (IsNotNull x)");
6777        assert_eq!(round("SELECT x IS TRUE"), "SELECT (IsTrue x)");
6778        assert_eq!(round("SELECT x IS NOT FALSE"), "SELECT (IsNotFalse x)");
6779        assert_eq!(round("SELECT x IS DISTINCT FROM y"), "SELECT (x IsDistinctFrom y)");
6780        assert_eq!(round("SELECT x IS NOT DISTINCT FROM y"), "SELECT (x IsNotDistinctFrom y)");
6781    }
6782
6783    #[test]
6784    fn the_like_family_folds_its_negation_into_the_operator() {
6785        assert_eq!(round("SELECT x LIKE 'a'"), "SELECT (x Like 'a')");
6786        assert_eq!(round("SELECT x NOT LIKE 'a'"), "SELECT (x NotLike 'a')");
6787        assert_eq!(round("SELECT x ILIKE 'a'"), "SELECT (x ILike 'a')");
6788        assert_eq!(round("SELECT x ~~ 'a'"), "SELECT (x Like 'a')", "the operator spelling");
6789        assert_eq!(round("SELECT x !~~ 'a'"), "SELECT (x NotLike 'a')");
6790        assert_eq!(round("SELECT x SIMILAR TO 'a'"), "SELECT (x SimilarTo 'a')");
6791        // Glob has no negated operator to fold into, so the negation stays where it was written.
6792        assert_eq!(round("SELECT x NOT GLOB 'a'"), "SELECT (Not (x Glob 'a'))");
6793    }
6794
6795    #[test]
6796    fn between_and_in_carry_their_negation_as_a_flag() {
6797        assert_eq!(round("SELECT x BETWEEN 1 AND 2"), "SELECT (x BETWEEN 1 AND 2)");
6798        assert_eq!(round("SELECT x NOT BETWEEN 1 AND 2"), "SELECT (NOT x BETWEEN 1 AND 2)");
6799        assert_eq!(round("SELECT x IN (1, 2)"), "SELECT (x IN [1, 2])");
6800        assert_eq!(round("SELECT x NOT IN (1, 2)"), "SELECT (NOT x IN [1, 2])");
6801    }
6802
6803    #[test]
6804    fn both_spellings_of_a_cast_are_the_same_node() {
6805        assert_eq!(round("SELECT CAST(x AS BIGINT)"), "SELECT CAST(x AS BIGINT)");
6806        assert_eq!(round("SELECT x::BIGINT"), "SELECT CAST(x AS BIGINT)");
6807        assert_eq!(round("SELECT TRY_CAST(x AS BIGINT)"), "SELECT TRY_CAST(x AS BIGINT)");
6808        assert_eq!(
6809            round("SELECT x::DECIMAL(18, 3)"),
6810            "SELECT CAST(x AS DECIMAL(18, 3))",
6811            "the type is kept as text because parsing it is the type system's job"
6812        );
6813    }
6814
6815    #[test]
6816    fn a_typed_literal_is_a_third_spelling_of_the_same_cast() {
6817        assert_eq!(round("SELECT DATE '1995-09-01'"), "SELECT CAST('1995-09-01' AS DATE)");
6818        assert_eq!(
6819            round("SELECT date '1995-09-01'"),
6820            "SELECT CAST('1995-09-01' AS date)",
6821            "the type is kept as written, the same as it is in the other two spellings"
6822        );
6823        assert_eq!(
6824            round("SELECT TIMESTAMP '2020-01-01 03:04:05'"),
6825            "SELECT CAST('2020-01-01 03:04:05' AS TIMESTAMP)"
6826        );
6827        assert_eq!(
6828            round("SELECT DECIMAL(5, 2) '1.5'"),
6829            "SELECT CAST('1.5' AS DECIMAL(5, 2))",
6830            "any type the cast takes is a typed literal, parameters and all"
6831        );
6832        assert_eq!(
6833            round("SELECT VARCHAR 'hi' FROM t"),
6834            "SELECT CAST('hi' AS VARCHAR) FROM t",
6835            "including the ones where the cast has nothing to do"
6836        );
6837    }
6838
6839    #[test]
6840    fn a_case_keeps_its_arms_in_order() {
6841        assert_eq!(
6842            round("SELECT CASE WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"),
6843            "SELECT CASE - WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"
6844        );
6845        assert_eq!(
6846            round("SELECT CASE x WHEN 1 THEN 'a' END"),
6847            "SELECT CASE x WHEN 1 THEN 'a' ELSE - END",
6848            "a simple case keeps the operand and a missing else is not an implicit null yet"
6849        );
6850    }
6851
6852    #[test]
6853    fn a_field_access_and_a_method_call_are_ordinary_function_calls() {
6854        // Which is what DuckDB makes of them too. Giving each its own AST node would mean the
6855        // binder needs a rule for something the function resolver already handles.
6856        assert_eq!(round("SELECT (f(x)).y"), "SELECT struct_extract(f(x), 'y')");
6857        assert_eq!(round("SELECT a[1]"), "SELECT array_extract(a, 1)");
6858    }
6859
6860    /// The four ways of leaving a bound out, all of which upstream fills in the same way.
6861    #[test]
6862    fn a_range_gets_the_bounds_the_query_left_out() {
6863        assert_eq!(round("SELECT a[1:2]"), "SELECT array_slice(a, 1, 2)");
6864        assert_eq!(round("SELECT a[:2]"), "SELECT array_slice(a, 1, 2)");
6865        assert_eq!(round("SELECT a[2:]"), "SELECT array_slice(a, 2, -1)");
6866        assert_eq!(round("SELECT a[:]"), "SELECT array_slice(a, 1, -1)");
6867        // `EndSliceMinus`, which is a range with no end rather than a subtraction of nothing.
6868        assert_eq!(round("SELECT a[1:-]"), "SELECT array_slice(a, 1, -1)");
6869        assert_eq!(round("SELECT a[1:2:3]"), "SELECT array_slice(a, 1, 2, 3)");
6870        // A step that was written and left empty, which upstream fills with a list so that the call
6871        // fails to bind. Answering a row here would be answering where the reference refuses.
6872        assert_eq!(round("SELECT a[1:2:]"), "SELECT array_slice(a, 1, 2, [])");
6873    }
6874
6875    /// `[]` is the one subscript the parser takes and the transformer refuses, in upstream's words.
6876    #[test]
6877    fn an_empty_subscript_is_not_a_subscript() {
6878        let error = parse_ast("SELECT a[]").expect_err("an empty subscript");
6879        assert_eq!(error.message(), "Empty subscript '[]' is not allowed");
6880    }
6881
6882    /// A rule that wrote a keyword is not a rule that said nothing, however few children it has.
6883    /// Per #313.
6884    #[test]
6885    fn a_keyword_is_not_stepped_through_on_the_way_to_its_one_argument() {
6886        for (sql, rule) in [
6887            ("SELECT unpack([1])", "UnpackExpression"),
6888            ("SELECT columns('a')", "ColumnsExpression"),
6889        ] {
6890            let error = parse_ast(sql).expect_err(sql);
6891            assert!(error.message().ends_with(rule), "{sql}: {error}");
6892        }
6893        // Grouping brackets really do say nothing, and that is the one rule of this shape that is
6894        // stepped through rather than refused.
6895        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
6896        assert_eq!(round("SELECT -(7)"), "SELECT (Negate 7)");
6897    }
6898
6899    /// The three spellings of a null check, two of which are their own grammar rule. Per #306.
6900    #[test]
6901    fn the_null_checks_are_calls_by_the_names_duckdb_prints() {
6902        // The keyword is the name, so the call is written with the canonical spelling of it whichever
6903        // case the query used. What the column is called is the binder's to decide.
6904        assert_eq!(round("SELECT COALESCE(a, b, 1)"), "SELECT coalesce(a, b, 1)");
6905        assert_eq!(round("SELECT coalesce(a)"), "SELECT coalesce(a)");
6906        assert_eq!(round("SELECT NULLIF(a, 1)"), "SELECT nullif(a, 1)");
6907        // `IFNULL` is a plain call that upstream's parser turns into the operator, qualifier and all.
6908        assert_eq!(round("SELECT ifnull(a, 1)"), "SELECT coalesce(a, 1)");
6909        assert_eq!(round("SELECT main.ifnull(a, 1)"), "SELECT coalesce(a, 1)");
6910        let error = parse_ast("SELECT ifnull(a)").expect_err("one argument to ifnull");
6911        assert_eq!(error.message(), "Wrong number of arguments to IFNULL.");
6912        let error = parse_ast("SELECT ifnull(a, b, c)").expect_err("three arguments to ifnull");
6913        assert_eq!(error.message(), "Wrong number of arguments to IFNULL.");
6914    }
6915
6916    /// The four string functions with a grammar rule of their own, written back out as the calls
6917    /// DuckDB's parser writes them as. Per #314.
6918    #[test]
6919    fn the_string_keywords_are_the_calls_duckdb_prints() {
6920        assert_eq!(round("SELECT substring(s, 2, 3)"), "SELECT substring(s, 2, 3)");
6921        assert_eq!(round("SELECT SUBSTRING(s FROM 2 FOR 3)"), "SELECT substring(s, 2, 3)");
6922        assert_eq!(round("SELECT substring(s FROM 2)"), "SELECT substring(s, 2)");
6923        // The `FOR` on its own is three arguments and not two, with the start filled in.
6924        assert_eq!(round("SELECT substring(s FOR 3)"), "SELECT substring(s, 1, 3)");
6925        // The haystack comes first in the call and second in the query.
6926        assert_eq!(round("SELECT position('c' IN s)"), "SELECT position(s, 'c')");
6927        assert_eq!(round("SELECT trim(s)"), "SELECT trim(s)");
6928        assert_eq!(round("SELECT trim(BOTH 'x' FROM s)"), "SELECT trim(s, 'x')");
6929        assert_eq!(round("SELECT trim(BOTH FROM s)"), "SELECT trim(s)");
6930        assert_eq!(round("SELECT trim(s, 'xy')"), "SELECT trim(s, 'xy')");
6931        // A direction is a different function and not a different argument.
6932        assert_eq!(round("SELECT trim(LEADING FROM s)"), "SELECT ltrim(s)");
6933        assert_eq!(round("SELECT trim(TRAILING FROM s)"), "SELECT rtrim(s)");
6934        assert_eq!(round("SELECT trim(LEADING 'x' FROM s)"), "SELECT ltrim(s, 'x')");
6935        assert_eq!(round("SELECT trim(TRAILING 'x' FROM s)"), "SELECT rtrim(s, 'x')");
6936        assert_eq!(
6937            round("SELECT overlay(s PLACING 'X' FROM 2 FOR 1)"),
6938            "SELECT overlay(s, 'X', 2, 1)"
6939        );
6940        assert_eq!(round("SELECT overlay(s PLACING 'X' FROM 2)"), "SELECT overlay(s, 'X', 2)");
6941        assert_eq!(round("SELECT overlay(s, 'X', 2, 1)"), "SELECT overlay(s, 'X', 2, 1)");
6942    }
6943
6944    #[test]
6945    fn an_aggregate_keeps_its_distinct() {
6946        assert_eq!(round("SELECT count(*)"), "SELECT count(*)");
6947        assert_eq!(round("SELECT count(DISTINCT x)"), "SELECT count(DISTINCT x)");
6948        assert_eq!(round("SELECT count(ALL x)"), "SELECT count(x)");
6949        assert_eq!(round("SELECT main.count(x)"), "SELECT main.count(x)");
6950    }
6951
6952    #[test]
6953    fn a_call_keeps_the_filter_it_was_written_with_and_the_word_where_is_optional() {
6954        // `FilterClauseContents <- 'WHERE'? Expression`, so both spellings parse and both land on
6955        // the same predicate. Which names are allowed to carry one is not a question the parser
6956        // can answer, so it keeps one wherever it was written and lets the binder refuse it.
6957        assert_eq!(round("SELECT sum(x) FILTER (WHERE y > 1)"), "SELECT sum(x) FILTER [(y Gt 1)]");
6958        assert_eq!(round("SELECT sum(x) FILTER (y > 1)"), "SELECT sum(x) FILTER [(y Gt 1)]");
6959        assert_eq!(round("SELECT count(*) FILTER (WHERE b)"), "SELECT count(*) FILTER [b]");
6960        assert_eq!(
6961            round("SELECT sum(DISTINCT x) FILTER (WHERE b)"),
6962            "SELECT sum(DISTINCT x) FILTER [b]"
6963        );
6964        assert_eq!(round("SELECT abs(x) FILTER (WHERE b)"), "SELECT abs(x) FILTER [b]");
6965    }
6966
6967    /// The `FILTER` goes before the `OVER`, which is a rule of the grammar and not of the binder.
6968    #[test]
6969    fn a_window_call_carries_its_filter_in_front_of_its_over() {
6970        assert_eq!(
6971            round("SELECT sum(x) FILTER (WHERE b) OVER ()"),
6972            "SELECT sum(x) FILTER [b] OVER [] [] [Range UnboundedPreceding CurrentRow NoOthers]"
6973        );
6974    }
6975
6976    #[test]
6977    fn the_modifiers_hang_off_the_query_and_not_off_the_select() {
6978        // `a UNION b ORDER BY x` sorts the union. Putting the order by on the select would have
6979        // made that unrepresentable, which is why the grammar puts it outside the chain and why
6980        // the AST follows.
6981        assert_eq!(
6982            round("SELECT 1 UNION ALL SELECT 2 ORDER BY 1"),
6983            "(SELECT 1 Union All SELECT 2) ORDER BY 1 Unstated Unstated"
6984        );
6985        assert_eq!(
6986            round("SELECT a FROM t UNION SELECT b FROM u EXCEPT SELECT c FROM v"),
6987            "((SELECT a FROM t Union Unstated SELECT b FROM u) Except Unstated SELECT c FROM v)",
6988            "set operators are left associative"
6989        );
6990        assert_eq!(
6991            round("SELECT 1 UNION SELECT 2 INTERSECT SELECT 3"),
6992            "(SELECT 1 Union Unstated (SELECT 2 Intersect Unstated SELECT 3))",
6993            "and intersect binds tighter than the other two"
6994        );
6995    }
6996
6997    #[test]
6998    fn the_sort_and_limit_clauses_keep_what_was_written() {
6999        assert_eq!(
7000            round("SELECT a FROM t ORDER BY a"),
7001            "SELECT a FROM t ORDER BY a Unstated Unstated"
7002        );
7003        assert_eq!(
7004            round("SELECT a FROM t ORDER BY a DESC NULLS LAST"),
7005            "SELECT a FROM t ORDER BY a Descending Last"
7006        );
7007        assert_eq!(round("SELECT a FROM t ORDER BY ALL"), "SELECT a FROM t ORDER BY ALL");
7008        assert_eq!(round("SELECT a FROM t GROUP BY ALL"), "SELECT a FROM t GROUP BY ALL");
7009        assert_eq!(round("SELECT a FROM t LIMIT 10 OFFSET 5"), "SELECT a FROM t LIMIT 10 OFFSET 5");
7010        assert_eq!(round("SELECT a FROM t OFFSET 5 LIMIT 10"), "SELECT a FROM t LIMIT 10 OFFSET 5");
7011        assert_eq!(round("SELECT a FROM t LIMIT 10%"), "SELECT a FROM t LIMIT 10%");
7012        assert_eq!(round("SELECT a FROM t LIMIT ALL"), "SELECT a FROM t", "which is no limit");
7013    }
7014
7015    #[test]
7016    fn a_subquery_appears_in_both_places_it_can() {
7017        assert_eq!(
7018            round("SELECT * FROM (SELECT x FROM t) AS s"),
7019            "SELECT * FROM (SELECT x FROM t) AS s"
7020        );
7021        assert_eq!(round("SELECT (SELECT 1)"), "SELECT (SELECT 1)");
7022    }
7023
7024    #[test]
7025    fn distinct_on_keeps_its_expressions() {
7026        assert_eq!(round("SELECT DISTINCT a"), "SELECT DISTINCT a");
7027        assert_eq!(round("SELECT ALL a"), "SELECT a", "which is the default written out");
7028        assert_eq!(round("SELECT DISTINCT ON (a, b) a"), "SELECT DISTINCT ON (a, b) a");
7029    }
7030
7031    #[test]
7032    fn an_operator_the_dialect_does_not_name_is_kept_by_name() {
7033        // The grammar text says `OperatorLiteral <- Identifier`, which reads as though any bare
7034        // word could be written infix. It cannot. That rule is one of the 24 the matcher overrides
7035        // and it is overridden to the bare operator matcher, so what it takes is a run of operator
7036        // characters. Believing the body here would have produced a transformer that accepted
7037        // `a foo b`, which DuckDB rejects.
7038        assert_eq!(round("SELECT a <=> b"), "SELECT (a <=> b)");
7039        assert!(parse_ast("SELECT a foo b").is_err(), "a bare word is not an operator");
7040    }
7041
7042    #[test]
7043    fn a_script_is_a_list_of_statements() {
7044        let ast = parse_ast("SELECT 1; SELECT 2;").unwrap();
7045        assert_eq!(ast.statements.len(), 2);
7046        // A trailing semicolon makes an empty top level statement in the parse tree, because the
7047        // grammar's `Statement? (';'+ / EndOfInput)` is happy with nothing on both sides. It is
7048        // dropped here rather than pretended away in the matcher.
7049        let Statement::Query(second) = ast.statements[1] else {
7050            panic!("the second statement is a query");
7051        };
7052        assert_eq!(show_query(&ast, second), "SELECT 2");
7053    }
7054
7055    #[test]
7056    fn an_unsupported_construct_names_itself_and_what_was_written() {
7057        let error = parse_ast("COMMENT ON TABLE t IS 'a note'").unwrap_err().to_string();
7058        assert!(error.starts_with("Not implemented Error"), "{error}");
7059        assert!(error.contains("COMMENT ON TABLE t IS 'a note'"), "{error}");
7060        assert!(error.contains("CommentStatement"), "{error}");
7061    }
7062
7063    #[test]
7064    fn a_long_construct_is_cut_short_in_the_message() {
7065        let query = format!("COMMENT ON TABLE t IS '{}'", "a".repeat(80));
7066        let error = parse_ast(&query).unwrap_err().to_string();
7067        assert!(error.contains("..."), "{error}");
7068        assert!(error.len() < 200, "{error}");
7069    }
7070
7071    #[test]
7072    fn the_transformer_never_panics_on_anything_the_matcher_accepts() {
7073        // The matcher accepts a good deal that means nothing, because the grammar does. Every one
7074        // of these parses and none of them is a statement this milestone covers, and the contract
7075        // is that the answer is an error either way.
7076        for query in [
7077            "SELECT",
7078            "FROM t SELECT",
7079            "SELECT * FROM t WHERE",
7080            "SELECT ()",
7081            "SELECT a FROM t GROUP BY ()",
7082        ] {
7083            let answer = parse_ast(query);
7084            if let Err(error) = answer {
7085                let message = error.to_string();
7086                assert!(
7087                    message.starts_with("Not implemented Error")
7088                        || message.starts_with("Parser Error"),
7089                    "{query} failed with {message}"
7090                );
7091            }
7092        }
7093    }
7094
7095    #[test]
7096    fn a_file_name_in_a_from_clause_is_a_table_name_with_the_quotes_off() {
7097        // Both spellings have to arrive as the same name, because the binder decides whether it is
7098        // a file by looking at the name, and `'hits.parquet'` with the quotes still on it is not
7099        // a path that anything can open.
7100        assert_eq!(round("SELECT * FROM 'hits.parquet'"), "SELECT * FROM hits.parquet");
7101        assert_eq!(round("SELECT * FROM \"hits.parquet\""), "SELECT * FROM hits.parquet");
7102        assert_eq!(round("SELECT * FROM 'hits.parquet' AS h"), "SELECT * FROM hits.parquet AS h");
7103        assert_eq!(
7104            round_with_case("SELECT Mixed FROM 'NoSuch/Mixed/File.csv'", IdentifierCase::Lower),
7105            "SELECT mixed FROM NoSuch/Mixed/File.csv"
7106        );
7107        assert_eq!(
7108            round_with_case("SELECT Mixed FROM \"QuotedTable\"", IdentifierCase::Upper),
7109            "SELECT MIXED FROM QuotedTable"
7110        );
7111    }
7112
7113    #[test]
7114    fn a_function_call_in_a_from_clause_is_a_source_and_not_an_expression() {
7115        assert_eq!(round("SELECT * FROM range(3)"), "SELECT * FROM range(3)");
7116        assert_eq!(round("SELECT * FROM range(1, 10, 2)"), "SELECT * FROM range(1, 10, 2)");
7117        assert_eq!(round("SELECT * FROM main.range(3)"), "SELECT * FROM main.range(3)");
7118        assert_eq!(round("SELECT * FROM range(3) AS t"), "SELECT * FROM range(3) AS t");
7119        // The grammar allows a call with no arguments here and the transformer keeps it, because
7120        // whether a particular function takes none is the binder's question and not this one's.
7121        assert_eq!(round("SELECT * FROM some_function()"), "SELECT * FROM some_function()");
7122        // `LATERAL` is read and dropped, because a FROM entry here already sees the entries written
7123        // to its left and the word asks for nothing more.
7124        assert_eq!(round("SELECT * FROM LATERAL range(3)"), "SELECT * FROM range(3)");
7125        assert_eq!(
7126            round("SELECT * FROM t, LATERAL (SELECT t.x) AS v"),
7127            "SELECT * FROM t, (SELECT t.x) AS v"
7128        );
7129    }
7130
7131    #[test]
7132    fn the_forms_of_a_table_function_this_does_not_cover_are_turned_away_by_name() {
7133        for query in ["SELECT * FROM range(3) WITH ORDINALITY", "SELECT * FROM t: range(3)"] {
7134            let error = parse_ast(query).unwrap_err().to_string();
7135            assert!(error.contains("grammar rule"), "{query} failed with {error}");
7136        }
7137    }
7138
7139    #[test]
7140    fn a_pragma_is_the_call_it_stands_for_by_the_time_it_leaves_here() {
7141        assert_eq!(round("PRAGMA version"), "SELECT * FROM pragma_version()");
7142        assert_eq!(round("PRAGMA database_size"), "SELECT * FROM pragma_database_size()");
7143        // The case the user wrote survives, because the name goes back out in the message about a
7144        // pragma that does not exist and the pin prints it back as it was typed.
7145        assert_eq!(round("PRAGMA VERSION"), "SELECT * FROM pragma_VERSION()");
7146        assert_eq!(round("PRAGMA table_info('t')"), "SELECT * FROM pragma_table_info('t')");
7147    }
7148
7149    #[test]
7150    fn a_pragma_that_is_a_statement_stays_one_rather_than_becoming_a_call() {
7151        // These write a setting and return no rows, so there is nothing to select from. The name
7152        // carries the value as well, and which name means what is decided a layer up.
7153        assert_eq!(round_statement("PRAGMA disable_optimizer"), "PRAGMA disable_optimizer");
7154        assert_eq!(round_statement("PRAGMA enable_profiling"), "PRAGMA enable_profiling");
7155        assert_eq!(round_statement("PRAGMA force_checkpoint"), "PRAGMA force_checkpoint");
7156        assert_eq!(round_statement("PRAGMA verify_parallelism"), "PRAGMA verify_parallelism");
7157        // A name of the same shape that no engine has gets here too, and the catalog is what turns
7158        // it down, so that the sentence about it is the one the catalog says about any pragma.
7159        assert_eq!(round_statement("PRAGMA enable_nothing_at_all"), "PRAGMA enable_nothing_at_all");
7160        // With parentheses it is a call again, because a pragma that takes an argument returns rows.
7161        assert_eq!(
7162            round("PRAGMA disable_optimizer('x')"),
7163            "SELECT * FROM pragma_disable_optimizer('x')"
7164        );
7165    }
7166
7167    #[test]
7168    fn a_bare_name_in_a_pragmas_parentheses_is_a_name_and_not_a_column() {
7169        // There is no FROM clause here for a column to come out of, so both spellings have to
7170        // arrive as the same string, and a qualified one has to arrive as one string and not two.
7171        assert_eq!(round("PRAGMA table_info(t)"), "SELECT * FROM pragma_table_info('t')");
7172        assert_eq!(round("PRAGMA table_info(main.t)"), "SELECT * FROM pragma_table_info('main.t')");
7173        assert_eq!(round("PRAGMA table_info(\"T\")"), "SELECT * FROM pragma_table_info('T')");
7174        // Anything that is not a name is left alone, so the binder is the one that says there is
7175        // no overload taking an integer rather than a table called 1 being looked for.
7176        assert_eq!(round("PRAGMA table_info(1)"), "SELECT * FROM pragma_table_info(1)");
7177    }
7178
7179    #[test]
7180    fn a_pragma_with_an_equals_sign_is_a_set_and_nothing_else() {
7181        assert_eq!(round_statement("PRAGMA memory_limit = '1GB'"), "SET memory_limit = '1GB'");
7182        assert_eq!(round_statement("PRAGMA threads = 4"), "SET threads = 4");
7183    }
7184
7185    #[test]
7186    fn a_pragma_with_empty_parentheses_does_not_parse_on_either_engine() {
7187        // The rule is `PragmaParameters <- Parens(List(Expression))` and a list of no expressions
7188        // does not match, which is where the pin's parser error comes from as well.
7189        let error = parse_ast("PRAGMA version()").unwrap_err().to_string();
7190        assert!(error.contains("syntax error at or near \")\""), "{error}");
7191    }
7192
7193    #[test]
7194    fn a_window_call_carries_its_partition_its_order_and_its_frame() {
7195        assert_eq!(
7196            round("SELECT row_number() OVER () FROM t"),
7197            "SELECT row_number() OVER [] [] [Range UnboundedPreceding CurrentRow NoOthers] FROM t"
7198        );
7199        assert_eq!(
7200            round("SELECT sum(a) OVER (PARTITION BY b, c ORDER BY d DESC NULLS FIRST) FROM t"),
7201            "SELECT sum(a) OVER [b, c] [d Descending First] \
7202             [Range UnboundedPreceding CurrentRow NoOthers] FROM t"
7203        );
7204        assert_eq!(
7205            round(
7206                "SELECT sum(a) OVER (ORDER BY b GROUPS BETWEEN 1 PRECEDING AND 2 FOLLOWING EXCLUDE TIES) FROM t"
7207            ),
7208            "SELECT sum(a) OVER [] [b Unstated Unstated] \
7209             [Groups Preceding(1) Following(2) Ties] FROM t"
7210        );
7211    }
7212
7213    /// A frame over the whole partition is the same frame however it was measured, so the three
7214    /// units collapse to one here rather than three ways of saying it reaching the binder.
7215    #[test]
7216    fn a_frame_with_both_ends_unbounded_is_counted_in_rows() {
7217        for unit in ["ROWS", "RANGE", "GROUPS"] {
7218            let query = format!(
7219                "SELECT sum(a) OVER (ORDER BY b {unit} BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t"
7220            );
7221            assert_eq!(
7222                round(&query),
7223                "SELECT sum(a) OVER [] [b Unstated Unstated] \
7224                 [Rows UnboundedPreceding UnboundedFollowing NoOthers] FROM t"
7225            );
7226        }
7227    }
7228
7229    /// A single bound names the start and the end is the current row, which is the standard's rule
7230    /// and is why the two spellings below have to arrive as the same frame.
7231    #[test]
7232    fn a_frame_written_with_one_bound_ends_at_the_current_row() {
7233        assert_eq!(
7234            round("SELECT sum(a) OVER (ORDER BY b ROWS UNBOUNDED PRECEDING) FROM t"),
7235            round(
7236                "SELECT sum(a) OVER (ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t"
7237            )
7238        );
7239    }
7240
7241    #[test]
7242    fn a_named_window_is_resolved_here_and_not_carried_any_further() {
7243        let inlined = round("SELECT sum(a) OVER (PARTITION BY b ORDER BY c) FROM t");
7244        assert_eq!(
7245            round("SELECT sum(a) OVER w FROM t WINDOW w AS (PARTITION BY b ORDER BY c)"),
7246            inlined
7247        );
7248        assert_eq!(
7249            round("SELECT sum(a) OVER (w) FROM t WINDOW w AS (PARTITION BY b ORDER BY c)"),
7250            inlined
7251        );
7252        // A definition can build on one written before it, and a copy can add the half the base
7253        // did not say.
7254        assert_eq!(
7255            round("SELECT sum(a) OVER v FROM t WINDOW w AS (PARTITION BY b), v AS (w ORDER BY c)"),
7256            inlined
7257        );
7258        assert_eq!(
7259            round("SELECT sum(a) OVER (w ORDER BY c) FROM t WINDOW w AS (PARTITION BY b)"),
7260            inlined
7261        );
7262        // The name is matched without regard to case, the way every other name here is.
7263        assert_eq!(
7264            round("SELECT sum(a) OVER W FROM t WINDOW w AS (PARTITION BY b ORDER BY c)"),
7265            inlined
7266        );
7267    }
7268
7269    /// A window clause is visible to the whole block it was written on, including a subquery
7270    /// inside it, which was measured on the pin.
7271    #[test]
7272    fn a_named_window_reaches_a_subquery_written_in_the_same_block() {
7273        let ast = parse_ast("SELECT (SELECT sum(b) OVER w FROM u) FROM t WINDOW w AS (ORDER BY b)");
7274        assert!(ast.is_ok(), "{:?}", ast.err());
7275        // And no further than that: the next statement in the script starts with none of them.
7276        let error =
7277            parse_ast("SELECT 1 FROM t WINDOW w AS (ORDER BY b); SELECT sum(a) OVER w FROM u;")
7278                .unwrap_err()
7279                .to_string();
7280        assert!(error.contains("window \"\"w\"\" does not exist"), "{error}");
7281    }
7282
7283    /// All four are the pin's sentences, in the pin's words, including the doubled quotes in the
7284    /// first one.
7285    #[test]
7286    fn the_four_complaints_about_a_named_window_are_upstreams() {
7287        let cases = [
7288            ("SELECT sum(a) OVER w FROM t", "window \"\"w\"\" does not exist"),
7289            (
7290                "SELECT sum(a) OVER (w PARTITION BY b) FROM t WINDOW w AS (PARTITION BY b)",
7291                "Cannot override PARTITION BY clause of window \"w\"",
7292            ),
7293            (
7294                "SELECT sum(a) OVER (w ORDER BY b) FROM t WINDOW w AS (ORDER BY b)",
7295                "Cannot override ORDER BY clause of window \"w\"",
7296            ),
7297            (
7298                "SELECT sum(a) OVER (w ROWS UNBOUNDED PRECEDING) FROM t WINDOW w AS (ORDER BY b ROWS UNBOUNDED PRECEDING)",
7299                "cannot copy window \"w\" because it has a frame clause",
7300            ),
7301        ];
7302        for (query, expected) in cases {
7303            let error = parse_ast(query).expect_err(query).to_string();
7304            assert!(error.contains(expected), "{query}: {error}");
7305        }
7306    }
7307
7308    /// `IGNORE NULLS` is a window modifier, so a call without an `OVER` still has nowhere to put
7309    /// it, and `EXCLUDE` needs a framing keyword in front of it on both engines.
7310    #[test]
7311    fn the_modifiers_that_only_a_window_takes_are_turned_down_without_one() {
7312        let error = parse_ast("SELECT first_value(a IGNORE NULLS) FROM t").unwrap_err().to_string();
7313        assert!(
7314            error.contains("RESPECT/IGNORE NULLS is not supported for non-window functions"),
7315            "{error}"
7316        );
7317        let error = parse_ast("SELECT sum(a) OVER (ORDER BY b EXCLUDE TIES) FROM t")
7318            .unwrap_err()
7319            .to_string();
7320        assert!(error.contains("syntax error at or near \"EXCLUDE\""), "{error}");
7321    }
7322
7323    /// A call with an `OVER` on it skips the rewrites an ordinary call goes through, which is
7324    /// visible on the one name that has a rewrite and an arity check of its own.
7325    #[test]
7326    fn a_window_call_is_not_put_through_the_rewrites_a_plain_call_is() {
7327        assert_eq!(
7328            round("SELECT ifnull(1) OVER () FROM t"),
7329            "SELECT ifnull(1) OVER [] [] [Range UnboundedPreceding CurrentRow NoOthers] FROM t"
7330        );
7331        let error = parse_ast("SELECT ifnull(1) FROM t").unwrap_err().to_string();
7332        assert!(error.contains("Wrong number of arguments to IFNULL."), "{error}");
7333    }
7334
7335    #[test]
7336    fn interning_means_a_name_written_twice_is_stored_once() {
7337        let ast = parse_ast("SELECT a, a, a FROM t WHERE a = a").unwrap();
7338        assert_eq!(ast.strings.iter().filter(|text| *text == "a").count(), 1);
7339    }
7340}