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