Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply adapter macro fixes on dbt-utils #6

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions integration_tests/dbt_utils/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ models:
cross_db_utils:
test_datediff:
+enabled: false
test_listagg:
+enabled: false

datetime:
test_date_spine:
Expand Down
6 changes: 2 additions & 4 deletions macros/dbt_utils/cross_db_utils/any_value.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{% macro default__any_value(expression) -%}

{% macro trino__any_value(expression) -%}
min({{ expression }})

{%- endmacro %}
{%- endmacro %}
6 changes: 2 additions & 4 deletions macros/dbt_utils/cross_db_utils/bool_or.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{% macro default__bool_or(expression) -%}

{% macro trino__bool_or(expression) -%}
bool_or({{ expression }})

{%- endmacro %}
{%- endmacro %}
7 changes: 2 additions & 5 deletions macros/dbt_utils/cross_db_utils/datatypes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@
{% endmacro %}

{% macro trino__type_numeric() %}
DECIMAL(
28,
6
)
DECIMAL(28, 6)
{% endmacro %}

{%- macro trino__type_int() -%}
integer
{%- endmacro -%}
{%- endmacro -%}
2 changes: 1 addition & 1 deletion macros/dbt_utils/cross_db_utils/date_trunc.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% macro trino__date_trunc(datepart, date) %}
date_trunc('{{datepart}}', {{date}})
{% endmacro %}
{% endmacro %}
6 changes: 1 addition & 5 deletions macros/dbt_utils/cross_db_utils/dateadd.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
{% macro trino__dateadd(datepart, interval, from_date_or_timestamp) %}
date_add(
'{{ datepart }}',
{{ interval }},
{{ from_date_or_timestamp }}
)
date_add('{{ datepart }}', {{ interval }}, {{ from_date_or_timestamp }})
{% endmacro %}
33 changes: 26 additions & 7 deletions macros/dbt_utils/cross_db_utils/datediff.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
{% macro trino__datediff(first_date, second_date, datepart) %}

date_diff(
'{{ datepart }}',
{{ first_date }},
{{ second_date }}
)

{% if datepart == 'year' %}
(year(CAST({{ second_date }} AS TIMESTAMP)) - year(CAST({{ first_date }} AS TIMESTAMP)))
{% elif datepart == 'quarter' %}
({{ datediff(first_date, second_date, 'year') }} * 4) + quarter(CAST({{ second_date }} AS TIMESTAMP)) - quarter(CAST({{ first_date }} AS TIMESTAMP))
{% elif datepart == 'month' %}
({{ datediff(first_date, second_date, 'year') }} * 12) + month(CAST({{ second_date }} AS TIMESTAMP)) - month(CAST({{ first_date }} AS TIMESTAMP))
{% elif datepart == 'day' %}
((to_milliseconds((CAST(CAST({{ second_date }} AS TIMESTAMP) AS DATE) - CAST(CAST({{ first_date }} AS TIMESTAMP) AS DATE)))) / 86400000)
{% elif datepart == 'week' %}
({{ datediff(first_date, second_date, 'day') }} / 7 + case
when dow(CAST({{first_date}} AS TIMESTAMP)) <= dow(CAST({{second_date}} AS TIMESTAMP)) then
case when {{first_date}} <= {{second_date}} then 0 else -1 end
else
case when {{first_date}} <= {{second_date}} then 1 else 0 end
end)
{% elif datepart == 'hour' %}
({{ datediff(first_date, second_date, 'day') }} * 24 + hour(CAST({{ second_date }} AS TIMESTAMP)) - hour(CAST({{ first_date }} AS TIMESTAMP)))
{% elif datepart == 'minute' %}
({{ datediff(first_date, second_date, 'hour') }} * 60 + minute(CAST({{ second_date }} AS TIMESTAMP)) - minute(CAST({{ first_date }} AS TIMESTAMP)))
{% elif datepart == 'second' %}
({{ datediff(first_date, second_date, 'minute') }} * 60 + second(CAST({{ second_date }} AS TIMESTAMP)) - second(CAST({{ first_date }} AS TIMESTAMP)))
{% elif datepart == 'millisecond' %}
(to_milliseconds((CAST({{ second_date }} AS TIMESTAMP) - CAST({{ first_date }} AS TIMESTAMP))))
{% else %}
{% if execute %}{{ exceptions.raise_compiler_error("Unsupported datepart for macro datediff in Trino: {!r}".format(datepart)) }}{% endif %}
{% endif %}
{% endmacro %}
2 changes: 1 addition & 1 deletion macros/dbt_utils/cross_db_utils/hash.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% macro trino__hash(field) -%}
lower(to_hex(md5(to_utf8(cast({{field}} as varchar)))))
{%- endmacro %}t
{%- endmacro %}
39 changes: 6 additions & 33 deletions macros/dbt_utils/cross_db_utils/listagg.sql
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
{# if there are instances of delimiter_text within your measure, you cannot include a limit_num #}
{% macro trino__listagg(measure, delimiter_text, order_by_clause, limit_num) -%}

{% if limit_num -%}
{% set ns = namespace() %}
{% set ns.delimiter_text_regex = delimiter_text|trim("'") %}
{% set special_chars %}\,^,$,.,|,?,*,+,(,),[,],{,}{% endset %}
{%- for char in special_chars.split(',') -%}
{% set escape_char %}\\{{ char }}{% endset %}
{% set ns.delimiter_text_regex = ns.delimiter_text_regex|replace(char,escape_char) %}
{%- endfor -%}

{% set regex %}'([^{{ ns.delimiter_text_regex }}]+{{ ns.delimiter_text_regex }}){1,{{ limit_num - 1}}}[^{{ ns.delimiter_text_regex }}]+'{% endset %}
regexp_substr(
listagg(
{{ measure }},
{{ delimiter_text }}
)
{% if order_by_clause -%}
within group ({{ order_by_clause }})
{%- endif %}
,{{ regex }}
)
{%- else %}
listagg(
{{ measure }},
{{ delimiter_text }}
)
{% if order_by_clause -%}
within group ({{ order_by_clause }})
{%- endif %}
{%- endif %}

{%- endmacro %}
{% set collect_list %} array_agg({{ measure }} {% if order_by_clause -%}{{ order_by_clause }}{%- endif %}) {% endset %}
{% set limited %} slice({{ collect_list }}, 1, {{ limit_num }}) {% endset %}
{% set collected = limited if limit_num else collect_list %}
{% set final %} array_join({{ collected }}, {{ delimiter_text }}) {% endset %}
{% do return(final) %}
{%- endmacro %}
7 changes: 1 addition & 6 deletions macros/dbt_utils/cross_db_utils/right.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
{% macro trino__right(string_text, length_expression) %}

case when {{ length_expression }} = 0
then ''
else
substr(
{{ string_text }},
-1 * ({{ length_expression }})
)
substr({{ string_text }}, -1 * ({{ length_expression }}))
end

{%- endmacro -%}