Skip to content

Commit d6cc6a3

Browse files
committed
only anomaly dim values
1 parent 3fbc7b5 commit d6cc6a3

File tree

3 files changed

+22
-36
lines changed

3 files changed

+22
-36
lines changed

integration_tests/tests/test_dimension_anomalies.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,6 @@ def test_anomalous_dimension_anomalies(test_id: str, dbt_project: DbtProject):
8787
for superhero in ["Superman", "Superman", "Superman", "Spiderman"]
8888
]
8989

90-
test_result = dbt_project.test(test_id, DBT_TEST_NAME, DBT_TEST_ARGS, data=data)
91-
92-
assert test_result["status"] == "pass"
93-
94-
anomaly_test_points = get_latest_anomaly_test_points(dbt_project, test_id)
95-
assert len(anomaly_test_points) == 0
96-
9790
data += [
9891
{
9992
TIMESTAMP_COLUMN: cur_date.strftime(DATE_FORMAT),
@@ -108,23 +101,17 @@ def test_anomalous_dimension_anomalies(test_id: str, dbt_project: DbtProject):
108101

109102
anomaly_test_points = get_latest_anomaly_test_points(dbt_project, test_id)
110103

111-
# Only test with anomalies are stored in the test points
104+
# Only dimension values with anomalies are stored in the test points
112105
dimension_values = set([x["dimension_value"] for x in anomaly_test_points])
113-
assert len(dimension_values) == 2
114-
assert "Superman" in dimension_values
115-
assert "Spiderman" in dimension_values
116106

117107
superman_anomaly_test_points = [
118108
x for x in anomaly_test_points if x["dimension_value"] == "Superman"
119109
]
120-
assert len(superman_anomaly_test_points) == 13
121-
assert any(x["is_anomalous"] for x in superman_anomaly_test_points)
122110

123-
spiderman_anomaly_test_points = [
124-
x for x in anomaly_test_points if x["dimension_value"] == "Spiderman"
125-
]
126-
assert len(spiderman_anomaly_test_points) == 13
127-
assert not any(x["is_anomalous"] for x in spiderman_anomaly_test_points)
111+
assert len(dimension_values) == 1
112+
assert "Superman" in dimension_values
113+
assert len(anomaly_test_points) == len(superman_anomaly_test_points)
114+
assert any(x["is_anomalous"] for x in superman_anomaly_test_points)
128115

129116

130117
# Anomalies currently not supported on ClickHouse

macros/edr/tests/test_dimension_anomalies.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
detection_period=detection_period,
4141
training_period=training_period,
4242
exclude_final_results=exclude_final_results) %}
43-
43+
4444
{%- if not test_configuration %}
4545
{{ exceptions.raise_compiler_error("Failed to create test configuration dict for test `{}`".format(test_table_name)) }}
4646
{%- endif %}
@@ -71,7 +71,7 @@
7171
{{ elementary.test_log('end', full_table_name) }}
7272

7373
{% set flattened_test = elementary.flatten_test(context["model"]) %}
74-
{% set anomalous_rows_sql = elementary.get_anomaly_query(flattened_test) %}
74+
{% set anomalous_rows_sql = elementary.get_anomaly_query_for_dimension_anomalies(flattened_test) %}
7575
{% do elementary.store_metrics_table_in_cache() %}
7676
{% do elementary.store_anomaly_test_results(flattened_test, anomalous_rows_sql) %}
7777

macros/edr/tests/test_utils/get_anomaly_query.sql

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
{%- macro get_anomaly_query(flattened_test=none) -%}
2-
{%- set anomaly_scores_query -%}
2+
{%- set query -%}
33
select * from ({{ elementary.get_read_anomaly_scores_query(flattened_test) }}) results
4-
{%- endset -%}
5-
6-
{% set anomalies_count_query -%}
7-
select count(*) as anomalies_count
8-
from ({{ anomaly_scores_query }})
94
where is_anomalous = true
105
{%- endset -%}
6+
{{- return(query) -}}
7+
{%- endmacro -%}
118

12-
{% set non_anomalies_query -%}
13-
{{ anomaly_scores_query }}
14-
where is_anomalous = true
9+
{%- macro get_anomaly_query_for_dimension_anomalies(flattened_test=none) -%}
10+
{%- set anomaly_scores_query -%}
11+
select distinct dimension_value
12+
from ({{ elementary.get_read_anomaly_scores_query(flattened_test) }}) results
13+
where dimension is not null
14+
and dimension_value is not null
15+
and is_anomalous = true
1516
{%- endset -%}
1617

17-
{% set anomalies_count_result = elementary.agate_to_dicts(elementary.run_query(anomalies_count_query)) %}
18-
{% set anomalies_count = anomalies_count_result[0]['anomalies_count'] %}
18+
{% set anomalous_dimension_values_query -%}
19+
select * from ({{ elementary.get_read_anomaly_scores_query(flattened_test) }}) results
20+
where dimension_value in ({{ anomaly_scores_query }})
21+
{%- endset -%}
1922

20-
{% if anomalies_count > 0 %}
21-
{{- return(anomaly_scores_query) -}}
22-
{% else %}
23-
{{- return(non_anomalies_query) -}}
24-
{% endif %}
23+
{{- return(anomalous_dimension_values_query) -}}
2524
{%- endmacro -%}
2625

2726
{% macro get_read_anomaly_scores_query(flattened_test=none) %}

0 commit comments

Comments
 (0)