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