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                let is_postgres = matches!(
1851                    self.dialect,
1852                    Some(
1853                        Dialect::Postgres
1854                            | Dialect::Redshift
1855                            | Dialect::Materialize
1856                            | Dialect::RisingWave
1857                    )
1858                );
1859                if is_postgres {
1860                    self.gen_expr(expr);
1861                    self.write("::");
1862                    self.gen_data_type(data_type);
1863                } else {
1864                    self.write_keyword("CAST(");
1865                    self.gen_expr(expr);
1866                    self.write(" ");
1867                    self.write_keyword("AS ");
1868                    self.gen_data_type(data_type);
1869                    self.write(")");
1870                }
1871            }
1872            Expr::TryCast { expr, data_type } => {
1873                self.write_keyword("TRY_CAST(");
1874                self.gen_expr(expr);
1875                self.write(" ");
1876                self.write_keyword("AS ");
1877                self.gen_data_type(data_type);
1878                self.write(")");
1879            }
1880            Expr::Extract { field, expr } => {
1881                self.write_keyword("EXTRACT(");
1882                self.gen_datetime_field(field);
1883                self.write(" ");
1884                self.write_keyword("FROM ");
1885                self.gen_expr(expr);
1886                self.write(")");
1887            }
1888            Expr::Interval { value, unit } => {
1889                self.write_keyword("INTERVAL ");
1890                self.gen_expr(value);
1891                if let Some(unit) = unit {
1892                    self.write(" ");
1893                    self.gen_datetime_field(unit);
1894                }
1895            }
1896            Expr::ArrayLiteral(items) => {
1897                self.write_keyword("ARRAY[");
1898                self.gen_expr_list(items);
1899                self.write("]");
1900            }
1901            Expr::Tuple(items) => {
1902                self.write("(");
1903                self.gen_expr_list(items);
1904                self.write(")");
1905            }
1906            Expr::Coalesce(items) => {
1907                self.write_keyword("COALESCE(");
1908                self.gen_expr_list(items);
1909                self.write(")");
1910            }
1911            Expr::If {
1912                condition,
1913                true_val,
1914                false_val,
1915            } => {
1916                self.write_keyword("IF(");
1917                self.gen_expr(condition);
1918                self.write(", ");
1919                self.gen_expr(true_val);
1920                if let Some(fv) = false_val {
1921                    self.write(", ");
1922                    self.gen_expr(fv);
1923                }
1924                self.write(")");
1925            }
1926            Expr::NullIf { expr, r#else } => {
1927                self.write_keyword("NULLIF(");
1928                self.gen_expr(expr);
1929                self.write(", ");
1930                self.gen_expr(r#else);
1931                self.write(")");
1932            }
1933            Expr::Collate { expr, collation } => {
1934                self.gen_expr(expr);
1935                self.write(" ");
1936                self.write_keyword("COLLATE ");
1937                self.write(collation);
1938            }
1939            Expr::Parameter(p) => self.write(p),
1940            Expr::TypeExpr(dt) => self.gen_data_type(dt),
1941            Expr::QualifiedWildcard { table } => {
1942                self.write(table);
1943                self.write(".*");
1944            }
1945            Expr::Alias { expr, name } => {
1946                self.gen_expr(expr);
1947                self.write(" ");
1948                self.write_keyword("AS ");
1949                self.write(name);
1950            }
1951            Expr::ArrayIndex { expr, index } => {
1952                self.gen_expr(expr);
1953                self.write("[");
1954                self.gen_expr(index);
1955                self.write("]");
1956            }
1957            Expr::JsonAccess {
1958                expr,
1959                path,
1960                as_text,
1961            } => {
1962                self.gen_expr(expr);
1963                if *as_text {
1964                    self.write("->>");
1965                } else {
1966                    self.write("->");
1967                }
1968                self.gen_expr(path);
1969            }
1970            Expr::Lambda { params, body } => {
1971                if params.len() == 1 {
1972                    self.write(&params[0]);
1973                } else {
1974                    self.write("(");
1975                    self.write(&params.join(", "));
1976                    self.write(")");
1977                }
1978                self.write(" -> ");
1979                self.gen_expr(body);
1980            }
1981            Expr::TypedFunction { func, filter, over } => {
1982                self.gen_typed_function(func);
1983
1984                if let Some(filter_expr) = filter {
1985                    self.write(" ");
1986                    self.write_keyword("FILTER (WHERE ");
1987                    self.gen_expr(filter_expr);
1988                    self.write(")");
1989                }
1990                if let Some(spec) = over {
1991                    self.write(" ");
1992                    self.write_keyword("OVER ");
1993                    if let Some(wref) = &spec.window_ref {
1994                        if spec.partition_by.is_empty()
1995                            && spec.order_by.is_empty()
1996                            && spec.frame.is_none()
1997                        {
1998                            self.write(wref);
1999                        } else {
2000                            self.write("(");
2001                            self.gen_window_spec(spec);
2002                            self.write(")");
2003                        }
2004                    } else {
2005                        self.write("(");
2006                        self.gen_window_spec(spec);
2007                        self.write(")");
2008                    }
2009                }
2010            }
2011            Expr::Commented { expr, comments } => {
2012                for comment in comments {
2013                    let normalized = self.normalize_comment(comment);
2014                    self.write(&normalized);
2015                    self.write(" ");
2016                }
2017                self.gen_expr(expr);
2018            }
2019        }
2020    }
2021
2022    fn gen_window_spec(&mut self, spec: &WindowSpec) {
2023        if let Some(wref) = &spec.window_ref {
2024            self.write(wref);
2025            if !spec.partition_by.is_empty() || !spec.order_by.is_empty() || spec.frame.is_some() {
2026                self.write(" ");
2027            }
2028        }
2029        if !spec.partition_by.is_empty() {
2030            self.write_keyword("PARTITION BY ");
2031            self.gen_expr_list(&spec.partition_by);
2032        }
2033        if !spec.order_by.is_empty() {
2034            if !spec.partition_by.is_empty() {
2035                self.write(" ");
2036            }
2037            self.write_keyword("ORDER BY ");
2038            for (i, item) in spec.order_by.iter().enumerate() {
2039                if i > 0 {
2040                    self.write(", ");
2041                }
2042                self.gen_expr(&item.expr);
2043                if !item.ascending {
2044                    self.write(" ");
2045                    self.write_keyword("DESC");
2046                }
2047                if let Some(nulls_first) = item.nulls_first {
2048                    if nulls_first {
2049                        self.write(" ");
2050                        self.write_keyword("NULLS FIRST");
2051                    } else {
2052                        self.write(" ");
2053                        self.write_keyword("NULLS LAST");
2054                    }
2055                }
2056            }
2057        }
2058        if let Some(frame) = &spec.frame {
2059            self.write(" ");
2060            self.gen_window_frame(frame);
2061        }
2062    }
2063
2064    fn gen_window_frame(&mut self, frame: &WindowFrame) {
2065        match frame.kind {
2066            WindowFrameKind::Rows => self.write_keyword("ROWS "),
2067            WindowFrameKind::Range => self.write_keyword("RANGE "),
2068            WindowFrameKind::Groups => self.write_keyword("GROUPS "),
2069        }
2070        if let Some(end) = &frame.end {
2071            self.write_keyword("BETWEEN ");
2072            self.gen_window_frame_bound(&frame.start);
2073            self.write(" ");
2074            self.write_keyword("AND ");
2075            self.gen_window_frame_bound(end);
2076        } else {
2077            self.gen_window_frame_bound(&frame.start);
2078        }
2079    }
2080
2081    fn gen_window_frame_bound(&mut self, bound: &WindowFrameBound) {
2082        match bound {
2083            WindowFrameBound::CurrentRow => self.write_keyword("CURRENT ROW"),
2084            WindowFrameBound::Preceding(None) => self.write_keyword("UNBOUNDED PRECEDING"),
2085            WindowFrameBound::Preceding(Some(n)) => {
2086                self.gen_expr(n);
2087                self.write(" ");
2088                self.write_keyword("PRECEDING");
2089            }
2090            WindowFrameBound::Following(None) => self.write_keyword("UNBOUNDED FOLLOWING"),
2091            WindowFrameBound::Following(Some(n)) => {
2092                self.gen_expr(n);
2093                self.write(" ");
2094                self.write_keyword("FOLLOWING");
2095            }
2096        }
2097    }
2098
2099    fn gen_datetime_field(&mut self, field: &DateTimeField) {
2100        let name = match field {
2101            DateTimeField::Year => "YEAR",
2102            DateTimeField::Quarter => "QUARTER",
2103            DateTimeField::Month => "MONTH",
2104            DateTimeField::Week => "WEEK",
2105            DateTimeField::Day => "DAY",
2106            DateTimeField::DayOfWeek => "DOW",
2107            DateTimeField::DayOfYear => "DOY",
2108            DateTimeField::Hour => "HOUR",
2109            DateTimeField::Minute => "MINUTE",
2110            DateTimeField::Second => "SECOND",
2111            DateTimeField::Millisecond => "MILLISECOND",
2112            DateTimeField::Microsecond => "MICROSECOND",
2113            DateTimeField::Nanosecond => "NANOSECOND",
2114            DateTimeField::Epoch => "EPOCH",
2115            DateTimeField::Timezone => "TIMEZONE",
2116            DateTimeField::TimezoneHour => "TIMEZONE_HOUR",
2117            DateTimeField::TimezoneMinute => "TIMEZONE_MINUTE",
2118        };
2119        self.write(name);
2120    }
2121
2122    /// Generate SQL for a typed function expression.
2123    fn gen_typed_function(&mut self, func: &TypedFunction) {
2124        let dialect = self.dialect;
2125        let is_tsql = matches!(dialect, Some(Dialect::Tsql) | Some(Dialect::Fabric));
2126        let is_mysql = matches!(
2127            dialect,
2128            Some(Dialect::Mysql)
2129                | Some(Dialect::SingleStore)
2130                | Some(Dialect::Doris)
2131                | Some(Dialect::StarRocks)
2132        );
2133        let is_bigquery = matches!(dialect, Some(Dialect::BigQuery));
2134        let is_snowflake = matches!(dialect, Some(Dialect::Snowflake));
2135        let is_oracle = matches!(dialect, Some(Dialect::Oracle));
2136        let is_hive_family = matches!(
2137            dialect,
2138            Some(Dialect::Hive) | Some(Dialect::Spark) | Some(Dialect::Databricks)
2139        );
2140
2141        match func {
2142            // ── Date/Time ──────────────────────────────────────────────
2143            TypedFunction::DateAdd {
2144                expr,
2145                interval,
2146                unit,
2147            } => {
2148                if is_tsql || is_snowflake {
2149                    self.write_keyword("DATEADD(");
2150                    if let Some(u) = unit {
2151                        self.gen_datetime_field(u);
2152                    } else {
2153                        self.write_keyword("DAY");
2154                    }
2155                    self.write(", ");
2156                    self.gen_expr(interval);
2157                    self.write(", ");
2158                    self.gen_expr(expr);
2159                    self.write(")");
2160                } else if is_bigquery {
2161                    self.write_keyword("DATE_ADD(");
2162                    self.gen_expr(expr);
2163                    self.write(", ");
2164                    self.write_keyword("INTERVAL ");
2165                    self.gen_expr(interval);
2166                    self.write(" ");
2167                    if let Some(u) = unit {
2168                        self.gen_datetime_field(u);
2169                    } else {
2170                        self.write_keyword("DAY");
2171                    }
2172                    self.write(")");
2173                } else {
2174                    self.write_keyword("DATE_ADD(");
2175                    self.gen_expr(expr);
2176                    self.write(", ");
2177                    self.gen_expr(interval);
2178                    if let Some(u) = unit {
2179                        self.write(", ");
2180                        self.gen_datetime_field(u);
2181                    }
2182                    self.write(")");
2183                }
2184            }
2185            TypedFunction::DateDiff { start, end, unit } => {
2186                if is_tsql || is_snowflake {
2187                    self.write_keyword("DATEDIFF(");
2188                    if let Some(u) = unit {
2189                        self.gen_datetime_field(u);
2190                    } else {
2191                        self.write_keyword("DAY");
2192                    }
2193                    self.write(", ");
2194                    self.gen_expr(start);
2195                    self.write(", ");
2196                    self.gen_expr(end);
2197                    self.write(")");
2198                } else if is_bigquery {
2199                    self.write_keyword("DATE_DIFF(");
2200                    self.gen_expr(end);
2201                    self.write(", ");
2202                    self.gen_expr(start);
2203                    self.write(", ");
2204                    if let Some(u) = unit {
2205                        self.gen_datetime_field(u);
2206                    } else {
2207                        self.write_keyword("DAY");
2208                    }
2209                    self.write(")");
2210                } else {
2211                    self.write_keyword("DATEDIFF(");
2212                    self.gen_expr(start);
2213                    self.write(", ");
2214                    self.gen_expr(end);
2215                    if let Some(u) = unit {
2216                        self.write(", ");
2217                        self.gen_datetime_field(u);
2218                    }
2219                    self.write(")");
2220                }
2221            }
2222            TypedFunction::DateTrunc { unit, expr } => {
2223                if is_tsql {
2224                    self.write_keyword("DATETRUNC(");
2225                    self.gen_datetime_field(unit);
2226                    self.write(", ");
2227                    self.gen_expr(expr);
2228                    self.write(")");
2229                } else if is_oracle {
2230                    self.write_keyword("TRUNC(");
2231                    self.gen_expr(expr);
2232                    self.write(", '");
2233                    self.gen_datetime_field(unit);
2234                    self.write("')");
2235                } else {
2236                    self.write_keyword("DATE_TRUNC(");
2237                    self.write("'");
2238                    self.gen_datetime_field(unit);
2239                    self.write("'");
2240                    self.write(", ");
2241                    self.gen_expr(expr);
2242                    self.write(")");
2243                }
2244            }
2245            TypedFunction::DateSub {
2246                expr,
2247                interval,
2248                unit,
2249            } => {
2250                if is_tsql || is_snowflake {
2251                    self.write_keyword("DATEADD(");
2252                    if let Some(u) = unit {
2253                        self.gen_datetime_field(u);
2254                    } else {
2255                        self.write_keyword("DAY");
2256                    }
2257                    self.write(", -(");
2258                    self.gen_expr(interval);
2259                    self.write("), ");
2260                    self.gen_expr(expr);
2261                    self.write(")");
2262                } else if is_bigquery {
2263                    self.write_keyword("DATE_SUB(");
2264                    self.gen_expr(expr);
2265                    self.write(", ");
2266                    self.write_keyword("INTERVAL ");
2267                    self.gen_expr(interval);
2268                    self.write(" ");
2269                    if let Some(u) = unit {
2270                        self.gen_datetime_field(u);
2271                    } else {
2272                        self.write_keyword("DAY");
2273                    }
2274                    self.write(")");
2275                } else {
2276                    self.write_keyword("DATE_SUB(");
2277                    self.gen_expr(expr);
2278                    self.write(", ");
2279                    self.gen_expr(interval);
2280                    if let Some(u) = unit {
2281                        self.write(", ");
2282                        self.gen_datetime_field(u);
2283                    }
2284                    self.write(")");
2285                }
2286            }
2287            TypedFunction::CurrentDate => {
2288                if is_tsql {
2289                    self.write_keyword("CAST(GETDATE() AS DATE)");
2290                } else if is_mysql || is_hive_family {
2291                    self.write_keyword("CURRENT_DATE()");
2292                } else {
2293                    self.write_keyword("CURRENT_DATE");
2294                }
2295            }
2296            TypedFunction::CurrentTimestamp => {
2297                if is_tsql {
2298                    self.write_keyword("GETDATE()");
2299                } else if is_mysql
2300                    || matches!(
2301                        dialect,
2302                        Some(Dialect::Postgres)
2303                            | Some(Dialect::DuckDb)
2304                            | Some(Dialect::Sqlite)
2305                            | Some(Dialect::Redshift)
2306                    )
2307                {
2308                    self.write_keyword("NOW()");
2309                } else {
2310                    self.write_keyword("CURRENT_TIMESTAMP()");
2311                }
2312            }
2313            TypedFunction::StrToTime { expr, format } => {
2314                if is_mysql {
2315                    self.write_keyword("STR_TO_DATE(");
2316                } else if is_bigquery {
2317                    self.write_keyword("PARSE_TIMESTAMP(");
2318                } else {
2319                    self.write_keyword("TO_TIMESTAMP(");
2320                }
2321                self.gen_expr(expr);
2322                self.write(", ");
2323                self.gen_expr(format);
2324                self.write(")");
2325            }
2326            TypedFunction::TimeToStr { expr, format } => {
2327                if is_mysql || is_hive_family {
2328                    self.write_keyword("DATE_FORMAT(");
2329                } else if is_bigquery {
2330                    self.write_keyword("FORMAT_TIMESTAMP(");
2331                } else if is_tsql {
2332                    self.write_keyword("FORMAT(");
2333                } else {
2334                    self.write_keyword("TO_CHAR(");
2335                }
2336                self.gen_expr(expr);
2337                self.write(", ");
2338                self.gen_expr(format);
2339                self.write(")");
2340            }
2341            TypedFunction::TsOrDsToDate { expr } => {
2342                if is_mysql {
2343                    self.write_keyword("DATE(");
2344                    self.gen_expr(expr);
2345                    self.write(")");
2346                } else {
2347                    self.write_keyword("CAST(");
2348                    self.gen_expr(expr);
2349                    self.write(" ");
2350                    self.write_keyword("AS DATE)");
2351                }
2352            }
2353            TypedFunction::Year { expr } => {
2354                if is_tsql {
2355                    self.write_keyword("YEAR(");
2356                    self.gen_expr(expr);
2357                    self.write(")");
2358                } else {
2359                    self.write_keyword("EXTRACT(YEAR FROM ");
2360                    self.gen_expr(expr);
2361                    self.write(")");
2362                }
2363            }
2364            TypedFunction::Month { expr } => {
2365                if is_tsql {
2366                    self.write_keyword("MONTH(");
2367                    self.gen_expr(expr);
2368                    self.write(")");
2369                } else {
2370                    self.write_keyword("EXTRACT(MONTH FROM ");
2371                    self.gen_expr(expr);
2372                    self.write(")");
2373                }
2374            }
2375            TypedFunction::Day { expr } => {
2376                if is_tsql {
2377                    self.write_keyword("DAY(");
2378                    self.gen_expr(expr);
2379                    self.write(")");
2380                } else {
2381                    self.write_keyword("EXTRACT(DAY FROM ");
2382                    self.gen_expr(expr);
2383                    self.write(")");
2384                }
2385            }
2386
2387            // ── String ─────────────────────────────────────────────────
2388            TypedFunction::Trim {
2389                expr,
2390                trim_type,
2391                trim_chars,
2392            } => {
2393                self.write_keyword("TRIM(");
2394                match trim_type {
2395                    TrimType::Leading => self.write_keyword("LEADING "),
2396                    TrimType::Trailing => self.write_keyword("TRAILING "),
2397                    TrimType::Both => {} // BOTH is default
2398                }
2399                if let Some(chars) = trim_chars {
2400                    self.gen_expr(chars);
2401                    self.write(" ");
2402                    self.write_keyword("FROM ");
2403                }
2404                self.gen_expr(expr);
2405                self.write(")");
2406            }
2407            TypedFunction::Substring {
2408                expr,
2409                start,
2410                length,
2411            } => {
2412                let name = if is_oracle
2413                    || is_hive_family
2414                    || is_mysql
2415                    || matches!(
2416                        dialect,
2417                        Some(Dialect::Sqlite)
2418                            | Some(Dialect::Doris)
2419                            | Some(Dialect::SingleStore)
2420                            | Some(Dialect::StarRocks)
2421                    ) {
2422                    "SUBSTR"
2423                } else {
2424                    "SUBSTRING"
2425                };
2426                self.write_keyword(name);
2427                self.write("(");
2428                self.gen_expr(expr);
2429                self.write(", ");
2430                self.gen_expr(start);
2431                if let Some(l) = length {
2432                    self.write(", ");
2433                    self.gen_expr(l);
2434                }
2435                self.write(")");
2436            }
2437            TypedFunction::Upper { expr } => {
2438                self.write_keyword("UPPER(");
2439                self.gen_expr(expr);
2440                self.write(")");
2441            }
2442            TypedFunction::Lower { expr } => {
2443                self.write_keyword("LOWER(");
2444                self.gen_expr(expr);
2445                self.write(")");
2446            }
2447            TypedFunction::RegexpLike {
2448                expr,
2449                pattern,
2450                flags,
2451            } => {
2452                self.write_keyword("REGEXP_LIKE(");
2453                self.gen_expr(expr);
2454                self.write(", ");
2455                self.gen_expr(pattern);
2456                if let Some(f) = flags {
2457                    self.write(", ");
2458                    self.gen_expr(f);
2459                }
2460                self.write(")");
2461            }
2462            TypedFunction::RegexpExtract {
2463                expr,
2464                pattern,
2465                group_index,
2466            } => {
2467                if is_bigquery || is_hive_family {
2468                    self.write_keyword("REGEXP_EXTRACT(");
2469                } else {
2470                    self.write_keyword("REGEXP_SUBSTR(");
2471                }
2472                self.gen_expr(expr);
2473                self.write(", ");
2474                self.gen_expr(pattern);
2475                if let Some(g) = group_index {
2476                    self.write(", ");
2477                    self.gen_expr(g);
2478                }
2479                self.write(")");
2480            }
2481            TypedFunction::RegexpReplace {
2482                expr,
2483                pattern,
2484                replacement,
2485                flags,
2486            } => {
2487                self.write_keyword("REGEXP_REPLACE(");
2488                self.gen_expr(expr);
2489                self.write(", ");
2490                self.gen_expr(pattern);
2491                self.write(", ");
2492                self.gen_expr(replacement);
2493                if let Some(f) = flags {
2494                    self.write(", ");
2495                    self.gen_expr(f);
2496                }
2497                self.write(")");
2498            }
2499            TypedFunction::ConcatWs { separator, exprs } => {
2500                self.write_keyword("CONCAT_WS(");
2501                self.gen_expr(separator);
2502                for e in exprs {
2503                    self.write(", ");
2504                    self.gen_expr(e);
2505                }
2506                self.write(")");
2507            }
2508            TypedFunction::Split { expr, delimiter } => {
2509                if is_tsql {
2510                    self.write_keyword("STRING_SPLIT(");
2511                } else {
2512                    self.write_keyword("SPLIT(");
2513                }
2514                self.gen_expr(expr);
2515                self.write(", ");
2516                self.gen_expr(delimiter);
2517                self.write(")");
2518            }
2519            TypedFunction::Initcap { expr } => {
2520                self.write_keyword("INITCAP(");
2521                self.gen_expr(expr);
2522                self.write(")");
2523            }
2524            TypedFunction::Length { expr } => {
2525                let name = if is_tsql || is_bigquery || is_snowflake {
2526                    "LEN"
2527                } else {
2528                    "LENGTH"
2529                };
2530                self.write_keyword(name);
2531                self.write("(");
2532                self.gen_expr(expr);
2533                self.write(")");
2534            }
2535            TypedFunction::Replace { expr, from, to } => {
2536                self.write_keyword("REPLACE(");
2537                self.gen_expr(expr);
2538                self.write(", ");
2539                self.gen_expr(from);
2540                self.write(", ");
2541                self.gen_expr(to);
2542                self.write(")");
2543            }
2544            TypedFunction::Reverse { expr } => {
2545                self.write_keyword("REVERSE(");
2546                self.gen_expr(expr);
2547                self.write(")");
2548            }
2549            TypedFunction::Left { expr, n } => {
2550                self.write_keyword("LEFT(");
2551                self.gen_expr(expr);
2552                self.write(", ");
2553                self.gen_expr(n);
2554                self.write(")");
2555            }
2556            TypedFunction::Right { expr, n } => {
2557                self.write_keyword("RIGHT(");
2558                self.gen_expr(expr);
2559                self.write(", ");
2560                self.gen_expr(n);
2561                self.write(")");
2562            }
2563            TypedFunction::Lpad { expr, length, pad } => {
2564                self.write_keyword("LPAD(");
2565                self.gen_expr(expr);
2566                self.write(", ");
2567                self.gen_expr(length);
2568                if let Some(p) = pad {
2569                    self.write(", ");
2570                    self.gen_expr(p);
2571                }
2572                self.write(")");
2573            }
2574            TypedFunction::Rpad { expr, length, pad } => {
2575                self.write_keyword("RPAD(");
2576                self.gen_expr(expr);
2577                self.write(", ");
2578                self.gen_expr(length);
2579                if let Some(p) = pad {
2580                    self.write(", ");
2581                    self.gen_expr(p);
2582                }
2583                self.write(")");
2584            }
2585
2586            // ── Aggregate ──────────────────────────────────────────────
2587            TypedFunction::Count { expr, distinct } => {
2588                self.write_keyword("COUNT(");
2589                if *distinct {
2590                    self.write_keyword("DISTINCT ");
2591                }
2592                self.gen_expr(expr);
2593                self.write(")");
2594            }
2595            TypedFunction::Sum { expr, distinct } => {
2596                self.write_keyword("SUM(");
2597                if *distinct {
2598                    self.write_keyword("DISTINCT ");
2599                }
2600                self.gen_expr(expr);
2601                self.write(")");
2602            }
2603            TypedFunction::Avg { expr, distinct } => {
2604                self.write_keyword("AVG(");
2605                if *distinct {
2606                    self.write_keyword("DISTINCT ");
2607                }
2608                self.gen_expr(expr);
2609                self.write(")");
2610            }
2611            TypedFunction::Min { expr } => {
2612                self.write_keyword("MIN(");
2613                self.gen_expr(expr);
2614                self.write(")");
2615            }
2616            TypedFunction::Max { expr } => {
2617                self.write_keyword("MAX(");
2618                self.gen_expr(expr);
2619                self.write(")");
2620            }
2621            TypedFunction::ArrayAgg { expr, distinct } => {
2622                let name = if matches!(dialect, Some(Dialect::DuckDb)) {
2623                    "LIST"
2624                } else if is_hive_family {
2625                    "COLLECT_LIST"
2626                } else {
2627                    "ARRAY_AGG"
2628                };
2629                self.write_keyword(name);
2630                self.write("(");
2631                if *distinct {
2632                    self.write_keyword("DISTINCT ");
2633                }
2634                self.gen_expr(expr);
2635                self.write(")");
2636            }
2637            TypedFunction::ApproxDistinct { expr } => {
2638                let name = if is_hive_family
2639                    || matches!(
2640                        dialect,
2641                        Some(Dialect::Presto) | Some(Dialect::Trino) | Some(Dialect::Athena)
2642                    ) {
2643                    "APPROX_DISTINCT"
2644                } else {
2645                    "APPROX_COUNT_DISTINCT"
2646                };
2647                self.write_keyword(name);
2648                self.write("(");
2649                self.gen_expr(expr);
2650                self.write(")");
2651            }
2652            TypedFunction::Variance { expr } => {
2653                let name = if is_tsql || is_oracle {
2654                    "VAR"
2655                } else {
2656                    "VARIANCE"
2657                };
2658                self.write_keyword(name);
2659                self.write("(");
2660                self.gen_expr(expr);
2661                self.write(")");
2662            }
2663            TypedFunction::Stddev { expr } => {
2664                self.write_keyword("STDDEV(");
2665                self.gen_expr(expr);
2666                self.write(")");
2667            }
2668            TypedFunction::GroupConcat {
2669                exprs,
2670                separator,
2671                order_by,
2672                distinct,
2673            } => {
2674                self.gen_group_concat(exprs, separator.as_deref(), order_by, *distinct);
2675            }
2676
2677            // ── Array ──────────────────────────────────────────────────
2678            TypedFunction::ArrayConcat { arrays } => {
2679                let name = if matches!(
2680                    dialect,
2681                    Some(Dialect::Postgres) | Some(Dialect::Redshift) | Some(Dialect::DuckDb)
2682                ) {
2683                    "ARRAY_CAT"
2684                } else {
2685                    "ARRAY_CONCAT"
2686                };
2687                self.write_keyword(name);
2688                self.write("(");
2689                self.gen_expr_list(arrays);
2690                self.write(")");
2691            }
2692            TypedFunction::ArrayContains { array, element } => {
2693                self.write_keyword("ARRAY_CONTAINS(");
2694                self.gen_expr(array);
2695                self.write(", ");
2696                self.gen_expr(element);
2697                self.write(")");
2698            }
2699            TypedFunction::ArraySize { expr } => {
2700                let name = if matches!(dialect, Some(Dialect::Postgres) | Some(Dialect::Redshift)) {
2701                    "ARRAY_LENGTH"
2702                } else if is_hive_family {
2703                    "SIZE"
2704                } else {
2705                    "ARRAY_SIZE"
2706                };
2707                self.write_keyword(name);
2708                self.write("(");
2709                self.gen_expr(expr);
2710                self.write(")");
2711            }
2712            TypedFunction::Explode { expr } => {
2713                self.write_keyword("EXPLODE(");
2714                self.gen_expr(expr);
2715                self.write(")");
2716            }
2717            TypedFunction::GenerateSeries { start, stop, step } => {
2718                self.write_keyword("GENERATE_SERIES(");
2719                self.gen_expr(start);
2720                self.write(", ");
2721                self.gen_expr(stop);
2722                if let Some(s) = step {
2723                    self.write(", ");
2724                    self.gen_expr(s);
2725                }
2726                self.write(")");
2727            }
2728            TypedFunction::Flatten { expr } => {
2729                self.write_keyword("FLATTEN(");
2730                self.gen_expr(expr);
2731                self.write(")");
2732            }
2733
2734            // ── JSON ───────────────────────────────────────────────────
2735            TypedFunction::JSONExtract { expr, path } => {
2736                if is_tsql {
2737                    self.write_keyword("JSON_VALUE(");
2738                } else {
2739                    self.write_keyword("JSON_EXTRACT(");
2740                }
2741                self.gen_expr(expr);
2742                self.write(", ");
2743                self.gen_expr(path);
2744                self.write(")");
2745            }
2746            TypedFunction::JSONExtractScalar { expr, path } => {
2747                if is_bigquery {
2748                    self.write_keyword("JSON_EXTRACT_SCALAR(");
2749                } else if is_tsql {
2750                    self.write_keyword("JSON_VALUE(");
2751                } else {
2752                    self.write_keyword("JSON_EXTRACT_SCALAR(");
2753                }
2754                self.gen_expr(expr);
2755                self.write(", ");
2756                self.gen_expr(path);
2757                self.write(")");
2758            }
2759            TypedFunction::ParseJSON { expr } => {
2760                if is_snowflake {
2761                    self.write_keyword("PARSE_JSON(");
2762                } else if is_bigquery {
2763                    self.write_keyword("JSON_PARSE(");
2764                } else {
2765                    self.write_keyword("PARSE_JSON(");
2766                }
2767                self.gen_expr(expr);
2768                self.write(")");
2769            }
2770            TypedFunction::JSONFormat { expr } => {
2771                if is_bigquery {
2772                    self.write_keyword("TO_JSON_STRING(");
2773                } else {
2774                    self.write_keyword("JSON_FORMAT(");
2775                }
2776                self.gen_expr(expr);
2777                self.write(")");
2778            }
2779
2780            // ── Window ─────────────────────────────────────────────────
2781            TypedFunction::RowNumber => self.write_keyword("ROW_NUMBER()"),
2782            TypedFunction::Rank => self.write_keyword("RANK()"),
2783            TypedFunction::DenseRank => self.write_keyword("DENSE_RANK()"),
2784            TypedFunction::NTile { n } => {
2785                self.write_keyword("NTILE(");
2786                self.gen_expr(n);
2787                self.write(")");
2788            }
2789            TypedFunction::Lead {
2790                expr,
2791                offset,
2792                default,
2793            } => {
2794                self.write_keyword("LEAD(");
2795                self.gen_expr(expr);
2796                if let Some(o) = offset {
2797                    self.write(", ");
2798                    self.gen_expr(o);
2799                }
2800                if let Some(d) = default {
2801                    self.write(", ");
2802                    self.gen_expr(d);
2803                }
2804                self.write(")");
2805            }
2806            TypedFunction::Lag {
2807                expr,
2808                offset,
2809                default,
2810            } => {
2811                self.write_keyword("LAG(");
2812                self.gen_expr(expr);
2813                if let Some(o) = offset {
2814                    self.write(", ");
2815                    self.gen_expr(o);
2816                }
2817                if let Some(d) = default {
2818                    self.write(", ");
2819                    self.gen_expr(d);
2820                }
2821                self.write(")");
2822            }
2823            TypedFunction::FirstValue { expr } => {
2824                self.write_keyword("FIRST_VALUE(");
2825                self.gen_expr(expr);
2826                self.write(")");
2827            }
2828            TypedFunction::LastValue { expr } => {
2829                self.write_keyword("LAST_VALUE(");
2830                self.gen_expr(expr);
2831                self.write(")");
2832            }
2833
2834            // ── Math ───────────────────────────────────────────────────
2835            TypedFunction::Abs { expr } => {
2836                self.write_keyword("ABS(");
2837                self.gen_expr(expr);
2838                self.write(")");
2839            }
2840            TypedFunction::Ceil { expr } => {
2841                let name = if is_tsql { "CEILING" } else { "CEIL" };
2842                self.write_keyword(name);
2843                self.write("(");
2844                self.gen_expr(expr);
2845                self.write(")");
2846            }
2847            TypedFunction::Floor { expr } => {
2848                self.write_keyword("FLOOR(");
2849                self.gen_expr(expr);
2850                self.write(")");
2851            }
2852            TypedFunction::Round { expr, decimals } => {
2853                self.write_keyword("ROUND(");
2854                self.gen_expr(expr);
2855                if let Some(d) = decimals {
2856                    self.write(", ");
2857                    self.gen_expr(d);
2858                }
2859                self.write(")");
2860            }
2861            TypedFunction::Log { expr, base } => {
2862                if let Some(b) = base {
2863                    self.write_keyword("LOG(");
2864                    if matches!(dialect, Some(Dialect::Postgres) | Some(Dialect::DuckDb)) {
2865                        // Postgres: LOG(base, expr)
2866                        self.gen_expr(b);
2867                        self.write(", ");
2868                        self.gen_expr(expr);
2869                    } else {
2870                        // Most: LOG(expr, base)
2871                        self.gen_expr(expr);
2872                        self.write(", ");
2873                        self.gen_expr(b);
2874                    }
2875                    self.write(")");
2876                } else {
2877                    // LOG(expr) — ln in Postgres, log10 in most others
2878                    self.write_keyword("LOG(");
2879                    self.gen_expr(expr);
2880                    self.write(")");
2881                }
2882            }
2883            TypedFunction::Ln { expr } => {
2884                self.write_keyword("LN(");
2885                self.gen_expr(expr);
2886                self.write(")");
2887            }
2888            TypedFunction::Pow { base, exponent } => {
2889                let name = if is_tsql || is_oracle { "POWER" } else { "POW" };
2890                self.write_keyword(name);
2891                self.write("(");
2892                self.gen_expr(base);
2893                self.write(", ");
2894                self.gen_expr(exponent);
2895                self.write(")");
2896            }
2897            TypedFunction::Sqrt { expr } => {
2898                self.write_keyword("SQRT(");
2899                self.gen_expr(expr);
2900                self.write(")");
2901            }
2902            TypedFunction::Greatest { exprs } => {
2903                self.write_keyword("GREATEST(");
2904                self.gen_expr_list(exprs);
2905                self.write(")");
2906            }
2907            TypedFunction::Least { exprs } => {
2908                self.write_keyword("LEAST(");
2909                self.gen_expr_list(exprs);
2910                self.write(")");
2911            }
2912            TypedFunction::Mod { left, right } => {
2913                self.write_keyword("MOD(");
2914                self.gen_expr(left);
2915                self.write(", ");
2916                self.gen_expr(right);
2917                self.write(")");
2918            }
2919
2920            // ── Conversion ─────────────────────────────────────────────
2921            TypedFunction::Hex { expr } => {
2922                let name = if matches!(
2923                    dialect,
2924                    Some(Dialect::Presto) | Some(Dialect::Trino) | Some(Dialect::Athena)
2925                ) {
2926                    "TO_HEX"
2927                } else {
2928                    "HEX"
2929                };
2930                self.write_keyword(name);
2931                self.write("(");
2932                self.gen_expr(expr);
2933                self.write(")");
2934            }
2935            TypedFunction::Unhex { expr } => {
2936                let name = if matches!(
2937                    dialect,
2938                    Some(Dialect::Presto) | Some(Dialect::Trino) | Some(Dialect::Athena)
2939                ) {
2940                    "FROM_HEX"
2941                } else {
2942                    "UNHEX"
2943                };
2944                self.write_keyword(name);
2945                self.write("(");
2946                self.gen_expr(expr);
2947                self.write(")");
2948            }
2949            TypedFunction::Md5 { expr } => {
2950                self.write_keyword("MD5(");
2951                self.gen_expr(expr);
2952                self.write(")");
2953            }
2954            TypedFunction::Sha { expr } => {
2955                let name = if is_mysql { "SHA1" } else { "SHA" };
2956                self.write_keyword(name);
2957                self.write("(");
2958                self.gen_expr(expr);
2959                self.write(")");
2960            }
2961            TypedFunction::Sha2 { expr, bit_length } => {
2962                self.write_keyword("SHA2(");
2963                self.gen_expr(expr);
2964                self.write(", ");
2965                self.gen_expr(bit_length);
2966                self.write(")");
2967            }
2968        }
2969    }
2970
2971    /// Emit a `GROUP_CONCAT` / `STRING_AGG` / `LISTAGG` expression
2972    /// appropriately for the active dialect.
2973    fn gen_group_concat(
2974        &mut self,
2975        exprs: &[Expr],
2976        separator: Option<&Expr>,
2977        order_by: &[OrderByItem],
2978        distinct: bool,
2979    ) {
2980        let dialect = self.dialect;
2981        let is_sqlite = matches!(dialect, Some(Dialect::Sqlite));
2982        let is_string_agg = matches!(
2983            dialect,
2984            Some(Dialect::Postgres)
2985                | Some(Dialect::Redshift)
2986                | Some(Dialect::BigQuery)
2987                | Some(Dialect::Tsql)
2988                | Some(Dialect::Fabric)
2989                | Some(Dialect::DuckDb)
2990        );
2991        let is_listagg = matches!(dialect, Some(Dialect::Oracle) | Some(Dialect::Snowflake));
2992
2993        if is_string_agg {
2994            // STRING_AGG(expr, sep [ORDER BY ...])
2995            self.write_keyword("STRING_AGG(");
2996            if distinct {
2997                self.write_keyword("DISTINCT ");
2998            }
2999            self.gen_group_concat_exprs(exprs);
3000            self.write(", ");
3001            match separator {
3002                Some(s) => self.gen_expr(s),
3003                None => self.write("','"),
3004            }
3005            if !order_by.is_empty() {
3006                self.write(" ");
3007                self.gen_group_concat_order_by(order_by);
3008            }
3009            self.write(")");
3010        } else if is_listagg {
3011            // LISTAGG(expr, sep) WITHIN GROUP (ORDER BY ...)
3012            self.write_keyword("LISTAGG(");
3013            if distinct {
3014                self.write_keyword("DISTINCT ");
3015            }
3016            self.gen_group_concat_exprs(exprs);
3017            self.write(", ");
3018            match separator {
3019                Some(s) => self.gen_expr(s),
3020                None => self.write("','"),
3021            }
3022            self.write(")");
3023            if !order_by.is_empty() {
3024                self.write(" ");
3025                self.write_keyword("WITHIN GROUP");
3026                self.write(" (");
3027                self.gen_group_concat_order_by(order_by);
3028                self.write(")");
3029            }
3030        } else if is_sqlite {
3031            // SQLite: GROUP_CONCAT(expr[, sep]). No DISTINCT/ORDER BY support;
3032            // they are dropped on output since the target dialect lacks them.
3033            self.write_keyword("GROUP_CONCAT(");
3034            self.gen_group_concat_exprs(exprs);
3035            if let Some(s) = separator {
3036                self.write(", ");
3037                self.gen_expr(s);
3038            }
3039            self.write(")");
3040        } else {
3041            // MySQL family (and default): full GROUP_CONCAT grammar.
3042            self.write_keyword("GROUP_CONCAT(");
3043            if distinct {
3044                self.write_keyword("DISTINCT ");
3045            }
3046            self.gen_group_concat_exprs(exprs);
3047            if !order_by.is_empty() {
3048                self.write(" ");
3049                self.gen_group_concat_order_by(order_by);
3050            }
3051            if let Some(s) = separator {
3052                self.write(" ");
3053                self.write_keyword("SEPARATOR");
3054                self.write(" ");
3055                self.gen_expr(s);
3056            }
3057            self.write(")");
3058        }
3059    }
3060
3061    fn gen_group_concat_exprs(&mut self, exprs: &[Expr]) {
3062        for (i, e) in exprs.iter().enumerate() {
3063            if i > 0 {
3064                self.write(", ");
3065            }
3066            self.gen_expr(e);
3067        }
3068    }
3069
3070    fn gen_group_concat_order_by(&mut self, order_by: &[OrderByItem]) {
3071        self.write_keyword("ORDER BY");
3072        self.write(" ");
3073        for (i, item) in order_by.iter().enumerate() {
3074            if i > 0 {
3075                self.write(", ");
3076            }
3077            self.gen_expr(&item.expr);
3078            if !item.ascending {
3079                self.write(" ");
3080                self.write_keyword("DESC");
3081            }
3082            if let Some(nulls_first) = item.nulls_first {
3083                self.write(" ");
3084                self.write_keyword(if nulls_first {
3085                    "NULLS FIRST"
3086                } else {
3087                    "NULLS LAST"
3088                });
3089            }
3090        }
3091    }
3092}
3093
3094impl Default for Generator {
3095    fn default() -> Self {
3096        Self::new()
3097    }
3098}
3099
3100#[cfg(test)]
3101mod tests {
3102    use super::*;
3103    use crate::parser::Parser;
3104
3105    fn roundtrip(sql: &str) -> String {
3106        let stmt = Parser::new(sql).unwrap().parse_statement().unwrap();
3107        let mut g = Generator::new();
3108        g.generate(&stmt)
3109    }
3110
3111    #[test]
3112    fn test_select_roundtrip() {
3113        assert_eq!(roundtrip("SELECT a, b FROM t"), "SELECT a, b FROM t");
3114    }
3115
3116    #[test]
3117    fn test_select_where() {
3118        assert_eq!(
3119            roundtrip("SELECT x FROM t WHERE x > 10"),
3120            "SELECT x FROM t WHERE x > 10"
3121        );
3122    }
3123
3124    #[test]
3125    fn test_select_wildcard() {
3126        assert_eq!(roundtrip("SELECT * FROM users"), "SELECT * FROM users");
3127    }
3128
3129    #[test]
3130    fn test_insert_values() {
3131        assert_eq!(
3132            roundtrip("INSERT INTO t (a, b) VALUES (1, 'hello')"),
3133            "INSERT INTO t (a, b) VALUES (1, 'hello')"
3134        );
3135    }
3136
3137    #[test]
3138    fn test_delete() {
3139        assert_eq!(
3140            roundtrip("DELETE FROM users WHERE id = 1"),
3141            "DELETE FROM users WHERE id = 1"
3142        );
3143    }
3144
3145    #[test]
3146    fn test_join() {
3147        assert_eq!(
3148            roundtrip("SELECT a.id, b.name FROM a INNER JOIN b ON a.id = b.a_id"),
3149            "SELECT a.id, b.name FROM a INNER JOIN b ON a.id = b.a_id"
3150        );
3151    }
3152
3153    #[test]
3154    fn test_create_table() {
3155        assert_eq!(
3156            roundtrip("CREATE TABLE users (id INT NOT NULL, name VARCHAR(255), email TEXT)"),
3157            "CREATE TABLE users (id INT NOT NULL, name VARCHAR(255), email TEXT)"
3158        );
3159    }
3160
3161    #[test]
3162    fn test_cte_roundtrip() {
3163        let sql = "WITH cte AS (SELECT 1 AS x) SELECT x FROM cte";
3164        assert_eq!(
3165            roundtrip(sql),
3166            "WITH cte AS (SELECT 1 AS x) SELECT x FROM cte"
3167        );
3168    }
3169
3170    #[test]
3171    fn test_union_roundtrip() {
3172        let sql = "SELECT 1 UNION ALL SELECT 2";
3173        assert_eq!(roundtrip(sql), "SELECT 1 UNION ALL SELECT 2");
3174    }
3175
3176    #[test]
3177    fn test_cast_roundtrip() {
3178        assert_eq!(
3179            roundtrip("SELECT CAST(x AS INT) FROM t"),
3180            "SELECT CAST(x AS INT) FROM t"
3181        );
3182    }
3183
3184    #[test]
3185    fn test_exists_roundtrip() {
3186        assert_eq!(
3187            roundtrip("SELECT * FROM t WHERE EXISTS (SELECT 1 FROM t2)"),
3188            "SELECT * FROM t WHERE EXISTS (SELECT 1 FROM t2)"
3189        );
3190    }
3191
3192    #[test]
3193    fn test_extract_roundtrip() {
3194        assert_eq!(
3195            roundtrip("SELECT EXTRACT(YEAR FROM created_at) FROM t"),
3196            "SELECT EXTRACT(YEAR FROM created_at) FROM t"
3197        );
3198    }
3199
3200    #[test]
3201    fn test_window_function_roundtrip() {
3202        assert_eq!(
3203            roundtrip("SELECT ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) FROM emp"),
3204            "SELECT ROW_NUMBER() OVER (PARTITION BY dept ORDER BY salary DESC) FROM emp"
3205        );
3206    }
3207
3208    #[test]
3209    fn test_subquery_from_roundtrip() {
3210        assert_eq!(
3211            roundtrip("SELECT * FROM (SELECT 1 AS x) AS sub"),
3212            "SELECT * FROM (SELECT 1 AS x) AS sub"
3213        );
3214    }
3215
3216    #[test]
3217    fn test_in_subquery_roundtrip() {
3218        assert_eq!(
3219            roundtrip("SELECT * FROM t WHERE id IN (SELECT id FROM t2)"),
3220            "SELECT * FROM t WHERE id IN (SELECT id FROM t2)"
3221        );
3222    }
3223
3224    // ═══════════════════════════════════════════════════════════════
3225    // Pretty-print tests
3226    // ═══════════════════════════════════════════════════════════════
3227
3228    fn pretty_print(sql: &str) -> String {
3229        let stmt = Parser::new(sql).unwrap().parse_statement().unwrap();
3230        let mut g = Generator::pretty();
3231        g.generate(&stmt)
3232    }
3233
3234    #[test]
3235    fn test_pretty_simple_select() {
3236        assert_eq!(
3237            pretty_print("SELECT a, b, c FROM t"),
3238            "SELECT\n  a,\n  b,\n  c\nFROM\n  t"
3239        );
3240    }
3241
3242    #[test]
3243    fn test_pretty_select_where() {
3244        assert_eq!(
3245            pretty_print("SELECT a FROM t WHERE a > 1"),
3246            "SELECT\n  a\nFROM\n  t\nWHERE\n  a > 1"
3247        );
3248    }
3249
3250    #[test]
3251    fn test_pretty_select_group_by_having() {
3252        assert_eq!(
3253            pretty_print("SELECT a, COUNT(*) FROM t GROUP BY a HAVING COUNT(*) > 1"),
3254            "SELECT\n  a,\n  COUNT(*)\nFROM\n  t\nGROUP BY\n  a\nHAVING\n  COUNT(*) > 1"
3255        );
3256    }
3257
3258    #[test]
3259    fn test_pretty_select_order_by_limit() {
3260        assert_eq!(
3261            pretty_print("SELECT a FROM t ORDER BY a DESC LIMIT 10"),
3262            "SELECT\n  a\nFROM\n  t\nORDER BY\n  a DESC\nLIMIT 10"
3263        );
3264    }
3265
3266    #[test]
3267    fn test_pretty_join() {
3268        assert_eq!(
3269            pretty_print("SELECT a.id, b.name FROM a INNER JOIN b ON a.id = b.a_id"),
3270            "SELECT\n  a.id,\n  b.name\nFROM\n  a\nINNER JOIN\n  b\n  ON a.id = b.a_id"
3271        );
3272    }
3273
3274    #[test]
3275    fn test_pretty_cte() {
3276        assert_eq!(
3277            pretty_print("WITH cte AS (SELECT 1 AS x) SELECT x FROM cte"),
3278            "WITH cte AS (\n  SELECT\n    1 AS x\n)\nSELECT\n  x\nFROM\n  cte"
3279        );
3280    }
3281
3282    #[test]
3283    fn test_pretty_union() {
3284        assert_eq!(
3285            pretty_print("SELECT 1 UNION ALL SELECT 2"),
3286            "SELECT\n  1\nUNION ALL\nSELECT\n  2"
3287        );
3288    }
3289
3290    #[test]
3291    fn test_pretty_insert() {
3292        assert_eq!(
3293            pretty_print("INSERT INTO t (a, b) VALUES (1, 'hello'), (2, 'world')"),
3294            "INSERT INTO t (a, b)\nVALUES\n  (1, 'hello'),\n  (2, 'world')"
3295        );
3296    }
3297
3298    #[test]
3299    fn test_pretty_update() {
3300        assert_eq!(
3301            pretty_print("UPDATE t SET a = 1, b = 2 WHERE c = 3"),
3302            "UPDATE t\nSET\n  a = 1,\n  b = 2\nWHERE\n  c = 3"
3303        );
3304    }
3305
3306    #[test]
3307    fn test_pretty_delete() {
3308        assert_eq!(
3309            pretty_print("DELETE FROM t WHERE id = 1"),
3310            "DELETE FROM t\nWHERE\n  id = 1"
3311        );
3312    }
3313
3314    #[test]
3315    fn test_pretty_create_table() {
3316        assert_eq!(
3317            pretty_print("CREATE TABLE t (id INT NOT NULL, name VARCHAR(255), email TEXT)"),
3318            "CREATE TABLE t (\n  id INT NOT NULL,\n  name VARCHAR(255),\n  email TEXT\n)"
3319        );
3320    }
3321
3322    #[test]
3323    fn test_pretty_complex_query() {
3324        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";
3325        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";
3326        assert_eq!(pretty_print(sql), expected);
3327    }
3328
3329    #[test]
3330    fn test_pretty_select_distinct() {
3331        assert_eq!(
3332            pretty_print("SELECT DISTINCT a, b FROM t"),
3333            "SELECT DISTINCT\n  a,\n  b\nFROM\n  t"
3334        );
3335    }
3336}