Skip to content

Commit a8233c2

Browse files
simen-strandclaude
andcommitted
Unwrap parenthesized single-column Redshift distkey
A parenthesized distkey such as `distkey = (col)` rendered as `DISTKEY((col))`, which doesn't match Redshift's documented `DISTKEY ( column_name )` syntax. Unwrap the parentheses the same way the sortkey handling does, and test the bare, quoted, string and parenthesized forms in model physical_properties. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Signed-off-by: Simen Strand <simen.strand@netcheck.de>
1 parent 663a0a2 commit a8233c2

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎sqlmesh/core/engine_adapter/redshift.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,9 @@ def _to_identifier_if_string(expression: exp.Expr) -> exp.Expr:
291291

292292
distkey = table_properties.get("DISTKEY")
293293
if distkey:
294-
properties.append(exp.DistKeyProperty(this=_to_identifier_if_string(distkey)))
294+
properties.append(
295+
exp.DistKeyProperty(this=_to_identifier_if_string(distkey.unnest()))
296+
)
295297

296298
sortkey = table_properties.get("SORTKEY")
297299
if sortkey:

‎tests/core/engine_adapter/test_redshift.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,44 @@ def test_create_table_physical_properties_from_model_definition(
198198
]
199199

200200

201+
@pytest.mark.parametrize(
202+
"distkey",
203+
['"id_file"', "id_file", "'id_file'", '("id_file")', "(id_file)"],
204+
)
205+
def test_create_table_physical_properties_distkey_from_model_definition(
206+
make_mocked_engine_adapter: t.Callable, distkey: str
207+
):
208+
adapter = make_mocked_engine_adapter(RedshiftEngineAdapter)
209+
model: SqlModel = t.cast(
210+
SqlModel,
211+
load_sql_based_model(
212+
d.parse(
213+
f"""
214+
MODEL (
215+
name test_schema.test_table,
216+
kind full,
217+
physical_properties (
218+
diststyle = key,
219+
distkey = {distkey}
220+
)
221+
);
222+
SELECT id_file::INT;
223+
"""
224+
)
225+
),
226+
)
227+
228+
adapter.create_table(
229+
model.name,
230+
target_columns_to_types=model.columns_to_types_or_raise,
231+
table_properties=model.physical_properties,
232+
)
233+
234+
assert to_sql_calls(adapter) == [
235+
'CREATE TABLE IF NOT EXISTS "test_schema"."test_table" ("id_file" INTEGER) DISTSTYLE KEY DISTKEY("id_file")',
236+
]
237+
238+
201239
def test_varchar_size_workaround(make_mocked_engine_adapter: t.Callable, mocker: MockerFixture):
202240
adapter = make_mocked_engine_adapter(RedshiftEngineAdapter)
203241

0 commit comments

Comments
 (0)