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

Initialize lift + shift for cross-db macros #594

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 6 additions & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
pytest
pytest-dotenv
dbt-tests-adapter
git+https://github.com/dbt-labs/dbt-core.git@jerco/utils-lift-shift#egg=dbt-core&subdirectory=core
git+https://github.com/dbt-labs/dbt-core.git@jerco/utils-lift-shift#egg=dbt-tests-adapter&subdirectory=tests/adapter
git+https://github.com/dbt-labs/dbt-core.git@jerco/utils-lift-shift#egg=dbt-postgres&subdirectory=plugins/postgres
git+https://github.com/dbt-labs/dbt-redshift.git@jerco/utils-lift-shift
git+https://github.com/dbt-labs/dbt-snowflake.git@jerco/utils-lift-shift
git+https://github.com/dbt-labs/dbt-bigquery.git@jerco/utils-lift-shift
78 changes: 76 additions & 2 deletions macros/cross_db_utils/dateadd.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
{# ------ IF WE DON'T CARE ABOUT BACKWARDS COMPATIBILITY AT ALL -------- #}

{% macro dateadd(datepart, interval, from_date_or_timestamp) %}
{{ return(adapter.dispatch('dateadd', 'dbt_utils')(datepart, interval, from_date_or_timestamp)) }}
{{ return(adapter.dispatch('dateadd', 'dbt')(datepart, interval, from_date_or_timestamp)) }}
{% endmacro %}

{# ------ IF WE CARE ABOUT BACKWARDS COMPATIBILITY A LITTLE -----------

{% macro dateadd(datepart, interval, from_date_or_timestamp) %}
{% if dbt_version[0]|int == 1 and dbt_version[2]|int >= 2 %}
{{ return(adapter.dispatch('dateadd', 'dbt')(datepart, interval, from_date_or_timestamp)) }}
{% else %}
{{ return(adapter.dispatch('dateadd', 'dbt_utils')(datepart, interval, from_date_or_timestamp)) }}
{% endif %}
{% endmacro %}

{% macro default__dateadd(datepart, interval, from_date_or_timestamp) %}

Expand Down Expand Up @@ -29,9 +40,72 @@

{% endmacro %}

{# redshift should use default instead of postgres #}
-- redshift should use default instead of postgres
{% macro redshift__dateadd(datepart, interval, from_date_or_timestamp) %}

{{ return(dbt_utils.default__dateadd(datepart, interval, from_date_or_timestamp)) }}

{% endmacro %}
#}

{# ------ IF WE CARE ABOUT BACKWARDS COMPATIBILITY A LOT ---------

{% macro dateadd(datepart, interval, from_date_or_timestamp) %}
{{ return(adapter.dispatch('dateadd', 'dbt_utils')(datepart, interval, from_date_or_timestamp)) }}
{% endmacro %}


{% macro default__dateadd(datepart, interval, from_date_or_timestamp) %}

{% if dbt_version[0]|int == 1 and dbt_version[2]|int >= 2 %}
{{ return(dbt.dateadd(datepart, interval, from_date_or_timestamp)) }}
{% else %}

dateadd(
{{ datepart }},
{{ interval }},
{{ from_date_or_timestamp }}
)

{% endif %}

{% endmacro %}


{% macro bigquery__dateadd(datepart, interval, from_date_or_timestamp) %}

{% if dbt_version[0]|int == 1 and dbt_version[2]|int >= 2 %}
{{ return(dbt.dateadd(datepart, interval, from_date_or_timestamp)) }}
{% else %}

datetime_add(
cast( {{ from_date_or_timestamp }} as datetime),
interval {{ interval }} {{ datepart }}
)

{% endif %}
{% endmacro %}

{% macro postgres__dateadd(datepart, interval, from_date_or_timestamp) %}

{% if dbt_version[0]|int == 1 and dbt_version[2]|int >= 2 %}
{{ return(dbt.dateadd(datepart, interval, from_date_or_timestamp)) }}
{% else %}

{{ from_date_or_timestamp }} + ((interval '1 {{ datepart }}') * ({{ interval }}))

{% endif %}
{% endmacro %}

-- redshift should use default instead of postgres
{% macro redshift__dateadd(datepart, interval, from_date_or_timestamp) %}

{% if dbt_version[0]|int == 1 and dbt_version[2]|int >= 2 %}
{{ return(dbt.dateadd(datepart, interval, from_date_or_timestamp)) }}
{% else %}

{{ return(dbt_utils.default__dateadd(datepart, interval, from_date_or_timestamp)) }}

{% endif %}
{% endmacro %}
#}
67 changes: 3 additions & 64 deletions macros/cross_db_utils/datediff.sql
Original file line number Diff line number Diff line change
@@ -1,66 +1,5 @@
------ IF WE DON'T CARE ABOUT BACKWARDS COMPATIBILITY --------

{% macro datediff(first_date, second_date, datepart) %}
{{ return(adapter.dispatch('datediff', 'dbt_utils')(first_date, second_date, datepart)) }}
{{ return(adapter.dispatch('datediff', 'dbt')(first_date, second_date, datepart)) }}
{% endmacro %}


{% macro default__datediff(first_date, second_date, datepart) -%}

datediff(
{{ datepart }},
{{ first_date }},
{{ second_date }}
)

{%- endmacro %}


{% macro bigquery__datediff(first_date, second_date, datepart) -%}

datetime_diff(
cast({{second_date}} as datetime),
cast({{first_date}} as datetime),
{{datepart}}
)

{%- endmacro %}

{% macro postgres__datediff(first_date, second_date, datepart) -%}

{% if datepart == 'year' %}
(date_part('year', ({{second_date}})::date) - date_part('year', ({{first_date}})::date))
{% elif datepart == 'quarter' %}
({{ dbt_utils.datediff(first_date, second_date, 'year') }} * 4 + date_part('quarter', ({{second_date}})::date) - date_part('quarter', ({{first_date}})::date))
{% elif datepart == 'month' %}
({{ dbt_utils.datediff(first_date, second_date, 'year') }} * 12 + date_part('month', ({{second_date}})::date) - date_part('month', ({{first_date}})::date))
{% elif datepart == 'day' %}
(({{second_date}})::date - ({{first_date}})::date)
{% elif datepart == 'week' %}
({{ dbt_utils.datediff(first_date, second_date, 'day') }} / 7 + case
when date_part('dow', ({{first_date}})::timestamp) <= date_part('dow', ({{second_date}})::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' %}
({{ dbt_utils.datediff(first_date, second_date, 'day') }} * 24 + date_part('hour', ({{second_date}})::timestamp) - date_part('hour', ({{first_date}})::timestamp))
{% elif datepart == 'minute' %}
({{ dbt_utils.datediff(first_date, second_date, 'hour') }} * 60 + date_part('minute', ({{second_date}})::timestamp) - date_part('minute', ({{first_date}})::timestamp))
{% elif datepart == 'second' %}
({{ dbt_utils.datediff(first_date, second_date, 'minute') }} * 60 + floor(date_part('second', ({{second_date}})::timestamp)) - floor(date_part('second', ({{first_date}})::timestamp)))
{% elif datepart == 'millisecond' %}
({{ dbt_utils.datediff(first_date, second_date, 'minute') }} * 60000 + floor(date_part('millisecond', ({{second_date}})::timestamp)) - floor(date_part('millisecond', ({{first_date}})::timestamp)))
{% elif datepart == 'microsecond' %}
({{ dbt_utils.datediff(first_date, second_date, 'minute') }} * 60000000 + floor(date_part('microsecond', ({{second_date}})::timestamp)) - floor(date_part('microsecond', ({{first_date}})::timestamp)))
{% else %}
{{ exceptions.raise_compiler_error("Unsupported datepart for macro datediff in postgres: {!r}".format(datepart)) }}
{% endif %}

{%- endmacro %}


{# redshift should use default instead of postgres #}
{% macro redshift__datediff(first_date, second_date, datepart) -%}

{{ return(dbt_utils.default__datediff(first_date, second_date, datepart)) }}

{%- endmacro %}
3 changes: 2 additions & 1 deletion run_functional_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ if [[ ! -f $VENV ]]; then
. $VENV

pip install --upgrade pip setuptools
pip install --pre "dbt-$1" -r dev-requirements.txt
pip install -r dev-requirements.txt
pip install --pre "dbt-$1"
fi

. $VENV
Expand Down
1 change: 1 addition & 0 deletions run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ if [[ ! -f $VENV ]]; then
. $VENV

pip install --upgrade pip setuptools
pip install -r dev-requirements.txt
pip install --pre "dbt-$1"
fi

Expand Down
41 changes: 0 additions & 41 deletions tests/functional/cross_db_utils/fixture_dateadd.py

This file was deleted.

66 changes: 0 additions & 66 deletions tests/functional/cross_db_utils/fixture_datediff.py

This file was deleted.

40 changes: 0 additions & 40 deletions tests/functional/cross_db_utils/test_dateadd.py

This file was deleted.

24 changes: 0 additions & 24 deletions tests/functional/cross_db_utils/test_datediff.py

This file was deleted.

Loading