-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
59 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
File renamed without changes.
File renamed without changes.
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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,7 @@ | ||
with deduped as ( | ||
|
||
{{ dbt_utils.deduplicate(ref('data_deduplicate'), group_by='user_id', order_by='version desc') | indent }} | ||
|
||
) | ||
|
||
select * from deduped |
This file was deleted.
Oops, something went wrong.
This file contains 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,46 @@ | ||
{%- macro deduplicate(relation, group_by, order_by=none) -%} | ||
{{ return(adapter.dispatch('deduplicate', 'dbt_utils')(relation, group_by, order_by=order_by)) }} | ||
{% endmacro %} | ||
|
||
{%- macro default__deduplicate(relation, group_by, order_by=none) -%} | ||
|
||
select | ||
{{ dbt_utils.star(relation, relation_alias='deduped') | indent }} | ||
from ( | ||
select | ||
_inner.*, | ||
row_number() over ( | ||
partition by {{ group_by }} | ||
{% if order_by is not none -%} | ||
order by {{ order_by }} | ||
{%- endif %} | ||
) as rn | ||
from {{ relation }} as _inner | ||
) as deduped | ||
where deduped.rn = 1 | ||
|
||
{%- endmacro -%} | ||
|
||
{# | ||
-- It is more performant to deduplicate using `array_agg` with a limit | ||
-- clause in BigQuery: | ||
-- https://github.com/dbt-labs/dbt-utils/issues/335#issuecomment-788157572 | ||
#} | ||
{%- macro bigquery__deduplicate(relation, group_by, order_by=none) -%} | ||
|
||
select | ||
{{ dbt_utils.star(relation, relation_alias='deduped') | indent }} | ||
from ( | ||
select | ||
array_agg ( | ||
original | ||
{% if order_by is not none -%} | ||
order by {{ order_by }} | ||
{%- endif %} | ||
limit 1 | ||
)[offset(0)] as deduped | ||
from {{ relation }} as original | ||
group by {{ group_by }} | ||
) | ||
|
||
{%- endmacro -%} |