Skip to content

Commit

Permalink
Fix BQ dateadd and datediff
Browse files Browse the repository at this point in the history
  • Loading branch information
clrcrl committed Jan 11, 2021
1 parent 840302f commit 2f709a9
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# dbt-utils v0.7.0 (unreleased)
* Added optional `where` clause in `unique_combination_of_columns` test macro [#295](https://github.com/fishtown-analytics/dbt-utils/pull/295) [findinpath](https://github.com/findinpath)

## Breaking changes:
- [Potential breaking change] The BigQuery implementations of `dateadd` and `datediff` macros now use `timestamp` types instead of `datetime` types, to be more consistent with other warehouses ([#318](https://github.com/fishtown-analytics/dbt-utils/pull/318))

## Features
* Add new `accepted_range` test ([#276](https://github.com/fishtown-analytics/dbt-utils/pull/276) [@joellabes](https://github.com/joellabes))
* Make `expression_is_true` work as a column test (code originally in [#226](https://github.com/fishtown-analytics/dbt-utils/pull/226/) from [@elliottohara](https://github.com/elliottohara), merged via [#313])
Expand Down
10 changes: 0 additions & 10 deletions integration_tests/models/schema_tests/test_recency.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@

{% if target.type == 'postgres' %}

select
{{ dbt_utils.date_trunc('day', dbt_utils.current_timestamp()) }} as today

{% else %}

select
cast({{ dbt_utils.date_trunc('day', dbt_utils.current_timestamp()) }} as datetime) as today

{% endif %}
4 changes: 2 additions & 2 deletions macros/cross_db_utils/dateadd.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

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

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

Expand Down
6 changes: 3 additions & 3 deletions macros/cross_db_utils/datediff.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

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

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

Expand Down
2 changes: 1 addition & 1 deletion macros/schema_tests/recency.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ select
end as error_result
from {{model}}
where {{column_name}} >=
{{dbt_utils.dateadd(datepart, interval * -1, dbt_utils.current_timestamp())}}
{{ dbt_utils.dateadd(datepart, interval * -1, dbt_utils.current_timestamp()) }}

{% endmacro %}

0 comments on commit 2f709a9

Please sign in to comment.