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                "SubstringExpression" => return self.substring(node),
1993                "PositionExpression" => return self.position(node),
1994                "TrimExpression" => return self.trim(node),
1995                "OverlayExpression" => return self.overlay(node),
1996                "ExtractExpression" => return self.extract(node),
1997                "CastExpression" => return self.cast(node),
1998                "TypeLiteral" => return self.typed_literal(node),
1999                "IntervalLiteral" => return self.interval_literal(node),
2000                "CaseExpression" => return self.case(node),
2001                "ParenthesisExpression" => return self.row(node),
2002                // `ParensExpression <- Parens(Expression)` covers more text than its child and
2003                // still says nothing about the value, because the brackets are grouping. It is the
2004                // one rule of that shape, which is why it is an arm rather than a second rule in
2005                // the step below. `ParenthesisExpression` is not this: it holds a list, and a list
2006                // of more than one is a row.
2007                "ParensExpression" if count == 1 => node = self.first(node),
2008                "BoundedListExpression" => return self.list(node),
2009                "QuestionMarkNumberedParameter"
2010                | "AnonymousParameter"
2011                | "NumberedParameter"
2012                | "ColLabelParameter" => return self.parameter(node),
2013                "SubqueryExpression" => return self.subquery(node),
2014                _ if count == 1 && self.text(self.first(node)) == self.text(node) => {
2015                    node = self.first(node);
2016                }
2017                _ => return self.unsupported(node),
2018            }
2019        }
2020    }
2021
2022    /// `X <- Y XTail*` where `XTail <- Operator Y`, the shape ten precedence levels share.
2023    fn tail_chain(&mut self, node: u32) -> Result<ExprRef> {
2024        let mut kids = self.kids(node);
2025        let head = kids.next().unwrap_or(NONE);
2026        let mut left = self.expr(head)?;
2027        for tail in kids {
2028            let operator = self.first(tail);
2029            // `ComparisonExpressionTail <- ComparisonOperator NotExpression? BetweenInLikeExpression`
2030            // is the one tail with an optional middle, so the operand is the last child and not the
2031            // second one. Taking the last is right for every tail and wrong for none.
2032            let operand = self.kids(tail).last().unwrap_or(NONE);
2033            if self.count(tail) > 2 {
2034                return self.unsupported(tail);
2035            }
2036            if self.contains(operator, "AnyAllParsedOperator") {
2037                let any_op = self.descendant(operator, "AnyOp");
2038                let op = self.binary_op(any_op)?;
2039                let reference = self.descendant(operand, "SubqueryReference");
2040                if reference == NONE {
2041                    return self.unsupported(operand);
2042                }
2043                let query = self.query(self.first(reference))?;
2044                let all = self.contains(operator, "SubqueryAll");
2045                left = self.push(Expr::QuantifiedSubquery { operand: left, op, query, all });
2046                continue;
2047            }
2048            let op = self.binary_op(operator)?;
2049            let right = self.expr(operand)?;
2050            left = self.push(Expr::Binary { op, left, right });
2051        }
2052        Ok(left)
2053    }
2054
2055    /// Which infix operator a tail's operator node is.
2056    fn binary_op(&mut self, node: u32) -> Result<BinaryOp> {
2057        // The operator rules nest: `ComparisonOperator` over `OperatorGreaterThan` over the symbol
2058        // itself. Every one of them covers the same tokens, so the text is the same at every level
2059        // and reading it once at the top is enough. The name is not, which is why the bottom of the
2060        // chain is walked to as well: `OtherOperator` says nothing and `OperatorLiteral` says
2061        // everything, and they are three levels apart.
2062        let mut leaf = node;
2063        while self.count(leaf) == 1 {
2064            leaf = self.first(leaf);
2065        }
2066        let text = self.text(node);
2067        let upper = text.to_ascii_uppercase();
2068        let op = match upper.as_str() {
2069            "OR" => BinaryOp::Or,
2070            "AND" => BinaryOp::And,
2071            "=" | "==" => BinaryOp::Eq,
2072            "!=" | "<>" => BinaryOp::NotEq,
2073            "<" => BinaryOp::Lt,
2074            ">" => BinaryOp::Gt,
2075            "<=" => BinaryOp::LtEq,
2076            ">=" => BinaryOp::GtEq,
2077            "+" => BinaryOp::Add,
2078            "-" => BinaryOp::Subtract,
2079            "*" => BinaryOp::Multiply,
2080            "/" => BinaryOp::Divide,
2081            "//" => BinaryOp::IntegerDivide,
2082            "%" => BinaryOp::Modulo,
2083            "^" | "**" => BinaryOp::Power,
2084            "&" => BinaryOp::BitAnd,
2085            "|" => BinaryOp::BitOr,
2086            "<<" => BinaryOp::ShiftLeft,
2087            ">>" => BinaryOp::ShiftRight,
2088            "||" => BinaryOp::Concat,
2089            "COLLATE" => BinaryOp::Collate,
2090            "->" => BinaryOp::Arrow,
2091            "->>" => BinaryOp::LongArrow,
2092            "@>" => BinaryOp::Contains,
2093            "<@" => BinaryOp::ContainedBy,
2094            "&&" => BinaryOp::Overlaps,
2095            "^@" => BinaryOp::StartsWith,
2096            "<<=" => BinaryOp::InetContainedByOrEq,
2097            ">>=" => BinaryOp::InetContainsOrEq,
2098            _ if self.name(leaf) == "AtTimeZoneOperator" => BinaryOp::AtTimeZone,
2099            // `IsDistinctFromOp <- 'IS' 'NOT'? 'DISTINCT' 'FROM'`, told apart by the middle word,
2100            // which is not in the tree because keywords are terminals.
2101            _ if self.name(leaf) == "IsDistinctFromOp" => {
2102                if upper.split_whitespace().any(|word| word == "NOT") {
2103                    BinaryOp::IsNotDistinctFrom
2104                } else {
2105                    BinaryOp::IsDistinctFrom
2106                }
2107            }
2108            // `OperatorLiteral` is the open end of the operator set. Its body in the grammar text
2109            // says `Identifier`, but it is one of the 24 rules whose body the matcher does not
2110            // walk and the matcher it is overridden to is the bare operator one, so what it
2111            // actually accepts is any run of operator characters that is not already a token.
2112            // `a <=> b` is such a run, DuckDB resolves it as a two argument function of that name,
2113            // and rejecting it here would reject SQL DuckDB accepts.
2114            _ if self.name(leaf) == "OperatorLiteral" => {
2115                let interned = self.intern(text);
2116                BinaryOp::Named(interned)
2117            }
2118            _ => return self.unsupported(node),
2119        };
2120        Ok(op)
2121    }
2122
2123    /// `LogicalOrExpression <- LogicalAndExpression LogicalOrExpressionTail*`, and the `AND` twin.
2124    ///
2125    /// Separate from the other tails because the tail here is `'OR' LogicalAndExpression` with the
2126    /// keyword as a terminal, so there is no operator node to read and the operator is the rule.
2127    fn logical(&mut self, node: u32, op: BinaryOp) -> Result<ExprRef> {
2128        let mut kids = self.kids(node);
2129        let head = kids.next().unwrap_or(NONE);
2130        let mut left = self.expr(head)?;
2131        for tail in kids {
2132            let right = self.expr(self.first(tail))?;
2133            left = self.push(Expr::Binary { op, left, right });
2134        }
2135        Ok(left)
2136    }
2137
2138    /// `LogicalNotExpression <- NotExpression? IsExpression`, where `NotExpression <- NotKeyword+`.
2139    ///
2140    /// The plus matters. `NOT NOT x` is two nodes in the parse tree and two negations in the AST,
2141    /// and folding them here would be an optimizer decision taken in the parser.
2142    fn logical_not(&mut self, node: u32) -> Result<ExprRef> {
2143        let negations = self.count(self.first(node));
2144        let mut expr = self.expr(self.nth(node, 1))?;
2145        for _ in 0..negations {
2146            expr = self.push(Expr::Unary { op: UnaryOp::Not, operand: expr });
2147        }
2148        Ok(expr)
2149    }
2150
2151    /// `IsExpression <- IsDistinctFromExpression IsTest*`, the postfix null and boolean tests.
2152    fn is_expression(&mut self, node: u32) -> Result<ExprRef> {
2153        let mut kids = self.kids(node);
2154        let head = kids.next().unwrap_or(NONE);
2155        let mut expr = self.expr(head)?;
2156        for test in kids {
2157            let inner = self.first(test);
2158            let negated = self.text(inner).to_ascii_uppercase().contains("NOT");
2159            let op = match self.name(inner) {
2160                "NotNull" => UnaryOp::IsNotNull,
2161                "IsNull" => UnaryOp::IsNull,
2162                // `IsLiteral <- 'IS' 'NOT'? IsLiteralValue`, and the value rule is one more level
2163                // down again because it is a choice of four and not four alternatives inlined.
2164                "IsLiteral" => match self.name(self.first(self.first(inner))) {
2165                    "NullLiteral" if negated => UnaryOp::IsNotNull,
2166                    "NullLiteral" => UnaryOp::IsNull,
2167                    "TrueLiteral" if negated => UnaryOp::IsNotTrue,
2168                    "TrueLiteral" => UnaryOp::IsTrue,
2169                    "FalseLiteral" if negated => UnaryOp::IsNotFalse,
2170                    "FalseLiteral" => UnaryOp::IsFalse,
2171                    "UnknownLiteral" if negated => UnaryOp::IsNotUnknown,
2172                    "UnknownLiteral" => UnaryOp::IsUnknown,
2173                    _ => return self.unsupported(inner),
2174                },
2175                _ => return self.unsupported(inner),
2176            };
2177            expr = self.push(Expr::Unary { op, operand: expr });
2178        }
2179        Ok(expr)
2180    }
2181
2182    /// `BetweenInLikeExpression <- OtherOperatorExpression BetweenInLikeOp?`.
2183    fn between_in_like(&mut self, node: u32) -> Result<ExprRef> {
2184        let operand = self.expr(self.first(node))?;
2185        // `BetweenInLikeOp <- 'NOT'? BetweenInLikeOpExpression`. The `NOT` is a terminal, so what
2186        // says it was written is that the op node covers a token the inner node does not.
2187        let op = self.nth(node, 1);
2188        let negated = self.text(op).to_ascii_uppercase().starts_with("NOT");
2189        let inner = self.first(self.first(op));
2190        match self.name(inner) {
2191            // `BetweenClause <- 'BETWEEN' x 'AND' y`.
2192            "BetweenClause" => {
2193                let low = self.expr(self.first(inner))?;
2194                let high = self.expr(self.nth(inner, 1))?;
2195                Ok(self.push(Expr::Between { operand, low, high, negated }))
2196            }
2197            // `InClause <- 'IN' InExpression`.
2198            "InClause" => {
2199                let expression = self.first(self.first(inner));
2200                match self.name(expression) {
2201                    "InExpressionList" => {
2202                        let mut items = Vec::new();
2203                        for kid in self.kids(expression) {
2204                            items.push(self.expr(kid)?);
2205                        }
2206                        let list = self.expr_slice(items);
2207                        Ok(self.push(Expr::In { operand, list, negated }))
2208                    }
2209                    "InSelectStatement" => {
2210                        let query = self.query(self.first(expression))?;
2211                        Ok(self.push(Expr::InSubquery { operand, query, negated }))
2212                    }
2213                    _ => self.unsupported(expression),
2214                }
2215            }
2216            // `LikeClause <- LikeVariations x EscapeClause?`.
2217            "LikeClause" => {
2218                if self.find(inner, "EscapeClause") != NONE {
2219                    return self.unsupported(inner);
2220                }
2221                let variation = self.name(self.first(self.first(inner)));
2222                let op = match (variation, negated) {
2223                    ("LikeToken", false) | ("NotLikeOp", true) => BinaryOp::Like,
2224                    ("LikeToken", true) | ("NotLikeOp", false) => BinaryOp::NotLike,
2225                    ("ILikeToken", false) | ("NotILikeOp", true) => BinaryOp::ILike,
2226                    ("ILikeToken", true) | ("NotILikeOp", false) => BinaryOp::NotILike,
2227                    // Glob and the bare regex match have no negated spelling of their own in
2228                    // `LikeVariations`, so a `NOT` in front of either stays an explicit negation.
2229                    ("GlobToken", _) => BinaryOp::Glob,
2230                    ("RegexMatchToken", _) => BinaryOp::Regex,
2231                    ("SimilarToToken", false) => BinaryOp::SimilarTo,
2232                    ("SimilarToToken", true) => BinaryOp::NotSimilarTo,
2233                    ("NotSimilarToOp", false) => BinaryOp::NotRegex,
2234                    ("NotSimilarToOp", true) => BinaryOp::Regex,
2235                    ("RegexInsensitiveMatchToken", false)
2236                    | ("NotRegexInsensitiveMatchOp", true) => BinaryOp::RegexInsensitive,
2237                    ("RegexInsensitiveMatchToken", true)
2238                    | ("NotRegexInsensitiveMatchOp", false) => BinaryOp::NotRegexInsensitive,
2239                    _ => return self.unsupported(inner),
2240                };
2241                let right = self.expr(self.nth(inner, 1))?;
2242                let expr = self.push(Expr::Binary { op, left: operand, right });
2243                // The like family folds its negation into the operator because it has a spelling
2244                // for the negated form. Glob and regex do not, so theirs stays where it was.
2245                if negated && matches!(op, BinaryOp::Glob | BinaryOp::Regex) {
2246                    return Ok(self.push(Expr::Unary { op: UnaryOp::Not, operand: expr }));
2247                }
2248                Ok(expr)
2249            }
2250            _ => self.unsupported(inner),
2251        }
2252    }
2253
2254    /// `PrefixExpression <- PrefixOperator* BaseExpression`, applied right to left.
2255    fn prefix(&mut self, node: u32) -> Result<ExprRef> {
2256        let kids: Vec<u32> = self.kids(node).collect();
2257        let mut expr = self.expr(kids[kids.len() - 1])?;
2258        for &operator in kids[..kids.len() - 1].iter().rev() {
2259            let op = match self.name(self.first(operator)) {
2260                "MinusPrefixOperator" => UnaryOp::Negate,
2261                "PlusPrefixOperator" => UnaryOp::Plus,
2262                "TildePrefixOperator" => UnaryOp::BitNot,
2263                _ => return self.unsupported(operator),
2264            };
2265            expr = self.push(Expr::Unary { op, operand: expr });
2266        }
2267        Ok(expr)
2268    }
2269
2270    /// `BaseExpression <- SingleExpression IndirectionList?`, the postfix chain.
2271    fn indirection(&mut self, node: u32) -> Result<ExprRef> {
2272        let mut expr = self.expr(self.first(node))?;
2273        for step in self.kids(self.nth(node, 1)) {
2274            let inner = self.first(step);
2275            expr = match self.name(inner) {
2276                // `CastOperator <- '::' Type`.
2277                "CastOperator" => {
2278                    let text = self.text(self.first(inner)).to_string();
2279                    let ty = self.intern(&text);
2280                    self.push(Expr::Cast { operand: expr, ty, try_cast: false })
2281                }
2282                "DotOperator" => {
2283                    let dot = self.first(inner);
2284                    match self.name(dot) {
2285                        // `DotColumnOperator <- '.' ColLabel`, which DuckDB resolves as a call of
2286                        // `struct_extract`. Writing it as that call rather than as its own node
2287                        // keeps the binder from needing a rule for a thing that is already a
2288                        // function.
2289                        "DotColumnOperator" => {
2290                            let field = self.identifier(self.first(dot));
2291                            let text = self.ast.string(field).to_string();
2292                            let literal = self.intern(&text);
2293                            let key = self
2294                                .push(Expr::Literal { kind: LiteralKind::String, text: literal });
2295                            let name = self.function_name("struct_extract");
2296                            let args = self.expr_slice(vec![expr, key]);
2297                            self.push(Expr::Function { name, args, distinct: false, filter: NONE })
2298                        }
2299                        // `DotMethodOperator <- '.' MethodExpression`, where `x.f(a)` is `f(x, a)`.
2300                        "DotMethodOperator" => {
2301                            let method = self.first(dot);
2302                            let text = self.text(self.first(method)).to_string();
2303                            let text = unquote(&text);
2304                            let name = self.function_name(&text);
2305                            let mut args = vec![expr];
2306                            let list = self.find(method, "MethodExpressionArguments");
2307                            if list != NONE {
2308                                let inner = self.first(list);
2309                                let arguments = self.find(inner, "MethodFunctionArguments");
2310                                if arguments != NONE {
2311                                    for kid in self.kids(arguments) {
2312                                        args.push(self.argument(kid)?);
2313                                    }
2314                                }
2315                            }
2316                            let args = self.expr_slice(args);
2317                            self.push(Expr::Function { name, args, distinct: false, filter: NONE })
2318                        }
2319                        _ => return self.unsupported(dot),
2320                    }
2321                }
2322                // `SliceExpression <- '[' SliceBound ']'` over
2323                // `SliceBound <- Expression? EndSliceBound? StepSliceBound?`, so a subscript is one
2324                // index when neither colon is there and a range when either of them is. Both become
2325                // a call, the same two calls DuckDB's own transformer writes.
2326                "SliceExpression" => self.subscript(inner, expr)?,
2327                // `PostfixOperator <- '!'`.
2328                "PostfixOperator" => {
2329                    self.push(Expr::Unary { op: UnaryOp::Factorial, operand: expr })
2330                }
2331                _ => return self.unsupported(inner),
2332            };
2333        }
2334        Ok(expr)
2335    }
2336
2337    /// `SliceExpression <- '[' SliceBound ']'`, which is `array_extract` or `array_slice`.
2338    ///
2339    /// The three parts of the bound are all optional and any of the eight combinations parses, so
2340    /// which call this is comes from which parts are there rather than from how many children the
2341    /// bound has. One expression and no colon is an index. Anything with a colon in it is a range,
2342    /// and a range the query did not write both ends of gets the ends DuckDB's transformer gives it:
2343    /// a missing begin is 1 and a missing end is -1, which is the last element, so `x[:]` is the
2344    /// whole of `x` and `array_slice(x, 1, -1)` answers the same thing.
2345    ///
2346    /// `EndSliceMinus` is the `-` in `x[1:-]`, which upstream reads as a range with no end rather
2347    /// than as a subtraction of nothing, and it answers `x[1:]`. So it is the missing end too.
2348    ///
2349    /// The step is the odd one. `x[1:2:]` is a step that is written and empty, and what upstream
2350    /// does with it is pass a list where the step goes, which then fails to bind because the fourth
2351    /// parameter is a BIGINT. The empty list here is that, measured off the pinned binary: it says
2352    /// `array_slice(INTEGER[], INTEGER_LITERAL, INTEGER_LITERAL, INTEGER[])` has no match, and the
2353    /// fourth type in that sentence is the list. Writing a 1 there instead would answer a row where
2354    /// the reference refuses.
2355    fn subscript(&mut self, node: u32, target: ExprRef) -> Result<ExprRef> {
2356        let bound = self.first(node);
2357        let (mut begin, mut end, mut step) = (NONE, NONE, NONE);
2358        for kid in self.kids(bound) {
2359            match self.name(kid) {
2360                "EndSliceBound" => end = kid,
2361                "StepSliceBound" => step = kid,
2362                _ => begin = kid,
2363            }
2364        }
2365        if end == NONE && step == NONE {
2366            if begin == NONE {
2367                return Err(Error::parser("Empty subscript '[]' is not allowed"));
2368            }
2369            let index = self.expr(begin)?;
2370            let name = self.function_name("array_extract");
2371            let args = self.expr_slice(vec![target, index]);
2372            return Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }));
2373        }
2374        let first = if begin == NONE { self.literal_number("1") } else { self.expr(begin)? };
2375        // `EndSliceBound <- ':' EndSliceValue?` and `EndSliceValue <- Expression / EndSliceMinus`,
2376        // so the end is written only when the value is there and is not the lone hyphen.
2377        let value = if end == NONE { NONE } else { self.find(end, "EndSliceValue") };
2378        let written = if value == NONE { NONE } else { self.first(value) };
2379        let last = if written == NONE || self.name(written) == "EndSliceMinus" {
2380            self.literal_number("-1")
2381        } else {
2382            self.expr(written)?
2383        };
2384        let mut args = vec![target, first, last];
2385        if step != NONE {
2386            let by = self.first(step);
2387            args.push(if by == NONE {
2388                self.push(Expr::List { items: Slice::default() })
2389            } else {
2390                self.expr(by)?
2391            });
2392        }
2393        let name = self.function_name("array_slice");
2394        let args = self.expr_slice(args);
2395        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
2396    }
2397
2398    /// A number literal the transformer writes rather than reads, for a bound a range left out.
2399    fn literal_number(&mut self, digits: &str) -> ExprRef {
2400        let text = self.intern(digits);
2401        self.push(Expr::Literal { kind: LiteralKind::Number, text })
2402    }
2403
2404    /// A one part function name, for the calls the transformer invents rather than reads.
2405    fn function_name(&mut self, name: &str) -> Slice {
2406        let interned = self.intern(name);
2407        self.part_slice(vec![interned])
2408    }
2409
2410    /// `StarExpression <- StarQualifierList? '*' ExcludeList? ReplaceList? RenameList?`.
2411    fn star(&mut self, node: u32) -> Result<ExprRef> {
2412        for name in ["ExcludeList", "RenameList"] {
2413            let list = self.find(node, name);
2414            if list != NONE {
2415                return self.unsupported(list);
2416            }
2417        }
2418        let replace = self.find(node, "ReplaceList");
2419        let replacements =
2420            if replace == NONE { Slice::default() } else { self.replacements(replace)? };
2421        let qualifier = self.find(node, "StarQualifierList");
2422        let qualifier =
2423            if qualifier == NONE { Slice::default() } else { self.name_parts(qualifier) };
2424        Ok(self.push(Expr::Star { qualifier, replacements }))
2425    }
2426
2427    /// `ReplaceList <- 'REPLACE' ReplaceEntries`, where an entry is `Expression 'AS'
2428    /// ColumnReference` and the entries are one bare entry or a parenthesized list of them.
2429    ///
2430    /// The duplicate check is here rather than in the binder because that is where DuckDB does it:
2431    /// naming the same column twice is a Parser Error there, and it is one of the few things about
2432    /// a star that can be decided without knowing what the star stands for.
2433    fn replacements(&mut self, node: u32) -> Result<Slice> {
2434        // `ReplaceEntries <- ReplaceEntrySingle / ReplaceEntryList` and both of those hold the
2435        // entries as their own children, so the same walk reads either shape.
2436        let entries = self.first(self.first(node));
2437        let listed: Vec<u32> =
2438            self.kids(entries).filter(|&kid| self.name(kid) == "ReplaceEntry").collect();
2439        let mut replacements = Vec::with_capacity(listed.len());
2440        for entry in listed {
2441            let expr = self.expr(self.first(entry))?;
2442            let alias = self.identifier(self.nth(entry, 1));
2443            let written = self.ast.string(alias).to_string();
2444            if replacements
2445                .iter()
2446                .any(|held: &Target| self.ast.string(held.alias).eq_ignore_ascii_case(&written))
2447            {
2448                return Err(Error::parser(format!(
2449                    "Duplicate entry \"{written}\" in REPLACE list"
2450                )));
2451            }
2452            replacements.push(Target { expr, alias });
2453        }
2454        Ok(self.target_slice(replacements))
2455    }
2456
2457    /// `FunctionExpression <- FunctionIdentifier FunctionExpressionArguments WithinGroupClause?
2458    /// FilterClause? ExportClause? OverClause?`.
2459    fn function(&mut self, node: u32) -> Result<ExprRef> {
2460        for name in ["WithinGroupClause", "ExportClause"] {
2461            let clause = self.find(node, name);
2462            if clause != NONE {
2463                return self.unsupported(clause);
2464            }
2465        }
2466        // `FilterClauseContents <- 'WHERE'? Expression`, so the word is optional and the predicate
2467        // is the last thing under it either way. Whether the call is allowed to carry one at all is
2468        // the binder's question, because it is a question about what the name resolves to.
2469        let clause = self.find(node, "FilterClause");
2470        let written =
2471            if clause == NONE { NONE } else { self.descendant(clause, "FilterClauseContents") };
2472        let filter = if written == NONE {
2473            NONE
2474        } else {
2475            let predicate = self.kids(written).last().unwrap_or(NONE);
2476            self.expr(predicate)?
2477        };
2478        let over = self.find(node, "OverClause");
2479        let name = self.name_parts(self.first(node));
2480        // `FunctionExpressionArguments <- Parens(FunctionExpressionArgumentList)` and
2481        // `FunctionExpressionArgumentList <- DistinctOrAll? FunctionArgumentList? OrderByClause?
2482        // IgnoreOrRespectNulls?`, so a call with no arguments still has both wrappers.
2483        let list = self.first(self.nth(node, 1));
2484        // An `ORDER BY` written inside the brackets is the order the call reads its rows in, which
2485        // is a different thing from the `ORDER BY` in an `OVER` and is written in a different place.
2486        // A call without an `OVER` is an aggregate and this is the ordered aggregate form, which is
2487        // still a gap, so the clause is only kept for a window call and the rest say so. Per #1203.
2488        let inside = self.find(list, "OrderByClause");
2489        if inside != NONE && over == NONE {
2490            return self.unsupported(inside);
2491        }
2492        let inner = if inside == NONE {
2493            Slice { start: 0, len: 0 }
2494        } else {
2495            // `ORDER BY ALL` names the call's own arguments rather than a list of keys, and what the
2496            // reference binary does with it in here is not the ordinary reading of the words, so it
2497            // is turned down rather than guessed at.
2498            let (items, all) = self.order_by(inside)?;
2499            if all {
2500                return self.unsupported(inside);
2501            }
2502            self.order_slice(items)
2503        };
2504        // Either word is a window modifier and nothing else carries one, so an ordinary call that
2505        // writes one is turned down here, in the sentence the pin turns it down with.
2506        let nulls = self.find(list, "IgnoreOrRespectNulls");
2507        if nulls != NONE && over == NONE {
2508            return Err(Error::parser(
2509                "RESPECT/IGNORE NULLS is not supported for non-window functions",
2510            ));
2511        }
2512        let ignore_nulls = nulls != NONE && self.name(self.first(nulls)) == "IgnoreNulls";
2513        let distinct = self.quantifier(self.find(list, "DistinctOrAll")) == Quantifier::Distinct;
2514        let mut args = Vec::new();
2515        let arguments = self.find(list, "FunctionArgumentList");
2516        if arguments != NONE {
2517            for kid in self.kids(arguments) {
2518                args.push(self.argument(kid)?);
2519            }
2520        }
2521        // A call with an `OVER` on it is a window call and none of the rewrites below apply to it.
2522        // The reference binary agrees on the one case where that is visible: `ifnull(1) OVER ()`
2523        // keeps its name and its one argument and is turned down for not naming an aggregate,
2524        // where the same call without the `OVER` is a rewrite and an arity error.
2525        if over != NONE {
2526            let args = self.expr_slice(args);
2527            let spec = self.over(over)?;
2528            return Ok(self.push(Expr::Window {
2529                name,
2530                args,
2531                distinct,
2532                filter,
2533                ignore_nulls,
2534                order: inner,
2535                spec,
2536            }));
2537        }
2538        // `IFNULL` is an ordinary call in the grammar and is not one by the time DuckDB's parser is
2539        // done with it: `ifnull(NULL, 3)` comes back named `COALESCE(NULL, 3)` there, and so does
2540        // `main.ifnull(NULL, 3)`, so the qualifier goes with the rewrite. The count is checked here
2541        // because that is where upstream checks it, with the sentence below rather than the binder's
2542        // arity error, and it is checked before the two arguments are looked at.
2543        if self.ast.name(name).last().is_some_and(|part| part.eq_ignore_ascii_case("ifnull")) {
2544            if args.len() != 2 {
2545                return Err(Error::parser("Wrong number of arguments to IFNULL."));
2546            }
2547            let args = self.expr_slice(args);
2548            let name = self.function_name("coalesce");
2549            return Ok(self.push(Expr::Function { name, args, distinct, filter }));
2550        }
2551        let args = self.expr_slice(args);
2552        Ok(self.push(Expr::Function { name, args, distinct, filter }))
2553    }
2554
2555    // Windows.
2556
2557    /// `WindowClause <- 'WINDOW' List(WindowDefinition)` and
2558    /// `WindowDefinition <- Identifier 'AS' WindowFrameDefinition`.
2559    ///
2560    /// The definitions are read in the order they were written and each one can see the ones before
2561    /// it, so `WINDOW w AS (ORDER BY i), v AS (w)` defines two windows that order the same way.
2562    fn window_clause(&mut self, node: u32) -> Result<()> {
2563        for kid in self.kids(node) {
2564            if self.name(kid) != "WindowDefinition" {
2565                continue;
2566            }
2567            let name = self.identifier(self.first(kid));
2568            let definition = self.find(kid, "WindowFrameDefinition");
2569            if definition == NONE {
2570                return self.unsupported(kid);
2571            }
2572            let (spec, framed) = self.window_definition(definition)?;
2573            let spec = self.push_window(spec);
2574            self.named_windows.push((name, spec, framed));
2575        }
2576        Ok(())
2577    }
2578
2579    /// `OverClause <- 'OVER' WindowFrame` and
2580    /// `WindowFrame <- ParensIdentifier / WindowFrameDefinition / IdentifierWindowFrame`.
2581    ///
2582    /// The first and the third spelling are a bare reference, written `OVER (w)` and `OVER w`, and
2583    /// both resolve to the window that name was given. A reference is resolved here rather than
2584    /// carried, because that is where the reference binary resolves it: a name nobody defined is a
2585    /// `Parser Error` there, and a view written with one comes back out of the catalog with the
2586    /// definition written in its place.
2587    fn over(&mut self, node: u32) -> Result<WindowRef> {
2588        let mut frame = self.first(node);
2589        if self.name(frame) == "WindowFrame" {
2590            frame = self.first(frame);
2591        }
2592        match self.name(frame) {
2593            "ParensIdentifier" | "IdentifierWindowFrame" => {
2594                let name = self.identifier(self.first(frame));
2595                let (spec, _) = self.named_window(name)?;
2596                Ok(spec)
2597            }
2598            "WindowFrameDefinition" => {
2599                let (spec, _) = self.window_definition(frame)?;
2600                Ok(self.push_window(spec))
2601            }
2602            _ => self.unsupported(frame),
2603        }
2604    }
2605
2606    /// The window a name stands for, and whether its definition wrote a frame clause.
2607    fn named_window(&self, name: StrRef) -> Result<(WindowRef, bool)> {
2608        let written = self.ast.string(name);
2609        let found = self
2610            .named_windows
2611            .iter()
2612            .rev()
2613            .find(|&&(defined, _, _)| self.ast.string(defined).eq_ignore_ascii_case(written));
2614        match found {
2615            Some(&(_, spec, framed)) => Ok((spec, framed)),
2616            // The doubled quotes are upstream's and not a slip here. It writes the name with the
2617            // quoting a printed identifier gets and then writes quotes around that as well, so a
2618            // window called `w` is reported as `""w""`.
2619            None => Err(Error::parser(format!("window \"\"{written}\"\" does not exist"))),
2620        }
2621    }
2622
2623    /// `WindowFrameDefinition <- WindowFrameNameContentsParens / WindowFrameContentsParens`,
2624    /// `WindowFrameNameContents <- BaseWindowName? WindowFrameContents` and
2625    /// `WindowFrameContents <- WindowPartition? OrderByClause? FrameClause?`.
2626    ///
2627    /// Returns the window and whether a frame clause was written, which the caller needs because a
2628    /// definition that wrote one cannot be used as the base of another.
2629    fn window_definition(&mut self, node: u32) -> Result<(WindowSpec, bool)> {
2630        let held = self.first(self.first(node));
2631        let (base, contents) = match self.name(held) {
2632            "WindowFrameNameContents" => {
2633                (self.find(held, "BaseWindowName"), self.find(held, "WindowFrameContents"))
2634            }
2635            "WindowFrameContents" => (NONE, held),
2636            _ => return self.unsupported(held),
2637        };
2638        if contents == NONE {
2639            return self.unsupported(node);
2640        }
2641        let partition = self.find(contents, "WindowPartition");
2642        let order = self.find(contents, "OrderByClause");
2643        let frame = self.find(contents, "FrameClause");
2644        let mut spec = WindowSpec::empty();
2645        if base != NONE {
2646            let name = self.identifier(self.first(base));
2647            let written = self.ast.string(name).to_string();
2648            let (found, framed) = self.named_window(name)?;
2649            // The three refusals are upstream's, in its words. What they have in common is that a
2650            // base window is copied and not merged, so anything the copy would have to combine with
2651            // something the base already said is turned down rather than guessed at.
2652            if framed {
2653                return Err(Error::parser(format!(
2654                    "cannot copy window \"{written}\" because it has a frame clause"
2655                )));
2656            }
2657            spec = self.ast.window(found);
2658            if partition != NONE && !spec.partition.is_empty() {
2659                return Err(Error::parser(format!(
2660                    "Cannot override PARTITION BY clause of window \"{written}\""
2661                )));
2662            }
2663            if order != NONE && !spec.order.is_empty() {
2664                return Err(Error::parser(format!(
2665                    "Cannot override ORDER BY clause of window \"{written}\""
2666                )));
2667            }
2668        }
2669        if partition != NONE {
2670            let mut items = Vec::new();
2671            for kid in self.kids(partition) {
2672                items.push(self.expr(kid)?);
2673            }
2674            spec.partition = self.expr_slice(items);
2675        }
2676        if order != NONE {
2677            let (items, all) = self.order_by(order)?;
2678            if all {
2679                return self.unsupported(order);
2680            }
2681            spec.order = self.order_slice(items);
2682        }
2683        if frame != NONE {
2684            self.frame_clause(&mut spec, frame)?;
2685        }
2686        Ok((spec, frame != NONE))
2687    }
2688
2689    /// `FrameClause <- Framing FrameExtent WindowExcludeClause?`.
2690    ///
2691    /// One normalisation happens here and it is the reference binary's. A frame that runs from the
2692    /// first row of the partition to the last says the same thing however it is measured, so
2693    /// `RANGE` and `GROUPS` become `ROWS` when both ends are unbounded. It matters because the
2694    /// printed form of a window is the column name a target with no alias gets, and upstream prints
2695    /// `ROWS` for all three spellings.
2696    fn frame_clause(&mut self, spec: &mut WindowSpec, node: u32) -> Result<()> {
2697        let framing = self.first(self.find(node, "Framing"));
2698        spec.unit = match self.name(framing) {
2699            "RowsFraming" => WindowUnit::Rows,
2700            "RangeFraming" => WindowUnit::Range,
2701            "GroupsFraming" => WindowUnit::Groups,
2702            _ => return self.unsupported(framing),
2703        };
2704        let extent = self.first(self.find(node, "FrameExtent"));
2705        match self.name(extent) {
2706            // `SingleFrameExtent <- FrameBound`, which names the start and leaves the end at the
2707            // current row.
2708            "SingleFrameExtent" => {
2709                spec.start = self.frame_bound(self.first(extent))?;
2710                spec.end = WindowBound::CurrentRow;
2711            }
2712            // `BetweenFrameExtent <- 'BETWEEN' FrameBound 'AND' FrameBound`.
2713            "BetweenFrameExtent" => {
2714                spec.start = self.frame_bound(self.first(extent))?;
2715                spec.end = self.frame_bound(self.nth(extent, 1))?;
2716            }
2717            _ => return self.unsupported(extent),
2718        }
2719        let exclude = self.find(node, "WindowExcludeClause");
2720        if exclude != NONE {
2721            let element = self.first(self.first(exclude));
2722            spec.exclude = match self.name(element) {
2723                "ExcludeCurrentRow" => WindowExclude::CurrentRow,
2724                "ExcludeGroup" => WindowExclude::Group,
2725                "ExcludeTies" => WindowExclude::Ties,
2726                "ExcludeNoOthers" => WindowExclude::NoOthers,
2727                _ => return self.unsupported(element),
2728            };
2729        }
2730        if spec.start == WindowBound::UnboundedPreceding
2731            && spec.end == WindowBound::UnboundedFollowing
2732        {
2733            spec.unit = WindowUnit::Rows;
2734        }
2735        Ok(())
2736    }
2737
2738    /// `FrameBound <- FrameUnbounded / FrameCurrentRow / FrameExpression`.
2739    fn frame_bound(&mut self, node: u32) -> Result<WindowBound> {
2740        let inner = if self.name(node) == "FrameBound" { self.first(node) } else { node };
2741        match self.name(inner) {
2742            "FrameCurrentRow" => Ok(WindowBound::CurrentRow),
2743            // `FrameUnbounded <- 'UNBOUNDED' PrecedingOrFollowing`.
2744            "FrameUnbounded" => {
2745                if self.preceding(self.first(inner)) {
2746                    Ok(WindowBound::UnboundedPreceding)
2747                } else {
2748                    Ok(WindowBound::UnboundedFollowing)
2749                }
2750            }
2751            // `FrameExpression <- Expression PrecedingOrFollowing`.
2752            "FrameExpression" => {
2753                let offset = self.expr(self.first(inner))?;
2754                if self.preceding(self.nth(inner, 1)) {
2755                    Ok(WindowBound::Preceding(offset))
2756                } else {
2757                    Ok(WindowBound::Following(offset))
2758                }
2759            }
2760            _ => self.unsupported(inner),
2761        }
2762    }
2763
2764    /// `PrecedingOrFollowing <- PrecedingFrame / FollowingFrame`, which of the two it was.
2765    fn preceding(&self, node: u32) -> bool {
2766        self.name(self.first(node)) == "PrecedingFrame"
2767    }
2768
2769    /// `CoalesceExpression <- 'COALESCE' Parens(List(Expression))`.
2770    ///
2771    /// A keyword is not a child and the two wrappers are transparent, so the children are the
2772    /// arguments. One of them is enough for the grammar and none of them is a syntax error, which is
2773    /// why there is no count checked here.
2774    ///
2775    /// The call is written with the canonical name rather than the one the query used, since there is
2776    /// nothing else to keep: the keyword is the name. Upstream prints the column in capitals whatever
2777    /// case was written, because `COALESCE` is an operator there and not a function name that its
2778    /// parser folded, and the binder is where that is decided here.
2779    fn coalesce(&mut self, node: u32) -> Result<ExprRef> {
2780        let mut args = Vec::new();
2781        for kid in self.kids(node) {
2782            args.push(self.expr(kid)?);
2783        }
2784        let args = self.expr_slice(args);
2785        let name = self.function_name("coalesce");
2786        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
2787    }
2788
2789    /// `NullIfExpression <- 'NULLIF' Parens(NullIfArguments)` and
2790    /// `NullIfArguments <- Expression ',' Expression`.
2791    ///
2792    /// Exactly two arguments, because the rule says so: `nullif(1)` and `nullif(1, 2, 3)` are syntax
2793    /// errors upstream and are syntax errors here for the same reason, so there is no arity to check
2794    /// after the parse.
2795    ///
2796    /// It stays a function called `nullif` rather than becoming the `CASE` upstream's macro expands
2797    /// to, since the column it produces is named after the call and not after the expansion.
2798    fn null_if(&mut self, node: u32) -> Result<ExprRef> {
2799        let arguments = self.find(node, "NullIfArguments");
2800        if arguments == NONE {
2801            return self.unsupported(node);
2802        }
2803        let mut args = Vec::new();
2804        for kid in self.kids(arguments) {
2805            args.push(self.expr(kid)?);
2806        }
2807        let args = self.expr_slice(args);
2808        let name = self.function_name("nullif");
2809        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
2810    }
2811
2812    /// `SubstringExpression <- 'SUBSTRING' Parens(SubstringArguments)` and
2813    /// `SubstringArguments <- SubstringParameters / SubstringExpressionList`.
2814    ///
2815    /// Both spellings are the same call and DuckDB's parser writes both of them back out as one:
2816    /// `substring(s FROM a FOR b)` comes back as the column `"substring"(s, a, b)` there, and so does
2817    /// `substring(s, a, b)`. The `FOR` on its own is the one worth pointing at, since it is not the
2818    /// two argument call it looks like. `substring('abcdef' FOR 3)` is `"substring"('abcdef', 1, 3)`
2819    /// upstream, so the start is filled in with a literal 1 here rather than left out.
2820    fn substring(&mut self, node: u32) -> Result<ExprRef> {
2821        let shape = self.first(self.first(node));
2822        let mut args = Vec::new();
2823        match self.name(shape) {
2824            "SubstringExpressionList" => {
2825                for kid in self.kids(shape) {
2826                    args.push(self.expr(kid)?);
2827                }
2828            }
2829            "SubstringParameters" => {
2830                args.push(self.expr(self.first(shape))?);
2831                // `SubstringFromFor <- SubstringFromOptionalFor / SubstringFor`, and both of those
2832                // hold the bounds as `FromExpression` and `ForExpression`, so finding them by name
2833                // reads either shape and neither one has to be told apart from the other.
2834                let bounds = self.first(self.nth(shape, 1));
2835                let from = self.find(bounds, "FromExpression");
2836                let start =
2837                    if from == NONE { self.number("1") } else { self.expr(self.first(from))? };
2838                args.push(start);
2839                let count = self.find(bounds, "ForExpression");
2840                if count != NONE {
2841                    args.push(self.expr(self.first(count))?);
2842                }
2843            }
2844            _ => return self.unsupported(shape),
2845        }
2846        let args = self.expr_slice(args);
2847        let name = self.function_name("substring");
2848        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
2849    }
2850
2851    /// `PositionExpression <- 'POSITION' Parens(PositionArguments)` and
2852    /// `PositionArguments <- OtherOperatorExpression 'IN' Expression`.
2853    ///
2854    /// The two arguments swap. `position('c' IN 'abcdef')` is `"position"('abcdef', 'c')` upstream,
2855    /// which is the same order `strpos` and `instr` are written in, so the haystack comes first in
2856    /// the call and second in the query.
2857    fn position(&mut self, node: u32) -> Result<ExprRef> {
2858        let arguments = self.first(node);
2859        if self.count(arguments) != 2 {
2860            return self.unsupported(arguments);
2861        }
2862        let needle = self.expr(self.first(arguments))?;
2863        let haystack = self.expr(self.nth(arguments, 1))?;
2864        let args = self.expr_slice(vec![haystack, needle]);
2865        let name = self.function_name("position");
2866        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
2867    }
2868
2869    /// `TrimExpression <- 'TRIM' Parens(TrimArguments)` and
2870    /// `TrimArguments <- TrimDirection? TrimSource? List(Expression)`.
2871    ///
2872    /// The direction is not an argument, it is the function: `LEADING` is `ltrim` upstream and
2873    /// `TRAILING` is `rtrim`, while `BOTH` and the bare form are both `trim`. The characters to strip
2874    /// are the last argument whichever way they were written, so `trim(BOTH 'x' FROM 'xxaxx')` and
2875    /// `trim('xxaxx', 'x')` are the same call, which is why the source goes on the end of the list
2876    /// rather than in front of it.
2877    fn trim(&mut self, node: u32) -> Result<ExprRef> {
2878        let arguments = self.first(node);
2879        let direction = self.find(arguments, "TrimDirection");
2880        let name = match direction {
2881            NONE => "trim",
2882            held => match self.name(self.first(held)) {
2883                "TrimLeading" => "ltrim",
2884                "TrimTrailing" => "rtrim",
2885                _ => "trim",
2886            },
2887        };
2888        let mut args = Vec::new();
2889        for kid in self.kids(arguments) {
2890            if matches!(self.name(kid), "TrimDirection" | "TrimSource") {
2891                continue;
2892            }
2893            args.push(self.expr(kid)?);
2894        }
2895        // `TrimSource <- Expression? 'FROM'`, so `trim(LEADING FROM s)` has the node with nothing
2896        // under it and there is no second argument to add.
2897        let source = self.find(arguments, "TrimSource");
2898        if source != NONE && self.count(source) == 1 {
2899            args.push(self.expr(self.first(source))?);
2900        }
2901        let args = self.expr_slice(args);
2902        let name = self.function_name(name);
2903        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
2904    }
2905
2906    /// `OverlayExpression <- 'OVERLAY' Parens(OverlayArguments)` and
2907    /// `OverlayArguments <- OverlayParameters / OverlayExpressionList`, where
2908    /// `OverlayParameters <- Expression 'PLACING' Expression FromExpression ForExpression?`.
2909    ///
2910    /// The arguments are already in the order the call takes them, so the keyword spelling is the
2911    /// list spelling with `PLACING`, `FROM` and `FOR` where the commas would be:
2912    /// `overlay('abcdef' PLACING 'X' FROM 2 FOR 1)` is `"overlay"('abcdef', 'X', 2, 1)` upstream.
2913    fn overlay(&mut self, node: u32) -> Result<ExprRef> {
2914        let shape = self.first(self.first(node));
2915        if !matches!(self.name(shape), "OverlayParameters" | "OverlayExpressionList") {
2916            return self.unsupported(shape);
2917        }
2918        let mut args = Vec::new();
2919        for kid in self.kids(shape) {
2920            let kid = match self.name(kid) {
2921                "FromExpression" | "ForExpression" => self.first(kid),
2922                _ => kid,
2923            };
2924            args.push(self.expr(kid)?);
2925        }
2926        let args = self.expr_slice(args);
2927        let name = self.function_name("overlay");
2928        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
2929    }
2930
2931    /// A number literal the query did not write, for the one place a lowering has to supply one.
2932    fn number(&mut self, text: &str) -> ExprRef {
2933        let text = self.intern(text);
2934        self.push(Expr::Literal { kind: LiteralKind::Number, text })
2935    }
2936
2937    /// `ExtractExpression <- 'EXTRACT' Parens(ExtractArguments)` and
2938    /// `ExtractArguments <- ExtractArgument 'FROM' Expression`.
2939    ///
2940    /// `EXTRACT` is not a function in the grammar because its argument list is not an argument
2941    /// list, and it is a function everywhere after here because DuckDB's parser does the same
2942    /// rewrite: `EXTRACT(minute FROM t)` is `date_part('minute', t)` and there is no separate
2943    /// implementation of one of them. The part is a keyword, an identifier or a string in the
2944    /// grammar, and all three become the string, which is why this is a rewrite and not a node.
2945    fn extract(&mut self, node: u32) -> Result<ExprRef> {
2946        let arguments = self.find(node, "ExtractArguments");
2947        if arguments == NONE {
2948            return self.unsupported(node);
2949        }
2950        let argument = self.first(self.first(arguments));
2951        let part = match self.name(argument) {
2952            "ExtractStringArgument" => self.string_value(argument)?,
2953            // A keyword, which is one of the thirteen the grammar names and is written back as the
2954            // one spelling that keyword has. `EXTRACT(seconds FROM t)` and `EXTRACT(SECOND FROM t)`
2955            // are both `date_part('SECOND', t)`, which was measured, and it shows up in the column
2956            // name as well as in the deparse, since an unaliased column is named after the call.
2957            "ExtractDatePartArgument" => date_part(self.text(argument)),
2958            // An identifier, taken as written. Which specifier names are legal is not a question
2959            // about syntax, so the answer to it lives with the function.
2960            "ExtractIdentifierArgument" => self.text(argument).to_string(),
2961            _ => return self.unsupported(argument),
2962        };
2963        let text = self.intern(&part);
2964        let part = self.push(Expr::Literal { kind: LiteralKind::String, text });
2965        let operand = self.expr(self.nth(arguments, 1))?;
2966        let name = self.function_name("date_part");
2967        let args = self.expr_slice(vec![part, operand]);
2968        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
2969    }
2970
2971    /// `FunctionArgument <- NamedFunctionArgument / PositionalFunctionArgument`.
2972    fn argument(&mut self, node: u32) -> Result<ExprRef> {
2973        let inner = self.first(node);
2974        match self.name(inner) {
2975            "PositionalFunctionArgument" => self.expr(self.first(inner)),
2976            _ => self.unsupported(inner),
2977        }
2978    }
2979
2980    /// One argument of a table function, which is the same rule plus the names.
2981    ///
2982    /// `NamedParameter <- TypeFuncName Type? NamedParameterAssignment Expression` and
2983    /// `NamedParameterAssignment <- ':=' / '=>'`, so those two spellings are what the grammar has.
2984    /// The binary accepts a third, `name = value`, which the grammar has no rule for because it
2985    /// parses as an equality and is picked apart afterwards. That is what happens here too: a
2986    /// positional argument that is a comparison between a bare name and something else is a named
2987    /// parameter, which is the reading upstream's own transformer gives it. `read_parquet(f,
2988    /// binary_as_string=True)` is the query that matters and it is the spelling the ClickBench
2989    /// entry uses.
2990    ///
2991    /// The name is not resolved here and neither is the value. Which parameters a function takes
2992    /// is the binder's question, and so is whether `binary_as_string=True` means anything to the
2993    /// function it was written on.
2994    fn table_argument(&mut self, node: u32) -> Result<Target> {
2995        let inner = self.first(node);
2996        if self.name(inner) == "NamedFunctionArgument" {
2997            let named = self.first(inner);
2998            if self.count(named) != 3 {
2999                // The optional `Type` between the name and the assignment, which is a macro
3000                // parameter's declaration and not a call.
3001                return self.unsupported(named);
3002            }
3003            let alias = self.identifier(self.first(named));
3004            let expr = self.expr(self.nth(named, 2))?;
3005            return Ok(Target { expr, alias });
3006        }
3007        let expr = self.expr(self.first(inner))?;
3008        if let Expr::Binary { op: BinaryOp::Eq, left, right } = self.ast.expr(expr) {
3009            if let Expr::Column { name } = self.ast.expr(left) {
3010                if name.len == 1 {
3011                    let alias = self.ast.parts[name.start as usize];
3012                    return Ok(Target { expr: right, alias });
3013                }
3014            }
3015        }
3016        Ok(Target { expr, alias: NONE })
3017    }
3018
3019    /// `CastExpression <- CastOrTryCast Parens(CastArguments)`.
3020    fn cast(&mut self, node: u32) -> Result<ExprRef> {
3021        let try_cast = self.name(self.first(self.first(node))) == "TryCastKeyword";
3022        // `CastArguments <- Expression 'AS' Type`.
3023        let arguments = self.nth(node, 1);
3024        let operand = self.expr(self.first(arguments))?;
3025        let text = self.text(self.nth(arguments, 1)).to_string();
3026        let ty = self.intern(&text);
3027        Ok(self.push(Expr::Cast { operand, ty, try_cast }))
3028    }
3029
3030    /// `TypeLiteral <- Type StringLiteral`, which is the cast written the other way round.
3031    ///
3032    /// `DATE '1995-09-01'` and `CAST('1995-09-01' AS DATE)` are the same expression upstream, and
3033    /// the proof is the column name: the pinned binary answers both of them in a column called
3034    /// `CAST('1995-09-01' AS DATE)`. So this is the cast node and nothing else, which means every
3035    /// type the cast already takes is a typed literal for free and the two can never drift.
3036    ///
3037    /// The string is the literal the grammar matched rather than any expression, so there is no
3038    /// constant folding question here. `DATE x` does not parse in the first place.
3039    fn typed_literal(&mut self, node: u32) -> Result<ExprRef> {
3040        let text = self.text(self.first(node)).to_string();
3041        let ty = self.intern(&text);
3042        let operand = self.expr(self.nth(node, 1))?;
3043        Ok(self.push(Expr::Cast { operand, ty, try_cast: false }))
3044    }
3045
3046    /// `IntervalLiteral <- 'INTERVAL' IntervalParameter Interval?`, which is a function call.
3047    ///
3048    /// There is no interval node and there does not need to be one, because DuckDB's own
3049    /// transformer rewrites the literal into a call and the column name says so: `INTERVAL 1 DAY`
3050    /// comes back from the pinned binary in a column called
3051    /// `to_days(CAST(trunc(CAST(1 AS DOUBLE)) AS INTEGER))`. So the literal and a handwritten
3052    /// `to_days(1)` are the same expression from here on and the two cannot drift apart.
3053    ///
3054    /// Every unit goes through a DOUBLE on the way in, which is what makes `INTERVAL 1.5 DAY` one
3055    /// day rather than a day and a half: the truncation is in the rewrite and not in the function.
3056    /// The two units that can carry a fraction skip the truncation and stay a DOUBLE all the way,
3057    /// so `INTERVAL 2.7 SECOND` really is two and seven tenths of a second.
3058    ///
3059    /// A literal with no unit is the cast written the other way round, so `INTERVAL '1 day'` is
3060    /// `CAST('1 day' AS INTERVAL)`. That arm also catches a word the grammar does not read as a
3061    /// unit, since `INTERVAL 1 d` parses as this rule with no `Interval` child and a column alias
3062    /// after it, which is why upstream answers it with a cast error about an INTEGER.
3063    fn interval_literal(&mut self, node: u32) -> Result<ExprRef> {
3064        let parameter = self.find(node, "IntervalParameter");
3065        if parameter == NONE {
3066            return self.unsupported(node);
3067        }
3068        let operand = self.expr(self.first(parameter))?;
3069        let unit = self.find(node, "Interval");
3070        if unit == NONE {
3071            let ty = self.intern("INTERVAL");
3072            return Ok(self.push(Expr::Cast { operand, ty, try_cast: false }));
3073        }
3074        let spelling = self.name(self.first(unit));
3075        // The seven range forms parse and then refuse, in upstream's words, with the unit names
3076        // spelled the canonical way rather than the way they were written: `interval 1 days to
3077        // hours` is `DAY TO HOUR` there as well.
3078        if spelling == "IntervalToInterval" {
3079            let pair = self.name(self.first(self.first(unit)));
3080            return Err(Error::parser(format!("{} is not supported", worded(pair))));
3081        }
3082        let Some(&(_, function, width)) = UNITS.iter().find(|(rule, _, _)| *rule == spelling)
3083        else {
3084            return self.unsupported(unit);
3085        };
3086        let double = self.intern("DOUBLE");
3087        let mut count = self.push(Expr::Cast { operand, ty: double, try_cast: false });
3088        if let Some(width) = width {
3089            let name = self.function_name("trunc");
3090            let args = self.expr_slice(vec![count]);
3091            let whole = self.push(Expr::Function { name, args, distinct: false, filter: NONE });
3092            let ty = self.intern(width);
3093            count = self.push(Expr::Cast { operand: whole, ty, try_cast: false });
3094        }
3095        let name = self.function_name(function);
3096        let args = self.expr_slice(vec![count]);
3097        Ok(self.push(Expr::Function { name, args, distinct: false, filter: NONE }))
3098    }
3099
3100    /// `CaseExpression <- 'CASE' Expression? CaseWhenThen+ CaseElse? 'END'`.
3101    fn case(&mut self, node: u32) -> Result<ExprRef> {
3102        let mut operand = NONE;
3103        let mut arms = Vec::new();
3104        let mut otherwise = NONE;
3105        for kid in self.kids(node) {
3106            match self.name(kid) {
3107                // `CaseWhenThen <- 'WHEN' Expression 'THEN' Expression`.
3108                "CaseWhenThen" => {
3109                    let when = self.expr(self.first(kid))?;
3110                    let then = self.expr(self.nth(kid, 1))?;
3111                    arms.push(CaseArm { when, then });
3112                }
3113                // `CaseElse <- 'ELSE' Expression`.
3114                "CaseElse" => otherwise = self.expr(self.first(kid))?,
3115                // The bare `Expression` before the first `WHEN`, which makes it a simple case.
3116                _ => operand = self.expr(kid)?,
3117            }
3118        }
3119        let start = self.ast.case_arms.len() as u32;
3120        self.ast.case_arms.extend(arms);
3121        let arms = Slice { start, len: self.ast.case_arms.len() as u32 - start };
3122        Ok(self.push(Expr::Case { operand, arms, otherwise }))
3123    }
3124
3125    /// `ParenthesisExpression <- Parens(List(Expression)?)`, which is a row value.
3126    ///
3127    /// One item is not a row. `(a)` is `a` in every dialect and reading it as a one column row
3128    /// would change what `(a) = (b)` means.
3129    fn row(&mut self, node: u32) -> Result<ExprRef> {
3130        let mut items = Vec::new();
3131        for kid in self.kids(node) {
3132            items.push(self.expr(kid)?);
3133        }
3134        if items.len() == 1 {
3135            return Ok(items[0]);
3136        }
3137        let items = self.expr_slice(items);
3138        Ok(self.push(Expr::Row { items }))
3139    }
3140
3141    /// `Parameter <- '?' Number / '?' / '$' Number / '$' ColLabel`, a prepared statement parameter.
3142    ///
3143    /// The identifier is what follows the marker, so `?1` and `$1` are both the parameter named 1,
3144    /// and a bare `?` takes the next number by where it was written. That is what DuckDB does, which
3145    /// is why `? + $2` prints as `$1 + $2`: the counting is its own and does not skip a number
3146    /// because a later parameter claimed it.
3147    fn parameter(&mut self, node: u32) -> Result<ExprRef> {
3148        let written = self.text(node).trim();
3149        let written = written.trim_start_matches(['?', '$']).trim();
3150        let name = if written.is_empty() {
3151            self.anonymous += 1;
3152            self.anonymous.to_string()
3153        } else {
3154            written.to_string()
3155        };
3156        let name = self.intern(&name);
3157        Ok(self.push(Expr::Parameter { name }))
3158    }
3159
3160    /// `BoundedListExpression <- '[' List(Expression)? ']'`, which is a LIST value.
3161    ///
3162    /// One item is a list of one here, unlike the parenthesised form, because the brackets are what
3163    /// say list and there is nothing else `[a]` could mean.
3164    fn list(&mut self, node: u32) -> Result<ExprRef> {
3165        let mut items = Vec::new();
3166        for kid in self.kids(node) {
3167            items.push(self.expr(kid)?);
3168        }
3169        let items = self.expr_slice(items);
3170        Ok(self.push(Expr::List { items }))
3171    }
3172
3173    /// `SubqueryExpression <- SubqueryNot? SubqueryExists? SubqueryReference`.
3174    fn subquery(&mut self, node: u32) -> Result<ExprRef> {
3175        let negated = self.find(node, "SubqueryNot") != NONE;
3176        let exists = self.find(node, "SubqueryExists") != NONE;
3177        let reference = self.find(node, "SubqueryReference");
3178        let query = self.query(self.first(reference))?;
3179        Ok(if exists {
3180            self.push(Expr::Exists { query, negated })
3181        } else if negated {
3182            return self.unsupported(node);
3183        } else {
3184            self.push(Expr::Subquery { query })
3185        })
3186    }
3187
3188    /// The value of a string literal, with the quotes gone and the escapes resolved.
3189    ///
3190    /// A literal can be several tokens. `'a' 'b'` on two lines is one literal that is `ab`, which is
3191    /// the SQL standard's rule and DuckDB's, so the node is decoded token by token rather than by
3192    /// taking its text and stripping the outside.
3193    fn string_value(&self, node: u32) -> Result<String> {
3194        let span = self.tree.node(node);
3195        let mut value = String::new();
3196        for token in &self.tokens[span.start as usize..span.end as usize] {
3197            if token.kind == Kind::String {
3198                value.push_str(&string_token(token.text(self.query))?);
3199            }
3200        }
3201        Ok(value)
3202    }
3203
3204    /// The token that opens a string literal, which is the whole of it when it has a prefix.
3205    ///
3206    /// Only the first token is asked, because a prefixed literal is one token: `E'a' 'b'` is a
3207    /// syntax error upstream rather than a concatenation, so there is no second prefix to disagree
3208    /// with this one.
3209    fn first_string(&self, node: u32) -> &'a str {
3210        let span = self.tree.node(node);
3211        self.tokens[span.start as usize..span.end as usize]
3212            .iter()
3213            .find(|token| token.kind == Kind::String)
3214            .map_or("", |token| token.text(self.query))
3215    }
3216
3217    /// A string literal as an expression, which is the value plus what the prefix makes of it.
3218    ///
3219    /// `N'abc'` is a cast of the string to VARCHAR upstream and not a plain string, and the column
3220    /// name is the proof: the pinned binary answers it in a column called `CAST('abc' AS VARCHAR)`.
3221    /// So it is written here as the cast it is, and then there is nothing left to keep in step.
3222    ///
3223    /// `x'4142'` is not a string at all, it is a BLOB, so it is the one prefix that becomes a
3224    /// different kind of literal rather than a string with something done to it.
3225    fn string_literal(&mut self, node: u32) -> Result<ExprRef> {
3226        let token = self.first_string(node);
3227        let prefix = match token.as_bytes() {
3228            [prefix, b'\'', ..] => *prefix,
3229            _ => 0,
3230        };
3231        if matches!(prefix, b'X' | b'x') {
3232            if let Some(body) = token.get(1..).and_then(quoted_body) {
3233                let text = blob_text(body.as_bytes())?;
3234                let text = self.intern(&text);
3235                return Ok(self.push(Expr::Literal { kind: LiteralKind::Blob, text }));
3236            }
3237        }
3238        let value = self.string_value(node)?;
3239        let text = self.intern(&value);
3240        let literal = self.push(Expr::Literal { kind: LiteralKind::String, text });
3241        if matches!(prefix, b'N' | b'n') {
3242            let ty = self.intern("VARCHAR");
3243            return Ok(self.push(Expr::Cast { operand: literal, ty, try_cast: false }));
3244        }
3245        Ok(literal)
3246    }
3247}
3248
3249/// Each unit an interval literal can be written in, as the grammar rule that spells it, the
3250/// function it becomes, and the width the count is truncated to on the way there.
3251///
3252/// A width of `None` is the pair that keeps what is after the point. Those two stay a DOUBLE and
3253/// never see `trunc`, which is the whole of the difference between `INTERVAL 2.7 SECOND` being two
3254/// and seven tenths of a second and `INTERVAL 1.5 DAY` being one day. Every entry, both spellings
3255/// of every keyword and the width of each one was read off the pinned binary's column names.
3256const UNITS: &[(&str, &str, Option<&str>)] = &[
3257    ("YearKeyword", "to_years", Some("INTEGER")),
3258    ("MonthKeyword", "to_months", Some("INTEGER")),
3259    ("QuarterKeyword", "to_quarters", Some("INTEGER")),
3260    ("DecadeKeyword", "to_decades", Some("INTEGER")),
3261    ("CenturyKeyword", "to_centuries", Some("INTEGER")),
3262    ("MillenniumKeyword", "to_millennia", Some("INTEGER")),
3263    ("DayKeyword", "to_days", Some("INTEGER")),
3264    ("WeekKeyword", "to_weeks", Some("INTEGER")),
3265    ("HourKeyword", "to_hours", Some("BIGINT")),
3266    ("MinuteKeyword", "to_minutes", Some("BIGINT")),
3267    ("MicrosecondKeyword", "to_microseconds", Some("BIGINT")),
3268    ("SecondKeyword", "to_seconds", None),
3269    ("MillisecondKeyword", "to_milliseconds", None),
3270];
3271
3272/// The one spelling a date part keyword is written back as, which is not always the singular.
3273///
3274/// Both spellings of each of the thirteen keywords land on one name, and the name is upper case and
3275/// is plural for the two smallest parts and singular for the rest. That is not a rule, it is a list,
3276/// and it was read off the pinned binary a keyword at a time: `EXTRACT(milliseconds FROM t)` and
3277/// `EXTRACT(millisecond FROM t)` are both `date_part('MILLISECONDS', t)` while `EXTRACT(seconds FROM
3278/// t)` is `date_part('SECOND', t)`.
3279///
3280/// A word that is not a keyword never reaches here, because the grammar tells the two apart, and it
3281/// keeps whatever case it was written in. `EXTRACT(epoch FROM t)` stays lower case, measured.
3282fn date_part(written: &str) -> String {
3283    const PARTS: &[(&str, &str)] = &[
3284        ("YEAR", "YEAR"),
3285        ("YEARS", "YEAR"),
3286        ("MONTH", "MONTH"),
3287        ("MONTHS", "MONTH"),
3288        ("DAY", "DAY"),
3289        ("DAYS", "DAY"),
3290        ("HOUR", "HOUR"),
3291        ("HOURS", "HOUR"),
3292        ("MINUTE", "MINUTE"),
3293        ("MINUTES", "MINUTE"),
3294        ("SECOND", "SECOND"),
3295        ("SECONDS", "SECOND"),
3296        ("MILLISECOND", "MILLISECONDS"),
3297        ("MILLISECONDS", "MILLISECONDS"),
3298        ("MICROSECOND", "MICROSECONDS"),
3299        ("MICROSECONDS", "MICROSECONDS"),
3300        ("WEEK", "WEEK"),
3301        ("WEEKS", "WEEK"),
3302        ("QUARTER", "QUARTER"),
3303        ("QUARTERS", "QUARTER"),
3304        ("DECADE", "DECADE"),
3305        ("DECADES", "DECADE"),
3306        ("CENTURY", "CENTURY"),
3307        ("CENTURIES", "CENTURY"),
3308        ("MILLENNIUM", "MILLENNIUM"),
3309        ("MILLENNIA", "MILLENNIUM"),
3310    ];
3311    PARTS
3312        .iter()
3313        .find(|(spelling, _)| spelling.eq_ignore_ascii_case(written))
3314        .map_or_else(|| written.to_string(), |(_, name)| (*name).to_string())
3315}
3316
3317/// A grammar rule name like `DayToHour` as the words upstream puts in the message for it.
3318fn worded(rule: &str) -> String {
3319    let mut out = String::new();
3320    for character in rule.chars() {
3321        if character.is_ascii_uppercase() && !out.is_empty() {
3322            out.push(' ');
3323        }
3324        out.push(character.to_ascii_uppercase());
3325    }
3326    out
3327}
3328
3329/// The value of one string token, with the quotes gone and whatever the prefix means resolved.
3330///
3331/// There is no fall through that keeps the source text. That arm is what answered `SELECT E'a'`
3332/// with the four characters `E'a'`, and a default that silently answers with the query is a default
3333/// that will do this again with the next spelling somebody adds, so a spelling this does not know
3334/// raises instead. Per #329.
3335fn string_token(text: &str) -> Result<String> {
3336    if let Some(body) = dollar_body(text) {
3337        return Ok(body.to_string());
3338    }
3339    if let Some(body) = quoted_body(text) {
3340        return Ok(body.replace("''", "'"));
3341    }
3342    let Some(body) = text.get(1..).and_then(quoted_body) else {
3343        return Ok(text.to_string());
3344    };
3345    match text.as_bytes()[0] {
3346        b'E' | b'e' => escaped(body),
3347        // `N'abc'` is the string and nothing else. The cast that makes the name is put on outside.
3348        b'N' | b'n' => Ok(body.replace("''", "'")),
3349        // Not a bit string, whatever the spelling suggests. Upstream answers `B'101'` with the four
3350        // characters `b101` as a VARCHAR, and `B''` with the one character `b`, which is measured
3351        // and not guessed. Nothing else is done with the body.
3352        b'B' | b'b' => Ok(format!("b{}", body.replace("''", "'"))),
3353        // `x'41'` is a BLOB and a BLOB is not a string, so the places that want a string out of a
3354        // literal, which are DESCRIBE and the part in EXTRACT, do not get one from this spelling.
3355        _ => Err(Error::not_implemented(format!("the string literal {text} is not supported yet"))),
3356    }
3357}
3358
3359/// The text a blob literal's body means, which is the text a blob prints as.
3360///
3361/// `x'4142'` is two bytes and the pinned binary calls the column `'AB'::BLOB`, so what is kept here
3362/// is the printed form and not the source. The cast that reads it back gives the bytes again, which
3363/// is what makes one text enough for both the value and the name, and it is `Value` that prints it
3364/// so the two spellings of a blob cannot drift apart.
3365///
3366/// Upstream writes `\xHH` for every pair without looking at the digits and lets the cast refuse the
3367/// ones that are not hex, which is why `x'4'` is a parser error and `x'zz'` is a conversion error
3368/// one step later. Doing the same thing gives both messages in the same words. The pairs are bytes
3369/// and not characters: `x'éé'` is four bytes and so two pairs, which is how upstream counts them.
3370fn blob_text(body: &[u8]) -> Result<String> {
3371    if body.len() % 2 != 0 {
3372        return Err(Error::parser("Hex string literal must have an even number of hex digits"));
3373    }
3374    let digit = |byte: u8| (byte as char).to_digit(16).map(|digit| digit as u8);
3375    let bytes: Option<Vec<u8>> =
3376        body.chunks(2).map(|pair| Some(digit(pair[0])? * 16 + digit(pair[1])?)).collect();
3377    match bytes {
3378        Some(bytes) => Ok(Value::Blob(bytes).to_string()),
3379        None => {
3380            Ok(body.chunks(2).map(|pair| format!("\\x{}", String::from_utf8_lossy(pair))).collect())
3381        }
3382    }
3383}
3384
3385/// The body of a single quoted string, for the tokens that are one.
3386///
3387/// An unterminated token has nothing to take off the end and keeps every byte it was given, which
3388/// is why the closing quote has to be a quote that is not also the opening one.
3389fn quoted_body(text: &str) -> Option<&str> {
3390    text.strip_prefix('\'').filter(|rest| !rest.is_empty()).and_then(|rest| rest.strip_suffix('\''))
3391}
3392
3393/// The body of an `E'...'` literal, with the C style escapes resolved.
3394///
3395/// Every rule here was read off the pinned binary one at a time. The named escapes are `\n`, `\t`,
3396/// `\r`, `\b` and `\f`, and `\v` is not one of them. `\x` takes one or two hex digits and `\0`
3397/// through `\7` take one to three octal digits, both of which write a byte and not a character, so
3398/// `\xc3\xa9` is one `é` and `\377` is not a string at all. `\uHHHH` takes exactly four hex digits
3399/// and writes the character they name. Anything else, including a `\u` that is short or names a
3400/// surrogate half or a NUL, drops the backslash and keeps the character, so `\q` is `q` and `\u41`
3401/// is `u41`.
3402///
3403/// The result is bytes until the end because the escapes write bytes, and the two ways of writing
3404/// something that is not a string both raise the way upstream raises them.
3405fn escaped(body: &str) -> Result<String> {
3406    let bytes = body.as_bytes();
3407    let mut out = Vec::with_capacity(bytes.len());
3408    let mut at = 0;
3409    while at < bytes.len() {
3410        let byte = bytes[at];
3411        at += 1;
3412        if byte == b'\'' && bytes.get(at) == Some(&b'\'') {
3413            out.push(b'\'');
3414            at += 1;
3415            continue;
3416        }
3417        if byte != b'\\' || at == bytes.len() {
3418            out.push(byte);
3419            continue;
3420        }
3421        let escape = bytes[at];
3422        at += 1;
3423        match escape {
3424            b'n' => out.push(b'\n'),
3425            b't' => out.push(b'\t'),
3426            b'r' => out.push(b'\r'),
3427            b'b' => out.push(0x08),
3428            b'f' => out.push(0x0c),
3429            b'x' => match digits(bytes, &mut at, 16, 2) {
3430                Some(value) => out.push(value as u8),
3431                None => out.push(b'x'),
3432            },
3433            b'0'..=b'7' => {
3434                at -= 1;
3435                let value = digits(bytes, &mut at, 8, 3).unwrap_or(0);
3436                out.push(value as u8);
3437            }
3438            b'u' => match four_hex(bytes, at).and_then(char::from_u32).filter(|c| *c != '\0') {
3439                Some(c) => {
3440                    at += 4;
3441                    out.extend_from_slice(c.encode_utf8(&mut [0; 4]).as_bytes());
3442                }
3443                None => out.push(b'u'),
3444            },
3445            other => out.push(other),
3446        }
3447    }
3448    if out.contains(&0) {
3449        return Err(Error::parser("Null character not permitted in escape string literal"));
3450    }
3451    String::from_utf8(out).map_err(|error| {
3452        Error::parser(format!(
3453            "Invalid UTF-8 in escape string literal at byte offset {}: byte mismatch",
3454            error.utf8_error().valid_up_to()
3455        ))
3456    })
3457}
3458
3459/// Up to `most` digits in `radix` starting at `at`, moving `at` past the ones that were taken.
3460///
3461/// `None` means there were none at all, which is the case where the escape was not an escape:
3462/// `\x` on its own is the letter `x` upstream and not a zero byte.
3463fn digits(bytes: &[u8], at: &mut usize, radix: u32, most: usize) -> Option<u32> {
3464    let mut value = None;
3465    for _ in 0..most {
3466        let Some(digit) = bytes.get(*at).and_then(|byte| (*byte as char).to_digit(radix)) else {
3467            break;
3468        };
3469        value = Some(value.unwrap_or(0) * radix + digit);
3470        *at += 1;
3471    }
3472    value
3473}
3474
3475/// The four hex digits of a `\uHHHH`, which has to be all four of them or it is not one.
3476///
3477/// Nothing is consumed here, because the digits are only digits if the whole escape works out. A
3478/// surrogate half is not a character and upstream does not pair it up either, so `😀` is
3479/// the ten characters it was written as, which is what the caller falls back to.
3480fn four_hex(bytes: &[u8], at: usize) -> Option<u32> {
3481    let digits = bytes.get(at..at + 4)?;
3482    if !digits.iter().all(u8::is_ascii_hexdigit) {
3483        return None;
3484    }
3485    u32::from_str_radix(std::str::from_utf8(digits).ok()?, 16).ok()
3486}
3487
3488/// The body of a dollar quoted string, for the tokens that are one.
3489///
3490/// The tag is whatever sits between the opening pair of dollars and may be empty, so `$$a$$` and
3491/// `$tag$a$tag$` both arrive here, and nothing inside the body is escaped, which is the whole reason
3492/// the spelling exists. The tokenizer has already found the closing tag, which is the part that takes
3493/// work, so this says where the body starts and ends and no more. A token that is not dollar quoted
3494/// gives `None` and so does an unterminated one, which has no closing tag to take off and keeps every
3495/// byte it was given, the way the matcher already treats it. Per #276.
3496fn dollar_body(text: &str) -> Option<&str> {
3497    let rest = text.strip_prefix('$')?;
3498    let close = rest.find('$')?;
3499    let (tag, body) = (&rest[..close], &rest[close + 1..]);
3500    body.strip_suffix(&format!("${tag}$"))
3501}
3502
3503/// Strip the quoting off an identifier.
3504///
3505/// DuckDB does not fold identifier case at any point, quoted or not, so this only removes the
3506/// quotes and resolves the doubled ones. Anything else would be the parser deciding what a name is.
3507///
3508/// Single quotes are stripped too, and the only way one gets here is the file name in `FROM
3509/// 'hits.parquet'`, because the matcher takes a string for a name in that position and in `COPY t TO
3510/// '...'` and nowhere else. Leaving them on would make that name different from the one `FROM
3511/// "hits.parquet"` writes, and DuckDB reads both of those as the same file.
3512fn unquote(text: &str) -> String {
3513    if let Some(body) = text.strip_prefix('"').and_then(|rest| rest.strip_suffix('"')) {
3514        return body.replace("\"\"", "\"");
3515    }
3516    match text.strip_prefix('\'').and_then(|rest| rest.strip_suffix('\'')) {
3517        Some(body) => body.replace("''", "'"),
3518        None => text.to_string(),
3519    }
3520}
3521
3522#[cfg(test)]
3523mod tests {
3524    use super::*;
3525    use crate::corpus::CORPUS;
3526    use crate::matcher::parse;
3527
3528    /// The AST written back out as text, which is what the assertions below read.
3529    ///
3530    /// Not a SQL printer and not trying to be. It is deliberately not valid SQL: operators are
3531    /// spelled with the name of the variant and every binary node is parenthesised, so that a test
3532    /// asserting on this text is asserting on the shape of the tree and not on a formatting choice.
3533    /// `a - b - c` and `a - (b - c)` have to look different here or the test that tells them apart
3534    /// is not a test.
3535    fn show(ast: &Ast, expr: ExprRef) -> String {
3536        if expr == NONE {
3537            return "-".to_string();
3538        }
3539        /// The `FILTER` on a call, which is nothing at all when there is none.
3540        fn shown_filter(ast: &Ast, filter: ExprRef) -> String {
3541            if filter == NONE { String::new() } else { format!(" FILTER [{}]", show(ast, filter)) }
3542        }
3543        /// A run of sort keys, which a window call has two of and in two different places.
3544        fn keys(ast: &Ast, slice: Slice) -> String {
3545            ast.order_list(slice)
3546                .iter()
3547                .map(|item| format!("{} {:?} {:?}", show(ast, item.expr), item.order, item.nulls))
3548                .collect::<Vec<_>>()
3549                .join(", ")
3550        }
3551        let list = |slice: Slice| {
3552            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
3553        };
3554        match ast.expr(expr) {
3555            Expr::Star { qualifier, replacements } => {
3556                let star = if qualifier.is_empty() {
3557                    "*".to_string()
3558                } else {
3559                    format!("{}.*", ast.name_text(qualifier))
3560                };
3561                if replacements.is_empty() {
3562                    return star;
3563                }
3564                let entries: Vec<String> = ast
3565                    .target_list(replacements)
3566                    .iter()
3567                    .map(|target| {
3568                        format!("{} AS {}", show(ast, target.expr), ast.string(target.alias))
3569                    })
3570                    .collect();
3571                format!("{star} REPLACE ({})", entries.join(", "))
3572            }
3573            Expr::Column { name } => ast.name_text(name),
3574            Expr::Literal { kind, text } => match kind {
3575                LiteralKind::Number => ast.string(text).to_string(),
3576                LiteralKind::String => format!("'{}'", ast.string(text)),
3577                LiteralKind::Blob => format!("'{}'::BLOB", ast.string(text)),
3578                other => format!("{other:?}").to_uppercase(),
3579            },
3580            Expr::Unary { op, operand } => format!("({op:?} {})", show(ast, operand)),
3581            Expr::Binary { op, left, right } => {
3582                let op = match op {
3583                    BinaryOp::Named(name) => ast.string(name).to_string(),
3584                    other => format!("{other:?}"),
3585                };
3586                format!("({} {op} {})", show(ast, left), show(ast, right))
3587            }
3588            Expr::Function { name, args, distinct, filter } => {
3589                let distinct = if distinct { "DISTINCT " } else { "" };
3590                let filter = shown_filter(ast, filter);
3591                format!("{}({distinct}{}){filter}", ast.name_text(name), list(args))
3592            }
3593            Expr::Window { name, args, distinct, filter, ignore_nulls, order: inner, spec } => {
3594                let distinct = if distinct { "DISTINCT " } else { "" };
3595                let filter = shown_filter(ast, filter);
3596                let nulls = if ignore_nulls { " IGNORE NULLS" } else { "" };
3597                let inner = keys(ast, inner);
3598                let inner = if inner.is_empty() { inner } else { format!(" ORDER BY {inner}") };
3599                let held = ast.window(spec);
3600                let order = keys(ast, held.order);
3601                let bound = |end: WindowBound| match end {
3602                    WindowBound::Preceding(offset) => format!("Preceding({})", show(ast, offset)),
3603                    WindowBound::Following(offset) => format!("Following({})", show(ast, offset)),
3604                    other => format!("{other:?}"),
3605                };
3606                format!(
3607                    "{}({distinct}{}{inner}{nulls}){filter} OVER [{}] [{order}] [{:?} {} {} {:?}]",
3608                    ast.name_text(name),
3609                    list(args),
3610                    list(held.partition),
3611                    held.unit,
3612                    bound(held.start),
3613                    bound(held.end),
3614                    held.exclude
3615                )
3616            }
3617            Expr::Cast { operand, ty, try_cast } => {
3618                let word = if try_cast { "TRY_CAST" } else { "CAST" };
3619                format!("{word}({} AS {})", show(ast, operand), ast.string(ty))
3620            }
3621            Expr::Case { operand, arms, otherwise } => {
3622                let arms = ast
3623                    .arm_list(arms)
3624                    .iter()
3625                    .map(|arm| format!("WHEN {} THEN {}", show(ast, arm.when), show(ast, arm.then)))
3626                    .collect::<Vec<_>>()
3627                    .join(" ");
3628                format!("CASE {} {arms} ELSE {} END", show(ast, operand), show(ast, otherwise))
3629            }
3630            Expr::Between { operand, low, high, negated } => {
3631                let not = if negated { "NOT " } else { "" };
3632                format!(
3633                    "({not}{} BETWEEN {} AND {})",
3634                    show(ast, operand),
3635                    show(ast, low),
3636                    show(ast, high)
3637                )
3638            }
3639            Expr::In { operand, list: items, negated } => {
3640                let not = if negated { "NOT " } else { "" };
3641                format!("({not}{} IN [{}])", show(ast, operand), list(items))
3642            }
3643            Expr::List { items } => format!("[{}]", list(items)),
3644            Expr::Parameter { name } => format!("${}", ast.string(name)),
3645            Expr::Row { items } => format!("ROW({})", list(items)),
3646            Expr::Subquery { query } => format!("({})", show_query(ast, query)),
3647            Expr::Exists { query, negated } => {
3648                let exists = format!("EXISTS ({})", show_query(ast, query));
3649                if negated { format!("NOT {exists}") } else { exists }
3650            }
3651            Expr::InSubquery { operand, query, negated } => {
3652                let written = format!("{} IN ({})", show(ast, operand), show_query(ast, query));
3653                if negated { format!("NOT {written}") } else { written }
3654            }
3655            Expr::QuantifiedSubquery { operand, op, query, all } => {
3656                let quantifier = if all { "ALL" } else { "ANY" };
3657                format!("{} {op:?} {quantifier} ({})", show(ast, operand), show_query(ast, query))
3658            }
3659        }
3660    }
3661
3662    /// One from item written back out.
3663    fn show_source(ast: &Ast, source: SourceRef) -> String {
3664        let alias = |alias: StrRef| match alias {
3665            NONE => String::new(),
3666            other => format!(" AS {}", ast.string(other)),
3667        };
3668        match ast.source(source) {
3669            Source::Table { name, alias: name_alias, .. } => {
3670                format!("{}{}", ast.name_text(name), alias(name_alias))
3671            }
3672            Source::Function { name, args, alias: call_alias, .. } => {
3673                let args = ast
3674                    .target_list(args)
3675                    .iter()
3676                    .map(|item| match item.alias {
3677                        NONE => show(ast, item.expr),
3678                        named => format!("{} := {}", ast.string(named), show(ast, item.expr)),
3679                    })
3680                    .collect::<Vec<_>>()
3681                    .join(", ");
3682                format!("{}({args}){}", ast.name_text(name), alias(call_alias))
3683            }
3684            Source::Subquery { query, alias: query_alias, .. } => {
3685                format!("({}){}", show_query(ast, query), alias(query_alias))
3686            }
3687            Source::Cte { cte, alias: cte_alias, .. } => {
3688                format!("{}{}", ast.string(ast.cte(cte).name), alias(cte_alias))
3689            }
3690            Source::Values { rows, alias: values_alias, .. } => {
3691                format!("{}{}", show_rows(ast, rows), alias(values_alias))
3692            }
3693            Source::Join { left, right, kind, natural, on, using } => {
3694                let natural = if natural { "NATURAL " } else { "" };
3695                let on = if on == NONE { String::new() } else { format!(" ON {}", show(ast, on)) };
3696                let using = if using.is_empty() {
3697                    String::new()
3698                } else {
3699                    format!(" USING ({})", ast.name_text(using))
3700                };
3701                format!(
3702                    "({} {natural}{kind:?} JOIN {}{on}{using})",
3703                    show_source(ast, left),
3704                    show_source(ast, right)
3705                )
3706            }
3707        }
3708    }
3709
3710    /// The rows of a `VALUES` written back out.
3711    fn show_rows(ast: &Ast, rows: Slice) -> String {
3712        let rows = ast
3713            .rows(rows)
3714            .iter()
3715            .map(|&row| {
3716                let items = ast
3717                    .expr_list(row)
3718                    .iter()
3719                    .map(|&item| show(ast, item))
3720                    .collect::<Vec<_>>()
3721                    .join(", ");
3722                format!("({items})")
3723            })
3724            .collect::<Vec<_>>()
3725            .join(", ");
3726        format!("VALUES {rows}")
3727    }
3728
3729    /// One query written back out.
3730    fn show_query(ast: &Ast, index: QueryRef) -> String {
3731        let query = ast.query(index);
3732        let list = |slice: Slice| {
3733            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
3734        };
3735        let mut out = String::new();
3736        for &index in ast.cte_list(query.ctes) {
3737            let cte = ast.cte(index);
3738            let columns = ast.name(cte.columns).collect::<Vec<_>>().join(", ");
3739            let columns = if columns.is_empty() { columns } else { format!("({columns})") };
3740            out += &format!(
3741                "WITH {}{columns} AS MATERIALIZED ({}) ",
3742                ast.string(cte.name),
3743                show_query(ast, cte.query)
3744            );
3745        }
3746        out += &match query.body {
3747            QueryBody::SetOp { op, quantifier, by_name, left, right } => {
3748                let by_name = if by_name { " BY NAME" } else { "" };
3749                format!(
3750                    "({} {op:?} {quantifier:?}{by_name} {})",
3751                    show_query(ast, left),
3752                    show_query(ast, right)
3753                )
3754            }
3755            QueryBody::Select(index) => {
3756                let select = ast.select(index);
3757                let distinct = match select.distinct {
3758                    Distinct::No => String::new(),
3759                    Distinct::Yes => " DISTINCT".to_string(),
3760                    Distinct::On(on) => format!(" DISTINCT ON ({})", list(on)),
3761                };
3762                let targets = ast
3763                    .target_list(select.targets)
3764                    .iter()
3765                    .map(|target| match target.alias {
3766                        NONE => show(ast, target.expr),
3767                        alias => format!("{} AS {}", show(ast, target.expr), ast.string(alias)),
3768                    })
3769                    .collect::<Vec<_>>()
3770                    .join(", ");
3771                let mut out = format!("SELECT{distinct} {targets}");
3772                if !select.from.is_empty() {
3773                    let from = ast
3774                        .source_list(select.from)
3775                        .iter()
3776                        .map(|&source| show_source(ast, source))
3777                        .collect::<Vec<_>>()
3778                        .join(", ");
3779                    out += &format!(" FROM {from}");
3780                }
3781                if select.filter != NONE {
3782                    out += &format!(" WHERE {}", show(ast, select.filter));
3783                }
3784                if select.group_by_all {
3785                    out += " GROUP BY ALL";
3786                } else if !select.group_by.is_empty() {
3787                    out += &format!(" GROUP BY {}", list(select.group_by));
3788                }
3789                if select.having != NONE {
3790                    out += &format!(" HAVING {}", show(ast, select.having));
3791                }
3792                out
3793            }
3794            QueryBody::Values(rows) => show_rows(ast, rows),
3795            QueryBody::Describe(inner) => format!("DESCRIBE {}", show_query(ast, inner)),
3796            QueryBody::Show { name, .. } => format!("SHOW {}", ast.name_text(name)),
3797        };
3798        if query.order_by_all {
3799            out += " ORDER BY ALL";
3800        } else if !query.order_by.is_empty() {
3801            let items = ast
3802                .order_list(query.order_by)
3803                .iter()
3804                .map(|item| format!("{} {:?} {:?}", show(ast, item.expr), item.order, item.nulls))
3805                .collect::<Vec<_>>()
3806                .join(", ");
3807            out += &format!(" ORDER BY {items}");
3808        }
3809        if query.limit != NONE {
3810            let percent = if query.limit_percent { "%" } else { "" };
3811            out += &format!(" LIMIT {}{percent}", show(ast, query.limit));
3812        }
3813        if query.offset != NONE {
3814            out += &format!(" OFFSET {}", show(ast, query.offset));
3815        }
3816        out
3817    }
3818
3819    /// One statement, transformed and written back out.
3820    fn round(query: &str) -> String {
3821        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
3822        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
3823        let Statement::Query(index) = ast.statements[0] else {
3824            panic!("{query} is not a query");
3825        };
3826        show_query(&ast, index)
3827    }
3828
3829    fn round_with_case(query: &str, case: IdentifierCase) -> String {
3830        let ast =
3831            parse_ast_with_case(query, case).unwrap_or_else(|error| panic!("{query}: {error}"));
3832        let Statement::Query(index) = ast.statements[0] else {
3833            panic!("{query} is not a query");
3834        };
3835        show_query(&ast, index)
3836    }
3837
3838    /// One statement, transformed and written back out as the DDL and DML shape it is.
3839    fn round_statement(query: &str) -> String {
3840        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
3841        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
3842        match ast.statements[0] {
3843            Statement::Query(index) => show_query(&ast, index),
3844            Statement::CreateTable(index) => {
3845                let create = ast.create_table(index);
3846                let mut out = "CREATE".to_string();
3847                if create.or_replace {
3848                    out += " OR REPLACE";
3849                }
3850                if create.temporary {
3851                    out += " TEMPORARY";
3852                }
3853                out += " TABLE";
3854                if create.if_not_exists {
3855                    out += " IF NOT EXISTS";
3856                }
3857                out += &format!(" {}", ast.name_text(create.name));
3858                let columns = ast
3859                    .column_defs(create.columns)
3860                    .iter()
3861                    .map(|def| {
3862                        let ty = match def.ty {
3863                            NONE => String::new(),
3864                            other => format!(" {}", ast.string(other)),
3865                        };
3866                        let null = if def.not_null { " NOT NULL" } else { "" };
3867                        format!("{}{ty}{null}", ast.string(def.name))
3868                    })
3869                    .collect::<Vec<_>>()
3870                    .join(", ");
3871                if !columns.is_empty() || create.query == NONE {
3872                    out += &format!(" ({columns})");
3873                }
3874                if create.query != NONE {
3875                    out += &format!(" AS {}", show_query(&ast, create.query));
3876                }
3877                out
3878            }
3879            Statement::CreateView(index) => {
3880                let create = ast.create_view(index);
3881                let mut out = "CREATE".to_string();
3882                if create.or_replace {
3883                    out += " OR REPLACE";
3884                }
3885                if create.temporary {
3886                    out += " TEMPORARY";
3887                }
3888                out += " VIEW";
3889                if create.if_not_exists {
3890                    out += " IF NOT EXISTS";
3891                }
3892                out += &format!(" {}", ast.name_text(create.name));
3893                if !create.columns.is_empty() {
3894                    let columns = ast.name(create.columns).collect::<Vec<_>>().join(", ");
3895                    out += &format!(" ({columns})");
3896                }
3897                out + &format!(" AS {}", show_query(&ast, create.query))
3898            }
3899            Statement::DropTable(index) => {
3900                let drop = ast.drop_table(index);
3901                let mut out = if drop.view { "DROP VIEW" } else { "DROP TABLE" }.to_string();
3902                if drop.if_exists {
3903                    out += " IF EXISTS";
3904                }
3905                let names = ast
3906                    .name_list(drop.names)
3907                    .iter()
3908                    .map(|&name| ast.name_text(name))
3909                    .collect::<Vec<_>>()
3910                    .join(", ");
3911                out + &format!(" {names}")
3912            }
3913            Statement::Insert(index) => {
3914                let insert = ast.insert(index);
3915                let mut out = format!("INSERT INTO {}", ast.name_text(insert.name));
3916                if !insert.columns.is_empty() {
3917                    let columns = ast.name(insert.columns).collect::<Vec<_>>().join(", ");
3918                    out += &format!(" ({columns})");
3919                }
3920                out + &format!(" {}", show_query(&ast, insert.source))
3921            }
3922            Statement::Set(index) if ast.setting(index).pragma => {
3923                format!("PRAGMA {}", ast.string(ast.setting(index).name))
3924            }
3925            Statement::Set(index) => {
3926                let setting = ast.setting(index);
3927                let scope = match setting.scope.keyword() {
3928                    "" => String::new(),
3929                    word => format!(" {word}"),
3930                };
3931                format!("SET{scope} {} = {}", ast.string(setting.name), show(&ast, setting.value))
3932            }
3933            Statement::Reset(index) => {
3934                let setting = ast.setting(index);
3935                let scope = match setting.scope.keyword() {
3936                    "" => String::new(),
3937                    word => format!(" {word}"),
3938                };
3939                format!("RESET{scope} {}", ast.string(setting.name))
3940            }
3941            Statement::Checkpoint => "CHECKPOINT".to_string(),
3942            Statement::Explain { query, analyze, statistics } => {
3943                let analyze = if analyze { "ANALYZE " } else { "" };
3944                let statistics = if statistics { "(STATISTICS) " } else { "" };
3945                format!("EXPLAIN {analyze}{statistics}{}", show_query(&ast, query))
3946            }
3947        }
3948    }
3949
3950    #[test]
3951    fn expressions_and_queries_keep_their_source_ranges() {
3952        let sql = "SELECT 1 + 22";
3953        let ast = parse_ast(sql).expect("the query parses");
3954        let Statement::Query(query) = ast.statements[0] else { panic!("a query") };
3955        assert_eq!(ast.query_span(query), Span::new(0, sql.len() as u32));
3956        let twenty_two = ast
3957            .exprs
3958            .iter()
3959            .enumerate()
3960            .find_map(|(at, expr)| match *expr {
3961                Expr::Literal { kind: LiteralKind::Number, text } if ast.string(text) == "22" => {
3962                    Some(at as u32)
3963                }
3964                _ => None,
3965            })
3966            .expect("the literal is in the arena");
3967        assert_eq!(ast.expr_span(twenty_two), Span::new(11, 13));
3968    }
3969
3970    #[test]
3971    fn an_explain_keeps_the_query_it_was_asked_about() {
3972        assert_eq!(
3973            round_statement("EXPLAIN SELECT a FROM t WHERE a > 1"),
3974            "EXPLAIN SELECT a FROM t WHERE (a Gt 1)"
3975        );
3976        assert_eq!(round_statement("explain select 1"), "EXPLAIN SELECT 1");
3977        assert_eq!(round_statement("explain analyze select 1"), "EXPLAIN ANALYZE SELECT 1");
3978    }
3979
3980    #[test]
3981    fn the_three_explain_options_this_answers_mean_what_their_names_say() {
3982        // `ANALYZE` in the list is the keyword written the other way, so the two spellings have to
3983        // land on the same statement rather than on two that happen to print alike.
3984        assert_eq!(round_statement("EXPLAIN (ANALYZE) SELECT 1"), "EXPLAIN ANALYZE SELECT 1");
3985        assert_eq!(
3986            round_statement("explain (analyze) select 1"),
3987            round_statement("explain analyze select 1")
3988        );
3989        // `LOGICAL` names the plan this already prints, so asking for it changes nothing.
3990        assert_eq!(round_statement("EXPLAIN (LOGICAL) SELECT 1"), "EXPLAIN SELECT 1");
3991        assert_eq!(
3992            round_statement("EXPLAIN (STATISTICS) SELECT 1"),
3993            "EXPLAIN (STATISTICS) SELECT 1"
3994        );
3995        assert_eq!(
3996            round_statement("EXPLAIN (ANALYZE, STATISTICS) SELECT 1"),
3997            "EXPLAIN ANALYZE (STATISTICS) SELECT 1"
3998        );
3999        assert_eq!(
4000            round_statement("EXPLAIN ANALYZE (STATISTICS) SELECT 1"),
4001            "EXPLAIN ANALYZE (STATISTICS) SELECT 1"
4002        );
4003    }
4004
4005    #[test]
4006    fn the_parts_of_an_explain_that_are_not_the_query_are_refused_by_name() {
4007        // An option name this does not answer is refused in DuckDB's own words, an option that
4008        // carries a value is refused by its grammar rule because none of the three takes one, and a
4009        // statement that is not a query has no plan to show.
4010        for (query, named) in [
4011            ("EXPLAIN (FORMAT JSON) SELECT 1", "Unimplemented explain type: format"),
4012            ("EXPLAIN (NONSENSE) SELECT 1", "Unimplemented explain type: nonsense"),
4013            ("EXPLAIN (ANALYZE false) SELECT 1", "ExplainOption"),
4014            ("EXPLAIN INSERT INTO t VALUES (1)", "InsertStatement"),
4015            ("EXPLAIN CREATE TABLE u (a INTEGER)", "CreateStatement"),
4016        ] {
4017            let error = parse_ast(query).expect_err(query).to_string();
4018            assert!(error.contains(named), "{query}: {error}");
4019        }
4020    }
4021
4022    #[test]
4023    fn a_set_keeps_its_name_its_scope_and_its_value() {
4024        assert_eq!(round_statement("SET memory_limit = '1GB'"), "SET memory_limit = '1GB'");
4025        assert_eq!(round_statement("set threads=4"), "SET threads = 4");
4026        assert_eq!(round_statement("SET GLOBAL threads = 4"), "SET GLOBAL threads = 4");
4027        assert_eq!(round_statement("SET SESSION threads = 4"), "SET SESSION threads = 4");
4028        assert_eq!(round_statement("SET LOCAL threads = 4"), "SET LOCAL threads = 4");
4029        assert_eq!(round_statement("RESET memory_limit"), "RESET memory_limit");
4030        assert_eq!(round_statement("RESET GLOBAL memory_limit"), "RESET GLOBAL memory_limit");
4031        assert_eq!(
4032            round_statement("SET TIME ZONE 'Asia/Kathmandu'"),
4033            "SET TimeZone = 'Asia/Kathmandu'"
4034        );
4035        assert_eq!(round_statement("SET TIME ZONE UTC"), "SET TimeZone = 'UTC'");
4036        assert_eq!(round_statement("SET TIME ZONE DEFAULT"), "RESET TimeZone");
4037        assert_eq!(round_statement("SET TIME ZONE LOCAL"), "RESET TimeZone");
4038    }
4039
4040    #[test]
4041    fn the_two_other_things_the_word_set_starts_are_refused_rather_than_read_as_settings() {
4042        // `SET VARIABLE x = 1` declares a session variable and `SET SCHEMA` picks where an
4043        // unqualified name is looked up. Neither is a knob on the engine and reading either as one
4044        // would change an answer quietly.
4045        for statement in ["SET VARIABLE x = 1", "SET SCHEMA 'main'"] {
4046            let error = parse_ast(statement).expect_err(statement);
4047            assert_eq!(error.code().duckdb_name(), "Not implemented Error", "{statement}");
4048        }
4049    }
4050
4051    #[test]
4052    fn a_setting_written_with_a_list_of_values_is_refused_rather_than_taking_the_first() {
4053        let error = parse_ast("SET search_path = a, b").expect_err("a list of two");
4054        assert_eq!(error.code().duckdb_name(), "Not implemented Error");
4055    }
4056
4057    #[test]
4058    fn the_query_m0_has_to_run_transforms() {
4059        assert_eq!(round("SELECT * FROM t WHERE x > 5"), "SELECT * FROM t WHERE (x Gt 5)");
4060    }
4061
4062    #[test]
4063    fn a_replace_list_rides_on_the_star_it_changes() {
4064        // The parentheses are optional around a single entry, which is how the clickbench load
4065        // recipe is not written but is how a lot of hand written sql is.
4066        assert_eq!(
4067            round("SELECT * REPLACE (a + 1 AS a) FROM t"),
4068            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
4069        );
4070        assert_eq!(
4071            round("SELECT * REPLACE a + 1 AS a FROM t"),
4072            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
4073        );
4074        assert_eq!(
4075            round("SELECT t.* REPLACE (make_date(a) AS a, b * 2 AS b) FROM t"),
4076            "SELECT t.* REPLACE (make_date(a) AS a, (b Multiply 2) AS b) FROM t"
4077        );
4078    }
4079
4080    #[test]
4081    fn one_column_cannot_be_replaced_twice() {
4082        // Caught here rather than in the binder because it is a mistake in what was written and
4083        // not a mistake about what is in the table, and duckdb reports it the same way.
4084        let error = parse_ast("SELECT * REPLACE (a + 1 AS a, a + 2 AS A) FROM t").unwrap_err();
4085        assert_eq!(error.to_string(), "Parser Error: Duplicate entry \"A\" in REPLACE list");
4086    }
4087
4088    #[test]
4089    fn a_table_function_argument_can_have_a_name_written_in_front_of_it() {
4090        // The grammar has `:=` and `=>`. It does not have `=`, which parses as a comparison and is
4091        // read back apart here, and that is the spelling the clickbench load recipe uses.
4092        for spelling in
4093            ["binary_as_string := True", "binary_as_string => True", "binary_as_string = True"]
4094        {
4095            assert_eq!(
4096                round(&format!("SELECT * FROM read_parquet('f.parquet', {spelling})")),
4097                "SELECT * FROM read_parquet('f.parquet', binary_as_string := TRUE)",
4098                "{spelling}"
4099            );
4100        }
4101    }
4102
4103    #[test]
4104    fn an_equality_that_is_not_a_bare_name_stays_an_argument() {
4105        // A qualified name on the left is not a parameter name, and neither is anything that is
4106        // not a name at all, so both of those stay the comparison they were written as.
4107        assert_eq!(round("SELECT * FROM f(t.a = 1)"), "SELECT * FROM f((t.a Eq 1))");
4108        assert_eq!(round("SELECT * FROM f(1 = 1)"), "SELECT * FROM f((1 Eq 1))");
4109    }
4110
4111    #[test]
4112    fn a_create_table_keeps_its_types_as_text() {
4113        assert_eq!(
4114            round_statement("CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"),
4115            "CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"
4116        );
4117        // The type is the text between the identifier and whatever follows it, parentheses and
4118        // all, because resolving `DECIMAL(18, 3)` into a width and a scale is the binder's job and
4119        // doing it here would mean two places that know the type table.
4120        assert_eq!(
4121            round_statement("CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"),
4122            "CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"
4123        );
4124    }
4125
4126    #[test]
4127    fn the_modifiers_on_a_create_table_survive() {
4128        assert_eq!(
4129            round_statement("CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"),
4130            "CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"
4131        );
4132        assert_eq!(
4133            round_statement("CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"),
4134            "CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"
4135        );
4136    }
4137
4138    #[test]
4139    fn or_replace_and_if_not_exists_in_one_statement_is_refused_here_and_not_later() {
4140        // The grammar has room for both and duckdb's has not, so its refusal is a parser error with
4141        // a caret under the `NOT` and this one is a parser error at the same stage. It is the same
4142        // sentence whatever is being created.
4143        for sql in [
4144            "CREATE OR REPLACE TABLE IF NOT EXISTS t (a INT)",
4145            "CREATE OR REPLACE VIEW IF NOT EXISTS v AS SELECT 1",
4146        ] {
4147            let error = parse_ast(sql).unwrap_err().to_string();
4148            assert_eq!(
4149                error,
4150                "Parser Error: Cannot specify both OR REPLACE and IF NOT EXISTS within single \
4151                 create statement"
4152            );
4153        }
4154    }
4155
4156    #[test]
4157    fn a_create_table_as_carries_the_query_and_not_the_types() {
4158        assert_eq!(
4159            round_statement("CREATE TABLE t AS SELECT a FROM u"),
4160            "CREATE TABLE t AS SELECT a FROM u"
4161        );
4162        // The names are the syntax's to say and the types are the query's, so the column
4163        // definitions here have names and no types.
4164        assert_eq!(
4165            round_statement("CREATE TABLE t (x, y) AS SELECT a, b FROM u"),
4166            "CREATE TABLE t (x, y) AS SELECT a, b FROM u"
4167        );
4168    }
4169
4170    #[test]
4171    fn a_create_view_carries_its_body_twice_over() {
4172        assert_eq!(
4173            round_statement("CREATE VIEW v AS SELECT a FROM u"),
4174            "CREATE VIEW v AS SELECT a FROM u"
4175        );
4176        assert_eq!(
4177            round_statement("CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"),
4178            "CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"
4179        );
4180        // The text the catalog keeps is the body and only the body, so that binding it again is
4181        // binding a query rather than a `CREATE` statement.
4182        let ast = parse_ast("CREATE VIEW v (x) AS SELECT a FROM u WHERE a > 1").expect("parses");
4183        let Statement::CreateView(index) = ast.statements[0] else {
4184            panic!("not a create view");
4185        };
4186        assert_eq!(ast.string(ast.create_view(index).sql), "SELECT a FROM u WHERE a > 1");
4187    }
4188
4189    #[test]
4190    fn a_drop_view_is_not_a_drop_table() {
4191        assert_eq!(round_statement("DROP VIEW IF EXISTS a, b"), "DROP VIEW IF EXISTS a, b");
4192        assert_eq!(round_statement("DROP TABLE a"), "DROP TABLE a");
4193    }
4194
4195    #[test]
4196    fn a_drop_table_is_a_list_of_qualified_names() {
4197        assert_eq!(round_statement("DROP TABLE t"), "DROP TABLE t");
4198        assert_eq!(round_statement("DROP TABLE IF EXISTS a, b.c"), "DROP TABLE IF EXISTS a, b.c");
4199    }
4200
4201    #[test]
4202    fn dropping_something_that_is_neither_a_table_nor_a_view_is_refused() {
4203        // `TableOrView` covers `MATERIALIZED VIEW` as well, which is not a thing this database has,
4204        // and dropping one as if it were an ordinary view is a wrong answer rather than a missing
4205        // feature.
4206        let error = parse_ast("DROP MATERIALIZED VIEW v").unwrap_err().to_string();
4207        assert!(error.starts_with("Not implemented Error"), "{error}");
4208    }
4209
4210    #[test]
4211    fn both_spellings_of_insert_arrive_at_a_query() {
4212        assert_eq!(
4213            round_statement("INSERT INTO t VALUES (1, 'a'), (2, 'b')"),
4214            "INSERT INTO t VALUES (1, 'a'), (2, 'b')"
4215        );
4216        assert_eq!(
4217            round_statement("INSERT INTO t (a, b) SELECT x, y FROM u"),
4218            "INSERT INTO t (a, b) SELECT x, y FROM u"
4219        );
4220    }
4221
4222    #[test]
4223    fn an_insert_clause_that_changes_the_answer_is_refused() {
4224        for query in [
4225            "INSERT INTO t VALUES (1) RETURNING *",
4226            "INSERT OR REPLACE INTO t VALUES (1)",
4227            "INSERT INTO t BY NAME SELECT 1 AS a",
4228            "INSERT INTO t VALUES (1) ON CONFLICT DO NOTHING",
4229            "INSERT INTO t DEFAULT VALUES",
4230        ] {
4231            let error = parse_ast(query).unwrap_err().to_string();
4232            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
4233        }
4234    }
4235
4236    #[test]
4237    fn a_column_constraint_that_is_not_not_null_is_refused() {
4238        // Nothing enforces a constraint yet. Accepting one and not enforcing it is the wrong
4239        // answer, so `NOT NULL` is kept because the column already has a nullability and the rest
4240        // are refused until there is somewhere to put them.
4241        for query in [
4242            "CREATE TABLE t (a INT PRIMARY KEY)",
4243            "CREATE TABLE t (a INT UNIQUE)",
4244            "CREATE TABLE t (a INT CHECK (a > 0))",
4245            "CREATE TABLE t (a INT DEFAULT 1)",
4246            "CREATE TABLE t (a INT REFERENCES u (b))",
4247            "CREATE TABLE t (a INT, PRIMARY KEY (a))",
4248        ] {
4249            let error = parse_ast(query).unwrap_err().to_string();
4250            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
4251        }
4252    }
4253
4254    #[test]
4255    fn values_is_a_query_on_its_own_and_in_a_from() {
4256        assert_eq!(round("VALUES (1), (2)"), "VALUES (1), (2)");
4257        // Parenthesised it is a subquery whose body is the values, and bare it is a `ValuesRef`.
4258        // Two rules and one meaning, which is the grammar's doing and not something to flatten
4259        // here, because the parenthesised form can carry an order by and the bare one cannot.
4260        assert_eq!(
4261            round("SELECT * FROM (VALUES (1, 2), (3, 4)) t(a, b)"),
4262            "SELECT * FROM (VALUES (1, 2), (3, 4)) AS t"
4263        );
4264        assert_eq!(
4265            round("SELECT * FROM VALUES (1, 2), (3, 4) AS t(a, b)"),
4266            "SELECT * FROM VALUES (1, 2), (3, 4) AS t"
4267        );
4268        // Rows of different widths parse. Saying so wants the column count, which for an insert is
4269        // the table's, so the check belongs to the binder and not here.
4270        assert_eq!(round("VALUES (1), (2, 3)"), "VALUES (1), (2, 3)");
4271    }
4272
4273    #[test]
4274    fn non_recursive_ctes_inline_and_semantic_variants_are_explicit() {
4275        assert_eq!(
4276            round("WITH t AS (SELECT 1 AS x) SELECT x FROM t"),
4277            "SELECT x FROM (SELECT 1 AS x) AS t"
4278        );
4279        assert_eq!(
4280            round("WITH t(x) AS NOT MATERIALIZED (SELECT 1) SELECT x FROM t"),
4281            "SELECT x FROM (SELECT 1) AS t"
4282        );
4283        let query = "WITH RECURSIVE t(x) AS (SELECT 1) SELECT x FROM t";
4284        let error = parse_ast(query).expect_err("the unsupported CTE shape is refused");
4285        assert!(error.to_string().starts_with("Not implemented Error"), "{query}: {error}");
4286    }
4287
4288    /// A plain definition named twice is held, and the same one named once is not.
4289    ///
4290    /// Inlining a definition that two places read means running it twice, so the rule is the count
4291    /// of reads and the word written only settles the cases where somebody wrote one. `NOT
4292    /// MATERIALIZED` is the one that says inline it anyway, and it says so however many times the
4293    /// name is read.
4294    #[test]
4295    fn a_plain_cte_read_twice_is_held_and_one_read_once_is_inlined() {
4296        assert_eq!(
4297            round("WITH t AS (SELECT 1 AS x) SELECT * FROM t a, t b"),
4298            "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT * FROM t AS a, t AS b"
4299        );
4300        assert_eq!(
4301            round("WITH t AS (SELECT 1 AS x) SELECT x FROM t"),
4302            "SELECT x FROM (SELECT 1 AS x) AS t"
4303        );
4304        assert_eq!(
4305            round("WITH t AS NOT MATERIALIZED (SELECT 1 AS x) SELECT * FROM t a, t b"),
4306            "SELECT * FROM (SELECT 1 AS x) AS a, (SELECT 1 AS x) AS b"
4307        );
4308        // A name a later definition reads is read, since that definition runs too.
4309        assert_eq!(
4310            round("WITH t AS (SELECT 1 AS x), u AS (SELECT x FROM t) SELECT x FROM t"),
4311            "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT x FROM t"
4312        );
4313        // Qualified, so it is not a read of the definition and there is only the one.
4314        assert_eq!(
4315            round("WITH t AS (SELECT 1 AS x) SELECT * FROM t a, main.t b"),
4316            "SELECT * FROM (SELECT 1 AS x) AS a, main.t AS b"
4317        );
4318    }
4319
4320    /// A definition written inside a subquery is inlined however many times it is read.
4321    ///
4322    /// The rows of a held definition are produced once for the whole statement, and a definition
4323    /// written inside a subquery can name a column of the query around it, which is an answer per
4324    /// outer row. Telling the two apart is a question about resolved columns, so what is asked here
4325    /// is the question this pass can answer: whether there is any query around it at all.
4326    #[test]
4327    fn a_cte_written_inside_a_subquery_is_inlined_however_often_it_is_read() {
4328        assert_eq!(
4329            round("SELECT * FROM (WITH t AS (SELECT 1 AS x) SELECT * FROM t a, t b) c"),
4330            "SELECT * FROM (SELECT * FROM (SELECT 1 AS x) AS a, (SELECT 1 AS x) AS b) AS c"
4331        );
4332        assert_eq!(
4333            round("WITH o AS (WITH i AS (SELECT 1 AS x) SELECT * FROM i a, i b) SELECT * FROM o"),
4334            "SELECT * FROM (SELECT * FROM (SELECT 1 AS x) AS a, (SELECT 1 AS x) AS b) AS o"
4335        );
4336    }
4337
4338    /// A name a definition further in takes over is left alone.
4339    ///
4340    /// Which of the two definitions a read means is a question about scopes, and the count here is
4341    /// a count of spellings, so a query that writes the name twice gets what every query got before
4342    /// the count existed.
4343    #[test]
4344    fn a_plain_cte_whose_name_is_written_again_further_in_is_inlined() {
4345        assert_eq!(
4346            round(
4347                "WITH t AS (SELECT 1 AS x) SELECT * FROM t a, \
4348                 (WITH t AS (SELECT 2 AS x) SELECT x FROM t) b"
4349            ),
4350            "SELECT * FROM (SELECT 1 AS x) AS a, (SELECT x FROM (SELECT 2 AS x) AS t) AS b"
4351        );
4352    }
4353
4354    /// A materialised one keeps its definition, because putting it in two places runs it twice.
4355    #[test]
4356    fn a_materialized_cte_stays_a_definition_and_its_references_stay_references() {
4357        assert_eq!(
4358            round("WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT x FROM t"),
4359            "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT x FROM t"
4360        );
4361        assert_eq!(
4362            round("WITH t(y) AS MATERIALIZED (SELECT 1) SELECT y FROM t"),
4363            "WITH t(y) AS MATERIALIZED (SELECT 1) SELECT y FROM t"
4364        );
4365        // Two references are two sources naming one definition, which is the whole point of the
4366        // word: the inlined form above would be two copies of the query.
4367        assert_eq!(
4368            round("WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT * FROM t a, t b"),
4369            "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT * FROM t AS a, t AS b"
4370        );
4371        // The inner name shadows the outer one, which is decided here and nowhere later.
4372        assert_eq!(
4373            round(
4374                "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT (WITH t AS (SELECT 2 AS x) \
4375                 SELECT x FROM t) AS inner"
4376            ),
4377            "WITH t AS MATERIALIZED (SELECT 1 AS x) SELECT (SELECT x FROM (SELECT 2 AS x) AS t) \
4378             AS inner"
4379        );
4380        // A definition may read one written before it, and it is the definition that is read
4381        // rather than a second copy of the query behind it.
4382        assert_eq!(
4383            round(
4384                "WITH a AS MATERIALIZED (SELECT 1 AS x), b AS MATERIALIZED (SELECT x + 1 AS y \
4385                 FROM a) SELECT y FROM b"
4386            ),
4387            "WITH a AS MATERIALIZED (SELECT 1 AS x) WITH b AS MATERIALIZED (SELECT (x Add 1) \
4388             AS y FROM a) SELECT y FROM b"
4389        );
4390    }
4391
4392    /// `DESCRIBE` is a query body, and the two spellings that name something become a star over it.
4393    ///
4394    /// Naming a table is not a shortcut for the query. On the reference binary `DESCRIBE t` and
4395    /// `DESCRIBE SELECT * FROM t` print the same six columns and the same rows, down to the `NO` on
4396    /// a column that refuses nulls, so rewriting one into the other costs nothing and leaves the
4397    /// binder with one case instead of three. A file name goes down the same path as a table name
4398    /// because a bare string in a `FROM` clause is already a name the replacement scan picks up.
4399    #[test]
4400    fn describe_rewrites_a_name_into_a_star_over_it() {
4401        assert_eq!(round("DESCRIBE SELECT 1 AS a"), "DESCRIBE SELECT 1 AS a");
4402        assert_eq!(round("DESCRIBE t"), "DESCRIBE SELECT * FROM t");
4403        assert_eq!(round("DESC t"), "DESCRIBE SELECT * FROM t");
4404        assert_eq!(round("DESCRIBE 'x.parquet'"), "DESCRIBE SELECT * FROM x.parquet");
4405        // A body and not a statement kind, so it nests both ways with no rule of its own.
4406        assert_eq!(
4407            round("SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"),
4408            "SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"
4409        );
4410        assert_eq!(round("DESCRIBE DESCRIBE SELECT 1 AS a"), "DESCRIBE DESCRIBE SELECT 1 AS a");
4411    }
4412
4413    /// `SUMMARIZE` shares both of `DESCRIBE`'s grammar rules and is a different statement.
4414    ///
4415    /// It reads every row and returns one row per column carrying the min, the max, the count and
4416    /// the approximate distinct count, so none of it falls out of the `DESCRIBE` path. The word is
4417    /// the only thing in the tree that tells the two apart, which is why the transform looks at it
4418    /// rather than trusting the rule name it arrived under.
4419    #[test]
4420    fn summarize_is_refused_even_though_it_parses_as_a_describe() {
4421        for query in ["SUMMARIZE t", "SUMMARIZE SELECT 1"] {
4422            let error = parse_ast(query).expect_err("summarize is not implemented");
4423            let message = error.to_string();
4424            assert!(message.starts_with("Not implemented Error"), "{query} failed with {message}");
4425        }
4426    }
4427
4428    #[test]
4429    fn every_statement_in_the_corpus_gets_a_defined_answer() {
4430        // The point of the test is the word defined. Half of these are statement kinds and
4431        // clauses this milestone does not cover, and the requirement is not that they work, it is
4432        // that they fail by saying so. A panic, a silently dropped clause or an internal error
4433        // would each be a different bug and all three would be invisible without this.
4434        let mut done = 0;
4435        for query in CORPUS {
4436            match parse_ast(query) {
4437                Ok(ast) => {
4438                    assert_eq!(ast.statements.len(), 1, "{query}");
4439                    done += 1;
4440                }
4441                Err(error) => {
4442                    let message = error.to_string();
4443                    assert!(
4444                        message.starts_with("Not implemented Error"),
4445                        "{query} failed with {message}, which is not a not-implemented error"
4446                    );
4447                }
4448            }
4449        }
4450        // Not an assertion about the right number. It is a ratchet: this only moves up, and the
4451        // day it moves down somebody has taken a construct out without meaning to.
4452        assert!(done >= 31, "only {done} of the corpus transforms, which is fewer than it was");
4453    }
4454
4455    #[test]
4456    fn the_ast_is_far_smaller_than_the_parse_tree() {
4457        let query = CORPUS[4];
4458        let tree = parse(query).unwrap();
4459        let ast = parse_ast(query).unwrap();
4460        // The twenty precedence levels are the difference. Every one of them is a node in the
4461        // parse tree for every expression at every depth, and none of them survives into the AST.
4462        assert!(
4463            ast.node_count() * 20 < tree.arena_len(),
4464            "{} ast nodes against {} parse nodes",
4465            ast.node_count(),
4466            tree.arena_len()
4467        );
4468    }
4469
4470    #[test]
4471    fn precedence_comes_out_of_the_chain_and_into_the_tree() {
4472        assert_eq!(round("SELECT 1 + 2 * 3"), "SELECT (1 Add (2 Multiply 3))");
4473        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
4474        assert_eq!(round("SELECT 1 + 2 + 3"), "SELECT ((1 Add 2) Add 3)");
4475        assert_eq!(round("SELECT 1 - 2 - 3"), "SELECT ((1 Subtract 2) Subtract 3)");
4476        assert_eq!(
4477            round("SELECT a OR b AND c"),
4478            "SELECT (a Or (b And c))",
4479            "and binds tighter than or"
4480        );
4481    }
4482
4483    #[test]
4484    fn a_double_negation_is_two_nodes_and_not_none() {
4485        // Folding it would be an optimizer decision and this is not the optimizer. It also would
4486        // not be safe in general: `NOT NOT x` on a null is still null and on a non boolean it is
4487        // still an error, and both of those have to survive to the binder to be reported.
4488        assert_eq!(round("SELECT NOT NOT a"), "SELECT (Not (Not a))");
4489    }
4490
4491    #[test]
4492    fn a_parenthesised_single_expression_is_not_a_row() {
4493        assert_eq!(round("SELECT (a)"), "SELECT a");
4494        assert_eq!(round("SELECT (a, b)"), "SELECT ROW(a, b)");
4495    }
4496
4497    #[test]
4498    fn a_bracketed_list_is_a_list_of_however_many_items_were_written() {
4499        // One item is a list of one, which is where this parts company with the parenthesised form
4500        // above: `(a)` is `a` and `[a]` is a list, because the brackets are what say list.
4501        assert_eq!(round("SELECT [a]"), "SELECT [a]");
4502        assert_eq!(round("SELECT [1, 2, 3]"), "SELECT [1, 2, 3]");
4503        assert_eq!(round("SELECT []"), "SELECT []");
4504        assert_eq!(round("SELECT ['a.parquet', 'b.parquet']"), "SELECT ['a.parquet', 'b.parquet']");
4505    }
4506
4507    #[test]
4508    fn a_parameter_carries_its_identifier_however_it_was_written() {
4509        assert_eq!(round("SELECT $1"), "SELECT $1");
4510        assert_eq!(round("SELECT ?1"), "SELECT $1");
4511        assert_eq!(round("SELECT $name"), "SELECT $name");
4512        // A bare question mark is numbered by where it is, and the counting is its own, so a later
4513        // `$2` does not push the first one along. This is duckdb v1.4.1, which prints `$1 + $2`.
4514        assert_eq!(round("SELECT ? + $2"), "SELECT ($1 Add $2)");
4515        assert_eq!(round("SELECT ?, ?, ?"), "SELECT $1, $2, $3");
4516    }
4517
4518    #[test]
4519    fn the_parameters_of_a_statement_are_listed_once_each_in_written_order() {
4520        let ast = parse_ast("SELECT $b, $a, $b WHERE $a").expect("parses");
4521        assert_eq!(ast.parameters(), vec!["b", "a"]);
4522        assert!(parse_ast("SELECT 1").expect("parses").parameters().is_empty());
4523    }
4524
4525    #[test]
4526    fn the_three_ways_to_write_an_alias_all_arrive() {
4527        assert_eq!(round("SELECT a AS b"), "SELECT a AS b");
4528        assert_eq!(round("SELECT a b"), "SELECT a AS b");
4529        assert_eq!(round("SELECT b: a"), "SELECT a AS b");
4530        assert_eq!(round("SELECT a"), "SELECT a", "and no alias when none was written");
4531    }
4532
4533    #[test]
4534    fn a_from_with_no_select_selects_everything() {
4535        // DuckDB's own shorthand. Inventing the star here rather than in the binder means the
4536        // binder never has to know that the clause it is looking at was the one that was missing.
4537        assert_eq!(round("FROM t"), "SELECT * FROM t");
4538        assert_eq!(round("FROM t SELECT a"), "SELECT a FROM t");
4539    }
4540
4541    #[test]
4542    fn joins_nest_to_the_left() {
4543        assert_eq!(
4544            round("SELECT * FROM a JOIN b ON a.i = b.i LEFT JOIN c USING (k)"),
4545            "SELECT * FROM ((a Inner JOIN b ON (a.i Eq b.i)) Left JOIN c USING (k))"
4546        );
4547        assert_eq!(
4548            round("SELECT * FROM a NATURAL JOIN b"),
4549            "SELECT * FROM (a NATURAL Inner JOIN b)"
4550        );
4551        assert_eq!(round("SELECT * FROM a CROSS JOIN b"), "SELECT * FROM (a Cross JOIN b)");
4552        assert_eq!(
4553            round("SELECT * FROM a POSITIONAL JOIN b"),
4554            "SELECT * FROM (a Positional JOIN b)"
4555        );
4556        assert_eq!(round("SELECT * FROM a, b"), "SELECT * FROM a, b", "a comma is not a join node");
4557    }
4558
4559    #[test]
4560    fn a_qualified_name_keeps_its_parts_however_it_was_spelled() {
4561        // Five grammar rules can produce a column reference and they disagree about which
4562        // component is a schema and which is a table. None of that is decidable without the
4563        // catalog, so the AST holds the parts and the binder decides.
4564        assert_eq!(round("SELECT a"), "SELECT a");
4565        assert_eq!(round("SELECT t.a"), "SELECT t.a");
4566        assert_eq!(round("SELECT s.t.a"), "SELECT s.t.a");
4567        assert_eq!(round("SELECT c.s.t.a"), "SELECT c.s.t.a");
4568        assert_eq!(round("SELECT * FROM s.t"), "SELECT * FROM s.t");
4569    }
4570
4571    #[test]
4572    fn a_star_can_be_qualified() {
4573        assert_eq!(round("SELECT *"), "SELECT *");
4574        assert_eq!(round("SELECT t.*"), "SELECT t.*");
4575        assert_eq!(round("SELECT s.t.*"), "SELECT s.t.*");
4576    }
4577
4578    #[test]
4579    fn a_quoted_identifier_keeps_its_case_and_loses_its_quotes() {
4580        // DuckDB does not fold identifier case at any point, quoted or not, which the tokenizer
4581        // work established by reading the source. So the only thing to do here is take the quotes
4582        // off and resolve the doubled ones.
4583        let ast = parse_ast("SELECT \"Mixed Case\", \"a\"\"b\"").unwrap();
4584        assert_eq!(ast.strings[0], "Mixed Case");
4585        assert_eq!(ast.strings[1], "a\"b");
4586    }
4587
4588    #[test]
4589    fn a_string_literal_is_decoded_and_adjacent_ones_are_joined() {
4590        assert_eq!(round("SELECT 'it''s'"), "SELECT 'it's'");
4591        assert_eq!(round("SELECT 'a'\n'b'"), "SELECT 'ab'", "the standard's adjacency rule");
4592    }
4593
4594    /// Per #276, where the tag and the dollars were coming through as part of the value.
4595    #[test]
4596    fn a_dollar_quoted_string_loses_its_dollars_and_its_tag() {
4597        assert_eq!(round("SELECT $$dollar quoted$$"), "SELECT 'dollar quoted'");
4598        assert_eq!(round("SELECT $tag$body$tag$"), "SELECT 'body'");
4599        assert_eq!(round("SELECT $$$$"), "SELECT ''", "an empty tag and an empty body");
4600        // Nothing in the body is escaped, which is what the spelling is for, so a quote is a quote
4601        // and a dollar that is not the closing tag is a dollar.
4602        assert_eq!(round("SELECT $tag$it''s $other$ fine$tag$"), "SELECT 'it''s $other$ fine'");
4603        // An unterminated one has no closing tag to take off and keeps every byte it was given.
4604        assert_eq!(round("SELECT $$open"), "SELECT '$$open'");
4605    }
4606
4607    /// Per #329, where every prefixed spelling came back as the source text it was written as.
4608    ///
4609    /// The escapes are the ones the pinned binary takes, read off it one at a time. The two that
4610    /// are easy to get wrong are `\v`, which is not an escape and is the letter, and `\u`, which
4611    /// wants all four digits and otherwise drops the backslash and keeps the letter.
4612    #[test]
4613    fn an_escape_string_resolves_its_backslashes() {
4614        assert_eq!(round("SELECT E'a\\nb'"), "SELECT 'a\nb'");
4615        assert_eq!(round("SELECT e'a\\tb'"), "SELECT 'a\tb'", "the prefix is a letter, not a name");
4616        assert_eq!(round("SELECT E'a\\rb'"), "SELECT 'a\rb'");
4617        assert_eq!(round("SELECT E'a\\bb'"), "SELECT 'a\u{8}b'");
4618        assert_eq!(round("SELECT E'a\\fb'"), "SELECT 'a\u{c}b'");
4619        assert_eq!(round("SELECT E'a\\\\b'"), "SELECT 'a\\b'");
4620        assert_eq!(round("SELECT E'a\\'b'"), "SELECT 'a'b'", "a quote, the same as ''");
4621        assert_eq!(round("SELECT E'a''b'"), "SELECT 'a'b'", "and '' still means a quote here");
4622        // A backslash in front of anything else is dropped and the character is kept, which is what
4623        // makes \v the letter v.
4624        assert_eq!(round("SELECT E'a\\vb'"), "SELECT 'avb'");
4625        assert_eq!(round("SELECT E'a\\qb'"), "SELECT 'aqb'");
4626    }
4627
4628    /// The escapes that write a byte rather than a character, and the one that writes a character.
4629    #[test]
4630    fn a_numeric_escape_writes_the_byte_or_the_character_it_names() {
4631        assert_eq!(round("SELECT E'\\x41'"), "SELECT 'A'");
4632        assert_eq!(round("SELECT E'\\x4142'"), "SELECT 'A42'", "two digits at the most");
4633        assert_eq!(
4634            round("SELECT E'a\\x'"),
4635            "SELECT 'ax'",
4636            "and one at the least, or it is a letter"
4637        );
4638        assert_eq!(round("SELECT E'\\101'"), "SELECT 'A'");
4639        assert_eq!(round("SELECT E'\\1011'"), "SELECT 'A1'", "three digits at the most");
4640        assert_eq!(round("SELECT E'\\8'"), "SELECT '8'", "8 is not an octal digit");
4641        // Bytes and not characters, so two of them make one character and one of them makes none.
4642        assert_eq!(round("SELECT E'\\xc3\\xa9'"), "SELECT 'é'");
4643        assert_eq!(round("SELECT E'\\u00e9'"), "SELECT 'é'");
4644        assert_eq!(round("SELECT E'a\\u41'"), "SELECT 'au41'", "four digits or it is a letter");
4645        assert_eq!(round("SELECT E'a\\uZZZZ'"), "SELECT 'auZZZZ'");
4646        assert_eq!(
4647            round("SELECT E'\\ud83d\\ude00'"),
4648            "SELECT 'ud83dude00'",
4649            "surrogates are not it"
4650        );
4651    }
4652
4653    /// The two ways an escape string is not a string at all, both with the message upstream gives.
4654    #[test]
4655    fn an_escape_string_that_is_not_a_string_raises() {
4656        let error = parse_ast("SELECT E'a\\x00'").unwrap_err().to_string();
4657        assert_eq!(error, "Parser Error: Null character not permitted in escape string literal");
4658        let error = parse_ast("SELECT E'a\\377'").unwrap_err().to_string();
4659        assert_eq!(
4660            error,
4661            "Parser Error: Invalid UTF-8 in escape string literal at byte offset 1: byte mismatch",
4662            "the offset is where the bytes stop being a string, not where the escape was written"
4663        );
4664    }
4665
4666    /// The other prefixes, all of them measured against the pinned binary rather than assumed.
4667    #[test]
4668    fn the_other_string_prefixes_are_what_upstream_makes_of_them() {
4669        // N is the string and a cast of it to VARCHAR, which is where the column name comes from.
4670        assert_eq!(round("SELECT N'abc'"), "SELECT CAST('abc' AS VARCHAR)");
4671        assert_eq!(round("SELECT n'abc'"), "SELECT CAST('abc' AS VARCHAR)");
4672        // B is not a bit string. It is the letter b in front of the body, untouched.
4673        assert_eq!(round("SELECT B'101'"), "SELECT 'b101'");
4674        assert_eq!(round("SELECT b'abc'"), "SELECT 'babc'");
4675        assert_eq!(round("SELECT B''"), "SELECT 'b'", "an empty one is the letter on its own");
4676    }
4677
4678    /// X is the prefix that is not a string at all, per #329.
4679    ///
4680    /// What is kept is the text the blob prints as, because that is the text the column is named
4681    /// after and the text the cast reads the bytes back from, and one text that does both is one
4682    /// text that cannot disagree with itself.
4683    #[test]
4684    fn a_hex_string_is_a_blob_and_not_a_string() {
4685        assert_eq!(round("SELECT x'4142'"), "SELECT 'AB'::BLOB");
4686        assert_eq!(round("SELECT X'4142'"), "SELECT 'AB'::BLOB");
4687        assert_eq!(round("SELECT x'ff41'"), "SELECT '\\xFFA'::BLOB", "a byte that does not print");
4688        assert_eq!(round("SELECT x''"), "SELECT ''::BLOB", "an empty one is an empty blob");
4689        // A quote and a backslash are bytes that do not print either, which is what keeps the text
4690        // something the cast can read back.
4691        assert_eq!(round("SELECT x'2741'"), "SELECT '\\x27A'::BLOB");
4692        assert_eq!(round("SELECT x'5c7834314141'"), "SELECT '\\x5Cx41AA'::BLOB");
4693        // An odd number of digits is a parser error and a digit that is not one is not, because
4694        // upstream writes the pairs out without looking at them and the cast is what looks.
4695        let error = parse_ast("SELECT x'4'").unwrap_err().to_string();
4696        assert_eq!(
4697            error,
4698            "Parser Error: Hex string literal must have an even number of hex digits"
4699        );
4700        assert_eq!(round("SELECT x'41zz'"), "SELECT '\\x41\\xzz'::BLOB");
4701    }
4702
4703    #[test]
4704    fn the_null_and_boolean_tests_are_postfix_unary_operators() {
4705        assert_eq!(round("SELECT x IS NULL"), "SELECT (IsNull x)");
4706        assert_eq!(round("SELECT x IS NOT NULL"), "SELECT (IsNotNull x)");
4707        assert_eq!(round("SELECT x ISNULL"), "SELECT (IsNull x)");
4708        assert_eq!(round("SELECT x NOTNULL"), "SELECT (IsNotNull x)");
4709        assert_eq!(round("SELECT x IS TRUE"), "SELECT (IsTrue x)");
4710        assert_eq!(round("SELECT x IS NOT FALSE"), "SELECT (IsNotFalse x)");
4711        assert_eq!(round("SELECT x IS DISTINCT FROM y"), "SELECT (x IsDistinctFrom y)");
4712        assert_eq!(round("SELECT x IS NOT DISTINCT FROM y"), "SELECT (x IsNotDistinctFrom y)");
4713    }
4714
4715    #[test]
4716    fn the_like_family_folds_its_negation_into_the_operator() {
4717        assert_eq!(round("SELECT x LIKE 'a'"), "SELECT (x Like 'a')");
4718        assert_eq!(round("SELECT x NOT LIKE 'a'"), "SELECT (x NotLike 'a')");
4719        assert_eq!(round("SELECT x ILIKE 'a'"), "SELECT (x ILike 'a')");
4720        assert_eq!(round("SELECT x ~~ 'a'"), "SELECT (x Like 'a')", "the operator spelling");
4721        assert_eq!(round("SELECT x !~~ 'a'"), "SELECT (x NotLike 'a')");
4722        assert_eq!(round("SELECT x SIMILAR TO 'a'"), "SELECT (x SimilarTo 'a')");
4723        // Glob has no negated operator to fold into, so the negation stays where it was written.
4724        assert_eq!(round("SELECT x NOT GLOB 'a'"), "SELECT (Not (x Glob 'a'))");
4725    }
4726
4727    #[test]
4728    fn between_and_in_carry_their_negation_as_a_flag() {
4729        assert_eq!(round("SELECT x BETWEEN 1 AND 2"), "SELECT (x BETWEEN 1 AND 2)");
4730        assert_eq!(round("SELECT x NOT BETWEEN 1 AND 2"), "SELECT (NOT x BETWEEN 1 AND 2)");
4731        assert_eq!(round("SELECT x IN (1, 2)"), "SELECT (x IN [1, 2])");
4732        assert_eq!(round("SELECT x NOT IN (1, 2)"), "SELECT (NOT x IN [1, 2])");
4733    }
4734
4735    #[test]
4736    fn both_spellings_of_a_cast_are_the_same_node() {
4737        assert_eq!(round("SELECT CAST(x AS BIGINT)"), "SELECT CAST(x AS BIGINT)");
4738        assert_eq!(round("SELECT x::BIGINT"), "SELECT CAST(x AS BIGINT)");
4739        assert_eq!(round("SELECT TRY_CAST(x AS BIGINT)"), "SELECT TRY_CAST(x AS BIGINT)");
4740        assert_eq!(
4741            round("SELECT x::DECIMAL(18, 3)"),
4742            "SELECT CAST(x AS DECIMAL(18, 3))",
4743            "the type is kept as text because parsing it is the type system's job"
4744        );
4745    }
4746
4747    #[test]
4748    fn a_typed_literal_is_a_third_spelling_of_the_same_cast() {
4749        assert_eq!(round("SELECT DATE '1995-09-01'"), "SELECT CAST('1995-09-01' AS DATE)");
4750        assert_eq!(
4751            round("SELECT date '1995-09-01'"),
4752            "SELECT CAST('1995-09-01' AS date)",
4753            "the type is kept as written, the same as it is in the other two spellings"
4754        );
4755        assert_eq!(
4756            round("SELECT TIMESTAMP '2020-01-01 03:04:05'"),
4757            "SELECT CAST('2020-01-01 03:04:05' AS TIMESTAMP)"
4758        );
4759        assert_eq!(
4760            round("SELECT DECIMAL(5, 2) '1.5'"),
4761            "SELECT CAST('1.5' AS DECIMAL(5, 2))",
4762            "any type the cast takes is a typed literal, parameters and all"
4763        );
4764        assert_eq!(
4765            round("SELECT VARCHAR 'hi' FROM t"),
4766            "SELECT CAST('hi' AS VARCHAR) FROM t",
4767            "including the ones where the cast has nothing to do"
4768        );
4769    }
4770
4771    #[test]
4772    fn a_case_keeps_its_arms_in_order() {
4773        assert_eq!(
4774            round("SELECT CASE WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"),
4775            "SELECT CASE - WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"
4776        );
4777        assert_eq!(
4778            round("SELECT CASE x WHEN 1 THEN 'a' END"),
4779            "SELECT CASE x WHEN 1 THEN 'a' ELSE - END",
4780            "a simple case keeps the operand and a missing else is not an implicit null yet"
4781        );
4782    }
4783
4784    #[test]
4785    fn a_field_access_and_a_method_call_are_ordinary_function_calls() {
4786        // Which is what DuckDB makes of them too. Giving each its own AST node would mean the
4787        // binder needs a rule for something the function resolver already handles.
4788        assert_eq!(round("SELECT (f(x)).y"), "SELECT struct_extract(f(x), 'y')");
4789        assert_eq!(round("SELECT a[1]"), "SELECT array_extract(a, 1)");
4790    }
4791
4792    /// The four ways of leaving a bound out, all of which upstream fills in the same way.
4793    #[test]
4794    fn a_range_gets_the_bounds_the_query_left_out() {
4795        assert_eq!(round("SELECT a[1:2]"), "SELECT array_slice(a, 1, 2)");
4796        assert_eq!(round("SELECT a[:2]"), "SELECT array_slice(a, 1, 2)");
4797        assert_eq!(round("SELECT a[2:]"), "SELECT array_slice(a, 2, -1)");
4798        assert_eq!(round("SELECT a[:]"), "SELECT array_slice(a, 1, -1)");
4799        // `EndSliceMinus`, which is a range with no end rather than a subtraction of nothing.
4800        assert_eq!(round("SELECT a[1:-]"), "SELECT array_slice(a, 1, -1)");
4801        assert_eq!(round("SELECT a[1:2:3]"), "SELECT array_slice(a, 1, 2, 3)");
4802        // A step that was written and left empty, which upstream fills with a list so that the call
4803        // fails to bind. Answering a row here would be answering where the reference refuses.
4804        assert_eq!(round("SELECT a[1:2:]"), "SELECT array_slice(a, 1, 2, [])");
4805    }
4806
4807    /// `[]` is the one subscript the parser takes and the transformer refuses, in upstream's words.
4808    #[test]
4809    fn an_empty_subscript_is_not_a_subscript() {
4810        let error = parse_ast("SELECT a[]").expect_err("an empty subscript");
4811        assert_eq!(error.message(), "Empty subscript '[]' is not allowed");
4812    }
4813
4814    /// A rule that wrote a keyword is not a rule that said nothing, however few children it has.
4815    /// Per #313.
4816    #[test]
4817    fn a_keyword_is_not_stepped_through_on_the_way_to_its_one_argument() {
4818        for (sql, rule) in [
4819            ("SELECT row(1)", "RowExpression"),
4820            ("SELECT try(1)", "TryExpression"),
4821            ("SELECT unpack([1])", "UnpackExpression"),
4822            ("SELECT columns('a')", "ColumnsExpression"),
4823        ] {
4824            let error = parse_ast(sql).expect_err(sql);
4825            assert!(error.message().ends_with(rule), "{sql}: {error}");
4826        }
4827        // Grouping brackets really do say nothing, and that is the one rule of this shape that is
4828        // stepped through rather than refused.
4829        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
4830        assert_eq!(round("SELECT -(7)"), "SELECT (Negate 7)");
4831    }
4832
4833    /// The three spellings of a null check, two of which are their own grammar rule. Per #306.
4834    #[test]
4835    fn the_null_checks_are_calls_by_the_names_duckdb_prints() {
4836        // The keyword is the name, so the call is written with the canonical spelling of it whichever
4837        // case the query used. What the column is called is the binder's to decide.
4838        assert_eq!(round("SELECT COALESCE(a, b, 1)"), "SELECT coalesce(a, b, 1)");
4839        assert_eq!(round("SELECT coalesce(a)"), "SELECT coalesce(a)");
4840        assert_eq!(round("SELECT NULLIF(a, 1)"), "SELECT nullif(a, 1)");
4841        // `IFNULL` is a plain call that upstream's parser turns into the operator, qualifier and all.
4842        assert_eq!(round("SELECT ifnull(a, 1)"), "SELECT coalesce(a, 1)");
4843        assert_eq!(round("SELECT main.ifnull(a, 1)"), "SELECT coalesce(a, 1)");
4844        let error = parse_ast("SELECT ifnull(a)").expect_err("one argument to ifnull");
4845        assert_eq!(error.message(), "Wrong number of arguments to IFNULL.");
4846        let error = parse_ast("SELECT ifnull(a, b, c)").expect_err("three arguments to ifnull");
4847        assert_eq!(error.message(), "Wrong number of arguments to IFNULL.");
4848    }
4849
4850    /// The four string functions with a grammar rule of their own, written back out as the calls
4851    /// DuckDB's parser writes them as. Per #314.
4852    #[test]
4853    fn the_string_keywords_are_the_calls_duckdb_prints() {
4854        assert_eq!(round("SELECT substring(s, 2, 3)"), "SELECT substring(s, 2, 3)");
4855        assert_eq!(round("SELECT SUBSTRING(s FROM 2 FOR 3)"), "SELECT substring(s, 2, 3)");
4856        assert_eq!(round("SELECT substring(s FROM 2)"), "SELECT substring(s, 2)");
4857        // The `FOR` on its own is three arguments and not two, with the start filled in.
4858        assert_eq!(round("SELECT substring(s FOR 3)"), "SELECT substring(s, 1, 3)");
4859        // The haystack comes first in the call and second in the query.
4860        assert_eq!(round("SELECT position('c' IN s)"), "SELECT position(s, 'c')");
4861        assert_eq!(round("SELECT trim(s)"), "SELECT trim(s)");
4862        assert_eq!(round("SELECT trim(BOTH 'x' FROM s)"), "SELECT trim(s, 'x')");
4863        assert_eq!(round("SELECT trim(BOTH FROM s)"), "SELECT trim(s)");
4864        assert_eq!(round("SELECT trim(s, 'xy')"), "SELECT trim(s, 'xy')");
4865        // A direction is a different function and not a different argument.
4866        assert_eq!(round("SELECT trim(LEADING FROM s)"), "SELECT ltrim(s)");
4867        assert_eq!(round("SELECT trim(TRAILING FROM s)"), "SELECT rtrim(s)");
4868        assert_eq!(round("SELECT trim(LEADING 'x' FROM s)"), "SELECT ltrim(s, 'x')");
4869        assert_eq!(round("SELECT trim(TRAILING 'x' FROM s)"), "SELECT rtrim(s, 'x')");
4870        assert_eq!(
4871            round("SELECT overlay(s PLACING 'X' FROM 2 FOR 1)"),
4872            "SELECT overlay(s, 'X', 2, 1)"
4873        );
4874        assert_eq!(round("SELECT overlay(s PLACING 'X' FROM 2)"), "SELECT overlay(s, 'X', 2)");
4875        assert_eq!(round("SELECT overlay(s, 'X', 2, 1)"), "SELECT overlay(s, 'X', 2, 1)");
4876    }
4877
4878    #[test]
4879    fn an_aggregate_keeps_its_distinct() {
4880        assert_eq!(round("SELECT count(*)"), "SELECT count(*)");
4881        assert_eq!(round("SELECT count(DISTINCT x)"), "SELECT count(DISTINCT x)");
4882        assert_eq!(round("SELECT count(ALL x)"), "SELECT count(x)");
4883        assert_eq!(round("SELECT main.count(x)"), "SELECT main.count(x)");
4884    }
4885
4886    #[test]
4887    fn a_call_keeps_the_filter_it_was_written_with_and_the_word_where_is_optional() {
4888        // `FilterClauseContents <- 'WHERE'? Expression`, so both spellings parse and both land on
4889        // the same predicate. Which names are allowed to carry one is not a question the parser
4890        // can answer, so it keeps one wherever it was written and lets the binder refuse it.
4891        assert_eq!(round("SELECT sum(x) FILTER (WHERE y > 1)"), "SELECT sum(x) FILTER [(y Gt 1)]");
4892        assert_eq!(round("SELECT sum(x) FILTER (y > 1)"), "SELECT sum(x) FILTER [(y Gt 1)]");
4893        assert_eq!(round("SELECT count(*) FILTER (WHERE b)"), "SELECT count(*) FILTER [b]");
4894        assert_eq!(
4895            round("SELECT sum(DISTINCT x) FILTER (WHERE b)"),
4896            "SELECT sum(DISTINCT x) FILTER [b]"
4897        );
4898        assert_eq!(round("SELECT abs(x) FILTER (WHERE b)"), "SELECT abs(x) FILTER [b]");
4899    }
4900
4901    /// The `FILTER` goes before the `OVER`, which is a rule of the grammar and not of the binder.
4902    #[test]
4903    fn a_window_call_carries_its_filter_in_front_of_its_over() {
4904        assert_eq!(
4905            round("SELECT sum(x) FILTER (WHERE b) OVER ()"),
4906            "SELECT sum(x) FILTER [b] OVER [] [] [Range UnboundedPreceding CurrentRow NoOthers]"
4907        );
4908    }
4909
4910    #[test]
4911    fn the_modifiers_hang_off_the_query_and_not_off_the_select() {
4912        // `a UNION b ORDER BY x` sorts the union. Putting the order by on the select would have
4913        // made that unrepresentable, which is why the grammar puts it outside the chain and why
4914        // the AST follows.
4915        assert_eq!(
4916            round("SELECT 1 UNION ALL SELECT 2 ORDER BY 1"),
4917            "(SELECT 1 Union All SELECT 2) ORDER BY 1 Unstated Unstated"
4918        );
4919        assert_eq!(
4920            round("SELECT a FROM t UNION SELECT b FROM u EXCEPT SELECT c FROM v"),
4921            "((SELECT a FROM t Union Unstated SELECT b FROM u) Except Unstated SELECT c FROM v)",
4922            "set operators are left associative"
4923        );
4924        assert_eq!(
4925            round("SELECT 1 UNION SELECT 2 INTERSECT SELECT 3"),
4926            "(SELECT 1 Union Unstated (SELECT 2 Intersect Unstated SELECT 3))",
4927            "and intersect binds tighter than the other two"
4928        );
4929    }
4930
4931    #[test]
4932    fn the_sort_and_limit_clauses_keep_what_was_written() {
4933        assert_eq!(
4934            round("SELECT a FROM t ORDER BY a"),
4935            "SELECT a FROM t ORDER BY a Unstated Unstated"
4936        );
4937        assert_eq!(
4938            round("SELECT a FROM t ORDER BY a DESC NULLS LAST"),
4939            "SELECT a FROM t ORDER BY a Descending Last"
4940        );
4941        assert_eq!(round("SELECT a FROM t ORDER BY ALL"), "SELECT a FROM t ORDER BY ALL");
4942        assert_eq!(round("SELECT a FROM t GROUP BY ALL"), "SELECT a FROM t GROUP BY ALL");
4943        assert_eq!(round("SELECT a FROM t LIMIT 10 OFFSET 5"), "SELECT a FROM t LIMIT 10 OFFSET 5");
4944        assert_eq!(round("SELECT a FROM t OFFSET 5 LIMIT 10"), "SELECT a FROM t LIMIT 10 OFFSET 5");
4945        assert_eq!(round("SELECT a FROM t LIMIT 10%"), "SELECT a FROM t LIMIT 10%");
4946        assert_eq!(round("SELECT a FROM t LIMIT ALL"), "SELECT a FROM t", "which is no limit");
4947    }
4948
4949    #[test]
4950    fn a_subquery_appears_in_both_places_it_can() {
4951        assert_eq!(
4952            round("SELECT * FROM (SELECT x FROM t) AS s"),
4953            "SELECT * FROM (SELECT x FROM t) AS s"
4954        );
4955        assert_eq!(round("SELECT (SELECT 1)"), "SELECT (SELECT 1)");
4956    }
4957
4958    #[test]
4959    fn distinct_on_keeps_its_expressions() {
4960        assert_eq!(round("SELECT DISTINCT a"), "SELECT DISTINCT a");
4961        assert_eq!(round("SELECT ALL a"), "SELECT a", "which is the default written out");
4962        assert_eq!(round("SELECT DISTINCT ON (a, b) a"), "SELECT DISTINCT ON (a, b) a");
4963    }
4964
4965    #[test]
4966    fn an_operator_the_dialect_does_not_name_is_kept_by_name() {
4967        // The grammar text says `OperatorLiteral <- Identifier`, which reads as though any bare
4968        // word could be written infix. It cannot. That rule is one of the 24 the matcher overrides
4969        // and it is overridden to the bare operator matcher, so what it takes is a run of operator
4970        // characters. Believing the body here would have produced a transformer that accepted
4971        // `a foo b`, which DuckDB rejects.
4972        assert_eq!(round("SELECT a <=> b"), "SELECT (a <=> b)");
4973        assert!(parse_ast("SELECT a foo b").is_err(), "a bare word is not an operator");
4974    }
4975
4976    #[test]
4977    fn a_script_is_a_list_of_statements() {
4978        let ast = parse_ast("SELECT 1; SELECT 2;").unwrap();
4979        assert_eq!(ast.statements.len(), 2);
4980        // A trailing semicolon makes an empty top level statement in the parse tree, because the
4981        // grammar's `Statement? (';'+ / EndOfInput)` is happy with nothing on both sides. It is
4982        // dropped here rather than pretended away in the matcher.
4983        let Statement::Query(second) = ast.statements[1] else {
4984            panic!("the second statement is a query");
4985        };
4986        assert_eq!(show_query(&ast, second), "SELECT 2");
4987    }
4988
4989    #[test]
4990    fn an_unsupported_construct_names_itself_and_what_was_written() {
4991        let error = parse_ast("ALTER TABLE t ADD COLUMN a INTEGER").unwrap_err().to_string();
4992        assert!(error.starts_with("Not implemented Error"), "{error}");
4993        assert!(error.contains("ALTER TABLE t ADD COLUMN a INTEGER"), "{error}");
4994        assert!(error.contains("AlterStatement"), "{error}");
4995    }
4996
4997    #[test]
4998    fn a_long_construct_is_cut_short_in_the_message() {
4999        let query = format!("ALTER TABLE t ADD COLUMN {} INTEGER", "a".repeat(80));
5000        let error = parse_ast(&query).unwrap_err().to_string();
5001        assert!(error.contains("..."), "{error}");
5002        assert!(error.len() < 200, "{error}");
5003    }
5004
5005    #[test]
5006    fn the_transformer_never_panics_on_anything_the_matcher_accepts() {
5007        // The matcher accepts a good deal that means nothing, because the grammar does. Every one
5008        // of these parses and none of them is a statement this milestone covers, and the contract
5009        // is that the answer is an error either way.
5010        for query in [
5011            "SELECT",
5012            "FROM t SELECT",
5013            "SELECT * FROM t WHERE",
5014            "SELECT ()",
5015            "SELECT a FROM t GROUP BY ()",
5016        ] {
5017            let answer = parse_ast(query);
5018            if let Err(error) = answer {
5019                let message = error.to_string();
5020                assert!(
5021                    message.starts_with("Not implemented Error")
5022                        || message.starts_with("Parser Error"),
5023                    "{query} failed with {message}"
5024                );
5025            }
5026        }
5027    }
5028
5029    #[test]
5030    fn a_file_name_in_a_from_clause_is_a_table_name_with_the_quotes_off() {
5031        // Both spellings have to arrive as the same name, because the binder decides whether it is
5032        // a file by looking at the name, and `'hits.parquet'` with the quotes still on it is not
5033        // a path that anything can open.
5034        assert_eq!(round("SELECT * FROM 'hits.parquet'"), "SELECT * FROM hits.parquet");
5035        assert_eq!(round("SELECT * FROM \"hits.parquet\""), "SELECT * FROM hits.parquet");
5036        assert_eq!(round("SELECT * FROM 'hits.parquet' AS h"), "SELECT * FROM hits.parquet AS h");
5037        assert_eq!(
5038            round_with_case("SELECT Mixed FROM 'NoSuch/Mixed/File.csv'", IdentifierCase::Lower),
5039            "SELECT mixed FROM NoSuch/Mixed/File.csv"
5040        );
5041        assert_eq!(
5042            round_with_case("SELECT Mixed FROM \"QuotedTable\"", IdentifierCase::Upper),
5043            "SELECT MIXED FROM QuotedTable"
5044        );
5045    }
5046
5047    #[test]
5048    fn a_function_call_in_a_from_clause_is_a_source_and_not_an_expression() {
5049        assert_eq!(round("SELECT * FROM range(3)"), "SELECT * FROM range(3)");
5050        assert_eq!(round("SELECT * FROM range(1, 10, 2)"), "SELECT * FROM range(1, 10, 2)");
5051        assert_eq!(round("SELECT * FROM main.range(3)"), "SELECT * FROM main.range(3)");
5052        assert_eq!(round("SELECT * FROM range(3) AS t"), "SELECT * FROM range(3) AS t");
5053        // The grammar allows a call with no arguments here and the transformer keeps it, because
5054        // whether a particular function takes none is the binder's question and not this one's.
5055        assert_eq!(round("SELECT * FROM some_function()"), "SELECT * FROM some_function()");
5056        // `LATERAL` is read and dropped, because a FROM entry here already sees the entries written
5057        // to its left and the word asks for nothing more.
5058        assert_eq!(round("SELECT * FROM LATERAL range(3)"), "SELECT * FROM range(3)");
5059        assert_eq!(
5060            round("SELECT * FROM t, LATERAL (SELECT t.x) AS v"),
5061            "SELECT * FROM t, (SELECT t.x) AS v"
5062        );
5063    }
5064
5065    #[test]
5066    fn the_forms_of_a_table_function_this_does_not_cover_are_turned_away_by_name() {
5067        for query in ["SELECT * FROM range(3) WITH ORDINALITY", "SELECT * FROM t: range(3)"] {
5068            let error = parse_ast(query).unwrap_err().to_string();
5069            assert!(error.contains("grammar rule"), "{query} failed with {error}");
5070        }
5071    }
5072
5073    #[test]
5074    fn a_pragma_is_the_call_it_stands_for_by_the_time_it_leaves_here() {
5075        assert_eq!(round("PRAGMA version"), "SELECT * FROM pragma_version()");
5076        assert_eq!(round("PRAGMA database_size"), "SELECT * FROM pragma_database_size()");
5077        // The case the user wrote survives, because the name goes back out in the message about a
5078        // pragma that does not exist and the pin prints it back as it was typed.
5079        assert_eq!(round("PRAGMA VERSION"), "SELECT * FROM pragma_VERSION()");
5080        assert_eq!(round("PRAGMA table_info('t')"), "SELECT * FROM pragma_table_info('t')");
5081    }
5082
5083    #[test]
5084    fn a_pragma_that_is_a_statement_stays_one_rather_than_becoming_a_call() {
5085        // These write a setting and return no rows, so there is nothing to select from. The name
5086        // carries the value as well, and which name means what is decided a layer up.
5087        assert_eq!(round_statement("PRAGMA disable_optimizer"), "PRAGMA disable_optimizer");
5088        assert_eq!(round_statement("PRAGMA enable_profiling"), "PRAGMA enable_profiling");
5089        assert_eq!(round_statement("PRAGMA force_checkpoint"), "PRAGMA force_checkpoint");
5090        assert_eq!(round_statement("PRAGMA verify_parallelism"), "PRAGMA verify_parallelism");
5091        // A name of the same shape that no engine has gets here too, and the catalog is what turns
5092        // it down, so that the sentence about it is the one the catalog says about any pragma.
5093        assert_eq!(round_statement("PRAGMA enable_nothing_at_all"), "PRAGMA enable_nothing_at_all");
5094        // With parentheses it is a call again, because a pragma that takes an argument returns rows.
5095        assert_eq!(
5096            round("PRAGMA disable_optimizer('x')"),
5097            "SELECT * FROM pragma_disable_optimizer('x')"
5098        );
5099    }
5100
5101    #[test]
5102    fn a_bare_name_in_a_pragmas_parentheses_is_a_name_and_not_a_column() {
5103        // There is no FROM clause here for a column to come out of, so both spellings have to
5104        // arrive as the same string, and a qualified one has to arrive as one string and not two.
5105        assert_eq!(round("PRAGMA table_info(t)"), "SELECT * FROM pragma_table_info('t')");
5106        assert_eq!(round("PRAGMA table_info(main.t)"), "SELECT * FROM pragma_table_info('main.t')");
5107        assert_eq!(round("PRAGMA table_info(\"T\")"), "SELECT * FROM pragma_table_info('T')");
5108        // Anything that is not a name is left alone, so the binder is the one that says there is
5109        // no overload taking an integer rather than a table called 1 being looked for.
5110        assert_eq!(round("PRAGMA table_info(1)"), "SELECT * FROM pragma_table_info(1)");
5111    }
5112
5113    #[test]
5114    fn a_pragma_with_an_equals_sign_is_a_set_and_nothing_else() {
5115        assert_eq!(round_statement("PRAGMA memory_limit = '1GB'"), "SET memory_limit = '1GB'");
5116        assert_eq!(round_statement("PRAGMA threads = 4"), "SET threads = 4");
5117    }
5118
5119    #[test]
5120    fn a_pragma_with_empty_parentheses_does_not_parse_on_either_engine() {
5121        // The rule is `PragmaParameters <- Parens(List(Expression))` and a list of no expressions
5122        // does not match, which is where the pin's parser error comes from as well.
5123        let error = parse_ast("PRAGMA version()").unwrap_err().to_string();
5124        assert!(error.contains("syntax error at or near \")\""), "{error}");
5125    }
5126
5127    #[test]
5128    fn a_window_call_carries_its_partition_its_order_and_its_frame() {
5129        assert_eq!(
5130            round("SELECT row_number() OVER () FROM t"),
5131            "SELECT row_number() OVER [] [] [Range UnboundedPreceding CurrentRow NoOthers] FROM t"
5132        );
5133        assert_eq!(
5134            round("SELECT sum(a) OVER (PARTITION BY b, c ORDER BY d DESC NULLS FIRST) FROM t"),
5135            "SELECT sum(a) OVER [b, c] [d Descending First] \
5136             [Range UnboundedPreceding CurrentRow NoOthers] FROM t"
5137        );
5138        assert_eq!(
5139            round(
5140                "SELECT sum(a) OVER (ORDER BY b GROUPS BETWEEN 1 PRECEDING AND 2 FOLLOWING EXCLUDE TIES) FROM t"
5141            ),
5142            "SELECT sum(a) OVER [] [b Unstated Unstated] \
5143             [Groups Preceding(1) Following(2) Ties] FROM t"
5144        );
5145    }
5146
5147    /// A frame over the whole partition is the same frame however it was measured, so the three
5148    /// units collapse to one here rather than three ways of saying it reaching the binder.
5149    #[test]
5150    fn a_frame_with_both_ends_unbounded_is_counted_in_rows() {
5151        for unit in ["ROWS", "RANGE", "GROUPS"] {
5152            let query = format!(
5153                "SELECT sum(a) OVER (ORDER BY b {unit} BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) FROM t"
5154            );
5155            assert_eq!(
5156                round(&query),
5157                "SELECT sum(a) OVER [] [b Unstated Unstated] \
5158                 [Rows UnboundedPreceding UnboundedFollowing NoOthers] FROM t"
5159            );
5160        }
5161    }
5162
5163    /// A single bound names the start and the end is the current row, which is the standard's rule
5164    /// and is why the two spellings below have to arrive as the same frame.
5165    #[test]
5166    fn a_frame_written_with_one_bound_ends_at_the_current_row() {
5167        assert_eq!(
5168            round("SELECT sum(a) OVER (ORDER BY b ROWS UNBOUNDED PRECEDING) FROM t"),
5169            round(
5170                "SELECT sum(a) OVER (ORDER BY b ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t"
5171            )
5172        );
5173    }
5174
5175    #[test]
5176    fn a_named_window_is_resolved_here_and_not_carried_any_further() {
5177        let inlined = round("SELECT sum(a) OVER (PARTITION BY b ORDER BY c) FROM t");
5178        assert_eq!(
5179            round("SELECT sum(a) OVER w FROM t WINDOW w AS (PARTITION BY b ORDER BY c)"),
5180            inlined
5181        );
5182        assert_eq!(
5183            round("SELECT sum(a) OVER (w) FROM t WINDOW w AS (PARTITION BY b ORDER BY c)"),
5184            inlined
5185        );
5186        // A definition can build on one written before it, and a copy can add the half the base
5187        // did not say.
5188        assert_eq!(
5189            round("SELECT sum(a) OVER v FROM t WINDOW w AS (PARTITION BY b), v AS (w ORDER BY c)"),
5190            inlined
5191        );
5192        assert_eq!(
5193            round("SELECT sum(a) OVER (w ORDER BY c) FROM t WINDOW w AS (PARTITION BY b)"),
5194            inlined
5195        );
5196        // The name is matched without regard to case, the way every other name here is.
5197        assert_eq!(
5198            round("SELECT sum(a) OVER W FROM t WINDOW w AS (PARTITION BY b ORDER BY c)"),
5199            inlined
5200        );
5201    }
5202
5203    /// A window clause is visible to the whole block it was written on, including a subquery
5204    /// inside it, which was measured on the pin.
5205    #[test]
5206    fn a_named_window_reaches_a_subquery_written_in_the_same_block() {
5207        let ast = parse_ast("SELECT (SELECT sum(b) OVER w FROM u) FROM t WINDOW w AS (ORDER BY b)");
5208        assert!(ast.is_ok(), "{:?}", ast.err());
5209        // And no further than that: the next statement in the script starts with none of them.
5210        let error =
5211            parse_ast("SELECT 1 FROM t WINDOW w AS (ORDER BY b); SELECT sum(a) OVER w FROM u;")
5212                .unwrap_err()
5213                .to_string();
5214        assert!(error.contains("window \"\"w\"\" does not exist"), "{error}");
5215    }
5216
5217    /// All four are the pin's sentences, in the pin's words, including the doubled quotes in the
5218    /// first one.
5219    #[test]
5220    fn the_four_complaints_about_a_named_window_are_upstreams() {
5221        let cases = [
5222            ("SELECT sum(a) OVER w FROM t", "window \"\"w\"\" does not exist"),
5223            (
5224                "SELECT sum(a) OVER (w PARTITION BY b) FROM t WINDOW w AS (PARTITION BY b)",
5225                "Cannot override PARTITION BY clause of window \"w\"",
5226            ),
5227            (
5228                "SELECT sum(a) OVER (w ORDER BY b) FROM t WINDOW w AS (ORDER BY b)",
5229                "Cannot override ORDER BY clause of window \"w\"",
5230            ),
5231            (
5232                "SELECT sum(a) OVER (w ROWS UNBOUNDED PRECEDING) FROM t WINDOW w AS (ORDER BY b ROWS UNBOUNDED PRECEDING)",
5233                "cannot copy window \"w\" because it has a frame clause",
5234            ),
5235        ];
5236        for (query, expected) in cases {
5237            let error = parse_ast(query).expect_err(query).to_string();
5238            assert!(error.contains(expected), "{query}: {error}");
5239        }
5240    }
5241
5242    /// `IGNORE NULLS` is a window modifier, so a call without an `OVER` still has nowhere to put
5243    /// it, and `EXCLUDE` needs a framing keyword in front of it on both engines.
5244    #[test]
5245    fn the_modifiers_that_only_a_window_takes_are_turned_down_without_one() {
5246        let error = parse_ast("SELECT first_value(a IGNORE NULLS) FROM t").unwrap_err().to_string();
5247        assert!(
5248            error.contains("RESPECT/IGNORE NULLS is not supported for non-window functions"),
5249            "{error}"
5250        );
5251        let error = parse_ast("SELECT sum(a) OVER (ORDER BY b EXCLUDE TIES) FROM t")
5252            .unwrap_err()
5253            .to_string();
5254        assert!(error.contains("syntax error at or near \"EXCLUDE\""), "{error}");
5255    }
5256
5257    /// A call with an `OVER` on it skips the rewrites an ordinary call goes through, which is
5258    /// visible on the one name that has a rewrite and an arity check of its own.
5259    #[test]
5260    fn a_window_call_is_not_put_through_the_rewrites_a_plain_call_is() {
5261        assert_eq!(
5262            round("SELECT ifnull(1) OVER () FROM t"),
5263            "SELECT ifnull(1) OVER [] [] [Range UnboundedPreceding CurrentRow NoOthers] FROM t"
5264        );
5265        let error = parse_ast("SELECT ifnull(1) FROM t").unwrap_err().to_string();
5266        assert!(error.contains("Wrong number of arguments to IFNULL."), "{error}");
5267    }
5268
5269    #[test]
5270    fn interning_means_a_name_written_twice_is_stored_once() {
5271        let ast = parse_ast("SELECT a, a, a FROM t WHERE a = a").unwrap();
5272        assert_eq!(ast.strings.iter().filter(|text| *text == "a").count(), 1);
5273    }
5274}