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