Skip to content

Commit

Permalink
Use natural join to avoid having to parse expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
judahrand committed Apr 14, 2022
1 parent 5f65cae commit d2ccc85
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions macros/sql/deduplicate.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@
)

select
data.*
distinct data.*
from {{ relation }} as data
join row_numbered using (
{{ group_by }}
{% for expression in order_by.split(',') %}
{% if expression.lower().endswith(' asc') or expression.lower().endswith(' desc') %}
{{ ',' ~ expression.rsplit(' ', 1)[0] }}
{% else %}
{{ ',' ~ expression }}
{% endif %}
{% endfor %}
)
{#
-- Not all DBs will support natural joins but the ones that do include:
-- Oracle, MySQL, SQLite, Redshift, Teradata, Materialize, Databricks
-- Apache Spark, SingleStore, Vertica
-- Those that do not appear to support natural joins include:
-- SQLServer, Trino, Presto, Rockset, Athena
#}
natural join row_numbered
where row_numbered.rn = 1

{%- endmacro -%}
Expand Down

0 comments on commit d2ccc85

Please sign in to comment.