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.

chore(marketing-analytics): snapshot the attribution SQL (#91071)

Javier Bahamondes8ede18cadee4
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.ambr
new file mode 100644
index 000000000000..3feeec68a052
--- /dev/null
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_paths_query_runner.ambr
@@ -0,0 +1,130 @@
+# serializer version: 1
+# name: TestMarketingAnalyticsAttributionPathsQueryRunner.test_attribution_paths_sql_0_campaign
+ '''
+ WITH person_arrays AS (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_14)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_15)s), ''), 'null'), '^"|"$', ''), %(hogql_val_16)s), 0.0)), and(equals(events.event, %(hogql_val_17)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_18)s), %(hogql_val_19)s), toDateTime(%(hogql_val_20)s, %(hogql_val_21)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_22)s), %(hogql_val_23)s), toDateTime(%(hogql_val_24)s, %(hogql_val_25)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), toString(ifNull(events__session.`$entry_utm_campaign`, %(hogql_val_26)s))), and(equals(events.event, %(hogql_val_27)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -500) AS touchpoints
+ FROM events LEFT JOIN (
+ SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_0)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_1)s), %(hogql_val_2)s) AS `$entry_utm_campaign`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), or(and(ifNull(greaterOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_3)s), minus(toDateTime(%(hogql_val_4)s, %(hogql_val_5)s), toIntervalDay(3))), 0), ifNull(lessOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_6)s), plus(toDateTime(%(hogql_val_7)s, %(hogql_val_8)s), toIntervalDay(3))), 0)), and(greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_9)s, %(hogql_val_10)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_11)s, %(hogql_val_12)s), toIntervalDay(3))))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_13)s)), events__session.session_id_v7) 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
+ FROM 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_28)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_29)s), %(hogql_val_30)s), toDateTime(%(hogql_val_31)s, %(hogql_val_32)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_33)s), %(hogql_val_34)s), toDateTime(%(hogql_val_35)s, %(hogql_val_36)s)))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_37)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_38)s), %(hogql_val_39)s), toDateTime(%(hogql_val_40)s, %(hogql_val_41)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_42)s), %(hogql_val_43)s), toDateTime(%(hogql_val_44)s, %(hogql_val_45)s))), and(equals(events.event, %(hogql_val_46)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_47)s, %(hogql_val_48)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_49)s, %(hogql_val_50)s)))))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)
+ HAVING greater(length(conversions), 0)), per_conversion_path AS MATERIALIZED (
+ SELECT (person_arrays.conversions[i]).2 AS conv_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, (person_arrays.conversions[i]).1), 0), ifNull(greaterOrEquals(t.1, minus((person_arrays.conversions[i]).1, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.2, tps) AS path_full, length(path_full) AS orig_len, arraySlice(path_full, -10) AS path, ifNull(greater(orig_len, 10), 0) AS truncated
+ FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), path_rows AS (
+ SELECT per_conversion_path.path AS path, count() AS conversions, sum(per_conversion_path.conv_value) AS conversion_value, max(per_conversion_path.truncated) AS path_truncated, 1 AS footer_key
+ FROM per_conversion_path
+ WHERE ifNull(greater(per_conversion_path.orig_len, 0), 0)
+ GROUP BY per_conversion_path.path), paths_footer AS (
+ SELECT count() AS total_conversions, countIf(ifNull(greater(per_conversion_path.orig_len, 0), 0)) AS attributed_conversions, 1 AS footer_key
+ FROM per_conversion_path)
+ SELECT path_rows.path AS path, path_rows.conversions AS conversions, path_rows.conversion_value AS conversion_value, path_rows.path_truncated AS path_truncated, paths_footer.total_conversions AS total_conversions, paths_footer.attributed_conversions AS attributed_conversions, path_rows.footer_key AS footer_key
+ FROM paths_footer LEFT JOIN path_rows ON equals(paths_footer.footer_key, path_rows.footer_key)
+ ORDER BY conversions DESC, path ASC
+ LIMIT 51
+ OFFSET 0
+ '''
+# ---
+# name: TestMarketingAnalyticsAttributionPathsQueryRunner.test_attribution_paths_sql_1_source
+ '''
+ WITH person_arrays AS (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_14)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_15)s), ''), 'null'), '^"|"$', ''), %(hogql_val_16)s), 0.0)), and(equals(events.event, %(hogql_val_17)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_18)s), %(hogql_val_19)s), toDateTime(%(hogql_val_20)s, %(hogql_val_21)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_22)s), %(hogql_val_23)s), toDateTime(%(hogql_val_24)s, %(hogql_val_25)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_26)s)), events__session.`$entry_utm_source`, %(hogql_val_27)s)), [%(hogql_val_28)s, %(hogql_val_29)s, %(hogql_val_30)s]), %(hogql_val_31)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_32)s)), events__session.`$entry_utm_source`, %(hogql_val_33)s)), [%(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s, %(hogql_val_40)s, %(hogql_val_41)s]), %(hogql_val_42)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_43)s)), events__session.`$entry_utm_source`, %(hogql_val_44)s)), [%(hogql_val_45)s]), %(hogql_val_46)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_47)s)), events__session.`$entry_utm_source`, %(hogql_val_48)s)), [%(hogql_val_49)s, %(hogql_val_50)s, %(hogql_val_51)s, %(hogql_val_52)s, %(hogql_val_53)s, %(hogql_val_54)s, %(hogql_val_55)s, %(hogql_val_56)s, %(hogql_val_57)s]), %(hogql_val_58)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_59)s)), events__session.`$entry_utm_source`, %(hogql_val_60)s)))))), and(equals(events.event, %(hogql_val_61)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -500) AS touchpoints
+ FROM events LEFT JOIN (
+ SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_0)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_1)s), %(hogql_val_2)s) AS `$entry_utm_source`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), or(and(ifNull(greaterOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_3)s), minus(toDateTime(%(hogql_val_4)s, %(hogql_val_5)s), toIntervalDay(3))), 0), ifNull(lessOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_6)s), plus(toDateTime(%(hogql_val_7)s, %(hogql_val_8)s), toIntervalDay(3))), 0)), and(greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_9)s, %(hogql_val_10)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_11)s, %(hogql_val_12)s), toIntervalDay(3))))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_13)s)), events__session.session_id_v7) 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
+ FROM 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_62)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_63)s), %(hogql_val_64)s), toDateTime(%(hogql_val_65)s, %(hogql_val_66)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_67)s), %(hogql_val_68)s), toDateTime(%(hogql_val_69)s, %(hogql_val_70)s)))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_71)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_72)s), %(hogql_val_73)s), toDateTime(%(hogql_val_74)s, %(hogql_val_75)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_76)s), %(hogql_val_77)s), toDateTime(%(hogql_val_78)s, %(hogql_val_79)s))), and(equals(events.event, %(hogql_val_80)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_81)s, %(hogql_val_82)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_83)s, %(hogql_val_84)s)))))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)
+ HAVING greater(length(conversions), 0)), per_conversion_path AS MATERIALIZED (
+ SELECT (person_arrays.conversions[i]).2 AS conv_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, (person_arrays.conversions[i]).1), 0), ifNull(greaterOrEquals(t.1, minus((person_arrays.conversions[i]).1, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.2, tps) AS path_full, length(path_full) AS orig_len, arraySlice(path_full, -10) AS path, ifNull(greater(orig_len, 10), 0) AS truncated
+ FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), path_rows AS (
+ SELECT per_conversion_path.path AS path, count() AS conversions, sum(per_conversion_path.conv_value) AS conversion_value, max(per_conversion_path.truncated) AS path_truncated, 1 AS footer_key
+ FROM per_conversion_path
+ WHERE ifNull(greater(per_conversion_path.orig_len, 0), 0)
+ GROUP BY per_conversion_path.path), paths_footer AS (
+ SELECT count() AS total_conversions, countIf(ifNull(greater(per_conversion_path.orig_len, 0), 0)) AS attributed_conversions, 1 AS footer_key
+ FROM per_conversion_path)
+ SELECT path_rows.path AS path, path_rows.conversions AS conversions, path_rows.conversion_value AS conversion_value, path_rows.path_truncated AS path_truncated, paths_footer.total_conversions AS total_conversions, paths_footer.attributed_conversions AS attributed_conversions, path_rows.footer_key AS footer_key
+ FROM paths_footer LEFT JOIN path_rows ON equals(paths_footer.footer_key, path_rows.footer_key)
+ ORDER BY conversions DESC, path ASC
+ LIMIT 51
+ OFFSET 0
+ '''
+# ---
+# name: TestMarketingAnalyticsAttributionPathsQueryRunner.test_attribution_paths_sql_2_channel
+ '''
+ WITH person_arrays AS (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_131)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_132)s), ''), 'null'), '^"|"$', ''), %(hogql_val_133)s), 0.0)), and(equals(events.event, %(hogql_val_134)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_135)s), %(hogql_val_136)s), toDateTime(%(hogql_val_137)s, %(hogql_val_138)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_139)s), %(hogql_val_140)s), toDateTime(%(hogql_val_141)s, %(hogql_val_142)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(notEmpty(ifNull(events__session.`$channel_type`, %(hogql_val_143)s)), events__session.`$channel_type`, %(hogql_val_144)s)), and(equals(events.event, %(hogql_val_145)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -500) AS touchpoints
+ FROM events LEFT JOIN (
+ SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_0)s)) AS `$start_timestamp`, multiIf(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_1)s), %(hogql_val_2)s), %(hogql_val_3)s), %(hogql_val_4)s)), %(hogql_val_5)s), %(hogql_val_6)s, or(in(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_7)s), %(hogql_val_8)s), %(hogql_val_9)s), %(hogql_val_10)s)), tuple(%(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s, %(hogql_val_16)s)), startsWith(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_17)s), %(hogql_val_18)s), %(hogql_val_19)s), %(hogql_val_20)s)), %(hogql_val_21)s), isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_gclid), %(hogql_val_22)s), %(hogql_val_23)s)), isNotNull(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_gad_source), %(hogql_val_24)s), %(hogql_val_25)s), %(hogql_val_26)s), %(hogql_val_27)s))), coalesce(coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_28)s), %(hogql_val_29)s), %(hogql_val_30)s), %(hogql_val_31)s)), ''), 'source')) , dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (cutToFirstSignificantSubdomain(coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_28)s), %(hogql_val_29)s), %(hogql_val_30)s), %(hogql_val_31)s)), '')), 'source'))), if(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_32)s), %(hogql_val_33)s), %(hogql_val_34)s), %(hogql_val_35)s)), %(hogql_val_36)s), %(hogql_val_37)s, NULL), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_38)s), %(hogql_val_39)s), %(hogql_val_40)s), %(hogql_val_41)s)), ''), 'medium')), coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_42)s), %(hogql_val_43)s), ''), 'source')) , dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (cutToFirstSignificantSubdomain(coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_42)s), %(hogql_val_43)s), '')), 'source'))), multiIf(ifNull(equals(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_gad_source), %(hogql_val_44)s), %(hogql_val_45)s), %(hogql_val_46)s), %(hogql_val_47)s), %(hogql_val_48)s), 0), %(hogql_val_49)s, match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_50)s), %(hogql_val_51)s), %(hogql_val_52)s), %(hogql_val_53)s)), %(hogql_val_54)s), %(hogql_val_55)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_56)s), %(hogql_val_57)s)), %(hogql_val_58)s, %(hogql_val_59)s)), and(ifNull(equals(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_60)s), %(hogql_val_61)s), %(hogql_val_62)s), 0), isNull(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_63)s), %(hogql_val_64)s), %(hogql_val_65)s), %(hogql_val_66)s))), or(isNull(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_67)s), %(hogql_val_68)s), %(hogql_val_69)s), %(hogql_val_70)s))), in(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_71)s), %(hogql_val_72)s), %(hogql_val_73)s), %(hogql_val_74)s)), tuple(%(hogql_val_75)s, %(hogql_val_76)s, %(hogql_val_77)s))), not(isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_78)s), %(hogql_val_79)s)))), %(hogql_val_80)s, coalesce(coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_81)s), %(hogql_val_82)s), %(hogql_val_83)s), %(hogql_val_84)s)), ''), 'source')), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (cutToFirstSignificantSubdomain(coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_81)s), %(hogql_val_82)s), %(hogql_val_83)s), %(hogql_val_84)s)), '')), 'source'))), if(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_85)s), %(hogql_val_86)s), %(hogql_val_87)s), %(hogql_val_88)s)), %(hogql_val_89)s), %(hogql_val_90)s, NULL), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_91)s), %(hogql_val_92)s), %(hogql_val_93)s), %(hogql_val_94)s)), ''), 'medium')), coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_95)s), %(hogql_val_96)s), ''), 'source')), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (cutToFirstSignificantSubdomain(coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_95)s), %(hogql_val_96)s), '')), 'source'))), multiIf(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_97)s), %(hogql_val_98)s), %(hogql_val_99)s), %(hogql_val_100)s)), %(hogql_val_101)s), %(hogql_val_102)s, match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_103)s), %(hogql_val_104)s), %(hogql_val_105)s), %(hogql_val_106)s)), %(hogql_val_107)s), %(hogql_val_108)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_109)s), %(hogql_val_110)s)), %(hogql_val_111)s, ifNull(equals(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_112)s), %(hogql_val_113)s), %(hogql_val_114)s), 0), %(hogql_val_115)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_116)s), %(hogql_val_117)s)), %(hogql_val_118)s, %(hogql_val_119)s))) AS `$channel_type`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), or(and(ifNull(greaterOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_120)s), minus(toDateTime(%(hogql_val_121)s, %(hogql_val_122)s), toIntervalDay(3))), 0), ifNull(lessOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_123)s), plus(toDateTime(%(hogql_val_124)s, %(hogql_val_125)s), toIntervalDay(3))), 0)), and(greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_126)s, %(hogql_val_127)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_128)s, %(hogql_val_129)s), toIntervalDay(3))))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_130)s)), events__session.session_id_v7) 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
+ FROM 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_146)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_147)s), %(hogql_val_148)s), toDateTime(%(hogql_val_149)s, %(hogql_val_150)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_151)s), %(hogql_val_152)s), toDateTime(%(hogql_val_153)s, %(hogql_val_154)s)))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_155)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_156)s), %(hogql_val_157)s), toDateTime(%(hogql_val_158)s, %(hogql_val_159)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_160)s), %(hogql_val_161)s), toDateTime(%(hogql_val_162)s, %(hogql_val_163)s))), and(equals(events.event, %(hogql_val_164)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_165)s, %(hogql_val_166)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_167)s, %(hogql_val_168)s)))))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)
+ HAVING greater(length(conversions), 0)), per_conversion_path AS MATERIALIZED (
+ SELECT (person_arrays.conversions[i]).2 AS conv_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, (person_arrays.conversions[i]).1), 0), ifNull(greaterOrEquals(t.1, minus((person_arrays.conversions[i]).1, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.2, tps) AS path_full, length(path_full) AS orig_len, arraySlice(path_full, -10) AS path, ifNull(greater(orig_len, 10), 0) AS truncated
+ FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), path_rows AS (
+ SELECT per_conversion_path.path AS path, count() AS conversions, sum(per_conversion_path.conv_value) AS conversion_value, max(per_conversion_path.truncated) AS path_truncated, 1 AS footer_key
+ FROM per_conversion_path
+ WHERE ifNull(greater(per_conversion_path.orig_len, 0), 0)
+ GROUP BY per_conversion_path.path), paths_footer AS (
+ SELECT count() AS total_conversions, countIf(ifNull(greater(per_conversion_path.orig_len, 0), 0)) AS attributed_conversions, 1 AS footer_key
+ FROM per_conversion_path)
+ SELECT path_rows.path AS path, path_rows.conversions AS conversions, path_rows.conversion_value AS conversion_value, path_rows.path_truncated AS path_truncated, paths_footer.total_conversions AS total_conversions, paths_footer.attributed_conversions AS attributed_conversions, path_rows.footer_key AS footer_key
+ FROM paths_footer LEFT JOIN path_rows ON equals(paths_footer.footer_key, path_rows.footer_key)
+ ORDER BY conversions DESC, path ASC
+ LIMIT 51
+ OFFSET 0
+ '''
+# ---
diff --git a/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.ambr b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.ambr
new file mode 100644
index 000000000000..bf038bd0c40a
--- /dev/null
+++ b/products/marketing_analytics/backend/hogql_queries/__snapshots__/test_attribution_table_query_runner.ambr
@@ -0,0 +1,190 @@
+# serializer version: 1
+# name: TestMarketingAnalyticsAttributionQueryRunner.test_attribution_table_sql_0_campaign
+ '''
+ WITH influenced_reach AS (
+ SELECT toString(ifNull(events__session.`$entry_utm_campaign`, %(hogql_val_8)s)) AS breakdown_value, uniq(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)) AS visitors
+ FROM 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, 420)
+ 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) LEFT JOIN (
+ SELECT nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_0)s), %(hogql_val_1)s) AS `$entry_utm_campaign`, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_2)s)) AS `$start_timestamp`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_3)s, %(hogql_val_4)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_5)s, %(hogql_val_6)s), toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_7)s)), events__session.session_id_v7)
+ WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_9)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_10)s, %(hogql_val_11)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_12)s, %(hogql_val_13)s)))
+ GROUP BY breakdown_value), person_arrays AS (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_28)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_29)s), ''), 'null'), '^"|"$', ''), %(hogql_val_30)s), 0.0)), and(equals(events.event, %(hogql_val_31)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_32)s), %(hogql_val_33)s), toDateTime(%(hogql_val_34)s, %(hogql_val_35)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_36)s), %(hogql_val_37)s), toDateTime(%(hogql_val_38)s, %(hogql_val_39)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), toString(ifNull(events__session.`$entry_utm_campaign`, %(hogql_val_40)s))), and(equals(events.event, %(hogql_val_41)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -500) AS touchpoints
+ FROM events LEFT JOIN (
+ SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_14)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_15)s), %(hogql_val_16)s) AS `$entry_utm_campaign`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), or(and(ifNull(greaterOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_17)s), minus(toDateTime(%(hogql_val_18)s, %(hogql_val_19)s), toIntervalDay(3))), 0), ifNull(lessOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_20)s), plus(toDateTime(%(hogql_val_21)s, %(hogql_val_22)s), toIntervalDay(3))), 0)), and(greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_23)s, %(hogql_val_24)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_25)s, %(hogql_val_26)s), toIntervalDay(3))))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_27)s)), events__session.session_id_v7) 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
+ FROM 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_42)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_43)s), %(hogql_val_44)s), toDateTime(%(hogql_val_45)s, %(hogql_val_46)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_47)s), %(hogql_val_48)s), toDateTime(%(hogql_val_49)s, %(hogql_val_50)s)))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_51)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_52)s), %(hogql_val_53)s), toDateTime(%(hogql_val_54)s, %(hogql_val_55)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_56)s), %(hogql_val_57)s), toDateTime(%(hogql_val_58)s, %(hogql_val_59)s))), and(equals(events.event, %(hogql_val_60)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_61)s, %(hogql_val_62)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_63)s, %(hogql_val_64)s)))))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)
+ HAVING greater(length(conversions), 0)), per_conversion AS MATERIALIZED (
+ SELECT person_arrays.person_id AS person_id, i, (person_arrays.conversions[i]).1 AS conversion_time, (person_arrays.conversions[i]).2 AS conversion_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, conversion_time), 0), ifNull(greaterOrEquals(t.1, minus(conversion_time, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.1, tps) AS touch_ts, arrayMap(t -> t.2, tps) AS dims, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMin(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMin(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_first, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMax(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMax(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_last, arrayMap(_x -> divide(1.0, greatest(accurateCastOrNull(length(touch_ts), %(hogql_val_65)s), 1.0)), touch_ts) AS w_linear, arrayMap(w -> divide(w, greatest(arraySum(arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)), 1e-06)), arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)) AS w_decay, if(ifNull(equals(length(touch_ts), 0), 0), [], if(ifNull(equals(length(touch_ts), 1), 0), [1.0], if(ifNull(equals(length(touch_ts), 2), 0), [0.5, 0.5], arrayMap(w -> divide(w, greatest(arraySum(arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_66)s), 2.0), 1.0)))), touch_ts))), 1e-06)), arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_67)s), 2.0), 1.0)))), touch_ts)))))) AS w_position
+ FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), per_touchpoint AS (
+ SELECT per_conversion.person_id AS person_id, per_conversion.i AS i, per_conversion.conversion_value AS conversion_value, per_conversion.dims[k] AS breakdown_value, per_conversion.w_first[k] AS w_first, per_conversion.w_last[k] AS w_last, per_conversion.w_linear[k] AS w_linear, per_conversion.w_decay[k] AS w_decay, per_conversion.w_position[k] AS w_position
+ FROM per_conversion ARRAY JOIN arrayEnumerate(per_conversion.touch_ts) AS k), per_conversion_dim AS (
+ SELECT per_touchpoint.breakdown_value AS breakdown_value, per_touchpoint.person_id AS person_id, per_touchpoint.i AS i, any(per_touchpoint.conversion_value) AS conv_value, sum(per_touchpoint.w_first) AS w_first, sum(per_touchpoint.w_last) AS w_last, sum(per_touchpoint.w_linear) AS w_linear, sum(per_touchpoint.w_decay) AS w_decay, sum(per_touchpoint.w_position) AS w_position
+ FROM per_touchpoint
+ GROUP BY per_touchpoint.breakdown_value, per_touchpoint.person_id, per_touchpoint.i), attribution_totals AS (
+ SELECT per_conversion_dim.breakdown_value AS breakdown_value, count() AS influenced_conversions, sum(per_conversion_dim.conv_value) AS influenced_value, sum(per_conversion_dim.w_first) AS w_first_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_first)) AS w_first_value, sum(per_conversion_dim.w_last) AS w_last_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_last)) AS w_last_value, sum(per_conversion_dim.w_linear) AS w_linear_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_linear)) AS w_linear_value, sum(per_conversion_dim.w_decay) AS w_decay_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_decay)) AS w_decay_value, sum(per_conversion_dim.w_position) AS w_position_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_position)) AS w_position_value
+ FROM per_conversion_dim
+ GROUP BY per_conversion_dim.breakdown_value), attribution_rows AS (
+ SELECT coalesce(nullIf(attribution_totals.breakdown_value, %(hogql_val_68)s), influenced_reach.breakdown_value) AS breakdown_value, ifNull(influenced_reach.visitors, 0) AS visitors, ifNull(attribution_totals.influenced_conversions, 0) AS influenced_conversions, ifNull(attribution_totals.influenced_value, 0.0) AS influenced_value, ifNull(attribution_totals.w_first_conversions, 0.0) AS w_first_conversions, ifNull(attribution_totals.w_first_value, 0.0) AS w_first_value, ifNull(attribution_totals.w_last_conversions, 0.0) AS w_last_conversions, ifNull(attribution_totals.w_last_value, 0.0) AS w_last_value, ifNull(attribution_totals.w_linear_conversions, 0.0) AS w_linear_conversions, ifNull(attribution_totals.w_linear_value, 0.0) AS w_linear_value, ifNull(attribution_totals.w_decay_conversions, 0.0) AS w_decay_conversions, ifNull(attribution_totals.w_decay_value, 0.0) AS w_decay_value, ifNull(attribution_totals.w_position_conversions, 0.0) AS w_position_conversions, ifNull(attribution_totals.w_position_value, 0.0) AS w_position_value, 1 AS footer_key
+ FROM attribution_totals FULL OUTER JOIN influenced_reach ON equals(attribution_totals.breakdown_value, influenced_reach.breakdown_value)), attribution_footer AS (
+ SELECT count() AS total_conversions, countIf(ifNull(greater(length(per_conversion.tps), 0), 0)) AS attributed_conversions, 1 AS footer_key
+ FROM per_conversion)
+ SELECT attribution_rows.breakdown_value AS breakdown_value, attribution_rows.visitors AS visitors, attribution_rows.influenced_conversions AS influenced_conversions, attribution_rows.influenced_value AS influenced_value, attribution_rows.w_first_conversions AS w_first_conversions, attribution_rows.w_first_value AS w_first_value, attribution_rows.w_last_conversions AS w_last_conversions, attribution_rows.w_last_value AS w_last_value, attribution_rows.w_linear_conversions AS w_linear_conversions, attribution_rows.w_linear_value AS w_linear_value, attribution_rows.w_decay_conversions AS w_decay_conversions, attribution_rows.w_decay_value AS w_decay_value, attribution_rows.w_position_conversions AS w_position_conversions, attribution_rows.w_position_value AS w_position_value, attribution_footer.total_conversions AS total_conversions, attribution_footer.attributed_conversions AS attributed_conversions, attribution_rows.footer_key AS footer_key
+ FROM attribution_footer LEFT JOIN attribution_rows ON equals(attribution_footer.footer_key, attribution_rows.footer_key)
+ ORDER BY influenced_conversions DESC, visitors DESC
+ LIMIT 101
+ OFFSET 0
+ '''
+# ---
+# name: TestMarketingAnalyticsAttributionQueryRunner.test_attribution_table_sql_1_source
+ '''
+ WITH influenced_reach AS (
+ SELECT if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_8)s)), events__session.`$entry_utm_source`, %(hogql_val_9)s)), [%(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s]), %(hogql_val_13)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_14)s)), events__session.`$entry_utm_source`, %(hogql_val_15)s)), [%(hogql_val_16)s, %(hogql_val_17)s, %(hogql_val_18)s, %(hogql_val_19)s, %(hogql_val_20)s, %(hogql_val_21)s, %(hogql_val_22)s, %(hogql_val_23)s]), %(hogql_val_24)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_25)s)), events__session.`$entry_utm_source`, %(hogql_val_26)s)), [%(hogql_val_27)s]), %(hogql_val_28)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_29)s)), events__session.`$entry_utm_source`, %(hogql_val_30)s)), [%(hogql_val_31)s, %(hogql_val_32)s, %(hogql_val_33)s, %(hogql_val_34)s, %(hogql_val_35)s, %(hogql_val_36)s, %(hogql_val_37)s, %(hogql_val_38)s, %(hogql_val_39)s]), %(hogql_val_40)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_41)s)), events__session.`$entry_utm_source`, %(hogql_val_42)s))))) AS breakdown_value, uniq(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)) AS visitors
+ FROM 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, 420)
+ 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) LEFT JOIN (
+ SELECT nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_0)s), %(hogql_val_1)s) AS `$entry_utm_source`, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_2)s)) AS `$start_timestamp`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_3)s, %(hogql_val_4)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_5)s, %(hogql_val_6)s), toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_7)s)), events__session.session_id_v7)
+ WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_43)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_44)s, %(hogql_val_45)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_46)s, %(hogql_val_47)s)))
+ GROUP BY breakdown_value), person_arrays AS (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_62)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_63)s), ''), 'null'), '^"|"$', ''), %(hogql_val_64)s), 0.0)), and(equals(events.event, %(hogql_val_65)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_66)s), %(hogql_val_67)s), toDateTime(%(hogql_val_68)s, %(hogql_val_69)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_70)s), %(hogql_val_71)s), toDateTime(%(hogql_val_72)s, %(hogql_val_73)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_74)s)), events__session.`$entry_utm_source`, %(hogql_val_75)s)), [%(hogql_val_76)s, %(hogql_val_77)s, %(hogql_val_78)s]), %(hogql_val_79)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_80)s)), events__session.`$entry_utm_source`, %(hogql_val_81)s)), [%(hogql_val_82)s, %(hogql_val_83)s, %(hogql_val_84)s, %(hogql_val_85)s, %(hogql_val_86)s, %(hogql_val_87)s, %(hogql_val_88)s, %(hogql_val_89)s]), %(hogql_val_90)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_91)s)), events__session.`$entry_utm_source`, %(hogql_val_92)s)), [%(hogql_val_93)s]), %(hogql_val_94)s, if(in(lower(if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_95)s)), events__session.`$entry_utm_source`, %(hogql_val_96)s)), [%(hogql_val_97)s, %(hogql_val_98)s, %(hogql_val_99)s, %(hogql_val_100)s, %(hogql_val_101)s, %(hogql_val_102)s, %(hogql_val_103)s, %(hogql_val_104)s, %(hogql_val_105)s]), %(hogql_val_106)s, if(notEmpty(ifNull(events__session.`$entry_utm_source`, %(hogql_val_107)s)), events__session.`$entry_utm_source`, %(hogql_val_108)s)))))), and(equals(events.event, %(hogql_val_109)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -500) AS touchpoints
+ FROM events LEFT JOIN (
+ SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_48)s)) AS `$start_timestamp`, nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_49)s), %(hogql_val_50)s) AS `$entry_utm_source`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), or(and(ifNull(greaterOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_51)s), minus(toDateTime(%(hogql_val_52)s, %(hogql_val_53)s), toIntervalDay(3))), 0), ifNull(lessOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_54)s), plus(toDateTime(%(hogql_val_55)s, %(hogql_val_56)s), toIntervalDay(3))), 0)), and(greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_57)s, %(hogql_val_58)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_59)s, %(hogql_val_60)s), toIntervalDay(3))))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_61)s)), events__session.session_id_v7) 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
+ FROM 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_110)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_111)s), %(hogql_val_112)s), toDateTime(%(hogql_val_113)s, %(hogql_val_114)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_115)s), %(hogql_val_116)s), toDateTime(%(hogql_val_117)s, %(hogql_val_118)s)))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_119)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_120)s), %(hogql_val_121)s), toDateTime(%(hogql_val_122)s, %(hogql_val_123)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_124)s), %(hogql_val_125)s), toDateTime(%(hogql_val_126)s, %(hogql_val_127)s))), and(equals(events.event, %(hogql_val_128)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_129)s, %(hogql_val_130)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_131)s, %(hogql_val_132)s)))))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)
+ HAVING greater(length(conversions), 0)), per_conversion AS MATERIALIZED (
+ SELECT person_arrays.person_id AS person_id, i, (person_arrays.conversions[i]).1 AS conversion_time, (person_arrays.conversions[i]).2 AS conversion_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, conversion_time), 0), ifNull(greaterOrEquals(t.1, minus(conversion_time, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.1, tps) AS touch_ts, arrayMap(t -> t.2, tps) AS dims, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMin(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMin(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_first, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMax(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMax(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_last, arrayMap(_x -> divide(1.0, greatest(accurateCastOrNull(length(touch_ts), %(hogql_val_133)s), 1.0)), touch_ts) AS w_linear, arrayMap(w -> divide(w, greatest(arraySum(arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)), 1e-06)), arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)) AS w_decay, if(ifNull(equals(length(touch_ts), 0), 0), [], if(ifNull(equals(length(touch_ts), 1), 0), [1.0], if(ifNull(equals(length(touch_ts), 2), 0), [0.5, 0.5], arrayMap(w -> divide(w, greatest(arraySum(arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_134)s), 2.0), 1.0)))), touch_ts))), 1e-06)), arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_135)s), 2.0), 1.0)))), touch_ts)))))) AS w_position
+ FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), per_touchpoint AS (
+ SELECT per_conversion.person_id AS person_id, per_conversion.i AS i, per_conversion.conversion_value AS conversion_value, per_conversion.dims[k] AS breakdown_value, per_conversion.w_first[k] AS w_first, per_conversion.w_last[k] AS w_last, per_conversion.w_linear[k] AS w_linear, per_conversion.w_decay[k] AS w_decay, per_conversion.w_position[k] AS w_position
+ FROM per_conversion ARRAY JOIN arrayEnumerate(per_conversion.touch_ts) AS k), per_conversion_dim AS (
+ SELECT per_touchpoint.breakdown_value AS breakdown_value, per_touchpoint.person_id AS person_id, per_touchpoint.i AS i, any(per_touchpoint.conversion_value) AS conv_value, sum(per_touchpoint.w_first) AS w_first, sum(per_touchpoint.w_last) AS w_last, sum(per_touchpoint.w_linear) AS w_linear, sum(per_touchpoint.w_decay) AS w_decay, sum(per_touchpoint.w_position) AS w_position
+ FROM per_touchpoint
+ GROUP BY per_touchpoint.breakdown_value, per_touchpoint.person_id, per_touchpoint.i), attribution_totals AS (
+ SELECT per_conversion_dim.breakdown_value AS breakdown_value, count() AS influenced_conversions, sum(per_conversion_dim.conv_value) AS influenced_value, sum(per_conversion_dim.w_first) AS w_first_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_first)) AS w_first_value, sum(per_conversion_dim.w_last) AS w_last_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_last)) AS w_last_value, sum(per_conversion_dim.w_linear) AS w_linear_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_linear)) AS w_linear_value, sum(per_conversion_dim.w_decay) AS w_decay_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_decay)) AS w_decay_value, sum(per_conversion_dim.w_position) AS w_position_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_position)) AS w_position_value
+ FROM per_conversion_dim
+ GROUP BY per_conversion_dim.breakdown_value), attribution_rows AS (
+ SELECT coalesce(nullIf(attribution_totals.breakdown_value, %(hogql_val_136)s), influenced_reach.breakdown_value) AS breakdown_value, ifNull(influenced_reach.visitors, 0) AS visitors, ifNull(attribution_totals.influenced_conversions, 0) AS influenced_conversions, ifNull(attribution_totals.influenced_value, 0.0) AS influenced_value, ifNull(attribution_totals.w_first_conversions, 0.0) AS w_first_conversions, ifNull(attribution_totals.w_first_value, 0.0) AS w_first_value, ifNull(attribution_totals.w_last_conversions, 0.0) AS w_last_conversions, ifNull(attribution_totals.w_last_value, 0.0) AS w_last_value, ifNull(attribution_totals.w_linear_conversions, 0.0) AS w_linear_conversions, ifNull(attribution_totals.w_linear_value, 0.0) AS w_linear_value, ifNull(attribution_totals.w_decay_conversions, 0.0) AS w_decay_conversions, ifNull(attribution_totals.w_decay_value, 0.0) AS w_decay_value, ifNull(attribution_totals.w_position_conversions, 0.0) AS w_position_conversions, ifNull(attribution_totals.w_position_value, 0.0) AS w_position_value, 1 AS footer_key
+ FROM attribution_totals FULL OUTER JOIN influenced_reach ON equals(attribution_totals.breakdown_value, influenced_reach.breakdown_value)), attribution_footer AS (
+ SELECT count() AS total_conversions, countIf(ifNull(greater(length(per_conversion.tps), 0), 0)) AS attributed_conversions, 1 AS footer_key
+ FROM per_conversion)
+ SELECT attribution_rows.breakdown_value AS breakdown_value, attribution_rows.visitors AS visitors, attribution_rows.influenced_conversions AS influenced_conversions, attribution_rows.influenced_value AS influenced_value, attribution_rows.w_first_conversions AS w_first_conversions, attribution_rows.w_first_value AS w_first_value, attribution_rows.w_last_conversions AS w_last_conversions, attribution_rows.w_last_value AS w_last_value, attribution_rows.w_linear_conversions AS w_linear_conversions, attribution_rows.w_linear_value AS w_linear_value, attribution_rows.w_decay_conversions AS w_decay_conversions, attribution_rows.w_decay_value AS w_decay_value, attribution_rows.w_position_conversions AS w_position_conversions, attribution_rows.w_position_value AS w_position_value, attribution_footer.total_conversions AS total_conversions, attribution_footer.attributed_conversions AS attributed_conversions, attribution_rows.footer_key AS footer_key
+ FROM attribution_footer LEFT JOIN attribution_rows ON equals(attribution_footer.footer_key, attribution_rows.footer_key)
+ ORDER BY influenced_conversions DESC, visitors DESC
+ LIMIT 101
+ OFFSET 0
+ '''
+# ---
+# name: TestMarketingAnalyticsAttributionQueryRunner.test_attribution_table_sql_2_channel
+ '''
+ WITH influenced_reach AS (
+ SELECT if(notEmpty(ifNull(events__session.`$channel_type`, %(hogql_val_125)s)), events__session.`$channel_type`, %(hogql_val_126)s) AS breakdown_value, uniq(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)) AS visitors
+ FROM 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, 420)
+ 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) LEFT JOIN (
+ SELECT multiIf(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_0)s), %(hogql_val_1)s), %(hogql_val_2)s), %(hogql_val_3)s)), %(hogql_val_4)s), %(hogql_val_5)s, or(in(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_6)s), %(hogql_val_7)s), %(hogql_val_8)s), %(hogql_val_9)s)), tuple(%(hogql_val_10)s, %(hogql_val_11)s, %(hogql_val_12)s, %(hogql_val_13)s, %(hogql_val_14)s, %(hogql_val_15)s)), startsWith(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_16)s), %(hogql_val_17)s), %(hogql_val_18)s), %(hogql_val_19)s)), %(hogql_val_20)s), isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_gclid), %(hogql_val_21)s), %(hogql_val_22)s)), isNotNull(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_gad_source), %(hogql_val_23)s), %(hogql_val_24)s), %(hogql_val_25)s), %(hogql_val_26)s))), coalesce(coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_27)s), %(hogql_val_28)s), %(hogql_val_29)s), %(hogql_val_30)s)), ''), 'source')) , dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (cutToFirstSignificantSubdomain(coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_27)s), %(hogql_val_28)s), %(hogql_val_29)s), %(hogql_val_30)s)), '')), 'source'))), if(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_31)s), %(hogql_val_32)s), %(hogql_val_33)s), %(hogql_val_34)s)), %(hogql_val_35)s), %(hogql_val_36)s, NULL), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_37)s), %(hogql_val_38)s), %(hogql_val_39)s), %(hogql_val_40)s)), ''), 'medium')), coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_41)s), %(hogql_val_42)s), ''), 'source')) , dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (cutToFirstSignificantSubdomain(coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_41)s), %(hogql_val_42)s), '')), 'source'))), multiIf(ifNull(equals(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_gad_source), %(hogql_val_43)s), %(hogql_val_44)s), %(hogql_val_45)s), %(hogql_val_46)s), %(hogql_val_47)s), 0), %(hogql_val_48)s, match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_49)s), %(hogql_val_50)s), %(hogql_val_51)s), %(hogql_val_52)s)), %(hogql_val_53)s), %(hogql_val_54)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_55)s), %(hogql_val_56)s)), %(hogql_val_57)s, %(hogql_val_58)s)), and(ifNull(equals(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_59)s), %(hogql_val_60)s), %(hogql_val_61)s), 0), isNull(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_62)s), %(hogql_val_63)s), %(hogql_val_64)s), %(hogql_val_65)s))), or(isNull(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_66)s), %(hogql_val_67)s), %(hogql_val_68)s), %(hogql_val_69)s))), in(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_70)s), %(hogql_val_71)s), %(hogql_val_72)s), %(hogql_val_73)s)), tuple(%(hogql_val_74)s, %(hogql_val_75)s, %(hogql_val_76)s))), not(isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_77)s), %(hogql_val_78)s)))), %(hogql_val_79)s, coalesce(coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_80)s), %(hogql_val_81)s), %(hogql_val_82)s), %(hogql_val_83)s)), ''), 'source')), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (cutToFirstSignificantSubdomain(coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_80)s), %(hogql_val_81)s), %(hogql_val_82)s), %(hogql_val_83)s)), '')), 'source'))), if(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_84)s), %(hogql_val_85)s), %(hogql_val_86)s), %(hogql_val_87)s)), %(hogql_val_88)s), %(hogql_val_89)s, NULL), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_90)s), %(hogql_val_91)s), %(hogql_val_92)s), %(hogql_val_93)s)), ''), 'medium')), coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_94)s), %(hogql_val_95)s), ''), 'source')), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (cutToFirstSignificantSubdomain(coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_94)s), %(hogql_val_95)s), '')), 'source'))), multiIf(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_96)s), %(hogql_val_97)s), %(hogql_val_98)s), %(hogql_val_99)s)), %(hogql_val_100)s), %(hogql_val_101)s, match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_102)s), %(hogql_val_103)s), %(hogql_val_104)s), %(hogql_val_105)s)), %(hogql_val_106)s), %(hogql_val_107)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_108)s), %(hogql_val_109)s)), %(hogql_val_110)s, ifNull(equals(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_111)s), %(hogql_val_112)s), %(hogql_val_113)s), 0), %(hogql_val_114)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_115)s), %(hogql_val_116)s)), %(hogql_val_117)s, %(hogql_val_118)s))) AS `$channel_type`, min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_119)s)) AS `$start_timestamp`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_120)s, %(hogql_val_121)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_122)s, %(hogql_val_123)s), toIntervalDay(3))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_124)s)), events__session.session_id_v7)
+ WHERE and(equals(events.team_id, 420), and(equals(events.event, %(hogql_val_127)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_128)s, %(hogql_val_129)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_130)s, %(hogql_val_131)s)))
+ GROUP BY breakdown_value), person_arrays AS (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id, groupArrayIf(tuple(toUnixTimestamp(toTimeZone(events.timestamp, %(hogql_val_263)s)), coalesce(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, %(hogql_val_264)s), ''), 'null'), '^"|"$', ''), %(hogql_val_265)s), 0.0)), and(equals(events.event, %(hogql_val_266)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_267)s), %(hogql_val_268)s), toDateTime(%(hogql_val_269)s, %(hogql_val_270)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_271)s), %(hogql_val_272)s), toDateTime(%(hogql_val_273)s, %(hogql_val_274)s)))) AS conversions, arraySlice(arraySort(groupUniqArrayIf(tuple(toUnixTimestamp(events__session.`$start_timestamp`), if(notEmpty(ifNull(events__session.`$channel_type`, %(hogql_val_275)s)), events__session.`$channel_type`, %(hogql_val_276)s)), and(equals(events.event, %(hogql_val_277)s), notEmpty(events.`$session_id`), greater(toUnixTimestamp(events__session.`$start_timestamp`), 0)))), -500) AS touchpoints
+ FROM events LEFT JOIN (
+ SELECT min(toTimeZone(raw_sessions.min_timestamp, %(hogql_val_132)s)) AS `$start_timestamp`, multiIf(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_133)s), %(hogql_val_134)s), %(hogql_val_135)s), %(hogql_val_136)s)), %(hogql_val_137)s), %(hogql_val_138)s, or(in(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_139)s), %(hogql_val_140)s), %(hogql_val_141)s), %(hogql_val_142)s)), tuple(%(hogql_val_143)s, %(hogql_val_144)s, %(hogql_val_145)s, %(hogql_val_146)s, %(hogql_val_147)s, %(hogql_val_148)s)), startsWith(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_149)s), %(hogql_val_150)s), %(hogql_val_151)s), %(hogql_val_152)s)), %(hogql_val_153)s), isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_gclid), %(hogql_val_154)s), %(hogql_val_155)s)), isNotNull(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_gad_source), %(hogql_val_156)s), %(hogql_val_157)s), %(hogql_val_158)s), %(hogql_val_159)s))), coalesce(coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_160)s), %(hogql_val_161)s), %(hogql_val_162)s), %(hogql_val_163)s)), ''), 'source')) , dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (cutToFirstSignificantSubdomain(coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_160)s), %(hogql_val_161)s), %(hogql_val_162)s), %(hogql_val_163)s)), '')), 'source'))), if(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_164)s), %(hogql_val_165)s), %(hogql_val_166)s), %(hogql_val_167)s)), %(hogql_val_168)s), %(hogql_val_169)s, NULL), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_170)s), %(hogql_val_171)s), %(hogql_val_172)s), %(hogql_val_173)s)), ''), 'medium')), coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_174)s), %(hogql_val_175)s), ''), 'source')) , dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_paid', (cutToFirstSignificantSubdomain(coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_174)s), %(hogql_val_175)s), '')), 'source'))), multiIf(ifNull(equals(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_gad_source), %(hogql_val_176)s), %(hogql_val_177)s), %(hogql_val_178)s), %(hogql_val_179)s), %(hogql_val_180)s), 0), %(hogql_val_181)s, match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_182)s), %(hogql_val_183)s), %(hogql_val_184)s), %(hogql_val_185)s)), %(hogql_val_186)s), %(hogql_val_187)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_188)s), %(hogql_val_189)s)), %(hogql_val_190)s, %(hogql_val_191)s)), and(ifNull(equals(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_192)s), %(hogql_val_193)s), %(hogql_val_194)s), 0), isNull(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_195)s), %(hogql_val_196)s), %(hogql_val_197)s), %(hogql_val_198)s))), or(isNull(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_199)s), %(hogql_val_200)s), %(hogql_val_201)s), %(hogql_val_202)s))), in(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_203)s), %(hogql_val_204)s), %(hogql_val_205)s), %(hogql_val_206)s)), tuple(%(hogql_val_207)s, %(hogql_val_208)s, %(hogql_val_209)s))), not(isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_210)s), %(hogql_val_211)s)))), %(hogql_val_212)s, coalesce(coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_213)s), %(hogql_val_214)s), %(hogql_val_215)s), %(hogql_val_216)s)), ''), 'source')), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (cutToFirstSignificantSubdomain(coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_source), %(hogql_val_213)s), %(hogql_val_214)s), %(hogql_val_215)s), %(hogql_val_216)s)), '')), 'source'))), if(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_217)s), %(hogql_val_218)s), %(hogql_val_219)s), %(hogql_val_220)s)), %(hogql_val_221)s), %(hogql_val_222)s, NULL), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_223)s), %(hogql_val_224)s), %(hogql_val_225)s), %(hogql_val_226)s)), ''), 'medium')), coalesce(dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_227)s), %(hogql_val_228)s), ''), 'source')), dictGetOrNull('posthog_test.channel_definition_dict', 'type_if_organic', (cutToFirstSignificantSubdomain(coalesce(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_227)s), %(hogql_val_228)s), '')), 'source'))), multiIf(match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_campaign), %(hogql_val_229)s), %(hogql_val_230)s), %(hogql_val_231)s), %(hogql_val_232)s)), %(hogql_val_233)s), %(hogql_val_234)s, match(lower(nullIf(nullIf(nullIf(nullIf(argMinMerge(raw_sessions.initial_utm_medium), %(hogql_val_235)s), %(hogql_val_236)s), %(hogql_val_237)s), %(hogql_val_238)s)), %(hogql_val_239)s), %(hogql_val_240)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_fbclid), %(hogql_val_241)s), %(hogql_val_242)s)), %(hogql_val_243)s, ifNull(equals(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_244)s), %(hogql_val_245)s), %(hogql_val_246)s), 0), %(hogql_val_247)s, isNotNull(nullIf(nullIf(argMinMerge(raw_sessions.initial_referring_domain), %(hogql_val_248)s), %(hogql_val_249)s)), %(hogql_val_250)s, %(hogql_val_251)s))) AS `$channel_type`, raw_sessions.session_id_v7 AS session_id_v7
+ FROM raw_sessions
+ WHERE and(equals(raw_sessions.team_id, 420), or(and(ifNull(greaterOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_252)s), minus(toDateTime(%(hogql_val_253)s, %(hogql_val_254)s), toIntervalDay(3))), 0), ifNull(lessOrEquals(toDateTime(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), %(hogql_val_255)s), plus(toDateTime(%(hogql_val_256)s, %(hogql_val_257)s), toIntervalDay(3))), 0)), and(greaterOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), minus(minus(toDateTime(%(hogql_val_258)s, %(hogql_val_259)s), toIntervalSecond(345600)), toIntervalDay(3))), lessOrEquals(fromUnixTimestamp(intDiv(toUInt64(bitShiftRight(raw_sessions.session_id_v7, 80)), 1000)), plus(toDateTime(%(hogql_val_260)s, %(hogql_val_261)s), toIntervalDay(3))))))
+ GROUP BY raw_sessions.session_id_v7) AS events__session ON equals(toUInt128(accurateCastOrNull(events.`$session_id`, %(hogql_val_262)s)), events__session.session_id_v7) 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), globalIn(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), (
+ SELECT if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id) AS person_id
+ FROM 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, 420)
+ 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)
+ WHERE and(equals(events.team_id, 420), equals(events.event, %(hogql_val_278)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_279)s), %(hogql_val_280)s), toDateTime(%(hogql_val_281)s, %(hogql_val_282)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_283)s), %(hogql_val_284)s), toDateTime(%(hogql_val_285)s, %(hogql_val_286)s)))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id))), or(and(equals(events.event, %(hogql_val_287)s), greaterOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_288)s), %(hogql_val_289)s), toDateTime(%(hogql_val_290)s, %(hogql_val_291)s)), lessOrEquals(toDateTime(toTimeZone(events.timestamp, %(hogql_val_292)s), %(hogql_val_293)s), toDateTime(%(hogql_val_294)s, %(hogql_val_295)s))), and(equals(events.event, %(hogql_val_296)s), greaterOrEquals(events.timestamp, minus(toDateTime(%(hogql_val_297)s, %(hogql_val_298)s), toIntervalSecond(345600))), lessOrEquals(events.timestamp, toDateTime(%(hogql_val_299)s, %(hogql_val_300)s)))))
+ GROUP BY if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id)
+ HAVING greater(length(conversions), 0)), per_conversion AS MATERIALIZED (
+ SELECT person_arrays.person_id AS person_id, i, (person_arrays.conversions[i]).1 AS conversion_time, (person_arrays.conversions[i]).2 AS conversion_value, arrayFilter(t -> and(ifNull(lessOrEquals(t.1, conversion_time), 0), ifNull(greaterOrEquals(t.1, minus(conversion_time, 345600)), 0)), person_arrays.touchpoints) AS tps, arrayMap(t -> t.1, tps) AS touch_ts, arrayMap(t -> t.2, tps) AS dims, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMin(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMin(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_first, arrayMap(_j -> if(ifNull(equals(_j, indexOf(touch_ts, arrayMax(touch_ts))), isNull(_j) and isNull(indexOf(touch_ts, arrayMax(touch_ts)))), 1.0, 0.0), arrayEnumerate(touch_ts)) AS w_last, arrayMap(_x -> divide(1.0, greatest(accurateCastOrNull(length(touch_ts), %(hogql_val_301)s), 1.0)), touch_ts) AS w_linear, arrayMap(w -> divide(w, greatest(arraySum(arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)), 1e-06)), arrayMap(ts -> exp(multiply(-0.6931471805599453, divide(minus(conversion_time, ts), 86400))), touch_ts)) AS w_decay, if(ifNull(equals(length(touch_ts), 0), 0), [], if(ifNull(equals(length(touch_ts), 1), 0), [1.0], if(ifNull(equals(length(touch_ts), 2), 0), [0.5, 0.5], arrayMap(w -> divide(w, greatest(arraySum(arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_302)s), 2.0), 1.0)))), touch_ts))), 1e-06)), arrayMap(_pw -> ifNull(_pw, 0.0), arrayMap(ts -> if(ifNull(equals(ts, arrayMin(touch_ts)), isNull(ts) and isNull(arrayMin(touch_ts))), 0.4, if(ifNull(equals(ts, arrayMax(touch_ts)), isNull(ts) and isNull(arrayMax(touch_ts))), 0.4, divide(0.2, greatest(minus(accurateCastOrNull(length(touch_ts), %(hogql_val_303)s), 2.0), 1.0)))), touch_ts)))))) AS w_position
+ FROM person_arrays ARRAY JOIN arrayEnumerate(person_arrays.conversions) AS i), per_touchpoint AS (
+ SELECT per_conversion.person_id AS person_id, per_conversion.i AS i, per_conversion.conversion_value AS conversion_value, per_conversion.dims[k] AS breakdown_value, per_conversion.w_first[k] AS w_first, per_conversion.w_last[k] AS w_last, per_conversion.w_linear[k] AS w_linear, per_conversion.w_decay[k] AS w_decay, per_conversion.w_position[k] AS w_position
+ FROM per_conversion ARRAY JOIN arrayEnumerate(per_conversion.touch_ts) AS k), per_conversion_dim AS (
+ SELECT per_touchpoint.breakdown_value AS breakdown_value, per_touchpoint.person_id AS person_id, per_touchpoint.i AS i, any(per_touchpoint.conversion_value) AS conv_value, sum(per_touchpoint.w_first) AS w_first, sum(per_touchpoint.w_last) AS w_last, sum(per_touchpoint.w_linear) AS w_linear, sum(per_touchpoint.w_decay) AS w_decay, sum(per_touchpoint.w_position) AS w_position
+ FROM per_touchpoint
+ GROUP BY per_touchpoint.breakdown_value, per_touchpoint.person_id, per_touchpoint.i), attribution_totals AS (
+ SELECT per_conversion_dim.breakdown_value AS breakdown_value, count() AS influenced_conversions, sum(per_conversion_dim.conv_value) AS influenced_value, sum(per_conversion_dim.w_first) AS w_first_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_first)) AS w_first_value, sum(per_conversion_dim.w_last) AS w_last_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_last)) AS w_last_value, sum(per_conversion_dim.w_linear) AS w_linear_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_linear)) AS w_linear_value, sum(per_conversion_dim.w_decay) AS w_decay_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_decay)) AS w_decay_value, sum(per_conversion_dim.w_position) AS w_position_conversions, sum(multiply(per_conversion_dim.conv_value, per_conversion_dim.w_position)) AS w_position_value
+ FROM per_conversion_dim
+ GROUP BY per_conversion_dim.breakdown_value), attribution_rows AS (
+ SELECT coalesce(nullIf(attribution_totals.breakdown_value, %(hogql_val_304)s), influenced_reach.breakdown_value) AS breakdown_value, ifNull(influenced_reach.visitors, 0) AS visitors, ifNull(attribution_totals.influenced_conversions, 0) AS influenced_conversions, ifNull(attribution_totals.influenced_value, 0.0) AS influenced_value, ifNull(attribution_totals.w_first_conversions, 0.0) AS w_first_conversions, ifNull(attribution_totals.w_first_value, 0.0) AS w_first_value, ifNull(attribution_totals.w_last_conversions, 0.0) AS w_last_conversions, ifNull(attribution_totals.w_last_value, 0.0) AS w_last_value, ifNull(attribution_totals.w_linear_conversions, 0.0) AS w_linear_conversions, ifNull(attribution_totals.w_linear_value, 0.0) AS w_linear_value, ifNull(attribution_totals.w_decay_conversions, 0.0) AS w_decay_conversions, ifNull(attribution_totals.w_decay_value, 0.0) AS w_decay_value, ifNull(attribution_totals.w_position_conversions, 0.0) AS w_position_conversions, ifNull(attribution_totals.w_position_value, 0.0) AS w_position_value, 1 AS footer_key
+ FROM attribution_totals FULL OUTER JOIN influenced_reach ON equals(attribution_totals.breakdown_value, influenced_reach.breakdown_value)), attribution_footer AS (
+ SELECT count() AS total_conversions, countIf(ifNull(greater(length(per_conversion.tps), 0), 0)) AS attributed_conversions, 1 AS footer_key
+ FROM per_conversion)
+ SELECT attribution_rows.breakdown_value AS breakdown_value, attribution_rows.visitors AS visitors, attribution_rows.influenced_conversions AS influenced_conversions, attribution_rows.influenced_value AS influenced_value, attribution_rows.w_first_conversions AS w_first_conversions, attribution_rows.w_first_value AS w_first_value, attribution_rows.w_last_conversions AS w_last_conversions, attribution_rows.w_last_value AS w_last_value, attribution_rows.w_linear_conversions AS w_linear_conversions, attribution_rows.w_linear_value AS w_linear_value, attribution_rows.w_decay_conversions AS w_decay_conversions, attribution_rows.w_decay_value AS w_decay_value, attribution_rows.w_position_conversions AS w_position_conversions, attribution_rows.w_position_value AS w_position_value, attribution_footer.total_conversions AS total_conversions, attribution_footer.attributed_conversions AS attributed_conversions, attribution_rows.footer_key AS footer_key
+ FROM attribution_footer LEFT JOIN attribution_rows ON equals(attribution_footer.footer_key, attribution_rows.footer_key)
+ ORDER BY influenced_conversions DESC, visitors DESC
+ LIMIT 101
+ OFFSET 0
+ '''
+# ---
diff --git a/products/marketing_analytics/backend/hogql_queries/test_attribution_paths_query_runner.py b/products/marketing_analytics/backend/hogql_queries/test_attribution_paths_query_runner.py
index 25c0a2c4a1d1..a04498809494 100644
--- a/products/marketing_analytics/backend/hogql_queries/test_attribution_paths_query_runner.py
+++ b/products/marketing_analytics/backend/hogql_queries/test_attribution_paths_query_runner.py
@@ -1,5 +1,8 @@
+import pytest
from posthog.test.base import BaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events
+from parameterized import parameterized
+
from posthog.schema import (
BaseMathType,
ConversionGoalFilter1,
@@ -9,6 +12,9 @@ from posthog.schema import (
PropertyMathType,
)
+from posthog.hogql.printer import prepare_and_print_ast
+from posthog.hogql.test.utils import pretty_print_in_tests
+
from posthog.models.utils import uuid7
from posthog.test.persons import create_person
@@ -395,3 +401,26 @@ class TestMarketingAnalyticsAttributionPathsQueryRunner(ClickhouseTestMixin, Bas
ctes = runner.to_query().ctes
assert ctes is not None
self.assertTrue(ctes["per_conversion_path"].materialized)
+
+ # Same three shapes as the attribution table: direct read, alias normalization, classifier.
+ @parameterized.expand(
+ [
+ ("campaign", MarketingAnalyticsAttributionBreakdown.CAMPAIGN),
+ ("source", MarketingAnalyticsAttributionBreakdown.SOURCE),
+ ("channel", MarketingAnalyticsAttributionBreakdown.CHANNEL),
+ ]
+ )
+ @pytest.mark.usefixtures("unittest_snapshot")
+ def test_attribution_paths_sql(self, _name: str, breakdown: MarketingAnalyticsAttributionBreakdown):
+ query = MarketingAnalyticsAttributionPathsQuery(
+ dateRange=DateRange(date_from="2023-01-01", date_to="2023-01-31"),
+ breakdownBy=breakdown,
+ conversionGoalId=GOAL_ID,
+ properties=[],
+ )
+ runner = MarketingAnalyticsAttributionPathsQueryRunner(query=query, team=self.team)
+ context = runner._shared_hogql_context
+ context.enable_select_queries = True
+ printed = prepare_and_print_ast(runner.to_query(), context=context, dialect="clickhouse")
+ sql = printed[0] if isinstance(printed, tuple) else printed
+ assert pretty_print_in_tests(sql, self.team.pk) == self.snapshot
diff --git a/products/marketing_analytics/backend/hogql_queries/test_attribution_table_query_runner.py b/products/marketing_analytics/backend/hogql_queries/test_attribution_table_query_runner.py
index 119c99d20040..71be949e221a 100644
--- a/products/marketing_analytics/backend/hogql_queries/test_attribution_table_query_runner.py
+++ b/products/marketing_analytics/backend/hogql_queries/test_attribution_table_query_runner.py
@@ -1,3 +1,4 @@
+import pytest
from posthog.test.base import BaseTest, ClickhouseTestMixin, _create_event, flush_persons_and_events
from unittest.mock import patch
@@ -19,6 +20,7 @@ from posthog.schema import (
from posthog.hogql import ast
from posthog.hogql.printer import prepare_and_print_ast
from posthog.hogql.property_access_types import RestrictedProperty
+from posthog.hogql.test.utils import pretty_print_in_tests
from posthog.hogql.visitor import TraversingVisitor
from posthog.models import PropertyDefinition
@@ -904,3 +906,31 @@ class TestMarketingAnalyticsAttributionQueryRunner(ClickhouseTestMixin, BaseTest
)
with self.assertRaises(ValueError):
MarketingAnalyticsAttributionQueryRunner(query=query, team=self.team).to_query()
+
+ def _printed_sql(self, breakdown: MarketingAnalyticsAttributionBreakdown) -> str:
+ query = MarketingAnalyticsAttributionQuery(
+ dateRange=DateRange(date_from="2023-01-01", date_to="2023-01-31"),
+ breakdownBy=breakdown,
+ conversionGoalId=GOAL_ID,
+ properties=[],
+ )
+ runner = MarketingAnalyticsAttributionQueryRunner(query=query, team=self.team)
+ context = runner._shared_hogql_context
+ # execute_hogql_query flips this on the context it is handed; do the same to print the real query.
+ context.enable_select_queries = True
+ printed = prepare_and_print_ast(runner.to_query(), context=context, dialect="clickhouse")
+ return pretty_print_in_tests(printed[0] if isinstance(printed, tuple) else printed, self.team.pk)
+
+ # One breakdown per SQL shape. Campaign reads a stored property, and the five breakdowns not listed
+ # here produce the same query with a different column. Source adds the alias normalization, and
+ # channel runs the classifier over raw_sessions.
+ @parameterized.expand(
+ [
+ ("campaign", MarketingAnalyticsAttributionBreakdown.CAMPAIGN),
+ ("source", MarketingAnalyticsAttributionBreakdown.SOURCE),
+ ("channel", MarketingAnalyticsAttributionBreakdown.CHANNEL),
+ ]
+ )
+ @pytest.mark.usefixtures("unittest_snapshot")
+ def test_attribution_table_sql(self, _name: str, breakdown: MarketingAnalyticsAttributionBreakdown):
+ assert self._printed_sql(breakdown) == self.snapshot