Skip to content

Commit 2721c45

Browse files
committed
dremio support in anomaly tests - WIP
1 parent 1671dc0 commit 2721c45

File tree

9 files changed

+80
-7
lines changed

9 files changed

+80
-7
lines changed

macros/edr/data_monitoring/anomaly_detection/get_anomaly_scores_query.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
{%- set bucket_seasonality_expr = elementary.const_as_text('no_seasonality') %}
2929
{%- endif %}
3030
{%- set detection_end = elementary.get_detection_end(test_configuration.detection_delay) %}
31+
{%- set detection_end_expr = elementary.edr_cast_as_timestamp(elementary.edr_datetime_to_sql(detection_end)) %}
3132
{%- set min_bucket_start_expr = elementary.get_trunc_min_bucket_start_expr(detection_end, metric_properties, test_configuration.days_back) %}
3233

3334
{# For timestamped tests, this will be the bucket start, and for non-timestamped tests it will be the
@@ -39,9 +40,9 @@
3940
with buckets as (
4041
select edr_bucket_start, edr_bucket_end
4142
from ({{ elementary.complete_buckets_cte(metric_properties, min_bucket_start_expr,
42-
elementary.edr_quote(detection_end)) }}) results
43-
where edr_bucket_start >= {{ elementary.edr_cast_as_timestamp(min_bucket_start_expr) }}
44-
and edr_bucket_end <= {{ elementary.edr_cast_as_timestamp(elementary.edr_quote(detection_end)) }}
43+
detection_end_expr) }}) results
44+
where edr_bucket_start >= {{ min_bucket_start_expr }}
45+
and edr_bucket_end <= {{ detection_end_expr }}
4546
),
4647
{% else %}
4748
with

macros/edr/data_monitoring/data_monitors_configuration/get_buckets_configuration.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{% macro get_detection_end(detection_delay) %}
22
{% if not detection_delay %}
3-
{% do return(elementary.get_run_started_at()) %}
3+
{% do return(elementary.get_run_started_at().replace(microsecond=0)) %}
44
{% endif %}
55

66
{%- set kwargs = {detection_delay.period+'s': detection_delay.count} %}
7-
{%- set detection_end = elementary.get_run_started_at() - modules.datetime.timedelta(**kwargs) %}
7+
{%- set detection_end = elementary.get_run_started_at().replace(microsecond=0) - modules.datetime.timedelta(**kwargs) %}
88
{% do return(detection_end) %}
99
{% endmacro %}
1010

@@ -105,8 +105,8 @@
105105
{%- set buckets = elementary.agate_to_dicts(elementary.run_query(incremental_bucket_times_query))[0] %}
106106
{% endif %}
107107
{%- if buckets %}
108-
{%- set min_bucket_start = elementary.edr_quote(buckets.get('min_bucket_start')) %}
109-
{%- set max_bucket_end = elementary.edr_quote(buckets.get('max_bucket_end')) %}
108+
{%- set min_bucket_start = elementary.edr_datetime_to_sql(buckets.get('min_bucket_start')) %}
109+
{%- set max_bucket_end = elementary.edr_datetime_to_sql(buckets.get('max_bucket_end')) %}
110110
{{ return([min_bucket_start, max_bucket_end]) }}
111111
{%- else %}
112112
{{ exceptions.raise_compiler_error("Failed to calc test buckets min and max") }}

macros/edr/system/system_utils/buckets_cte.sql

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,21 @@
144144
{{ return(complete_buckets_cte) }}
145145
{% endmacro %}
146146

147+
{% macro dremio__complete_buckets_cte(time_bucket, bucket_end_expr, min_bucket_start_expr, max_bucket_end_expr) %}
148+
{%- set complete_buckets_cte %}
149+
with integers as (
150+
select (row_number() over (order by t1.val, t2.val, t3.val, t4.val)) - 1 as num
151+
from (values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) t1(val)
152+
cross join (values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) t2(val)
153+
cross join (values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) t3(val)
154+
cross join (values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) t4(val)
155+
)
156+
select
157+
{{ elementary.edr_timeadd(time_bucket.period, 'num * ' ~ time_bucket.count, min_bucket_start_expr) }} as edr_bucket_start,
158+
{{ elementary.edr_timeadd(time_bucket.period, '(num + 1) * ' ~ time_bucket.count, min_bucket_start_expr) }} as edr_bucket_end
159+
from integers
160+
where {{ elementary.edr_timeadd(time_bucket.period, '(num + 1) * ' ~ time_bucket.count, min_bucket_start_expr) }} <= {{ max_bucket_end_expr }}
161+
{%- endset %}
162+
{{ return(complete_buckets_cte) }}
163+
{% endmacro %}
164+

