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