Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion sqlmesh/core/engine_adapter/redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,12 @@ def _to_identifier_if_string(expression: exp.Expr) -> exp.Expr:

sortkey = table_properties.get("SORTKEY")
if sortkey:
sortkey_expressions = sortkey.expressions if sortkey.expressions else [sortkey]
if isinstance(sortkey, (exp.Tuple, exp.Array)):
sortkey_expressions = sortkey.expressions
elif isinstance(sortkey, exp.Paren):
sortkey_expressions = [sortkey.unnest()]
else:
sortkey_expressions = [sortkey]
properties.append(
exp.SortKeyProperty(
this=[
Expand Down
19 changes: 15 additions & 4 deletions tests/core/engine_adapter/test_redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,33 @@ def test_create_table_physical_properties_with_string_columns(
]


@pytest.mark.parametrize(
"sortkey, expected_sortkey",
[
('"batch_time"', 'SORTKEY("batch_time")'),
("batch_time", 'SORTKEY("batch_time")'),
('("batch_time")', 'SORTKEY("batch_time")'),
("(batch_time)", 'SORTKEY("batch_time")'),
('("batch_time", "id_file")', 'SORTKEY("batch_time", "id_file")'),
('["batch_time", "id_file"]', 'SORTKEY("batch_time", "id_file")'),
],
)
def test_create_table_physical_properties_from_model_definition(
make_mocked_engine_adapter: t.Callable,
make_mocked_engine_adapter: t.Callable, sortkey: str, expected_sortkey: str
):
adapter = make_mocked_engine_adapter(RedshiftEngineAdapter)
model: SqlModel = t.cast(
SqlModel,
load_sql_based_model(
d.parse(
"""
f"""
MODEL (
name test_schema.test_table,
kind full,
physical_properties (
diststyle = key,
distkey = "id_file",
sortkey = "batch_time"
sortkey = {sortkey}
)
);
SELECT id_file::INT, batch_time::TIMESTAMP;
Expand All @@ -183,7 +194,7 @@ def test_create_table_physical_properties_from_model_definition(
)

assert to_sql_calls(adapter) == [
'CREATE TABLE IF NOT EXISTS "test_schema"."test_table" ("id_file" INTEGER, "batch_time" TIMESTAMP) DISTSTYLE KEY DISTKEY("id_file") SORTKEY("batch_time")',
f'CREATE TABLE IF NOT EXISTS "test_schema"."test_table" ("id_file" INTEGER, "batch_time" TIMESTAMP) DISTSTYLE KEY DISTKEY("id_file") {expected_sortkey}',
]


Expand Down
Loading