-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update README.md * Mutually excl range examples in disclosure triangle * Fix union_relations error when no include/exclude provided * Fix union_relations error when no include/exclude provided (#509) * Update CHANGELOG.md * Add dedupe macro * Add test for dedupe macro * Add documentation to README * Add entry to CHANGELOG * Implement review
- Loading branch information
Showing
7 changed files
with
86 additions
and
4 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
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 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,3 @@ | ||
user_id,event,version | ||
1,play,1 | ||
1,play,2 |
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,2 @@ | ||
user_id,event,version | ||
1,play,2 |
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 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 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 -%} |