Skip to main content

rudb_parse/
transform.rs

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