Skip to content

Commit 2a1523b

Browse files
committed
fix(clickhouse): unwrap parenthesized PRIMARY_KEY independent of ORDER_BY
The PRIMARY_KEY branch in _build_table_properties_exp tested the ORDER_BY value (ordered_by_raw) for exp.Paren instead of the primary_key value, a copy-paste leftover from the ORDER_BY block above it. As a result a single parenthesized PRIMARY_KEY such as PRIMARY_KEY = (a) was only unwrapped when ORDER_BY also happened to be a Paren; with a bare column, a tuple, or no ORDER_BY the parentheses were kept and the adapter emitted the malformed PRIMARY KEY ((a)). The generated primary key wrongly depended on the unrelated ORDER_BY shape. Check primary_key for exp.Paren so it is unwrapped regardless of ORDER_BY, and add regression cases pairing PRIMARY_KEY = (a) with a bare and a tuple ORDER_BY. Signed-off-by: Anas Khan <83116240+anxkhn@users.noreply.github.com>
1 parent b0bc176 commit 2a1523b

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

sqlmesh/core/engine_adapter/clickhouse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ def _build_table_properties_exp(
850850
primary_key_vals = []
851851
if isinstance(primary_key, (exp.Tuple, exp.Array)):
852852
primary_key_vals = primary_key.expressions
853-
if isinstance(ordered_by_raw, exp.Paren):
853+
if isinstance(primary_key, exp.Paren):
854854
primary_key_vals = [primary_key.this]
855855

856856
if not primary_key_vals:

tests/core/engine_adapter/test_clickhouse.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,20 @@ def build_properties_sql(storage_format="", order_by="", primary_key="", propert
305305
== "ENGINE=MergeTree ORDER BY (a) PRIMARY KEY (a)"
306306
)
307307

308+
# A parenthesized single-column PRIMARY_KEY must be unwrapped regardless of the
309+
# ORDER_BY shape. Previously the PRIMARY_KEY branch checked ORDER_BY's expression,
310+
# so pairing PRIMARY_KEY = (a) with a non-parenthesized ORDER_BY emitted the
311+
# malformed "PRIMARY KEY ((a))".
312+
assert (
313+
build_properties_sql(order_by="ORDER_BY = a,", primary_key="PRIMARY_KEY = (a)")
314+
== "ENGINE=MergeTree ORDER BY (a) PRIMARY KEY (a)"
315+
)
316+
317+
assert (
318+
build_properties_sql(order_by="ORDER_BY = (a, b),", primary_key="PRIMARY_KEY = (a)")
319+
== "ENGINE=MergeTree ORDER BY (a, b) PRIMARY KEY (a)"
320+
)
321+
308322
assert build_properties_sql(order_by="ORDER_BY = a + 1,") == "ENGINE=MergeTree ORDER BY (a + 1)"
309323

310324
assert (

0 commit comments

Comments
 (0)