diff --git a/CHANGELOG.md b/CHANGELOG.md index fc0092d1..b7799278 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,9 +11,11 @@ # Unreleased ## Fixes - deduplicate macro for Databricks now uses the QUALIFY clause, which fixes NULL columns issues from the default natural join logic +- deduplicate macro for Redshift now uses the QUALIFY clause, which fixes NULL columns issues from the default natural join logic ## Contributors: [@graciegoheen](https://github.com/graciegoheen) +[@yauhen-sobaleu](https://github.com/yauhen-sobaleu) # dbt utils v1.1.1 ## New features diff --git a/macros/sql/deduplicate.sql b/macros/sql/deduplicate.sql index 8d372abe..3e75579c 100644 --- a/macros/sql/deduplicate.sql +++ b/macros/sql/deduplicate.sql @@ -29,10 +29,17 @@ {%- endmacro -%} -{# Redshift should use default instead of Postgres #} +-- Redshift has the `QUALIFY` syntax: +-- https://docs.aws.amazon.com/redshift/latest/dg/r_QUALIFY_clause.html {% macro redshift__deduplicate(relation, partition_by, order_by) -%} - {{ return(dbt_utils.default__deduplicate(relation, partition_by, order_by=order_by)) }} + select * + from {{ relation }} as tt + qualify + row_number() over ( + partition by {{ partition_by }} + order by {{ order_by }} + ) = 1 {% endmacro %}