diff --git a/ee/clickhouse/queries/test/__snapshots__/test_breakdown_props.ambr b/ee/clickhouse/queries/test/__snapshots__/test_breakdown_props.ambr new file mode 100644 index 0000000000000..1dc13e551e422 --- /dev/null +++ b/ee/clickhouse/queries/test/__snapshots__/test_breakdown_props.ambr @@ -0,0 +1,252 @@ +# serializer version: 1 +# name: TestBreakdownProps.test_breakdown_group_props + ''' + + SELECT replaceRegexpAll(JSONExtractRaw(group_properties_0, 'industry'), '^"|"$', '') AS value, + count(*) as count + FROM events e + LEFT JOIN + (SELECT group_key, + argMax(group_properties, _timestamp) AS group_properties_0 + FROM groups + WHERE team_id = 99999 + AND group_type_index = 0 + GROUP BY group_key) groups_0 ON "$group_0" == groups_0.group_key + WHERE team_id = 99999 + AND event = '$pageview' + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2020-01-01 00:00:00', 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-12 23:59:59', 'UTC') + AND ((isNull(replaceRegexpAll(JSONExtractRaw(group_properties_0, 'out'), '^"|"$', '')) + OR NOT JSONHas(group_properties_0, 'out'))) + GROUP BY value + ORDER BY count DESC, value DESC + LIMIT 6 + OFFSET 0 + ''' +# --- +# name: TestBreakdownProps.test_breakdown_group_props.1 + ''' + + SELECT replaceRegexpAll(JSONExtractRaw(group_properties_0, 'industry'), '^"|"$', '') AS value, + count(*) as count + FROM events e + LEFT JOIN + (SELECT group_key, + argMax(group_properties, _timestamp) AS group_properties_0 + FROM groups + WHERE team_id = 99999 + AND group_type_index = 0 + GROUP BY group_key) groups_0 ON "$group_0" == groups_0.group_key + WHERE team_id = 99999 + AND event = '$pageview' + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2020-01-01 00:00:00', 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-12 23:59:59', 'UTC') + AND ((isNull(replaceRegexpAll(JSONExtractRaw(group_properties_0, 'out'), '^"|"$', '')) + OR NOT JSONHas(group_properties_0, 'out'))) + GROUP BY value + ORDER BY count DESC, value DESC + LIMIT 6 + OFFSET 0 + ''' +# --- +# name: TestBreakdownProps.test_breakdown_person_props + ''' + + SELECT replaceRegexpAll(JSONExtractRaw(person_props, '$browser'), '^"|"$', '') AS value, + count(*) as count + FROM events e + INNER 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) AS pdi ON e.distinct_id = pdi.distinct_id + INNER JOIN + (SELECT id, + argMax(properties, version) as person_props + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1) person ON pdi.person_id = person.id + WHERE team_id = 99999 + AND event = '$pageview' + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2019-12-21 00:00:00', 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-04 23:59:59', 'UTC') + GROUP BY value + ORDER BY count DESC, value DESC + LIMIT 6 + OFFSET 0 + ''' +# --- +# name: TestBreakdownProps.test_breakdown_person_props_materialized + ''' + + SELECT "pmat_$browser" AS value, + count(*) as count + FROM events e + INNER 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) AS pdi ON e.distinct_id = pdi.distinct_id + INNER JOIN + (SELECT id, + argMax(pmat_$browser, version) as pmat_$browser + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1) person ON pdi.person_id = person.id + WHERE team_id = 99999 + AND event = '$pageview' + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2019-12-21 00:00:00', 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-04 23:59:59', 'UTC') + GROUP BY value + ORDER BY count DESC, value DESC + LIMIT 6 + OFFSET 0 + ''' +# --- +# name: TestBreakdownProps.test_breakdown_person_props_with_entity_filter_and_or_props_with_partial_pushdown + ''' + + SELECT replaceRegexpAll(JSONExtractRaw(person_props, '$browser'), '^"|"$', '') AS value, + count(*) as count + FROM events e + INNER 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) AS pdi ON e.distinct_id = pdi.distinct_id + INNER JOIN + (SELECT id, + argMax(properties, version) as person_props + FROM person + WHERE team_id = 99999 + AND id IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND ((replaceRegexpAll(JSONExtractRaw(properties, '$browser'), '^"|"$', '') ILIKE '%test%')) ) + GROUP BY id + HAVING max(is_deleted) = 0 + AND ((replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), '$browser'), '^"|"$', '') ILIKE '%test%')) SETTINGS optimize_aggregation_in_order = 1) person ON pdi.person_id = person.id + WHERE team_id = 99999 + AND event = '$pageview' + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2019-12-21 00:00:00', 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-04 23:59:59', 'UTC') + AND ((has(['test2'], replaceRegexpAll(JSONExtractRaw(person_props, '$os'), '^"|"$', '')) + OR has(['val'], replaceRegexpAll(JSONExtractRaw(e.properties, 'key'), '^"|"$', '')))) + GROUP BY value + ORDER BY count DESC, value DESC + LIMIT 6 + OFFSET 0 + ''' +# --- +# name: TestBreakdownProps.test_breakdown_session_props + ''' + + SELECT sessions.session_duration AS value, + count(*) as count + FROM events e + INNER JOIN + (SELECT "$session_id", + dateDiff('second', min(timestamp), max(timestamp)) as session_duration + FROM events + WHERE "$session_id" != '' + AND team_id = 99999 + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2020-01-02 00:00:00', 'UTC') - INTERVAL 24 HOUR + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-12 23:59:59', 'UTC') + INTERVAL 24 HOUR + GROUP BY "$session_id") AS sessions ON sessions."$session_id" = e."$session_id" + WHERE team_id = 99999 + AND event = '$pageview' + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2020-01-02 00:00:00', 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-12 23:59:59', 'UTC') + GROUP BY value + ORDER BY count DESC, value DESC + LIMIT 26 + OFFSET 0 + ''' +# --- +# name: TestBreakdownProps.test_breakdown_with_math_property_session + ''' + + SELECT replaceRegexpAll(JSONExtractRaw(person_props, '$browser'), '^"|"$', '') AS value, + sum(session_duration) as count + FROM events e + INNER 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) AS pdi ON e.distinct_id = pdi.distinct_id + INNER JOIN + (SELECT id, + argMax(properties, version) as person_props + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1) person ON pdi.person_id = person.id + INNER JOIN + (SELECT "$session_id", + dateDiff('second', min(timestamp), max(timestamp)) as session_duration + FROM events + WHERE "$session_id" != '' + AND team_id = 99999 + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2020-01-02 00:00:00', 'UTC') - INTERVAL 24 HOUR + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-12 23:59:59', 'UTC') + INTERVAL 24 HOUR + GROUP BY "$session_id") AS sessions ON sessions."$session_id" = e."$session_id" + WHERE team_id = 99999 + AND event = '$pageview' + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2020-01-02 00:00:00', 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-12 23:59:59', 'UTC') + GROUP BY value + ORDER BY count DESC, value DESC + LIMIT 26 + OFFSET 0 + ''' +# --- +# name: TestBreakdownProps.test_breakdown_with_math_property_session.1 + ''' + + SELECT replaceRegexpAll(JSONExtractRaw(person_props, '$browser'), '^"|"$', '') AS value, + count(*) as count + FROM events e + INNER 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) AS pdi ON e.distinct_id = pdi.distinct_id + INNER JOIN + (SELECT id, + argMax(properties, version) as person_props + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1) person ON pdi.person_id = person.id + INNER JOIN + (SELECT "$session_id", + dateDiff('second', min(timestamp), max(timestamp)) as session_duration + FROM events + WHERE "$session_id" != '' + AND team_id = 99999 + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2020-01-02 00:00:00', 'UTC') - INTERVAL 24 HOUR + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-12 23:59:59', 'UTC') + INTERVAL 24 HOUR + GROUP BY "$session_id") AS sessions ON sessions."$session_id" = e."$session_id" + WHERE team_id = 99999 + AND event = '$pageview' + AND toTimeZone(timestamp, 'UTC') >= toDateTime('2020-01-02 00:00:00', 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-12 23:59:59', 'UTC') + GROUP BY value + ORDER BY count DESC, value DESC + LIMIT 26 + OFFSET 0 + ''' +# --- diff --git a/ee/clickhouse/queries/test/__snapshots__/test_cohort_query.ambr b/ee/clickhouse/queries/test/__snapshots__/test_cohort_query.ambr new file mode 100644 index 0000000000000..edc267c4e62c2 --- /dev/null +++ b/ee/clickhouse/queries/test/__snapshots__/test_cohort_query.ambr @@ -0,0 +1,811 @@ +# serializer version: 1 +# name: TestCohortQuery.test_basic_query + ''' + + SELECT person.person_id AS id + FROM + (SELECT pdi.person_id AS person_id, + countIf(timestamp > now() - INTERVAL 1 day + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_0_level_0_0, + countIf(timestamp > now() - INTERVAL 2 week + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_0_level_1_0, + minIf(timestamp, ((replaceRegexpAll(JSONExtractRaw(properties, '$current_url'), '^"|"$', '') = 'https://posthog.com/feedback/123' + AND event = '$autocapture'))) >= now() - INTERVAL 2 week + AND minIf(timestamp, ((replaceRegexpAll(JSONExtractRaw(properties, '$current_url'), '^"|"$', '') = 'https://posthog.com/feedback/123' + AND event = '$autocapture'))) < now() as first_time_condition_None_level_level_0_level_1_level_0_0 + FROM events e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview', '$pageview', '$autocapture'] + GROUP BY person_id) behavior_query + INNER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id + FROM person + WHERE team_id = 99999 + AND id IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND (((has(['test@posthog.com'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', ''))))) ) + GROUP BY id + HAVING max(is_deleted) = 0 + AND (((has(['test@posthog.com'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'email'), '^"|"$', ''))))) SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id + WHERE 1 = 1 + AND ((((performed_event_condition_None_level_level_0_level_0_level_0_0) + OR (performed_event_condition_None_level_level_0_level_0_level_1_0)) + AND ((first_time_condition_None_level_level_0_level_1_level_0_0)))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_cohort_filter_with_another_cohort_with_event_sequence + ''' + + SELECT person.person_id AS id + FROM + (SELECT person_id, + max(if(event = '$pageview' + AND event_0_latest_0 < event_0_latest_1 + AND event_0_latest_1 <= event_0_latest_0 + INTERVAL 3 day, 2, 1)) = 2 AS steps_0, + max(if(event = '$new_view' + AND event_1_latest_0 < event_1_latest_1 + AND event_1_latest_1 <= event_1_latest_0 + INTERVAL 8 day, 2, 1)) = 2 AS steps_1 + FROM + (SELECT person_id, + event, + properties, + distinct_id, timestamp, event_0_latest_0, + min(event_0_latest_1) over (PARTITION by person_id + ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) event_0_latest_1, + event_1_latest_0, + min(event_1_latest_1) over (PARTITION by person_id + ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) event_1_latest_1 + FROM + (SELECT if(notEmpty(pdi.distinct_id), pdi.person_id, e.person_id) AS person_id, + event, + properties, + distinct_id, timestamp, if(event = '$pageview' + AND timestamp > now() - INTERVAL 8 day, 1, 0) AS event_0_step_0, + if(event_0_step_0 = 1, timestamp, null) AS event_0_latest_0, + if(event = '$pageview' + AND timestamp > now() - INTERVAL 8 day, 1, 0) AS event_0_step_1, + if(event_0_step_1 = 1, timestamp, null) AS event_0_latest_1, + if(event = '$new_view' + AND timestamp > now() - INTERVAL 8 day, 1, 0) AS event_1_step_0, + if(event_1_step_0 = 1, timestamp, null) AS event_1_latest_0, + if(event = '$new_view' + AND timestamp > now() - INTERVAL 8 day, 1, 0) AS event_1_step_1, + if(event_1_step_1 = 1, timestamp, null) AS event_1_latest_1 + FROM events AS e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview', '$pageview', '$new_view', '$new_view'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 8 day )) + GROUP BY person_id) funnel_query + INNER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id + FROM person + WHERE team_id = 99999 + AND id IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND (((has(['test'], replaceRegexpAll(JSONExtractRaw(properties, 'name'), '^"|"$', ''))))) ) + GROUP BY id + HAVING max(is_deleted) = 0 + AND (((has(['test'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', ''))))) SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = funnel_query.person_id + WHERE 1 = 1 + AND ((((steps_0)) + AND (steps_1))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_cohort_filter_with_extra + ''' + + SELECT person.person_id AS id + FROM + (SELECT pdi.person_id AS person_id, + countIf(timestamp > now() - INTERVAL 1 week + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_0_0 + FROM events e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 1 week + GROUP BY person_id) behavior_query + INNER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id + FROM person + WHERE team_id = 99999 + AND id IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND ((((has(['test'], replaceRegexpAll(JSONExtractRaw(properties, 'name'), '^"|"$', '')))))) ) + GROUP BY id + HAVING max(is_deleted) = 0 + AND ((((has(['test'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', '')))))) SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id + WHERE 1 = 1 + AND (((performed_event_condition_None_level_level_0_level_0_0))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_cohort_filter_with_extra.1 + ''' + + SELECT if(behavior_query.person_id = '00000000-0000-0000-0000-000000000000', person.person_id, behavior_query.person_id) AS id + FROM + (SELECT pdi.person_id AS person_id, + countIf(timestamp > now() - INTERVAL 1 week + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_1_level_0_0 + FROM events e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 1 week + GROUP BY person_id) behavior_query + FULL OUTER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id, + argMax(properties, version) as person_props + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id + WHERE 1 = 1 + AND ((((has(['test'], replaceRegexpAll(JSONExtractRaw(person_props, 'name'), '^"|"$', '')))) + OR ((performed_event_condition_None_level_level_0_level_1_level_0_0)))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_performed_event_sequence + ''' + + SELECT funnel_query.person_id AS id + FROM + (SELECT person_id, + max(if(event = '$pageview' + AND event_0_latest_0 < event_0_latest_1 + AND event_0_latest_1 <= event_0_latest_0 + INTERVAL 3 day, 2, 1)) = 2 AS steps_0 + FROM + (SELECT person_id, + event, + properties, + distinct_id, timestamp, event_0_latest_0, + min(event_0_latest_1) over (PARTITION by person_id + ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) event_0_latest_1 + FROM + (SELECT if(notEmpty(pdi.distinct_id), pdi.person_id, e.person_id) AS person_id, + event, + properties, + distinct_id, timestamp, if(event = '$pageview' + AND timestamp > now() - INTERVAL 7 day, 1, 0) AS event_0_step_0, + if(event_0_step_0 = 1, timestamp, null) AS event_0_latest_0, + if(event = '$pageview' + AND timestamp > now() - INTERVAL 7 day, 1, 0) AS event_0_step_1, + if(event_0_step_1 = 1, timestamp, null) AS event_0_latest_1 + FROM events AS e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview', '$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 7 day )) + GROUP BY person_id) funnel_query + WHERE 1 = 1 + AND (((steps_0))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_performed_event_sequence_and_clause_with_additional_event + ''' + + SELECT funnel_query.person_id AS id + FROM + (SELECT person_id, + max(if(event = '$pageview' + AND event_0_latest_0 < event_0_latest_1 + AND event_0_latest_1 <= event_0_latest_0 + INTERVAL 3 day, 2, 1)) = 2 AS steps_0, + countIf(timestamp > now() - INTERVAL 1 week + AND timestamp < now() + AND event = '$new_view' + AND 1=1) >= 1 AS performed_event_multiple_condition_None_level_level_0_level_1_0 + FROM + (SELECT person_id, + event, + properties, + distinct_id, timestamp, event_0_latest_0, + min(event_0_latest_1) over (PARTITION by person_id + ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) event_0_latest_1 + FROM + (SELECT if(notEmpty(pdi.distinct_id), pdi.person_id, e.person_id) AS person_id, + event, + properties, + distinct_id, timestamp, if(event = '$pageview' + AND timestamp > now() - INTERVAL 7 day, 1, 0) AS event_0_step_0, + if(event_0_step_0 = 1, timestamp, null) AS event_0_latest_0, + if(event = '$pageview' + AND timestamp > now() - INTERVAL 7 day, 1, 0) AS event_0_step_1, + if(event_0_step_1 = 1, timestamp, null) AS event_0_latest_1 + FROM events AS e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$new_view', '$pageview', '$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 1 week )) + GROUP BY person_id) funnel_query + WHERE 1 = 1 + AND (((steps_0) + OR (performed_event_multiple_condition_None_level_level_0_level_1_0))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_performed_event_sequence_with_person_properties + ''' + + SELECT person.person_id AS id + FROM + (SELECT person_id, + max(if(event = '$pageview' + AND event_0_latest_0 < event_0_latest_1 + AND event_0_latest_1 <= event_0_latest_0 + INTERVAL 3 day, 2, 1)) = 2 AS steps_0, + countIf(timestamp > now() - INTERVAL 1 week + AND timestamp < now() + AND event = '$pageview' + AND 1=1) >= 1 AS performed_event_multiple_condition_None_level_level_0_level_1_0 + FROM + (SELECT person_id, + event, + properties, + distinct_id, timestamp, event_0_latest_0, + min(event_0_latest_1) over (PARTITION by person_id + ORDER BY timestamp DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) event_0_latest_1 + FROM + (SELECT if(notEmpty(pdi.distinct_id), pdi.person_id, e.person_id) AS person_id, + event, + properties, + distinct_id, timestamp, if(event = '$pageview' + AND timestamp > now() - INTERVAL 7 day, 1, 0) AS event_0_step_0, + if(event_0_step_0 = 1, timestamp, null) AS event_0_latest_0, + if(event = '$pageview' + AND timestamp > now() - INTERVAL 7 day, 1, 0) AS event_0_step_1, + if(event_0_step_1 = 1, timestamp, null) AS event_0_latest_1 + FROM events AS e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview', '$pageview', '$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 1 week )) + GROUP BY person_id) funnel_query + INNER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id + FROM person + WHERE team_id = 99999 + AND id IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND ((has(['test@posthog.com'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', '')))) ) + GROUP BY id + HAVING max(is_deleted) = 0 + AND ((has(['test@posthog.com'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'email'), '^"|"$', '')))) SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = funnel_query.person_id + WHERE 1 = 1 + AND (((steps_0) + AND (performed_event_multiple_condition_None_level_level_0_level_1_0))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_performed_event_with_event_filters_and_explicit_date + ''' + + SELECT behavior_query.person_id AS id + FROM + (SELECT pdi.person_id AS person_id, + countIf(timestamp > 'explicit_timestamp' + AND timestamp < now() + AND event = '$pageview' + AND (has(['something'], replaceRegexpAll(JSONExtractRaw(properties, '$filter_prop'), '^"|"$', '')))) > 0 AS performed_event_condition_None_level_level_0_level_0_0 + FROM events e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 4 day + GROUP BY person_id) behavior_query + WHERE 1 = 1 + AND (((performed_event_condition_None_level_level_0_level_0_0))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_person + ''' + + SELECT if(behavior_query.person_id = '00000000-0000-0000-0000-000000000000', person.person_id, behavior_query.person_id) AS id + FROM + (SELECT pdi.person_id AS person_id, + countIf(timestamp > now() - INTERVAL 1 week + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_0_0 + FROM events e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 1 week + GROUP BY person_id) behavior_query + FULL OUTER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id, + argMax(properties, version) as person_props + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id + WHERE 1 = 1 + AND (((performed_event_condition_None_level_level_0_level_0_0) + OR (has(['test@posthog.com'], replaceRegexpAll(JSONExtractRaw(person_props, '$sample_field'), '^"|"$', ''))))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_person_materialized + ''' + + SELECT if(behavior_query.person_id = '00000000-0000-0000-0000-000000000000', person.person_id, behavior_query.person_id) AS id + FROM + (SELECT pdi.person_id AS person_id, + countIf(timestamp > now() - INTERVAL 1 week + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_0_0 + FROM events e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 1 week + GROUP BY person_id) behavior_query + FULL OUTER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id, + argMax(pmat_$sample_field, version) as pmat_$sample_field + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id + WHERE 1 = 1 + AND (((performed_event_condition_None_level_level_0_level_0_0) + OR (has(['test@posthog.com'], "pmat_$sample_field")))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_person_properties_with_pushdowns + ''' + + SELECT if(behavior_query.person_id = '00000000-0000-0000-0000-000000000000', person.person_id, behavior_query.person_id) AS id + FROM + (SELECT pdi.person_id AS person_id, + countIf(timestamp > now() - INTERVAL 1 day + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_0_level_0_0, + countIf(timestamp > now() - INTERVAL 2 week + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_0_level_1_0, + minIf(timestamp, ((replaceRegexpAll(JSONExtractRaw(properties, '$current_url'), '^"|"$', '') = 'https://posthog.com/feedback/123' + AND event = '$autocapture'))) >= now() - INTERVAL 2 week + AND minIf(timestamp, ((replaceRegexpAll(JSONExtractRaw(properties, '$current_url'), '^"|"$', '') = 'https://posthog.com/feedback/123' + AND event = '$autocapture'))) < now() as first_time_condition_None_level_level_0_level_1_level_0_0 + FROM events e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview', '$pageview', '$autocapture'] + GROUP BY person_id) behavior_query + FULL OUTER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id, + argMax(properties, version) as person_props + FROM person + WHERE team_id = 99999 + AND id IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND (((has(['test@posthog.com'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', ''))))) ) + GROUP BY id + HAVING max(is_deleted) = 0 + AND (((has(['test@posthog.com'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'email'), '^"|"$', ''))))) SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id + WHERE 1 = 1 + AND ((((performed_event_condition_None_level_level_0_level_0_level_0_0) + OR (performed_event_condition_None_level_level_0_level_0_level_1_0) + OR (has(['special'], replaceRegexpAll(JSONExtractRaw(person_props, 'name'), '^"|"$', '')))) + AND ((first_time_condition_None_level_level_0_level_1_level_0_0)))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_person_props_only + ''' + SELECT persons.id AS id + FROM + (SELECT argMax(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'email'), ''), 'null'), '^"|"$', ''), person.version) AS properties___email, + argMax(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'name'), ''), 'null'), '^"|"$', ''), person.version) AS properties___name, + person.id AS id + FROM person + WHERE and(equals(person.team_id, 99999), in(id, + (SELECT where_optimization.id AS id + FROM person AS where_optimization + WHERE and(equals(where_optimization.team_id, 99999), or(ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), 'test1@posthog.com'), 0), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), 'test2@posthog.com'), 0), and(ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', ''), 'test3'), 0), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'email'), ''), 'null'), '^"|"$', ''), 'test3@posthog.com'), 0))))))) + GROUP BY person.id + HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))), 0))) AS persons + WHERE or(or(ifNull(equals(persons.properties___email, 'test1@posthog.com'), 0), ifNull(equals(persons.properties___email, 'test2@posthog.com'), 0)), and(ifNull(equals(persons.properties___name, 'test3'), 0), ifNull(equals(persons.properties___email, 'test3@posthog.com'), 0))) + ORDER BY persons.id ASC + LIMIT 100 SETTINGS readonly=2, + max_execution_time=60, + allow_experimental_object_type=1, + format_csv_allow_double_quotes=0, + max_ast_elements=4000000, + max_expanded_ast_elements=4000000, + max_bytes_before_external_group_by=0 + ''' +# --- +# name: TestCohortQuery.test_person_props_only.1 + ''' + + SELECT id + FROM person + WHERE team_id = 99999 + AND id IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND (((has(['test1@posthog.com'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', ''))) + OR (has(['test2@posthog.com'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', '')))) + OR ((has(['test3'], replaceRegexpAll(JSONExtractRaw(properties, 'name'), '^"|"$', ''))) + AND (has(['test3@posthog.com'], replaceRegexpAll(JSONExtractRaw(properties, 'email'), '^"|"$', ''))))) ) + GROUP BY id + HAVING max(is_deleted) = 0 + AND (((has(['test1@posthog.com'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'email'), '^"|"$', ''))) + OR (has(['test2@posthog.com'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'email'), '^"|"$', '')))) + OR ((has(['test3'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', ''))) + AND (has(['test3@posthog.com'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'email'), '^"|"$', ''))))) SETTINGS optimize_aggregation_in_order = 1 + ''' +# --- +# name: TestCohortQuery.test_precalculated_cohort_filter_with_extra_filters + ''' + + SELECT count(DISTINCT person_id) + FROM cohortpeople + WHERE team_id = 99999 + AND cohort_id = 99999 + AND version = NULL + ''' +# --- +# name: TestCohortQuery.test_precalculated_cohort_filter_with_extra_filters.1 + ''' + /* cohort_calculation: */ + SELECT count(DISTINCT person_id) + FROM cohortpeople + WHERE team_id = 99999 + AND cohort_id = 99999 + AND version = 0 + ''' +# --- +# name: TestCohortQuery.test_precalculated_cohort_filter_with_extra_filters.2 + ''' + SELECT persons.id AS id + FROM + (SELECT argMax(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(person.properties, 'name'), ''), 'null'), '^"|"$', ''), person.version) AS properties___name, + person.id AS id + FROM person + WHERE and(equals(person.team_id, 99999), in(id, + (SELECT where_optimization.id AS id + FROM person AS where_optimization + WHERE and(equals(where_optimization.team_id, 99999), or(ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', ''), 'test'), 0), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(where_optimization.properties, 'name'), ''), 'null'), '^"|"$', ''), 'test2'), 0)))))) + GROUP BY person.id + HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))), 0))) AS persons + WHERE or(ifNull(equals(persons.properties___name, 'test'), 0), ifNull(equals(persons.properties___name, 'test2'), 0)) + ORDER BY persons.id ASC + LIMIT 100 SETTINGS readonly=2, + max_execution_time=60, + allow_experimental_object_type=1, + format_csv_allow_double_quotes=0, + max_ast_elements=4000000, + max_expanded_ast_elements=4000000, + max_bytes_before_external_group_by=0 + ''' +# --- +# name: TestCohortQuery.test_precalculated_cohort_filter_with_extra_filters.3 + ''' + + SELECT id + FROM person + WHERE team_id = 99999 + AND id IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND ((((has(['test'], replaceRegexpAll(JSONExtractRaw(properties, 'name'), '^"|"$', ''))))) + OR (has(['test2'], replaceRegexpAll(JSONExtractRaw(properties, 'name'), '^"|"$', '')))) ) + GROUP BY id + HAVING max(is_deleted) = 0 + AND ((((has(['test'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', ''))))) + OR (has(['test2'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', '')))) SETTINGS optimize_aggregation_in_order = 1 + ''' +# --- +# name: TestCohortQuery.test_static_cohort_filter + ''' + + SELECT count(DISTINCT person_id) + FROM person_static_cohort + WHERE team_id = 99999 + AND cohort_id = 99999 + ''' +# --- +# name: TestCohortQuery.test_static_cohort_filter.1 + ''' + + SELECT person.person_id AS id + FROM + (SELECT *, + id AS person_id + FROM + (SELECT id + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1)) person + WHERE 1 = 1 + AND (((id IN + (SELECT person_id as id + FROM person_static_cohort + WHERE cohort_id = 99999 + AND team_id = 99999)))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_static_cohort_filter_with_extra + ''' + + SELECT count(DISTINCT person_id) + FROM person_static_cohort + WHERE team_id = 99999 + AND cohort_id = 99999 + ''' +# --- +# name: TestCohortQuery.test_static_cohort_filter_with_extra.1 + ''' + + SELECT if(behavior_query.person_id = '00000000-0000-0000-0000-000000000000', person.person_id, behavior_query.person_id) AS id + FROM + (SELECT pdi.person_id AS person_id, + countIf(timestamp > now() - INTERVAL 1 week + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_1_0 + FROM events e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 1 week + GROUP BY person_id) behavior_query + FULL OUTER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id + WHERE 1 = 1 + AND (((id IN + (SELECT person_id as id + FROM person_static_cohort + WHERE cohort_id = 99999 + AND team_id = 99999)) + AND (performed_event_condition_None_level_level_0_level_1_0))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_static_cohort_filter_with_extra.2 + ''' + + SELECT if(behavior_query.person_id = '00000000-0000-0000-0000-000000000000', person.person_id, behavior_query.person_id) AS id + FROM + (SELECT pdi.person_id AS person_id, + countIf(timestamp > now() - INTERVAL 1 week + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_1_level_0_0 + FROM events e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 1 week + GROUP BY person_id) behavior_query + FULL OUTER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id + WHERE 1 = 1 + AND ((((id IN + (SELECT person_id as id + FROM person_static_cohort + WHERE cohort_id = 99999 + AND team_id = 99999))) + OR ((performed_event_condition_None_level_level_0_level_1_level_0_0)))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- +# name: TestCohortQuery.test_unwrapping_static_cohort_filter_hidden_in_layers_of_cohorts + ''' + + SELECT count(DISTINCT person_id) + FROM person_static_cohort + WHERE team_id = 99999 + AND cohort_id = 99999 + ''' +# --- +# name: TestCohortQuery.test_unwrapping_static_cohort_filter_hidden_in_layers_of_cohorts.1 + ''' + + SELECT if(behavior_query.person_id = '00000000-0000-0000-0000-000000000000', person.person_id, behavior_query.person_id) AS id + FROM + (SELECT pdi.person_id AS person_id, + countIf(timestamp > now() - INTERVAL 7 day + AND timestamp < now() + AND event = '$new_view' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_0_level_0_0, + countIf(timestamp > now() - INTERVAL 1 week + AND timestamp < now() + AND event = '$pageview' + AND 1=1) > 0 AS performed_event_condition_None_level_level_0_level_1_0 + FROM events e + LEFT OUTER 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) AS pdi ON e.distinct_id = pdi.distinct_id + WHERE team_id = 99999 + AND event IN ['$new_view', '$pageview'] + AND timestamp <= now() + AND timestamp >= now() - INTERVAL 7 day + GROUP BY person_id) behavior_query + FULL OUTER JOIN + (SELECT *, + id AS person_id + FROM + (SELECT id + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1)) person ON person.person_id = behavior_query.person_id + WHERE 1 = 1 + AND ((((performed_event_condition_None_level_level_0_level_0_level_0_0) + AND (id NOT IN + (SELECT person_id as id + FROM person_static_cohort + WHERE cohort_id = 99999 + AND team_id = 99999))) + OR (performed_event_condition_None_level_level_0_level_1_0))) SETTINGS optimize_aggregation_in_order = 1, + join_algorithm = 'auto' + ''' +# --- diff --git a/ee/clickhouse/queries/test/__snapshots__/test_event_query.ambr b/ee/clickhouse/queries/test/__snapshots__/test_event_query.ambr new file mode 100644 index 0000000000000..3abda0aeb34ad --- /dev/null +++ b/ee/clickhouse/queries/test/__snapshots__/test_event_query.ambr @@ -0,0 +1,124 @@ +# serializer version: 1 +# name: TestEventQuery.test_account_filters + ''' + + SELECT count(DISTINCT person_id) + FROM cohortpeople + WHERE team_id = 99999 + AND cohort_id = 99999 + AND version = NULL + ''' +# --- +# name: TestEventQuery.test_account_filters.1 + ''' + /* cohort_calculation: */ + SELECT count(DISTINCT person_id) + FROM cohortpeople + WHERE team_id = 99999 + AND cohort_id = 99999 + AND version = 0 + ''' +# --- +# name: TestEventQuery.test_account_filters.2 + ''' + SELECT e.timestamp as timestamp, + if(notEmpty(pdi.distinct_id), pdi.person_id, e.person_id) as person_id + FROM events e + LEFT OUTER JOIN + (SELECT distinct_id, + argMax(person_id, version) as person_id + FROM person_distinct_id2 + WHERE team_id = 99999 + AND distinct_id IN + (SELECT distinct_id + FROM events + WHERE team_id = 99999 + AND event = 'event_name' + AND toTimeZone(timestamp, 'UTC') >= toDateTime(toStartOfDay(toDateTime('2021-01-14 00:00:00', 'UTC')), 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2021-01-21 23:59:59', 'UTC')) + GROUP BY distinct_id + HAVING argMax(is_deleted, version) = 0) AS pdi ON e.distinct_id = pdi.distinct_id + INNER JOIN + (SELECT id + FROM person + WHERE team_id = 99999 + AND id IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND (has(['Jane'], replaceRegexpAll(JSONExtractRaw(properties, 'name'), '^"|"$', ''))) ) + GROUP BY id + HAVING max(is_deleted) = 0 + AND (has(['Jane'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', ''))) SETTINGS optimize_aggregation_in_order = 1) person ON person.id = pdi.person_id + WHERE team_id = 99999 + AND event = 'event_name' + AND toTimeZone(timestamp, 'UTC') >= toDateTime(toStartOfDay(toDateTime('2021-01-14 00:00:00', 'UTC')), 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2021-01-21 23:59:59', 'UTC') + ''' +# --- +# name: TestEventQuery.test_basic_event_filter + ''' + SELECT e.timestamp as timestamp + FROM events e + WHERE team_id = 99999 + AND event = 'viewed' + AND toTimeZone(timestamp, 'UTC') >= toDateTime(toStartOfDay(toDateTime('2021-05-01 00:00:00', 'UTC')), 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2021-05-07 23:59:59', 'UTC') + ''' +# --- +# name: TestEventQuery.test_cohort_filter + ''' + SELECT e.timestamp as timestamp, + if(notEmpty(pdi.distinct_id), pdi.person_id, e.person_id) as person_id + FROM events e + LEFT OUTER JOIN + (SELECT distinct_id, + argMax(person_id, version) as person_id + FROM person_distinct_id2 + WHERE team_id = 99999 + AND distinct_id IN + (SELECT distinct_id + FROM events + WHERE team_id = 99999 + AND event = 'viewed' + AND toTimeZone(timestamp, 'UTC') >= toDateTime(toStartOfDay(toDateTime('2021-05-01 00:00:00', 'UTC')), 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2021-05-07 23:59:59', 'UTC')) + GROUP BY distinct_id + HAVING argMax(is_deleted, version) = 0) AS pdi ON e.distinct_id = pdi.distinct_id + INNER JOIN + (SELECT id + FROM person + WHERE team_id = 99999 + GROUP BY id + HAVING max(is_deleted) = 0 SETTINGS optimize_aggregation_in_order = 1) person ON person.id = pdi.person_id + WHERE team_id = 99999 + AND event = 'viewed' + AND toTimeZone(timestamp, 'UTC') >= toDateTime(toStartOfDay(toDateTime('2021-05-01 00:00:00', 'UTC')), 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2021-05-07 23:59:59', 'UTC') + AND (if(notEmpty(pdi.distinct_id), pdi.person_id, e.person_id) IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND id IN + (SELECT id + FROM person + WHERE team_id = 99999 + AND ((has(['test'], replaceRegexpAll(JSONExtractRaw(properties, 'name'), '^"|"$', '')))) ) + GROUP BY id + HAVING max(is_deleted) = 0 + AND ((has(['test'], replaceRegexpAll(JSONExtractRaw(argMax(person.properties, version), 'name'), '^"|"$', '')))) SETTINGS optimize_aggregation_in_order = 1)) + ''' +# --- +# name: TestEventQuery.test_denormalised_props + ''' + SELECT e.timestamp as timestamp, + e."mat_test_prop" as "mat_test_prop" + FROM events e + WHERE team_id = 99999 + AND event = 'user signed up' + AND toTimeZone(timestamp, 'UTC') >= toDateTime(toStartOfDay(toDateTime('2020-01-01 00:00:00', 'UTC')), 'UTC') + AND toTimeZone(timestamp, 'UTC') <= toDateTime('2020-01-14 23:59:59', 'UTC') + AND ((has(['hi'], "mat_test_prop")) + AND (has(['hi'], "mat_test_prop"))) + ''' +# ---