: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(experiments): add distinct_id and person_id to precompute tables (#124860)
Juraj Majerik634bf330edc3
diff --git a/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl b/posthog/clickhouse/hcl/golden/dev/auxiliary.hclindex a90ed9b39d84..0d1a9e4d155e 100644--- a/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl+++ b/posthog/clickhouse/hcl/golden/dev/auxiliary.hcl@@ -445,6 +445,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}@@ -2934,6 +2941,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}diff --git a/posthog/clickhouse/hcl/golden/dev/data.hcl b/posthog/clickhouse/hcl/golden/dev/data.hclindex b5a186520df2..44836f6f34e1 100644--- a/posthog/clickhouse/hcl/golden/dev/data.hcl+++ b/posthog/clickhouse/hcl/golden/dev/data.hcl@@ -1735,6 +1735,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}@@ -1779,6 +1786,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}@@ -5534,6 +5548,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}diff --git a/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hcl b/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hclindex 5f44cbb02d30..f08276d31c6f 100644--- a/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/auxiliary.hcl@@ -445,6 +445,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}@@ -2977,6 +2984,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}diff --git a/posthog/clickhouse/hcl/golden/local-multi/data.hcl b/posthog/clickhouse/hcl/golden/local-multi/data.hclindex 6b49ca428ab1..737e15785045 100644--- a/posthog/clickhouse/hcl/golden/local-multi/data.hcl+++ b/posthog/clickhouse/hcl/golden/local-multi/data.hcl@@ -1830,6 +1830,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}@@ -1874,6 +1881,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}@@ -5758,6 +5772,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hclindex 9895dad26e39..1980b548d7a0 100644--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl@@ -1955,6 +1955,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}@@ -1999,6 +2006,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}@@ -11758,6 +11772,13 @@ SQLcolumn "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}@@ -11808,6 +11829,13 @@ SQLcolumn "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}diff --git a/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl b/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hclindex 5f225b60fe66..ec51438b5762 100644--- a/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl+++ b/posthog/clickhouse/hcl/golden/prod-eu/auxiliary.hcl@@ -445,6 +445,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}@@ -2934,6 +2941,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}diff --git a/posthog/clickhouse/hcl/golden/prod-eu/data.hcl b/posthog/clickhouse/hcl/golden/prod-eu/data.hclindex 6ba9707ca10e..41a754bfd235 100644--- a/posthog/clickhouse/hcl/golden/prod-eu/data.hcl+++ b/posthog/clickhouse/hcl/golden/prod-eu/data.hcl@@ -1692,6 +1692,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}@@ -1736,6 +1743,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}@@ -5709,6 +5723,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}diff --git a/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl b/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hclindex 93d692cd3ba3..6a10cb79c02c 100644--- a/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl+++ b/posthog/clickhouse/hcl/golden/prod-us/auxiliary.hcl@@ -445,6 +445,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}@@ -2972,6 +2979,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}diff --git a/posthog/clickhouse/hcl/golden/prod-us/data.hcl b/posthog/clickhouse/hcl/golden/prod-us/data.hclindex 7c977c4bbce9..dabf7d8ac616 100644--- a/posthog/clickhouse/hcl/golden/prod-us/data.hcl+++ b/posthog/clickhouse/hcl/golden/prod-us/data.hcl@@ -1807,6 +1807,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}@@ -1851,6 +1858,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}@@ -5522,6 +5536,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}diff --git a/posthog/clickhouse/hcl/roles/coshared/aux_data/tables.hcl b/posthog/clickhouse/hcl/roles/coshared/aux_data/tables.hclindex a177a37b0bba..fb8396c68902 100644--- a/posthog/clickhouse/hcl/roles/coshared/aux_data/tables.hcl+++ b/posthog/clickhouse/hcl/roles/coshared/aux_data/tables.hcl@@ -96,6 +96,13 @@ database "posthog" {column "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "timestamp" {type = "DateTime64(6, 'UTC')"}diff --git a/posthog/clickhouse/hcl/roles/data/common/cloud_and_local.hcl b/posthog/clickhouse/hcl/roles/data/common/cloud_and_local.hclindex e048a7198ae3..b409ffeb4c4c 100644--- a/posthog/clickhouse/hcl/roles/data/common/cloud_and_local.hcl+++ b/posthog/clickhouse/hcl/roles/data/common/cloud_and_local.hcl@@ -437,6 +437,13 @@ SQLcolumn "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}@@ -1050,6 +1057,13 @@ SQLcolumn "entity_id" {type = "String"}+ column "distinct_id" {+ type = "String"+ }+ column "person_id" {+ type = "UUID"+ default = "toUUIDOrZero(entity_id)"+ }column "variant" {type = "String"}diff --git a/posthog/clickhouse/hcl/sql/dev/auxiliary.sql b/posthog/clickhouse/hcl/sql/dev/auxiliary.sqlindex eb95ad514b45..017a00fe11e4 100644--- a/posthog/clickhouse/hcl/sql/dev/auxiliary.sql+++ b/posthog/clickhouse/hcl/sql/dev/auxiliary.sql@@ -622,6 +622,8 @@ CREATE TABLE posthog.sharded_experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -1701,6 +1703,8 @@ CREATE TABLE posthog.experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,diff --git a/posthog/clickhouse/hcl/sql/dev/data.sql b/posthog/clickhouse/hcl/sql/dev/data.sqlindex ce3aed1d9771..0492bb145cf6 100644--- a/posthog/clickhouse/hcl/sql/dev/data.sql+++ b/posthog/clickhouse/hcl/sql/dev/data.sql@@ -373,6 +373,8 @@ CREATE TABLE posthog.experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -1139,6 +1141,8 @@ CREATE TABLE posthog.sharded_experiment_exposures_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),@@ -2797,6 +2801,8 @@ CREATE TABLE posthog.experiment_exposures_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),diff --git a/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sql b/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sqlindex 89fa3324b427..0abc88af38d9 100644--- a/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/auxiliary.sql@@ -636,6 +636,8 @@ CREATE TABLE posthog.sharded_experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -1722,6 +1724,8 @@ CREATE TABLE posthog.experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,diff --git a/posthog/clickhouse/hcl/sql/local-multi/data.sql b/posthog/clickhouse/hcl/sql/local-multi/data.sqlindex 035e152ffff5..862db991efeb 100644--- a/posthog/clickhouse/hcl/sql/local-multi/data.sql+++ b/posthog/clickhouse/hcl/sql/local-multi/data.sql@@ -304,6 +304,8 @@ CREATE TABLE posthog.experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -1123,6 +1125,8 @@ CREATE TABLE posthog.sharded_experiment_exposures_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),@@ -3409,6 +3413,8 @@ CREATE TABLE posthog.experiment_exposures_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sqlindex e74e7dc93895..5140a852867f 100644--- a/posthog/clickhouse/hcl/sql/local-single/all.sql+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql@@ -2134,6 +2134,8 @@ CREATE TABLE posthog.sharded_experiment_exposures_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),@@ -2147,6 +2149,8 @@ CREATE TABLE posthog.sharded_experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -6792,6 +6796,8 @@ CREATE TABLE posthog.experiment_exposures_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),@@ -6805,6 +6811,8 @@ CREATE TABLE posthog.experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,diff --git a/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql b/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sqlindex 1854007c14e8..6bb9f0de70e1 100644--- a/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql+++ b/posthog/clickhouse/hcl/sql/prod-eu/auxiliary.sql@@ -622,6 +622,8 @@ CREATE TABLE posthog.sharded_experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -1701,6 +1703,8 @@ CREATE TABLE posthog.experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,diff --git a/posthog/clickhouse/hcl/sql/prod-eu/data.sql b/posthog/clickhouse/hcl/sql/prod-eu/data.sqlindex 491415a21438..6a46b3f226ec 100644--- a/posthog/clickhouse/hcl/sql/prod-eu/data.sql+++ b/posthog/clickhouse/hcl/sql/prod-eu/data.sql@@ -386,6 +386,8 @@ CREATE TABLE posthog.experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -1413,6 +1415,8 @@ CREATE TABLE posthog.sharded_experiment_exposures_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),@@ -3239,6 +3243,8 @@ CREATE TABLE posthog.experiment_exposures_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),diff --git a/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql b/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sqlindex a4c004ae6549..515531b2c911 100644--- a/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql+++ b/posthog/clickhouse/hcl/sql/prod-us/auxiliary.sql@@ -622,6 +622,8 @@ CREATE TABLE posthog.sharded_experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -1701,6 +1703,8 @@ CREATE TABLE posthog.experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,diff --git a/posthog/clickhouse/hcl/sql/prod-us/data.sql b/posthog/clickhouse/hcl/sql/prod-us/data.sqlindex b1acd23b5ffb..2f9c75229c9c 100644--- a/posthog/clickhouse/hcl/sql/prod-us/data.sql+++ b/posthog/clickhouse/hcl/sql/prod-us/data.sql@@ -398,6 +398,8 @@ CREATE TABLE posthog.experiment_metric_events_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -1307,6 +1309,8 @@ CREATE TABLE posthog.sharded_experiment_exposures_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),@@ -3163,6 +3167,8 @@ CREATE TABLE posthog.experiment_exposures_preaggregated (team_id Int64,job_id UUID,entity_id String,+ distinct_id String,+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),diff --git a/posthog/clickhouse/migrations/0365_experiment_preaggregated_person_columns.py b/posthog/clickhouse/migrations/0365_experiment_preaggregated_person_columns.pynew file mode 100644index 000000000000..4372ebf823db--- /dev/null+++ b/posthog/clickhouse/migrations/0365_experiment_preaggregated_person_columns.py@@ -0,0 +1,48 @@+from posthog.clickhouse.client.connection import NodeRole+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions+from posthog.clickhouse.preaggregation.experiment_exposures_sql import (+ DISTRIBUTED_EXPERIMENT_EXPOSURES_TABLE,+ SHARDED_EXPERIMENT_EXPOSURES_TABLE,+)+from posthog.clickhouse.preaggregation.experiment_metric_events_sql import (+ DISTRIBUTED_EXPERIMENT_METRIC_EVENTS_TABLE,+ SHARDED_EXPERIMENT_METRIC_EVENTS_TABLE,+)++# Adds distinct_id and person_id to both experiment precompute tables. The SQL files carry the+# rationale. person_id has a DEFAULT expression, so existing parts read it as their entity_id+# without a rewrite.+#+# Unqualified table names, as in 0358, so the ALTER reaches the tables in the configured database.+# Positioned rather than appended, so the column order matches the declarative schema.+ADD_PERSON_COLUMNS = (+ "ADD COLUMN IF NOT EXISTS distinct_id String AFTER entity_id, "+ "ADD COLUMN IF NOT EXISTS person_id UUID DEFAULT toUUIDOrZero(entity_id) AFTER distinct_id"+)++operations = [+ run_sql_with_exceptions(+ f"ALTER TABLE {SHARDED_EXPERIMENT_EXPOSURES_TABLE()} {ADD_PERSON_COLUMNS}",+ node_roles=[NodeRole.DATA],+ sharded=True,+ is_alter_on_replicated_table=True,+ ),+ run_sql_with_exceptions(+ f"ALTER TABLE {DISTRIBUTED_EXPERIMENT_EXPOSURES_TABLE()} {ADD_PERSON_COLUMNS}",+ node_roles=[NodeRole.DATA],+ sharded=False,+ is_alter_on_replicated_table=False,+ ),+ run_sql_with_exceptions(+ f"ALTER TABLE {SHARDED_EXPERIMENT_METRIC_EVENTS_TABLE()} {ADD_PERSON_COLUMNS}",+ node_roles=[NodeRole.AUX],+ sharded=False,+ is_alter_on_replicated_table=True,+ ),+ run_sql_with_exceptions(+ f"ALTER TABLE {DISTRIBUTED_EXPERIMENT_METRIC_EVENTS_TABLE()} {ADD_PERSON_COLUMNS}",+ node_roles=[NodeRole.DATA, NodeRole.AUX],+ sharded=False,+ is_alter_on_replicated_table=False,+ ),+]diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txtindex 7432923099bf..c8ad93a53a90 100644--- a/posthog/clickhouse/migrations/max_migration.txt+++ b/posthog/clickhouse/migrations/max_migration.txt@@ -1 +1 @@-0364_sessions_v3_more_ad_ids+0365_experiment_preaggregated_person_columnsdiff --git a/posthog/clickhouse/preaggregation/experiment_exposures_sql.py b/posthog/clickhouse/preaggregation/experiment_exposures_sql.pyindex 670b43a69e40..4555fdfb8d52 100644--- a/posthog/clickhouse/preaggregation/experiment_exposures_sql.py+++ b/posthog/clickhouse/preaggregation/experiment_exposures_sql.py@@ -34,6 +34,13 @@ CREATE TABLE IF NOT EXISTS {table_name}-- Per-entity exposure dataentity_id String,++ -- distinct_id of the first exposure event, the key a person-overrides lookup needs to+ -- find this row's current person.+ distinct_id String,+ -- The entity's person, kept separate from entity_id so it can be rewritten after a person+ -- merge: entity_id is in the sorting key, which ClickHouse refuses to UPDATE.+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),diff --git a/posthog/clickhouse/preaggregation/experiment_metric_events_sql.py b/posthog/clickhouse/preaggregation/experiment_metric_events_sql.pyindex 06aa6274196f..b6cb9ec2538a 100644--- a/posthog/clickhouse/preaggregation/experiment_metric_events_sql.py+++ b/posthog/clickhouse/preaggregation/experiment_metric_events_sql.py@@ -45,6 +45,13 @@ CREATE TABLE IF NOT EXISTS {table_name}-- Per-event dataentity_id String,++ -- distinct_id of the source event, the key a person-overrides lookup needs to+ -- find this row's current person.+ distinct_id String,+ -- The entity's person, kept separate from entity_id so it can be rewritten after a person+ -- merge: entity_id is in the sorting key, which ClickHouse refuses to UPDATE.+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambrindex 7c927ce9fc14..9744547c03c5 100644--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr@@ -2119,6 +2119,13 @@-- Per-entity exposure dataentity_id String,++ -- distinct_id of the first exposure event, the key a person-overrides lookup needs to+ -- find this row's current person.+ distinct_id String,+ -- The entity's person, kept separate from entity_id so it can be rewritten after a person+ -- merge: entity_id is in the sorting key, which ClickHouse refuses to UPDATE.+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),@@ -2147,6 +2154,13 @@-- Per-event dataentity_id String,++ -- distinct_id of the source event, the key a person-overrides lookup needs to+ -- find this row's current person.+ distinct_id String,+ -- The entity's person, kept separate from entity_id so it can be rewritten after a person+ -- merge: entity_id is in the sorting key, which ClickHouse refuses to UPDATE.+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -8234,6 +8248,13 @@-- Per-entity exposure dataentity_id String,++ -- distinct_id of the first exposure event, the key a person-overrides lookup needs to+ -- find this row's current person.+ distinct_id String,+ -- The entity's person, kept separate from entity_id so it can be rewritten after a person+ -- merge: entity_id is in the sorting key, which ClickHouse refuses to UPDATE.+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),@@ -8267,6 +8288,13 @@-- Per-event dataentity_id String,++ -- distinct_id of the source event, the key a person-overrides lookup needs to+ -- find this row's current person.+ distinct_id String,+ -- The entity's person, kept separate from entity_id so it can be rewritten after a person+ -- merge: entity_id is in the sorting key, which ClickHouse refuses to UPDATE.+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,@@ -14126,6 +14154,13 @@-- Per-entity exposure dataentity_id String,++ -- distinct_id of the first exposure event, the key a person-overrides lookup needs to+ -- find this row's current person.+ distinct_id String,+ -- The entity's person, kept separate from entity_id so it can be rewritten after a person+ -- merge: entity_id is in the sorting key, which ClickHouse refuses to UPDATE.+ person_id UUID DEFAULT toUUIDOrZero(entity_id),variant String,first_exposure_time DateTime64(6, 'UTC'),last_exposure_time DateTime64(6, 'UTC'),@@ -14159,6 +14194,13 @@-- Per-event dataentity_id String,++ -- distinct_id of the source event, the key a person-overrides lookup needs to+ -- find this row's current person.+ distinct_id String,+ -- The entity's person, kept separate from entity_id so it can be rewritten after a person+ -- merge: entity_id is in the sorting key, which ClickHouse refuses to UPDATE.+ person_id UUID DEFAULT toUUIDOrZero(entity_id),timestamp DateTime64(6, 'UTC'),event_uuid UUID,session_id String,