Skip to content

Commit

Permalink
Enhance formatting of function macros
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesmet committed Sep 9, 2022
1 parent bd006f4 commit 275bfdb
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions dbt/include/trino/macros/crossdb/current_timestamp.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% macro trino__current_timestamp() %}
{% macro trino__current_timestamp() -%}
current_timestamp
{% endmacro %}
{%- endmacro %}
12 changes: 6 additions & 6 deletions dbt/include/trino/macros/crossdb/datatypes.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{% macro trino__type_float() %}
{% macro trino__type_float() -%}
double
{% endmacro %}
{%- endmacro %}

{% macro trino__type_string() %}
{% macro trino__type_string() -%}
varchar
{% endmacro %}
{%- endmacro %}

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

{%- macro trino__type_int() -%}
integer
Expand Down
4 changes: 2 additions & 2 deletions dbt/include/trino/macros/crossdb/date_trunc.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% macro trino__date_trunc(datepart, date) %}
{% macro trino__date_trunc(datepart, date) -%}
date_trunc('{{datepart}}', {{date}})
{% endmacro %}
{%- endmacro %}
4 changes: 2 additions & 2 deletions dbt/include/trino/macros/crossdb/dateadd.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% macro trino__dateadd(datepart, interval, from_date_or_timestamp) %}
{% macro trino__dateadd(datepart, interval, from_date_or_timestamp) -%}
date_add('{{ datepart }}', {{ interval }}, {{ from_date_or_timestamp }})
{% endmacro %}
{%- endmacro %}
26 changes: 13 additions & 13 deletions dbt/include/trino/macros/crossdb/datediff.sql
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{% macro trino__datediff(first_date, second_date, datepart) %}
{% if datepart == 'year' %}
{% macro trino__datediff(first_date, second_date, datepart) -%}
{%- if datepart == 'year' -%}
(year(CAST({{ second_date }} AS TIMESTAMP)) - year(CAST({{ first_date }} AS TIMESTAMP)))
{% elif datepart == 'quarter' %}
{%- 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' %}
{%- 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' %}
{%- 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' %}
{%- 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' %}
{%- 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' %}
{%- 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' %}
{%- 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' %}
{%- elif datepart == 'millisecond' -%}
(to_milliseconds((CAST({{ second_date }} AS TIMESTAMP) - CAST({{ first_date }} AS TIMESTAMP))))
{% else %}
{%- else -%}
{% if execute %}{{ exceptions.raise_compiler_error("Unsupported datepart for macro datediff in Trino: {!r}".format(datepart)) }}{% endif %}
{% endif %}
{% endmacro %}
{%- endif -%}
{%- endmacro %}
4 changes: 2 additions & 2 deletions dbt/include/trino/macros/crossdb/safe_cast.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% macro trino__safe_cast(field, type) %}
{% macro trino__safe_cast(field, type) -%}
try_cast({{field}} as {{type}})
{% endmacro %}
{%- endmacro %}
10 changes: 5 additions & 5 deletions dbt/include/trino/macros/materializations/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,28 @@
{{ run_hooks(pre_hooks) }}

{% if on_table_exists == 'rename' %}
-- build modeldock
{#-- build modeldock #}
{% call statement('main') -%}
{{ create_table_as(False, intermediate_relation, sql) }}
{%- endcall %}

-- cleanup
{#-- cleanup #}
{% if old_relation is not none %}
{{ adapter.rename_relation(old_relation, backup_relation) }}
{% endif %}

{{ adapter.rename_relation(intermediate_relation, target_relation) }}

-- finally, drop the existing/backup relation after the commit
{#-- finally, drop the existing/backup relation after the commit #}
{{ drop_relation_if_exists(backup_relation) }}

{% elif on_table_exists == 'drop' %}
-- cleanup
{#-- cleanup #}
{%- if old_relation is not none -%}
{{ adapter.drop_relation(old_relation) }}
{%- endif -%}

-- build model
{#-- build model #}
{% call statement('main') -%}
{{ create_table_as(False, target_relation, sql) }}
{%- endcall %}
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/adapter/materialization/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
""".lstrip()

iceberg_macro_override_sql = """\
{% macro trino__current_timestamp() %}
{% macro trino__current_timestamp() -%}
current_timestamp(6)
{% endmacro %}
{%- endmacro %}
"""


Expand Down

0 comments on commit 275bfdb

Please sign in to comment.