-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add runtime per-model warehouse config on snowflake models (#1358) #1788
Merged
beckjake
merged 2 commits into
dev/louisa-may-alcott
from
feature/warehouse-model-config
Sep 27, 2019
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
from typing import Mapping, Any, Optional | ||
|
||
from dbt.adapters.sql import SQLAdapter | ||
from dbt.adapters.snowflake import SnowflakeConnectionManager | ||
from dbt.adapters.snowflake import SnowflakeRelation | ||
from dbt.utils import filter_null_values | ||
from dbt.exceptions import RuntimeException | ||
|
||
|
||
class SnowflakeAdapter(SQLAdapter): | ||
|
@@ -10,7 +13,7 @@ class SnowflakeAdapter(SQLAdapter): | |
|
||
AdapterSpecificConfigs = frozenset( | ||
{"transient", "cluster_by", "automatic_clustering", "secure", | ||
"copy_grants"} | ||
"copy_grants", "warehouse"} | ||
) | ||
|
||
@classmethod | ||
|
@@ -40,3 +43,34 @@ def _make_match_kwargs(self, database, schema, identifier): | |
return filter_null_values( | ||
{"identifier": identifier, "schema": schema, "database": database} | ||
) | ||
|
||
def _get_warehouse(self) -> str: | ||
_, table = self.execute( | ||
'select current_warehouse() as warehouse', | ||
fetch=True | ||
) | ||
if len(table) == 0 or len(table[0]) == 0: | ||
# can this happen? | ||
raise RuntimeException( | ||
'Could not get current warehouse: no results' | ||
) | ||
return str(table[0][0]) | ||
|
||
def _use_warehouse(self, warehouse: str): | ||
"""Use the given warehouse. Quotes are never applied.""" | ||
self.execute('use warehouse {}'.format(warehouse)) | ||
|
||
def pre_model_hook(self, config: Mapping[str, Any]) -> Optional[str]: | ||
default_warehouse = self.config.credentials.warehouse | ||
warehouse = config.get('warehouse', default_warehouse) | ||
if warehouse == default_warehouse or warehouse is None: | ||
return None | ||
previous = self._get_warehouse() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like that this cost is only incurred if a non-default warehouse is specified! |
||
self._use_warehouse(warehouse) | ||
return previous | ||
|
||
def post_model_hook( | ||
self, config: Mapping[str, Any], context: Optional[str] | ||
) -> None: | ||
if context is not None: | ||
self._use_warehouse(context) |
2 changes: 2 additions & 0 deletions
2
test/integration/050_warehouse_test/models/expected_warehouse.sql
This file contains 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,2 @@ | ||
{{ config(materialized='table') }} | ||
select 'DBT_TEST_ALT' as warehouse |
2 changes: 2 additions & 0 deletions
2
test/integration/050_warehouse_test/models/invalid_warehouse.sql
This file contains 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,2 @@ | ||
{{ config(warehouse='DBT_TEST_DOES_NOT_EXIST') }} | ||
select current_warehouse() as warehouse |
2 changes: 2 additions & 0 deletions
2
test/integration/050_warehouse_test/models/override_warehouse.sql
This file contains 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,2 @@ | ||
{{ config(warehouse='DBT_TEST_ALT', materialized='table') }} | ||
select current_warehouse() as warehouse |
This file contains 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,28 @@ | ||
from test.integration.base import DBTIntegrationTest, use_profile | ||
import os | ||
|
||
|
||
class TestDebug(DBTIntegrationTest): | ||
@property | ||
def schema(self): | ||
return 'dbt_warehouse_050' | ||
|
||
@staticmethod | ||
def dir(value): | ||
return os.path.normpath(value) | ||
|
||
@property | ||
def models(self): | ||
return self.dir('models') | ||
|
||
@use_profile('snowflake') | ||
def test_snowflake_override_ok(self): | ||
self.run_dbt([ | ||
'run', | ||
'--models', 'override_warehouse', 'expected_warehouse', | ||
]) | ||
self.assertManyRelationsEqual([['OVERRIDE_WAREHOUSE'], ['EXPECTED_WAREHOUSE']]) | ||
|
||
@use_profile('snowflake') | ||
def test_snowflake_override_noexist(self): | ||
self.run_dbt(['run', '--models', 'invalid_warehouse'], expect_pass=False) |
This file contains 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
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.
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.
🙌 🙏