Skip to content

Handle ephemeral models better #243

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 3 commits into from
Jun 29, 2020
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
15 changes: 15 additions & 0 deletions macros/cross_db_utils/_is_ephemeral.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% macro _is_ephemeral(obj, macro) %}
{%- if obj.is_cte -%}
{% if obj.name.startswith('__dbt__CTE__') %}
{% set model_name = obj.name[12:] %}
{% else %}
{% set model_name = obj.name %}
{%- endif -%}
{% set error_message %}
The `{{ macro }}` macro cannot be used with ephemeral models, as it relies on the information schema.

`{{ model_name }}` is an ephemeral model. Consider making it a view or table instead.
{% endset %}
{%- do exceptions.raise_compiler_error(error_message) -%}
{%- endif -%}
{% endmacro %}
9 changes: 9 additions & 0 deletions macros/schema_tests/equality.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@

-- setup
{%- do dbt_utils._is_relation(model, 'test_equality') -%}

{#-
If the compare_cols arg is provided, we can run this test without querying the
information schema — this allows the model to be an ephemeral model
-#}
{%- if not kwargs.get('compare_columns', None) -%}
{%- do dbt_utils._is_ephemeral(model, 'test_equality') -%}
{%- endif -%}

{% set compare_model = kwargs.get('compare_model', kwargs.get('arg')) %}
{% set compare_columns = kwargs.get('compare_columns', adapter.get_columns_in_relation(model) | map(attribute='quoted') ) %}
{% set compare_cols_csv = compare_columns | join(', ') %}
Expand Down
3 changes: 2 additions & 1 deletion macros/sql/nullcheck_table.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% macro nullcheck_table(relation) %}

{%- do dbt_utils._is_relation(relation, 'nullcheck_table') -%}
{%- do dbt_utils._is_ephemeral(relation, 'nullcheck_table') -%}
{% set cols = adapter.get_columns_in_relation(relation) %}

select {{ dbt_utils.nullcheck(cols) }}
from {{relation}}

{% endmacro %}
2 changes: 2 additions & 0 deletions macros/sql/star.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% macro star(from, relation_alias=False, except=[]) -%}

{%- do dbt_utils._is_relation(from, 'star') -%}
{%- do dbt_utils._is_ephemeral(from, 'star') -%}

{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. #}
{%- if not execute -%}
Expand All @@ -9,6 +10,7 @@

{%- set include_cols = [] %}
{%- set cols = adapter.get_columns_in_relation(from) -%}

{%- for col in cols -%}

{%- if col.column not in except -%}
Expand Down
1 change: 1 addition & 0 deletions macros/sql/union.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
{%- do relation_columns.update({relation: []}) -%}

{%- do dbt_utils._is_relation(relation, 'union_relations') -%}
{%- do dbt_utils._is_ephemeral(relation, 'union_relations') -%}
{%- set cols = adapter.get_columns_in_relation(relation) -%}
{%- for col in cols -%}

Expand Down
1 change: 1 addition & 0 deletions macros/sql/unpivot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Arguments:
{%- do table_columns.update({relation: []}) %}

{%- do dbt_utils._is_relation(relation, 'unpivot') -%}
{%- do dbt_utils._is_ephemeral(relation, 'unpivot') -%}
{%- set cols = adapter.get_columns_in_relation(relation) %}

{%- for col in cols -%}
Expand Down