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