Skip to main content

sqlglot_rust/generator/
sql_generator.rs

1use crate::ast::*;
2use crate::dialects::Dialect;
3
4/// SQL code generator that converts an AST into a SQL string.
5///
6/// Supports all statement and expression types defined in the AST,
7/// including CTEs, subqueries, UNION/INTERSECT/EXCEPT, CAST, window
8/// functions, EXISTS, EXTRACT, INTERVAL, and more.
9pub struct Generator {
10    output: String,
11    /// When true, emit formatted SQL with indentation and newlines.
12    pretty: bool,
13    indent: usize,
14    /// Target dialect for dialect-specific generation (typed functions).
15    dialect: Option<Dialect>,
16}
17
18impl Generator {
19    #[must_use]
20    pub fn new() -> Self {
21        Self {
22            output: String::new(),
23            pretty: false,
24            indent: 0,
25            dialect: None,
26        }
27    }
28
29    /// Create a generator that produces formatted SQL.
30    #[must_use]
31    pub fn pretty() -> Self {
32        Self {
33            output: String::new(),
34            pretty: true,
35            indent: 0,
36            dialect: None,
37        }
38    }
39
40    /// Create a generator targeting a specific dialect.
41    #[must_use]
42    pub fn with_dialect(dialect: Dialect) -> Self {
43        Self {
44            output: String::new(),
45            pretty: false,
46            indent: 0,
47            dialect: Some(dialect),
48        }
49    }
50
51    /// Generate SQL from a statement.
52    #[must_use]
53    pub fn generate(&mut self, statement: &Statement) -> String {
54        self.output.clear();
55        self.gen_statement(statement);
56        self.output.clone()
57    }
58
59    /// Generate SQL for an expression (static helper for `Expr::sql()`).
60    #[must_use]
61    pub fn expr_to_sql(expr: &Expr) -> String {
62        let mut g = Self::new();
63        g.gen_expr(expr);
64        g.output
65    }
66
67    fn write(&mut self, s: &str) {
68        self.output.push_str(s);
69    }
70
71    /// Emit a newline followed by current indentation (pretty mode only).
72    fn newline(&mut self) {
73        if self.pretty {
74            self.output.push('\n');
75            for _ in 0..self.indent {
76                self.output.push_str("  ");
77            }
78        }
79    }
80
81    /// In pretty mode: newline + indent. In compact mode: a single space.
82    fn sep(&mut self) {
83        if self.pretty {
84            self.newline();
85        } else {
86            self.output.push(' ');
87        }
88    }
89
90    fn indent_up(&mut self) {
91        self.indent += 1;
92    }
93
94    fn indent_down(&mut self) {
95        self.indent = self.indent.saturating_sub(1);
96    }
97
98    fn write_keyword(&mut self, s: &str) {
99        self.write(s);
100    }
101
102    /// Write an identifier with the given quoting style.
103    /// If a target dialect is set and the identifier is quoted, the quoting
104    /// is transformed to the target dialect's canonical style.
105    fn write_quoted(&mut self, name: &str, style: QuoteStyle) {
106        let effective_style = if style.is_quoted() {
107            if let Some(dialect) = self.dialect {
108                QuoteStyle::for_dialect(dialect)
109            } else {
110                style
111            }
112        } else {
113            style
114        };
115        match effective_style {
116            QuoteStyle::None => self.write(name),
117            QuoteStyle::DoubleQuote => {
118                self.write("\"");
119                self.write(&name.replace('"', "\"\""));
120                self.write("\"");
121            }
122            QuoteStyle::Backtick => {
123                self.write("`");
124                self.write(&name.replace('`', "``"));
125                self.write("`");
126            }
127            QuoteStyle::Bracket => {
128                self.write("[");
129                self.write(&name.replace(']', "]]"));
130                self.write("]");
131            }
132        }
133    }
134
135    // ══════════════════════════════════════════════════════════════
136    // Statements
137    // ══════════════════════════════════════════════════════════════
138
139    /// Emit comment strings, each on its own line.
140    /// MySQL hash comments (`#`) are converted to standard `--` comments
141    /// when the target dialect is not MySQL.
142    fn gen_comments(&mut self, comments: &[String]) {
143        for comment in comments {
144            let normalized = self.normalize_comment(comment);
145            self.write(&normalized);
146            self.newline_or_space();
147        }
148    }
149
150    /// Normalize a comment for the target dialect.
151    /// Converts MySQL `#` comments to `--` when targeting non-MySQL dialects.
152    fn normalize_comment(&self, comment: &str) -> String {
153        if comment.starts_with('#') {
154            let is_mysql_target = matches!(
155                self.dialect,
156                Some(Dialect::Mysql | Dialect::Doris | Dialect::SingleStore | Dialect::StarRocks)
157            );
158            if !is_mysql_target {
159                return format!("--{}", &comment[1..]);
160            }
161        }
162        comment.to_string()
163    }
164
165    /// Emit a newline in pretty mode or a space in compact mode.
166    fn newline_or_space(&mut self) {
167        if self.pretty {
168            self.output.push('\n');
169            for _ in 0..self.indent {
170                self.output.push_str("  ");
171            }
172        } else {
173            self.output.push('\n');
174        }
175    }
176
177    fn gen_statement(&mut self, statement: &Statement) {
178        // Emit leading comments for statements that carry them.
179        match statement {
180            Statement::Select(s) => {
181                self.gen_comments(&s.comments);
182                self.gen_select(s);
183            }
184            Statement::Insert(s) => {
185                self.gen_comments(&s.comments);
186                self.gen_insert(s);
187            }
188            Statement::Update(s) => {
189                self.gen_comments(&s.comments);
190                self.gen_update(s);
191            }
192            Statement::Delete(s) => {
193                self.gen_comments(&s.comments);
194                self.gen_delete(s);
195            }
196            Statement::CreateTable(s) => {
197                self.gen_comments(&s.comments);
198                self.gen_create_table(s);
199            }
200            Statement::DropTable(s) => {
201                self.gen_comments(&s.comments);
202                self.gen_drop_table(s);
203            }
204            Statement::SetOperation(s) => {
205                self.gen_comments(&s.comments);
206                self.gen_set_operation(s);
207            }
208            Statement::AlterTable(s) => {
209                self.gen_comments(&s.comments);
210                self.gen_alter_table(s);
211            }
212            Statement::CreateView(s) => {
213                self.gen_comments(&s.comments);
214                self.gen_create_view(s);
215            }
216            Statement::DropView(s) => {
217                self.gen_comments(&s.comments);
218                self.gen_drop_view(s);
219            }
220            Statement::Truncate(s) => {
221                self.gen_comments(&s.comments);
222                self.gen_truncate(s);
223            }
224            Statement::Transaction(s) => self.gen_transaction(s),
225            Statement::Explain(s) => {
226                self.gen_comments(&s.comments);
227                self.gen_explain(s);
228            }
229            Statement::Use(s) => {
230                self.gen_comments(&s.comments);
231                self.gen_use(s);
232            }
233            Statement::Merge(s) => {
234                self.gen_comments(&s.comments);
235                self.gen_merge(s);
236            }
237            Statement::Expression(e) => self.gen_expr(e),
238        }
239    }
240
241    // ── SELECT ──────────────────────────────────────────────────
242
243    fn gen_select(&mut self, sel: &SelectStatement) {
244        // CTEs
245        if !sel.ctes.is_empty() {
246            self.gen_ctes(&sel.ctes);
247            self.sep();
248        }
249
250        self.write_keyword("SELECT");
251        if sel.distinct {
252            self.write(" ");
253            self.write_keyword("DISTINCT");
254        }
255        if let Some(top) = &sel.top {
256            self.write(" ");
257            self.write_keyword("TOP ");
258            self.gen_expr(top);
259        }
260
261        // columns
262        if self.pretty {
263            self.indent_up();
264            for (i, item) in sel.columns.iter().enumerate() {
265                self.newline();
266                self.gen_select_item(item);
267                if i < sel.columns.len() - 1 {
268                    self.write(",");
269                }
270            }
271            self.indent_down();
272        } else {
273            self.write(" ");
274            for (i, item) in sel.columns.iter().enumerate() {
275                if i > 0 {
276                    self.write(", ");
277                }
278                self.gen_select_item(item);
279            }
280        }
281
282        if let Some(from) = &sel.from {
283            self.sep();
284            self.write_keyword("FROM");
285            if self.pretty {
286                self.indent_up();
287                self.newline();
288                self.gen_table_source(&from.source);
289                self.indent_down();
290            } else {
291                self.write(" ");
292                self.gen_table_source(&from.source);
293            }
294        }
295
296        for join in &sel.joins {
297            self.gen_join(join);
298        }
299
300        if let Some(wh) = &sel.where_clause {
301            self.sep();
302            self.write_keyword("WHERE");
303            if self.pretty {
304                self.indent_up();
305                self.newline();
306                self.gen_expr(wh);
307                self.indent_down();
308            } else {
309                self.write(" ");
310                self.gen_expr(wh);
311            }
312        }
313
314        if !sel.group_by.is_empty() {
315            self.sep();
316            self.write_keyword("GROUP BY");
317            if self.pretty {
318                self.indent_up();
319                self.newline();
320                self.gen_expr_list(&sel.group_by);
321                self.indent_down();
322            } else {
323                self.write(" ");
324                self.gen_expr_list(&sel.group_by);
325            }
326        }
327
328        if let Some(having) = &sel.having {
329            self.sep();
330            self.write_keyword("HAVING");
331            if self.pretty {
332                self.indent_up();
333                self.newline();
334                self.gen_expr(having);
335                self.indent_down();
336            } else {
337                self.write(" ");
338                self.gen_expr(having);
339            }
340        }
341
342        if let Some(qualify) = &sel.qualify {
343            self.sep();
344            self.write_keyword("QUALIFY");
345            if self.pretty {
346                self.indent_up();
347                self.newline();
348                self.gen_expr(qualify);
349                self.indent_down();
350            } else {
351                self.write(" ");
352                self.gen_expr(qualify);
353            }
354        }
355
356        if !sel.window_definitions.is_empty() {
357            self.sep();
358            self.write_keyword("WINDOW ");
359            for (i, wd) in sel.window_definitions.iter().enumerate() {
360                if i > 0 {
361                    self.write(", ");
362                }
363                self.write(&wd.name);
364                self.write(" AS (");
365                self.gen_window_spec(&wd.spec);
366                self.write(")");
367            }
368        }
369
370        self.gen_order_by(&sel.order_by);
371
372        if let Some(limit) = &sel.limit {
373            self.sep();
374            self.write_keyword("LIMIT ");
375            self.gen_expr(limit);
376        }
377
378        if let Some(offset) = &sel.offset {
379            self.sep();
380            self.write_keyword("OFFSET ");
381            self.gen_expr(offset);
382        }
383
384        if let Some(fetch) = &sel.fetch_first {
385            self.sep();
386            self.write_keyword("FETCH FIRST ");
387            self.gen_expr(fetch);
388            self.write(" ");
389            self.write_keyword("ROWS ONLY");
390        }
391    }
392
393    fn gen_ctes(&mut self, ctes: &[Cte]) {
394        self.write_keyword("WITH ");
395        if ctes.iter().any(|c| c.recursive) {
396            self.write_keyword("RECURSIVE ");
397        }
398        for (i, cte) in ctes.iter().enumerate() {
399            if i > 0 {
400                self.write(",");
401                self.sep();
402            }
403            self.write_quoted(&cte.name, cte.name_quote_style);
404            if !cte.columns.is_empty() {
405                self.write("(");
406                self.write(&cte.columns.join(", "));
407                self.write(")");
408            }
409            self.write(" ");
410            self.write_keyword("AS ");
411            if let Some(true) = cte.materialized {
412                self.write_keyword("MATERIALIZED ");
413            } else if let Some(false) = cte.materialized {
414                self.write_keyword("NOT MATERIALIZED ");
415            }
416            self.write("(");
417            if self.pretty {
418                self.indent_up();
419                self.newline();
420                self.gen_statement(&cte.query);
421                self.indent_down();
422                self.newline();
423            } else {
424                self.gen_statement(&cte.query);
425            }
426            self.write(")");
427        }
428    }
429
430    fn gen_select_item(&mut self, item: &SelectItem) {
431        match item {
432            SelectItem::Wildcard => self.write("*"),
433            SelectItem::QualifiedWildcard { table } => {
434                self.write(table);
435                self.write(".*");
436            }
437            SelectItem::Expr {
438                expr,
439                alias,
440                alias_quote_style,
441            } => {
442                self.gen_expr(expr);
443                if let Some(alias) = alias {
444                    self.write(" ");
445                    self.write_keyword("AS ");
446                    self.write_quoted(alias, *alias_quote_style);
447                }
448            }
449        }
450    }
451
452    fn gen_table_source(&mut self, source: &TableSource) {
453        match source {
454            TableSource::Table(table_ref) => self.gen_table_ref(table_ref),
455            TableSource::Subquery {
456                query,
457                alias,
458                alias_quote_style,
459            } => {
460                self.write("(");
461                self.gen_statement(query);
462                self.write(")");
463                if let Some(alias) = alias {
464                    self.write(" ");
465                    if !self.omit_table_alias_as() {
466                        self.write_keyword("AS ");
467                    }
468                    self.write_quoted(alias, *alias_quote_style);
469                }
470            }
471            TableSource::TableFunction {
472                name,
473                args,
474                alias,
475                alias_quote_style,
476            } => {
477                self.write(name);
478                self.write("(");
479                self.gen_expr_list(args);
480                self.write(")");
481                if let Some(alias) = alias {
482                    self.write(" ");
483                    if !self.omit_table_alias_as() {
484                        self.write_keyword("AS ");
485                    }
486                    self.write_quoted(alias, *alias_quote_style);
487                }
488            }
489            TableSource::Lateral { source } => {
490                self.write_keyword("LATERAL ");
491                self.gen_table_source(source);
492            }
493            TableSource::Unnest {
494                expr,
495                alias,
496                alias_quote_style,
497                with_offset,
498            } => {
499                self.write_keyword("UNNEST(");
500                self.gen_expr(expr);
501                self.write(")");
502                if let Some(alias) = alias {
503                    self.write(" ");
504                    if !self.omit_table_alias_as() {
505                        self.write_keyword("AS ");
506                    }
507                    self.write_quoted(alias, *alias_quote_style);
508                }
509                if *with_offset {
510                    self.write(" ");
511                    self.write_keyword("WITH OFFSET");
512                }
513            }
514            TableSource::Pivot {
515                source,
516                aggregate,
517                for_column,
518                in_values,
519                alias,
520                alias_quote_style,
521            } => {
522                self.gen_table_source(source);
523                self.write(" ");
524                self.write_keyword("PIVOT");
525                self.write(" (");
526                self.gen_expr(aggregate);
527                self.write(" ");
528                self.write_keyword("FOR ");
529                self.write(for_column);
530                self.write(" ");
531                self.write_keyword("IN ");
532                self.write("(");
533                self.gen_pivot_values(in_values);
534                self.write("))");
535                if let Some(alias) = alias {
536                    self.write(" ");
537                    if !self.omit_table_alias_as() {
538                        self.write_keyword("AS ");
539                    }
540                    self.write_quoted(alias, *alias_quote_style);
541                }
542            }
543            TableSource::Unpivot {
544                source,
545                value_column,
546                for_column,
547                in_columns,
548                alias,
549                alias_quote_style,
550            } => {
551                self.gen_table_source(source);
552                self.write(" ");
553                self.write_keyword("UNPIVOT");
554                self.write(" (");
555                self.write(value_column);
556                self.write(" ");
557                self.write_keyword("FOR ");
558                self.write(for_column);
559                self.write(" ");
560                self.write_keyword("IN ");
561                self.write("(");
562                self.gen_pivot_values(in_columns);
563                self.write("))");
564                if let Some(alias) = alias {
565                    self.write(" ");
566                    if !self.omit_table_alias_as() {
567                        self.write_keyword("AS ");
568                    }
569                    self.write_quoted(alias, *alias_quote_style);
570                }
571            }
572        }
573    }
574
575    fn gen_pivot_values(&mut self, values: &[PivotValue]) {
576        for (i, pv) in values.iter().enumerate() {
577            if i > 0 {
578                self.write(", ");
579            }
580            self.gen_expr(&pv.value);
581            if let Some(alias) = &pv.alias {
582                self.write(" ");
583                self.write_keyword("AS ");
584                self.write_quoted(alias, pv.alias_quote_style);
585            }
586        }
587    }
588
589    /// Returns true if the target dialect forbids `AS` in table aliases.
590    fn omit_table_alias_as(&self) -> bool {
591        matches!(self.dialect, Some(Dialect::Oracle))
592    }
593
594    fn gen_table_ref(&mut self, table: &TableRef) {
595        if let Some(catalog) = &table.catalog {
596            self.write(catalog);
597            self.write(".");
598        }
599        if let Some(schema) = &table.schema {
600            self.write(schema);
601            self.write(".");
602        }
603        self.write_quoted(&table.name, table.name_quote_style);
604        if let Some(alias) = &table.alias {
605            self.write(" ");
606            if !self.omit_table_alias_as() {
607                self.write_keyword("AS ");
608            }
609            self.write_quoted(alias, table.alias_quote_style);
610        }
611    }
612
613    fn gen_join(&mut self, join: &JoinClause) {
614        let join_kw = match join.join_type {
615            JoinType::Inner => "INNER JOIN",
616            JoinType::Left => "LEFT JOIN",
617            JoinType::Right => "RIGHT JOIN",
618            JoinType::Full => "FULL JOIN",
619            JoinType::Cross => "CROSS JOIN",
620            JoinType::Natural => "NATURAL JOIN",
621            JoinType::Lateral => "LATERAL JOIN",
622        };
623        self.sep();
624        self.write_keyword(join_kw);
625        if self.pretty {
626            self.indent_up();
627            self.newline();
628            self.gen_table_source(&join.table);
629        } else {
630            self.write(" ");
631            self.gen_table_source(&join.table);
632        }
633        if let Some(on) = &join.on {
634            if self.pretty {
635                self.newline();
636            } else {
637                self.write(" ");
638            }
639            self.write_keyword("ON ");
640            self.gen_expr(on);
641        }
642        if !join.using.is_empty() {
643            if self.pretty {
644                self.newline();
645            } else {
646                self.write(" ");
647            }
648            self.write_keyword("USING (");
649            self.write(&join.using.join(", "));
650            self.write(")");
651        }
652        if self.pretty {
653            self.indent_down();
654        }
655    }
656
657    fn gen_order_by(&mut self, items: &[OrderByItem]) {
658        if items.is_empty() {
659            return;
660        }
661        self.sep();
662        self.write_keyword("ORDER BY");
663        if self.pretty {
664            self.indent_up();
665            self.newline();
666        } else {
667            self.write(" ");
668        }
669        for (i, item) in items.iter().enumerate() {
670            if i > 0 {
671                self.write(", ");
672            }
673            self.gen_expr(&item.expr);
674            if !item.ascending {
675                self.write(" ");
676                self.write_keyword("DESC");
677            }
678            if let Some(nulls_first) = item.nulls_first {
679                if nulls_first {
680                    self.write(" ");
681                    self.write_keyword("NULLS FIRST");
682                } else {
683                    self.write(" ");
684                    self.write_keyword("NULLS LAST");
685                }
686            }
687        }
688        if self.pretty {
689            self.indent_down();
690        }
691    }
692
693    // ── Set operations ──────────────────────────────────────────
694
695    fn gen_set_operation(&mut self, sop: &SetOperationStatement) {
696        self.gen_statement(&sop.left);
697        let op_kw = match sop.op {
698            SetOperationType::Union => "UNION",
699            SetOperationType::Intersect => "INTERSECT",
700            SetOperationType::Except => "EXCEPT",
701        };
702        self.sep();
703        self.write_keyword(op_kw);
704        if sop.all {
705            self.write(" ");
706            self.write_keyword("ALL");
707        }
708        self.sep();
709        self.gen_statement(&sop.right);
710
711        self.gen_order_by(&sop.order_by);
712
713        if let Some(limit) = &sop.limit {
714            self.sep();
715            self.write_keyword("LIMIT ");
716            self.gen_expr(limit);
717        }
718        if let Some(offset) = &sop.offset {
719            self.sep();
720            self.write_keyword("OFFSET ");
721            self.gen_expr(offset);
722        }
723    }
724
725    // ── INSERT ──────────────────────────────────────────────────
726
727    fn gen_insert(&mut self, ins: &InsertStatement) {
728        self.write_keyword("INSERT INTO ");
729        self.gen_table_ref(&ins.table);
730
731        if !ins.columns.is_empty() {
732            self.write(" (");
733            self.write(&ins.columns.join(", "));
734            self.write(")");
735        }
736
737        match &ins.source {
738            InsertSource::Values(rows) => {
739                self.sep();
740                self.write_keyword("VALUES");
741                if self.pretty {
742                    self.indent_up();
743                    for (i, row) in rows.iter().enumerate() {
744                        self.newline();
745                        self.write("(");
746                        self.gen_expr_list(row);
747                        self.write(")");
748                        if i < rows.len() - 1 {
749                            self.write(",");
750                        }
751                    }
752                    self.indent_down();
753                } else {
754                    self.write(" ");
755                    for (i, row) in rows.iter().enumerate() {
756                        if i > 0 {
757                            self.write(", ");
758                        }
759                        self.write("(");
760                        self.gen_expr_list(row);
761                        self.write(")");
762                    }
763                }
764            }
765            InsertSource::Query(query) => {
766                self.sep();
767                self.gen_statement(query);
768            }
769            InsertSource::Default => {
770                self.sep();
771                self.write_keyword("DEFAULT VALUES");
772            }
773        }
774
775        if let Some(on_conflict) = &ins.on_conflict {
776            self.sep();
777            self.write_keyword("ON CONFLICT");
778            if !on_conflict.columns.is_empty() {
779                self.write(" (");
780                self.write(&on_conflict.columns.join(", "));
781                self.write(")");
782            }
783            match &on_conflict.action {
784                ConflictAction::DoNothing => {
785                    self.write(" ");
786                    self.write_keyword("DO NOTHING");
787                }
788                ConflictAction::DoUpdate(assignments) => {
789                    self.write(" ");
790                    self.write_keyword("DO UPDATE SET ");
791                    for (i, (col, val)) in assignments.iter().enumerate() {
792                        if i > 0 {
793                            self.write(", ");
794                        }
795                        self.write(col);
796                        self.write(" = ");
797                        self.gen_expr(val);
798                    }
799                }
800            }
801        }
802
803        if !ins.returning.is_empty() {
804            self.sep();
805            self.write_keyword("RETURNING ");
806            for (i, item) in ins.returning.iter().enumerate() {
807                if i > 0 {
808                    self.write(", ");
809                }
810                self.gen_select_item(item);
811            }
812        }
813    }
814
815    // ── UPDATE ──────────────────────────────────────────────────
816
817    fn gen_update(&mut self, upd: &UpdateStatement) {
818        self.write_keyword("UPDATE ");
819        self.gen_table_ref(&upd.table);
820        self.sep();
821        self.write_keyword("SET");
822
823        if self.pretty {
824            self.indent_up();
825            for (i, (col, val)) in upd.assignments.iter().enumerate() {
826                self.newline();
827                self.write(col);
828                self.write(" = ");
829                self.gen_expr(val);
830                if i < upd.assignments.len() - 1 {
831                    self.write(",");
832                }
833            }
834            self.indent_down();
835        } else {
836            self.write(" ");
837            for (i, (col, val)) in upd.assignments.iter().enumerate() {
838                if i > 0 {
839                    self.write(", ");
840                }
841                self.write(col);
842                self.write(" = ");
843                self.gen_expr(val);
844            }
845        }
846
847        if let Some(from) = &upd.from {
848            self.sep();
849            self.write_keyword("FROM ");
850            self.gen_table_source(&from.source);
851        }
852
853        if let Some(wh) = &upd.where_clause {
854            self.sep();
855            self.write_keyword("WHERE");
856            if self.pretty {
857                self.indent_up();
858                self.newline();
859                self.gen_expr(wh);
860                self.indent_down();
861            } else {
862                self.write(" ");
863                self.gen_expr(wh);
864            }
865        }
866
867        if !upd.returning.is_empty() {
868            self.sep();
869            self.write_keyword("RETURNING ");
870            for (i, item) in upd.returning.iter().enumerate() {
871                if i > 0 {
872                    self.write(", ");
873                }
874                self.gen_select_item(item);
875            }
876        }
877    }
878
879    // ── DELETE ──────────────────────────────────────────────────
880
881    fn gen_delete(&mut self, del: &DeleteStatement) {
882        self.write_keyword("DELETE FROM ");
883        self.gen_table_ref(&del.table);
884
885        if let Some(using) = &del.using {
886            self.sep();
887            self.write_keyword("USING ");
888            self.gen_table_source(&using.source);
889        }
890
891        if let Some(wh) = &del.where_clause {
892            self.sep();
893            self.write_keyword("WHERE");
894            if self.pretty {
895                self.indent_up();
896                self.newline();
897                self.gen_expr(wh);
898                self.indent_down();
899            } else {
900                self.write(" ");
901                self.gen_expr(wh);
902            }
903        }
904
905        if !del.returning.is_empty() {
906            self.sep();
907            self.write_keyword("RETURNING ");
908            for (i, item) in del.returning.iter().enumerate() {
909                if i > 0 {
910                    self.write(", ");
911                }
912                self.gen_select_item(item);
913            }
914        }
915    }
916
917    // ── CREATE TABLE ────────────────────────────────────────────
918
919    // ── MERGE ───────────────────────────────────────────────────
920
921    fn gen_merge(&mut self, merge: &MergeStatement) {
922        self.write_keyword("MERGE INTO ");
923        self.gen_table_ref(&merge.target);
924
925        self.sep();
926        self.write_keyword("USING ");
927        self.gen_table_source(&merge.source);
928
929        self.sep();
930        self.write_keyword("ON");
931        if self.pretty {
932            self.indent_up();
933            self.newline();
934            self.gen_expr(&merge.on);
935            self.indent_down();
936        } else {
937            self.write(" ");
938            self.gen_expr(&merge.on);
939        }
940
941        for clause in &merge.clauses {
942            self.sep();
943            self.gen_merge_clause(clause);
944        }
945
946        if !merge.output.is_empty() {
947            self.sep();
948            self.write_keyword("OUTPUT ");
949            for (i, item) in merge.output.iter().enumerate() {
950                if i > 0 {
951                    self.write(", ");
952                }
953                self.gen_select_item(item);
954            }
955        }
956    }
957
958    fn gen_merge_clause(&mut self, clause: &MergeClause) {
959        self.write_keyword("WHEN ");
960        match &clause.kind {
961            MergeClauseKind::Matched => self.write_keyword("MATCHED"),
962            MergeClauseKind::NotMatched => self.write_keyword("NOT MATCHED"),
963            MergeClauseKind::NotMatchedBySource => self.write_keyword("NOT MATCHED BY SOURCE"),
964        }
965
966        if let Some(cond) = &clause.condition {
967            self.write_keyword(" AND ");
968            self.gen_expr(cond);
969        }
970
971        self.write_keyword(" THEN");
972
973        match &clause.action {
974            MergeAction::Update(assignments) => {
975                self.sep();
976                self.write_keyword("UPDATE SET");
977                if self.pretty {
978                    self.indent_up();
979                    for (i, (col, val)) in assignments.iter().enumerate() {
980                        self.newline();
981                        self.write(col);
982                        self.write(" = ");
983                        self.gen_expr(val);
984                        if i < assignments.len() - 1 {
985                            self.write(",");
986                        }
987                    }
988                    self.indent_down();
989                } else {
990                    self.write(" ");
991                    for (i, (col, val)) in assignments.iter().enumerate() {
992                        if i > 0 {
993                            self.write(", ");
994                        }
995                        self.write(col);
996                        self.write(" = ");
997                        self.gen_expr(val);
998                    }
999                }
1000            }
1001            MergeAction::Insert { columns, values } => {
1002                self.sep();
1003                self.write_keyword("INSERT");
1004                if !columns.is_empty() {
1005                    self.write(" (");
1006                    self.write(&columns.join(", "));
1007                    self.write(")");
1008                }
1009                self.write_keyword(" VALUES");
1010                self.write(" (");
1011                for (i, val) in values.iter().enumerate() {
1012                    if i > 0 {
1013                        self.write(", ");
1014                    }
1015                    self.gen_expr(val);
1016                }
1017                self.write(")");
1018            }
1019            MergeAction::InsertRow => {
1020                self.sep();
1021                self.write_keyword("INSERT ROW");
1022            }
1023            MergeAction::Delete => {
1024                self.sep();
1025                self.write_keyword("DELETE");
1026            }
1027        }
1028    }
1029
1030    // ── CREATE TABLE ────────────────────────────────────────────
1031
1032    fn gen_create_table(&mut self, ct: &CreateTableStatement) {
1033        self.write_keyword("CREATE ");
1034        if ct.temporary {
1035            self.write_keyword("TEMPORARY ");
1036        }
1037        self.write_keyword("TABLE ");
1038        if ct.if_not_exists {
1039            self.write_keyword("IF NOT EXISTS ");
1040        }
1041        self.gen_table_ref(&ct.table);
1042
1043        if let Some(as_select) = &ct.as_select {
1044            self.write(" ");
1045            self.write_keyword("AS ");
1046            self.gen_statement(as_select);
1047            return;
1048        }
1049
1050        self.write(" (");
1051
1052        if self.pretty {
1053            self.indent_up();
1054            for (i, col) in ct.columns.iter().enumerate() {
1055                self.newline();
1056                self.gen_column_def(col);
1057                if i < ct.columns.len() - 1 || !ct.constraints.is_empty() {
1058                    self.write(",");
1059                }
1060            }
1061            for (i, constraint) in ct.constraints.iter().enumerate() {
1062                self.newline();
1063                self.gen_table_constraint(constraint);
1064                if i < ct.constraints.len() - 1 {
1065                    self.write(",");
1066                }
1067            }
1068            self.indent_down();
1069            self.newline();
1070        } else {
1071            for (i, col) in ct.columns.iter().enumerate() {
1072                if i > 0 {
1073                    self.write(", ");
1074                }
1075                self.gen_column_def(col);
1076            }
1077            for (i, constraint) in ct.constraints.iter().enumerate() {
1078                if i + ct.columns.len() > 0 {
1079                    self.write(", ");
1080                }
1081                self.gen_table_constraint(constraint);
1082            }
1083        }
1084
1085        self.write(")");
1086    }
1087
1088    fn gen_column_def(&mut self, col: &ColumnDef) {
1089        self.write(&col.name);
1090        self.write(" ");
1091        self.gen_data_type(&col.data_type);
1092
1093        if col.primary_key {
1094            self.write(" ");
1095            self.write_keyword("PRIMARY KEY");
1096        }
1097        if col.unique {
1098            self.write(" ");
1099            self.write_keyword("UNIQUE");
1100        }
1101        if col.auto_increment {
1102            self.write(" ");
1103            self.write_keyword("AUTOINCREMENT");
1104        }
1105
1106        match col.nullable {
1107            Some(false) => {
1108                self.write(" ");
1109                self.write_keyword("NOT NULL");
1110            }
1111            Some(true) => {
1112                self.write(" ");
1113                self.write_keyword("NULL");
1114            }
1115            None => {}
1116        }
1117
1118        if let Some(default) = &col.default {
1119            self.write(" ");
1120            self.write_keyword("DEFAULT ");
1121            self.gen_expr(default);
1122        }
1123
1124        if let Some(collation) = &col.collation {
1125            self.write(" ");
1126            self.write_keyword("COLLATE ");
1127            self.write(collation);
1128        }
1129
1130        if let Some(comment) = &col.comment {
1131            self.write(" ");
1132            self.write_keyword("COMMENT '");
1133            self.write(&comment.replace('\'', "''"));
1134            self.write("'");
1135        }
1136    }
1137
1138    fn gen_table_constraint(&mut self, constraint: &TableConstraint) {
1139        match constraint {
1140            TableConstraint::PrimaryKey { name, columns } => {
1141                if let Some(name) = name {
1142                    self.write_keyword("CONSTRAINT ");
1143                    self.write(name);
1144                    self.write(" ");
1145                }
1146                self.write_keyword("PRIMARY KEY (");
1147                self.write(&columns.join(", "));
1148                self.write(")");
1149            }
1150            TableConstraint::Unique { name, columns } => {
1151                if let Some(name) = name {
1152                    self.write_keyword("CONSTRAINT ");
1153                    self.write(name);
1154                    self.write(" ");
1155                }
1156                self.write_keyword("UNIQUE (");
1157                self.write(&columns.join(", "));
1158                self.write(")");
1159            }
1160            TableConstraint::ForeignKey {
1161                name,
1162                columns,
1163                ref_table,
1164                ref_columns,
1165                on_delete,
1166                on_update,
1167            } => {
1168                if let Some(name) = name {
1169                    self.write_keyword("CONSTRAINT ");
1170                    self.write(name);
1171                    self.write(" ");
1172                }
1173                self.write_keyword("FOREIGN KEY (");
1174                self.write(&columns.join(", "));
1175                self.write(") ");
1176                self.write_keyword("REFERENCES ");
1177                self.gen_table_ref(ref_table);
1178                self.write(" (");
1179                self.write(&ref_columns.join(", "));
1180                self.write(")");
1181                if let Some(action) = on_delete {
1182                    self.write(" ");
1183                    self.write_keyword("ON DELETE ");
1184                    self.gen_referential_action(action);
1185                }
1186                if let Some(action) = on_update {
1187                    self.write(" ");
1188                    self.write_keyword("ON UPDATE ");
1189                    self.gen_referential_action(action);
1190                }
1191            }
1192            TableConstraint::Check { name, expr } => {
1193                if let Some(name) = name {
1194                    self.write_keyword("CONSTRAINT ");
1195                    self.write(name);
1196                    self.write(" ");
1197                }
1198                self.write_keyword("CHECK (");
1199                self.gen_expr(expr);
1200                self.write(")");
1201            }
1202        }
1203    }
1204
1205    fn gen_referential_action(&mut self, action: &ReferentialAction) {
1206        match action {
1207            ReferentialAction::Cascade => self.write_keyword("CASCADE"),
1208            ReferentialAction::Restrict => self.write_keyword("RESTRICT"),
1209            ReferentialAction::NoAction => self.write_keyword("NO ACTION"),
1210            ReferentialAction::SetNull => self.write_keyword("SET NULL"),
1211            ReferentialAction::SetDefault => self.write_keyword("SET DEFAULT"),
1212        }
1213    }
1214
1215    // ── DROP TABLE ──────────────────────────────────────────────
1216
1217    fn gen_drop_table(&mut self, dt: &DropTableStatement) {
1218        self.write_keyword("DROP TABLE ");
1219        if dt.if_exists {
1220            self.write_keyword("IF EXISTS ");
1221        }
1222        self.gen_table_ref(&dt.table);
1223        if dt.cascade {
1224            self.write(" ");
1225            self.write_keyword("CASCADE");
1226        }
1227    }
1228
1229    // ── ALTER TABLE ─────────────────────────────────────────────
1230
1231    fn gen_alter_table(&mut self, alt: &AlterTableStatement) {
1232        self.write_keyword("ALTER TABLE ");
1233        self.gen_table_ref(&alt.table);
1234
1235        for (i, action) in alt.actions.iter().enumerate() {
1236            if i > 0 {
1237                self.write(",");
1238            }
1239            self.write(" ");
1240            match action {
1241                AlterTableAction::AddColumn(col) => {
1242                    self.write_keyword("ADD COLUMN ");
1243                    self.gen_column_def(col);
1244                }
1245                AlterTableAction::DropColumn { name, if_exists } => {
1246                    self.write_keyword("DROP COLUMN ");
1247                    if *if_exists {
1248                        self.write_keyword("IF EXISTS ");
1249                    }
1250                    self.write(name);
1251                }
1252                AlterTableAction::RenameColumn { old_name, new_name } => {
1253                    self.write_keyword("RENAME COLUMN ");
1254                    self.write(old_name);
1255                    self.write(" ");
1256                    self.write_keyword("TO ");
1257                    self.write(new_name);
1258                }
1259                AlterTableAction::AlterColumnType { name, data_type } => {
1260                    self.write_keyword("ALTER COLUMN ");
1261                    self.write(name);
1262                    self.write(" ");
1263                    self.write_keyword("TYPE ");
1264                    self.gen_data_type(data_type);
1265                }
1266                AlterTableAction::AddConstraint(constraint) => {
1267                    self.write_keyword("ADD ");
1268                    self.gen_table_constraint(constraint);
1269                }
1270                AlterTableAction::DropConstraint { name } => {
1271                    self.write_keyword("DROP CONSTRAINT ");
1272                    self.write(name);
1273                }
1274                AlterTableAction::RenameTable { new_name } => {
1275                    self.write_keyword("RENAME TO ");
1276                    self.write(new_name);
1277                }
1278            }
1279        }
1280    }
1281
1282    // ── CREATE / DROP VIEW ──────────────────────────────────────
1283
1284    fn gen_create_view(&mut self, cv: &CreateViewStatement) {
1285        self.write_keyword("CREATE ");
1286        if cv.or_replace {
1287            self.write_keyword("OR REPLACE ");
1288        }
1289        if cv.materialized {
1290            self.write_keyword("MATERIALIZED ");
1291        }
1292        self.write_keyword("VIEW ");
1293        if cv.if_not_exists {
1294            self.write_keyword("IF NOT EXISTS ");
1295        }
1296        self.gen_table_ref(&cv.name);
1297
1298        if !cv.columns.is_empty() {
1299            self.write(" (");
1300            self.write(&cv.columns.join(", "));
1301            self.write(")");
1302        }
1303
1304        self.write(" ");
1305        self.write_keyword("AS ");
1306        self.gen_statement(&cv.query);
1307    }
1308
1309    fn gen_drop_view(&mut self, dv: &DropViewStatement) {
1310        self.write_keyword("DROP ");
1311        if dv.materialized {
1312            self.write_keyword("MATERIALIZED ");
1313        }
1314        self.write_keyword("VIEW ");
1315        if dv.if_exists {
1316            self.write_keyword("IF EXISTS ");
1317        }
1318        self.gen_table_ref(&dv.name);
1319    }
1320
1321    // ── TRUNCATE ────────────────────────────────────────────────
1322
1323    fn gen_truncate(&mut self, t: &TruncateStatement) {
1324        self.write_keyword("TRUNCATE TABLE ");
1325        self.gen_table_ref(&t.table);
1326    }
1327
1328    // ── Transaction ─────────────────────────────────────────────
1329
1330    fn gen_transaction(&mut self, t: &TransactionStatement) {
1331        match t {
1332            TransactionStatement::Begin => self.write_keyword("BEGIN"),
1333            TransactionStatement::Commit => self.write_keyword("COMMIT"),
1334            TransactionStatement::Rollback => self.write_keyword("ROLLBACK"),
1335            TransactionStatement::Savepoint(name) => {
1336                self.write_keyword("SAVEPOINT ");
1337                self.write(name);
1338            }
1339            TransactionStatement::ReleaseSavepoint(name) => {
1340                self.write_keyword("RELEASE SAVEPOINT ");
1341                self.write(name);
1342            }
1343            TransactionStatement::RollbackTo(name) => {
1344                self.write_keyword("ROLLBACK TO SAVEPOINT ");
1345                self.write(name);
1346            }
1347        }
1348    }
1349
1350    // ── EXPLAIN ─────────────────────────────────────────────────
1351
1352    fn gen_explain(&mut self, e: &ExplainStatement) {
1353        self.write_keyword("EXPLAIN ");
1354        if e.analyze {
1355            self.write_keyword("ANALYZE ");
1356        }
1357        self.gen_statement(&e.statement);
1358    }
1359
1360    // ── USE ─────────────────────────────────────────────────────
1361
1362    fn gen_use(&mut self, u: &UseStatement) {
1363        self.write_keyword("USE ");
1364        self.write(&u.name);
1365    }
1366
1367    // ══════════════════════════════════════════════════════════════
1368    // Data types
1369    // ══════════════════════════════════════════════════════════════
1370
1371    fn gen_data_type(&mut self, dt: &DataType) {
1372        match dt {
1373            DataType::TinyInt => self.write("TINYINT"),
1374            DataType::SmallInt => self.write("SMALLINT"),
1375            DataType::Int => self.write("INT"),
1376            DataType::BigInt => self.write("BIGINT"),
1377            DataType::Float => self.write("FLOAT"),
1378            DataType::Double => self.write("DOUBLE"),
1379            DataType::Real => self.write("REAL"),
1380            DataType::Decimal { precision, scale } | DataType::Numeric { precision, scale } => {
1381                self.write(if matches!(dt, DataType::Numeric { .. }) {
1382                    "NUMERIC"
1383                } else {
1384                    "DECIMAL"
1385                });
1386                if let Some(p) = precision {
1387                    self.write(&format!("({p}"));
1388                    if let Some(s) = scale {
1389                        self.write(&format!(", {s}"));
1390                    }
1391                    self.write(")");
1392                }
1393            }
1394            DataType::Varchar(len) => {
1395                self.write("VARCHAR");
1396                if let Some(n) = len {
1397                    self.write(&format!("({n})"));
1398                }
1399            }
1400            DataType::Char(len) => {
1401                self.write("CHAR");
1402                if let Some(n) = len {
1403                    self.write(&format!("({n})"));
1404                }
1405            }
1406            DataType::Text => self.write("TEXT"),
1407            DataType::String => self.write("STRING"),
1408            DataType::Binary(len) => {
1409                self.write("BINARY");
1410                if let Some(n) = len {
1411                    self.write(&format!("({n})"));
1412                }
1413            }
1414            DataType::Varbinary(len) => {
1415                self.write("VARBINARY");
1416                if let Some(n) = len {
1417                    self.write(&format!("({n})"));
1418                }
1419            }
1420            DataType::Boolean => self.write("BOOLEAN"),
1421            DataType::Date => self.write("DATE"),
1422            DataType::Time { precision } => {
1423                self.write("TIME");
1424                if let Some(p) = precision {
1425                    self.write(&format!("({p})"));
1426                }
1427            }
1428            DataType::Timestamp { precision, with_tz } => {
1429                self.write("TIMESTAMP");
1430                if let Some(p) = precision {
1431                    self.write(&format!("({p})"));
1432                }
1433                if *with_tz {
1434                    self.write(" WITH TIME ZONE");
1435                }
1436            }
1437            DataType::Interval => self.write("INTERVAL"),
1438            DataType::DateTime => self.write("DATETIME"),
1439            DataType::Blob => self.write("BLOB"),
1440            DataType::Bytea => self.write("BYTEA"),
1441            DataType::Bytes => self.write("BYTES"),
1442            DataType::Json => self.write("JSON"),
1443            DataType::Jsonb => self.write("JSONB"),
1444            DataType::Uuid => self.write("UUID"),
1445            DataType::Array(inner) => {
1446                let is_postgres = matches!(
1447                    self.dialect,
1448                    Some(
1449                        Dialect::Postgres
1450                            | Dialect::Redshift
1451                            | Dialect::Materialize
1452                            | Dialect::RisingWave
1453                    )
1454                );
1455                if is_postgres {
1456                    // PostgreSQL: emit "typename[]"
1457                    if let Some(inner) = inner {
1458                        self.gen_data_type(inner);
1459                        self.write("[]");
1460                    } else {
1461                        self.write("ARRAY");
1462                    }
1463                } else {
1464                    self.write("ARRAY");
1465                    if let Some(inner) = inner {
1466                        self.write("<");
1467                        self.gen_data_type(inner);
1468                        self.write(">");
1469                    }
1470                }
1471            }
1472            DataType::Map { key, value } => {
1473                self.write("MAP<");
1474                self.gen_data_type(key);
1475                self.write(", ");
1476                self.gen_data_type(value);
1477                self.write(">");
1478            }
1479            DataType::Struct(fields) => {
1480                self.write("STRUCT<");
1481                for (i, (name, dt)) in fields.iter().enumerate() {
1482                    if i > 0 {
1483                        self.write(", ");
1484                    }
1485                    self.write(name);
1486                    self.write(" ");
1487                    self.gen_data_type(dt);
1488                }
1489                self.write(">");
1490            }
1491            DataType::Tuple(types) => {
1492                self.write("TUPLE(");
1493                for (i, dt) in types.iter().enumerate() {
1494                    if i > 0 {
1495                        self.write(", ");
1496                    }
1497                    self.gen_data_type(dt);
1498                }
1499                self.write(")");
1500            }
1501            DataType::Null => self.write("NULL"),
1502            DataType::Variant => self.write("VARIANT"),
1503            DataType::Object => self.write("OBJECT"),
1504            DataType::Xml => self.write("XML"),
1505            DataType::Inet => self.write("INET"),
1506            DataType::Cidr => self.write("CIDR"),
1507            DataType::Macaddr => self.write("MACADDR"),
1508            DataType::Bit(len) => {
1509                self.write("BIT");
1510                if let Some(n) = len {
1511                    self.write(&format!("({n})"));
1512                }
1513            }
1514            DataType::Money => self.write("MONEY"),
1515            DataType::Serial => self.write("SERIAL"),
1516            DataType::BigSerial => self.write("BIGSERIAL"),
1517            DataType::SmallSerial => self.write("SMALLSERIAL"),
1518            DataType::Regclass => self.write("REGCLASS"),
1519            DataType::Regtype => self.write("REGTYPE"),
1520            DataType::Hstore => self.write("HSTORE"),
1521            DataType::Geography => self.write("GEOGRAPHY"),
1522            DataType::Geometry => self.write("GEOMETRY"),
1523            DataType::Super => self.write("SUPER"),
1524            DataType::Unknown(name) => self.write(name),
1525        }
1526    }
1527
1528    // ══════════════════════════════════════════════════════════════
1529    // Expressions
1530    // ══════════════════════════════════════════════════════════════
1531
1532    fn binary_op_str(op: &BinaryOperator) -> &'static str {
1533        match op {
1534            BinaryOperator::Plus => " + ",
1535            BinaryOperator::Minus => " - ",
1536            BinaryOperator::Multiply => " * ",
1537            BinaryOperator::Divide => " / ",
1538            BinaryOperator::Modulo => " % ",
1539            BinaryOperator::Eq => " = ",
1540            BinaryOperator::Neq => " <> ",
1541            BinaryOperator::Lt => " < ",
1542            BinaryOperator::Gt => " > ",
1543            BinaryOperator::LtEq => " <= ",
1544            BinaryOperator::GtEq => " >= ",
1545            BinaryOperator::And => " AND ",
1546            BinaryOperator::Or => " OR ",
1547            BinaryOperator::Xor => " XOR ",
1548            BinaryOperator::Concat => " || ",
1549            BinaryOperator::BitwiseAnd => " & ",
1550            BinaryOperator::BitwiseOr => " | ",
1551            BinaryOperator::BitwiseXor => " ^ ",
1552            BinaryOperator::ShiftLeft => " << ",
1553            BinaryOperator::ShiftRight => " >> ",
1554            BinaryOperator::Arrow => " -> ",
1555            BinaryOperator::DoubleArrow => " ->> ",
1556        }
1557    }
1558
1559    fn gen_expr_list(&mut self, exprs: &[Expr]) {
1560        for (i, expr) in exprs.iter().enumerate() {
1561            if i > 0 {
1562                self.write(", ");
1563            }
1564            self.gen_expr(expr);
1565        }
1566    }
1567
1568    fn gen_expr(&mut self, expr: &Expr) {
1569        match expr {
1570            Expr::Column {
1571                table,
1572                name,
1573                quote_style,
1574                table_quote_style,
1575            } => {
1576                if let Some(t) = table {
1577                    self.write_quoted(t, *table_quote_style);
1578                    self.write(".");
1579                }
1580                self.write_quoted(name, *quote_style);
1581            }
1582            Expr::Number(n) => self.write(n),
1583            Expr::StringLiteral(s) => {
1584                self.write("'");
1585                self.write(&s.replace('\'', "''"));
1586                self.write("'");
1587            }
1588            Expr::Boolean(b) => self.write(if *b { "TRUE" } else { "FALSE" }),
1589            Expr::Null => self.write("NULL"),
1590            Expr::Default => self.write_keyword("DEFAULT"),
1591            Expr::Wildcard | Expr::Star => self.write("*"),
1592
1593            Expr::Cube { exprs } => {
1594                self.write_keyword("CUBE");
1595                self.write("(");
1596                self.gen_expr_list(exprs);
1597                self.write(")");
1598            }
1599            Expr::Rollup { exprs } => {
1600                self.write_keyword("ROLLUP");
1601                self.write("(");
1602                self.gen_expr_list(exprs);
1603                self.write(")");
1604            }
1605            Expr::GroupingSets { sets } => {
1606                self.write_keyword("GROUPING SETS");
1607                self.write("(");
1608                self.gen_expr_list(sets);
1609                self.write(")");
1610            }
1611
1612            Expr::BinaryOp { left, op, right } => {
1613                self.gen_expr(left);
1614                self.write(Self::binary_op_str(op));
1615                self.gen_expr(right);
1616            }
1617            Expr::AnyOp { expr, op, right } => {
1618                self.gen_expr(expr);
1619                self.write(Self::binary_op_str(op));
1620                self.write_keyword("ANY");
1621                self.write("(");
1622                if let Expr::Subquery(query) = right.as_ref() {
1623                    self.gen_statement(query);
1624                } else {
1625                    self.gen_expr(right);
1626                }
1627                self.write(")");
1628            }
1629            Expr::AllOp { expr, op, right } => {
1630                self.gen_expr(expr);
1631                self.write(Self::binary_op_str(op));
1632                self.write_keyword("ALL");
1633                self.write("(");
1634                if let Expr::Subquery(query) = right.as_ref() {
1635                    self.gen_statement(query);
1636                } else {
1637                    self.gen_expr(right);
1638                }
1639                self.write(")");
1640            }
1641            Expr::UnaryOp { op, expr } => {
1642                let op_str = match op {
1643                    UnaryOperator::Not => "NOT ",
1644                    UnaryOperator::Minus => "-",
1645                    UnaryOperator::Plus => "+",
1646                    UnaryOperator::BitwiseNot => "~",
1647                };
1648                self.write(op_str);
1649                self.gen_expr(expr);
1650            }
1651            Expr::Function {
1652                name,
1653                args,
1654                distinct,
1655                filter,
1656                over,
1657            } => {
1658                self.write(name);
1659                self.write("(");
1660                if *distinct {
1661                    self.write_keyword("DISTINCT ");
1662                }
1663                self.gen_expr_list(args);
1664                self.write(")");
1665
1666                if let Some(filter_expr) = filter {
1667                    self.write(" ");
1668                    self.write_keyword("FILTER (WHERE ");
1669                    self.gen_expr(filter_expr);
1670                    self.write(")");
1671                }
1672                if let Some(spec) = over {
1673                    self.write(" ");
1674                    self.write_keyword("OVER ");
1675                    if let Some(wref) = &spec.window_ref {
1676                        if spec.partition_by.is_empty()
1677                            && spec.order_by.is_empty()
1678                            && spec.frame.is_none()
1679                        {
1680                            self.write(wref);
1681                        } else {
1682                            self.write("(");
1683                            self.gen_window_spec(spec);
1684                            self.write(")");
1685                        }
1686                    } else {
1687                        self.write("(");
1688                        self.gen_window_spec(spec);
1689                        self.write(")");
1690                    }
1691                }
1692            }
1693            Expr::Between {
1694                expr,
1695                low,
1696                high,
1697                negated,
1698            } => {
1699                self.gen_expr(expr);
1700                if *negated {
1701                    self.write(" ");
1702                    self.write_keyword("NOT");
1703                }
1704                self.write(" ");
1705                self.write_keyword("BETWEEN ");
1706                self.gen_expr(low);
1707                self.write(" ");
1708                self.write_keyword("AND ");
1709                self.gen_expr(high);
1710            }
1711            Expr::InList {
1712                expr,
1713                list,
1714                negated,
1715            } => {
1716                self.gen_expr(expr);
1717                if *negated {
1718                    self.write(" ");
1719                    self.write_keyword("NOT");
1720                }
1721                self.write(" ");
1722                self.write_keyword("IN (");
1723                self.gen_expr_list(list);
1724                self.write(")");
1725            }
1726            Expr::InSubquery {
1727                expr,
1728                subquery,
1729                negated,
1730            } => {
1731                self.gen_expr(expr);
1732                if *negated {
1733                    self.write(" ");
1734                    self.write_keyword("NOT");
1735                }
1736                self.write(" ");
1737                self.write_keyword("IN (");
1738                self.gen_statement(subquery);
1739                self.write(")");
1740            }
1741            Expr::IsNull { expr, negated } => {
1742                self.gen_expr(expr);
1743                if *negated {
1744                    self.write(" ");
1745                    self.write_keyword("IS NOT NULL");
1746                } else {
1747                    self.write(" ");
1748                    self.write_keyword("IS NULL");
1749                }
1750            }
1751            Expr::IsBool {
1752                expr,
1753                value,
1754                negated,
1755            } => {
1756                self.gen_expr(expr);
1757                self.write(" ");
1758                match (negated, value) {
1759                    (false, true) => self.write_keyword("IS TRUE"),
1760                    (false, false) => self.write_keyword("IS FALSE"),
1761                    (true, true) => self.write_keyword("IS NOT TRUE"),
1762                    (true, false) => self.write_keyword("IS NOT FALSE"),
1763                }
1764            }
1765            Expr::Like {
1766                expr,
1767                pattern,
1768                negated,
1769                escape,
1770            } => {
1771                self.gen_expr(expr);
1772                if *negated {
1773                    self.write(" ");
1774                    self.write_keyword("NOT");
1775                }
1776                self.write(" ");
1777                self.write_keyword("LIKE ");
1778                self.gen_expr(pattern);
1779                if let Some(esc) = escape {
1780                    self.write(" ");
1781                    self.write_keyword("ESCAPE ");
1782                    self.gen_expr(esc);
1783                }
1784            }
1785            Expr::ILike {
1786                expr,
1787                pattern,
1788                negated,
1789                escape,
1790            } => {
1791                self.gen_expr(expr);
1792                if *negated {
1793                    self.write(" ");
1794                    self.write_keyword("NOT");
1795                }
1796                self.write(" ");
1797                self.write_keyword("ILIKE ");
1798                self.gen_expr(pattern);
1799                if let Some(esc) = escape {
1800                    self.write(" ");
1801                    self.write_keyword("ESCAPE ");
1802                    self.gen_expr(esc);
1803                }
1804            }
1805            Expr::Case {
1806                operand,
1807                when_clauses,
1808                else_clause,
1809            } => {
1810                self.write_keyword("CASE");
1811                if let Some(op) = operand {
1812                    self.write(" ");
1813                    self.gen_expr(op);
1814                }
1815                for (cond, result) in when_clauses {
1816                    self.write(" ");
1817                    self.write_keyword("WHEN ");
1818                    self.gen_expr(cond);
1819                    self.write(" ");
1820                    self.write_keyword("THEN ");
1821                    self.gen_expr(result);
1822                }
1823                if let Some(el) = else_clause {
1824                    self.write(" ");
1825                    self.write_keyword("ELSE ");
1826                    self.gen_expr(el);
1827                }
1828                self.write(" ");
1829                self.write_keyword("END");
1830            }
1831            Expr::Nested(inner) => {
1832                self.write("(");
1833                self.gen_expr(inner);
1834                self.write(")");
1835            }
1836            Expr::Subquery(query) => {
1837                self.write("(");
1838                self.gen_statement(query);
1839                self.write(")");
1840            }
1841            Expr::Exists { subquery, negated } => {
1842                if *negated {
1843                    self.write_keyword("NOT ");
1844                }
1845                self.write_keyword("EXISTS (");
1846                self.gen_statement(subquery);
1847                self.write(")");
1848            }
1849            Expr::Cast { expr, data_type } => {
1850                // ANSI typed string literals: emit DATE 'x' / TIMESTAMP 'x' / TIME 'x'
1851                if let Expr::StringLiteral(val) = expr.as_ref() {
1852                    let ansi_keyword = match data_type {
1853                        DataType::Date => Some("DATE"),
1854                        DataType::Timestamp { .. } => Some("TIMESTAMP"),
1855                        DataType::Time { .. } => Some("TIME"),
1856                        _ => None,
1857                    };
1858                    if let Some(kw) = ansi_keyword {
1859                        let is_mysql_family = matches!(
1860                            self.dialect,
1861                            Some(
1862                                Dialect::Mysql
1863                                    | Dialect::Doris
1864                                    | Dialect::SingleStore
1865                                    | Dialect::StarRocks
1866                            )
1867                        );
1868                        if !is_mysql_family {
1869                            self.write_keyword(kw);
1870                            self.write(" '");
1871                            self.write(val);
1872                            self.write("'");
1873                            return;
1874                        }
1875                    }
1876                }
1877
1878                let is_postgres = matches!(
1879                    self.dialect,
1880                    Some(
1881                        Dialect::Postgres
1882                            | Dialect::Redshift
1883                            | Dialect::Materialize
1884                            | Dialect::RisingWave
1885                    )
1886                );
1887                if is_postgres {
1888                    self.gen_expr(expr);
1889                    self.write("::");
1890                    self.gen_data_type(data_type);
1891                } else {
1892                    self.write_keyword("CAST(");
1893                    self.gen_expr(expr);
1894                    self.write(" ");
1895                    self.write_keyword("AS ");
1896                    self.gen_data_type(data_type);
1897                    self.write(")");
1898                }
1899            }
1900            Expr::TryCast { expr, data_type } => {
1901                self.write_keyword("TRY_CAST(");
1902                self.gen_expr(expr);
1903                self.write(" ");
1904                self.write_keyword("AS ");
1905                self.gen_data_type(data_type);
1906                self.write(")");
1907            }
1908            Expr::Extract { field, expr } => {
1909                self.write_keyword("EXTRACT(");
1910                self.gen_datetime_field(field);
1911                self.write(" ");
1912                self.write_keyword("FROM ");
1913                self.gen_expr(expr);
1914                self.write(")");
1915            }
1916            Expr::Interval { value, unit } => {
1917                self.write_keyword("INTERVAL ");
1918                self.gen_expr(value);
1919                if let Some(unit) = unit {
1920                    self.write(" ");
1921                    self.gen_datetime_field(unit);
1922                }
1923            }
1924            Expr::ArrayLiteral(items) => {
1925                self.write_keyword("ARRAY[");
1926                self.gen_expr_list(items);
1927                self.write("]");
1928            }
1929            Expr::Tuple(items) => {
1930                self.write("(");
1931                self.gen_expr_list(items);
1932                self.write(")");
1933            }
1934            Expr::Coalesce(items) => {
1935                self.write_keyword("COALESCE(");
1936                self.gen_expr_list(items);
1937                self.write(")");
1938            }
1939            Expr::If {
1940                condition,
1941                true_val,
1942                false_val,
1943            } => {
1944                self.write_keyword("IF(");
1945                self.gen_expr(condition);
1946                self.write(", ");
1947                self.gen_expr(true_val);
1948                if let Some(fv) = false_val {
1949                    self.write(", ");
1950                    self.gen_expr(fv);
1951                }
1952                self.write(")");
1953            }
1954            Expr::NullIf { expr, r#else } => {
1955                self.write_keyword("NULLIF(");
1956                self.gen_expr(expr);
1957                self.write(", ");
1958                self.gen_expr(r#else);
1959                self.write(")");
1960            }
1961            Expr::Collate { expr, collation } => {
1962                self.gen_expr(expr);
1963                self.write(" ");
1964                self.write_keyword("COLLATE ");
1965                self.write(collation);
1966            }
1967            Expr::Parameter(p) => self.write(p),
1968            Expr::TypeExpr(dt) => self.gen_data_type(dt),
1969            Expr::QualifiedWildcard { table } => {
1970                self.write(table);
1971                self.write(".*");
1972            }
1973            Expr::Alias { expr, name } => {
1974                self.gen_expr(expr);
1975                self.write(" ");
1976                self.write_keyword("AS ");
1977                self.write(name);
1978            }
1979            Expr::ArrayIndex { expr, index } => {
1980                self.gen_expr(expr);
1981                self.write("[");
1982                self.gen_expr(index);
1983                self.write("]");
1984            }
1985            Expr::JsonAccess {
1986                expr,
1987                path,
1988                as_text,
1989            } => {
1990                self.gen_expr(expr);
1991                if *as_text {
1992                    self.write("->>");
1993                } else {
1994                    self.write("->");
1995                }
1996                self.gen_expr(path);
1997            }
1998            Expr::Lambda { params, body } => {
1999                if params.len() == 1 {
2000                    self.write(&params[0]);
2001                } else {
2002                    self.write("(");
2003                    self.write(&params.join(", "));
2004                    self.write(")");
2005                }
2006                self.write(" -> ");
2007                self.gen_expr(body);
2008            }
2009            Expr::TypedFunction { func, filter, over } => {
2010                self.gen_typed_function(func);
2011
2012                if let Some(filter_expr) = filter {
2013                    self.write(" ");
2014                    self.write_keyword("FILTER (WHERE ");
2015                    self.gen_expr(filter_expr);
2016                    self.write(")");
2017                }
2018                if let Some(spec) = over {
2019                    self.write(" ");
2020                    self.write_keyword("OVER ");
2021                    if let Some(wref) = &spec.window_ref {
2022                        if spec.partition_by.is_empty()
2023                            && spec.order_by.is_empty()
2024                            && spec.frame.is_none()
2025                        {
2026                            self.write(wref);
2027                        } else {
2028                            self.write("(");
2029                            self.gen_window_spec(spec);
2030                            self.write(")");
2031                        }
2032                    } else {
2033                        self.write("(");
2034                        self.gen_window_spec(spec);
2035                        self.write(")");
2036                    }
2037                }
2038            }
2039            Expr::Commented { expr, comments } => {
2040                for comment in comments {
2041                    let normalized = self.normalize_comment(comment);
2042                    self.write(&normalized);
2043                    self.write(" ");
2044                }
2045                self.gen_expr(expr);
2046            }
2047        }
2048    }
2049
2050    fn gen_window_spec(&mut self, spec: &WindowSpec) {
2051        if let Some(wref) = &spec.window_ref {
2052            self.write(wref);
2053            if !spec.partition_by.is_empty() || !spec.order_by.is_empty() || spec.frame.is_some() {
2054                self.write(" ");
2055            }
2056        }
2057        if !spec.partition_by.is_empty() {
2058            self.write_keyword("PARTITION BY ");
2059            self.gen_expr_list(&spec.partition_by);
2060        }
2061        if !spec.order_by.is_empty() {
2062            if !spec.partition_by.is_empty() {
2063                self.write(" ");
2064            }
2065            self.write_keyword("ORDER BY ");
2066            for (i, item) in spec.order_by.iter().enumerate() {
2067                if i > 0 {
2068                    self.write(", ");
2069                }
2070                self.gen_expr(&item.expr);
2071                if !item.ascending {
2072                    self.write(" ");
2073                    self.write_keyword("DESC");
2074                }
2075                if let Some(nulls_first) = item.nulls_first {
2076                    if nulls_first {
2077                        self.write(" ");
2078                        self.write_keyword("NULLS FIRST");
2079                    } else {
2080                        self.write(" ");
2081                        self.write_keyword("NULLS LAST");
2082                    }
2083                }
2084            }
2085        }
2086        if let Some(frame) = &spec.frame {
2087            self.write(" ");
2088            self.gen_window_frame(frame);
2089        }
2090    }
2091
2092    fn gen_window_frame(&mut self, frame: &WindowFrame) {
2093        match frame.kind {
2094            WindowFrameKind::Rows => self.write_keyword("ROWS "),
2095            WindowFrameKind::Range => self.write_keyword("RANGE "),
2096            WindowFrameKind::Groups => self.write_keyword("GROUPS "),
2097        }
2098        if let Some(end) = &frame.end {
2099            self.write_keyword("BETWEEN ");
2100            self.gen_window_frame_bound(&frame.start);
2101            self.write(" ");
2102            self.write_keyword("AND ");
2103            self.gen_window_frame_bound(end);
2104        } else {
2105            self.gen_window_frame_bound(&frame.start);
2106        }
2107    }
2108
2109    fn gen_window_frame_bound(&mut self, bound: &WindowFrameBound) {
2110        match bound {
2111            WindowFrameBound::CurrentRow => self.write_keyword("CURRENT ROW"),
2112            WindowFrameBound::Preceding(None) => self.write_keyword("UNBOUNDED PRECEDING"),
2113            WindowFrameBound::Preceding(Some(n)) => {
2114                self.gen_expr(n);
2115                self.write(" ");
2116                self.write_keyword("PRECEDING");
2117            }
2118            WindowFrameBound::Following(None) => self.write_keyword("UNBOUNDED FOLLOWING"),
2119            WindowFrameBound::Following(Some(n)) => {
2120                self.gen_expr(n);
2121                self.write(" ");
2122                self.write_keyword("FOLLOWING");
2123            }
2124        }
2125    }
2126
2127    fn gen_datetime_field(&mut self, field: &DateTimeField) {
2128        let name = match field {
2129            DateTimeField::Year => "YEAR",
2130            DateTimeField::Quarter => "QUARTER",
2131            DateTimeField::Month => "MONTH",
2132            DateTimeField::Week => "WEEK",
2133            DateTimeField::Day => "DAY",
2134            DateTimeField::DayOfWeek => "DOW",
2135            DateTimeField::DayOfYear => "DOY",
2136            DateTimeField::Hour => "HOUR",
2137            DateTimeField::Minute => "MINUTE",
2138            DateTimeField::Second => "SECOND",
2139            DateTimeField::Millisecond => "MILLISECOND",
2140            DateTimeField::Microsecond => "MICROSECOND",
2141            DateTimeField::Nanosecond => "NANOSECOND",
2142            DateTimeField::Epoch => "EPOCH",
2143            DateTimeField::Timezone => "TIMEZONE",
2144            DateTimeField::TimezoneHour => "TIMEZONE_HOUR",
2145            DateTimeField::TimezoneMinute => "TIMEZONE_MINUTE",
2146        };
2147        self.write(name);
2148    }
2149
2150    /// Generate SQL for a typed function expression.
2151    fn gen_typed_function(&mut self, func: &TypedFunction) {
2152        let dialect = self.dialect;
2153        let is_tsql = matches!(dialect, Some(Dialect::Tsql) | Some(Dialect::Fabric));
2154        let is_mysql = matches!(
2155            dialect,
2156            Some(Dialect::Mysql)
2157                | Some(Dialect::SingleStore)
2158                | Some(Dialect::Doris)
2159                | Some(Dialect::StarRocks)
2160        );
2161        let is_bigquery = matches!(dialect, Some(Dialect::BigQuery));
2162        let is_snowflake = matches!(dialect, Some(Dialect::Snowflake));
2163        let is_oracle = matches!(dialect, Some(Dialect::Oracle));
2164        let is_hive_family = matches!(
2165            dialect,
2166            Some(Dialect::Hive) | Some(Dialect::Spark) | Some(Dialect::Databricks)
2167        );
2168
2169        match func {
2170            // ── Date/Time ──────────────────────────────────────────────
2171            TypedFunction::DateAdd {
2172                expr,
2173                interval,
2174                unit,
2175            } => {
2176                if is_tsql || is_snowflake {
2177                    self.write_keyword("DATEADD(");
2178                    if let Some(u) = unit {
2179                        self.gen_datetime_field(u);
2180                    } else {
2181                        self.write_keyword("DAY");
2182                    }
2183                    self.write(", ");
2184                    self.gen_expr(interval);
2185                    self.write(", ");
2186                    self.gen_expr(expr);
2187                    self.write(")");
2188                } else if is_bigquery {
2189                    self.write_keyword("DATE_ADD(");
2190                    self.gen_expr(expr);
2191                    self.write(", ");
2192                    self.write_keyword("INTERVAL ");
2193                    self.gen_expr(interval);
2194                    self.write(" ");
2195                    if let Some(u) = unit {
2196                        self.gen_datetime_field(u);
2197                    } else {
2198                        self.write_keyword("DAY");
2199                    }
2200                    self.write(")");
2201                } else {
2202                    self.write_keyword("DATE_ADD(");
2203                    self.gen_expr(expr);
2204                    self.write(", ");
2205                    self.gen_expr(interval);
2206                    if let Some(u) = unit {
2207                        self.write(", ");
2208                        self.gen_datetime_field(u);
2209                    }
2210                    self.write(")");
2211                }
2212            }
2213            TypedFunction::DateDiff { start, end, unit } => {
2214                if is_tsql || is_snowflake {
2215                    self.write_keyword("DATEDIFF(");
2216                    if let Some(u) = unit {
2217                        self.gen_datetime_field(u);
2218                    } else {
2219                        self.write_keyword("DAY");
2220                    }
2221                    self.write(", ");
2222                    self.gen_expr(start);
2223                    self.write(", ");
2224                    self.gen_expr(end);
2225                    self.write(")");
2226                } else if is_bigquery {
2227                    self.write_keyword("DATE_DIFF(");
2228                    self.gen_expr(end);
2229                    self.write(", ");
2230                    self.gen_expr(start);
2231                    self.write(", ");
2232                    if let Some(u) = unit {
2233                        self.gen_datetime_field(u);
2234                    } else {
2235                        self.write_keyword("DAY");
2236                    }
2237                    self.write(")");
2238                } else {
2239                    self.write_keyword("DATEDIFF(");
2240                    self.gen_expr(start);
2241                    self.write(", ");
2242                    self.gen_expr(end);
2243                    if let Some(u) = unit {
2244                        self.write(", ");
2245                        self.gen_datetime_field(u);
2246                    }
2247                    self.write(")");
2248                }
2249            }
2250            TypedFunction::DateTrunc { unit, expr } => {
2251                if is_tsql {
2252                    self.write_keyword("DATETRUNC(");
2253                    self.gen_datetime_field(unit);
2254                    self.write(", ");
2255                    self.gen_expr(expr);
2256                    self.write(")");
2257                } else if is_oracle {
2258                    self.write_keyword("TRUNC(");
2259                    self.gen_expr(expr);
2260                    self.write(", '");
2261                    self.gen_datetime_field(unit);
2262                    self.write("')");
2263                } else {
2264                    self.write_keyword("DATE_TRUNC(");
2265                    self.write("'");
2266                    self.gen_datetime_field(unit);
2267                    self.write("'");
2268                    self.write(", ");
2269                    self.gen_expr(expr);
2270                    self.write(")");
2271                }
2272            }
2273            TypedFunction::DateSub {
2274                expr,
2275                interval,
2276                unit,
2277            } => {
2278                if is_tsql || is_snowflake {
2279                    self.write_keyword("DATEADD(");
2280                    if let Some(u) = unit {
2281                        self.gen_datetime_field(u);
2282                    } else {
2283                        self.write_keyword("DAY");
2284                    }
2285                    self.write(", -(");
2286                    self.gen_expr(interval);
2287                    self.write("), ");
2288                    self.gen_expr(expr);
2289                    self.write(")");
2290                } else if is_bigquery {
2291                    self.write_keyword("DATE_SUB(");
2292                    self.gen_expr(expr);
2293                    self.write(", ");
2294                    self.write_keyword("INTERVAL ");
2295                    self.gen_expr(interval);
2296                    self.write(" ");
2297                    if let Some(u) = unit {
2298                        self.gen_datetime_field(u);
2299                    } else {
2300                        self.write_keyword("DAY");
2301                    }
2302                    self.write(")");
2303                } else {
2304                    self.write_keyword("DATE_SUB(");
2305                    self.gen_expr(expr);
2306                    self.write(", ");
2307                    self.gen_expr(interval);
2308                    if let Some(u) = unit {
2309                        self.write(", ");
2310                        self.gen_datetime_field(u);
2311                    }
2312                    self.write(")");
2313                }
2314            }
2315            TypedFunction::CurrentDate => {
2316                if is_tsql {
2317                    self.write_keyword("CAST(GETDATE() AS DATE)");
2318                } else if is_mysql || is_hive_family {
2319                    self.write_keyword("CURRENT_DATE()");
2320                } else {
2321                    self.write_keyword("CURRENT_DATE");
2322                }
2323            }
2324            TypedFunction::CurrentTimestamp => {
2325                if is_tsql {
2326                    self.write_keyword("GETDATE()");
2327                } else if is_mysql
2328                    || matches!(
2329                        dialect,
2330                        Some(Dialect::Postgres)
2331                            | Some(Dialect::DuckDb)
2332                            | Some(Dialect::Sqlite)
2333                            | Some(Dialect::Redshift)
2334                    )
2335                {
2336                    self.write_keyword("NOW()");
2337                } else {
2338                    self.write_keyword("CURRENT_TIMESTAMP()");
2339                }
2340            }
2341            TypedFunction::StrToTime { expr, format } => {
2342                if is_mysql {
2343                    self.write_keyword("STR_TO_DATE(");
2344                } else if is_bigquery {
2345                    self.write_keyword("PARSE_TIMESTAMP(");
2346                } else {
2347                    self.write_keyword("TO_TIMESTAMP(");
2348                }
2349                self.gen_expr(expr);
2350                self.write(", ");
2351                self.gen_expr(format);
2352                self.write(")");
2353            }
2354            TypedFunction::TimeToStr { expr, format } => {
2355                if is_mysql || is_hive_family {
2356                    self.write_keyword("DATE_FORMAT(");
2357                } else if is_bigquery {
2358                    self.write_keyword("FORMAT_TIMESTAMP(");
2359                } else if is_tsql {
2360                    self.write_keyword("FORMAT(");
2361                } else {
2362                    self.write_keyword("TO_CHAR(");
2363                }
2364                self.gen_expr(expr);
2365                self.write(", ");
2366                self.gen_expr(format);
2367                self.write(")");
2368            }
2369            TypedFunction::TsOrDsToDate { expr } => {
2370                if is_mysql {
2371                    self.write_keyword("DATE(");
2372                    self.gen_expr(expr);
2373                    self.write(")");
2374                } else {
2375                    self.write_keyword("CAST(");
2376                    self.gen_expr(expr);
2377                    self.write(" ");
2378                    self.write_keyword("AS DATE)");
2379                }
2380            }
2381            TypedFunction::Year { expr } => {
2382                if is_tsql {
2383                    self.write_keyword("YEAR(");
2384                    self.gen_expr(expr);
2385                    self.write(")");
2386                } else {
2387                    self.write_keyword("EXTRACT(YEAR FROM ");
2388                    self.gen_expr(expr);
2389                    self.write(")");
2390                }
2391            }
2392            TypedFunction::Month { expr } => {
2393                if is_tsql {
2394                    self.write_keyword("MONTH(");
2395                    self.gen_expr(expr);
2396                    self.write(")");
2397                } else {
2398                    self.write_keyword("EXTRACT(MONTH FROM ");
2399                    self.gen_expr(expr);
2400                    self.write(")");
2401                }
2402            }
2403            TypedFunction::Day { expr } => {
2404                if is_tsql {
2405                    self.write_keyword("DAY(");
2406                    self.gen_expr(expr);
2407                    self.write(")");
2408                } else {
2409                    self.write_keyword("EXTRACT(DAY FROM ");
2410                    self.gen_expr(expr);
2411                    self.write(")");
2412                }
2413            }
2414
2415            // ── String ─────────────────────────────────────────────────
2416            TypedFunction::Trim {
2417                expr,
2418                trim_type,
2419                trim_chars,
2420            } => {
2421                self.write_keyword("TRIM(");
2422                match trim_type {
2423                    TrimType::Leading => self.write_keyword("LEADING "),
2424                    TrimType::Trailing => self.write_keyword("TRAILING "),
2425                    TrimType::Both => {} // BOTH is default
2426                }
2427                if let Some(chars) = trim_chars {
2428                    self.gen_expr(chars);
2429                    self.write(" ");
2430                    self.write_keyword("FROM ");
2431                }
2432                self.gen_expr(expr);
2433                self.write(")");
2434            }
2435            TypedFunction::Substring {
2436                expr,
2437                start,
2438                length,
2439            } => {
2440                let name = if is_oracle
2441                    || is_hive_family
2442                    || is_mysql
2443                    || matches!(
2444                        dialect,
2445                        Some(Dialect::Sqlite)
2446                            | Some(Dialect::Doris)
2447                            | Some(Dialect::SingleStore)
2448                            | Some(Dialect::StarRocks)
2449                    ) {
2450                    "SUBSTR"
2451                } else {
2452                    "SUBSTRING"
2453                };
2454                self.write_keyword(name);
2455                self.write("(");
2456                self.gen_expr(expr);
2457                self.write(", ");
2458                self.gen_expr(start);
2459                if let Some(l) = length {
2460                    self.write(", ");
2461                    self.gen_expr(l);
2462                }
2463                self.write(")");
2464            }
2465            TypedFunction::Upper { expr } => {
2466                self.write_keyword("UPPER(");
2467                self.gen_expr(expr);
2468                self.write(")");
2469            }
2470            TypedFunction::Lower { expr } => {
2471                self.write_keyword("LOWER(");
2472                self.gen_expr(expr);
2473                self.write(")");
2474            }
2475            TypedFunction::RegexpLike {
2476                expr,
2477                pattern,
2478                flags,
2479            } => {
2480                self.write_keyword("REGEXP_LIKE(");
2481                self.gen_expr(expr);
2482                self.write(", ");
2483                self.gen_expr(pattern);
2484                if let Some(f) = flags {
2485                    self.write(", ");
2486                    self.gen_expr(f);
2487                }
2488                self.write(")");
2489            }
2490            TypedFunction::RegexpExtract {
2491                expr,
2492                pattern,
2493                group_index,
2494            } => {
2495                if is_bigquery || is_hive_family {
2496                    self.write_keyword("REGEXP_EXTRACT(");
2497                } else {
2498                    self.write_keyword("REGEXP_SUBSTR(");
2499                }
2500                self.gen_expr(expr);
2501                self.write(", ");
2502                self.gen_expr(pattern);
2503                if let Some(g) = group_index {
2504                    self.write(", ");
2505                    self.gen_expr(g);
2506                }
2507                self.write(")");
2508            }
2509            TypedFunction::RegexpReplace {
2510                expr,
2511                pattern,
2512                replacement,
2513                flags,
2514            } => {
2515                self.write_keyword("REGEXP_REPLACE(");
2516                self.gen_expr(expr);
2517                self.write(", ");
2518                self.gen_expr(pattern);
2519                self.write(", ");
2520                self.gen_expr(replacement);
2521                if let Some(f) = flags {
2522                    self.write(", ");
2523                    self.gen_expr(f);
2524                }
2525                self.write(")");
2526            }
2527            TypedFunction::ConcatWs { separator, exprs } => {
2528                self.write_keyword("CONCAT_WS(");
2529                self.gen_expr(separator);
2530                for e in exprs {
2531                    self.write(", ");
2532                    self.gen_expr(e);
2533                }
2534                self.write(")");
2535            }
2536            TypedFunction::Split { expr, delimiter } => {
2537                if is_tsql {
2538                    self.write_keyword("STRING_SPLIT(");
2539                } else {
2540                    self.write_keyword("SPLIT(");
2541                }
2542                self.gen_expr(expr);
2543                self.write(", ");
2544                self.gen_expr(delimiter);
2545                self.write(")");
2546            }
2547            TypedFunction::Initcap { expr } => {
2548                self.write_keyword("INITCAP(");
2549                self.gen_expr(expr);
2550                self.write(")");
2551            }
2552            TypedFunction::Length { expr } => {
2553                let name = if is_tsql || is_bigquery || is_snowflake {
2554                    "LEN"
2555                } else {
2556                    "LENGTH"
2557                };
2558                self.write_keyword(name);
2559                self.write("(");
2560                self.gen_expr(expr);
2561                self.write(")");
2562            }
2563            TypedFunction::Replace { expr, from, to } => {
2564                self.write_keyword("REPLACE(");
2565                self.gen_expr(expr);
2566                self.write(", ");
2567                self.gen_expr(from);
2568                self.write(", ");
2569                self.gen_expr(to);
2570                self.write(")");
2571            }
2572            TypedFunction::Reverse { expr } => {
2573                self.write_keyword("REVERSE(");
2574                self.gen_expr(expr);
2575                self.write(")");
2576            }
2577            TypedFunction::Left { expr, n } => {
2578                self.write_keyword("LEFT(");
2579                self.gen_expr(expr);
2580                self.write(", ");
2581                self.gen_expr(n);
2582                self.write(")");
2583            }
2584            TypedFunction::Right { expr, n } => {
2585                self.write_keyword("RIGHT(");
2586                self.gen_expr(expr);
2587                self.write(", ");
2588                self.gen_expr(n);
2589                self.write(")");
2590            }
2591            TypedFunction::Lpad { expr, length, pad } => {
2592                self.write_keyword("LPAD(");
2593                self.gen_expr(expr);
2594                self.write(", ");
2595                self.gen_expr(length);
2596                if let Some(p) = pad {
2597                    self.write(", ");
2598                    self.gen_expr(p);
2599                }
2600                self.write(")");
2601            }
2602            TypedFunction::Rpad { expr, length, pad } => {
2603                self.write_keyword("RPAD(");
2604                self.gen_expr(expr);
2605                self.write(", ");
2606                self.gen_expr(length);
2607                if let Some(p) = pad {
2608                    self.write(", ");
2609                    self.gen_expr(p);
2610                }
2611                self.write(")");
2612            }
2613
2614            // ── Aggregate ──────────────────────────────────────────────
2615            TypedFunction::Count { expr, distinct } => {
2616                self.write_keyword("COUNT(");
2617                if *distinct {
2618                    self.write_keyword("DISTINCT ");
2619                }
2620                self.gen_expr(expr);
2621                self.write(")");
2622            }
2623            TypedFunction::Sum { expr, distinct } => {
2624                self.write_keyword("SUM(");
2625                if *distinct {
2626                    self.write_keyword("DISTINCT ");
2627                }
2628                self.gen_expr(expr);
2629                self.write(")");
2630            }
2631            TypedFunction::Avg { expr, distinct } => {
2632                self.write_keyword("AVG(");
2633                if *distinct {
2634                    self.write_keyword("DISTINCT ");
2635                }
2636                self.gen_expr(expr);
2637                self.write(")");
2638            }
2639            TypedFunction::Min { expr } => {
2640                self.write_keyword("MIN(");
2641                self.gen_expr(expr);
2642                self.write(")");
2643            }
2644            TypedFunction::Max { expr } => {
2645                self.write_keyword("MAX(");
2646                self.gen_expr(expr);
2647                self.write(")");
2648            }
2649            TypedFunction::ArrayAgg { expr, distinct } => {
2650                let name = if matches!(dialect, Some(Dialect::DuckDb)) {
2651                    "LIST"
2652                } else if is_hive_family {
2653                    "COLLECT_LIST"
2654                } else {
2655                    "ARRAY_AGG"
2656                };
2657                self.write_keyword(name);
2658                self.write("(");
2659                if *distinct {
2660                    self.write_keyword("DISTINCT ");
2661                }
2662                self.gen_expr(expr);
2663                self.write(")");
2664            }
2665            TypedFunction::ApproxDistinct { expr } => {
2666                let name = if is_hive_family
2667                    || matches!(
2668                        dialect,
2669                        Some(Dialect::Presto) | Some(Dialect::Trino) | Some(Dialect::Athena)
2670                    ) {
2671                    "APPROX_DISTINCT"
2672                } else {
2673                    "APPROX_COUNT_DISTINCT"
2674                };
2675                self.write_keyword(name);
2676                self.write("(");
2677                self.gen_expr(expr);
2678                self.write(")");
2679            }
2680            TypedFunction::Variance { expr } => {
2681                let name = if is_tsql || is_oracle {
2682                    "VAR"
2683                } else {
2684                    "VARIANCE"
2685                };
2686                self.write_keyword(name);
2687                self.write("(");
2688                self.gen_expr(expr);
2689                self.write(")");
2690            }
2691            TypedFunction::Stddev { expr } => {
2692                self.write_keyword("STDDEV(");
2693                self.gen_expr(expr);
2694                self.write(")");
2695            }
2696            TypedFunction::GroupConcat {
2697                exprs,
2698                separator,
2699                order_by,
2700                distinct,
2701            } => {
2702                self.gen_group_concat(exprs, separator.as_deref(), order_by, *distinct);
2703            }
2704
2705            // ── Array ──────────────────────────────────────────────────
2706            TypedFunction::ArrayConcat { arrays } => {
2707                let name = if matches!(
2708                    dialect,
2709                    Some(Dialect::Postgres) | Some(Dialect::Redshift) | Some(Dialect::DuckDb)
2710                ) {
2711                    "ARRAY_CAT"
2712                } else {
2713                    "ARRAY_CONCAT"
2714                };
2715                self.write_keyword(name);
2716                self.write("(");
2717                self.gen_expr_list(arrays);
2718                self.write(")");
2719            }
2720            TypedFunction::ArrayContains { array, element } => {
2721                self.write_keyword("ARRAY_CONTAINS(");
2722                self.gen_expr(array);
2723                self.write(", ");
2724                self.gen_expr(element);
2725                self.write(")");
2726            }
2727            TypedFunction::ArraySize { expr } => {
2728                let name = if matches!(dialect, Some(Dialect::Postgres) | Some(Dialect::Redshift)) {
2729                    "ARRAY_LENGTH"
2730                } else if is_hive_family {
2731                    "SIZE"
2732                } else {
2733                    "ARRAY_SIZE"
2734                };
2735                self.write_keyword(name);
2736                self.write("(");
2737                self.gen_expr(expr);
2738                self.write(")");
2739            }
2740            TypedFunction::Explode { expr } => {
2741                self.write_keyword("EXPLODE(");
2742                self.gen_expr(expr);
2743                self.write(")");
2744            }
2745            TypedFunction::GenerateSeries { start, stop, step } => {
2746                self.write_keyword("GENERATE_SERIES(");
2747                self.gen_expr(start);
2748                self.write(", ");
2749                self.gen_expr(stop);
2750                if let Some(s) = step {
2751                    self.write(", ");
2752                    self.gen_expr(s);
2753                }
2754                self.write(")");
2755            }
2756            TypedFunction::Flatten { expr } => {
2757                self.write_keyword("FLATTEN(");
2758                self.gen_expr(expr);
2759                self.write(")");
2760            }
2761
2762            // ── JSON ───────────────────────────────────────────────────
2763            TypedFunction::JSONExtract { expr, path } => {
2764                if is_tsql {
2765                    self.write_keyword("JSON_VALUE(");
2766                } else {
2767                    self.write_keyword("JSON_EXTRACT(");
2768                }
2769                self.gen_expr(expr);
2770                self.write(", ");
2771                self.gen_expr(path);
2772                self.write(")");
2773            }
2774            TypedFunction::JSONExtractScalar { expr, path } => {
2775                if is_bigquery {
2776                    self.write_keyword("JSON_EXTRACT_SCALAR(");
2777                } else if is_tsql {
2778                    self.write_keyword("JSON_VALUE(");
2779                } else {
2780                    self.write_keyword("JSON_EXTRACT_SCALAR(");
2781                }
2782                self.gen_expr(expr);
2783                self.write(", ");
2784                self.gen_expr(path);
2785                self.write(")");
2786            }
2787            TypedFunction::ParseJSON { expr } => {
2788                if is_snowflake {
2789                    self.write_keyword("PARSE_JSON(");
2790                } else if is_bigquery {
2791                    self.write_keyword("JSON_PARSE(");
2792                } else {
2793                    self.write_keyword("PARSE_JSON(");
2794                }
2795                self.gen_expr(expr);
2796                self.write(")");
2797            }
2798            TypedFunction::JSONFormat { expr } => {
2799                if is_bigquery {
2800                    self.write_keyword("TO_JSON_STRING(");
2801                } else {
2802                    self.write_keyword("JSON_FORMAT(");
2803                }
2804                self.gen_expr(expr);
2805                self.write(")");
2806            }
2807
2808            // ── Window ─────────────────────────────────────────────────
2809            TypedFunction::RowNumber => self.write_keyword("ROW_NUMBER()"),
2810            TypedFunction::Rank => self.write_keyword("RANK()"),
2811            TypedFunction::DenseRank => self.write_keyword("DENSE_RANK()"),
2812            TypedFunction::NTile { n } => {
2813                self.write_keyword("NTILE(");
2814                self.gen_expr(n);
2815                self.write(")");
2816            }
2817            TypedFunction::Lead {
2818                expr,
2819                offset,
2820                default,
2821            } => {
2822                self.write_keyword("LEAD(");
2823                self.gen_expr(expr);
2824                if let Some(o) = offset {
2825                    self.write(", ");
2826                    self.gen_expr(o);
2827                }
2828                if let Some(d) = default {
2829                    self.write(", ");
2830                    self.gen_expr(d);
2831                }
2832                self.write(")");
2833            }
2834            TypedFunction::Lag {
2835                expr,
2836                offset,
2837                default,
2838            } => {
2839                self.write_keyword("LAG(");
2840                self.gen_expr(expr);
2841                if let Some(o) = offset {
2842                    self.write(", ");
2843                    self.gen_expr(o);
2844                }
2845                if let Some(d) = default {
2846                    self.write(", ");
2847                    self.gen_expr(d);
2848                }
2849                self.write(")");
2850            }
2851            TypedFunction::FirstValue { expr } => {
2852                self.write_keyword("FIRST_VALUE(");
2853                self.gen_expr(expr);
2854                self.write(")");
2855            }
2856            TypedFunction::LastValue { expr } => {
2857                self.write_keyword("LAST_VALUE(");
2858                self.gen_expr(expr);
2859                self.write(")");
2860            }
2861
2862            // ── Math ───────────────────────────────────────────────────
2863            TypedFunction::Abs { expr } => {
2864                self.write_keyword("ABS(");
2865                self.gen_expr(expr);
2866                self.write(")");
2867            }
2868            TypedFunction::Ceil { expr } => {
2869                let name = if is_tsql { "CEILING" } else { "CEIL" };
2870                self.write_keyword(name);
2871                self.write("(");
2872                self.gen_expr(expr);
2873                self.write(")");
2874            }
2875            TypedFunction::Floor { expr } => {
2876                self.write_keyword("FLOOR(");
2877                self.gen_expr(expr);
2878                self.write(")");
2879            }
2880            TypedFunction::Round { expr, decimals } => {
2881                self.write_keyword("ROUND(");
2882                self.gen_expr(expr);
2883                if let Some(d) = decimals {
2884                    self.write(", ");
2885                    self.gen_expr(d);
2886                }
2887                self.write(")");
2888            }
2889            TypedFunction::Log { expr, base } => {
2890                if let Some(b) = base {
2891                    self.write_keyword("LOG(");
2892                    if matches!(dialect, Some(Dialect::Postgres) | Some(Dialect::DuckDb)) {
2893                        // Postgres: LOG(base, expr)
2894                        self.gen_expr(b);
2895                        self.write(", ");
2896                        self.gen_expr(expr);
2897                    } else {
2898                        // Most: LOG(expr, base)
2899                        self.gen_expr(expr);
2900                        self.write(", ");
2901                        self.gen_expr(b);
2902                    }
2903                    self.write(")");
2904                } else {
2905                    // LOG(expr) — ln in Postgres, log10 in most others
2906                    self.write_keyword("LOG(");
2907                    self.gen_expr(expr);
2908                    self.write(")");
2909                }
2910            }
2911            TypedFunction::Ln { expr } => {
2912                self.write_keyword("LN(");
2913                self.gen_expr(expr);
2914                self.write(")");
2915            }
2916            TypedFunction::Pow { base, exponent } => {
2917                let name = if is_tsql || is_oracle { "POWER" } else { "POW" };
2918                self.write_keyword(name);
2919                self.write("(");
2920                self.gen_expr(base);
2921                self.write(", ");
2922                self.gen_expr(exponent);
2923                self.write(")");
2924            }
2925            TypedFunction::Sqrt { expr } => {
2926                self.write_keyword("SQRT(");
2927                self.gen_expr(expr);
2928                self.write(")");
2929            }
2930            TypedFunction::Greatest { exprs } => {
2931                self.write_keyword("GREATEST(");
2932                self.gen_expr_list(exprs);
2933                self.write(")");
2934            }
2935            TypedFunction::Least { exprs } => {
2936                self.write_keyword("LEAST(");
2937                self.gen_expr_list(exprs);
2938                self.write(")");
2939            }
2940            TypedFunction::Mod { left, right } => {
2941                self.write_keyword("MOD(");
2942                self.gen_expr(left);
2943                self.write(", ");
2944                self.gen_expr(right);
2945                self.write(")");
2946            }
2947
2948            // ── Conversion ─────────────────────────────────────────────
2949            TypedFunction::Hex { expr } => {
2950                let name = if matches!(
2951                    dialect,
2952                    Some(Dialect::Presto) | Some(Dialect::Trino) | Some(Dialect::Athena)
2953                ) {
2954                    "TO_HEX"
2955                } else {
2956                    "HEX"
2957                };
2958                self.write_keyword(name);
2959                self.write("(");
2960                self.gen_expr(expr);
2961                self.write(")");
2962            }
2963            TypedFunction::Unhex { expr } => {
2964                let name = if matches!(
2965                    dialect,
2966                    Some(Dialect::Presto) | Some(Dialect::Trino) | Some(Dialect::Athena)
2967                ) {
2968                    "FROM_HEX"
2969                } else {
2970                    "UNHEX"
2971                };
2972                self.write_keyword(name);
2973                self.write("(");
2974                self.gen_expr(expr);
2975                self.write(")");
2976            }
2977            TypedFunction::Md5 { expr } => {
2978                self.write_keyword("MD5(");
2979                self.gen_expr(expr);
2980                self.write(")");
2981            }
2982            TypedFunction::Sha { expr } => {
2983                let name = if is_mysql { "SHA1" } else { "SHA" };
2984                self.write_keyword(name);
2985                self.write("(");
2986                self.gen_expr(expr);
2987                self.write(")");
2988            }
2989            TypedFunction::Sha2 { expr, bit_length } => {
2990                self.write_keyword("SHA2(");
2991                self.gen_expr(expr);
2992                self.write(", ");
2993                self.gen_expr(bit_length);
2994                self.write(")");
2995            }
2996        }
2997    }
2998
2999    /// Emit a `GROUP_CONCAT` / `STRING_AGG` / `LISTAGG` expression
3000    /// appropriately for the active dialect.
3001    fn gen_group_concat(
3002        &mut self,
3003        exprs: &[Expr],
3004        separator: Option<&Expr>,
3005        order_by: &[OrderByItem],
3006        distinct: bool,
3007    ) {
3008        let dialect = self.dialect;
3009        let is_sqlite = matches!(dialect, Some(Dialect::Sqlite));
3010        let is_string_agg = matches!(
3011            dialect,
3012            Some(Dialect::Postgres)
3013                | Some(Dialect::Redshift)
3014                | Some(Dialect::BigQuery)
3015                | Some(Dialect::Tsql)
3016                | Some(Dialect::Fabric)
3017                | Some(Dialect::DuckDb)
3018        );
3019        let is_listagg = matches!(dialect, Some(Dialect::Oracle) | Some(Dialect::Snowflake));
3020
3021        if is_string_agg {
3022            // STRING_AGG(expr, sep [ORDER BY ...])
3023            self.write_keyword("STRING_AGG(");
3024            if distinct {
3025                self.write_keyword("DISTINCT ");
3026            }
3027            self.gen_group_concat_exprs(exprs);
3028            self.write(", ");
3029            match separator {
3030                Some(s) => self.gen_expr(s),
3031                None => self.write("','"),
3032            }
3033            if !order_by.is_empty() {
3034                self.write(" ");
3035                self.gen_group_concat_order_by(order_by);
3036            }
3037            self.write(")");
3038        } else if is_listagg {
3039            // LISTAGG(expr, sep) WITHIN GROUP (ORDER BY ...)
3040            self.write_keyword("LISTAGG(");
3041            if distinct {
3042                self.write_keyword("DISTINCT ");
3043            }
3044            self.gen_group_concat_exprs(exprs);
3045            self.write(", ");
3046            match separator {
3047                Some(s) => self.gen_expr(s),
3048                None => self.write("','"),
3049            }
3050            self.write(")");
3051            if !order_by.is_empty() {
3052                self.write(" ");
3053                self.write_keyword("WITHIN GROUP");
3054                self.write(" (");
3055                self.gen_group_concat_order_by(order_by);
3056                self.write(")");
3057            }
3058        } else if is_sqlite {
3059            // SQLite: GROUP_CONCAT(expr[, sep]). No DISTINCT/ORDER BY support;
3060            // they are dropped on output since the target dialect lacks them.
3061            self.write_keyword("GROUP_CONCAT(");
3062            self.gen_group_concat_exprs(exprs);
3063            if let Some(s) = separator {
3064                self.write(", ");
3065                self.gen_expr(s);
3066            }
3067            self.write(")");
3068        } else {
3069            // MySQL family (and default): full GROUP_CONCAT grammar.
3070            self.write_keyword("GROUP_CONCAT(");
3071            if distinct {
3072                self.write_keyword("DISTINCT ");
3073            }
3074            self.gen_group_concat_exprs(exprs);
3075            if !order_by.is_empty() {
3076                self.write(" ");
3077                self.gen_group_concat_order_by(order_by);
3078            }
3079            if let Some(s) = separator {
3080                self.write(" ");
3081                self.write_keyword("SEPARATOR");
3082                self.write(" ");
3083                self.gen_expr(s);
3084            }
3085            self.write(")");
3086        }
3087    }
3088
3089    fn gen_group_concat_exprs(&mut self, exprs: &[Expr]) {
3090        for (i, e) in exprs.iter().enumerate() {
3091            if i > 0 {
3092                self.write(", ");
3093            }
3094            self.gen_expr(e);
3095        }
3096    }
3097
3098    fn gen_group_concat_order_by(&mut self, order_by: &[OrderByItem]) {
3099        self.write_keyword("ORDER BY");
3100        self.write(" ");
3101        for (i, item) in order_by.iter().enumerate() {
3102            if i > 0 {
3103                self.write(", ");
3104            }
3105            self.gen_expr(&item.expr);
3106            if !item.ascending {
3107                self.write(" ");
3108                self.write_keyword("DESC");
3109            }
3110            if let Some(nulls_first) = item.nulls_first {
3111                self.write(" ");
3112                self.write_keyword(if nulls_first {
3113                    "NULLS FIRST"
3114                } else {
3115                    "NULLS LAST"
3116                });
3117            }
3118        }
3119    }
3120}
3121
3122impl Default for Generator {
3123    fn default() -> Self {
3124        Self::new()
3125    }
3126}
3127
3128#[cfg(test)]
3129mod tests {
3130    use super::*;
3131    use crate::parser::Parser;
3132
3133    fn roundtrip(sql: &str) -> String {
3134        let stmt = Parser::new(sql).unwrap().parse_statement().unwrap();
3135        let mut g = Generator::new();
3136        g.generate(&stmt)
3137    }
3138
3139    #[test]
3140    fn test_select_roundtrip() {
3141        assert_eq!(roundtrip("SELECT a, b FROM t"), "SELECT a, b FROM t");
3142    }
3143
3144    #[test]
3145    fn test_select_where() {
3146        assert_eq!(
3147            roundtrip("SELECT x FROM t WHERE x > 10"),
3148            "SELECT x FROM t WHERE x > 10"
3149        );
3150    }
3151
3152    #[test]
3153    fn test_select_wildcard() {
3154        assert_eq!(roundtrip("SELECT * FROM users"), "SELECT * FROM users");
3155    }
3156
3157    #[test]
3158    fn test_insert_values() {
3159        assert_eq!(
3160            roundtrip("INSERT INTO t (a, b) VALUES (1, 'hello')"),
3161            "INSERT INTO t (a, b) VALUES (1, 'hello')"
3162        );
3163    }
3164
3165    #[test]
3166    fn test_delete() {
3167        assert_eq!(
3168            roundtrip("DELETE FROM users WHERE id = 1"),
3169            "DELETE FROM users WHERE id = 1"
3170        );
3171    }
3172
3173    #[test]
3174    fn test_join() {
3175        assert_eq!(
3176            roundtrip("SELECT a.id, b.name FROM a INNER JOIN b ON a.id = b.a_id"),
3177            "SELECT a.id, b.name FROM a INNER JOIN b ON a.id = b.a_id"
3178        );
3179    }
3180
3181    #[test]
3182    fn test_create_table() {
3183        assert_eq!(
3184            roundtrip("CREATE TABLE users (id INT NOT NULL, name VARCHAR(255), email TEXT)"),
3185            "CREATE TABLE users (id INT NOT NULL, name VARCHAR(255), email TEXT)"
3186        );
3187    }
3188
3189    #[test]
3190    fn test_cte_roundtrip() {
3191        let sql = "WITH cte AS (SELECT 1 AS x) SELECT x FROM cte";
3192        assert_eq!(
3193            roundtrip(sql),
3194            "WITH cte AS (SELECT 1 AS x) SELECT x FROM cte"
3195        );
3196    }
3197
3198    #[test]
3199    fn test_union_roundtrip() {
3200        let sql = "SELECT 1 UNION ALL SELECT 2";
3201        assert_eq!(roundtrip(sql), "SELECT 1 UNION ALL SELECT 2");
3202    }
3203
3204    #[test]
3205    fn test_cast_roundtrip() {
3206        assert_eq!(
3207            roundtrip("SELECT CAST(x AS INT) FROM t"),
3208            "SELECT CAST(x AS INT) FROM t"
3209        );
3210    }
3211
3212    #[test]
3213    fn test_exists_roundtrip() {
3214        assert_eq!(
3215            roundtrip("SELECT * FROM t WHERE EXISTS (SELECT 1 FROM t2)"),
3216            "SELECT * FROM t WHERE EXISTS (SELECT 1 FROM t2)"
3217        );
3218    }
3219
3220    #[test]
3221    fn test_extract_roundtrip() {
3222        assert_eq!(
3223            roundtrip("SELECT EXTRACT(YEAR FROM created_at) FROM t"),
3224            "SELECT EXTRACT(YEAR FROM created_at) FROM t"
3225        );
3226    }
3227
3228    #[test]
3229    fn test_window_function_roundtrip() {
3230        assert_eq!(
3231            roundtrip("SELECT ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) FROM emp"),
3232            "SELECT ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) FROM emp"
3233        );
3234    }
3235
3236    #[test]
3237    fn test_subquery_from_roundtrip() {
3238        assert_eq!(
3239            roundtrip("SELECT * FROM (SELECT 1 AS x) AS sub"),
3240            "SELECT * FROM (SELECT 1 AS x) AS sub"
3241        );
3242    }
3243
3244    #[test]
3245    fn test_in_subquery_roundtrip() {
3246        assert_eq!(
3247            roundtrip("SELECT * FROM t WHERE id IN (SELECT id FROM t2)"),
3248            "SELECT * FROM t WHERE id IN (SELECT id FROM t2)"
3249        );
3250    }
3251
3252    // ═══════════════════════════════════════════════════════════════
3253    // Pretty-print tests
3254    // ═══════════════════════════════════════════════════════════════
3255
3256    fn pretty_print(sql: &str) -> String {
3257        let stmt = Parser::new(sql).unwrap().parse_statement().unwrap();
3258        let mut g = Generator::pretty();
3259        g.generate(&stmt)
3260    }
3261
3262    #[test]
3263    fn test_pretty_simple_select() {
3264        assert_eq!(
3265            pretty_print("SELECT a, b, c FROM t"),
3266            "SELECT\n  a,\n  b,\n  c\nFROM\n  t"
3267        );
3268    }
3269
3270    #[test]
3271    fn test_pretty_select_where() {
3272        assert_eq!(
3273            pretty_print("SELECT a FROM t WHERE a > 1"),
3274            "SELECT\n  a\nFROM\n  t\nWHERE\n  a > 1"
3275        );
3276    }
3277
3278    #[test]
3279    fn test_pretty_select_group_by_having() {
3280        assert_eq!(
3281            pretty_print("SELECT a, COUNT(*) FROM t GROUP BY a HAVING COUNT(*) > 1"),
3282            "SELECT\n  a,\n  COUNT(*)\nFROM\n  t\nGROUP BY\n  a\nHAVING\n  COUNT(*) > 1"
3283        );
3284    }
3285
3286    #[test]
3287    fn test_pretty_select_order_by_limit() {
3288        assert_eq!(
3289            pretty_print("SELECT a FROM t ORDER BY a DESC LIMIT 10"),
3290            "SELECT\n  a\nFROM\n  t\nORDER BY\n  a DESC\nLIMIT 10"
3291        );
3292    }
3293
3294    #[test]
3295    fn test_pretty_join() {
3296        assert_eq!(
3297            pretty_print("SELECT a.id, b.name FROM a INNER JOIN b ON a.id = b.a_id"),
3298            "SELECT\n  a.id,\n  b.name\nFROM\n  a\nINNER JOIN\n  b\n  ON a.id = b.a_id"
3299        );
3300    }
3301
3302    #[test]
3303    fn test_pretty_cte() {
3304        assert_eq!(
3305            pretty_print("WITH cte AS (SELECT 1 AS x) SELECT x FROM cte"),
3306            "WITH cte AS (\n  SELECT\n    1 AS x\n)\nSELECT\n  x\nFROM\n  cte"
3307        );
3308    }
3309
3310    #[test]
3311    fn test_pretty_union() {
3312        assert_eq!(
3313            pretty_print("SELECT 1 UNION ALL SELECT 2"),
3314            "SELECT\n  1\nUNION ALL\nSELECT\n  2"
3315        );
3316    }
3317
3318    #[test]
3319    fn test_pretty_insert() {
3320        assert_eq!(
3321            pretty_print("INSERT INTO t (a, b) VALUES (1, 'hello'), (2, 'world')"),
3322            "INSERT INTO t (a, b)\nVALUES\n  (1, 'hello'),\n  (2, 'world')"
3323        );
3324    }
3325
3326    #[test]
3327    fn test_pretty_update() {
3328        assert_eq!(
3329            pretty_print("UPDATE t SET a = 1, b = 2 WHERE c = 3"),
3330            "UPDATE t\nSET\n  a = 1,\n  b = 2\nWHERE\n  c = 3"
3331        );
3332    }
3333
3334    #[test]
3335    fn test_pretty_delete() {
3336        assert_eq!(
3337            pretty_print("DELETE FROM t WHERE id = 1"),
3338            "DELETE FROM t\nWHERE\n  id = 1"
3339        );
3340    }
3341
3342    #[test]
3343    fn test_pretty_create_table() {
3344        assert_eq!(
3345            pretty_print("CREATE TABLE t (id INT NOT NULL, name VARCHAR(255), email TEXT)"),
3346            "CREATE TABLE t (\n  id INT NOT NULL,\n  name VARCHAR(255),\n  email TEXT\n)"
3347        );
3348    }
3349
3350    #[test]
3351    fn test_pretty_complex_query() {
3352        let sql = "SELECT a, SUM(b) FROM t1 INNER JOIN t2 ON t1.id = t2.id WHERE t1.x > 1 GROUP BY a HAVING SUM(b) > 10 ORDER BY a LIMIT 100";
3353        let expected = "SELECT\n  a,\n  SUM(b)\nFROM\n  t1\nINNER JOIN\n  t2\n  ON t1.id = t2.id\nWHERE\n  t1.x > 1\nGROUP BY\n  a\nHAVING\n  SUM(b) > 10\nORDER BY\n  a\nLIMIT 100";
3354        assert_eq!(pretty_print(sql), expected);
3355    }
3356
3357    #[test]
3358    fn test_pretty_select_distinct() {
3359        assert_eq!(
3360            pretty_print("SELECT DISTINCT a, b FROM t"),
3361            "SELECT DISTINCT\n  a,\n  b\nFROM\n  t"
3362        );
3363    }
3364}