|
| 1 | +{# |
| 2 | + dbt-core does not dispatch "get_fixture_sql" or "get_expected_sql", so this file |
| 3 | + shadows the implementations from the dbt global project |
| 4 | + (macros/unit_test_sql/get_fixture_sql.sql) - adapter package macros take |
| 5 | + precedence over the global project. Keep in sync with dbt-core when upgrading. |
| 6 | + |
| 7 | + To ease a future transition if dbt-core adds adapter.dispatch for these macros, |
| 8 | + the public wrappers below are intentionally thin. They delegate to the |
| 9 | + SQL Server-specific implementations (sqlserver__get_fixture_sql and |
| 10 | + sqlserver__get_expected_sql), which contain the copied/adapted upstream logic. |
| 11 | + |
| 12 | + When upstream dispatches these macros, the public wrappers can be deleted and |
| 13 | + the sqlserver__ implementations kept as the dispatched handlers. |
| 14 | + |
| 15 | + Changes from upstream (see dbt-msft/dbt-sqlserver#698): |
| 16 | + - sqlserver__get_fixture_sql: the empty-rows branch emits "select top 0" |
| 17 | + instead of "limit 0", which is not valid T-SQL. |
| 18 | + - sqlserver__get_expected_sql: the empty-rows branch emits a "select top 0" |
| 19 | + of typed nulls instead of "select * from dbt_internal_unit_test_actual limit 0". |
| 20 | + Besides the invalid "limit", sqlserver__get_unit_test_sql wraps the expected |
| 21 | + SQL in its own view, where that CTE name is out of scope. |
| 22 | +#} |
| 23 | + |
| 24 | +{% macro get_fixture_sql(rows, column_name_to_data_types) %} |
| 25 | + {{ return(sqlserver__get_fixture_sql(rows, column_name_to_data_types)) }} |
| 26 | +{% endmacro %} |
| 27 | + |
| 28 | + |
| 29 | +{% macro sqlserver__get_fixture_sql(rows, column_name_to_data_types) %} |
| 30 | +-- Fixture for {{ model.name }} |
| 31 | +{% set default_row = {} %} |
| 32 | + |
| 33 | +{%- if not column_name_to_data_types -%} |
| 34 | +{#-- Use defer_relation IFF it is available in the manifest and 'this' is missing from the database --#} |
| 35 | +{%- set this_or_defer_relation = defer_relation if (defer_relation and not load_relation(this)) else this -%} |
| 36 | +{%- set columns_in_relation = adapter.get_columns_in_relation(this_or_defer_relation) -%} |
| 37 | + |
| 38 | +{%- set column_name_to_data_types = {} -%} |
| 39 | +{%- set column_name_to_quoted = {} -%} |
| 40 | +{%- for column in columns_in_relation -%} |
| 41 | + |
| 42 | +{#-- This needs to be a case-insensitive comparison --#} |
| 43 | +{%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%} |
| 44 | +{%- do column_name_to_quoted.update({column.name|lower: column.quoted}) -%} |
| 45 | +{%- endfor -%} |
| 46 | +{%- endif -%} |
| 47 | + |
| 48 | +{%- if not column_name_to_data_types -%} |
| 49 | + {{ exceptions.raise_compiler_error("Not able to get columns for unit test '" ~ model.name ~ "' from relation " ~ this ~ " because the relation doesn't exist") }} |
| 50 | +{%- endif -%} |
| 51 | + |
| 52 | +{%- for column_name, column_type in column_name_to_data_types.items() -%} |
| 53 | + {%- do default_row.update({column_name: (safe_cast("null", column_type) | trim )}) -%} |
| 54 | +{%- endfor -%} |
| 55 | + |
| 56 | +{{ validate_fixture_rows(rows, row_number) }} |
| 57 | + |
| 58 | +{%- for row in rows -%} |
| 59 | +{%- set formatted_row = format_row(row, column_name_to_data_types) -%} |
| 60 | +{%- set default_row_copy = default_row.copy() -%} |
| 61 | +{%- do default_row_copy.update(formatted_row) -%} |
| 62 | +select |
| 63 | +{%- for column_name, column_value in default_row_copy.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%}, {%- endif %} |
| 64 | +{%- endfor %} |
| 65 | +{%- if not loop.last %} |
| 66 | +union all |
| 67 | +{% endif %} |
| 68 | +{%- endfor -%} |
| 69 | + |
| 70 | +{%- if (rows | length) == 0 -%} |
| 71 | + select top 0 |
| 72 | + {%- for column_name, column_value in default_row.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%},{%- endif %} |
| 73 | + {%- endfor %} |
| 74 | +{%- endif -%} |
| 75 | +{% endmacro %} |
| 76 | + |
| 77 | + |
| 78 | +{% macro get_expected_sql(rows, column_name_to_data_types, column_name_to_quoted) %} |
| 79 | + {{ return(sqlserver__get_expected_sql(rows, column_name_to_data_types, column_name_to_quoted)) }} |
| 80 | +{% endmacro %} |
| 81 | + |
| 82 | + |
| 83 | +{% macro sqlserver__get_expected_sql(rows, column_name_to_data_types, column_name_to_quoted) %} |
| 84 | + |
| 85 | +{%- if (rows | length) == 0 -%} |
| 86 | + select top 0 |
| 87 | + {%- 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 %} |
| 88 | + {%- endfor %} |
| 89 | +{%- else -%} |
| 90 | +{%- for row in rows -%} |
| 91 | +{%- set formatted_row = format_row(row, column_name_to_data_types) -%} |
| 92 | +select |
| 93 | +{%- for column_name, column_value in formatted_row.items() %} {{ column_value }} as {{ column_name_to_quoted[column_name] }}{% if not loop.last -%}, {%- endif %} |
| 94 | +{%- endfor %} |
| 95 | +{%- if not loop.last %} |
| 96 | +union all |
| 97 | +{% endif %} |
| 98 | +{%- endfor -%} |
| 99 | +{%- endif -%} |
| 100 | + |
| 101 | +{% endmacro %} |
0 commit comments