Skip to content

Commit 1d9374c

Browse files
author
Michael Myaskovsky
committed
tests almost work
1 parent c0b0dbf commit 1d9374c

File tree

15 files changed

+142
-42
lines changed

15 files changed

+142
-42
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
{% macro clear_env() %}
2-
{% do elementary_tests.edr_drop_schema(elementary.target_database(), generate_schema_name()) %}
32
{% set database_name, schema_name = elementary.get_package_database_and_schema('elementary') %}
43
{% do elementary_tests.edr_drop_schema(database_name, schema_name) %}
4+
{% do elementary_tests.edr_drop_schema(elementary.target_database(), generate_schema_name()) %}
55
{% endmacro %}
66

77
{% macro edr_drop_schema(database_name, schema_name) %}
8+
{% do return(adapter.dispatch('edr_drop_schema', 'elementary_tests')(database_name, schema_name)) %}
9+
{% endmacro %}
10+
11+
{% macro default__edr_drop_schema(database_name, schema_name) %}
812
{% set schema_relation = api.Relation.create(database=database_name, schema=schema_name) %}
913
{% do dbt.drop_schema(schema_relation) %}
1014
{% do adapter.commit() %}
1115
{% endmacro %}
16+
17+
{% macro clickhouse__edr_drop_schema(database_name, schema_name) %}
18+
{% do run_query("DROP DATABASE IF EXISTS " ~ schema_name) %}
19+
{% do adapter.commit() %}
20+
{% endmacro %}

integration_tests/tests/dbt_project.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ def test(
151151
) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
152152
if columns and test_column:
153153
raise ValueError("You can't specify both 'columns' and 'test_column'.")
154-
155154
test_vars = test_vars or {}
156155
test_vars["elementary_enabled"] = elementary_enabled
157156

macros/edr/data_monitoring/anomaly_detection/get_anomaly_scores_query.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@
223223
)
224224

225225
select * from anomaly_scores
226-
227226
{% endset %}
228227
{{ return(anomaly_scores_query) }}
229228
{% endmacro %}

macros/edr/data_monitoring/data_monitors_configuration/get_buckets_configuration.sql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
{{ return(trunc_min_bucket_start_expr) }}
1515
{% endmacro %}
1616

