From d2ccc85b3eda7d87f686abad332c0ec84511f001 Mon Sep 17 00:00:00 2001 From: Judah Rand <17158624+judahrand@users.noreply.github.com> Date: Thu, 14 Apr 2022 11:22:16 +0100 Subject: [PATCH] Use `natural join` to avoid having to parse expressions --- macros/sql/deduplicate.sql | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/macros/sql/deduplicate.sql b/macros/sql/deduplicate.sql index c1c3c29b..9e6ac5a8 100644 --- a/macros/sql/deduplicate.sql +++ b/macros/sql/deduplicate.sql @@ -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 -%}