Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(alerts): correct MAD detector training-window and offset bugs (#76168)

posthog[bot]c1170533eb17
diff --git a/ee/clickhouse/models/test/__snapshots__/test_cohort.ambr b/ee/clickhouse/models/test/__snapshots__/test_cohort.ambr
index a5837d0ca386..0badb9672750 100644
--- a/ee/clickhouse/models/test/__snapshots__/test_cohort.ambr
+++ b/ee/clickhouse/models/test/__snapshots__/test_cohort.ambr
@@ -222,102 +222,6 @@
join_algorithm = 'auto'
'''
# ---
-# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator[new_events_schema.1]
- '''
- /* cohort_calculation:cohort_calc:00000000 */
- INSERT INTO cohortpeople
- SELECT id,
- 99999 as cohort_id,
- 99999 as team_id,
- 1 AS sign,
- 3 AS version
- FROM ((
- (SELECT source.id AS id
- FROM
- (SELECT actor_id AS actor_id,
- count() AS event_count,
- groupUniqArray(distinct_id) AS event_distinct_ids,
- max(timestamp) AS last_seen,
- actor_id AS id
- FROM
- (SELECT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS actor_id,
- toTimeZone(e.timestamp, 'UTC') AS timestamp,
- e.uuid AS uuid,
- e.distinct_id AS distinct_id
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(timestamp, toDateTime64('today', 6, 'UTC')), equals(e.event, '$pageview')))
- GROUP BY actor_id) AS source
- ORDER BY source.id ASC
- LIMIT 1000000000 SETTINGS optimize_aggregation_in_order=1,
- join_algorithm='auto'))
- EXCEPT (
- (SELECT cohort_people.person_id AS id
- FROM
- (SELECT DISTINCT cohortpeople.person_id AS person_id,
- cohortpeople.cohort_id AS cohort_id,
- cohortpeople.team_id AS team_id
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), in(tuple(cohortpeople.cohort_id, cohortpeople.version), [(99999, 0)]))) AS cohort_people
- WHERE and(equals(cohort_people.cohort_id, 99999), equals(cohort_people.team_id, 99999))
- LIMIT 1000000000))) as person SETTINGS optimize_aggregation_in_order = 1,
- join_algorithm = 'auto'
- '''
-# ---
-# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator[new_events_schema]
- '''
- /* cohort_calculation:cohort_calc:00000000 */
- INSERT INTO cohortpeople
- SELECT id,
- 99999 as cohort_id,
- 99999 as team_id,
- 1 AS sign,
- 2 AS version
- FROM ((
- (SELECT source.id AS id
- FROM
- (SELECT actor_id AS actor_id,
- count() AS event_count,
- groupUniqArray(distinct_id) AS event_distinct_ids,
- max(timestamp) AS last_seen,
- actor_id AS id
- FROM
- (SELECT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS actor_id,
- toTimeZone(e.timestamp, 'UTC') AS timestamp,
- e.uuid AS uuid,
- e.distinct_id AS distinct_id
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(timestamp, toDateTime64('today', 6, 'UTC')), equals(e.event, '$pageview')))
- GROUP BY actor_id) AS source
- ORDER BY source.id ASC
- LIMIT 1000000000 SETTINGS optimize_aggregation_in_order=1,
- join_algorithm='auto'))
- EXCEPT (
- (SELECT cohort_people.person_id AS id
- FROM
- (SELECT DISTINCT cohortpeople.person_id AS person_id,
- cohortpeople.cohort_id AS cohort_id,
- cohortpeople.team_id AS team_id
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), in(tuple(cohortpeople.cohort_id, cohortpeople.version), [(99999, 0)]))) AS cohort_people
- WHERE and(equals(cohort_people.cohort_id, 99999), equals(cohort_people.team_id, 99999))
- LIMIT 1000000000))) as person SETTINGS optimize_aggregation_in_order = 1,
- join_algorithm = 'auto'
- '''
-# ---
# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator_and_no_precalculation
'''
SELECT uuid,
@@ -372,60 +276,6 @@
LIMIT 1000000000)))
'''
# ---
-# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator_and_no_precalculation[new_events_schema]
- '''
- SELECT uuid,
- distinct_id
- FROM events_json
- WHERE team_id = 99999
- AND (distinct_id IN
- (SELECT distinct_id
- FROM
- (SELECT person_distinct_ids.distinct_id AS distinct_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id,
- person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE in(person_distinct_ids.person_id, ((
- (SELECT source.id AS id
- FROM
- (SELECT actor_id AS actor_id,
- count() AS event_count,
- groupUniqArray(distinct_id) AS event_distinct_ids,
- max(timestamp) AS last_seen,
- actor_id AS id
- FROM
- (SELECT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS actor_id,
- toTimeZone(e.timestamp, 'UTC') AS timestamp,
- e.uuid AS uuid,
- e.distinct_id AS distinct_id
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(timestamp, toDateTime64('today', 6, 'UTC')), equals(e.event, '$pageview')))
- GROUP BY actor_id) AS source
- ORDER BY source.id ASC SETTINGS optimize_aggregation_in_order=1,
- join_algorithm='auto'))
- EXCEPT (
- (SELECT cohort_people.person_id AS id
- FROM
- (SELECT DISTINCT cohortpeople.person_id AS person_id,
- cohortpeople.cohort_id AS cohort_id,
- cohortpeople.team_id AS team_id
- FROM cohortpeople
- WHERE 0) AS cohort_people
- WHERE and(equals(cohort_people.cohort_id, 99999), equals(cohort_people.team_id, 99999))))))
- LIMIT 1000000000)))
- '''
-# ---
# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator_for_behavioural_cohorts
'''
/* cohort_calculation:cohort_calc:00000000 */
@@ -602,182 +452,6 @@
join_algorithm = 'auto'
'''
# ---
-# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator_for_behavioural_cohorts[new_events_schema.1]
- '''
- /* cohort_calculation:cohort_calc:00000000 */
- INSERT INTO cohortpeople
- SELECT id,
- 99999 as cohort_id,
- 99999 as team_id,
- 1 AS sign,
- 3 AS version
- FROM (
- (SELECT source.id AS id
- FROM
- (SELECT actor_id AS actor_id,
- count() AS event_count,
- groupUniqArray(distinct_id) AS event_distinct_ids,
- actor_id AS id
- FROM
- (SELECT min(toTimeZone(e.timestamp, 'UTC')) AS min_timestamp,
- minIf(toTimeZone(e.timestamp, 'UTC'), greaterOrEquals(toTimeZone(e.timestamp, 'UTC'), toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))) AS min_timestamp_with_condition,
- if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS actor_id,
- argMin(e.uuid, toTimeZone(e.timestamp, 'UTC')) AS uuid,
- argMin(e.distinct_id, toTimeZone(e.timestamp, 'UTC')) AS distinct_id
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), lessOrEquals(e.timestamp, toDateTime64('today', 6, 'UTC')), equals(e.event, 'signup'))
- GROUP BY if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)
- HAVING and(equals(min_timestamp, min_timestamp_with_condition), notEquals(min_timestamp, fromUnixTimestamp(0))))
- GROUP BY actor_id) AS source
- ORDER BY source.id ASC
- LIMIT 1000000000 SETTINGS optimize_aggregation_in_order=1,
- join_algorithm='auto')) as person SETTINGS optimize_aggregation_in_order = 1,
- join_algorithm = 'auto'
- '''
-# ---
-# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator_for_behavioural_cohorts[new_events_schema.2]
- '''
- /* cohort_calculation:cohort_calc:00000000 */
- INSERT INTO cohortpeople
- SELECT id,
- 99999 as cohort_id,
- 99999 as team_id,
- 1 AS sign,
- 2 AS version
- FROM ((
- (SELECT source.id AS id
- FROM
- (SELECT actor_id AS actor_id,
- count() AS event_count,
- groupUniqArray(distinct_id) AS event_distinct_ids,
- max(timestamp) AS last_seen,
- actor_id AS id
- FROM
- (SELECT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS actor_id,
- toTimeZone(e.timestamp, 'UTC') AS timestamp,
- e.uuid AS uuid,
- e.distinct_id AS distinct_id
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(timestamp, toDateTime64('today', 6, 'UTC')), equals(e.event, '$pageview')))
- GROUP BY actor_id) AS source
- ORDER BY source.id ASC
- LIMIT 1000000000 SETTINGS optimize_aggregation_in_order=1,
- join_algorithm='auto'))
- EXCEPT (
- (SELECT cohort_people.person_id AS id
- FROM
- (SELECT DISTINCT cohortpeople.person_id AS person_id,
- cohortpeople.cohort_id AS cohort_id,
- cohortpeople.team_id AS team_id
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), in(tuple(cohortpeople.cohort_id, cohortpeople.version), [(99999, 0)]))) AS cohort_people
- WHERE and(equals(cohort_people.cohort_id, 99999), equals(cohort_people.team_id, 99999))
- LIMIT 1000000000))) as person SETTINGS optimize_aggregation_in_order = 1,
- join_algorithm = 'auto'
- '''
-# ---
-# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator_for_behavioural_cohorts[new_events_schema.3]
- '''
- /* cohort_calculation:cohort_calc:00000000 */
- INSERT INTO cohortpeople
- SELECT id,
- 99999 as cohort_id,
- 99999 as team_id,
- 1 AS sign,
- 3 AS version
- FROM ((
- (SELECT source.id AS id
- FROM
- (SELECT actor_id AS actor_id,
- count() AS event_count,
- groupUniqArray(distinct_id) AS event_distinct_ids,
- max(timestamp) AS last_seen,
- actor_id AS id
- FROM
- (SELECT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS actor_id,
- toTimeZone(e.timestamp, 'UTC') AS timestamp,
- e.uuid AS uuid,
- e.distinct_id AS distinct_id
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), lessOrEquals(timestamp, toDateTime64('today', 6, 'UTC')), equals(e.event, '$pageview')))
- GROUP BY actor_id) AS source
- ORDER BY source.id ASC
- LIMIT 1000000000 SETTINGS optimize_aggregation_in_order=1,
- join_algorithm='auto'))
- EXCEPT (
- (SELECT cohort_people.person_id AS id
- FROM
- (SELECT DISTINCT cohortpeople.person_id AS person_id,
- cohortpeople.cohort_id AS cohort_id,
- cohortpeople.team_id AS team_id
- FROM cohortpeople
- WHERE and(equals(cohortpeople.team_id, 99999), in(tuple(cohortpeople.cohort_id, cohortpeople.version), [(99999, 0)]))) AS cohort_people
- WHERE and(equals(cohort_people.cohort_id, 99999), equals(cohort_people.team_id, 99999))
- LIMIT 1000000000))) as person SETTINGS optimize_aggregation_in_order = 1,
- join_algorithm = 'auto'
- '''
-# ---
-# name: TestCohort.test_cohortpeople_with_not_in_cohort_operator_for_behavioural_cohorts[new_events_schema]
- '''
- /* cohort_calculation:cohort_calc:00000000 */
- INSERT INTO cohortpeople
- SELECT id,
- 99999 as cohort_id,
- 99999 as team_id,
- 1 AS sign,
- 2 AS version
- FROM (
- (SELECT source.id AS id
- FROM
- (SELECT actor_id AS actor_id,
- count() AS event_count,
- groupUniqArray(distinct_id) AS event_distinct_ids,
- actor_id AS id
- FROM
- (SELECT min(toTimeZone(e.timestamp, 'UTC')) AS min_timestamp,
- minIf(toTimeZone(e.timestamp, 'UTC'), greaterOrEquals(toTimeZone(e.timestamp, 'UTC'), toDateTime64('explicit_redacted_timestamp', 6, 'UTC'))) AS min_timestamp_with_condition,
- if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS actor_id,
- argMin(e.uuid, toTimeZone(e.timestamp, 'UTC')) AS uuid,
- argMin(e.distinct_id, toTimeZone(e.timestamp, 'UTC')) AS distinct_id
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), lessOrEquals(e.timestamp, toDateTime64('today', 6, 'UTC')), equals(e.event, 'signup'))
- GROUP BY if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)
- HAVING and(equals(min_timestamp, min_timestamp_with_condition), notEquals(min_timestamp, fromUnixTimestamp(0))))
- GROUP BY actor_id) AS source
- ORDER BY source.id ASC
- LIMIT 1000000000 SETTINGS optimize_aggregation_in_order=1,
- join_algorithm='auto')) as person SETTINGS optimize_aggregation_in_order = 1,
- join_algorithm = 'auto'
- '''
-# ---
# name: TestCohort.test_static_cohort_precalculated
'''
SELECT distinct_id
diff --git a/ee/clickhouse/models/test/__snapshots__/test_property.ambr b/ee/clickhouse/models/test/__snapshots__/test_property.ambr
index 263b5d3453d7..49f279860e40 100644
--- a/ee/clickhouse/models/test/__snapshots__/test_property.ambr
+++ b/ee/clickhouse/models/test/__snapshots__/test_property.ambr
@@ -9,22 +9,6 @@
OR (has(['val_2'], replaceRegexpAll(JSONExtractRaw(properties, 'attr_1'), '^"|"$', ''))))
'''
# ---
-# name: TestPropFormat.test_parse_groups[new_events_schema]
- '''
- SELECT uuid
- FROM events_json
- WHERE team_id = 99999
- AND ((has(['val_1'], replaceRegexpAll(ifNull(if(toJSONString(properties.^attr_1) != '{}', toJSONString(properties.^attr_1), if(startsWith(dynamicType(properties.attr_1), 'Array')
- OR startsWith(dynamicType(properties.attr_1), 'Map')
- OR startsWith(dynamicType(properties.attr_1), 'Tuple'), toJSONString(properties.attr_1), if(startsWith(dynamicType(properties.attr_1), 'DateTime'), replaceOne(toString(properties.attr_1), ' ', 'T'), toString(properties.attr_1)))), ''), '^"|"$', ''))
- AND has(['val_2'], replaceRegexpAll(ifNull(if(toJSONString(properties.^attr_2) != '{}', toJSONString(properties.^attr_2), if(startsWith(dynamicType(properties.attr_2), 'Array')
- OR startsWith(dynamicType(properties.attr_2), 'Map')
- OR startsWith(dynamicType(properties.attr_2), 'Tuple'), toJSONString(properties.attr_2), if(startsWith(dynamicType(properties.attr_2), 'DateTime'), replaceOne(toString(properties.attr_2), ' ', 'T'), toString(properties.attr_2)))), ''), '^"|"$', '')))
- OR (has(['val_2'], replaceRegexpAll(ifNull(if(toJSONString(properties.^attr_1) != '{}', toJSONString(properties.^attr_1), if(startsWith(dynamicType(properties.attr_1), 'Array')
- OR startsWith(dynamicType(properties.attr_1), 'Map')
- OR startsWith(dynamicType(properties.attr_1), 'Tuple'), toJSONString(properties.attr_1), if(startsWith(dynamicType(properties.attr_1), 'DateTime'), replaceOne(toString(properties.attr_1), ' ', 'T'), toString(properties.attr_1)))), ''), '^"|"$', ''))))
- '''
-# ---
# name: TestPropFormat.test_parse_groups_persons
'''
SELECT uuid
@@ -72,53 +56,6 @@
WHERE has(['[email protected]'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', ''))))))
'''
# ---
-# name: TestPropFormat.test_parse_groups_persons[new_events_schema]
- '''
- SELECT uuid
- FROM events_json
- WHERE team_id = 99999
- AND ((distinct_id IN
- (SELECT distinct_id
- FROM
- (SELECT distinct_id,
- argMax(person_id, version) as person_id
- FROM person_distinct_id2
- WHERE team_id = 99999
- GROUP BY distinct_id
- HAVING argMax(is_deleted, version) = 0)
- WHERE person_id IN
- (SELECT id
- FROM
- (SELECT id,
- argMax(properties, person._timestamp) as properties,
- max(is_deleted) as is_deleted
- FROM person
- WHERE team_id = 99999
- GROUP BY id
- HAVING is_deleted = 0)
- WHERE has(['[email protected]'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', '')))))
- OR (distinct_id IN
- (SELECT distinct_id
- FROM
- (SELECT distinct_id,
- argMax(person_id, version) as person_id
- FROM person_distinct_id2
- WHERE team_id = 99999
- GROUP BY distinct_id
- HAVING argMax(is_deleted, version) = 0)
- WHERE person_id IN
- (SELECT id
- FROM
- (SELECT id,
- argMax(properties, person._timestamp) as properties,
- max(is_deleted) as is_deleted
- FROM person
- WHERE team_id = 99999
- GROUP BY id
- HAVING is_deleted = 0)
- WHERE has(['[email protected]'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', ''))))))
- '''
-# ---
# name: test_parse_groups_persons_edge_case_with_single_filter
tuple(
'AND ( has(%(vglobalperson_0)s, "pmat_email"))',
diff --git a/ee/clickhouse/queries/test/__snapshots__/test_related_actors_query.ambr b/ee/clickhouse/queries/test/__snapshots__/test_related_actors_query.ambr
index 3e2c7d207f1e..d60304ead56e 100644
--- a/ee/clickhouse/queries/test/__snapshots__/test_related_actors_query.ambr
+++ b/ee/clickhouse/queries/test/__snapshots__/test_related_actors_query.ambr
@@ -27,34 +27,6 @@
use_hive_partitioning=0
'''
# ---
-# name: TestRelatedGroupsQuery.test_query[new_events_schema]
- '''
- SELECT DISTINCT tuples.1 AS group_type_index,
- tuples.2 AS group_key
- FROM events_json AS events
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) ARRAY
- JOIN arrayFilter(x -> notEquals(x.2, ''), [tuple(0, JSONExtractString(ifNull(toJSONString(events.properties.`$group_0`), ''))), tuple(1, JSONExtractString(ifNull(toJSONString(events.properties.`$group_1`), '')))]) AS tuples
- WHERE and(equals(events.team_id, 99999), greater(events.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), less(events.timestamp, toDateTime64('today', 6, 'UTC')), equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), '00000000-0000-0000-0000-000000000000'))
- LIMIT 100 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
# name: TestRelatedPersonsQuery.test_query_related_people
'''
SELECT DISTINCT person_distinct_ids.person_id AS person_id
@@ -104,52 +76,3 @@
use_hive_partitioning=0
'''
# ---
-# name: TestRelatedPersonsQuery.test_query_related_people[new_events_schema.1]
- '''
- SELECT DISTINCT tuples.1 AS group_type_index,
- tuples.2 AS group_key
- FROM events_json AS events ARRAY
- JOIN arrayFilter(x -> notEquals(x.2, ''), [tuple(1, JSONExtractString(ifNull(toJSONString(events.properties.`$group_1`), '')))]) AS tuples
- WHERE and(equals(events.team_id, 99999), greater(events.timestamp, toDateTime64('2024-12-01 12:00:00.000000', 6, 'UTC')), less(events.timestamp, toDateTime64('today', 6, 'UTC')), equals(JSONExtractString(ifNull(toJSONString(events.properties.`$group_0`), '')), 'org:1'))
- LIMIT 100 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
-# name: TestRelatedPersonsQuery.test_query_related_people[new_events_schema]
- '''
- SELECT DISTINCT person_distinct_ids.person_id AS person_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id,
- person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE in(person_distinct_ids.distinct_id,
- (SELECT events.distinct_id AS distinct_id
- FROM events_json AS events
- WHERE and(equals(events.team_id, 99999), greater(events.timestamp, toDateTime64('2024-12-01 12:00:00.000000', 6, 'UTC')), less(events.timestamp, toDateTime64('today', 6, 'UTC')), equals(JSONExtractString(ifNull(toJSONString(events.properties.`$group_0`), '')), 'org:1'))))
- LIMIT 100 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
diff --git a/ee/clickhouse/views/test/__snapshots__/test_clickhouse_groups.ambr b/ee/clickhouse/views/test/__snapshots__/test_clickhouse_groups.ambr
index 812ba0874bcf..e7308bbddf74 100644
--- a/ee/clickhouse/views/test/__snapshots__/test_clickhouse_groups.ambr
+++ b/ee/clickhouse/views/test/__snapshots__/test_clickhouse_groups.ambr
@@ -50,57 +50,6 @@
use_hive_partitioning=0
'''
# ---
-# name: GroupsViewSetTestCase.test_related_groups[new_events_schema.1]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT DISTINCT tuples.1 AS group_type_index,
- tuples.2 AS group_key
- FROM events_json AS events ARRAY
- JOIN arrayFilter(x -> notEquals(x.2, ''), [tuple(1, JSONExtractString(ifNull(toJSONString(events.properties.`$group_1`), '')))]) AS tuples
- WHERE and(equals(events.team_id, 99999), greater(events.timestamp, toDateTime64('2021-02-09 00:00:00.000000', 6, 'UTC')), less(events.timestamp, toDateTime64('today', 6, 'UTC')), equals(JSONExtractString(ifNull(toJSONString(events.properties.`$group_0`), '')), '0::0'))
- LIMIT 100 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
-# name: GroupsViewSetTestCase.test_related_groups[new_events_schema]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT DISTINCT person_distinct_ids.person_id AS person_id
- FROM
- (SELECT argMax(person_distinct_id2.person_id, person_distinct_id2.version) AS person_id,
- person_distinct_id2.distinct_id AS distinct_id
- FROM person_distinct_id2
- WHERE equals(person_distinct_id2.team_id, 99999)
- GROUP BY person_distinct_id2.distinct_id
- HAVING equals(argMax(person_distinct_id2.is_deleted, person_distinct_id2.version), 0) SETTINGS optimize_aggregation_in_order=1) AS person_distinct_ids
- WHERE in(person_distinct_ids.distinct_id,
- (SELECT events.distinct_id AS distinct_id
- FROM events_json AS events
- WHERE and(equals(events.team_id, 99999), greater(events.timestamp, toDateTime64('2021-02-09 00:00:00.000000', 6, 'UTC')), less(events.timestamp, toDateTime64('today', 6, 'UTC')), equals(JSONExtractString(ifNull(toJSONString(events.properties.`$group_0`), '')), '0::0'))))
- LIMIT 100 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
# name: GroupsViewSetTestCase.test_related_groups_person
'''
/* user_id:0 request:_snapshot_ */
@@ -130,58 +79,3 @@
use_hive_partitioning=0
'''
# ---
-# name: GroupsViewSetTestCase.test_related_groups_person.1
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT DISTINCT $group_1 AS group_key
- FROM events e
- JOIN
- (SELECT distinct_id,
- argMax(person_id, version) as person_id
- FROM person_distinct_id2
- WHERE team_id = 99999
- GROUP BY distinct_id
- HAVING argMax(is_deleted, version) = 0) pdi on e.distinct_id = pdi.distinct_id
- JOIN
- (SELECT group_key
- FROM groups
- WHERE team_id = 99999
- AND group_type_index = 1
- GROUP BY group_key) groups ON $group_1 = groups.group_key
- WHERE team_id = 99999
- AND timestamp > '2021-02-09T00:00:00.000000'
- AND timestamp < '2021-05-10T00:00:00.000000'
- AND group_key != ''
- AND pdi.person_id = '01795392-cc00-0003-7dc7-67a694604d72'
- ORDER BY group_key
- '''
-# ---
-# name: GroupsViewSetTestCase.test_related_groups_person[new_events_schema]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT DISTINCT tuples.1 AS group_type_index,
- tuples.2 AS group_key
- FROM events_json AS events
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS events__override ON equals(events.distinct_id, events__override.distinct_id) ARRAY
- JOIN arrayFilter(x -> notEquals(x.2, ''), [tuple(0, JSONExtractString(ifNull(toJSONString(events.properties.`$group_0`), ''))), tuple(1, JSONExtractString(ifNull(toJSONString(events.properties.`$group_1`), '')))]) AS tuples
- WHERE and(equals(events.team_id, 99999), greater(events.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), less(events.timestamp, toDateTime64('today', 6, 'UTC')), equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), '00000000-0000-0000-0000-000000000000'))
- LIMIT 100 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
diff --git a/ee/clickhouse/views/test/__snapshots__/test_clickhouse_trends.ambr b/ee/clickhouse/views/test/__snapshots__/test_clickhouse_trends.ambr
index ab66adfa80b4..edac21bc2d12 100644
--- a/ee/clickhouse/views/test/__snapshots__/test_clickhouse_trends.ambr
+++ b/ee/clickhouse/views/test/__snapshots__/test_clickhouse_trends.ambr
@@ -20,27 +20,6 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrends.test_insight_trends_aggregate[new_events_schema]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT count() AS total
- FROM events_json AS e
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- ORDER BY 1 DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
# name: ClickhouseTestTrends.test_insight_trends_basic
'''
/* user_id:0 request:_snapshot_ */
@@ -80,45 +59,6 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrends.test_insight_trends_basic[new_events_schema]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date) AS total
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start
- FROM
- (SELECT count(DISTINCT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS total,
- toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- GROUP BY day_start)
- GROUP BY day_start
- ORDER BY day_start ASC)
- ORDER BY arraySum(total) DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
# name: ClickhouseTestTrends.test_insight_trends_cumulative
'''
/* user_id:0 request:_snapshot_ */
@@ -139,252 +79,9 @@
WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
GROUP BY day_start)
GROUP BY day_start
- ORDER BY day_start ASC)
- ORDER BY day_start ASC)
- ORDER BY arraySum(total) DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
-# name: ClickhouseTestTrends.test_insight_trends_cumulative.1
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayFill(x -> ifNull(greater(x, 0), 0), arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date)) AS total
- FROM
- (SELECT day_start AS day_start,
- sum(count) OVER (
- ORDER BY day_start ASC) AS count
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start
- FROM
- (SELECT count(DISTINCT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS total,
- min(toStartOfDay(toTimeZone(e.timestamp, 'UTC'))) AS day_start
- FROM events AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- GROUP BY if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id))
- GROUP BY day_start
- ORDER BY day_start ASC)
- ORDER BY day_start ASC)
- ORDER BY arraySum(total) DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
-# name: ClickhouseTestTrends.test_insight_trends_cumulative.2
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT groupArray(1)(date)[1] AS date,
- arrayFold((acc, x) -> arrayMap(i -> plus(acc[i], x[i]), range(1, plus(length(date), 1))), groupArray(ifNull(total, 0)), arrayWithConstant(length(date), reinterpretAsFloat64(0))) AS total,
- if(ifNull(greaterOrEquals(row_number, 25), 0), '$$_posthog_breakdown_other_$$', breakdown_value) AS breakdown_value
- FROM
- (SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayFill(x -> ifNull(greater(x, 0), 0), arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date)) AS total,
- breakdown_value AS breakdown_value,
- rowNumberInAllBlocks() AS row_number
- FROM
- (SELECT day_start AS day_start,
- sum(count) OVER (PARTITION BY breakdown_value
- ORDER BY day_start ASC) AS count,
- breakdown_value AS breakdown_value
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start,
- breakdown_value AS breakdown_value
- FROM
- (SELECT count() AS total,
- toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start,
- ifNull(nullIf(leftUTF8(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, 'key'), ''), 'null'), '^"|"$', '')), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value
- FROM events AS e
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- GROUP BY day_start,
- breakdown_value)
- GROUP BY day_start,
- breakdown_value
- ORDER BY day_start ASC,
- breakdown_value ASC)
- ORDER BY day_start ASC)
- GROUP BY breakdown_value
- ORDER BY if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_other_$$'), 0), 2, if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_null_$$'), 0), 1, 0)) ASC, arraySum(total) DESC, breakdown_value ASC)
- WHERE isNotNull(breakdown_value)
- GROUP BY breakdown_value
- ORDER BY if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_other_$$'), 0), 2, if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_null_$$'), 0), 1, 0)) ASC, arraySum(total) DESC, breakdown_value ASC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
-# name: ClickhouseTestTrends.test_insight_trends_cumulative.3
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT groupArray(1)(date)[1] AS date,
- arrayFold((acc, x) -> arrayMap(i -> plus(acc[i], x[i]), range(1, plus(length(date), 1))), groupArray(ifNull(total, 0)), arrayWithConstant(length(date), reinterpretAsFloat64(0))) AS total,
- if(ifNull(greaterOrEquals(row_number, 25), 0), '$$_posthog_breakdown_other_$$', breakdown_value) AS breakdown_value
- FROM
- (SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayFill(x -> ifNull(greater(x, 0), 0), arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date)) AS total,
- breakdown_value AS breakdown_value,
- rowNumberInAllBlocks() AS row_number
- FROM
- (SELECT day_start AS day_start,
- sum(count) OVER (PARTITION BY breakdown_value
- ORDER BY day_start ASC) AS count,
- breakdown_value AS breakdown_value
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start,
- breakdown_value AS breakdown_value
- FROM
- (SELECT counts AS total,
- toStartOfDay(timestamp) AS day_start,
- breakdown_value AS breakdown_value
- FROM
- (SELECT d.timestamp AS timestamp,
- count(DISTINCT e.actor_id) AS counts,
- e.breakdown_value AS breakdown_value
- FROM
- (SELECT toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS timestamp,
- if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS actor_id,
- ifNull(nullIf(leftUTF8(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, 'key'), ''), 'null'), '^"|"$', '')), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value
- FROM events AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- LEFT JOIN
- (SELECT person.id AS id,
- replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'key'), ''), 'null'), '^"|"$', '') AS properties___key
- FROM person
- WHERE and(equals(person.team_id, 99999), in(tuple(person.id, person.version),
- (SELECT person.id AS id, max(person.version) AS version
- FROM person
- WHERE equals(person.team_id, 99999)
- GROUP BY person.id
- HAVING and(equals(argMax(person.is_deleted, person.version), 0), less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))))))) SETTINGS optimize_aggregation_in_order=1) AS e__person ON equals(if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id), e__person.id)
- WHERE and(equals(e.team_id, 99999), and(equals(e.event, '$pageview'), ifNull(equals(e__person.properties___key, 'some_val'), 0), ifNull(equals(e__person.properties___key, 'some_val'), 0)), greaterOrEquals(timestamp, minus(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(7))), lessOrEquals(timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))))
- GROUP BY timestamp, actor_id,
- breakdown_value) AS e
- CROSS JOIN
- (SELECT minus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')), toIntervalDay(1)), toIntervalDay(numbers.number)) AS timestamp
- FROM numbers(dateDiff('day', minus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(7)), assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')))) AS numbers) AS d
- WHERE and(lessOrEquals(e.timestamp, d.timestamp), greater(e.timestamp, minus(d.timestamp, toIntervalDay(7))))
- GROUP BY d.timestamp,
- breakdown_value
- ORDER BY d.timestamp ASC)
- WHERE and(greaterOrEquals(timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')))))
- GROUP BY day_start,
- breakdown_value
- ORDER BY day_start ASC,
- breakdown_value ASC)
- ORDER BY day_start ASC)
- GROUP BY breakdown_value
- ORDER BY if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_other_$$'), 0), 2, if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_null_$$'), 0), 1, 0)) ASC, arraySum(total) DESC, breakdown_value ASC)
- WHERE isNotNull(breakdown_value)
- GROUP BY breakdown_value
- ORDER BY if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_other_$$'), 0), 2, if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_null_$$'), 0), 1, 0)) ASC, arraySum(total) DESC, breakdown_value ASC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
-# name: ClickhouseTestTrends.test_insight_trends_cumulative.4
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT groupArray(1)(date)[1] AS date,
- arrayFold((acc, x) -> arrayMap(i -> plus(acc[i], x[i]), range(1, plus(length(date), 1))), groupArray(ifNull(total, 0)), arrayWithConstant(length(date), reinterpretAsFloat64(0))) AS total,
- if(ifNull(greaterOrEquals(row_number, 25), 0), '$$_posthog_breakdown_other_$$', breakdown_value) AS breakdown_value
- FROM
- (SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayFill(x -> ifNull(greater(x, 0), 0), arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date)) AS total,
- breakdown_value AS breakdown_value,
- rowNumberInAllBlocks() AS row_number
- FROM
- (SELECT day_start AS day_start,
- sum(count) OVER (PARTITION BY breakdown_value
- ORDER BY day_start ASC) AS count,
- breakdown_value AS breakdown_value
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start,
- breakdown_value AS breakdown_value
- FROM
- (SELECT count(DISTINCT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS total,
- min(toStartOfDay(toTimeZone(e.timestamp, 'UTC'))) AS day_start,
- ifNull(nullIf(leftUTF8(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, 'key'), ''), 'null'), '^"|"$', '')), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value
- FROM events AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- GROUP BY if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id),
- breakdown_value)
- GROUP BY day_start,
- breakdown_value
- ORDER BY day_start ASC,
- breakdown_value ASC)
- ORDER BY day_start ASC)
- GROUP BY breakdown_value
- ORDER BY if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_other_$$'), 0), 2, if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_null_$$'), 0), 1, 0)) ASC, arraySum(total) DESC, breakdown_value ASC)
- WHERE isNotNull(breakdown_value)
- GROUP BY breakdown_value
- ORDER BY if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_other_$$'), 0), 2, if(ifNull(equals(breakdown_value, '$$_posthog_breakdown_null_$$'), 0), 1, 0)) ASC, arraySum(total) DESC, breakdown_value ASC
+ ORDER BY day_start ASC)
+ ORDER BY day_start ASC)
+ ORDER BY arraySum(total) DESC
LIMIT 50000 SETTINGS readonly=2,
max_execution_time=60,
allow_experimental_object_type=1,
@@ -399,7 +96,7 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrends.test_insight_trends_cumulative[new_events_schema.1]
+# name: ClickhouseTestTrends.test_insight_trends_cumulative.1
'''
/* user_id:0 request:_snapshot_ */
SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
@@ -415,7 +112,7 @@
FROM
(SELECT count(DISTINCT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS total,
min(toStartOfDay(toTimeZone(e.timestamp, 'UTC'))) AS day_start
- FROM events_json AS e
+ FROM events AS e
LEFT OUTER JOIN
(SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
person_distinct_id_overrides.distinct_id AS distinct_id
@@ -443,7 +140,7 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrends.test_insight_trends_cumulative[new_events_schema.2]
+# name: ClickhouseTestTrends.test_insight_trends_cumulative.2
'''
/* user_id:0 request:_snapshot_ */
SELECT groupArray(1)(date)[1] AS date,
@@ -467,8 +164,8 @@
FROM
(SELECT count() AS total,
toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start,
- ifNull(nullIf(leftUTF8(toString(if(notEquals(toJSONString(e.properties.^key), '{}'), toJSONString(e.properties.^key), if(isNull(e.properties.key), NULL, if(startsWith(dynamicType(e.properties.key), 'DateTime'), replaceOne(toString(e.properties.key), ' ', 'T'), if(or(startsWith(dynamicType(e.properties.key), 'Array'), startsWith(dynamicType(e.properties.key), 'Map'), startsWith(dynamicType(e.properties.key), 'Tuple')), toJSONString(e.properties.key), toString(e.properties.key)))))), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value
- FROM events_json AS e
+ ifNull(nullIf(leftUTF8(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, 'key'), ''), 'null'), '^"|"$', '')), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value
+ FROM events AS e
WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
GROUP BY day_start,
breakdown_value)
@@ -496,7 +193,7 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrends.test_insight_trends_cumulative[new_events_schema.3]
+# name: ClickhouseTestTrends.test_insight_trends_cumulative.3
'''
/* user_id:0 request:_snapshot_ */
SELECT groupArray(1)(date)[1] AS date,
@@ -528,8 +225,8 @@
FROM
(SELECT toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS timestamp,
if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id) AS actor_id,
- ifNull(nullIf(leftUTF8(toString(if(notEquals(toJSONString(e.properties.^key), '{}'), toJSONString(e.properties.^key), if(isNull(e.properties.key), NULL, if(startsWith(dynamicType(e.properties.key), 'DateTime'), replaceOne(toString(e.properties.key), ' ', 'T'), if(or(startsWith(dynamicType(e.properties.key), 'Array'), startsWith(dynamicType(e.properties.key), 'Map'), startsWith(dynamicType(e.properties.key), 'Tuple')), toJSONString(e.properties.key), toString(e.properties.key)))))), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value
- FROM events_json AS e
+ ifNull(nullIf(leftUTF8(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, 'key'), ''), 'null'), '^"|"$', '')), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value
+ FROM events AS e
LEFT OUTER JOIN
(SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
person_distinct_id_overrides.distinct_id AS distinct_id
@@ -582,7 +279,7 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrends.test_insight_trends_cumulative[new_events_schema.4]
+# name: ClickhouseTestTrends.test_insight_trends_cumulative.4
'''
/* user_id:0 request:_snapshot_ */
SELECT groupArray(1)(date)[1] AS date,
@@ -606,8 +303,8 @@
FROM
(SELECT count(DISTINCT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS total,
min(toStartOfDay(toTimeZone(e.timestamp, 'UTC'))) AS day_start,
- ifNull(nullIf(leftUTF8(toString(if(notEquals(toJSONString(e.properties.^key), '{}'), toJSONString(e.properties.^key), if(isNull(e.properties.key), NULL, if(startsWith(dynamicType(e.properties.key), 'DateTime'), replaceOne(toString(e.properties.key), ' ', 'T'), if(or(startsWith(dynamicType(e.properties.key), 'Array'), startsWith(dynamicType(e.properties.key), 'Map'), startsWith(dynamicType(e.properties.key), 'Tuple')), toJSONString(e.properties.key), toString(e.properties.key)))))), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value
- FROM events_json AS e
+ ifNull(nullIf(leftUTF8(toString(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(e.properties, 'key'), ''), 'null'), '^"|"$', '')), 400), ''), '$$_posthog_breakdown_null_$$') AS breakdown_value
+ FROM events AS e
LEFT OUTER JOIN
(SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
person_distinct_id_overrides.distinct_id AS distinct_id
@@ -642,43 +339,6 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrends.test_insight_trends_cumulative[new_events_schema]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayFill(x -> ifNull(greater(x, 0), 0), arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date)) AS total
- FROM
- (SELECT day_start AS day_start,
- sum(count) OVER (
- ORDER BY day_start ASC) AS count
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start
- FROM
- (SELECT count() AS total,
- toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start
- FROM events_json AS e
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- GROUP BY day_start)
- GROUP BY day_start
- ORDER BY day_start ASC)
- ORDER BY day_start ASC)
- ORDER BY arraySum(total) DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
# name: ClickhouseTestTrendsCaching.test_insight_trends_merging
'''
/* user_id:0 request:_snapshot_ */
@@ -757,84 +417,6 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrendsCaching.test_insight_trends_merging[new_events_schema.1]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date) AS total
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start
- FROM
- (SELECT count(DISTINCT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS total,
- toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- GROUP BY day_start)
- GROUP BY day_start
- ORDER BY day_start ASC)
- ORDER BY arraySum(total) DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
-# name: ClickhouseTestTrendsCaching.test_insight_trends_merging[new_events_schema]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date) AS total
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start
- FROM
- (SELECT count(DISTINCT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS total,
- toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-15 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- GROUP BY day_start)
- GROUP BY day_start
- ORDER BY day_start ASC)
- ORDER BY arraySum(total) DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
# name: ClickhouseTestTrendsCaching.test_insight_trends_merging_skipped_interval
'''
/* user_id:0 request:_snapshot_ */
@@ -913,84 +495,6 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrendsCaching.test_insight_trends_merging_skipped_interval[new_events_schema.1]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2012-01-02 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2012-01-02 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-16 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date) AS total
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start
- FROM
- (SELECT count(DISTINCT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS total,
- toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2012-01-02 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-16 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- GROUP BY day_start)
- GROUP BY day_start
- ORDER BY day_start ASC)
- ORDER BY arraySum(total) DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
-# name: ClickhouseTestTrendsCaching.test_insight_trends_merging_skipped_interval[new_events_schema]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2011-12-31 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2011-12-31 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2012-01-14 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date) AS total
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start
- FROM
- (SELECT count(DISTINCT if(not(empty(e__override.distinct_id)), e__override.person_id, e.person_id)) AS total,
- toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start
- FROM events_json AS e
- LEFT OUTER JOIN
- (SELECT argMax(person_distinct_id_overrides.person_id, person_distinct_id_overrides.version) AS person_id,
- person_distinct_id_overrides.distinct_id AS distinct_id
- FROM person_distinct_id_overrides
- WHERE equals(person_distinct_id_overrides.team_id, 99999)
- GROUP BY person_distinct_id_overrides.distinct_id
- HAVING equals(argMax(person_distinct_id_overrides.is_deleted, person_distinct_id_overrides.version), 0) SETTINGS optimize_aggregation_in_order=1) AS e__override ON equals(e.distinct_id, e__override.distinct_id)
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2011-12-31 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2012-01-14 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- GROUP BY day_start)
- GROUP BY day_start
- ORDER BY day_start ASC)
- ORDER BY arraySum(total) DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
# name: ClickhouseTestTrendsGroups.test_aggregating_by_group
'''
/* user_id:0 request:_snapshot_ */
@@ -1023,38 +527,6 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrendsGroups.test_aggregating_by_group[new_events_schema]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2020-01-12 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date) AS total
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start
- FROM
- (SELECT count(DISTINCT e.`$group_0`) AS total,
- toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start
- FROM events_json AS e
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-12 23:59:59', 'UTC'))), equals(e.event, '$pageview'), ifNull(notEquals(nullIf(nullIf(e.properties.`$group_0`, ''), 'null'), ''), 1), notEquals(e.`$group_0`, ''))
- GROUP BY day_start)
- GROUP BY day_start
- ORDER BY day_start ASC)
- ORDER BY arraySum(total) DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
# name: ClickhouseTestTrendsGroups.test_aggregating_by_session
'''
/* user_id:0 request:_snapshot_ */
@@ -1087,35 +559,3 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestTrendsGroups.test_aggregating_by_session[new_events_schema]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT arrayMap(number -> plus(toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toIntervalDay(number)), range(0, plus(coalesce(dateDiff('day', toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC')), toIntervalDay(1)), toStartOfInterval(assumeNotNull(toDateTime('2020-01-12 23:59:59', 'UTC')), toIntervalDay(1)))), 1))) AS date,
- arrayMap(_match_date -> arraySum(arraySlice(groupArray(ifNull(count, 0)), indexOf(groupArray(day_start) AS _days_for_count, _match_date) AS _index, plus(minus(arrayLastIndex(x -> ifNull(equals(x, _match_date), isNull(x)
- and isNull(_match_date)), _days_for_count), _index), 1))), date) AS total
- FROM
- (SELECT sum(total) AS count,
- day_start AS day_start
- FROM
- (SELECT count(DISTINCT e.`$session_id`) AS total,
- toStartOfDay(toTimeZone(e.timestamp, 'UTC')) AS day_start
- FROM events_json AS e
- WHERE and(equals(e.team_id, 99999), greaterOrEquals(e.timestamp, toStartOfInterval(assumeNotNull(toDateTime('2020-01-01 00:00:00', 'UTC')), toIntervalDay(1))), lessOrEquals(e.timestamp, assumeNotNull(toDateTime('2020-01-12 23:59:59', 'UTC'))), equals(e.event, '$pageview'))
- GROUP BY day_start)
- GROUP BY day_start
- ORDER BY day_start ASC)
- ORDER BY arraySum(total) DESC
- LIMIT 50000 SETTINGS readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=0,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
diff --git a/ee/clickhouse/views/test/funnel/__snapshots__/test_clickhouse_funnel_unordered.ambr b/ee/clickhouse/views/test/funnel/__snapshots__/test_clickhouse_funnel_unordered.ambr
index c58673209c1a..ca6a7a958682 100644
--- a/ee/clickhouse/views/test/funnel/__snapshots__/test_clickhouse_funnel_unordered.ambr
+++ b/ee/clickhouse/views/test/funnel/__snapshots__/test_clickhouse_funnel_unordered.ambr
@@ -59,63 +59,3 @@
use_hive_partitioning=0
'''
# ---
-# name: ClickhouseTestUnorderedFunnelGroups.test_unordered_funnel_with_groups[new_events_schema]
- '''
- /* user_id:0 request:_snapshot_ */
- SELECT sum(step_1) AS step_1,
- sum(step_2) AS step_2,
- arrayMap(x -> if(isNaN(x), NULL, x), [avgArrayOrNull(step_1_conversion_times)])[1] AS step_1_average_conversion_time,
- arrayMap(x -> if(isNaN(x), NULL, x), [medianArrayOrNull(step_1_conversion_times)])[1] AS step_1_median_conversion_time,
- arrayMap(x -> if(isNaN(x), NULL, x), [arrayReduce('median', arrayFlatten(groupArray(arrayFlatten(groupArray(total_conversion_times))) OVER ()))])[1] AS total_median_conversion_time,
- groupArray(row_number) AS row_number,
- final_prop AS final_prop
- FROM
- (SELECT countIf(ifNull(notEquals(bitAnd(steps_bitfield, 1), 0), 1)) AS step_1,
- countIf(ifNull(notEquals(bitAnd(steps_bitfield, 2), 0), 1)) AS step_2,
- groupArrayIf(timings[1], ifNull(greater(timings[1], 0), 0)) AS step_1_conversion_times,
- groupArrayIf(arraySum(timings), ifNull(greaterOrEquals(step_reached, 1), 0)) AS total_conversion_times,
- rowNumberInAllBlocks() AS row_number,
- breakdown AS final_prop
- FROM
- (SELECT arraySort(t -> t.1, groupArray(tuple(accurateCastOrNull(timestamp, 'Float64'), uuid, '', arrayFilter(x -> ifNull(notEquals(x, 0), 1), [multiply(1, step_0), multiply(2, step_1)])))) AS events_array,
- [''] AS prop,
- arrayJoin(aggregate_funnel(2, 1209600, 'first_touch', 'unordered', prop, [], arrayFilter((x, x_before, x_after) -> not(and(lessOrEquals(length(x.4), 1), equals(x.4, x_before.4), equals(x.4, x_after.4), equals(x.3, x_before.3), equals(x.3, x_after.3), ifNull(greater(x.1, x_before.1), 0), ifNull(less(x.1, x_after.1), 0))), events_array, arrayRotateRight(events_array, 1), arrayRotateLeft(events_array, 1)))) AS af_tuple,
- af_tuple.1 AS step_reached,
- plus(af_tuple.1, 1) AS steps,
- af_tuple.2 AS breakdown,
- af_tuple.3 AS timings,
- af_tuple.5 AS steps_bitfield,
- aggregation_target AS aggregation_target
- FROM
- (SELECT toTimeZone(e.timestamp, 'UTC') AS timestamp,
- e.`$group_0` AS aggregation_target,
- e.uuid AS uuid,
- e.`$session_id` AS `$session_id`,
- e.`$window_id` AS `$window_id`,
- if(equals(e.event, 'user signed up'), 1, 0) AS step_0,
- if(equals(e.event, 'paid'), 1, 0) AS step_1
- FROM events_json AS e
- WHERE and(equals(e.team_id, 99999), and(and(greaterOrEquals(e.timestamp, toDateTime64('2020-01-01 00:00:00.000000', 6, 'UTC')), lessOrEquals(e.timestamp, toDateTime64('2020-01-14 23:59:59.999999', 6, 'UTC'))), in(e.event, tuple('paid', 'user signed up')), ifNull(notEquals(nullIf(nullIf(e.properties.`$group_0`, ''), 'null'), ''), 1), and(notEquals(toString(aggregation_target), ''), isNotNull(aggregation_target))), or(equals(step_0, 1), equals(step_1, 1))))
- GROUP BY aggregation_target
- HAVING ifNull(greaterOrEquals(step_reached, 0), 0))
- GROUP BY breakdown
- ORDER BY step_2 DESC,
- step_1 DESC)
- GROUP BY final_prop
- ORDER BY step_2 DESC,
- step_1 DESC
- LIMIT 100 SETTINGS join_algorithm='auto',
- readonly=2,
- max_execution_time=60,
- allow_experimental_object_type=1,
- max_ast_elements=4000000,
- max_expanded_ast_elements=4000000,
- max_bytes_before_external_group_by=23622320128,
- transform_null_in=1,
- optimize_min_equality_disjunction_chain_length=4294967295,
- optimize_rewrite_aggregate_function_with_if=0,
- optimize_min_inequality_conjunction_chain_length=4294967295,
- allow_experimental_join_condition=1,
- use_hive_partitioning=0
- '''
-# ---
diff --git a/posthog/tasks/alerts/detectors/statistical/mad.py b/posthog/tasks/alerts/detectors/statistical/mad.py
index f0072bda2b9b..65040d18908b 100644
--- a/posthog/tasks/alerts/detectors/statistical/mad.py
+++ b/posthog/tasks/alerts/detectors/statistical/mad.py
@@ -29,15 +29,23 @@ class MADDetector(BaseDetector):
def detect(self, data: np.ndarray) -> DetectionResult:
threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)
window = self.config.get("window", 30)
+ diffs_n = self.preprocessing_config.get("diffs_n", 0) or 0
+ offset = max(self.training_offset, 1)
- if not self._validate_data(data, min_length=window + 1):
+ if not self._validate_data(data, min_length=window + offset + diffs_n):
return DetectionResult(is_anomaly=False)
+ original_length = len(data)
data = self.preprocess(data)
values = data if data.ndim == 1 else data[:, 0]
-
- # Use rolling window (exclude current point) to fit the model
- window_data = values[-(window + 1) : -1]
+ # Differencing prepends synthetic (zero-valued) points to keep the array
+ # length unchanged - drop them so the training window only ever sees
+ # genuine differenced values.
+ values = values[diffs_n:]
+
+ # Use rolling window, honoring training_offset to exclude points closest
+ # to the one being scored, so a live check agrees with detect_batch()
+ window_data = values[-(window + offset) : -offset]
current_value = values[-1]
clf = MAD()
@@ -51,7 +59,7 @@ class MADDetector(BaseDetector):
return DetectionResult(
is_anomaly=is_anomaly,
score=prob,
- triggered_indices=[len(values) - 1] if is_anomaly else [],
+ triggered_indices=[original_length - 1] if is_anomaly else [],
all_scores=[prob],
metadata={
"median": float(clf.median_),
@@ -64,18 +72,23 @@ class MADDetector(BaseDetector):
def detect_batch(self, data: np.ndarray) -> DetectionResult:
threshold = self.config.get("threshold", self.DEFAULT_THRESHOLD)
window = self.config.get("window", 30)
+ diffs_n = self.preprocessing_config.get("diffs_n", 0) or 0
+ offset = max(self.training_offset, 1)
- if not self._validate_data(data, min_length=window + 1):
+ if not self._validate_data(data, min_length=window + offset + diffs_n):
return DetectionResult(is_anomaly=False)
data = self.preprocess(data)
values = data if data.ndim == 1 else data[:, 0]
+ # Keep indices aligned with the original series: scores/triggers below
+ # are shifted back by diffs_n before being returned.
+ values = values[diffs_n:]
triggered = []
- scores: list[float | None] = [None] * window
+ scores: list[float | None] = [None] * (diffs_n + window + offset - 1)
- for i in range(window, len(values)):
- window_data = values[i - window : i]
+ for i in range(window + offset - 1, len(values)):
+ window_data = values[i - window - offset + 1 : i - offset + 1]
current_val = values[i]
clf = MAD()
@@ -86,7 +99,7 @@ class MADDetector(BaseDetector):
scores.append(prob)
if prob > threshold:
- triggered.append(i)
+ triggered.append(i + diffs_n)
return DetectionResult(
is_anomaly=len(triggered) > 0,
diff --git a/posthog/tasks/alerts/detectors/test/test_detectors.py b/posthog/tasks/alerts/detectors/test/test_detectors.py
index aff08d06b0fa..3147cede3c26 100644
--- a/posthog/tasks/alerts/detectors/test/test_detectors.py
+++ b/posthog/tasks/alerts/detectors/test/test_detectors.py
@@ -181,6 +181,40 @@ class TestStatisticalDetectors:
for key in ["q1", "q3", "iqr", "raw_distance"]:
assert key in result.metadata
+ def test_mad_rejects_data_that_only_covers_the_undiffed_window(self) -> None:
+ # With diffs_n=1, the training window needs one extra raw point beyond
+ # window+1, otherwise first_difference()'s synthetic leading value (a
+ # zero) would land inside the fit window and silently shrink it by one
+ # genuine point. window+1 (the old minimum) must now be rejected.
+ data = np.array([10, 10, 10, 10, 10, 10]) # len == window + 1
+ detector = MADDetector({"threshold": 0.9, "window": 5, "preprocessing": {"diffs_n": 1}})
+ assert detector.detect(data).is_anomaly is False
+
+ def test_mad_batch_triggered_indices_align_with_raw_series_when_diffing(self) -> None:
+ # detect_batch trims preprocess()'s synthetic leading points internally;
+ # triggered_indices/all_scores must be shifted back so callers (which
+ # map indices to posthog/tasks/alerts/detector.py's original series
+ # dates) don't get an off-by-one against every date after the first.
+ detector = MADDetector({"threshold": 0.9, "window": 5, "preprocessing": {"diffs_n": 1}})
+ result = detector.detect_batch(BATCH_DATA)
+ assert len(result.all_scores) == len(BATCH_DATA)
+ assert all(0 <= i < len(BATCH_DATA) for i in result.triggered_indices)
+ # The spike is at BATCH_DATA[6]; a diffed detector also flags the
+ # reversion at [7], but never an index outside the raw series.
+ assert 6 in result.triggered_indices
+
+ def test_mad_detect_honors_training_offset_n(self) -> None:
+ # detect() used to hardcode a training_offset of 1 regardless of
+ # config, so training_offset_n had no effect on live checks (only
+ # detect_batch's loop bounds implicitly depended on window). Placing
+ # a spike so it falls inside the offset=1 training window but outside
+ # the offset=4 one must change whether the same current point trips.
+ data = np.array([9, 11, 10, 9, 11, 10, 30, 11, 14])
+ offset_1 = MADDetector({"threshold": 0.9, "window": 5, "training_offset_n": 1})
+ offset_4 = MADDetector({"threshold": 0.9, "window": 5, "training_offset_n": 4})
+ assert offset_1.detect(data).is_anomaly is False
+ assert offset_4.detect(data).is_anomaly is True
+
class TestPyODDetectors:
@parameterized.expand(PYOD_DETECTORS_FOR_ANOMALY_TEST)