From 5539dfac01bcde2bfa3ae30f0e5e1f57d33e6ba5 Mon Sep 17 00:00:00 2001 From: Sean Register Date: Thu, 7 May 2026 10:55:48 +1200 Subject: [PATCH] Emit comma between CTEs in non-compact WITH formatting format_with_clause_left closed every CTE with a bare ")", which produced invalid SQL whenever there were two or more CTEs in a style that does not use compact CTEs (e.g. dbt). The compact-CTE branch already inlined "), name AS (" correctly; the non-compact branch is now position-aware and emits ")," for every CTE except the last. Adds a minimal multi-CTE fixture for the dbt style as a regression test. --- src/formatter/select.rs | 7 ++++++- tests/fixtures/dbt/select_cte_multi.expected | 18 ++++++++++++++++++ tests/fixtures/dbt/select_cte_multi.sql | 1 + tests/fixtures_test.rs | 5 +++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/dbt/select_cte_multi.expected create mode 100644 tests/fixtures/dbt/select_cte_multi.sql diff --git a/src/formatter/select.rs b/src/formatter/select.rs index a02a4a4..1e88b02 100644 --- a/src/formatter/select.rs +++ b/src/formatter/select.rs @@ -1593,7 +1593,12 @@ impl<'a> Formatter<'a> { } if !self.config.compact_ctes { - lines.push(")".to_string()); + let is_last = i == ctes.len() - 1; + lines.push(if is_last { + ")".to_string() + } else { + "),".to_string() + }); } } diff --git a/tests/fixtures/dbt/select_cte_multi.expected b/tests/fixtures/dbt/select_cte_multi.expected new file mode 100644 index 0000000..3e2cdfd --- /dev/null +++ b/tests/fixtures/dbt/select_cte_multi.expected @@ -0,0 +1,18 @@ +with + +a as ( + + select 1 as x + +), +b as ( + + select 2 as y + +) + +select * + +from + a, + b; diff --git a/tests/fixtures/dbt/select_cte_multi.sql b/tests/fixtures/dbt/select_cte_multi.sql new file mode 100644 index 0000000..92483b4 --- /dev/null +++ b/tests/fixtures/dbt/select_cte_multi.sql @@ -0,0 +1 @@ +WITH a AS (SELECT 1 AS x), b AS (SELECT 2 AS y) SELECT * FROM a, b diff --git a/tests/fixtures_test.rs b/tests/fixtures_test.rs index 820fc6d..ae9231a 100644 --- a/tests/fixtures_test.rs +++ b/tests/fixtures_test.rs @@ -256,6 +256,11 @@ fn dbt_select_cte() { run_fixture(Style::Dbt, "select_cte"); } +#[test] +fn dbt_select_cte_multi() { + run_fixture(Style::Dbt, "select_cte_multi"); +} + // ── GitLab fixtures ───────────────────────────────────────────────────── #[test]