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, Result};
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,
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    let tokens = tokenize(query)?;
42    let tree = parse_tokens(query, &tokens, PROGRAM, true)?;
43    transform(query, &tokens, &tree)
44}
45
46/// Transform a parse tree that has already been produced.
47pub fn transform(query: &str, tokens: &[Token], tree: &Tree) -> Result<Ast> {
48    let mut transform = Transform {
49        query,
50        tokens,
51        tree,
52        ast: Ast::default(),
53        interned: HashMap::new(),
54        anonymous: 0,
55    };
56    transform.program(tree.root())?;
57    Ok(transform.ast)
58}
59
60struct Transform<'a> {
61    query: &'a str,
62    tokens: &'a [Token],
63    tree: &'a Tree,
64    ast: Ast,
65    interned: HashMap<String, StrRef>,
66    /// How many bare `?` parameters have been seen, which is what numbers the next one.
67    anonymous: u32,
68}
69
70impl<'a> Transform<'a> {
71    // The parts that walk the parse tree without caring what it says.
72
73    /// The text a node covers.
74    fn text(&self, node: u32) -> &'a str {
75        self.tree.text(node, self.query, self.tokens)
76    }
77
78    /// The name of the rule a node is.
79    fn name(&self, node: u32) -> &'static str {
80        self.tree.name(node)
81    }
82
83    /// The children of a node.
84    ///
85    /// Returned with the tree's lifetime rather than the borrow of `self`, so that the caller can
86    /// iterate it while calling the `&mut self` methods that build the arena. Copying the `&Tree`
87    /// out first is what buys that, and it is why every walker here starts by doing so.
88    fn kids(&self, node: u32) -> impl Iterator<Item = u32> + use<'a> {
89        let tree = self.tree;
90        tree.children(node)
91    }
92
93    /// How many children a node has.
94    fn count(&self, node: u32) -> usize {
95        self.kids(node).count()
96    }
97
98    /// The n'th child, or `NONE`.
99    fn nth(&self, node: u32, n: usize) -> u32 {
100        self.kids(node).nth(n).unwrap_or(NONE)
101    }
102
103    /// The first child, or `NONE`.
104    fn first(&self, node: u32) -> u32 {
105        self.nth(node, 0)
106    }
107
108    /// The first child named `name`, or `NONE`.
109    ///
110    /// Optional parts of a sequence do not leave a placeholder behind, so `SimpleSelect` with a
111    /// `WHERE` and no `GROUP BY` has the where clause as its second child and a `SimpleSelect` with
112    /// neither has something else there. Positional indexing into an optional sequence is the
113    /// single easiest way to write a transformer that is subtly wrong, so nothing here does it.
114    fn find(&self, node: u32, name: &str) -> u32 {
115        self.kids(node).find(|&kid| self.name(kid) == name).unwrap_or(NONE)
116    }
117
118    /// Every leaf of a subtree, in order.
119    ///
120    /// A leaf is a rule that matched only terminals, which for a name is the identifier itself. It
121    /// is how all thirty odd spellings of a qualified name collapse into one walk: whether the
122    /// parse said `SchemaQualification ReservedTableQualification ReservedColumnName` or
123    /// `IdentifierDot IdentifierDot ColumnName`, the leaves are the parts in order.
124    fn leaves(&self, node: u32, out: &mut Vec<u32>) {
125        let mut any = false;
126        for kid in self.kids(node) {
127            any = true;
128            self.leaves(kid, &mut *out);
129        }
130        if !any {
131            out.push(node);
132        }
133    }
134
135    // The parts that build the arena.
136
137    /// Intern a string, returning its index.
138    fn intern(&mut self, text: &str) -> StrRef {
139        if let Some(&index) = self.interned.get(text) {
140            return index;
141        }
142        let index = u32::try_from(self.ast.strings.len())
143            .map_err(|_| Error::internal("more than four billion strings in one query"))
144            .unwrap_or(NONE);
145        self.ast.strings.push(text.to_string());
146        self.interned.insert(text.to_string(), index);
147        index
148    }
149
150    /// Push an expression and return its index.
151    fn push(&mut self, expr: Expr) -> ExprRef {
152        let index = self.ast.exprs.len() as u32;
153        self.ast.exprs.push(expr);
154        index
155    }
156
157    /// Push a from item and return its index.
158    fn push_source(&mut self, source: Source) -> SourceRef {
159        let index = self.ast.sources.len() as u32;
160        self.ast.sources.push(source);
161        index
162    }
163
164    /// Push a query and return its index.
165    fn push_query(&mut self, query: Query) -> QueryRef {
166        let index = self.ast.queries.len() as u32;
167        self.ast.queries.push(query);
168        index
169    }
170
171    /// Push a select and return its index.
172    fn push_select(&mut self, select: Select) -> SelectRef {
173        let index = self.ast.selects.len() as u32;
174        self.ast.selects.push(select);
175        index
176    }
177
178    /// Turn a vector of expressions into a slice of the expression list arena.
179    fn expr_slice(&mut self, items: Vec<ExprRef>) -> Slice {
180        let start = self.ast.expr_lists.len() as u32;
181        self.ast.expr_lists.extend(items);
182        Slice { start, len: self.ast.expr_lists.len() as u32 - start }
183    }
184
185    /// Turn a vector of strings into a slice of the name arena.
186    fn part_slice(&mut self, items: Vec<StrRef>) -> Slice {
187        let start = self.ast.parts.len() as u32;
188        self.ast.parts.extend(items);
189        Slice { start, len: self.ast.parts.len() as u32 - start }
190    }
191
192    /// Turn a vector of column definitions into a slice of the column arena.
193    fn column_def_slice(&mut self, items: Vec<ColumnDef>) -> Slice {
194        let start = self.ast.column_defs.len() as u32;
195        self.ast.column_defs.extend(items);
196        Slice { start, len: self.ast.column_defs.len() as u32 - start }
197    }
198
199    /// Turn a vector of targets into a slice of the target arena.
200    fn target_slice(&mut self, items: Vec<Target>) -> Slice {
201        let start = self.ast.targets.len() as u32;
202        self.ast.targets.extend(items);
203        Slice { start, len: self.ast.targets.len() as u32 - start }
204    }
205
206    /// Turn a vector of qualified names into a slice of the name list arena.
207    fn name_list_slice(&mut self, items: Vec<Slice>) -> Slice {
208        let start = self.ast.name_lists.len() as u32;
209        self.ast.name_lists.extend(items);
210        Slice { start, len: self.ast.name_lists.len() as u32 - start }
211    }
212
213    /// The error for a construct the transformer does not cover yet.
214    ///
215    /// Both halves matter. The text is what the user wrote, which is the only part they can act on,
216    /// and the rule name is what we act on, because it is the exact grammar rule to go implement.
217    fn unsupported<T>(&self, node: u32) -> Result<T> {
218        let text = self.text(node);
219        let text = if text.chars().count() > 60 {
220            let cut = text.char_indices().nth(60).map_or(text.len(), |(at, _)| at);
221            format!("{}...", &text[..cut])
222        } else {
223            text.to_string()
224        };
225        Err(Error::not_implemented(format!(
226            "{text} is not supported yet, the grammar rule is {}",
227            self.name(node)
228        )))
229    }
230
231    // Names.
232
233    /// One identifier out of a subtree, with the quoting and any trailing dot removed.
234    fn identifier(&mut self, node: u32) -> StrRef {
235        let mut leaves = Vec::new();
236        self.leaves(node, &mut leaves);
237        let text = leaves.last().map_or("", |&leaf| self.text(leaf));
238        let text = unquote(text.strip_suffix('.').unwrap_or(text));
239        self.intern(&text)
240    }
241
242    /// Every part of a qualified name, outermost first.
243    fn name_parts(&mut self, node: u32) -> Slice {
244        let mut leaves = Vec::new();
245        self.leaves(node, &mut leaves);
246        let mut parts = Vec::with_capacity(leaves.len());
247        for leaf in leaves {
248            let text = self.text(leaf);
249            // A node that covers no tokens is an optional part that was not written, and a bare
250            // `*` is the star and not a name part. Neither is a component of anything.
251            if text.is_empty() || text == "*" {
252                continue;
253            }
254            let text = unquote(text.strip_suffix('.').unwrap_or(text));
255            let interned = self.intern(&text);
256            parts.push(interned);
257        }
258        self.part_slice(parts)
259    }
260
261    // Statements.
262
263    /// `Program <- TopLevelStatement*`.
264    fn program(&mut self, node: u32) -> Result<()> {
265        for top in self.kids(node) {
266            // A script that ends in a semicolon produces a last `TopLevelStatement` whose only
267            // child is the end of input, because the grammar says `Statement? (';'+ / EndOfInput)`
268            // and both halves of that are happy to match nothing. It is a real node and it is not a
269            // statement, so it is dropped here rather than pretended away in the matcher.
270            let Some(statement) = self.kids(top).find(|&kid| self.name(kid) == "Statement") else {
271                continue;
272            };
273            let statement = self.statement(statement)?;
274            self.ast.statements.push(statement);
275        }
276        Ok(())
277    }
278
279    /// `Statement <- SelectStatement / ...`, twenty seven alternatives of which four are done.
280    fn statement(&mut self, node: u32) -> Result<Statement> {
281        let inner = self.first(node);
282        match self.name(inner) {
283            "SelectStatement" => {
284                let query = self.query(self.first(inner))?;
285                Ok(Statement::Query(query))
286            }
287            "CreateStatement" => self.create_statement(inner),
288            "DropStatement" => self.drop_statement(inner),
289            "InsertStatement" => self.insert_statement(inner),
290            "SetStatement" => self.set_statement(inner),
291            "ResetStatement" => self.reset_statement(inner),
292            _ => self.unsupported(inner),
293        }
294    }
295
296    /// `SetStatement <- 'SET' SetAssignmentOrTimeZone`.
297    ///
298    /// Of the three assignments, `StandardAssignment` is the one that is done. `SET SCHEMA` and
299    /// `SET TIME ZONE` are each a setting this database has nothing to do with yet, and they are a
300    /// refusal rather than a silent success, because a statement that says where to look for a
301    /// table and is ignored is a statement that changes an answer.
302    fn set_statement(&mut self, node: u32) -> Result<Statement> {
303        let inner = self.first(self.find(node, "SetAssignmentOrTimeZone"));
304        if self.name(inner) != "StandardAssignment" {
305            return self.unsupported(inner);
306        }
307        let (name, scope) = self.setting_name(self.find(inner, "SetVariableOrSetting"))?;
308        let assignment = self.find(inner, "SetAssignment");
309        let list = self.find(assignment, "VariableList");
310        let mut values = Vec::new();
311        for kid in self.kids(list) {
312            values.push(self.expr(kid)?);
313        }
314        // The grammar takes a list because `SET search_path = a, b` is a list in postgres. Nothing
315        // here has a setting that reads one, and taking the first of several would be worse than
316        // saying so.
317        let [value] = values[..] else {
318            return self.unsupported(list);
319        };
320        let index = self.ast.settings.len() as u32;
321        self.ast.settings.push(Setting { name, scope, value });
322        Ok(Statement::Set(index))
323    }
324
325    /// `ResetStatement <- 'RESET' SetVariableOrSetting`.
326    fn reset_statement(&mut self, node: u32) -> Result<Statement> {
327        let (name, scope) = self.setting_name(self.find(node, "SetVariableOrSetting"))?;
328        let index = self.ast.settings.len() as u32;
329        self.ast.settings.push(Setting { name, scope, value: NONE });
330        Ok(Statement::Reset(index))
331    }
332
333    /// `SetVariableOrSetting <- SetVariable / SetSetting`, where the setting carries a scope word.
334    ///
335    /// `SET VARIABLE x = 1` is the other alternative and is a different feature: a variable is a
336    /// value the session holds and `getvariable` reads back, where a setting is a knob on the
337    /// engine. Refused rather than treated as a setting of that name.
338    fn setting_name(&mut self, node: u32) -> Result<(StrRef, Scope)> {
339        let inner = self.first(node);
340        if self.name(inner) != "SetSetting" {
341            return self.unsupported(inner);
342        }
343        let written = self.find(inner, "SettingScope");
344        let scope = if written == NONE {
345            Scope::Unwritten
346        } else {
347            match self.name(self.first(written)) {
348                "GlobalScope" => Scope::Global,
349                "SessionScope" => Scope::Session,
350                "LocalScope" => Scope::Local,
351                _ => return self.unsupported(written),
352            }
353        };
354        Ok((self.identifier(self.find(inner, "SettingName")), scope))
355    }
356
357    /// `CreateStatement <- 'CREATE' OrReplace? Temporary? CreateStatementVariation`.
358    ///
359    /// Of the nine variations, `CreateTableStmt` and `CreateViewStmt` are the ones that are done.
360    /// The other seven are a macro, a sequence, a type, a schema, an index, a secret and a trigger,
361    /// and each of them is a catalog entry this database has no room for yet.
362    fn create_statement(&mut self, node: u32) -> Result<Statement> {
363        let or_replace = self.find(node, "OrReplace") != NONE;
364        let temporary = self.find(node, "Temporary") != NONE;
365        let variation = self.find(node, "CreateStatementVariation");
366        let inner = self.first(variation);
367        // duckdb refuses this pair in the parser, with a caret under the `NOT`, because none of its
368        // create rules has room for both. The vendored grammar has room for both, so the refusal is
369        // here instead, which is the same stage and therefore the same sentence.
370        if or_replace && self.find(inner, "IfNotExists") != NONE {
371            return Err(Error::parser(
372                "Cannot specify both OR REPLACE and IF NOT EXISTS within single create statement",
373            ));
374        }
375        match self.name(inner) {
376            "CreateTableStmt" => self.create_table_statement(inner, or_replace, temporary),
377            "CreateViewStmt" => self.create_view_statement(inner, or_replace, temporary),
378            _ => self.unsupported(inner),
379        }
380    }
381
382    /// `CreateTableStmt <- 'TABLE' IfNotExists? QualifiedName CreateTableDefinition`.
383    fn create_table_statement(
384        &mut self,
385        inner: u32,
386        or_replace: bool,
387        temporary: bool,
388    ) -> Result<Statement> {
389        let name = self.name_parts(self.find(inner, "QualifiedName"));
390        let if_not_exists = self.find(inner, "IfNotExists") != NONE;
391        let definition = self.find(inner, "CreateTableDefinition");
392        let body = self.first(definition);
393        let (columns, query) = match self.name(body) {
394            "CreateColumnList" => (self.column_list(body)?, NONE),
395            "CreateTableAs" => self.create_table_as(body)?,
396            _ => return self.unsupported(body),
397        };
398        let index = self.ast.create_tables.len() as u32;
399        self.ast.create_tables.push(CreateTable {
400            name,
401            columns,
402            query,
403            if_not_exists,
404            or_replace,
405            temporary,
406        });
407        Ok(Statement::CreateTable(index))
408    }
409
410    /// `CreateViewStmt <- CreateSecure? CreateRecursive? 'VIEW' IfNotExists? QualifiedName
411    /// InsertColumnList? WithList? 'AS' SelectStatementInternal`.
412    ///
413    /// The body is transformed here as well as kept as text. Transforming it is what makes a view
414    /// whose body does not parse a parse error at creation, which is where it belongs, and the text
415    /// is what the catalog keeps so that the body can be bound again at every reference.
416    fn create_view_statement(
417        &mut self,
418        inner: u32,
419        or_replace: bool,
420        temporary: bool,
421    ) -> Result<Statement> {
422        for kid in self.kids(inner) {
423            // `SECURE` is a column and row policy, `RECURSIVE` is a different shape of view
424            // entirely, and `WITH` carries options. Dropping any of the three silently would make a
425            // view that is not the view that was asked for.
426            if matches!(self.name(kid), "CreateSecure" | "CreateRecursive" | "WithList") {
427                return self.unsupported(kid);
428            }
429        }
430        let name = self.name_parts(self.find(inner, "QualifiedName"));
431        let if_not_exists = self.find(inner, "IfNotExists") != NONE;
432        let list = self.find(inner, "InsertColumnList");
433        let columns = if list == NONE {
434            Slice::default()
435        } else {
436            let mut parts = Vec::new();
437            for kid in self.kids(self.find(list, "ColumnList")) {
438                parts.push(self.identifier(kid));
439            }
440            self.part_slice(parts)
441        };
442        let body = self.find(inner, "SelectStatementInternal");
443        let sql = self.text(body).to_string();
444        let sql = self.intern(&sql);
445        let query = self.query(body)?;
446        let index = self.ast.create_views.len() as u32;
447        self.ast.create_views.push(CreateView {
448            name,
449            columns,
450            query,
451            sql,
452            if_not_exists,
453            or_replace,
454            temporary,
455        });
456        Ok(Statement::CreateView(index))
457    }
458
459    /// `CreateColumnList <- Parens(CreateTableColumnList?) PartitionSortedOptions? WithList?`.
460    fn column_list(&mut self, node: u32) -> Result<Slice> {
461        for kid in self.kids(node) {
462            if matches!(self.name(kid), "PartitionOptions" | "SortedOptions" | "WithList") {
463                return self.unsupported(kid);
464            }
465        }
466        let list = self.find(node, "CreateTableColumnList");
467        if list == NONE {
468            // `CREATE TABLE t ()` parses. It is a table of no columns, and the catalog is entitled
469            // to refuse it, but that is not this layer's refusal to make.
470            return Ok(Slice::default());
471        }
472        let mut defs = Vec::new();
473        for element in self.kids(list) {
474            let inner = self.first(element);
475            if self.name(inner) != "CreateTableColumnDefinition" {
476                // A table level `PRIMARY KEY`, `UNIQUE`, `CHECK` or `FOREIGN KEY`. Constraints are
477                // not enforced anywhere yet and silently dropping one is a wrong answer waiting to
478                // happen, so it is refused instead.
479                return self.unsupported(inner);
480            }
481            defs.push(self.column_definition(self.first(inner))?);
482        }
483        Ok(self.column_def_slice(defs))
484    }
485
486    /// `ColumnDefinition <- DottedIdentifier Type? GeneratedColumn? ConstraintNameClause?
487    /// ColumnConstraint*`.
488    fn column_definition(&mut self, node: u32) -> Result<ColumnDef> {
489        let name = self.identifier(self.find(node, "DottedIdentifier"));
490        let type_node = self.find(node, "Type");
491        let ty = if type_node == NONE {
492            NONE
493        } else {
494            let text = self.text(type_node).to_string();
495            self.intern(&text)
496        };
497        if self.find(node, "GeneratedColumn") != NONE {
498            return self.unsupported(self.find(node, "GeneratedColumn"));
499        }
500        let mut not_null = false;
501        for kid in self.kids(node) {
502            if self.name(kid) != "ColumnConstraint" {
503                continue;
504            }
505            let constraint = self.first(kid);
506            match self.name(constraint) {
507                "NotNullConstraint" => {
508                    not_null = self.name(self.first(constraint)) == "NotNullColumnConstraint";
509                }
510                _ => return self.unsupported(constraint),
511            }
512        }
513        Ok(ColumnDef { name, ty, not_null })
514    }
515
516    /// `CreateTableAs <- IdentifierList? PartitionSortedOptions? WithList? 'AS' Statement
517    /// WithData?`.
518    ///
519    /// The names in the `IdentifierList` become column definitions with no type, because the types
520    /// are the query's and only the names are the syntax's to say.
521    fn create_table_as(&mut self, node: u32) -> Result<(Slice, QueryRef)> {
522        for kid in self.kids(node) {
523            if matches!(
524                self.name(kid),
525                "PartitionOptions" | "SortedOptions" | "WithList" | "WithData"
526            ) {
527                return self.unsupported(kid);
528            }
529        }
530        let names = self.find(node, "IdentifierList");
531        let columns = if names == NONE {
532            Slice::default()
533        } else {
534            let mut defs = Vec::new();
535            for kid in self.kids(names) {
536                let name = self.identifier(kid);
537                defs.push(ColumnDef { name, ty: NONE, not_null: false });
538            }
539            self.column_def_slice(defs)
540        };
541        let statement = self.find(node, "Statement");
542        let inner = self.first(statement);
543        if self.name(inner) != "SelectStatement" {
544            return self.unsupported(inner);
545        }
546        let query = self.query(self.first(inner))?;
547        Ok((columns, query))
548    }
549
550    /// `DropStatement <- 'DROP' DropEntries DropBehavior?`.
551    ///
552    /// `DropTable <- TableOrView IfExists? List(BaseTableName)`, and `TableOrView` covers `VIEW`
553    /// and `MATERIALIZED VIEW` as well as `TABLE`, so it is checked rather than assumed. The first
554    /// two are done and a materialized view is not a thing this database has.
555    fn drop_statement(&mut self, node: u32) -> Result<Statement> {
556        if self.find(node, "DropBehavior") != NONE {
557            return self.unsupported(self.find(node, "DropBehavior"));
558        }
559        let entries = self.find(node, "DropEntries");
560        let inner = self.first(entries);
561        if self.name(inner) != "DropTable" {
562            return self.unsupported(inner);
563        }
564        let kind = self.find(inner, "TableOrView");
565        let view = match self.name(self.first(kind)) {
566            "CommentTable" => false,
567            "CommentView" => true,
568            _ => return self.unsupported(kind),
569        };
570        let if_exists = self.find(inner, "IfExists") != NONE;
571        let mut names = Vec::new();
572        for kid in self.kids(inner) {
573            if self.name(kid) == "BaseTableName" {
574                names.push(self.name_parts(kid));
575            }
576        }
577        let names = self.name_list_slice(names);
578        let index = self.ast.drop_tables.len() as u32;
579        self.ast.drop_tables.push(DropTable { names, if_exists, view });
580        Ok(Statement::DropTable(index))
581    }
582
583    /// `InsertStatement <- ... InsertTarget InsertColumnList? InsertValues ...`.
584    ///
585    /// `ON CONFLICT`, `RETURNING`, `BY NAME`, `BY POSITION`, `OR REPLACE` and the rest of the
586    /// clauses the grammar hangs off this are each a refusal, because every one of them changes
587    /// what the statement means and none of them changes it in a way anything downstream would
588    /// notice if it were dropped.
589    fn insert_statement(&mut self, node: u32) -> Result<Statement> {
590        for kid in self.kids(node) {
591            if matches!(
592                self.name(kid),
593                "InsertTarget" | "InsertColumnList" | "InsertValues" | "WithClause"
594            ) {
595                continue;
596            }
597            return self.unsupported(kid);
598        }
599        if self.find(node, "WithClause") != NONE {
600            return self.unsupported(self.find(node, "WithClause"));
601        }
602        let name = self.name_parts(self.find(self.find(node, "InsertTarget"), "BaseTableName"));
603        let list = self.find(node, "InsertColumnList");
604        let columns = if list == NONE {
605            Slice::default()
606        } else {
607            let mut parts = Vec::new();
608            for kid in self.kids(self.find(list, "ColumnList")) {
609                parts.push(self.identifier(kid));
610            }
611            self.part_slice(parts)
612        };
613        let values = self.find(node, "InsertValues");
614        let inner = self.first(values);
615        if self.name(inner) != "SelectInsertValues" {
616            return self.unsupported(inner);
617        }
618        let source = self.query(self.find(inner, "SelectStatementInternal"))?;
619        let index = self.ast.inserts.len() as u32;
620        self.ast.inserts.push(Insert { name, columns, source });
621        Ok(Statement::Insert(index))
622    }
623
624    /// `SelectStatementInternal <- WithClause? SelectSetOpChain ResultModifiers?`.
625    fn query(&mut self, node: u32) -> Result<QueryRef> {
626        if self.find(node, "WithClause") != NONE {
627            return self.unsupported(self.find(node, "WithClause"));
628        }
629        let chain = self.find(node, "SelectSetOpChain");
630        if chain == NONE {
631            return self.unsupported(node);
632        }
633        let query = self.set_op_chain(chain)?;
634        let modifiers = self.find(node, "ResultModifiers");
635        if modifiers != NONE {
636            self.result_modifiers(query, modifiers)?;
637        }
638        Ok(query)
639    }
640
641    /// `SelectSetOpChain <- IntersectChain SelectSetOpChainTail*`, left associative.
642    fn set_op_chain(&mut self, node: u32) -> Result<QueryRef> {
643        let mut kids = self.kids(node);
644        let head = kids.next().unwrap_or(NONE);
645        let mut left = self.intersect_chain(head)?;
646        for tail in kids {
647            // `SelectSetOpChainTail <- SetopClause IntersectChain`.
648            let clause = self.first(tail);
649            let (op, quantifier, by_name) = self.setop_clause(clause)?;
650            let right = self.intersect_chain(self.nth(tail, 1))?;
651            left = self.push_query(Query::bare(QueryBody::SetOp {
652                op,
653                quantifier,
654                by_name,
655                left,
656                right,
657            }));
658        }
659        Ok(left)
660    }
661
662    /// `IntersectChain <- SelectAtom IntersectChainTail*`, which binds tighter than union.
663    fn intersect_chain(&mut self, node: u32) -> Result<QueryRef> {
664        let mut kids = self.kids(node);
665        let head = kids.next().unwrap_or(NONE);
666        let mut left = self.select_atom(head)?;
667        for tail in kids {
668            // `IntersectChainTail <- SetIntersectClause SelectAtom`.
669            let clause = self.first(tail);
670            let quantifier = self.quantifier(self.find(clause, "DistinctOrAll"));
671            let right = self.select_atom(self.nth(tail, 1))?;
672            left = self.push_query(Query::bare(QueryBody::SetOp {
673                op: SetOp::Intersect,
674                quantifier,
675                by_name: false,
676                left,
677                right,
678            }));
679        }
680        Ok(left)
681    }
682
683    /// `SetopClause <- SetopType DistinctOrAll? ByName?`.
684    fn setop_clause(&mut self, node: u32) -> Result<(SetOp, Quantifier, bool)> {
685        let kind = self.find(node, "SetopType");
686        let op = match self.name(self.first(kind)) {
687            "SetopUnion" => SetOp::Union,
688            "SetopExcept" => SetOp::Except,
689            _ => return self.unsupported(kind),
690        };
691        let quantifier = self.quantifier(self.find(node, "DistinctOrAll"));
692        Ok((op, quantifier, self.find(node, "ByName") != NONE))
693    }
694
695    /// `DistinctOrAll <- DistinctKeyword / AllKeyword`, absent included.
696    fn quantifier(&self, node: u32) -> Quantifier {
697        if node == NONE {
698            return Quantifier::Unstated;
699        }
700        match self.name(self.first(node)) {
701            "DistinctKeyword" => Quantifier::Distinct,
702            "AllKeyword" => Quantifier::All,
703            _ => Quantifier::Unstated,
704        }
705    }
706
707    /// `SelectAtom <- SelectParens / SelectStatementType`.
708    fn select_atom(&mut self, node: u32) -> Result<QueryRef> {
709        let inner = self.first(node);
710        match self.name(inner) {
711            // `SelectParens <- Parens(SelectStatementInternal)`, so the parens buy a query that
712            // carries its own order by and limit and nothing else.
713            "SelectParens" => self.query(self.first(inner)),
714            "SelectStatementType" => {
715                let kind = self.first(inner);
716                match self.name(kind) {
717                    "OptionalParensSimpleSelect" => {
718                        let select = self.simple_select(self.unwrap_parens(kind))?;
719                        Ok(self.push_query(Query::bare(QueryBody::Select(select))))
720                    }
721                    "ValuesClause" => {
722                        let rows = self.values_clause(kind)?;
723                        Ok(self.push_query(Query::bare(QueryBody::Values(rows))))
724                    }
725                    "DescribeStatement" => self.describe_statement(kind),
726                    _ => self.unsupported(kind),
727                }
728            }
729            _ => self.unsupported(inner),
730        }
731    }
732
733    /// `DescribeStatement <- ShowTables / ShowDeprecatedSelect / DescribeSelect / ShowAllTables /
734    /// ShowByName / DescribeByName`.
735    ///
736    /// Two of the six are done and they are the two that describe a relation. The four that are
737    /// not are `SHOW`, which is a different statement wearing this rule: three of its four forms
738    /// list what the database has rather than what a query returns, and the fourth is `SHOW <query>`,
739    /// which upstream documents as deprecated. Describing something through a deprecated spelling
740    /// is not worth implementing before the spelling that replaced it has users.
741    ///
742    /// `SUMMARIZE` shares `DescribeByName` and `DescribeSelect` with `DESCRIBE` and is refused
743    /// here, because it returns twelve columns of statistics rather than six of schema and reading
744    /// it as a describe would answer a different question than the one that was asked.
745    fn describe_statement(&mut self, node: u32) -> Result<QueryRef> {
746        let inner = self.first(node);
747        match self.name(inner) {
748            "DescribeSelect" => {
749                self.describe_and_not_summarize(inner)?;
750                let query = self.query(self.find(inner, "SelectStatementInternal"))?;
751                Ok(self.push_query(Query::bare(QueryBody::Describe(query))))
752            }
753            "DescribeByName" => {
754                self.describe_and_not_summarize(inner)?;
755                let target = self.find(inner, "DescribeTarget");
756                if target == NONE {
757                    return self.unsupported(inner);
758                }
759                let source = self.describe_target(target)?;
760                let query = self.star_over(source);
761                Ok(self.push_query(Query::bare(QueryBody::Describe(query))))
762            }
763            _ => self.unsupported(inner),
764        }
765    }
766
767    /// `DescribeOrSummarize <- DescribeRule / Summarize`, where only the first is done.
768    fn describe_and_not_summarize(&mut self, node: u32) -> Result<()> {
769        let word = self.find(node, "DescribeOrSummarize");
770        if word == NONE || self.name(self.first(word)) != "DescribeRule" {
771            return self.unsupported(if word == NONE { node } else { word });
772        }
773        Ok(())
774    }
775
776    /// `DescribeTarget <- DescribeBaseTableName / DescribeStringLiteral`, as a source to read from.
777    ///
778    /// Both become a `FROM` item and not a lookup of their own, because the string form is the
779    /// replacement scan and the binder already knows how to turn `'hits.parquet'` into a reader.
780    /// A name that is a table, a view, a file or nothing at all then gets one answer from one place.
781    fn describe_target(&mut self, node: u32) -> Result<SourceRef> {
782        let inner = self.first(node);
783        let name = match self.name(inner) {
784            "DescribeBaseTableName" => self.name_parts(self.find(inner, "BaseTableName")),
785            "DescribeStringLiteral" => {
786                let text = self.string_value(self.find(inner, "StringLiteral"));
787                let part = self.intern(&text);
788                self.part_slice(vec![part])
789            }
790            _ => return self.unsupported(inner),
791        };
792        Ok(self.push_source(Source::Table { name, alias: NONE, columns: Slice::default() }))
793    }
794
795    /// `SELECT * FROM <source>`, which is what `DESCRIBE t` means.
796    fn star_over(&mut self, source: SourceRef) -> QueryRef {
797        let star =
798            self.push(Expr::Star { qualifier: Slice::default(), replacements: Slice::default() });
799        let targets = self.target_slice(vec![Target { expr: star, alias: NONE }]);
800        let start = self.ast.source_lists.len() as u32;
801        self.ast.source_lists.push(source);
802        let from = Slice { start, len: 1 };
803        let select = self.push_select(Select { targets, from, ..Select::empty() });
804        self.push_query(Query::bare(QueryBody::Select(select)))
805    }
806
807    /// `ValuesClause <- 'VALUES' List(ValuesExpressions)`, each of which is `Parens(List(Expression))`.
808    ///
809    /// The rows are not checked against each other for width here. Two rows of different widths
810    /// parse, and saying so is the binder's job, because the message wants to name the column count
811    /// it expected and the parser does not know it for `INSERT` where the table decides.
812    fn values_clause(&mut self, node: u32) -> Result<Slice> {
813        let mut rows = Vec::new();
814        for kid in self.kids(node) {
815            if self.name(kid) != "ValuesExpressions" {
816                continue;
817            }
818            let mut items = Vec::new();
819            for expr in self.kids(kid) {
820                items.push(self.expr(expr)?);
821            }
822            let slice = self.expr_slice(items);
823            rows.push(slice);
824        }
825        let start = self.ast.rows.len() as u32;
826        self.ast.rows.extend(rows);
827        Ok(Slice { start, len: self.ast.rows.len() as u32 - start })
828    }
829
830    /// `OptionalParensSimpleSelect <- SimpleSelectParens / SimpleSelect`, down to the select.
831    fn unwrap_parens(&self, node: u32) -> u32 {
832        let mut node = self.first(node);
833        while self.name(node) == "SimpleSelectParens" {
834            node = self.first(node);
835        }
836        node
837    }
838
839    /// `ResultModifiers <- OrderByClause? LimitOffset?`.
840    fn result_modifiers(&mut self, query: QueryRef, node: u32) -> Result<()> {
841        let order = self.find(node, "OrderByClause");
842        if order != NONE {
843            let (items, all) = self.order_by(order)?;
844            let start = self.ast.order_items.len() as u32;
845            self.ast.order_items.extend(items);
846            self.ast.queries[query as usize].order_by =
847                Slice { start, len: self.ast.order_items.len() as u32 - start };
848            self.ast.queries[query as usize].order_by_all = all;
849        }
850        let limit = self.find(node, "LimitOffset");
851        if limit != NONE {
852            self.limit_offset(query, self.first(limit))?;
853        }
854        Ok(())
855    }
856
857    /// The four spellings of a limit and an offset, in either order and either one alone.
858    fn limit_offset(&mut self, query: QueryRef, node: u32) -> Result<()> {
859        match self.name(node) {
860            "LimitOffsetClause" | "OffsetLimitClause" => {
861                let limit = self.find(node, "LimitClause");
862                if limit != NONE {
863                    self.limit(query, limit)?;
864                }
865                let offset = self.find(node, "OffsetClause");
866                if offset != NONE {
867                    self.offset(query, offset)?;
868                }
869                Ok(())
870            }
871            _ => self.unsupported(node),
872        }
873    }
874
875    /// `LimitClause <- 'LIMIT' LimitValue`.
876    fn limit(&mut self, query: QueryRef, node: u32) -> Result<()> {
877        let value = self.first(node);
878        let inner = self.first(value);
879        match self.name(inner) {
880            // `LIMIT ALL` is no limit at all, which is what an absent limit already means.
881            "LimitAll" => Ok(()),
882            // `LimitExpression <- Expression '%'?`. The percent sign is a terminal so it leaves no
883            // node behind, and the only thing that says it was written is the text of the rule that
884            // matched it.
885            "LimitExpression" => {
886                let expr = self.expr(self.first(inner))?;
887                self.ast.queries[query as usize].limit = expr;
888                self.ast.queries[query as usize].limit_percent = self.text(inner).ends_with('%');
889                Ok(())
890            }
891            "LimitLiteralPercent" => {
892                let expr = self.expr(self.first(inner))?;
893                self.ast.queries[query as usize].limit = expr;
894                self.ast.queries[query as usize].limit_percent = true;
895                Ok(())
896            }
897            _ => self.unsupported(inner),
898        }
899    }
900
901    /// `OffsetClause <- 'OFFSET' OffsetValue`, where `OffsetValue <- Expression RowOrRows?`.
902    fn offset(&mut self, query: QueryRef, node: u32) -> Result<()> {
903        let value = self.first(node);
904        let expr = self.expr(self.first(value))?;
905        self.ast.queries[query as usize].offset = expr;
906        Ok(())
907    }
908
909    /// `SimpleSelect <- SelectFrom WhereClause? GroupByClause? HavingClause? WindowClause?
910    /// QualifyClause? SampleClause?`.
911    fn simple_select(&mut self, node: u32) -> Result<SelectRef> {
912        for name in ["WindowClause", "QualifyClause", "SampleClause"] {
913            let clause = self.find(node, name);
914            if clause != NONE {
915                return self.unsupported(clause);
916            }
917        }
918        let mut select = Select::empty();
919        self.select_from(&mut select, self.first(node))?;
920        let filter = self.find(node, "WhereClause");
921        if filter != NONE {
922            select.filter = self.expr(self.first(filter))?;
923        }
924        let group = self.find(node, "GroupByClause");
925        if group != NONE {
926            self.group_by(&mut select, self.first(group))?;
927        }
928        let having = self.find(node, "HavingClause");
929        if having != NONE {
930            select.having = self.expr(self.first(having))?;
931        }
932        Ok(self.push_select(select))
933    }
934
935    /// `SelectFrom <- SelectFromClause / FromSelectClause`, which is `SELECT ... FROM ...` and
936    /// DuckDB's `FROM ... SELECT ...` written the other way round.
937    fn select_from(&mut self, select: &mut Select, node: u32) -> Result<()> {
938        let clause = self.first(node);
939        let targets = self.find(clause, "SelectClause");
940        let from = self.find(clause, "FromClause");
941        if from != NONE {
942            select.from = self.sources(from)?;
943        }
944        if targets == NONE {
945            // `FROM t` on its own. DuckDB reads it as `SELECT * FROM t`, and inventing the star
946            // here rather than in the binder keeps the binder from having to know the shape of the
947            // clause that was missing.
948            let star = self
949                .push(Expr::Star { qualifier: Slice::default(), replacements: Slice::default() });
950            let start = self.ast.targets.len() as u32;
951            self.ast.targets.push(Target { expr: star, alias: NONE });
952            select.targets = Slice { start, len: 1 };
953            return Ok(());
954        }
955        self.select_clause(select, targets)
956    }
957
958    /// `SelectClause <- 'SELECT' DistinctClause? TargetList?`.
959    fn select_clause(&mut self, select: &mut Select, node: u32) -> Result<()> {
960        let distinct = self.find(node, "DistinctClause");
961        if distinct != NONE {
962            let inner = self.first(distinct);
963            select.distinct = match self.name(inner) {
964                // `SELECT ALL` is the default spelled out.
965                "DistinctAll" => Distinct::No,
966                "DistinctOn" => {
967                    let on = self.find(inner, "DistinctOnTargets");
968                    if on == NONE {
969                        Distinct::Yes
970                    } else {
971                        let mut items = Vec::new();
972                        for kid in self.kids(on) {
973                            items.push(self.expr(kid)?);
974                        }
975                        Distinct::On(self.expr_slice(items))
976                    }
977                }
978                _ => return self.unsupported(inner),
979            };
980        }
981        let list = self.find(node, "TargetList");
982        if list == NONE {
983            return Ok(());
984        }
985        let mut targets = Vec::new();
986        for kid in self.kids(list) {
987            targets.push(self.target(kid)?);
988        }
989        select.targets = self.target_slice(targets);
990        Ok(())
991    }
992
993    /// `AliasedExpression <- ColIdExpression / ExpressionAsCollabel / ExpressionOptIdentifier`.
994    fn target(&mut self, node: u32) -> Result<Target> {
995        let inner = self.first(node);
996        match self.name(inner) {
997            // `ColIdExpression <- ColId ':' Expression`, the alias written first.
998            "ColIdExpression" => {
999                let alias = self.identifier(self.first(inner));
1000                let expr = self.expr(self.nth(inner, 1))?;
1001                Ok(Target { expr, alias })
1002            }
1003            "ExpressionAsCollabel" => {
1004                let expr = self.expr(self.first(inner))?;
1005                let alias = self.identifier(self.nth(inner, 1));
1006                Ok(Target { expr, alias })
1007            }
1008            "ExpressionOptIdentifier" => {
1009                let expr = self.expr(self.first(inner))?;
1010                let alias =
1011                    if self.count(inner) > 1 { self.identifier(self.nth(inner, 1)) } else { NONE };
1012                Ok(Target { expr, alias })
1013            }
1014            _ => self.unsupported(inner),
1015        }
1016    }
1017
1018    /// `GroupByClause <- 'GROUP' 'BY' GroupByExpressions`.
1019    fn group_by(&mut self, select: &mut Select, node: u32) -> Result<()> {
1020        let inner = self.first(node);
1021        match self.name(inner) {
1022            "GroupByAll" => {
1023                select.group_by_all = true;
1024                Ok(())
1025            }
1026            "GroupByList" => {
1027                let mut items = Vec::new();
1028                for kid in self.kids(inner) {
1029                    // `GroupByExpression <- EmptyGroupingItem / CubeOrRollupClause /
1030                    // GroupingSetsClause / GroupByBaseExpression`.
1031                    let expression = self.first(kid);
1032                    if self.name(expression) != "GroupByBaseExpression" {
1033                        return self.unsupported(expression);
1034                    }
1035                    items.push(self.expr(self.first(expression))?);
1036                }
1037                select.group_by = self.expr_slice(items);
1038                Ok(())
1039            }
1040            _ => self.unsupported(inner),
1041        }
1042    }
1043
1044    /// `OrderByClause <- 'ORDER' 'BY' OrderByExpressions`, where `OrderByExpressions <- OrderByAll
1045    /// / OrderByExpressionList`.
1046    fn order_by(&mut self, node: u32) -> Result<(Vec<OrderItem>, bool)> {
1047        let inner = self.first(self.first(node));
1048        match self.name(inner) {
1049            "OrderByAll" => {
1050                let (order, nulls) = self.sort_options(inner);
1051                Ok((vec![OrderItem { expr: NONE, order, nulls }], true))
1052            }
1053            "OrderByExpressionList" => {
1054                let mut items = Vec::new();
1055                for kid in self.kids(inner) {
1056                    // `OrderByExpression <- Expression DescOrAsc? NullsFirstOrLast?`.
1057                    let expr = self.expr(self.first(kid))?;
1058                    let (order, nulls) = self.sort_options(kid);
1059                    items.push(OrderItem { expr, order, nulls });
1060                }
1061                Ok((items, false))
1062            }
1063            _ => self.unsupported(inner),
1064        }
1065    }
1066
1067    /// The direction and the null placement of one sort key, either of which may be unwritten.
1068    fn sort_options(&self, node: u32) -> (Order, Nulls) {
1069        let direction = self.find(node, "DescOrAsc");
1070        let order = if direction == NONE {
1071            Order::Unstated
1072        } else if self.name(self.first(direction)) == "DescendingOrder" {
1073            Order::Descending
1074        } else {
1075            Order::Ascending
1076        };
1077        let placement = self.find(node, "NullsFirstOrLast");
1078        let nulls = if placement == NONE {
1079            Nulls::Unstated
1080        } else if self.name(self.first(placement)) == "NullsFirst" {
1081            Nulls::First
1082        } else {
1083            Nulls::Last
1084        };
1085        (order, nulls)
1086    }
1087
1088    // From clauses.
1089
1090    /// `FromClause <- 'FROM' List(TableRef)`.
1091    fn sources(&mut self, node: u32) -> Result<Slice> {
1092        let mut items = Vec::new();
1093        for kid in self.kids(node) {
1094            items.push(self.table_ref(kid)?);
1095        }
1096        let start = self.ast.source_lists.len() as u32;
1097        self.ast.source_lists.extend(items);
1098        Ok(Slice { start, len: self.ast.source_lists.len() as u32 - start })
1099    }
1100
1101    /// `TableRef <- InnerTableRef JoinOrPivot*`, left associative like the set operators.
1102    fn table_ref(&mut self, node: u32) -> Result<SourceRef> {
1103        let mut kids = self.kids(node);
1104        let head = kids.next().unwrap_or(NONE);
1105        let mut left = self.inner_table_ref(head)?;
1106        for tail in kids {
1107            let clause = self.first(tail);
1108            if self.name(clause) != "JoinClause" {
1109                return self.unsupported(clause);
1110            }
1111            left = self.join(left, self.first(clause))?;
1112        }
1113        Ok(left)
1114    }
1115
1116    /// `InnerTableRef <- ValuesRef / TableFunction / TableSubquery / BaseTableRef / ParensTableRef`.
1117    fn inner_table_ref(&mut self, node: u32) -> Result<SourceRef> {
1118        let inner = if self.name(node) == "InnerTableRef" { self.first(node) } else { node };
1119        match self.name(inner) {
1120            "BaseTableRef" => {
1121                if self.find(inner, "TableAliasColon") != NONE {
1122                    return self.unsupported(inner);
1123                }
1124                for name in ["AtClause", "SampleClause"] {
1125                    let clause = self.find(inner, name);
1126                    if clause != NONE {
1127                        return self.unsupported(clause);
1128                    }
1129                }
1130                let name = self.name_parts(self.find(inner, "BaseTableName"));
1131                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
1132                Ok(self.push_source(Source::Table { name, alias, columns }))
1133            }
1134            "TableSubquery" => {
1135                if self.find(inner, "TableAliasColon") != NONE
1136                    || self.find(inner, "Lateral") != NONE
1137                {
1138                    return self.unsupported(inner);
1139                }
1140                // `SubqueryReference <- Parens(SelectStatementInternal)`.
1141                let reference = self.find(inner, "SubqueryReference");
1142                let query = self.query(self.first(reference))?;
1143                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
1144                Ok(self.push_source(Source::Subquery { query, alias, columns }))
1145            }
1146            // `TableFunction <- TableFunctionLateralOpt / TableFunctionAliasColon`, and
1147            // `TableFunctionLateralOpt <- Lateral? QualifiedTableFunction TableFunctionArguments
1148            // WithOrdinality? TableAlias?`. The colon form and `LATERAL` are their own work, and
1149            // `WITH ORDINALITY` adds a column, so all three are turned away rather than dropped.
1150            "TableFunction" => {
1151                let form = self.first(inner);
1152                for name in ["TableAliasColon", "Lateral", "WithOrdinality", "SampleClause"] {
1153                    let clause = self.find(form, name);
1154                    if clause != NONE {
1155                        return self.unsupported(clause);
1156                    }
1157                }
1158                let name = self.name_parts(self.find(form, "QualifiedTableFunction"));
1159                let mut args = Vec::new();
1160                // `TableFunctionArguments <- Parens(List(FunctionArgument)?)`, so a call with no
1161                // arguments has the wrapper and no list under it.
1162                let list = self.find(form, "TableFunctionArguments");
1163                for kid in self.kids(list) {
1164                    args.push(self.table_argument(kid)?);
1165                }
1166                let args = self.target_slice(args);
1167                let (alias, columns) = self.table_alias(self.find(form, "TableAlias"));
1168                Ok(self.push_source(Source::Function { name, args, alias, columns }))
1169            }
1170            "ValuesRef" => {
1171                if self.find(inner, "TableAliasColon") != NONE {
1172                    return self.unsupported(inner);
1173                }
1174                let rows = self.values_clause(self.find(inner, "ValuesClause"))?;
1175                let (alias, columns) = self.table_alias(self.find(inner, "TableAlias"));
1176                Ok(self.push_source(Source::Values { rows, alias, columns }))
1177            }
1178            "ParensTableRef" => {
1179                if self.find(inner, "TableAliasColon") != NONE
1180                    || self.find(inner, "SampleClause") != NONE
1181                    || self.find(inner, "TableAlias") != NONE
1182                {
1183                    return self.unsupported(inner);
1184                }
1185                self.table_ref(self.find(inner, "TableRef"))
1186            }
1187            _ => self.unsupported(inner),
1188        }
1189    }
1190
1191    /// `TableAlias <- TableAliasAs / TableAliasWithoutAs`, either with a column alias list.
1192    fn table_alias(&mut self, node: u32) -> (StrRef, Slice) {
1193        if node == NONE {
1194            return (NONE, Slice::default());
1195        }
1196        let inner = self.first(node);
1197        let alias = self.identifier(self.first(inner));
1198        let list = self.find(inner, "ColumnAliases");
1199        if list == NONE {
1200            return (alias, Slice::default());
1201        }
1202        let mut columns = Vec::new();
1203        for kid in self.kids(list) {
1204            let name = self.identifier(kid);
1205            columns.push(name);
1206        }
1207        (alias, self.part_slice(columns))
1208    }
1209
1210    /// `JoinClause <- JoinByClause / RegularJoinClause / JoinWithoutOnClause / NearestJoinClause`.
1211    fn join(&mut self, left: SourceRef, node: u32) -> Result<SourceRef> {
1212        match self.name(node) {
1213            // `RegularJoinClause <- Asof? JoinType? 'JOIN' TableRef JoinQualifier`.
1214            "RegularJoinClause" => {
1215                if self.find(node, "Asof") != NONE {
1216                    return self.unsupported(node);
1217                }
1218                let kind = self.join_type(self.find(node, "JoinType"));
1219                let right = self.table_ref(self.find(node, "TableRef"))?;
1220                let (on, using) = self.join_qualifier(self.find(node, "JoinQualifier"))?;
1221                Ok(self.push_source(Source::Join { left, right, kind, natural: false, on, using }))
1222            }
1223            // `JoinWithoutOnClause <- JoinPrefix 'JOIN' InnerTableRef`, which is cross, natural and
1224            // positional. Those three are exactly the joins that carry no condition.
1225            "JoinWithoutOnClause" => {
1226                let prefix = self.first(self.find(node, "JoinPrefix"));
1227                let (kind, natural) = match self.name(prefix) {
1228                    "CrossJoinPrefix" => (JoinKind::Cross, false),
1229                    "PositionalJoinPrefix" => (JoinKind::Positional, false),
1230                    "NaturalJoinPrefix" => (self.join_type(self.find(prefix, "JoinType")), true),
1231                    _ => return self.unsupported(prefix),
1232                };
1233                let right = self.inner_table_ref(self.find(node, "InnerTableRef"))?;
1234                Ok(self.push_source(Source::Join {
1235                    left,
1236                    right,
1237                    kind,
1238                    natural,
1239                    on: NONE,
1240                    using: Slice::default(),
1241                }))
1242            }
1243            _ => self.unsupported(node),
1244        }
1245    }
1246
1247    /// `JoinType <- FullJoin / LeftJoin / RightJoin / SemiJoin / AntiJoin / InnerJoin`, absent
1248    /// meaning inner, which is what SQL has always meant by a bare `JOIN`.
1249    fn join_type(&self, node: u32) -> JoinKind {
1250        if node == NONE {
1251            return JoinKind::Inner;
1252        }
1253        match self.name(self.first(node)) {
1254            "FullJoin" => JoinKind::Full,
1255            "LeftJoin" => JoinKind::Left,
1256            "RightJoin" => JoinKind::Right,
1257            "SemiJoin" => JoinKind::Semi,
1258            "AntiJoin" => JoinKind::Anti,
1259            _ => JoinKind::Inner,
1260        }
1261    }
1262
1263    /// `JoinQualifier <- OnClause / UsingClause`.
1264    fn join_qualifier(&mut self, node: u32) -> Result<(ExprRef, Slice)> {
1265        let inner = self.first(node);
1266        match self.name(inner) {
1267            "OnClause" => Ok((self.expr(self.first(inner))?, Slice::default())),
1268            "UsingClause" => {
1269                let mut columns = Vec::new();
1270                for kid in self.kids(inner) {
1271                    let name = self.identifier(kid);
1272                    columns.push(name);
1273                }
1274                Ok((NONE, self.part_slice(columns)))
1275            }
1276            _ => self.unsupported(inner),
1277        }
1278    }
1279
1280    // Expressions.
1281
1282    /// One expression, from wherever in the precedence chain it starts.
1283    ///
1284    /// The loop is the whole design. A rule that says something gets an arm, a rule with exactly
1285    /// one child said nothing and is stepped through, and anything else is an error naming itself.
1286    /// The chain rules never get an arm for their one child case, which is why adding a precedence
1287    /// level upstream costs nothing here.
1288    fn expr(&mut self, node: u32) -> Result<ExprRef> {
1289        let mut node = node;
1290        loop {
1291            let count = self.count(node);
1292            let name = self.name(node);
1293            match name {
1294                "LogicalOrExpression" if count > 1 => return self.logical(node, BinaryOp::Or),
1295                "LogicalAndExpression" if count > 1 => return self.logical(node, BinaryOp::And),
1296                "LogicalNotExpression" if count > 1 => return self.logical_not(node),
1297                "IsExpression" if count > 1 => return self.is_expression(node),
1298                "BetweenInLikeExpression" if count > 1 => return self.between_in_like(node),
1299                "PrefixExpression" if count > 1 => return self.prefix(node),
1300                "BaseExpression" if count > 1 => return self.indirection(node),
1301                "LambdaArrowExpression"
1302                | "IsDistinctFromExpression"
1303                | "ComparisonExpression"
1304                | "OtherOperatorExpression"
1305                | "BitwiseExpression"
1306                | "AdditiveExpression"
1307                | "MultiplicativeExpression"
1308                | "ExponentiationExpression"
1309                | "CollateExpression"
1310                | "AtTimeZoneExpression"
1311                    if count > 1 =>
1312                {
1313                    return self.tail_chain(node);
1314                }
1315                "ColumnReference" => {
1316                    let name = self.name_parts(node);
1317                    return Ok(self.push(Expr::Column { name }));
1318                }
1319                "StarExpression" => return self.star(node),
1320                "NumberLiteral" => {
1321                    let text = self.text(node).to_string();
1322                    let text = self.intern(&text);
1323                    return Ok(self.push(Expr::Literal { kind: LiteralKind::Number, text }));
1324                }
1325                "StringLiteral" => {
1326                    let text = self.string_value(node);
1327                    let text = self.intern(&text);
1328                    return Ok(self.push(Expr::Literal { kind: LiteralKind::String, text }));
1329                }
1330                "NullLiteral" | "TrueLiteral" | "FalseLiteral" => {
1331                    let kind = match name {
1332                        "NullLiteral" => LiteralKind::Null,
1333                        "TrueLiteral" => LiteralKind::True,
1334                        _ => LiteralKind::False,
1335                    };
1336                    return Ok(self.push(Expr::Literal { kind, text: NONE }));
1337                }
1338                "FunctionExpression" => return self.function(node),
1339                "ExtractExpression" => return self.extract(node),
1340                "CastExpression" => return self.cast(node),
1341                "CaseExpression" => return self.case(node),
1342                "ParenthesisExpression" => return self.row(node),
1343                "BoundedListExpression" => return self.list(node),
1344                "QuestionMarkNumberedParameter"
1345                | "AnonymousParameter"
1346                | "NumberedParameter"
1347                | "ColLabelParameter" => return self.parameter(node),
1348                "SubqueryExpression" => return self.subquery(node),
1349                _ if count == 1 => node = self.first(node),
1350                _ => return self.unsupported(node),
1351            }
1352        }
1353    }
1354
1355    /// `X <- Y XTail*` where `XTail <- Operator Y`, the shape ten precedence levels share.
1356    fn tail_chain(&mut self, node: u32) -> Result<ExprRef> {
1357        let mut kids = self.kids(node);
1358        let head = kids.next().unwrap_or(NONE);
1359        let mut left = self.expr(head)?;
1360        for tail in kids {
1361            let operator = self.first(tail);
1362            let op = self.binary_op(operator)?;
1363            // `ComparisonExpressionTail <- ComparisonOperator NotExpression? BetweenInLikeExpression`
1364            // is the one tail with an optional middle, so the operand is the last child and not the
1365            // second one. Taking the last is right for every tail and wrong for none.
1366            let operand = self.kids(tail).last().unwrap_or(NONE);
1367            if self.count(tail) > 2 {
1368                return self.unsupported(tail);
1369            }
1370            let right = self.expr(operand)?;
1371            left = self.push(Expr::Binary { op, left, right });
1372        }
1373        Ok(left)
1374    }
1375
1376    /// Which infix operator a tail's operator node is.
1377    fn binary_op(&mut self, node: u32) -> Result<BinaryOp> {
1378        // The operator rules nest: `ComparisonOperator` over `OperatorGreaterThan` over the symbol
1379        // itself. Every one of them covers the same tokens, so the text is the same at every level
1380        // and reading it once at the top is enough. The name is not, which is why the bottom of the
1381        // chain is walked to as well: `OtherOperator` says nothing and `OperatorLiteral` says
1382        // everything, and they are three levels apart.
1383        let mut leaf = node;
1384        while self.count(leaf) == 1 {
1385            leaf = self.first(leaf);
1386        }
1387        let text = self.text(node);
1388        let upper = text.to_ascii_uppercase();
1389        let op = match upper.as_str() {
1390            "OR" => BinaryOp::Or,
1391            "AND" => BinaryOp::And,
1392            "=" | "==" => BinaryOp::Eq,
1393            "!=" | "<>" => BinaryOp::NotEq,
1394            "<" => BinaryOp::Lt,
1395            ">" => BinaryOp::Gt,
1396            "<=" => BinaryOp::LtEq,
1397            ">=" => BinaryOp::GtEq,
1398            "+" => BinaryOp::Add,
1399            "-" => BinaryOp::Subtract,
1400            "*" => BinaryOp::Multiply,
1401            "/" => BinaryOp::Divide,
1402            "//" => BinaryOp::IntegerDivide,
1403            "%" => BinaryOp::Modulo,
1404            "^" | "**" => BinaryOp::Power,
1405            "&" => BinaryOp::BitAnd,
1406            "|" => BinaryOp::BitOr,
1407            "<<" => BinaryOp::ShiftLeft,
1408            ">>" => BinaryOp::ShiftRight,
1409            "||" => BinaryOp::Concat,
1410            "COLLATE" => BinaryOp::Collate,
1411            "->" => BinaryOp::Arrow,
1412            "->>" => BinaryOp::LongArrow,
1413            "@>" => BinaryOp::Contains,
1414            "<@" => BinaryOp::ContainedBy,
1415            "&&" => BinaryOp::Overlaps,
1416            "^@" => BinaryOp::StartsWith,
1417            "<<=" => BinaryOp::InetContainedByOrEq,
1418            ">>=" => BinaryOp::InetContainsOrEq,
1419            _ if self.name(leaf) == "AtTimeZoneOperator" => BinaryOp::AtTimeZone,
1420            // `IsDistinctFromOp <- 'IS' 'NOT'? 'DISTINCT' 'FROM'`, told apart by the middle word,
1421            // which is not in the tree because keywords are terminals.
1422            _ if self.name(leaf) == "IsDistinctFromOp" => {
1423                if upper.split_whitespace().any(|word| word == "NOT") {
1424                    BinaryOp::IsNotDistinctFrom
1425                } else {
1426                    BinaryOp::IsDistinctFrom
1427                }
1428            }
1429            // `OperatorLiteral` is the open end of the operator set. Its body in the grammar text
1430            // says `Identifier`, but it is one of the 24 rules whose body the matcher does not
1431            // walk and the matcher it is overridden to is the bare operator one, so what it
1432            // actually accepts is any run of operator characters that is not already a token.
1433            // `a <=> b` is such a run, DuckDB resolves it as a two argument function of that name,
1434            // and rejecting it here would reject SQL DuckDB accepts.
1435            _ if self.name(leaf) == "OperatorLiteral" => {
1436                let interned = self.intern(text);
1437                BinaryOp::Named(interned)
1438            }
1439            _ => return self.unsupported(node),
1440        };
1441        Ok(op)
1442    }
1443
1444    /// `LogicalOrExpression <- LogicalAndExpression LogicalOrExpressionTail*`, and the `AND` twin.
1445    ///
1446    /// Separate from the other tails because the tail here is `'OR' LogicalAndExpression` with the
1447    /// keyword as a terminal, so there is no operator node to read and the operator is the rule.
1448    fn logical(&mut self, node: u32, op: BinaryOp) -> Result<ExprRef> {
1449        let mut kids = self.kids(node);
1450        let head = kids.next().unwrap_or(NONE);
1451        let mut left = self.expr(head)?;
1452        for tail in kids {
1453            let right = self.expr(self.first(tail))?;
1454            left = self.push(Expr::Binary { op, left, right });
1455        }
1456        Ok(left)
1457    }
1458
1459    /// `LogicalNotExpression <- NotExpression? IsExpression`, where `NotExpression <- NotKeyword+`.
1460    ///
1461    /// The plus matters. `NOT NOT x` is two nodes in the parse tree and two negations in the AST,
1462    /// and folding them here would be an optimizer decision taken in the parser.
1463    fn logical_not(&mut self, node: u32) -> Result<ExprRef> {
1464        let negations = self.count(self.first(node));
1465        let mut expr = self.expr(self.nth(node, 1))?;
1466        for _ in 0..negations {
1467            expr = self.push(Expr::Unary { op: UnaryOp::Not, operand: expr });
1468        }
1469        Ok(expr)
1470    }
1471
1472    /// `IsExpression <- IsDistinctFromExpression IsTest*`, the postfix null and boolean tests.
1473    fn is_expression(&mut self, node: u32) -> Result<ExprRef> {
1474        let mut kids = self.kids(node);
1475        let head = kids.next().unwrap_or(NONE);
1476        let mut expr = self.expr(head)?;
1477        for test in kids {
1478            let inner = self.first(test);
1479            let negated = self.text(inner).to_ascii_uppercase().contains("NOT");
1480            let op = match self.name(inner) {
1481                "NotNull" => UnaryOp::IsNotNull,
1482                "IsNull" => UnaryOp::IsNull,
1483                // `IsLiteral <- 'IS' 'NOT'? IsLiteralValue`, and the value rule is one more level
1484                // down again because it is a choice of four and not four alternatives inlined.
1485                "IsLiteral" => match self.name(self.first(self.first(inner))) {
1486                    "NullLiteral" if negated => UnaryOp::IsNotNull,
1487                    "NullLiteral" => UnaryOp::IsNull,
1488                    "TrueLiteral" if negated => UnaryOp::IsNotTrue,
1489                    "TrueLiteral" => UnaryOp::IsTrue,
1490                    "FalseLiteral" if negated => UnaryOp::IsNotFalse,
1491                    "FalseLiteral" => UnaryOp::IsFalse,
1492                    "UnknownLiteral" if negated => UnaryOp::IsNotUnknown,
1493                    "UnknownLiteral" => UnaryOp::IsUnknown,
1494                    _ => return self.unsupported(inner),
1495                },
1496                _ => return self.unsupported(inner),
1497            };
1498            expr = self.push(Expr::Unary { op, operand: expr });
1499        }
1500        Ok(expr)
1501    }
1502
1503    /// `BetweenInLikeExpression <- OtherOperatorExpression BetweenInLikeOp?`.
1504    fn between_in_like(&mut self, node: u32) -> Result<ExprRef> {
1505        let operand = self.expr(self.first(node))?;
1506        // `BetweenInLikeOp <- 'NOT'? BetweenInLikeOpExpression`. The `NOT` is a terminal, so what
1507        // says it was written is that the op node covers a token the inner node does not.
1508        let op = self.nth(node, 1);
1509        let negated = self.text(op).to_ascii_uppercase().starts_with("NOT");
1510        let inner = self.first(self.first(op));
1511        match self.name(inner) {
1512            // `BetweenClause <- 'BETWEEN' x 'AND' y`.
1513            "BetweenClause" => {
1514                let low = self.expr(self.first(inner))?;
1515                let high = self.expr(self.nth(inner, 1))?;
1516                Ok(self.push(Expr::Between { operand, low, high, negated }))
1517            }
1518            // `InClause <- 'IN' InExpression`.
1519            "InClause" => {
1520                let expression = self.first(self.first(inner));
1521                match self.name(expression) {
1522                    "InExpressionList" => {
1523                        let mut items = Vec::new();
1524                        for kid in self.kids(expression) {
1525                            items.push(self.expr(kid)?);
1526                        }
1527                        let list = self.expr_slice(items);
1528                        Ok(self.push(Expr::In { operand, list, negated }))
1529                    }
1530                    _ => self.unsupported(expression),
1531                }
1532            }
1533            // `LikeClause <- LikeVariations x EscapeClause?`.
1534            "LikeClause" => {
1535                if self.find(inner, "EscapeClause") != NONE {
1536                    return self.unsupported(inner);
1537                }
1538                let variation = self.name(self.first(self.first(inner)));
1539                let op = match (variation, negated) {
1540                    ("LikeToken", false) | ("NotLikeOp", true) => BinaryOp::Like,
1541                    ("LikeToken", true) | ("NotLikeOp", false) => BinaryOp::NotLike,
1542                    ("ILikeToken", false) | ("NotILikeOp", true) => BinaryOp::ILike,
1543                    ("ILikeToken", true) | ("NotILikeOp", false) => BinaryOp::NotILike,
1544                    // Glob and the bare regex match have no negated spelling of their own in
1545                    // `LikeVariations`, so a `NOT` in front of either stays an explicit negation.
1546                    ("GlobToken", _) => BinaryOp::Glob,
1547                    ("RegexMatchToken", _) => BinaryOp::Regex,
1548                    ("SimilarToToken", false) | ("NotSimilarToOp", true) => BinaryOp::SimilarTo,
1549                    ("SimilarToToken", true) | ("NotSimilarToOp", false) => BinaryOp::NotSimilarTo,
1550                    ("RegexInsensitiveMatchToken", false)
1551                    | ("NotRegexInsensitiveMatchOp", true) => BinaryOp::RegexInsensitive,
1552                    ("RegexInsensitiveMatchToken", true)
1553                    | ("NotRegexInsensitiveMatchOp", false) => BinaryOp::NotRegexInsensitive,
1554                    _ => return self.unsupported(inner),
1555                };
1556                let right = self.expr(self.nth(inner, 1))?;
1557                let expr = self.push(Expr::Binary { op, left: operand, right });
1558                // The like family folds its negation into the operator because it has a spelling
1559                // for the negated form. Glob and regex do not, so theirs stays where it was.
1560                if negated && matches!(op, BinaryOp::Glob | BinaryOp::Regex) {
1561                    return Ok(self.push(Expr::Unary { op: UnaryOp::Not, operand: expr }));
1562                }
1563                Ok(expr)
1564            }
1565            _ => self.unsupported(inner),
1566        }
1567    }
1568
1569    /// `PrefixExpression <- PrefixOperator* BaseExpression`, applied right to left.
1570    fn prefix(&mut self, node: u32) -> Result<ExprRef> {
1571        let kids: Vec<u32> = self.kids(node).collect();
1572        let mut expr = self.expr(kids[kids.len() - 1])?;
1573        for &operator in kids[..kids.len() - 1].iter().rev() {
1574            let op = match self.name(self.first(operator)) {
1575                "MinusPrefixOperator" => UnaryOp::Negate,
1576                "PlusPrefixOperator" => UnaryOp::Plus,
1577                "TildePrefixOperator" => UnaryOp::BitNot,
1578                _ => return self.unsupported(operator),
1579            };
1580            expr = self.push(Expr::Unary { op, operand: expr });
1581        }
1582        Ok(expr)
1583    }
1584
1585    /// `BaseExpression <- SingleExpression IndirectionList?`, the postfix chain.
1586    fn indirection(&mut self, node: u32) -> Result<ExprRef> {
1587        let mut expr = self.expr(self.first(node))?;
1588        for step in self.kids(self.nth(node, 1)) {
1589            let inner = self.first(step);
1590            expr = match self.name(inner) {
1591                // `CastOperator <- '::' Type`.
1592                "CastOperator" => {
1593                    let text = self.text(self.first(inner)).to_string();
1594                    let ty = self.intern(&text);
1595                    self.push(Expr::Cast { operand: expr, ty, try_cast: false })
1596                }
1597                "DotOperator" => {
1598                    let dot = self.first(inner);
1599                    match self.name(dot) {
1600                        // `DotColumnOperator <- '.' ColLabel`, which DuckDB resolves as a call of
1601                        // `struct_extract`. Writing it as that call rather than as its own node
1602                        // keeps the binder from needing a rule for a thing that is already a
1603                        // function.
1604                        "DotColumnOperator" => {
1605                            let field = self.identifier(self.first(dot));
1606                            let text = self.ast.string(field).to_string();
1607                            let literal = self.intern(&text);
1608                            let key = self
1609                                .push(Expr::Literal { kind: LiteralKind::String, text: literal });
1610                            let name = self.function_name("struct_extract");
1611                            let args = self.expr_slice(vec![expr, key]);
1612                            self.push(Expr::Function { name, args, distinct: false })
1613                        }
1614                        // `DotMethodOperator <- '.' MethodExpression`, where `x.f(a)` is `f(x, a)`.
1615                        "DotMethodOperator" => {
1616                            let method = self.first(dot);
1617                            let text = self.text(self.first(method)).to_string();
1618                            let text = unquote(&text);
1619                            let name = self.function_name(&text);
1620                            let mut args = vec![expr];
1621                            let list = self.find(method, "MethodExpressionArguments");
1622                            if list != NONE {
1623                                let inner = self.first(list);
1624                                let arguments = self.find(inner, "MethodFunctionArguments");
1625                                if arguments != NONE {
1626                                    for kid in self.kids(arguments) {
1627                                        args.push(self.argument(kid)?);
1628                                    }
1629                                }
1630                            }
1631                            let args = self.expr_slice(args);
1632                            self.push(Expr::Function { name, args, distinct: false })
1633                        }
1634                        _ => return self.unsupported(dot),
1635                    }
1636                }
1637                // `SliceExpression <- '[' SliceBound ']'`, one index or a range.
1638                "SliceExpression" => {
1639                    let bound = self.first(inner);
1640                    let has_end = self.find(bound, "EndSliceBound") != NONE;
1641                    let has_step = self.find(bound, "StepSliceBound") != NONE;
1642                    if has_end || has_step {
1643                        return self.unsupported(inner);
1644                    }
1645                    let index = self.expr(self.first(bound))?;
1646                    let name = self.function_name("array_extract");
1647                    let args = self.expr_slice(vec![expr, index]);
1648                    self.push(Expr::Function { name, args, distinct: false })
1649                }
1650                // `PostfixOperator <- '!'`.
1651                "PostfixOperator" => {
1652                    self.push(Expr::Unary { op: UnaryOp::Factorial, operand: expr })
1653                }
1654                _ => return self.unsupported(inner),
1655            };
1656        }
1657        Ok(expr)
1658    }
1659
1660    /// A one part function name, for the calls the transformer invents rather than reads.
1661    fn function_name(&mut self, name: &str) -> Slice {
1662        let interned = self.intern(name);
1663        self.part_slice(vec![interned])
1664    }
1665
1666    /// `StarExpression <- StarQualifierList? '*' ExcludeList? ReplaceList? RenameList?`.
1667    fn star(&mut self, node: u32) -> Result<ExprRef> {
1668        for name in ["ExcludeList", "RenameList"] {
1669            let list = self.find(node, name);
1670            if list != NONE {
1671                return self.unsupported(list);
1672            }
1673        }
1674        let replace = self.find(node, "ReplaceList");
1675        let replacements =
1676            if replace == NONE { Slice::default() } else { self.replacements(replace)? };
1677        let qualifier = self.find(node, "StarQualifierList");
1678        let qualifier =
1679            if qualifier == NONE { Slice::default() } else { self.name_parts(qualifier) };
1680        Ok(self.push(Expr::Star { qualifier, replacements }))
1681    }
1682
1683    /// `ReplaceList <- 'REPLACE' ReplaceEntries`, where an entry is `Expression 'AS'
1684    /// ColumnReference` and the entries are one bare entry or a parenthesized list of them.
1685    ///
1686    /// The duplicate check is here rather than in the binder because that is where DuckDB does it:
1687    /// naming the same column twice is a Parser Error there, and it is one of the few things about
1688    /// a star that can be decided without knowing what the star stands for.
1689    fn replacements(&mut self, node: u32) -> Result<Slice> {
1690        // `ReplaceEntries <- ReplaceEntrySingle / ReplaceEntryList` and both of those hold the
1691        // entries as their own children, so the same walk reads either shape.
1692        let entries = self.first(self.first(node));
1693        let listed: Vec<u32> =
1694            self.kids(entries).filter(|&kid| self.name(kid) == "ReplaceEntry").collect();
1695        let mut replacements = Vec::with_capacity(listed.len());
1696        for entry in listed {
1697            let expr = self.expr(self.first(entry))?;
1698            let alias = self.identifier(self.nth(entry, 1));
1699            let written = self.ast.string(alias).to_string();
1700            if replacements
1701                .iter()
1702                .any(|held: &Target| self.ast.string(held.alias).eq_ignore_ascii_case(&written))
1703            {
1704                return Err(Error::parser(format!(
1705                    "Duplicate entry \"{written}\" in REPLACE list"
1706                )));
1707            }
1708            replacements.push(Target { expr, alias });
1709        }
1710        Ok(self.target_slice(replacements))
1711    }
1712
1713    /// `FunctionExpression <- FunctionIdentifier FunctionExpressionArguments WithinGroupClause?
1714    /// FilterClause? ExportClause? OverClause?`.
1715    fn function(&mut self, node: u32) -> Result<ExprRef> {
1716        for name in ["WithinGroupClause", "FilterClause", "ExportClause", "OverClause"] {
1717            let clause = self.find(node, name);
1718            if clause != NONE {
1719                return self.unsupported(clause);
1720            }
1721        }
1722        let name = self.name_parts(self.first(node));
1723        // `FunctionExpressionArguments <- Parens(FunctionExpressionArgumentList)` and
1724        // `FunctionExpressionArgumentList <- DistinctOrAll? FunctionArgumentList? OrderByClause?
1725        // IgnoreOrRespectNulls?`, so a call with no arguments still has both wrappers.
1726        let list = self.first(self.nth(node, 1));
1727        for name in ["OrderByClause", "IgnoreOrRespectNulls"] {
1728            let clause = self.find(list, name);
1729            if clause != NONE {
1730                return self.unsupported(clause);
1731            }
1732        }
1733        let distinct = self.quantifier(self.find(list, "DistinctOrAll")) == Quantifier::Distinct;
1734        let mut args = Vec::new();
1735        let arguments = self.find(list, "FunctionArgumentList");
1736        if arguments != NONE {
1737            for kid in self.kids(arguments) {
1738                args.push(self.argument(kid)?);
1739            }
1740        }
1741        let args = self.expr_slice(args);
1742        Ok(self.push(Expr::Function { name, args, distinct }))
1743    }
1744
1745    /// `ExtractExpression <- 'EXTRACT' Parens(ExtractArguments)` and
1746    /// `ExtractArguments <- ExtractArgument 'FROM' Expression`.
1747    ///
1748    /// `EXTRACT` is not a function in the grammar because its argument list is not an argument
1749    /// list, and it is a function everywhere after here because DuckDB's parser does the same
1750    /// rewrite: `EXTRACT(minute FROM t)` is `date_part('minute', t)` and there is no separate
1751    /// implementation of one of them. The part is a keyword, an identifier or a string in the
1752    /// grammar, and all three become the string, which is why this is a rewrite and not a node.
1753    fn extract(&mut self, node: u32) -> Result<ExprRef> {
1754        let arguments = self.find(node, "ExtractArguments");
1755        if arguments == NONE {
1756            return self.unsupported(node);
1757        }
1758        let argument = self.first(self.first(arguments));
1759        let part = match self.name(argument) {
1760            "ExtractStringArgument" => self.string_value(argument),
1761            // A keyword or an identifier, both taken as written. Which specifier names are legal is
1762            // not a question about syntax, so the answer to it lives with the function.
1763            "ExtractDatePartArgument" | "ExtractIdentifierArgument" => {
1764                self.text(argument).to_string()
1765            }
1766            _ => return self.unsupported(argument),
1767        };
1768        let text = self.intern(&part);
1769        let part = self.push(Expr::Literal { kind: LiteralKind::String, text });
1770        let operand = self.expr(self.nth(arguments, 1))?;
1771        let name = self.function_name("date_part");
1772        let args = self.expr_slice(vec![part, operand]);
1773        Ok(self.push(Expr::Function { name, args, distinct: false }))
1774    }
1775
1776    /// `FunctionArgument <- NamedFunctionArgument / PositionalFunctionArgument`.
1777    fn argument(&mut self, node: u32) -> Result<ExprRef> {
1778        let inner = self.first(node);
1779        match self.name(inner) {
1780            "PositionalFunctionArgument" => self.expr(self.first(inner)),
1781            _ => self.unsupported(inner),
1782        }
1783    }
1784
1785    /// One argument of a table function, which is the same rule plus the names.
1786    ///
1787    /// `NamedParameter <- TypeFuncName Type? NamedParameterAssignment Expression` and
1788    /// `NamedParameterAssignment <- ':=' / '=>'`, so those two spellings are what the grammar has.
1789    /// The binary accepts a third, `name = value`, which the grammar has no rule for because it
1790    /// parses as an equality and is picked apart afterwards. That is what happens here too: a
1791    /// positional argument that is a comparison between a bare name and something else is a named
1792    /// parameter, which is the reading upstream's own transformer gives it. `read_parquet(f,
1793    /// binary_as_string=True)` is the query that matters and it is the spelling the ClickBench
1794    /// entry uses.
1795    ///
1796    /// The name is not resolved here and neither is the value. Which parameters a function takes
1797    /// is the binder's question, and so is whether `binary_as_string=True` means anything to the
1798    /// function it was written on.
1799    fn table_argument(&mut self, node: u32) -> Result<Target> {
1800        let inner = self.first(node);
1801        if self.name(inner) == "NamedFunctionArgument" {
1802            let named = self.first(inner);
1803            if self.count(named) != 3 {
1804                // The optional `Type` between the name and the assignment, which is a macro
1805                // parameter's declaration and not a call.
1806                return self.unsupported(named);
1807            }
1808            let alias = self.identifier(self.first(named));
1809            let expr = self.expr(self.nth(named, 2))?;
1810            return Ok(Target { expr, alias });
1811        }
1812        let expr = self.expr(self.first(inner))?;
1813        if let Expr::Binary { op: BinaryOp::Eq, left, right } = self.ast.expr(expr) {
1814            if let Expr::Column { name } = self.ast.expr(left) {
1815                if name.len == 1 {
1816                    let alias = self.ast.parts[name.start as usize];
1817                    return Ok(Target { expr: right, alias });
1818                }
1819            }
1820        }
1821        Ok(Target { expr, alias: NONE })
1822    }
1823
1824    /// `CastExpression <- CastOrTryCast Parens(CastArguments)`.
1825    fn cast(&mut self, node: u32) -> Result<ExprRef> {
1826        let try_cast = self.name(self.first(self.first(node))) == "TryCastKeyword";
1827        // `CastArguments <- Expression 'AS' Type`.
1828        let arguments = self.nth(node, 1);
1829        let operand = self.expr(self.first(arguments))?;
1830        let text = self.text(self.nth(arguments, 1)).to_string();
1831        let ty = self.intern(&text);
1832        Ok(self.push(Expr::Cast { operand, ty, try_cast }))
1833    }
1834
1835    /// `CaseExpression <- 'CASE' Expression? CaseWhenThen+ CaseElse? 'END'`.
1836    fn case(&mut self, node: u32) -> Result<ExprRef> {
1837        let mut operand = NONE;
1838        let mut arms = Vec::new();
1839        let mut otherwise = NONE;
1840        for kid in self.kids(node) {
1841            match self.name(kid) {
1842                // `CaseWhenThen <- 'WHEN' Expression 'THEN' Expression`.
1843                "CaseWhenThen" => {
1844                    let when = self.expr(self.first(kid))?;
1845                    let then = self.expr(self.nth(kid, 1))?;
1846                    arms.push(CaseArm { when, then });
1847                }
1848                // `CaseElse <- 'ELSE' Expression`.
1849                "CaseElse" => otherwise = self.expr(self.first(kid))?,
1850                // The bare `Expression` before the first `WHEN`, which makes it a simple case.
1851                _ => operand = self.expr(kid)?,
1852            }
1853        }
1854        let start = self.ast.case_arms.len() as u32;
1855        self.ast.case_arms.extend(arms);
1856        let arms = Slice { start, len: self.ast.case_arms.len() as u32 - start };
1857        Ok(self.push(Expr::Case { operand, arms, otherwise }))
1858    }
1859
1860    /// `ParenthesisExpression <- Parens(List(Expression)?)`, which is a row value.
1861    ///
1862    /// One item is not a row. `(a)` is `a` in every dialect and reading it as a one column row
1863    /// would change what `(a) = (b)` means.
1864    fn row(&mut self, node: u32) -> Result<ExprRef> {
1865        let mut items = Vec::new();
1866        for kid in self.kids(node) {
1867            items.push(self.expr(kid)?);
1868        }
1869        if items.len() == 1 {
1870            return Ok(items[0]);
1871        }
1872        let items = self.expr_slice(items);
1873        Ok(self.push(Expr::Row { items }))
1874    }
1875
1876    /// `Parameter <- '?' Number / '?' / '$' Number / '$' ColLabel`, a prepared statement parameter.
1877    ///
1878    /// The identifier is what follows the marker, so `?1` and `$1` are both the parameter named 1,
1879    /// and a bare `?` takes the next number by where it was written. That is what DuckDB does, which
1880    /// is why `? + $2` prints as `$1 + $2`: the counting is its own and does not skip a number
1881    /// because a later parameter claimed it.
1882    fn parameter(&mut self, node: u32) -> Result<ExprRef> {
1883        let written = self.text(node).trim();
1884        let written = written.trim_start_matches(['?', '$']).trim();
1885        let name = if written.is_empty() {
1886            self.anonymous += 1;
1887            self.anonymous.to_string()
1888        } else {
1889            written.to_string()
1890        };
1891        let name = self.intern(&name);
1892        Ok(self.push(Expr::Parameter { name }))
1893    }
1894
1895    /// `BoundedListExpression <- '[' List(Expression)? ']'`, which is a LIST value.
1896    ///
1897    /// One item is a list of one here, unlike the parenthesised form, because the brackets are what
1898    /// say list and there is nothing else `[a]` could mean.
1899    fn list(&mut self, node: u32) -> Result<ExprRef> {
1900        let mut items = Vec::new();
1901        for kid in self.kids(node) {
1902            items.push(self.expr(kid)?);
1903        }
1904        let items = self.expr_slice(items);
1905        Ok(self.push(Expr::List { items }))
1906    }
1907
1908    /// `SubqueryExpression <- SubqueryNot? SubqueryExists? SubqueryReference`.
1909    fn subquery(&mut self, node: u32) -> Result<ExprRef> {
1910        if self.find(node, "SubqueryNot") != NONE || self.find(node, "SubqueryExists") != NONE {
1911            return self.unsupported(node);
1912        }
1913        let reference = self.find(node, "SubqueryReference");
1914        let query = self.query(self.first(reference))?;
1915        Ok(self.push(Expr::Subquery { query }))
1916    }
1917
1918    /// The value of a string literal, with the quotes gone and the escapes resolved.
1919    ///
1920    /// A literal can be several tokens. `'a' 'b'` on two lines is one literal that is `ab`, which is
1921    /// the SQL standard's rule and DuckDB's, so the node is decoded token by token rather than by
1922    /// taking its text and stripping the outside.
1923    fn string_value(&self, node: u32) -> String {
1924        let span = self.tree.node(node);
1925        let mut value = String::new();
1926        for token in &self.tokens[span.start as usize..span.end as usize] {
1927            if token.kind != Kind::String {
1928                continue;
1929            }
1930            let text = token.text(self.query);
1931            match text.strip_prefix('\'').and_then(|rest| rest.strip_suffix('\'')) {
1932                Some(body) => value.push_str(&body.replace("''", "'")),
1933                None => value.push_str(text),
1934            }
1935        }
1936        value
1937    }
1938}
1939
1940/// Strip the quoting off an identifier.
1941///
1942/// DuckDB does not fold identifier case at any point, quoted or not, so this only removes the
1943/// quotes and resolves the doubled ones. Anything else would be the parser deciding what a name is.
1944///
1945/// Single quotes are stripped too, and the only way one gets here is the file name in `FROM
1946/// 'hits.parquet'`, because the matcher takes a string for a name in that position and in `COPY t TO
1947/// '...'` and nowhere else. Leaving them on would make that name different from the one `FROM
1948/// "hits.parquet"` writes, and DuckDB reads both of those as the same file.
1949fn unquote(text: &str) -> String {
1950    if let Some(body) = text.strip_prefix('"').and_then(|rest| rest.strip_suffix('"')) {
1951        return body.replace("\"\"", "\"");
1952    }
1953    match text.strip_prefix('\'').and_then(|rest| rest.strip_suffix('\'')) {
1954        Some(body) => body.replace("''", "'"),
1955        None => text.to_string(),
1956    }
1957}
1958
1959#[cfg(test)]
1960mod tests {
1961    use super::*;
1962    use crate::corpus::CORPUS;
1963    use crate::matcher::parse;
1964
1965    /// The AST written back out as text, which is what the assertions below read.
1966    ///
1967    /// Not a SQL printer and not trying to be. It is deliberately not valid SQL: operators are
1968    /// spelled with the name of the variant and every binary node is parenthesised, so that a test
1969    /// asserting on this text is asserting on the shape of the tree and not on a formatting choice.
1970    /// `a - b - c` and `a - (b - c)` have to look different here or the test that tells them apart
1971    /// is not a test.
1972    fn show(ast: &Ast, expr: ExprRef) -> String {
1973        if expr == NONE {
1974            return "-".to_string();
1975        }
1976        let list = |slice: Slice| {
1977            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
1978        };
1979        match ast.expr(expr) {
1980            Expr::Star { qualifier, replacements } => {
1981                let star = if qualifier.is_empty() {
1982                    "*".to_string()
1983                } else {
1984                    format!("{}.*", ast.name_text(qualifier))
1985                };
1986                if replacements.is_empty() {
1987                    return star;
1988                }
1989                let entries: Vec<String> = ast
1990                    .target_list(replacements)
1991                    .iter()
1992                    .map(|target| {
1993                        format!("{} AS {}", show(ast, target.expr), ast.string(target.alias))
1994                    })
1995                    .collect();
1996                format!("{star} REPLACE ({})", entries.join(", "))
1997            }
1998            Expr::Column { name } => ast.name_text(name),
1999            Expr::Literal { kind, text } => match kind {
2000                LiteralKind::Number => ast.string(text).to_string(),
2001                LiteralKind::String => format!("'{}'", ast.string(text)),
2002                other => format!("{other:?}").to_uppercase(),
2003            },
2004            Expr::Unary { op, operand } => format!("({op:?} {})", show(ast, operand)),
2005            Expr::Binary { op, left, right } => {
2006                let op = match op {
2007                    BinaryOp::Named(name) => ast.string(name).to_string(),
2008                    other => format!("{other:?}"),
2009                };
2010                format!("({} {op} {})", show(ast, left), show(ast, right))
2011            }
2012            Expr::Function { name, args, distinct } => {
2013                let distinct = if distinct { "DISTINCT " } else { "" };
2014                format!("{}({distinct}{})", ast.name_text(name), list(args))
2015            }
2016            Expr::Cast { operand, ty, try_cast } => {
2017                let word = if try_cast { "TRY_CAST" } else { "CAST" };
2018                format!("{word}({} AS {})", show(ast, operand), ast.string(ty))
2019            }
2020            Expr::Case { operand, arms, otherwise } => {
2021                let arms = ast
2022                    .arm_list(arms)
2023                    .iter()
2024                    .map(|arm| format!("WHEN {} THEN {}", show(ast, arm.when), show(ast, arm.then)))
2025                    .collect::<Vec<_>>()
2026                    .join(" ");
2027                format!("CASE {} {arms} ELSE {} END", show(ast, operand), show(ast, otherwise))
2028            }
2029            Expr::Between { operand, low, high, negated } => {
2030                let not = if negated { "NOT " } else { "" };
2031                format!(
2032                    "({not}{} BETWEEN {} AND {})",
2033                    show(ast, operand),
2034                    show(ast, low),
2035                    show(ast, high)
2036                )
2037            }
2038            Expr::In { operand, list: items, negated } => {
2039                let not = if negated { "NOT " } else { "" };
2040                format!("({not}{} IN [{}])", show(ast, operand), list(items))
2041            }
2042            Expr::List { items } => format!("[{}]", list(items)),
2043            Expr::Parameter { name } => format!("${}", ast.string(name)),
2044            Expr::Row { items } => format!("ROW({})", list(items)),
2045            Expr::Subquery { query } => format!("({})", show_query(ast, query)),
2046        }
2047    }
2048
2049    /// One from item written back out.
2050    fn show_source(ast: &Ast, source: SourceRef) -> String {
2051        let alias = |alias: StrRef| match alias {
2052            NONE => String::new(),
2053            other => format!(" AS {}", ast.string(other)),
2054        };
2055        match ast.source(source) {
2056            Source::Table { name, alias: name_alias, .. } => {
2057                format!("{}{}", ast.name_text(name), alias(name_alias))
2058            }
2059            Source::Function { name, args, alias: call_alias, .. } => {
2060                let args = ast
2061                    .target_list(args)
2062                    .iter()
2063                    .map(|item| match item.alias {
2064                        NONE => show(ast, item.expr),
2065                        named => format!("{} := {}", ast.string(named), show(ast, item.expr)),
2066                    })
2067                    .collect::<Vec<_>>()
2068                    .join(", ");
2069                format!("{}({args}){}", ast.name_text(name), alias(call_alias))
2070            }
2071            Source::Subquery { query, alias: query_alias, .. } => {
2072                format!("({}){}", show_query(ast, query), alias(query_alias))
2073            }
2074            Source::Values { rows, alias: values_alias, .. } => {
2075                format!("{}{}", show_rows(ast, rows), alias(values_alias))
2076            }
2077            Source::Join { left, right, kind, natural, on, using } => {
2078                let natural = if natural { "NATURAL " } else { "" };
2079                let on = if on == NONE { String::new() } else { format!(" ON {}", show(ast, on)) };
2080                let using = if using.is_empty() {
2081                    String::new()
2082                } else {
2083                    format!(" USING ({})", ast.name_text(using))
2084                };
2085                format!(
2086                    "({} {natural}{kind:?} JOIN {}{on}{using})",
2087                    show_source(ast, left),
2088                    show_source(ast, right)
2089                )
2090            }
2091        }
2092    }
2093
2094    /// The rows of a `VALUES` written back out.
2095    fn show_rows(ast: &Ast, rows: Slice) -> String {
2096        let rows = ast
2097            .rows(rows)
2098            .iter()
2099            .map(|&row| {
2100                let items = ast
2101                    .expr_list(row)
2102                    .iter()
2103                    .map(|&item| show(ast, item))
2104                    .collect::<Vec<_>>()
2105                    .join(", ");
2106                format!("({items})")
2107            })
2108            .collect::<Vec<_>>()
2109            .join(", ");
2110        format!("VALUES {rows}")
2111    }
2112
2113    /// One query written back out.
2114    fn show_query(ast: &Ast, index: QueryRef) -> String {
2115        let query = ast.query(index);
2116        let list = |slice: Slice| {
2117            ast.expr_list(slice).iter().map(|&item| show(ast, item)).collect::<Vec<_>>().join(", ")
2118        };
2119        let mut out = match query.body {
2120            QueryBody::SetOp { op, quantifier, by_name, left, right } => {
2121                let by_name = if by_name { " BY NAME" } else { "" };
2122                format!(
2123                    "({} {op:?} {quantifier:?}{by_name} {})",
2124                    show_query(ast, left),
2125                    show_query(ast, right)
2126                )
2127            }
2128            QueryBody::Select(index) => {
2129                let select = ast.select(index);
2130                let distinct = match select.distinct {
2131                    Distinct::No => String::new(),
2132                    Distinct::Yes => " DISTINCT".to_string(),
2133                    Distinct::On(on) => format!(" DISTINCT ON ({})", list(on)),
2134                };
2135                let targets = ast
2136                    .target_list(select.targets)
2137                    .iter()
2138                    .map(|target| match target.alias {
2139                        NONE => show(ast, target.expr),
2140                        alias => format!("{} AS {}", show(ast, target.expr), ast.string(alias)),
2141                    })
2142                    .collect::<Vec<_>>()
2143                    .join(", ");
2144                let mut out = format!("SELECT{distinct} {targets}");
2145                if !select.from.is_empty() {
2146                    let from = ast
2147                        .source_list(select.from)
2148                        .iter()
2149                        .map(|&source| show_source(ast, source))
2150                        .collect::<Vec<_>>()
2151                        .join(", ");
2152                    out += &format!(" FROM {from}");
2153                }
2154                if select.filter != NONE {
2155                    out += &format!(" WHERE {}", show(ast, select.filter));
2156                }
2157                if select.group_by_all {
2158                    out += " GROUP BY ALL";
2159                } else if !select.group_by.is_empty() {
2160                    out += &format!(" GROUP BY {}", list(select.group_by));
2161                }
2162                if select.having != NONE {
2163                    out += &format!(" HAVING {}", show(ast, select.having));
2164                }
2165                out
2166            }
2167            QueryBody::Values(rows) => show_rows(ast, rows),
2168            QueryBody::Describe(inner) => format!("DESCRIBE {}", show_query(ast, inner)),
2169        };
2170        if query.order_by_all {
2171            out += " ORDER BY ALL";
2172        } else if !query.order_by.is_empty() {
2173            let items = ast
2174                .order_list(query.order_by)
2175                .iter()
2176                .map(|item| format!("{} {:?} {:?}", show(ast, item.expr), item.order, item.nulls))
2177                .collect::<Vec<_>>()
2178                .join(", ");
2179            out += &format!(" ORDER BY {items}");
2180        }
2181        if query.limit != NONE {
2182            let percent = if query.limit_percent { "%" } else { "" };
2183            out += &format!(" LIMIT {}{percent}", show(ast, query.limit));
2184        }
2185        if query.offset != NONE {
2186            out += &format!(" OFFSET {}", show(ast, query.offset));
2187        }
2188        out
2189    }
2190
2191    /// One statement, transformed and written back out.
2192    fn round(query: &str) -> String {
2193        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
2194        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
2195        let Statement::Query(index) = ast.statements[0] else {
2196            panic!("{query} is not a query");
2197        };
2198        show_query(&ast, index)
2199    }
2200
2201    /// One statement, transformed and written back out as the DDL and DML shape it is.
2202    fn round_statement(query: &str) -> String {
2203        let ast = parse_ast(query).unwrap_or_else(|error| panic!("{query}: {error}"));
2204        assert_eq!(ast.statements.len(), 1, "{query} is one statement");
2205        match ast.statements[0] {
2206            Statement::Query(index) => show_query(&ast, index),
2207            Statement::CreateTable(index) => {
2208                let create = ast.create_table(index);
2209                let mut out = "CREATE".to_string();
2210                if create.or_replace {
2211                    out += " OR REPLACE";
2212                }
2213                if create.temporary {
2214                    out += " TEMPORARY";
2215                }
2216                out += " TABLE";
2217                if create.if_not_exists {
2218                    out += " IF NOT EXISTS";
2219                }
2220                out += &format!(" {}", ast.name_text(create.name));
2221                let columns = ast
2222                    .column_defs(create.columns)
2223                    .iter()
2224                    .map(|def| {
2225                        let ty = match def.ty {
2226                            NONE => String::new(),
2227                            other => format!(" {}", ast.string(other)),
2228                        };
2229                        let null = if def.not_null { " NOT NULL" } else { "" };
2230                        format!("{}{ty}{null}", ast.string(def.name))
2231                    })
2232                    .collect::<Vec<_>>()
2233                    .join(", ");
2234                if !columns.is_empty() || create.query == NONE {
2235                    out += &format!(" ({columns})");
2236                }
2237                if create.query != NONE {
2238                    out += &format!(" AS {}", show_query(&ast, create.query));
2239                }
2240                out
2241            }
2242            Statement::CreateView(index) => {
2243                let create = ast.create_view(index);
2244                let mut out = "CREATE".to_string();
2245                if create.or_replace {
2246                    out += " OR REPLACE";
2247                }
2248                if create.temporary {
2249                    out += " TEMPORARY";
2250                }
2251                out += " VIEW";
2252                if create.if_not_exists {
2253                    out += " IF NOT EXISTS";
2254                }
2255                out += &format!(" {}", ast.name_text(create.name));
2256                if !create.columns.is_empty() {
2257                    let columns = ast.name(create.columns).collect::<Vec<_>>().join(", ");
2258                    out += &format!(" ({columns})");
2259                }
2260                out + &format!(" AS {}", show_query(&ast, create.query))
2261            }
2262            Statement::DropTable(index) => {
2263                let drop = ast.drop_table(index);
2264                let mut out = if drop.view { "DROP VIEW" } else { "DROP TABLE" }.to_string();
2265                if drop.if_exists {
2266                    out += " IF EXISTS";
2267                }
2268                let names = ast
2269                    .name_list(drop.names)
2270                    .iter()
2271                    .map(|&name| ast.name_text(name))
2272                    .collect::<Vec<_>>()
2273                    .join(", ");
2274                out + &format!(" {names}")
2275            }
2276            Statement::Insert(index) => {
2277                let insert = ast.insert(index);
2278                let mut out = format!("INSERT INTO {}", ast.name_text(insert.name));
2279                if !insert.columns.is_empty() {
2280                    let columns = ast.name(insert.columns).collect::<Vec<_>>().join(", ");
2281                    out += &format!(" ({columns})");
2282                }
2283                out + &format!(" {}", show_query(&ast, insert.source))
2284            }
2285            Statement::Set(index) => {
2286                let setting = ast.setting(index);
2287                let scope = match setting.scope.keyword() {
2288                    "" => String::new(),
2289                    word => format!(" {word}"),
2290                };
2291                format!("SET{scope} {} = {}", ast.string(setting.name), show(&ast, setting.value))
2292            }
2293            Statement::Reset(index) => {
2294                let setting = ast.setting(index);
2295                let scope = match setting.scope.keyword() {
2296                    "" => String::new(),
2297                    word => format!(" {word}"),
2298                };
2299                format!("RESET{scope} {}", ast.string(setting.name))
2300            }
2301        }
2302    }
2303
2304    #[test]
2305    fn a_set_keeps_its_name_its_scope_and_its_value() {
2306        assert_eq!(round_statement("SET memory_limit = '1GB'"), "SET memory_limit = '1GB'");
2307        assert_eq!(round_statement("set threads=4"), "SET threads = 4");
2308        assert_eq!(round_statement("SET GLOBAL threads = 4"), "SET GLOBAL threads = 4");
2309        assert_eq!(round_statement("SET SESSION threads = 4"), "SET SESSION threads = 4");
2310        assert_eq!(round_statement("SET LOCAL threads = 4"), "SET LOCAL threads = 4");
2311        assert_eq!(round_statement("RESET memory_limit"), "RESET memory_limit");
2312        assert_eq!(round_statement("RESET GLOBAL memory_limit"), "RESET GLOBAL memory_limit");
2313    }
2314
2315    #[test]
2316    fn the_two_other_things_the_word_set_starts_are_refused_rather_than_read_as_settings() {
2317        // `SET VARIABLE x = 1` declares a session variable and `SET SCHEMA` picks where an
2318        // unqualified name is looked up. Neither is a knob on the engine and reading either as one
2319        // would change an answer quietly.
2320        for statement in ["SET VARIABLE x = 1", "SET SCHEMA 'main'", "SET TIME ZONE 'UTC'"] {
2321            let error = parse_ast(statement).expect_err(statement);
2322            assert_eq!(error.code().duckdb_name(), "Not implemented Error", "{statement}");
2323        }
2324    }
2325
2326    #[test]
2327    fn a_setting_written_with_a_list_of_values_is_refused_rather_than_taking_the_first() {
2328        let error = parse_ast("SET search_path = a, b").expect_err("a list of two");
2329        assert_eq!(error.code().duckdb_name(), "Not implemented Error");
2330    }
2331
2332    #[test]
2333    fn the_query_m0_has_to_run_transforms() {
2334        assert_eq!(round("SELECT * FROM t WHERE x > 5"), "SELECT * FROM t WHERE (x Gt 5)");
2335    }
2336
2337    #[test]
2338    fn a_replace_list_rides_on_the_star_it_changes() {
2339        // The parentheses are optional around a single entry, which is how the clickbench load
2340        // recipe is not written but is how a lot of hand written sql is.
2341        assert_eq!(
2342            round("SELECT * REPLACE (a + 1 AS a) FROM t"),
2343            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
2344        );
2345        assert_eq!(
2346            round("SELECT * REPLACE a + 1 AS a FROM t"),
2347            "SELECT * REPLACE ((a Add 1) AS a) FROM t"
2348        );
2349        assert_eq!(
2350            round("SELECT t.* REPLACE (make_date(a) AS a, b * 2 AS b) FROM t"),
2351            "SELECT t.* REPLACE (make_date(a) AS a, (b Multiply 2) AS b) FROM t"
2352        );
2353    }
2354
2355    #[test]
2356    fn one_column_cannot_be_replaced_twice() {
2357        // Caught here rather than in the binder because it is a mistake in what was written and
2358        // not a mistake about what is in the table, and duckdb reports it the same way.
2359        let error = parse_ast("SELECT * REPLACE (a + 1 AS a, a + 2 AS A) FROM t").unwrap_err();
2360        assert_eq!(error.to_string(), "Parser Error: Duplicate entry \"A\" in REPLACE list");
2361    }
2362
2363    #[test]
2364    fn a_table_function_argument_can_have_a_name_written_in_front_of_it() {
2365        // The grammar has `:=` and `=>`. It does not have `=`, which parses as a comparison and is
2366        // read back apart here, and that is the spelling the clickbench load recipe uses.
2367        for spelling in
2368            ["binary_as_string := True", "binary_as_string => True", "binary_as_string = True"]
2369        {
2370            assert_eq!(
2371                round(&format!("SELECT * FROM read_parquet('f.parquet', {spelling})")),
2372                "SELECT * FROM read_parquet('f.parquet', binary_as_string := TRUE)",
2373                "{spelling}"
2374            );
2375        }
2376    }
2377
2378    #[test]
2379    fn an_equality_that_is_not_a_bare_name_stays_an_argument() {
2380        // A qualified name on the left is not a parameter name, and neither is anything that is
2381        // not a name at all, so both of those stay the comparison they were written as.
2382        assert_eq!(round("SELECT * FROM f(t.a = 1)"), "SELECT * FROM f((t.a Eq 1))");
2383        assert_eq!(round("SELECT * FROM f(1 = 1)"), "SELECT * FROM f((1 Eq 1))");
2384    }
2385
2386    #[test]
2387    fn a_create_table_keeps_its_types_as_text() {
2388        assert_eq!(
2389            round_statement("CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"),
2390            "CREATE TABLE t (a INTEGER, b VARCHAR NOT NULL)"
2391        );
2392        // The type is the text between the identifier and whatever follows it, parentheses and
2393        // all, because resolving `DECIMAL(18, 3)` into a width and a scale is the binder's job and
2394        // doing it here would mean two places that know the type table.
2395        assert_eq!(
2396            round_statement("CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"),
2397            "CREATE TABLE t (a DECIMAL(18, 3), b STRUCT(x INT))"
2398        );
2399    }
2400
2401    #[test]
2402    fn the_modifiers_on_a_create_table_survive() {
2403        assert_eq!(
2404            round_statement("CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"),
2405            "CREATE OR REPLACE TEMPORARY TABLE s.t (a INT)"
2406        );
2407        assert_eq!(
2408            round_statement("CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"),
2409            "CREATE TEMPORARY TABLE IF NOT EXISTS s.t (a INT)"
2410        );
2411    }
2412
2413    #[test]
2414    fn or_replace_and_if_not_exists_in_one_statement_is_refused_here_and_not_later() {
2415        // The grammar has room for both and duckdb's has not, so its refusal is a parser error with
2416        // a caret under the `NOT` and this one is a parser error at the same stage. It is the same
2417        // sentence whatever is being created.
2418        for sql in [
2419            "CREATE OR REPLACE TABLE IF NOT EXISTS t (a INT)",
2420            "CREATE OR REPLACE VIEW IF NOT EXISTS v AS SELECT 1",
2421        ] {
2422            let error = parse_ast(sql).unwrap_err().to_string();
2423            assert_eq!(
2424                error,
2425                "Parser Error: Cannot specify both OR REPLACE and IF NOT EXISTS within single \
2426                 create statement"
2427            );
2428        }
2429    }
2430
2431    #[test]
2432    fn a_create_table_as_carries_the_query_and_not_the_types() {
2433        assert_eq!(
2434            round_statement("CREATE TABLE t AS SELECT a FROM u"),
2435            "CREATE TABLE t AS SELECT a FROM u"
2436        );
2437        // The names are the syntax's to say and the types are the query's, so the column
2438        // definitions here have names and no types.
2439        assert_eq!(
2440            round_statement("CREATE TABLE t (x, y) AS SELECT a, b FROM u"),
2441            "CREATE TABLE t (x, y) AS SELECT a, b FROM u"
2442        );
2443    }
2444
2445    #[test]
2446    fn a_create_view_carries_its_body_twice_over() {
2447        assert_eq!(
2448            round_statement("CREATE VIEW v AS SELECT a FROM u"),
2449            "CREATE VIEW v AS SELECT a FROM u"
2450        );
2451        assert_eq!(
2452            round_statement("CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"),
2453            "CREATE OR REPLACE VIEW main.v (x, y) AS SELECT a, b FROM u"
2454        );
2455        // The text the catalog keeps is the body and only the body, so that binding it again is
2456        // binding a query rather than a `CREATE` statement.
2457        let ast = parse_ast("CREATE VIEW v (x) AS SELECT a FROM u WHERE a > 1").expect("parses");
2458        let Statement::CreateView(index) = ast.statements[0] else {
2459            panic!("not a create view");
2460        };
2461        assert_eq!(ast.string(ast.create_view(index).sql), "SELECT a FROM u WHERE a > 1");
2462    }
2463
2464    #[test]
2465    fn a_drop_view_is_not_a_drop_table() {
2466        assert_eq!(round_statement("DROP VIEW IF EXISTS a, b"), "DROP VIEW IF EXISTS a, b");
2467        assert_eq!(round_statement("DROP TABLE a"), "DROP TABLE a");
2468    }
2469
2470    #[test]
2471    fn a_drop_table_is_a_list_of_qualified_names() {
2472        assert_eq!(round_statement("DROP TABLE t"), "DROP TABLE t");
2473        assert_eq!(round_statement("DROP TABLE IF EXISTS a, b.c"), "DROP TABLE IF EXISTS a, b.c");
2474    }
2475
2476    #[test]
2477    fn dropping_something_that_is_neither_a_table_nor_a_view_is_refused() {
2478        // `TableOrView` covers `MATERIALIZED VIEW` as well, which is not a thing this database has,
2479        // and dropping one as if it were an ordinary view is a wrong answer rather than a missing
2480        // feature.
2481        let error = parse_ast("DROP MATERIALIZED VIEW v").unwrap_err().to_string();
2482        assert!(error.starts_with("Not implemented Error"), "{error}");
2483    }
2484
2485    #[test]
2486    fn both_spellings_of_insert_arrive_at_a_query() {
2487        assert_eq!(
2488            round_statement("INSERT INTO t VALUES (1, 'a'), (2, 'b')"),
2489            "INSERT INTO t VALUES (1, 'a'), (2, 'b')"
2490        );
2491        assert_eq!(
2492            round_statement("INSERT INTO t (a, b) SELECT x, y FROM u"),
2493            "INSERT INTO t (a, b) SELECT x, y FROM u"
2494        );
2495    }
2496
2497    #[test]
2498    fn an_insert_clause_that_changes_the_answer_is_refused() {
2499        for query in [
2500            "INSERT INTO t VALUES (1) RETURNING *",
2501            "INSERT OR REPLACE INTO t VALUES (1)",
2502            "INSERT INTO t BY NAME SELECT 1 AS a",
2503            "INSERT INTO t VALUES (1) ON CONFLICT DO NOTHING",
2504            "INSERT INTO t DEFAULT VALUES",
2505        ] {
2506            let error = parse_ast(query).unwrap_err().to_string();
2507            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
2508        }
2509    }
2510
2511    #[test]
2512    fn a_column_constraint_that_is_not_not_null_is_refused() {
2513        // Nothing enforces a constraint yet. Accepting one and not enforcing it is the wrong
2514        // answer, so `NOT NULL` is kept because the column already has a nullability and the rest
2515        // are refused until there is somewhere to put them.
2516        for query in [
2517            "CREATE TABLE t (a INT PRIMARY KEY)",
2518            "CREATE TABLE t (a INT UNIQUE)",
2519            "CREATE TABLE t (a INT CHECK (a > 0))",
2520            "CREATE TABLE t (a INT DEFAULT 1)",
2521            "CREATE TABLE t (a INT REFERENCES u (b))",
2522            "CREATE TABLE t (a INT, PRIMARY KEY (a))",
2523        ] {
2524            let error = parse_ast(query).unwrap_err().to_string();
2525            assert!(error.starts_with("Not implemented Error"), "{query} gave {error}");
2526        }
2527    }
2528
2529    #[test]
2530    fn values_is_a_query_on_its_own_and_in_a_from() {
2531        assert_eq!(round("VALUES (1), (2)"), "VALUES (1), (2)");
2532        // Parenthesised it is a subquery whose body is the values, and bare it is a `ValuesRef`.
2533        // Two rules and one meaning, which is the grammar's doing and not something to flatten
2534        // here, because the parenthesised form can carry an order by and the bare one cannot.
2535        assert_eq!(
2536            round("SELECT * FROM (VALUES (1, 2), (3, 4)) t(a, b)"),
2537            "SELECT * FROM (VALUES (1, 2), (3, 4)) AS t"
2538        );
2539        assert_eq!(
2540            round("SELECT * FROM VALUES (1, 2), (3, 4) AS t(a, b)"),
2541            "SELECT * FROM VALUES (1, 2), (3, 4) AS t"
2542        );
2543        // Rows of different widths parse. Saying so wants the column count, which for an insert is
2544        // the table's, so the check belongs to the binder and not here.
2545        assert_eq!(round("VALUES (1), (2, 3)"), "VALUES (1), (2, 3)");
2546    }
2547
2548    /// `DESCRIBE` is a query body, and the two spellings that name something become a star over it.
2549    ///
2550    /// Naming a table is not a shortcut for the query. On the reference binary `DESCRIBE t` and
2551    /// `DESCRIBE SELECT * FROM t` print the same six columns and the same rows, down to the `NO` on
2552    /// a column that refuses nulls, so rewriting one into the other costs nothing and leaves the
2553    /// binder with one case instead of three. A file name goes down the same path as a table name
2554    /// because a bare string in a `FROM` clause is already a name the replacement scan picks up.
2555    #[test]
2556    fn describe_rewrites_a_name_into_a_star_over_it() {
2557        assert_eq!(round("DESCRIBE SELECT 1 AS a"), "DESCRIBE SELECT 1 AS a");
2558        assert_eq!(round("DESCRIBE t"), "DESCRIBE SELECT * FROM t");
2559        assert_eq!(round("DESC t"), "DESCRIBE SELECT * FROM t");
2560        assert_eq!(round("DESCRIBE 'x.parquet'"), "DESCRIBE SELECT * FROM x.parquet");
2561        // A body and not a statement kind, so it nests both ways with no rule of its own.
2562        assert_eq!(
2563            round("SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"),
2564            "SELECT column_name FROM (DESCRIBE SELECT 1 AS a)"
2565        );
2566        assert_eq!(round("DESCRIBE DESCRIBE SELECT 1 AS a"), "DESCRIBE DESCRIBE SELECT 1 AS a");
2567    }
2568
2569    /// `SUMMARIZE` shares both of `DESCRIBE`'s grammar rules and is a different statement.
2570    ///
2571    /// It reads every row and returns one row per column carrying the min, the max, the count and
2572    /// the approximate distinct count, so none of it falls out of the `DESCRIBE` path. The word is
2573    /// the only thing in the tree that tells the two apart, which is why the transform looks at it
2574    /// rather than trusting the rule name it arrived under.
2575    #[test]
2576    fn summarize_is_refused_even_though_it_parses_as_a_describe() {
2577        for query in ["SUMMARIZE t", "SUMMARIZE SELECT 1"] {
2578            let error = parse_ast(query).expect_err("summarize is not implemented");
2579            let message = error.to_string();
2580            assert!(message.starts_with("Not implemented Error"), "{query} failed with {message}");
2581        }
2582    }
2583
2584    #[test]
2585    fn every_statement_in_the_corpus_gets_a_defined_answer() {
2586        // The point of the test is the word defined. Forty of these are statement kinds and
2587        // clauses this milestone does not cover, and the requirement is not that they work, it is
2588        // that they fail by saying so. A panic, a silently dropped clause or an internal error
2589        // would each be a different bug and all three would be invisible without this.
2590        let mut done = 0;
2591        for query in CORPUS {
2592            match parse_ast(query) {
2593                Ok(ast) => {
2594                    assert_eq!(ast.statements.len(), 1, "{query}");
2595                    done += 1;
2596                }
2597                Err(error) => {
2598                    let message = error.to_string();
2599                    assert!(
2600                        message.starts_with("Not implemented Error"),
2601                        "{query} failed with {message}, which is not a not-implemented error"
2602                    );
2603                }
2604            }
2605        }
2606        // Not an assertion about the right number. It is a ratchet: this only moves up, and the
2607        // day it moves down somebody has taken a construct out without meaning to.
2608        assert!(done >= 23, "only {done} of the corpus transforms, which is fewer than it was");
2609    }
2610
2611    #[test]
2612    fn the_ast_is_far_smaller_than_the_parse_tree() {
2613        let query = CORPUS[4];
2614        let tree = parse(query).unwrap();
2615        let ast = parse_ast(query).unwrap();
2616        // The twenty precedence levels are the difference. Every one of them is a node in the
2617        // parse tree for every expression at every depth, and none of them survives into the AST.
2618        assert!(
2619            ast.node_count() * 20 < tree.arena_len(),
2620            "{} ast nodes against {} parse nodes",
2621            ast.node_count(),
2622            tree.arena_len()
2623        );
2624    }
2625
2626    #[test]
2627    fn precedence_comes_out_of_the_chain_and_into_the_tree() {
2628        assert_eq!(round("SELECT 1 + 2 * 3"), "SELECT (1 Add (2 Multiply 3))");
2629        assert_eq!(round("SELECT (1 + 2) * 3"), "SELECT ((1 Add 2) Multiply 3)");
2630        assert_eq!(round("SELECT 1 + 2 + 3"), "SELECT ((1 Add 2) Add 3)");
2631        assert_eq!(round("SELECT 1 - 2 - 3"), "SELECT ((1 Subtract 2) Subtract 3)");
2632        assert_eq!(
2633            round("SELECT a OR b AND c"),
2634            "SELECT (a Or (b And c))",
2635            "and binds tighter than or"
2636        );
2637    }
2638
2639    #[test]
2640    fn a_double_negation_is_two_nodes_and_not_none() {
2641        // Folding it would be an optimizer decision and this is not the optimizer. It also would
2642        // not be safe in general: `NOT NOT x` on a null is still null and on a non boolean it is
2643        // still an error, and both of those have to survive to the binder to be reported.
2644        assert_eq!(round("SELECT NOT NOT a"), "SELECT (Not (Not a))");
2645    }
2646
2647    #[test]
2648    fn a_parenthesised_single_expression_is_not_a_row() {
2649        assert_eq!(round("SELECT (a)"), "SELECT a");
2650        assert_eq!(round("SELECT (a, b)"), "SELECT ROW(a, b)");
2651    }
2652
2653    #[test]
2654    fn a_bracketed_list_is_a_list_of_however_many_items_were_written() {
2655        // One item is a list of one, which is where this parts company with the parenthesised form
2656        // above: `(a)` is `a` and `[a]` is a list, because the brackets are what say list.
2657        assert_eq!(round("SELECT [a]"), "SELECT [a]");
2658        assert_eq!(round("SELECT [1, 2, 3]"), "SELECT [1, 2, 3]");
2659        assert_eq!(round("SELECT []"), "SELECT []");
2660        assert_eq!(round("SELECT ['a.parquet', 'b.parquet']"), "SELECT ['a.parquet', 'b.parquet']");
2661    }
2662
2663    #[test]
2664    fn a_parameter_carries_its_identifier_however_it_was_written() {
2665        assert_eq!(round("SELECT $1"), "SELECT $1");
2666        assert_eq!(round("SELECT ?1"), "SELECT $1");
2667        assert_eq!(round("SELECT $name"), "SELECT $name");
2668        // A bare question mark is numbered by where it is, and the counting is its own, so a later
2669        // `$2` does not push the first one along. This is duckdb v1.4.1, which prints `$1 + $2`.
2670        assert_eq!(round("SELECT ? + $2"), "SELECT ($1 Add $2)");
2671        assert_eq!(round("SELECT ?, ?, ?"), "SELECT $1, $2, $3");
2672    }
2673
2674    #[test]
2675    fn the_parameters_of_a_statement_are_listed_once_each_in_written_order() {
2676        let ast = parse_ast("SELECT $b, $a, $b WHERE $a").expect("parses");
2677        assert_eq!(ast.parameters(), vec!["b", "a"]);
2678        assert!(parse_ast("SELECT 1").expect("parses").parameters().is_empty());
2679    }
2680
2681    #[test]
2682    fn the_three_ways_to_write_an_alias_all_arrive() {
2683        assert_eq!(round("SELECT a AS b"), "SELECT a AS b");
2684        assert_eq!(round("SELECT a b"), "SELECT a AS b");
2685        assert_eq!(round("SELECT b: a"), "SELECT a AS b");
2686        assert_eq!(round("SELECT a"), "SELECT a", "and no alias when none was written");
2687    }
2688
2689    #[test]
2690    fn a_from_with_no_select_selects_everything() {
2691        // DuckDB's own shorthand. Inventing the star here rather than in the binder means the
2692        // binder never has to know that the clause it is looking at was the one that was missing.
2693        assert_eq!(round("FROM t"), "SELECT * FROM t");
2694        assert_eq!(round("FROM t SELECT a"), "SELECT a FROM t");
2695    }
2696
2697    #[test]
2698    fn joins_nest_to_the_left() {
2699        assert_eq!(
2700            round("SELECT * FROM a JOIN b ON a.i = b.i LEFT JOIN c USING (k)"),
2701            "SELECT * FROM ((a Inner JOIN b ON (a.i Eq b.i)) Left JOIN c USING (k))"
2702        );
2703        assert_eq!(
2704            round("SELECT * FROM a NATURAL JOIN b"),
2705            "SELECT * FROM (a NATURAL Inner JOIN b)"
2706        );
2707        assert_eq!(round("SELECT * FROM a CROSS JOIN b"), "SELECT * FROM (a Cross JOIN b)");
2708        assert_eq!(
2709            round("SELECT * FROM a POSITIONAL JOIN b"),
2710            "SELECT * FROM (a Positional JOIN b)"
2711        );
2712        assert_eq!(round("SELECT * FROM a, b"), "SELECT * FROM a, b", "a comma is not a join node");
2713    }
2714
2715    #[test]
2716    fn a_qualified_name_keeps_its_parts_however_it_was_spelled() {
2717        // Five grammar rules can produce a column reference and they disagree about which
2718        // component is a schema and which is a table. None of that is decidable without the
2719        // catalog, so the AST holds the parts and the binder decides.
2720        assert_eq!(round("SELECT a"), "SELECT a");
2721        assert_eq!(round("SELECT t.a"), "SELECT t.a");
2722        assert_eq!(round("SELECT s.t.a"), "SELECT s.t.a");
2723        assert_eq!(round("SELECT c.s.t.a"), "SELECT c.s.t.a");
2724        assert_eq!(round("SELECT * FROM s.t"), "SELECT * FROM s.t");
2725    }
2726
2727    #[test]
2728    fn a_star_can_be_qualified() {
2729        assert_eq!(round("SELECT *"), "SELECT *");
2730        assert_eq!(round("SELECT t.*"), "SELECT t.*");
2731        assert_eq!(round("SELECT s.t.*"), "SELECT s.t.*");
2732    }
2733
2734    #[test]
2735    fn a_quoted_identifier_keeps_its_case_and_loses_its_quotes() {
2736        // DuckDB does not fold identifier case at any point, quoted or not, which the tokenizer
2737        // work established by reading the source. So the only thing to do here is take the quotes
2738        // off and resolve the doubled ones.
2739        let ast = parse_ast("SELECT \"Mixed Case\", \"a\"\"b\"").unwrap();
2740        assert_eq!(ast.strings[0], "Mixed Case");
2741        assert_eq!(ast.strings[1], "a\"b");
2742    }
2743
2744    #[test]
2745    fn a_string_literal_is_decoded_and_adjacent_ones_are_joined() {
2746        assert_eq!(round("SELECT 'it''s'"), "SELECT 'it's'");
2747        assert_eq!(round("SELECT 'a'\n'b'"), "SELECT 'ab'", "the standard's adjacency rule");
2748    }
2749
2750    #[test]
2751    fn the_null_and_boolean_tests_are_postfix_unary_operators() {
2752        assert_eq!(round("SELECT x IS NULL"), "SELECT (IsNull x)");
2753        assert_eq!(round("SELECT x IS NOT NULL"), "SELECT (IsNotNull x)");
2754        assert_eq!(round("SELECT x ISNULL"), "SELECT (IsNull x)");
2755        assert_eq!(round("SELECT x NOTNULL"), "SELECT (IsNotNull x)");
2756        assert_eq!(round("SELECT x IS TRUE"), "SELECT (IsTrue x)");
2757        assert_eq!(round("SELECT x IS NOT FALSE"), "SELECT (IsNotFalse x)");
2758        assert_eq!(round("SELECT x IS DISTINCT FROM y"), "SELECT (x IsDistinctFrom y)");
2759        assert_eq!(round("SELECT x IS NOT DISTINCT FROM y"), "SELECT (x IsNotDistinctFrom y)");
2760    }
2761
2762    #[test]
2763    fn the_like_family_folds_its_negation_into_the_operator() {
2764        assert_eq!(round("SELECT x LIKE 'a'"), "SELECT (x Like 'a')");
2765        assert_eq!(round("SELECT x NOT LIKE 'a'"), "SELECT (x NotLike 'a')");
2766        assert_eq!(round("SELECT x ILIKE 'a'"), "SELECT (x ILike 'a')");
2767        assert_eq!(round("SELECT x ~~ 'a'"), "SELECT (x Like 'a')", "the operator spelling");
2768        assert_eq!(round("SELECT x !~~ 'a'"), "SELECT (x NotLike 'a')");
2769        assert_eq!(round("SELECT x SIMILAR TO 'a'"), "SELECT (x SimilarTo 'a')");
2770        // Glob has no negated operator to fold into, so the negation stays where it was written.
2771        assert_eq!(round("SELECT x NOT GLOB 'a'"), "SELECT (Not (x Glob 'a'))");
2772    }
2773
2774    #[test]
2775    fn between_and_in_carry_their_negation_as_a_flag() {
2776        assert_eq!(round("SELECT x BETWEEN 1 AND 2"), "SELECT (x BETWEEN 1 AND 2)");
2777        assert_eq!(round("SELECT x NOT BETWEEN 1 AND 2"), "SELECT (NOT x BETWEEN 1 AND 2)");
2778        assert_eq!(round("SELECT x IN (1, 2)"), "SELECT (x IN [1, 2])");
2779        assert_eq!(round("SELECT x NOT IN (1, 2)"), "SELECT (NOT x IN [1, 2])");
2780    }
2781
2782    #[test]
2783    fn both_spellings_of_a_cast_are_the_same_node() {
2784        assert_eq!(round("SELECT CAST(x AS BIGINT)"), "SELECT CAST(x AS BIGINT)");
2785        assert_eq!(round("SELECT x::BIGINT"), "SELECT CAST(x AS BIGINT)");
2786        assert_eq!(round("SELECT TRY_CAST(x AS BIGINT)"), "SELECT TRY_CAST(x AS BIGINT)");
2787        assert_eq!(
2788            round("SELECT x::DECIMAL(18, 3)"),
2789            "SELECT CAST(x AS DECIMAL(18, 3))",
2790            "the type is kept as text because parsing it is the type system's job"
2791        );
2792    }
2793
2794    #[test]
2795    fn a_case_keeps_its_arms_in_order() {
2796        assert_eq!(
2797            round("SELECT CASE WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"),
2798            "SELECT CASE - WHEN a THEN 1 WHEN b THEN 2 ELSE 3 END"
2799        );
2800        assert_eq!(
2801            round("SELECT CASE x WHEN 1 THEN 'a' END"),
2802            "SELECT CASE x WHEN 1 THEN 'a' ELSE - END",
2803            "a simple case keeps the operand and a missing else is not an implicit null yet"
2804        );
2805    }
2806
2807    #[test]
2808    fn a_field_access_and_a_method_call_are_ordinary_function_calls() {
2809        // Which is what DuckDB makes of them too. Giving each its own AST node would mean the
2810        // binder needs a rule for something the function resolver already handles.
2811        assert_eq!(round("SELECT (f(x)).y"), "SELECT struct_extract(f(x), 'y')");
2812        assert_eq!(round("SELECT a[1]"), "SELECT array_extract(a, 1)");
2813    }
2814
2815    #[test]
2816    fn an_aggregate_keeps_its_distinct() {
2817        assert_eq!(round("SELECT count(*)"), "SELECT count(*)");
2818        assert_eq!(round("SELECT count(DISTINCT x)"), "SELECT count(DISTINCT x)");
2819        assert_eq!(round("SELECT count(ALL x)"), "SELECT count(x)");
2820        assert_eq!(round("SELECT main.count(x)"), "SELECT main.count(x)");
2821    }
2822
2823    #[test]
2824    fn the_modifiers_hang_off_the_query_and_not_off_the_select() {
2825        // `a UNION b ORDER BY x` sorts the union. Putting the order by on the select would have
2826        // made that unrepresentable, which is why the grammar puts it outside the chain and why
2827        // the AST follows.
2828        assert_eq!(
2829            round("SELECT 1 UNION ALL SELECT 2 ORDER BY 1"),
2830            "(SELECT 1 Union All SELECT 2) ORDER BY 1 Unstated Unstated"
2831        );
2832        assert_eq!(
2833            round("SELECT a FROM t UNION SELECT b FROM u EXCEPT SELECT c FROM v"),
2834            "((SELECT a FROM t Union Unstated SELECT b FROM u) Except Unstated SELECT c FROM v)",
2835            "set operators are left associative"
2836        );
2837        assert_eq!(
2838            round("SELECT 1 UNION SELECT 2 INTERSECT SELECT 3"),
2839            "(SELECT 1 Union Unstated (SELECT 2 Intersect Unstated SELECT 3))",
2840            "and intersect binds tighter than the other two"
2841        );
2842    }
2843
2844    #[test]
2845    fn the_sort_and_limit_clauses_keep_what_was_written() {
2846        assert_eq!(
2847            round("SELECT a FROM t ORDER BY a"),
2848            "SELECT a FROM t ORDER BY a Unstated Unstated"
2849        );
2850        assert_eq!(
2851            round("SELECT a FROM t ORDER BY a DESC NULLS LAST"),
2852            "SELECT a FROM t ORDER BY a Descending Last"
2853        );
2854        assert_eq!(round("SELECT a FROM t ORDER BY ALL"), "SELECT a FROM t ORDER BY ALL");
2855        assert_eq!(round("SELECT a FROM t GROUP BY ALL"), "SELECT a FROM t GROUP BY ALL");
2856        assert_eq!(round("SELECT a FROM t LIMIT 10 OFFSET 5"), "SELECT a FROM t LIMIT 10 OFFSET 5");
2857        assert_eq!(round("SELECT a FROM t OFFSET 5 LIMIT 10"), "SELECT a FROM t LIMIT 10 OFFSET 5");
2858        assert_eq!(round("SELECT a FROM t LIMIT 10%"), "SELECT a FROM t LIMIT 10%");
2859        assert_eq!(round("SELECT a FROM t LIMIT ALL"), "SELECT a FROM t", "which is no limit");
2860    }
2861
2862    #[test]
2863    fn a_subquery_appears_in_both_places_it_can() {
2864        assert_eq!(
2865            round("SELECT * FROM (SELECT x FROM t) AS s"),
2866            "SELECT * FROM (SELECT x FROM t) AS s"
2867        );
2868        assert_eq!(round("SELECT (SELECT 1)"), "SELECT (SELECT 1)");
2869    }
2870
2871    #[test]
2872    fn distinct_on_keeps_its_expressions() {
2873        assert_eq!(round("SELECT DISTINCT a"), "SELECT DISTINCT a");
2874        assert_eq!(round("SELECT ALL a"), "SELECT a", "which is the default written out");
2875        assert_eq!(round("SELECT DISTINCT ON (a, b) a"), "SELECT DISTINCT ON (a, b) a");
2876    }
2877
2878    #[test]
2879    fn an_operator_the_dialect_does_not_name_is_kept_by_name() {
2880        // The grammar text says `OperatorLiteral <- Identifier`, which reads as though any bare
2881        // word could be written infix. It cannot. That rule is one of the 24 the matcher overrides
2882        // and it is overridden to the bare operator matcher, so what it takes is a run of operator
2883        // characters. Believing the body here would have produced a transformer that accepted
2884        // `a foo b`, which DuckDB rejects.
2885        assert_eq!(round("SELECT a <=> b"), "SELECT (a <=> b)");
2886        assert!(parse_ast("SELECT a foo b").is_err(), "a bare word is not an operator");
2887    }
2888
2889    #[test]
2890    fn a_script_is_a_list_of_statements() {
2891        let ast = parse_ast("SELECT 1; SELECT 2;").unwrap();
2892        assert_eq!(ast.statements.len(), 2);
2893        // A trailing semicolon makes an empty top level statement in the parse tree, because the
2894        // grammar's `Statement? (';'+ / EndOfInput)` is happy with nothing on both sides. It is
2895        // dropped here rather than pretended away in the matcher.
2896        let Statement::Query(second) = ast.statements[1] else {
2897            panic!("the second statement is a query");
2898        };
2899        assert_eq!(show_query(&ast, second), "SELECT 2");
2900    }
2901
2902    #[test]
2903    fn an_unsupported_construct_names_itself_and_what_was_written() {
2904        let error = parse_ast("ALTER TABLE t ADD COLUMN a INTEGER").unwrap_err().to_string();
2905        assert!(error.starts_with("Not implemented Error"), "{error}");
2906        assert!(error.contains("ALTER TABLE t ADD COLUMN a INTEGER"), "{error}");
2907        assert!(error.contains("AlterStatement"), "{error}");
2908    }
2909
2910    #[test]
2911    fn a_long_construct_is_cut_short_in_the_message() {
2912        let query = format!("ALTER TABLE t ADD COLUMN {} INTEGER", "a".repeat(80));
2913        let error = parse_ast(&query).unwrap_err().to_string();
2914        assert!(error.contains("..."), "{error}");
2915        assert!(error.len() < 200, "{error}");
2916    }
2917
2918    #[test]
2919    fn the_transformer_never_panics_on_anything_the_matcher_accepts() {
2920        // The matcher accepts a good deal that means nothing, because the grammar does. Every one
2921        // of these parses and none of them is a statement this milestone covers, and the contract
2922        // is that the answer is an error either way.
2923        for query in [
2924            "SELECT",
2925            "FROM t SELECT",
2926            "SELECT * FROM t WHERE",
2927            "SELECT ()",
2928            "SELECT a FROM t GROUP BY ()",
2929        ] {
2930            let answer = parse_ast(query);
2931            if let Err(error) = answer {
2932                let message = error.to_string();
2933                assert!(
2934                    message.starts_with("Not implemented Error")
2935                        || message.starts_with("Parser Error"),
2936                    "{query} failed with {message}"
2937                );
2938            }
2939        }
2940    }
2941
2942    #[test]
2943    fn a_file_name_in_a_from_clause_is_a_table_name_with_the_quotes_off() {
2944        // Both spellings have to arrive as the same name, because the binder decides whether it is
2945        // a file by looking at the name, and `'hits.parquet'` with the quotes still on it is not
2946        // a path that anything can open.
2947        assert_eq!(round("SELECT * FROM 'hits.parquet'"), "SELECT * FROM hits.parquet");
2948        assert_eq!(round("SELECT * FROM \"hits.parquet\""), "SELECT * FROM hits.parquet");
2949        assert_eq!(round("SELECT * FROM 'hits.parquet' AS h"), "SELECT * FROM hits.parquet AS h");
2950    }
2951
2952    #[test]
2953    fn a_function_call_in_a_from_clause_is_a_source_and_not_an_expression() {
2954        assert_eq!(round("SELECT * FROM range(3)"), "SELECT * FROM range(3)");
2955        assert_eq!(round("SELECT * FROM range(1, 10, 2)"), "SELECT * FROM range(1, 10, 2)");
2956        assert_eq!(round("SELECT * FROM main.range(3)"), "SELECT * FROM main.range(3)");
2957        assert_eq!(round("SELECT * FROM range(3) AS t"), "SELECT * FROM range(3) AS t");
2958        // The grammar allows a call with no arguments here and the transformer keeps it, because
2959        // whether a particular function takes none is the binder's question and not this one's.
2960        assert_eq!(round("SELECT * FROM some_function()"), "SELECT * FROM some_function()");
2961    }
2962
2963    #[test]
2964    fn the_forms_of_a_table_function_this_does_not_cover_are_turned_away_by_name() {
2965        for query in [
2966            "SELECT * FROM range(3) WITH ORDINALITY",
2967            "SELECT * FROM LATERAL range(3)",
2968            "SELECT * FROM t: range(3)",
2969        ] {
2970            let error = parse_ast(query).unwrap_err().to_string();
2971            assert!(error.contains("grammar rule"), "{query} failed with {error}");
2972        }
2973    }
2974
2975    #[test]
2976    fn interning_means_a_name_written_twice_is_stored_once() {
2977        let ast = parse_ast("SELECT a, a, a FROM t WHERE a = a").unwrap();
2978        assert_eq!(ast.strings.iter().filter(|text| *text == "a").count(), 1);
2979    }
2980}