|
| 1 | +{# This is here for backwards compatibility only #} |
| 2 | + |
1 | 3 | {% macro datediff(first_date, second_date, datepart) %}
|
2 | 4 | {{ return(adapter.dispatch('datediff', 'dbt_utils')(first_date, second_date, datepart)) }}
|
3 | 5 | {% endmacro %}
|
4 | 6 |
|
5 |
| - |
6 |
| -{% macro default__datediff(first_date, second_date, datepart) -%} |
7 |
| - |
8 |
| - datediff( |
9 |
| - {{ datepart }}, |
10 |
| - {{ first_date }}, |
11 |
| - {{ second_date }} |
12 |
| - ) |
13 |
| - |
14 |
| -{%- endmacro %} |
15 |
| - |
16 |
| - |
17 |
| -{% macro bigquery__datediff(first_date, second_date, datepart) -%} |
18 |
| - |
19 |
| - datetime_diff( |
20 |
| - cast({{second_date}} as datetime), |
21 |
| - cast({{first_date}} as datetime), |
22 |
| - {{datepart}} |
23 |
| - ) |
24 |
| - |
25 |
| -{%- endmacro %} |
26 |
| - |
27 |
| -{% macro postgres__datediff(first_date, second_date, datepart) -%} |
28 |
| - |
29 |
| - {% if datepart == 'year' %} |
30 |
| - (date_part('year', ({{second_date}})::date) - date_part('year', ({{first_date}})::date)) |
31 |
| - {% elif datepart == 'quarter' %} |
32 |
| - ({{ dbt_utils.datediff(first_date, second_date, 'year') }} * 4 + date_part('quarter', ({{second_date}})::date) - date_part('quarter', ({{first_date}})::date)) |
33 |
| - {% elif datepart == 'month' %} |
34 |
| - ({{ dbt_utils.datediff(first_date, second_date, 'year') }} * 12 + date_part('month', ({{second_date}})::date) - date_part('month', ({{first_date}})::date)) |
35 |
| - {% elif datepart == 'day' %} |
36 |
| - (({{second_date}})::date - ({{first_date}})::date) |
37 |
| - {% elif datepart == 'week' %} |
38 |
| - ({{ dbt_utils.datediff(first_date, second_date, 'day') }} / 7 + case |
39 |
| - when date_part('dow', ({{first_date}})::timestamp) <= date_part('dow', ({{second_date}})::timestamp) then |
40 |
| - case when {{first_date}} <= {{second_date}} then 0 else -1 end |
41 |
| - else |
42 |
| - case when {{first_date}} <= {{second_date}} then 1 else 0 end |
43 |
| - end) |
44 |
| - {% elif datepart == 'hour' %} |
45 |
| - ({{ dbt_utils.datediff(first_date, second_date, 'day') }} * 24 + date_part('hour', ({{second_date}})::timestamp) - date_part('hour', ({{first_date}})::timestamp)) |
46 |
| - {% elif datepart == 'minute' %} |
47 |
| - ({{ dbt_utils.datediff(first_date, second_date, 'hour') }} * 60 + date_part('minute', ({{second_date}})::timestamp) - date_part('minute', ({{first_date}})::timestamp)) |
48 |
| - {% elif datepart == 'second' %} |
49 |
| - ({{ dbt_utils.datediff(first_date, second_date, 'minute') }} * 60 + floor(date_part('second', ({{second_date}})::timestamp)) - floor(date_part('second', ({{first_date}})::timestamp))) |
50 |
| - {% elif datepart == 'millisecond' %} |
51 |
| - ({{ dbt_utils.datediff(first_date, second_date, 'minute') }} * 60000 + floor(date_part('millisecond', ({{second_date}})::timestamp)) - floor(date_part('millisecond', ({{first_date}})::timestamp))) |
52 |
| - {% elif datepart == 'microsecond' %} |
53 |
| - ({{ dbt_utils.datediff(first_date, second_date, 'minute') }} * 60000000 + floor(date_part('microsecond', ({{second_date}})::timestamp)) - floor(date_part('microsecond', ({{first_date}})::timestamp))) |
54 |
| - {% else %} |
55 |
| - {{ exceptions.raise_compiler_error("Unsupported datepart for macro datediff in postgres: {!r}".format(datepart)) }} |
56 |
| - {% endif %} |
57 |
| - |
58 |
| -{%- endmacro %} |
59 |
| - |
60 |
| - |
61 |
| -{# redshift should use default instead of postgres #} |
62 |
| -{% macro redshift__datediff(first_date, second_date, datepart) -%} |
63 |
| - |
64 |
| - {{ return(dbt_utils.default__datediff(first_date, second_date, datepart)) }} |
65 |
| - |
66 |
| -{%- endmacro %} |
| 7 | +{% macro default__datediff(first_date, second_date, datepart) %} |
| 8 | + {{ return(adapter.dispatch('datediff', 'dbt')(first_date, second_date, datepart)) }} |
| 9 | +{% endmacro %} |
0 commit comments