Skip to content
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

Regression: Correctly handle missing relations in get_column_values #448

Merged
merged 3 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions integration_tests/data/sql/data_get_column_values_dropped.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
field
a
b
c
d
e
f
g
g
g
g
g
6 changes: 6 additions & 0 deletions integration_tests/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ seeds:
sql:
data_events_20180103:
+schema: events

data_get_column_values_dropped:
# TODO: Ideally this would use the adapter, but it gives a "Tried to drop relation, but its type is null" error.
#+post-hook: "{% do adapter.drop_relation(this) %}"
+post-hook: "drop table {{ this }}"


schema_tests:
data_test_sequential_timestamps:
Expand Down
25 changes: 25 additions & 0 deletions integration_tests/tests/sql/test_get_column_values_use_default.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

{% set column_values = dbt_utils.get_column_values(ref('data_get_column_values_dropped'), 'field', default=['y', 'z'], order_by="field") %}

with expected as (
select { dbt_utils.safe_cast('y', dbt_utils.type_string()) }} as expected union all
select { dbt_utils.safe_cast('z', dbt_utils.type_string()) }} as expected
),

actual as (
select
{% for val in column_values %}
{{ dbt_utils.safe_cast(val, dbt_utils.type_string()) }} as 'actual'
{% if not loop.last %}
union all
{% endif %}
{% endfor %}
),

failures as (
select * from actual
except
select * from expected
)

select * from failures
11 changes: 7 additions & 4 deletions macros/sql/get_column_values.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
{# TODO: Change the method signature in a future 0.x.0 release #}
{%- set target_relation = table -%}

{# adapter.load_relation is a convenience wrapper to avoid building a Relation when we already have one #}
{% set relation_exists = (load_relation(target_relation)) is not none %}

{%- call statement('get_column_values', fetch_result=true) %}

{%- if not target_relation and default is none -%}
{%- if not relation_exists and default is none -%}

{{ exceptions.raise_compiler_error("In get_column_values(): relation " ~ table ~ " does not exist and no default value was provided.") }}
{{ exceptions.raise_compiler_error("In get_column_values(): relation " ~ target_relation ~ " does not exist and no default value was provided.") }}

{%- elif not target_relation and default is not none -%}
{%- elif not relation_exists and default is not none -%}

{{ log("Relation " ~ table ~ " does not exist. Returning the default value: " ~ default) }}
{{ log("Relation " ~ target_relation ~ " does not exist. Returning the default value: " ~ default) }}

{{ return(default) }}

Expand Down