-
Notifications
You must be signed in to change notification settings - Fork 110
Empty unit test fixtures (rows: []) emit invalid LIMIT 0 syntax #699
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
Benjamin-Knight
wants to merge
5
commits into
master
Choose a base branch
from
fix/698-unit-test-empty-rows-tsql
base: master
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
766cd32
Update version to 1.10.0rc1 on release/v1.10
axellpadilla d3f32e8
Merge pull request #695 from dbt-msft/chore/version-v1.10.0rc1
axellpadilla d0f251c
fix: empty unit test fixtures (rows: []) emit invalid LIMIT 0 syntax …
Benjamin-Knight b1b2876
Merge branch 'master' into fix/698-unit-test-empty-rows-tsql
Benjamin-Knight c54a40e
Merge branch 'master' into fix/698-unit-test-empty-rows-tsql
Benjamin-Knight 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
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
83 changes: 83 additions & 0 deletions
83
dbt/include/sqlserver/macros/materializations/models/unit_test/get_fixture_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,83 @@ | ||
| {# | ||
| dbt-core does not dispatch "get_fixture_sql" or "get_expected_sql", so this file | ||
| shadows the implementations from the dbt global project | ||
| (macros/unit_test_sql/get_fixture_sql.sql) - adapter package macros take | ||
| precedence over the global project. Keep in sync with dbt-core when upgrading. | ||
|
|
||
| Changes from upstream (see dbt-msft/dbt-sqlserver#698): | ||
| - get_fixture_sql: the empty-rows branch emits "select top 0" instead of | ||
| "limit 0", which is not valid T-SQL. | ||
| - get_expected_sql: the empty-rows branch emits a "select top 0" of typed | ||
| nulls instead of "select * from dbt_internal_unit_test_actual limit 0". | ||
| Besides the invalid "limit", sqlserver__get_unit_test_sql wraps the | ||
| expected SQL in its own view, where that CTE name is out of scope. | ||
| #} | ||
|
|
||
| {% macro get_fixture_sql(rows, column_name_to_data_types) %} | ||
| -- Fixture for {{ model.name }} | ||
| {% set default_row = {} %} | ||
|
|
||
| {%- if not column_name_to_data_types -%} | ||
| {#-- Use defer_relation IFF it is available in the manifest and 'this' is missing from the database --#} | ||
| {%- set this_or_defer_relation = defer_relation if (defer_relation and not load_relation(this)) else this -%} | ||
| {%- set columns_in_relation = adapter.get_columns_in_relation(this_or_defer_relation) -%} | ||
|
|
||
| {%- set column_name_to_data_types = {} -%} | ||
| {%- set column_name_to_quoted = {} -%} | ||
| {%- for column in columns_in_relation -%} | ||
|
|
||
| {#-- This needs to be a case-insensitive comparison --#} | ||
| {%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%} | ||
| {%- do column_name_to_quoted.update({column.name|lower: column.quoted}) -%} | ||
| {%- endfor -%} | ||
| {%- endif -%} | ||
|
|
||
| {%- if not column_name_to_data_types -%} | ||
| {{ exceptions.raise_compiler_error("Not able to get columns for unit test '" ~ model.name ~ "' from relation " ~ this ~ " because the relation doesn't exist") }} | ||
| {%- endif -%} | ||
|
|
||
| {%- for column_name, column_type in column_name_to_data_types.items() -%} | ||
| {%- do default_row.update({column_name: (safe_cast("null", column_type) | trim )}) -%} | ||
| {%- endfor -%} | ||
|
|
||
| {{ validate_fixture_rows(rows, row_number) }} | ||
|
|
||
| {%- for row in rows -%} | ||
| {%- set formatted_row = format_row(row, column_name_to_data_types) -%} | ||
| {%- set default_row_copy = default_row.copy() -%} | ||
| {%- do default_row_copy.update(formatted_row) -%} | ||
| select | ||
| {%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%}, {%- endif %} | ||
| {%- endfor %} | ||
| {%- if not loop.last %} | ||
| union all | ||
| {% endif %} | ||
| {%- endfor -%} | ||
|
|
||
| {%- if (rows | length) == 0 -%} | ||
| select top 0 | ||
| {%- for column_name, column_value in default_row.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%},{%- endif %} | ||
| {%- endfor %} | ||
| {%- endif -%} | ||
| {% endmacro %} | ||
|
|
||
|
|
||
| {% macro get_expected_sql(rows, column_name_to_data_types, column_name_to_quoted) %} | ||
|
|
||
| {%- if (rows | length) == 0 -%} | ||
| select top 0 | ||
| {%- for column_name, column_type in column_name_to_data_types.items() %} {{ safe_cast("null", column_type) | trim }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%},{%- endif %} | ||
| {%- endfor %} | ||
| {%- else -%} | ||
| {%- for row in rows -%} | ||
| {%- set formatted_row = format_row(row, column_name_to_data_types) -%} | ||
| select | ||
| {%- for column_name, column_value in formatted_row.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%}, {%- endif %} | ||
| {%- endfor %} | ||
| {%- if not loop.last %} | ||
| union all | ||
| {% endif %} | ||
| {%- endfor -%} | ||
| {%- endif -%} | ||
|
|
||
| {% endmacro %} |
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
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.
I don't see any other solution than shadowing, that said, inside the shadowed macro we could introduce a call to macro sqlserver__get_fixture_sql(...) instead. This would keep the SQL Server-specific logic isolated and make the implementation closer to what we would want upstream if they use adapter calls later.
This is not that important but ideally with a link to an upstream dbt-core/dbt-adapters issue requesting dispatch support. If upstream adds that dispatch point later, our future patch should mostly be removing the shadow and keeping only the SQL Server-specific sqlserver__get_fixture_sql implementation.