Skip to content

Commit c5c689c

Browse files
committed
bugfix for anomaly scores logic due to usage of stddev_pop
1 parent cec4698 commit c5c689c

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
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
@@ -190,7 +190,8 @@
190190
metric_name,
191191
case
192192
when training_stddev is null then null
193-
when training_stddev = 0 then 0
193+
when training_set_size = 1 then null -- Single value case - no historical context for anomaly detection
194+
when training_stddev = 0 then 0 -- Stationary data case - valid, all values are identical
194195
else (metric_value - training_avg) / (training_stddev)
195196
end as anomaly_score,
196197
{{ test_configuration.anomaly_sensitivity }} as anomaly_score_threshold,
@@ -202,12 +203,12 @@
202203

203204
{% set limit_values = elementary.get_limit_metric_values(test_configuration) %}
204205
case
205-
when training_stddev is null then null
206+
when training_stddev is null or training_set_size = 1 then null
206207
when {{ limit_values.min_metric_value }} > 0 or metric_name in {{ elementary.to_sql_list(elementary.get_negative_value_supported_metrics()) }} then {{ limit_values.min_metric_value }}
207208
else 0
208209
end as min_metric_value,
209210
case
210-
when training_stddev is null then null
211+
when training_stddev is null or training_set_size = 1 then null
211212
else {{ limit_values.max_metric_value }}
212213
end as max_metric_value,
213214
training_avg,

macros/edr/data_monitoring/monitors/column_numeric_monitors.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
{%- endmacro %}
3636

3737
{% macro dremio__standard_deviation(column_name) -%}
38+
-- Dremio's stddev in window functions can raise division by zero with single values
39+
-- stddev_pop returns 0 for single values instead of raising an error
40+
-- We'll handle the single-value case in the anomaly detection logic using training_set_size
3841
stddev_pop(cast({{ column_name }} as {{ elementary.edr_type_float() }}))
3942
{%- endmacro %}
4043

models/edr/data_monitoring/anomaly_detection/metrics_anomaly_score.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ metrics_anomaly_score as (
4646
metric_name,
4747
case
4848
when training_stddev is null then null
49-
when training_stddev = 0 then 0
49+
when training_set_size = 1 then null -- Single value case - no historical context for anomaly detection
50+
when training_stddev = 0 then 0 -- Stationary data case - valid, all values are identical
5051
else (metric_value - training_avg) / (training_stddev)
5152
end as anomaly_score,
5253
metric_value as latest_metric_value,

0 commit comments

Comments
 (0)