17-
{# This macro can't be used without truncating to full buckets #}
17+
{# This macro cant be used without truncating to full buckets #}
1818
{% macro get_backfill_bucket_start(detection_end, backfill_days) %}
1919
{% do return((detection_end - modules.datetime.timedelta(backfill_days)).strftime("%Y-%m-%d 00:00:00")) %}
2020
{% endmacro %}
@@ -38,7 +38,6 @@
3838
{%- else %}
3939
{%- set data_monitoring_metrics_relation = elementary.get_elementary_relation('data_monitoring_metrics') %}
4040
{%- endif %}
41-
4241
{%- set regular_bucket_times_query %}
4342
with bucket_times as (
4443
select
@@ -100,11 +99,11 @@
10099

101100
{# We assume we should also cosider sources as incremental #}
102101
{% if force_metrics_backfill or not (elementary.is_incremental_model(elementary.get_model_graph_node(model_relation), source_included=true) or unit_test) %}
102+
103103
{%- set buckets = elementary.agate_to_dicts(elementary.run_query(regular_bucket_times_query))[0] %}
104104
{%- else %}
105105
{%- set buckets = elementary.agate_to_dicts(elementary.run_query(incremental_bucket_times_query))[0] %}
106106
{% endif %}
107-
108107
{%- if buckets %}
109108
{%- set min_bucket_start = elementary.edr_quote(buckets.get('min_bucket_start')) %}
110109
{%- set max_bucket_end = elementary.edr_quote(buckets.get('max_bucket_end')) %}

macros/edr/data_monitoring/monitors_query/column_monitoring_query.sql

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
{% do metric_name_to_type.update({metric.name: metric.type}) %}
1414
{% endfor %}
1515

16-
1716
with monitored_table as (
1817
select * from {{ monitored_table }}
1918
{% if metric_properties.where_expression %} where {{ metric_properties.where_expression }} {% endif %}
@@ -26,7 +25,6 @@
2625
where edr_bucket_start >= {{ elementary.edr_cast_as_timestamp(min_bucket_start) }}
2726
and edr_bucket_end <= {{ elementary.edr_cast_as_timestamp(max_bucket_end) }}
2827
),
29-
3028
filtered_monitored_table as (
3129
select {{ column_obj.quoted }},
3230
{%- if dimensions -%} {{ elementary.select_dimensions_columns(dimensions, "dimension") }}, {%- endif -%}
@@ -96,39 +94,10 @@
9694
),
9795

9896
column_metrics_unpivot as (
99-
100-
{%- if column_metrics %}
101-
{% for metric_name, metric_type in metric_name_to_type.items() %}
102-
select
103-
{{ elementary.const_as_string(column_obj.name) }} as edr_column_name,
104-
bucket_start,
105-
bucket_end,
106-
{% if timestamp_column %}
107-
{{ elementary.timediff("hour", "bucket_start", "bucket_end") }} as bucket_duration_hours,
108-
{% else %}
109-
{{ elementary.null_int() }} as bucket_duration_hours,
110-
{% endif %}
111-
{% if dimensions | length > 0 %}
112-
{{ elementary.const_as_string(elementary.join_list(dimensions, separator='; ')) }} as dimension,
113-
{{ elementary.list_concat_with_separator(prefixed_dimensions, separator='; ') }} as dimension_value,
114-
{% else %}
115-
{{ elementary.null_string() }} as dimension,
116-
{{ elementary.null_string() }} as dimension_value,
117-
{% endif %}
118-
{{ elementary.edr_cast_as_float(metric_type) }} as metric_value,
119-
{{ elementary.edr_cast_as_string(elementary.edr_quote(metric_name)) }} as metric_name,
120-
{{ elementary.edr_cast_as_string(elementary.edr_quote(metric_type)) }} as metric_type
121-
from column_metrics where {{ metric_type }} is not null
122-
{% if not loop.last %} union all {% endif %}
123-
{%- endfor %}
124-
{%- else %}
125-
{{ elementary.empty_table([('edr_column_name','string'),('bucket_start','timestamp'),('bucket_end','timestamp'),('bucket_duration_hours','int'),('dimension','string'),('dimension_value','string'),('metric_name','string'),('metric_type','string'),('metric_value','float')]) }}
126-
{%- endif %}
127-
97+
{{ elementary.get_column_metrics_unpivot_query(column_metrics, column_obj, metric_name_to_type, timestamp_column, dimensions) }}
12898
),
12999

130100
metrics_final as (
131-
132101
select
133102
{{ elementary.edr_cast_as_string(full_table_name_str) }} as full_table_name,
134103
edr_column_name as column_name,
@@ -143,7 +112,6 @@
143112
dimension_value,
144113
{{elementary.dict_to_quoted_json(metric_properties) }} as metric_properties
145114
from column_metrics_unpivot
146-
147115
)
148116

149117
select
@@ -174,6 +142,70 @@
174142

175143
{% endmacro %}
176144

145+
{% macro get_column_metrics_unpivot_query(column_metrics, column_obj, metric_name_to_type, timestamp_column, dimensions) %}
146+
{{ return(adapter.dispatch('get_column_metrics_unpivot_query', 'elementary')(column_metrics, column_obj, metric_name_to_type, timestamp_column, dimensions)) }}
147+
{% endmacro %}
148+
149+
{% macro default__get_column_metrics_unpivot_query(column_metrics, column_obj, metric_name_to_type, timestamp_column, dimensions) %}
150+
{%- if column_metrics %}
151+
{% for metric_name, metric_type in metric_name_to_type.items() %}
152+
select
153+
{{ elementary.const_as_string(column_obj.name) }} as edr_column_name,
154+
bucket_start,
155+
bucket_end,
156+
{% if timestamp_column %}
157+
{{ elementary.timediff("hour", "bucket_start", "bucket_end") }} as bucket_duration_hours,
158+
{% else %}
159+
{{ elementary.null_int() }} as bucket_duration_hours,
160+
{% endif %}
161+
{% if dimensions | length > 0 %}
162+
{{ elementary.const_as_string(elementary.join_list(dimensions, separator='; ')) }} as dimension,
163+
{{ elementary.list_concat_with_separator(prefixed_dimensions, separator='; ') }} as dimension_value,
164+
{% else %}
165+
{{ elementary.null_string() }} as dimension,
166+
{{ elementary.null_string() }} as dimension_value,
167+
{% endif %}
168+
{{ elementary.edr_cast_as_float(metric_type) }} as metric_value,
169+
{{ elementary.edr_cast_as_string(elementary.edr_quote(metric_name)) }} as metric_name,
170+
{{ elementary.edr_cast_as_string(elementary.edr_quote(metric_type)) }} as metric_type
171+
from column_metrics where {{ metric_type }} is not null
172+
{% if not loop.last %} union all {% endif %}
173+
{%- endfor %}
174+
{%- else %}
175+
{{ elementary.empty_table([('edr_column_name','string'),('bucket_start','timestamp'),('bucket_end','timestamp'),('bucket_duration_hours','int'),('dimension','string'),('dimension_value','string'),('metric_name','string'),('metric_type','string'),('metric_value','float')]) }}
176+
{%- endif %}
177+
{% endmacro %}
178+
179+
{% macro clickhouse__get_column_metrics_unpivot_query(column_metrics, column_obj, metric_name_to_type, timestamp_column, dimensions) %}
180+
{%- if column_metrics %}
181+
{% for metric_name, metric_type in metric_name_to_type.items() %}
182+
select
183+
{{ elementary.const_as_string(column_obj.name) }} as edr_column_name,
184+
bucket_start,
185+
bucket_end,
186+
{% if timestamp_column %}
187+
{{ elementary.timediff("hour", "bucket_start", "bucket_end") }} as bucket_duration_hours,
188+
{% else %}
189+
{{ elementary.null_int() }} as bucket_duration_hours,
190+
{% endif %}
191+
{% if dimensions | length > 0 %}
192+
{{ elementary.const_as_string(elementary.join_list(dimensions, separator='; ')) }} as dimension,
193+
{{ elementary.list_concat_with_separator(prefixed_dimensions, separator='; ') }} as dimension_value,
194+
{% else %}
195+
CAST(NULL AS Nullable(String)) as dimension,
196+
CAST(NULL AS Nullable(String)) as dimension_value,
197+
{% endif %}
198+
{{ elementary.edr_cast_as_float(metric_type) }} as metric_value,
199+
cast({{ elementary.edr_quote(metric_name) }} as Nullable({{ elementary.edr_type_string() }})) as metric_name,
200+
cast({{ elementary.edr_quote(metric_type) }} as Nullable({{ elementary.edr_type_string() }})) as metric_type
201+
from column_metrics where {{ metric_type }} is not null
202+
{% if not loop.last %} union all {% endif %}
203+
{%- endfor %}
204+
{%- else %}
205+
{{ elementary.empty_table([('edr_column_name','string'),('bucket_start','timestamp'),('bucket_end','timestamp'),('bucket_duration_hours','int'),('dimension','string'),('dimension_value','string'),('metric_name','string'),('metric_type','string'),('metric_value','float')]) }}
206+
{%- endif %}
207+
{% endmacro %}
208+
177209
{% macro select_dimensions_columns(dimension_columns, as_prefix="") %}
178210
{% set select_statements %}
179211
{%- for column in dimension_columns -%}

macros/edr/system/system_utils/buckets_cte.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
{{ return('') }}
1212
{% endmacro %}
1313

14+
{% macro clickhouse__complete_buckets_cte(time_bucket, bucket_end_expr, min_bucket_start_expr, max_bucket_end_expr) %}
15+
with numbers as (
16+
select arrayJoin(range(0, toUInt32({{ elementary.edr_datediff(min_bucket_start_expr, max_bucket_end_expr, time_bucket.period) }}))) as n
17+
)
18+
select
19+
{{ elementary.edr_timeadd(time_bucket.period, 'n', min_bucket_start_expr) }} as edr_bucket_start,
20+
{{ elementary.edr_timeadd(time_bucket.period, 'n + 1', min_bucket_start_expr) }} as edr_bucket_end
21+
from numbers
22+
where {{ elementary.edr_timeadd(time_bucket.period, 'n + 1', min_bucket_start_expr) }} <= {{ max_bucket_end_expr }}
23+
{% endmacro %}
1424

1525
{% macro spark__complete_buckets_cte(time_bucket, bucket_end_expr, min_bucket_start_expr, max_bucket_end_expr) %}
1626
{%- set complete_buckets_cte %}
@@ -133,3 +143,4 @@
133143
{%- endset %}
134144
{{ return(complete_buckets_cte) }}
135145
{% endmacro %}
146+

macros/edr/tests/test_all_columns_anomalies.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
{% if not model_relation %}
66
{{ exceptions.raise_compiler_error("Unsupported model: " ~ model ~ " (this might happen if you override 'ref' or 'source')") }}
77
{% endif %}
8-
98
{%- if elementary.is_ephemeral_model(model_relation) %}
109
{{ exceptions.raise_compiler_error("The test is not supported for ephemeral models, model name: {}".format(model_relation.identifier)) }}
1110
{%- endif %}
@@ -66,6 +65,7 @@
6665
column_name=column_name,
6766
metric_properties=metric_properties) %}
6867
{%- endif %}
68+
6969
{{ elementary.debug_log('min_bucket_start - ' ~ min_bucket_start) }}
7070
{{ elementary.test_log('start', full_table_name, column_name) }}
7171

@@ -89,7 +89,6 @@
8989
metric_names=all_columns_monitors,
9090
columns_only=true,
9191
metric_properties=metric_properties) %}
92-
9392
{% set anomaly_scores_test_table_relation = elementary.create_elementary_test_table(database_name, tests_schema_name, test_table_name, 'anomaly_scores', anomaly_scores_query) %}
9493

