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