-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add validate_sql method to base adapter with implementation for SQLAdapters #8001
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3a99352
Add dry_run method to base adapter with implementation for SQLAdapters
tlento d71a162
Switch dry_run implementation to be macro-based
tlento 3b74488
Improve error message for InvalidConnectionError in test_invalid_dry_…
tlento 32bf27a
Merge branch 'main' into CT-2681-dry-run-adapter-method
tlento 2b8592b
Merge branch 'main' into CT-2681-dry-run-adapter-method
tlento 83950d7
Rename dry_run to validate_sql
tlento 7d2a6f6
Rename macro and test files to validate_sql
tlento 58b82d6
Fix changelog entry
tlento 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 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 validate_sql method to BaseAdapter with implementation for SQLAdapter | ||
| time: 2023-06-29T17:57:12.599313-07:00 | ||
| custom: | ||
| Author: tlento | ||
| Issue: "7839" |
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
10 changes: 10 additions & 0 deletions
10
core/dbt/include/global_project/macros/adapters/validate_sql.sql
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,10 @@ | ||
| {% macro validate_sql(sql) -%} | ||
| {{ return(adapter.dispatch('validate_sql', 'dbt')(sql)) }} | ||
| {% endmacro %} | ||
|
|
||
| {% macro default__validate_sql(sql) -%} | ||
| {% call statement('validate_sql') -%} | ||
| explain {{ sql }} | ||
| {% endcall %} | ||
| {{ return(load_result('validate_sql')) }} | ||
| {% endmacro %} |
75 changes: 75 additions & 0 deletions
75
tests/adapter/dbt/tests/adapter/utils/test_validate_sql.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,75 @@ | ||
| from typing import Type | ||
|
|
||
| import pytest | ||
|
|
||
| from dbt.adapters.base.impl import BaseAdapter | ||
| from dbt.exceptions import DbtRuntimeError, InvalidConnectionError | ||
|
|
||
|
|
||
| class BaseDryRunMethod: | ||
| """Tests the behavior of the dry run method for the relevant adapters. | ||
|
|
||
| The valid and invalid SQL should work with most engines by default, but | ||
| both inputs can be overridden as needed for a given engine to get the correct | ||
| behavior. | ||
|
|
||
| The base method is meant to throw the appropriate custom exception when dry_run | ||
| fails. | ||
| """ | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def valid_sql(self) -> str: | ||
| """Returns a valid statement for issuing as a dry run query. | ||
|
|
||
| Ideally this would be checkable for non-execution. For example, we could use a | ||
| CREATE TABLE statement with an assertion that no table was created. However, | ||
| for most adapter types this is unnecessary - the EXPLAIN keyword has exactly the | ||
| behavior we want, and here we are essentially testing to make sure it is | ||
| supported. As such, we return a simple SELECT query, and leave it to | ||
| engine-specific test overrides to specify more detailed behavior as appropriate. | ||
| """ | ||
|
|
||
| return "select 1" | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def invalid_sql(self) -> str: | ||
| """Returns an invalid statement for issuing a bad dry run query.""" | ||
|
|
||
| return "Let's run some invalid SQL and see if we get an error!" | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def expected_exception(self) -> Type[Exception]: | ||
| """Returns the Exception type thrown by a failed query. | ||
|
|
||
| Defaults to dbt.exceptions.DbtRuntimeError because that is the most common | ||
| base exception for adapters to throw.""" | ||
| return DbtRuntimeError | ||
|
|
||
| def test_valid_dry_run(self, adapter: BaseAdapter, valid_sql: str) -> None: | ||
| """Executes a dry run query on valid SQL. No news is good news.""" | ||
| with adapter.connection_named("test_valid_sql_validation"): | ||
| adapter.validate_sql(valid_sql) | ||
|
|
||
| def test_invalid_dry_run( | ||
| self, | ||
| adapter: BaseAdapter, | ||
| invalid_sql: str, | ||
| expected_exception: Type[Exception], | ||
| ) -> None: | ||
| """Executes a dry run query on invalid SQL, expecting the exception.""" | ||
| with pytest.raises(expected_exception=expected_exception) as excinfo: | ||
| with adapter.connection_named("test_invalid_sql_validation"): | ||
| adapter.validate_sql(invalid_sql) | ||
|
|
||
| # InvalidConnectionError is a subclass of DbtRuntimeError, so we have to handle | ||
| # it separately. | ||
| if excinfo.type == InvalidConnectionError: | ||
| raise ValueError( | ||
| "Unexpected InvalidConnectionError. This typically indicates a problem " | ||
| "with the test setup, rather than the expected error for an invalid " | ||
| "validate_sql query." | ||
| ) from excinfo.value | ||
|
|
||
|
|
||
| class TestDryRunMethod(BaseDryRunMethod): | ||
| pass |
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.
Weird, must be some auto-formatter somewhere. :/