macros/utils/cross_db_utils/current_timestamp.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,12 @@
6969
{% macro trino__edr_current_timestamp_in_utc() -%}
7070
cast(current_timestamp at time zone 'UTC' as timestamp(6))
7171
{%- endmacro -%}
72+
73+
{% macro dremio__edr_current_timestamp() -%}
74+
CURRENT_TIMESTAMP()
75+
{%- endmacro -%}
76+
77+
{% macro dremio__edr_current_timestamp_in_utc() -%}
78+
{# Dremio timezones are always in UTC #}
79+
CURRENT_TIMESTAMP()
80+
{%- endmacro -%}

macros/utils/cross_db_utils/datediff.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,18 @@
156156
{% endif %}
157157
{{ return(macro(elementary.edr_cast_as_timestamp(first_date), elementary.edr_cast_as_timestamp(second_date), date_part)) }}
158158
{% endmacro %}
159+
160+
{% macro dremio__edr_datediff(first_date, second_date, date_part) %}
161+
{% set macro = dbt.datediff or dbt_utils.datediff %}
162+
{% if not macro %}
163+
{{ exceptions.raise_compiler_error("Did not find a `datediff` macro.") }}
164+
{% endif %}
165+
166+
{% set sql = macro(elementary.edr_cast_as_timestamp(first_date), elementary.edr_cast_as_timestamp(second_date), date_part) %}
167+
168+
{# Hack - dbt-dremio implements this macro as a select statement (which seems to be necessary), but in order
169+
for it to really work we wrap it in parentheses and remove ; if it is there #}
170+
{% set sql = '(' ~ sql.strip().replace(';', '') ~ ')' %}
171+
172+
{% do return(sql) %}
173+
{% endmacro %}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{% macro edr_datetime_to_sql(dt) %}
2+
{% do return(adapter.dispatch("edr_datetime_to_sql", "elementary")(dt)) %}
3+
{% endmacro %}
4+
5+
{% macro default__edr_datetime_to_sql(dt) %}
6+
{% do return(elementary.edr_quote(dt)) %}
7+
{% endmacro %}
8+
9+
{% macro dremio__edr_datetime_to_sql(dt) %}
10+
{% if dt is string %}
11+
{% set dt = modules.datetime.datetime.fromisoformat(dt) %}
12+
{% endif %}
13+
{% do return(elementary.edr_quote(dt.strftime(elementary.get_time_format()))) %}
14+
{% endmacro %}

macros/utils/cross_db_utils/timeadd.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@
3333
{% macro trino__edr_timeadd(date_part, number, timestamp_expression) %}
3434
date_add('{{ date_part }}', {{ elementary.edr_cast_as_int(number) }}, {{ elementary.edr_cast_as_timestamp(timestamp_expression) }})
3535
{% endmacro %}
36+
37+
{% macro dremio__edr_timeadd(date_part, number, timestamp_expression) %}
38+
timestampadd({{ date_part }}, {{ elementary.edr_cast_as_int(number) }}, {{ elementary.edr_cast_as_timestamp(timestamp_expression) }})
39+
{% endmacro %}

macros/utils/data_types/cast_column.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
)
3030
{%- endmacro -%}
3131

32+
{%- macro dremio__edr_cast_as_timestamp(timestamp_field) -%}
33+
cast({{ timestamp_field }} as {{ elementary.edr_type_timestamp() }})
34+
{%- endmacro -%}
35+
3236
{%- macro edr_cast_as_float(column) -%}
3337
cast({{ column }} as {{ elementary.edr_type_float() }})
3438
{%- endmacro -%}
@@ -85,6 +89,10 @@
8589
)
8690
{%- endmacro -%}
8791

92+
{%- macro dremio__edr_cast_as_date(timestamp_field) -%}
93+
cast({{ timestamp_field }} as {{ elementary.edr_type_date() }})
94+
{%- endmacro -%}
95+
8896

8997
{%- macro const_as_text(string) -%}
9098
{{ return(adapter.dispatch('const_as_text', 'elementary')(string)) }}

macros/utils/data_types/data_type.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,7 @@
156156
{% macro trino__edr_type_timestamp() %}
157157
timestamp(6)
158158
{% endmacro %}
159+
160+
{% macro dremio__edr_type_timestamp() %}
161+
timestamp
162+
{% endmacro %}

0 commit comments

Comments
 (0)