9594
{{- elementary.test_log('end', full_table_name, 'all columns') }}

macros/edr/tests/test_utils/clean_elementary_test_tables.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
{% do return(elementary.get_transactionless_clean_elementary_test_tables_queries(test_table_relations)) %}
3636
{% endmacro %}
3737

38+
{% macro clickhouse__get_clean_elementary_test_tables_queries(test_table_relations) %}
39+
{% do return(elementary.get_transactionless_clean_elementary_test_tables_queries(test_table_relations)) %}
40+
{% endmacro %}
41+
3842
{% macro athena__get_clean_elementary_test_tables_queries(test_table_relations) %}
3943
{% set queries = [] %}
4044
{% for test_relation in test_table_relations %}

macros/edr/tests/test_utils/create_elementary_test_table.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
identifier=temp_table_name,
1515
type='table') -%}
1616

17-
{# Create the table if it doesn't exist #}
17+
{# Create the table if it doesnt exist #}
1818
{%- do elementary.create_or_replace(false, temp_table_relation, sql_query) %}
1919

2020
{# Cache the test table for easy access later #}

macros/utils/cross_db_utils/datediff.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
{{ return(macro(first_date, second_date, date_part)) }}
1313
{% endmacro %}
1414

15+
{% macro clickhouse__edr_datediff(first_date, second_date, date_part) %}
16+
{%- set first_date_expr = elementary.edr_cast_as_timestamp(first_date) if first_date is string else first_date -%}
17+
{%- set second_date_expr = elementary.edr_cast_as_timestamp(second_date) if second_date is string else second_date -%}
18+
coalesce(dateDiff('{{ date_part }}', {{ first_date_expr }}, {{ second_date_expr }}), 0)::Nullable(Int32)
19+
{% endmacro %}
20+
1521
{% macro bigquery__edr_datediff(first_date, second_date, date_part) %}
1622
{%- if date_part | lower in ['second', 'minute', 'hour', 'day'] %}
1723
timestamp_diff({{ second_date }}, {{ first_date }}, {{ date_part }})

0 commit comments

Comments
 (0)