-
Notifications
You must be signed in to change notification settings - Fork 196
Add cluster_by in Snowflake dynamic table materialization config #1305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nazliander
wants to merge
2
commits into
dbt-labs:main
Choose a base branch
from
nazliander:feat/issue-706
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
dbt-snowflake/.changes/unreleased/Features-20250830-120802.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Add cluster by config in Snowflake for dynamic tables | ||
time: 2025-08-30T12:08:02.458481+02:00 | ||
custom: | ||
Author: nazliander | ||
Issue: "706" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
dbt-snowflake/tests/functional/adapter/test_cluster_by.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import pytest | ||
from dbt.tests.util import check_table_does_exist, run_dbt | ||
|
||
_SEED_CSV = """ | ||
id,first_name,last_name,email,product_id | ||
1,Jack,Hunter,jhunter0@foo.bar,1 | ||
2,Kathryn,Walker,kwalker1@foo.bar,1 | ||
3,Gerald,Ryan,gryan2@foo.bar,3 | ||
4,Jack,Hunter,jhunter1@foo.bar,4 | ||
5,Kathryn,Walker,kwalker2@foo.bar,5 | ||
6,Gerald,Ryan,gryan3@foo.bar,6 | ||
""".lstrip() | ||
|
||
|
||
class TestClusterBy: | ||
@pytest.fixture(scope="class") | ||
def seeds(self): | ||
return {"seed.csv": _SEED_CSV} | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self, dbt_profile_target): | ||
warehouse_name = dbt_profile_target["warehouse"] | ||
|
||
_DYNAMIC_TABLE_1_SQL = f""" | ||
{{{{ config(materialized='dynamic_table', snowflake_warehouse='{warehouse_name}', target_lag='1 minute') }}}} | ||
select * from {{{{ ref('seed') }}}} | ||
""".lstrip() | ||
|
||
_DYNAMIC_TABLE_2_SQL = f""" | ||
{{{{ config(materialized='dynamic_table', cluster_by=['last_name'], snowflake_warehouse='{warehouse_name}', target_lag='1 minute') }}}} | ||
select * from {{{{ ref('dynamic_table_1') }}}} | ||
""".lstrip() | ||
|
||
_DYNAMIC_TABLE_3_SQL = f""" | ||
{{{{ config(materialized='dynamic_table', cluster_by=['last_name', 'first_name'], snowflake_warehouse='{warehouse_name}', target_lag='1 minute') }}}} | ||
select | ||
last_name, | ||
first_name, | ||
count(*) as count | ||
from {{{{ ref('seed') }}}} | ||
group by 1, 2 | ||
""".lstrip() | ||
|
||
_DYNAMIC_TABLE_4_SQL = f""" | ||
{{{{ config(materialized='dynamic_table', cluster_by=['last_name', 'product_id % 3'], snowflake_warehouse='{warehouse_name}', target_lag='1 minute') }}}} | ||
select | ||
last_name, | ||
first_name, | ||
product_id, | ||
count(*) as count | ||
from {{{{ ref('seed') }}}} | ||
group by 1, 2, 3 | ||
""".lstrip() | ||
|
||
return { | ||
"dynamic_table_1.sql": _DYNAMIC_TABLE_1_SQL, | ||
"dynamic_table_2.sql": _DYNAMIC_TABLE_2_SQL, | ||
"dynamic_table_3.sql": _DYNAMIC_TABLE_3_SQL, | ||
"dynamic_table_4.sql": _DYNAMIC_TABLE_4_SQL, | ||
} | ||
|
||
def test_snowflake_dynamic_table_cluster_by(self, project): | ||
|
||
run_dbt(["seed"]) | ||
|
||
db_with_schema = f"{project.database}.{project.test_schema}" | ||
|
||
check_table_does_exist( | ||
project.adapter, f"{db_with_schema}.{self._available_models_in_setup()['seed_table']}" | ||
) | ||
|
||
run_dbt() | ||
|
||
# Check that all dynamic tables exist | ||
check_table_does_exist( | ||
project.adapter, | ||
f"{db_with_schema}.{self._available_models_in_setup()['dynamic_table_1']}", | ||
) | ||
check_table_does_exist( | ||
project.adapter, | ||
f"{db_with_schema}.{self._available_models_in_setup()['dynamic_table_2']}", | ||
) | ||
check_table_does_exist( | ||
project.adapter, | ||
f"{db_with_schema}.{self._available_models_in_setup()['dynamic_table_3']}", | ||
) | ||
check_table_does_exist( | ||
project.adapter, | ||
f"{db_with_schema}.{self._available_models_in_setup()['dynamic_table_4']}", | ||
) | ||
|
||
with project.adapter.connection_named("__test"): | ||
# Check if cluster_by is applied to dynamic_table_2 (should cluster by last_name) | ||
cluster_by = self._get_dynamic_table_ddl( | ||
project, self._available_models_in_setup()["dynamic_table_2"] | ||
) | ||
assert "CLUSTER BY (LAST_NAME)" in cluster_by.upper() | ||
|
||
# Check if cluster_by is applied to dynamic_table_3 (should cluster by last_name, first_name) | ||
cluster_by = self._get_dynamic_table_ddl( | ||
project, self._available_models_in_setup()["dynamic_table_3"] | ||
) | ||
assert "CLUSTER BY (LAST_NAME, FIRST_NAME)" in cluster_by.upper() | ||
|
||
# Check if cluster_by is applied to dynamic_table_4 (should cluster by last_name, product_id % 3) | ||
cluster_by = self._get_dynamic_table_ddl( | ||
project, self._available_models_in_setup()["dynamic_table_4"] | ||
) | ||
assert "CLUSTER BY (LAST_NAME, PRODUCT_ID % 3)" in cluster_by.upper() | ||
|
||
def _get_dynamic_table_ddl(self, project, table_name: str) -> str: | ||
ddl_query = f"SELECT GET_DDL('DYNAMIC_TABLE', '{project.database}.{project.test_schema}.{table_name}')" | ||
ddl = project.run_sql(ddl_query, fetch="one") | ||
return ddl[0] | ||
|
||
def _available_models_in_setup(self) -> dict[str, str]: | ||
return dict( | ||
seed_table="SEED", | ||
dynamic_table_1="DYNAMIC_TABLE_1", | ||
dynamic_table_2="DYNAMIC_TABLE_2", | ||
dynamic_table_3="DYNAMIC_TABLE_3", | ||
dynamic_table_4="DYNAMIC_TABLE_4", | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this import is causing issues and it doesn't appear used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am using on line 105 simply I was lazy to write a new parser or change the old one.
I guess I should have used a generic type, not a
|
🤦 I will try to solve it somewhere this week.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like should have been compatible with Python 3.9, I was using the wrong Union notation with
|
. I pushed a fixup commit now. At least locally I can see unit tests are passing. 👍