diff --git a/CHANGELOG.md b/CHANGELOG.md index f9b82fffb7c..124cdef77e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Changes - Graph refactor: fix common issues with load order ([#292](https://github.com/fishtown-analytics/dbt/pull/292)) +- Graph refactor: multiple references to an ephemeral models should share a CTE ([#316](https://github.com/fishtown-analytics/dbt/pull/316)) - Refactor: factor out jinja interactions ([#309](https://github.com/fishtown-analytics/dbt/pull/309)) - Speedup: detect cycles at the end of compilation ([#307](https://github.com/fishtown-analytics/dbt/pull/307)) - Speedup: write graph file with gpickle instead of yaml ([#306](https://github.com/fishtown-analytics/dbt/pull/306)) diff --git a/dbt/compilation.py b/dbt/compilation.py index 5bb03cf45a9..83799151480 100644 --- a/dbt/compilation.py +++ b/dbt/compilation.py @@ -206,10 +206,12 @@ def do_ref(*args): target_model_id = target_model.get('unique_id') - model['depends_on'].append(target_model_id) + if target_model_id not in model['depends_on']: + model['depends_on'].append(target_model_id) if get_materialization(target_model) == 'ephemeral': - model['extra_cte_ids'].append(target_model_id) + if target_model_id not in model['extra_cte_ids']: + model['extra_cte_ids'].append(target_model_id) return '__dbt__CTE__{}'.format(target_model.get('name')) else: return '"{}"."{}"'.format(schema, target_model.get('name')) diff --git a/test/integration/020_ephemeral_test/models/dependent.sql b/test/integration/020_ephemeral_test/models/dependent.sql index 5a5a0215dac..1047f0149e5 100644 --- a/test/integration/020_ephemeral_test/models/dependent.sql +++ b/test/integration/020_ephemeral_test/models/dependent.sql @@ -1 +1,5 @@ -select * from {{ref('base')}} + +-- multiple ephemeral refs should share a cte +select * from {{ref('base')}} where gender = 'Male' +union all +select * from {{ref('base')}} where gender = 'Female'