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.

feat(flags): add property-group maps to flag_evaluations (#117390)

Phil Haack4aa2d66a3e78
diff --git a/posthog/clickhouse/hcl/golden/dev/data.hcl b/posthog/clickhouse/hcl/golden/dev/data.hcl
index 10e8003572dc..a7a331298d8c 100644
--- a/posthog/clickhouse/hcl/golden/dev/data.hcl
+++ b/posthog/clickhouse/hcl/golden/dev/data.hcl
@@ -1849,6 +1849,12 @@ database "posthog" {
type = "String"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -6004,6 +6010,14 @@ database "posthog" {
default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^\"|\"$', '')"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ materialized = "mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)')))"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ materialized = "mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)')))"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -6038,6 +6052,26 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ index "properties_group_custom_keys_bf" {
+ expr = "mapKeys(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "properties_group_custom_values_bf" {
+ expr = "mapValues(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_keys_bf" {
+ expr = "mapKeys(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_values_bf" {
+ expr = "mapValues(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.flag_evaluations"
replica_name = "{replica}"
diff --git a/posthog/clickhouse/hcl/golden/local-multi/data.hcl b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
index 820b7bad47b6..a0e49b7439dd 100644
--- a/posthog/clickhouse/hcl/golden/local-multi/data.hcl
+++ b/posthog/clickhouse/hcl/golden/local-multi/data.hcl
@@ -1944,6 +1944,12 @@ database "posthog" {
type = "String"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -6514,6 +6520,14 @@ database "posthog" {
default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^\"|\"$', '')"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ materialized = "mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)')))"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ materialized = "mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)')))"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -6548,6 +6562,26 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ index "properties_group_custom_keys_bf" {
+ expr = "mapKeys(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "properties_group_custom_values_bf" {
+ expr = "mapValues(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_keys_bf" {
+ expr = "mapKeys(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_values_bf" {
+ expr = "mapValues(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.flag_evaluations"
replica_name = "{replica}"
diff --git a/posthog/clickhouse/hcl/golden/local-single/all.hcl b/posthog/clickhouse/hcl/golden/local-single/all.hcl
index b1e6a9f277e8..2b90f0dc2704 100644
--- a/posthog/clickhouse/hcl/golden/local-single/all.hcl
+++ b/posthog/clickhouse/hcl/golden/local-single/all.hcl
@@ -2024,6 +2024,12 @@ database "posthog" {
type = "String"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -12560,6 +12566,14 @@ SQL
default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^\"|\"$', '')"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ materialized = "mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)')))"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ materialized = "mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)')))"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -12594,6 +12608,26 @@ SQL
type = "minmax"
granularity = 1
}
+ index "properties_group_custom_keys_bf" {
+ expr = "mapKeys(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "properties_group_custom_values_bf" {
+ expr = "mapValues(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_keys_bf" {
+ expr = "mapKeys(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_values_bf" {
+ expr = "mapValues(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.flag_evaluations"
replica_name = "{replica}"
diff --git a/posthog/clickhouse/hcl/golden/prod-eu/data.hcl b/posthog/clickhouse/hcl/golden/prod-eu/data.hcl
index 21eb044a577c..fcfba7615d83 100644
--- a/posthog/clickhouse/hcl/golden/prod-eu/data.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-eu/data.hcl
@@ -1806,6 +1806,12 @@ database "posthog" {
type = "String"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -6179,6 +6185,14 @@ database "posthog" {
default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^\"|\"$', '')"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ materialized = "mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)')))"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ materialized = "mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)')))"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -6213,6 +6227,26 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ index "properties_group_custom_keys_bf" {
+ expr = "mapKeys(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "properties_group_custom_values_bf" {
+ expr = "mapValues(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_keys_bf" {
+ expr = "mapKeys(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_values_bf" {
+ expr = "mapValues(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.flag_evaluations"
replica_name = "{replica}"
diff --git a/posthog/clickhouse/hcl/golden/prod-us/data.hcl b/posthog/clickhouse/hcl/golden/prod-us/data.hcl
index 23ac4033746f..82ab4645bf90 100644
--- a/posthog/clickhouse/hcl/golden/prod-us/data.hcl
+++ b/posthog/clickhouse/hcl/golden/prod-us/data.hcl
@@ -1921,6 +1921,12 @@ database "posthog" {
type = "String"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -5992,6 +5998,14 @@ database "posthog" {
default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^\"|\"$', '')"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ materialized = "mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)')))"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ materialized = "mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)')))"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -6026,6 +6040,26 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ index "properties_group_custom_keys_bf" {
+ expr = "mapKeys(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "properties_group_custom_values_bf" {
+ expr = "mapValues(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_keys_bf" {
+ expr = "mapKeys(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_values_bf" {
+ expr = "mapValues(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.flag_evaluations"
replica_name = "{replica}"
diff --git a/posthog/clickhouse/hcl/roles/data/shared/flag_evaluations.hcl b/posthog/clickhouse/hcl/roles/data/shared/flag_evaluations.hcl
index 7d2d07e93993..2b16f5475128 100644
--- a/posthog/clickhouse/hcl/roles/data/shared/flag_evaluations.hcl
+++ b/posthog/clickhouse/hcl/roles/data/shared/flag_evaluations.hcl
@@ -83,6 +83,12 @@ database "posthog" {
type = "String"
comment = "column_materializer::properties::$feature_flag_request_id"
}
+ column "properties_group_custom" {
+ type = "Map(String, String)"
+ }
+ column "person_properties_map_custom" {
+ type = "Map(String, String)"
+ }
column "_timestamp" {
type = "DateTime"
}
@@ -140,6 +146,12 @@ database "posthog" {
patch_column "request_id" {
default = "replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^\"|\"$', '')"
}
+ patch_column "properties_group_custom" {
+ materialized = "mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)')))"
+ }
+ patch_column "person_properties_map_custom" {
+ materialized = "mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)')))"
+ }
index "distinct_id_idx" {
expr = "distinct_id"
type = "bloom_filter(0.01)"
@@ -165,6 +177,26 @@ database "posthog" {
type = "minmax"
granularity = 1
}
+ index "properties_group_custom_keys_bf" {
+ expr = "mapKeys(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "properties_group_custom_values_bf" {
+ expr = "mapValues(properties_group_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_keys_bf" {
+ expr = "mapKeys(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
+ index "person_properties_map_custom_values_bf" {
+ expr = "mapValues(person_properties_map_custom)"
+ type = "bloom_filter"
+ granularity = 1
+ }
engine "replicated_replacing_merge_tree" {
zoo_path = "/clickhouse/tables/{shard}/posthog.flag_evaluations"
replica_name = "{replica}"
diff --git a/posthog/clickhouse/hcl/sql/dev/data.sql b/posthog/clickhouse/hcl/sql/dev/data.sql
index c95bdc915a97..5beb841c7879 100644
--- a/posthog/clickhouse/hcl/sql/dev/data.sql
+++ b/posthog/clickhouse/hcl/sql/dev/data.sql
@@ -1285,6 +1285,8 @@ CREATE TABLE posthog.sharded_flag_evaluations (
response LowCardinality(String) DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response',
session_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id',
request_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ properties_group_custom Map(String, String) MATERIALIZED mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)'))),
+ person_properties_map_custom Map(String, String) MATERIALIZED mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)'))),
_timestamp DateTime,
_offset UInt64,
_partition UInt64,
@@ -1292,7 +1294,11 @@ CREATE TABLE posthog.sharded_flag_evaluations (
INDEX person_id_idx person_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX session_id_idx session_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX request_id_idx request_id TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX inserted_at_idx inserted_at TYPE minmax GRANULARITY 1
+ INDEX inserted_at_idx inserted_at TYPE minmax GRANULARITY 1,
+ INDEX properties_group_custom_keys_bf mapKeys(properties_group_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX properties_group_custom_values_bf mapValues(properties_group_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX person_properties_map_custom_keys_bf mapKeys(person_properties_map_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX person_properties_map_custom_values_bf mapValues(person_properties_map_custom) TYPE bloom_filter GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.flag_evaluations', '{replica}', _timestamp) ORDER BY (team_id, flag_key, toDate(timestamp), cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toYYYYMM(timestamp) TTL toDate(timestamp) + toIntervalDay(90) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.sharded_heatmaps (
session_id String,
@@ -3011,6 +3017,8 @@ CREATE TABLE posthog.flag_evaluations (
response LowCardinality(String) COMMENT 'column_materializer::properties::$feature_flag_response',
session_id String COMMENT 'column_materializer::properties::$session_id',
request_id String COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ properties_group_custom Map(String, String),
+ person_properties_map_custom Map(String, String),
_timestamp DateTime,
_offset UInt64,
_partition UInt64
diff --git a/posthog/clickhouse/hcl/sql/local-multi/data.sql b/posthog/clickhouse/hcl/sql/local-multi/data.sql
index e63662e95203..a69f8896cb58 100644
--- a/posthog/clickhouse/hcl/sql/local-multi/data.sql
+++ b/posthog/clickhouse/hcl/sql/local-multi/data.sql
@@ -1363,6 +1363,8 @@ CREATE TABLE posthog.sharded_flag_evaluations (
response LowCardinality(String) DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response',
session_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id',
request_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ properties_group_custom Map(String, String) MATERIALIZED mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)'))),
+ person_properties_map_custom Map(String, String) MATERIALIZED mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)'))),
_timestamp DateTime,
_offset UInt64,
_partition UInt64,
@@ -1370,7 +1372,11 @@ CREATE TABLE posthog.sharded_flag_evaluations (
INDEX person_id_idx person_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX session_id_idx session_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX request_id_idx request_id TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX inserted_at_idx inserted_at TYPE minmax GRANULARITY 1
+ INDEX inserted_at_idx inserted_at TYPE minmax GRANULARITY 1,
+ INDEX properties_group_custom_keys_bf mapKeys(properties_group_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX properties_group_custom_values_bf mapValues(properties_group_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX person_properties_map_custom_keys_bf mapKeys(person_properties_map_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX person_properties_map_custom_values_bf mapValues(person_properties_map_custom) TYPE bloom_filter GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.flag_evaluations', '{replica}', _timestamp) ORDER BY (team_id, flag_key, toDate(timestamp), cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toYYYYMM(timestamp) TTL toDate(timestamp) + toIntervalDay(90) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.sharded_heatmaps (
session_id String,
@@ -3601,6 +3607,8 @@ CREATE TABLE posthog.flag_evaluations (
response LowCardinality(String) COMMENT 'column_materializer::properties::$feature_flag_response',
session_id String COMMENT 'column_materializer::properties::$session_id',
request_id String COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ properties_group_custom Map(String, String),
+ person_properties_map_custom Map(String, String),
_timestamp DateTime,
_offset UInt64,
_partition UInt64
diff --git a/posthog/clickhouse/hcl/sql/local-single/all.sql b/posthog/clickhouse/hcl/sql/local-single/all.sql
index 0fb1abe05009..75e2ba733630 100644
--- a/posthog/clickhouse/hcl/sql/local-single/all.sql
+++ b/posthog/clickhouse/hcl/sql/local-single/all.sql
@@ -2240,6 +2240,8 @@ CREATE TABLE posthog.sharded_flag_evaluations (
response LowCardinality(String) DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response',
session_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id',
request_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ properties_group_custom Map(String, String) MATERIALIZED mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)'))),
+ person_properties_map_custom Map(String, String) MATERIALIZED mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)'))),
_timestamp DateTime,
_offset UInt64,
_partition UInt64,
@@ -2247,7 +2249,11 @@ CREATE TABLE posthog.sharded_flag_evaluations (
INDEX person_id_idx person_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX session_id_idx session_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX request_id_idx request_id TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX inserted_at_idx inserted_at TYPE minmax GRANULARITY 1
+ INDEX inserted_at_idx inserted_at TYPE minmax GRANULARITY 1,
+ INDEX properties_group_custom_keys_bf mapKeys(properties_group_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX properties_group_custom_values_bf mapValues(properties_group_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX person_properties_map_custom_keys_bf mapKeys(person_properties_map_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX person_properties_map_custom_values_bf mapValues(person_properties_map_custom) TYPE bloom_filter GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.flag_evaluations', '{replica}', _timestamp) ORDER BY (team_id, flag_key, toDate(timestamp), cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toYYYYMM(timestamp) TTL toDate(timestamp) + toIntervalDay(90) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.sharded_heatmaps (
session_id String,
@@ -6850,6 +6856,8 @@ CREATE TABLE posthog.flag_evaluations (
response LowCardinality(String) COMMENT 'column_materializer::properties::$feature_flag_response',
session_id String COMMENT 'column_materializer::properties::$session_id',
request_id String COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ properties_group_custom Map(String, String),
+ person_properties_map_custom Map(String, String),
_timestamp DateTime,
_offset UInt64,
_partition UInt64
diff --git a/posthog/clickhouse/hcl/sql/prod-eu/data.sql b/posthog/clickhouse/hcl/sql/prod-eu/data.sql
index 52e862331bd4..25dfae15e218 100644
--- a/posthog/clickhouse/hcl/sql/prod-eu/data.sql
+++ b/posthog/clickhouse/hcl/sql/prod-eu/data.sql
@@ -1562,6 +1562,8 @@ CREATE TABLE posthog.sharded_flag_evaluations (
response LowCardinality(String) DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response',
session_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id',
request_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ properties_group_custom Map(String, String) MATERIALIZED mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)'))),
+ person_properties_map_custom Map(String, String) MATERIALIZED mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)'))),
_timestamp DateTime,
_offset UInt64,
_partition UInt64,
@@ -1569,7 +1571,11 @@ CREATE TABLE posthog.sharded_flag_evaluations (
INDEX person_id_idx person_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX session_id_idx session_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX request_id_idx request_id TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX inserted_at_idx inserted_at TYPE minmax GRANULARITY 1
+ INDEX inserted_at_idx inserted_at TYPE minmax GRANULARITY 1,
+ INDEX properties_group_custom_keys_bf mapKeys(properties_group_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX properties_group_custom_values_bf mapValues(properties_group_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX person_properties_map_custom_keys_bf mapKeys(person_properties_map_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX person_properties_map_custom_values_bf mapValues(person_properties_map_custom) TYPE bloom_filter GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.flag_evaluations', '{replica}', _timestamp) ORDER BY (team_id, flag_key, toDate(timestamp), cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toYYYYMM(timestamp) TTL toDate(timestamp) + toIntervalDay(90) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.sharded_heatmaps (
session_id String,
@@ -3458,6 +3464,8 @@ CREATE TABLE posthog.flag_evaluations (
response LowCardinality(String) COMMENT 'column_materializer::properties::$feature_flag_response',
session_id String COMMENT 'column_materializer::properties::$session_id',
request_id String COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ properties_group_custom Map(String, String),
+ person_properties_map_custom Map(String, String),
_timestamp DateTime,
_offset UInt64,
_partition UInt64
diff --git a/posthog/clickhouse/hcl/sql/prod-us/data.sql b/posthog/clickhouse/hcl/sql/prod-us/data.sql
index 1a2fe6f7e0b2..a293b5c79c09 100644
--- a/posthog/clickhouse/hcl/sql/prod-us/data.sql
+++ b/posthog/clickhouse/hcl/sql/prod-us/data.sql
@@ -1404,6 +1404,8 @@ CREATE TABLE posthog.sharded_flag_evaluations (
response LowCardinality(String) DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_response'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_response',
session_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$session_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$session_id',
request_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ properties_group_custom Map(String, String) MATERIALIZED mapSort(mapFilter((key, _) -> ((key NOT LIKE '$%') AND (key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'))), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)'))),
+ person_properties_map_custom Map(String, String) MATERIALIZED mapSort(mapFilter((key, _) -> (key NOT LIKE '$%'), CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)'))),
_timestamp DateTime,
_offset UInt64,
_partition UInt64,
@@ -1411,7 +1413,11 @@ CREATE TABLE posthog.sharded_flag_evaluations (
INDEX person_id_idx person_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX session_id_idx session_id TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX request_id_idx request_id TYPE bloom_filter(0.01) GRANULARITY 1,
- INDEX inserted_at_idx inserted_at TYPE minmax GRANULARITY 1
+ INDEX inserted_at_idx inserted_at TYPE minmax GRANULARITY 1,
+ INDEX properties_group_custom_keys_bf mapKeys(properties_group_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX properties_group_custom_values_bf mapValues(properties_group_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX person_properties_map_custom_keys_bf mapKeys(person_properties_map_custom) TYPE bloom_filter GRANULARITY 1,
+ INDEX person_properties_map_custom_values_bf mapValues(person_properties_map_custom) TYPE bloom_filter GRANULARITY 1
) ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/posthog.flag_evaluations', '{replica}', _timestamp) ORDER BY (team_id, flag_key, toDate(timestamp), cityHash64(distinct_id), cityHash64(uuid)) PARTITION BY toYYYYMM(timestamp) TTL toDate(timestamp) + toIntervalDay(90) SETTINGS index_granularity = 8192, ttl_only_drop_parts = 1;
CREATE TABLE posthog.sharded_heatmaps (
session_id String,
@@ -3225,6 +3231,8 @@ CREATE TABLE posthog.flag_evaluations (
response LowCardinality(String) COMMENT 'column_materializer::properties::$feature_flag_response',
session_id String COMMENT 'column_materializer::properties::$session_id',
request_id String COMMENT 'column_materializer::properties::$feature_flag_request_id',
+ properties_group_custom Map(String, String),
+ person_properties_map_custom Map(String, String),
_timestamp DateTime,
_offset UInt64,
_partition UInt64
diff --git a/posthog/clickhouse/migrations/0356_flag_evaluations_property_groups.py b/posthog/clickhouse/migrations/0356_flag_evaluations_property_groups.py
new file mode 100644
index 000000000000..80d033b6a7f3
--- /dev/null
+++ b/posthog/clickhouse/migrations/0356_flag_evaluations_property_groups.py
@@ -0,0 +1,38 @@
+from posthog.clickhouse.client.connection import NodeRole
+from posthog.clickhouse.client.migration_tools import run_sql_with_exceptions
+from posthog.clickhouse.property_groups import flag_evaluations_property_group_definitions, iter_groups, property_groups
+from posthog.models.flag_evaluations.sql import FLAG_EVALUATIONS_DATA_TABLE, FLAG_EVALUATIONS_TABLE
+
+# Adds the property-group map columns to sharded_flag_evaluations and the flag_evaluations read
+# table. posthog/models/flag_evaluations/sql.py carries the rationale.
+#
+# The ALTERs change metadata only. A part written before them has no map files, and ClickHouse
+# computes the maps from the JSON when a query reads that part. writable_flag_evaluations stays
+# as it is, so inserts arrive without the maps and the shards compute them.
+
+operations = [
+ *[
+ run_sql_with_exceptions(
+ statement,
+ node_roles=[NodeRole.DATA],
+ sharded=True,
+ is_alter_on_replicated_table=True,
+ )
+ for group in iter_groups(flag_evaluations_property_group_definitions)
+ for statement in property_groups.get_alter_create_statements(
+ FLAG_EVALUATIONS_DATA_TABLE, group.source_column, group.group_name
+ )
+ ],
+ *[
+ run_sql_with_exceptions(
+ statement,
+ node_roles=[NodeRole.DATA],
+ sharded=False,
+ is_alter_on_replicated_table=False,
+ )
+ for group in iter_groups(flag_evaluations_property_group_definitions)
+ for statement in property_groups.get_alter_create_statements(
+ FLAG_EVALUATIONS_TABLE, group.source_column, group.group_name
+ )
+ ],
+]
diff --git a/posthog/clickhouse/migrations/max_migration.txt b/posthog/clickhouse/migrations/max_migration.txt
index 44cd66913e30..5bc212e557c7 100644
--- a/posthog/clickhouse/migrations/max_migration.txt
+++ b/posthog/clickhouse/migrations/max_migration.txt
@@ -1 +1 @@
-0355_logs34_to_pattern_buckets_mv
+0356_flag_evaluations_property_groups
diff --git a/posthog/clickhouse/property_groups.py b/posthog/clickhouse/property_groups.py
index b581bdbb9f39..6503c3b995f0 100644
--- a/posthog/clickhouse/property_groups.py
+++ b/posthog/clickhouse/property_groups.py
@@ -1,18 +1,20 @@
import dataclasses
-from collections.abc import Callable, Iterable, Mapping
+from collections.abc import Callable, Iterable, Iterator, Mapping
from dataclasses import dataclass
from typing import Optional
+from posthog.dataclasses import frozen
+
TableName = str
PropertySourceColumnName = str
PropertyGroupName = str
-@dataclass
+@dataclass(frozen=True)
class PropertyGroupDefinition:
key_filter_expression: str
key_filter_function: Callable[[str], bool]
- codec: str = "ZSTD(1)"
+ codec: str | None = "ZSTD(1)"
is_materialized: bool = True
column_type_name: str = "map"
column_name: Optional[str] = None
@@ -34,13 +36,14 @@ class PropertyGroupDefinition:
if not self.is_materialized:
return column_definition
+ codec_clause = f"CODEC({self.codec})" if self.codec is not None else ""
return f"""\
{column_definition}
MATERIALIZED mapSort(
mapFilter((key, _) -> {self.key_filter_expression},
CAST(JSONExtractKeysAndValues({source_column}, 'String'), 'Map(String, String)'))
)
- CODEC({self.codec})
+ {codec_clause}
"""
def get_index_definitions(
@@ -54,13 +57,24 @@ class PropertyGroupDefinition:
yield f"{map_column_name}_values_bf mapValues({map_column_name}) TYPE bloom_filter"
+PropertyGroupDefinitions = Mapping[PropertySourceColumnName, Mapping[PropertyGroupName, PropertyGroupDefinition]]
+
+
+@frozen
+class ColumnPropertyGroup:
+ source_column: PropertySourceColumnName
+ group_name: PropertyGroupName
+ definition: PropertyGroupDefinition
+
+
+def iter_groups(definitions: PropertyGroupDefinitions) -> Iterator[ColumnPropertyGroup]:
+ for source_column, groups in definitions.items():
+ for group_name, definition in groups.items():
+ yield ColumnPropertyGroup(source_column=source_column, group_name=group_name, definition=definition)
+
+
class PropertyGroupManager:
- def __init__(
- self,
- groups: Mapping[
- TableName, Mapping[PropertySourceColumnName, Mapping[PropertyGroupName, PropertyGroupDefinition]]
- ],
- ) -> None:
+ def __init__(self, groups: Mapping[TableName, PropertyGroupDefinitions]) -> None:
self.__groups = groups
def get_property_group_columns(
@@ -81,11 +95,10 @@ class PropertyGroupManager:
Returns an iterable of SQL DDL chunks that can be used to define all property groups for the provided table as
part of a CREATE TABLE statement.
"""
- for source_column, groups in self.__groups[table].items():
- for group_name, group_definition in groups.items():
- yield group_definition.get_column_definition(source_column, group_name)
- for index_definition in group_definition.get_index_definitions(source_column, group_name):
- yield f"INDEX {index_definition}"
+ for group in iter_groups(self.__groups[table]):
+ yield group.definition.get_column_definition(group.source_column, group.group_name)
+ for index_definition in group.definition.get_index_definitions(group.source_column, group.group_name):
+ yield f"INDEX {index_definition}"
def get_alter_create_statements(
self,
@@ -214,18 +227,42 @@ event_property_group_definitions = {
},
}
+
+def without_materialization(definitions: PropertyGroupDefinitions) -> PropertyGroupDefinitions:
+ """The same groups for a Distributed table, which declares the columns but computes nothing."""
+ return {
+ column_name: {
+ group_name: dataclasses.replace(group_definition, is_materialized=False)
+ for group_name, group_definition in column_group_definitions.items()
+ }
+ for column_name, column_group_definitions in definitions.items()
+ }
+
+
+# Flag calls carry no $ai_* keys, so the ai groups are left out. The feature_flags group is left out because a
+# $feature/* map would copy a large share of each flag call's JSON. No column on this table family carries a CODEC.
+# hidden=True stops HogQL from reading the maps, so it reads the JSON until every environment has the columns.
+flag_evaluations_property_group_definitions: PropertyGroupDefinitions = {
+ "properties": {
+ "custom": dataclasses.replace(
+ event_property_group_definitions["properties"]["custom"], codec=None, hidden=True
+ ),
+ },
+ "person_properties": {
+ "custom": dataclasses.replace(
+ event_property_group_definitions["person_properties"]["custom"], codec=None, hidden=True
+ ),
+ },
+}
+
property_groups = PropertyGroupManager(
{
"sharded_events": event_property_group_definitions,
- "events": {
- # the events distributed table shares the same property group names and types as the sharded_events table,
- # just without the materialize statements
- column_name: {
- group_name: dataclasses.replace(group_definition, is_materialized=False)
- for group_name, group_definition in column_group_definitions.items()
- }
- for column_name, column_group_definitions in event_property_group_definitions.items()
- },
+ # the events distributed table shares the same property group names and types as the sharded_events table,
+ # just without the materialize statements
+ "events": without_materialization(event_property_group_definitions),
+ "sharded_flag_evaluations": flag_evaluations_property_group_definitions,
+ "flag_evaluations": without_materialization(flag_evaluations_property_group_definitions),
"logs_distributed": {
"attributes": {
"str": PropertyGroupDefinition(
diff --git a/posthog/clickhouse/test/__snapshots__/test_schema.ambr b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
index 83980d694126..306c27454939 100644
--- a/posthog/clickhouse/test/__snapshots__/test_schema.ambr
+++ b/posthog/clickhouse/test/__snapshots__/test_schema.ambr
@@ -2105,6 +2105,8 @@
, session_id String COMMENT 'column_materializer::properties::$session_id'
, request_id String COMMENT 'column_materializer::properties::$feature_flag_request_id'
+ , properties_group_custom Map(String, String)
+ , person_properties_map_custom Map(String, String)
, _timestamp DateTime
, _offset UInt64
@@ -8415,6 +8417,13 @@
, request_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id'
+ , properties_group_custom Map(String, String) MATERIALIZED mapSort( mapFilter((key, _) -> key NOT LIKE '$%' AND key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)')) )
+ , INDEX properties_group_custom_keys_bf mapKeys(properties_group_custom) TYPE bloom_filter
+ , INDEX properties_group_custom_values_bf mapValues(properties_group_custom) TYPE bloom_filter
+ , person_properties_map_custom Map(String, String) MATERIALIZED mapSort( mapFilter((key, _) -> key NOT LIKE '$%', CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)')) )
+ , INDEX person_properties_map_custom_keys_bf mapKeys(person_properties_map_custom) TYPE bloom_filter
+ , INDEX person_properties_map_custom_values_bf mapValues(person_properties_map_custom) TYPE bloom_filter
+
, INDEX distinct_id_idx distinct_id TYPE bloom_filter(0.01) GRANULARITY 1
, INDEX person_id_idx person_id TYPE bloom_filter(0.01) GRANULARITY 1
, INDEX session_id_idx session_id TYPE bloom_filter(0.01) GRANULARITY 1
@@ -14623,6 +14632,13 @@
, request_id String DEFAULT replaceRegexpAll(JSONExtractRaw(properties, '$feature_flag_request_id'), '^"|"$', '') COMMENT 'column_materializer::properties::$feature_flag_request_id'
+ , properties_group_custom Map(String, String) MATERIALIZED mapSort( mapFilter((key, _) -> key NOT LIKE '$%' AND key NOT IN ('token', 'distinct_id', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term', 'gclid', 'gad_source', 'gclsrc', 'dclid', 'gbraid', 'wbraid', 'fbclid', 'msclkid', 'twclid', 'li_fat_id', 'mc_cid', 'igshid', 'ttclid', 'rdt_cid', 'epik', 'qclid', 'sccid', 'irclid', '_kx'), CAST(JSONExtractKeysAndValues(properties, 'String'), 'Map(String, String)')) )
+ , INDEX properties_group_custom_keys_bf mapKeys(properties_group_custom) TYPE bloom_filter
+ , INDEX properties_group_custom_values_bf mapValues(properties_group_custom) TYPE bloom_filter
+ , person_properties_map_custom Map(String, String) MATERIALIZED mapSort( mapFilter((key, _) -> key NOT LIKE '$%', CAST(JSONExtractKeysAndValues(person_properties, 'String'), 'Map(String, String)')) )
+ , INDEX person_properties_map_custom_keys_bf mapKeys(person_properties_map_custom) TYPE bloom_filter
+ , INDEX person_properties_map_custom_values_bf mapValues(person_properties_map_custom) TYPE bloom_filter
+
, INDEX distinct_id_idx distinct_id TYPE bloom_filter(0.01) GRANULARITY 1
, INDEX person_id_idx person_id TYPE bloom_filter(0.01) GRANULARITY 1
, INDEX session_id_idx session_id TYPE bloom_filter(0.01) GRANULARITY 1
diff --git a/posthog/clickhouse/test/test_schema.py b/posthog/clickhouse/test/test_schema.py
index 2a0d7acc39f6..0e4f587fb07d 100644
--- a/posthog/clickhouse/test/test_schema.py
+++ b/posthog/clickhouse/test/test_schema.py
@@ -15,6 +15,7 @@ from posthog.hogql.database.schema.flag_evaluations import FLAG_EVALUATIONS_CLIC
from posthog.clickhouse.client import sync_execute
from posthog.clickhouse.kafka_engine import CONSUMER_GROUP_EVENTS_JSON_NATIVE_JSON, KAFKA_COLUMNS_WITH_PARTITION
from posthog.clickhouse.logs import LOGS34_TO_PATTERN_BUCKETS_MV_SELECT, LOGS34_TO_VOLUME_BUCKETS_MV_SELECT
+from posthog.clickhouse.property_groups import flag_evaluations_property_group_definitions, iter_groups
from posthog.clickhouse.schema import (
CREATE_KAFKA_TABLE_QUERIES,
CREATE_MERGETREE_TABLE_QUERIES,
@@ -275,6 +276,12 @@ def test_flag_evaluations_read_table_declares_every_stored_column():
# The comment above _COPIED_COLUMNS gives the reason the backfill leaves out each of these columns.
_FLAG_EVALUATIONS_COLUMNS_THE_BACKFILL_LEAVES_OUT = {"inserted_at", "_offset", "_partition"}
+# MATERIALIZED, so no insert can name them, and every shard computes them from the JSON.
+_FLAG_EVALUATIONS_PROPERTY_GROUP_COLUMN_NAMES = {
+ group.definition.get_column_name(group.source_column, group.group_name)
+ for group in iter_groups(flag_evaluations_property_group_definitions)
+}
+
def test_flag_evaluations_backfill_copies_every_stored_column():
# A column that the backfill does not copy gets its DEFAULT on every backfilled row. No error
@@ -284,7 +291,12 @@ def test_flag_evaluations_backfill_copies_every_stored_column():
typed = set(_declared_column_names(_FLAG_EVALUATIONS_TYPED_COLUMNS))
assert typed
- assert stored == set(_COPIED_COLUMNS) | typed | _FLAG_EVALUATIONS_COLUMNS_THE_BACKFILL_LEAVES_OUT
+ assert stored == (
+ set(_COPIED_COLUMNS)
+ | typed
+ | _FLAG_EVALUATIONS_COLUMNS_THE_BACKFILL_LEAVES_OUT
+ | _FLAG_EVALUATIONS_PROPERTY_GROUP_COLUMN_NAMES
+ )
def _hogql_column_names(table: Table) -> set[str]:
@@ -308,6 +320,7 @@ _FLAG_EVALUATIONS_COLUMNS_HIDDEN_FROM_HOGQL = {
"person_properties",
"person_created_at",
"person_mode",
+ *_FLAG_EVALUATIONS_PROPERTY_GROUP_COLUMN_NAMES,
}
diff --git a/posthog/dags/tests/test_data_deletion_requests.py b/posthog/dags/tests/test_data_deletion_requests.py
index 79b61362f88d..a9297b6876ab 100644
--- a/posthog/dags/tests/test_data_deletion_requests.py
+++ b/posthog/dags/tests/test_data_deletion_requests.py
@@ -2841,10 +2841,13 @@ def test_verify_property_removal_counts_person_properties_on_flag_evaluations(
cluster.any_host(_truncate_flag_evaluations).result()
-def _flag_evaluation_rows(team_id: int, client: Client) -> list[tuple[str, dict, dict | None, str, str, str, str, int]]:
+def _flag_evaluation_rows(
+ team_id: int, client: Client
+) -> list[tuple[str, dict, dict | None, str, str, str, str, int, dict[str, str], dict[str, str]]]:
rows = client.execute(
"SELECT toString(uuid), properties, person_properties, toString(person_created_at), person_mode, flag_key, "
- "session_id, toUnixTimestamp(_timestamp) FROM flag_evaluations WHERE team_id = %(team_id)s AND _row_exists = 1",
+ "session_id, toUnixTimestamp(_timestamp), properties_group_custom, person_properties_map_custom "
+ "FROM flag_evaluations WHERE team_id = %(team_id)s AND _row_exists = 1",
{"team_id": team_id},
)
return sorted(
@@ -2866,6 +2869,7 @@ _STORED_PERSON = {"email": "[email protected]", "plan": "pro"}
pytest.param(["$session_id"], [], _STORED_PERSON, id="typed_column"),
pytest.param(["$feature_flag"], [], _STORED_PERSON, id="sort_key_column"),
pytest.param(["$session_id"], ["email"], _STORED_PERSON, id="with_person_properties"),
+ pytest.param(["keep"], [], _STORED_PERSON, id="custom_property"),
# An insert that sets person_properties to '' stores '', and the event-property half still
# selects the row, so the copy must leave the empty value alone.
pytest.param(["$session_id"], ["email"], None, id="empty_person_properties"),
@@ -2927,6 +2931,8 @@ def test_full_job_property_removal_rewrites_flag_evaluations(
cleaned.get("$feature_flag", ""),
cleaned.get("$session_id", ""),
int(marker.timestamp()) + 1,
+ {key: value for key, value in cleaned.items() if not key.startswith("$")},
+ cleaned_person or {},
)
for uuid in uuids
]
diff --git a/posthog/hogql/database/schema/test/test_flag_evaluations.py b/posthog/hogql/database/schema/test/test_flag_evaluations.py
index 36999edc9888..4dd8ed7bd88c 100644
--- a/posthog/hogql/database/schema/test/test_flag_evaluations.py
+++ b/posthog/hogql/database/schema/test/test_flag_evaluations.py
@@ -142,6 +142,34 @@ class TestFlagEvaluationsTable(ClickhouseTestMixin, BaseTest):
)
]
+ def test_shards_compute_the_property_group_maps_on_insert(self):
+ row_uuid = uuid.uuid4()
+ sync_execute(
+ """
+ INSERT INTO writable_flag_evaluations
+ (uuid, event, properties, person_properties, timestamp, team_id, distinct_id)
+ VALUES
+ """,
+ [
+ (
+ str(row_uuid),
+ "$feature_flag_called",
+ json.dumps({"quote": 'a"b', "nulled": None, "$lib": "web", "token": "t", "keep": "yes"}),
+ json.dumps({"email": "[email protected]", "$creator_event_uuid": "x"}),
+ self.occurred_at,
+ self.team.pk,
+ "probe-distinct-id",
+ )
+ ],
+ )
+
+ rows = sync_execute(
+ "SELECT properties_group_custom, person_properties_map_custom FROM flag_evaluations WHERE uuid = %(uuid)s",
+ {"uuid": str(row_uuid)},
+ )
+
+ assert rows == [({"quote": 'a"b', "keep": "yes"}, {"email": "[email protected]"})]
+
def test_person_id_follows_a_later_merge(self):
# Ingestion never rewrites the person on the row, so only the read can correct it. Drop the
# override join and `uniq(person_id)` counts the pre-merge and the post-merge person as two
diff --git a/posthog/models/flag_evaluations/sql.py b/posthog/models/flag_evaluations/sql.py
index 1d80e13e8387..3f0a3d5188a2 100644
--- a/posthog/models/flag_evaluations/sql.py
+++ b/posthog/models/flag_evaluations/sql.py
@@ -1,3 +1,5 @@
+from collections.abc import Iterable
+
from django.conf import settings
from posthog.clickhouse.kafka_engine import (
@@ -7,6 +9,7 @@ from posthog.clickhouse.kafka_engine import (
trim_quotes_expr,
ttl_period,
)
+from posthog.clickhouse.property_groups import property_groups
from posthog.clickhouse.table_engines import Distributed, MergeTreeEngine, ReplacingMergeTree, ReplicationScheme
from posthog.kafka_client.topics import KAFKA_CLICKHOUSE_FLAG_EVALUATIONS
@@ -172,6 +175,25 @@ _FLAG_EVALUATIONS_PROXY_TYPED_COLUMNS = """
, request_id String COMMENT 'column_materializer::properties::$feature_flag_request_id'
"""
+
+# posthog/clickhouse/test/test_schema.py reads one column per line, so each piece renders on one line.
+def _single_line(pieces: Iterable[str]) -> str:
+ return "".join(f"\n , {' '.join(piece.split())}" for piece in pieces)
+
+
+# Property-group maps with the events tables' key filters. On cloud, HogQL reads custom event and
+# person properties on events from the same maps instead of the JSON. They are MATERIALIZED, unlike
+# the typed columns above, because no writer sets them. Property removal copies rows without them,
+# and the reingest recomputes them from the cleaned JSON, as on sharded_events.
+_FLAG_EVALUATIONS_PROPERTY_GROUP_COLUMNS = _single_line(
+ property_groups.get_create_table_pieces(FLAG_EVALUATIONS_DATA_TABLE)
+)
+
+# The read table declares the map columns plain, without their indexes, like the typed columns.
+_FLAG_EVALUATIONS_PROXY_PROPERTY_GROUP_COLUMNS = _single_line(
+ property_groups.get_create_table_pieces(FLAG_EVALUATIONS_TABLE)
+)
+
# The bloom filters cover point lookups the sort key can't serve (a specific user,
# person, session, or flags-service request). The minmax on inserted_at serves
# incremental consumers that checkpoint on it: partitioning is on timestamp, so an
@@ -211,6 +233,7 @@ CREATE TABLE IF NOT EXISTS {FLAG_EVALUATIONS_DATA_TABLE}
(
{_FLAG_EVALUATIONS_COLUMNS}
{_FLAG_EVALUATIONS_TYPED_COLUMNS}
+ {_FLAG_EVALUATIONS_PROPERTY_GROUP_COLUMNS}
{_FLAG_EVALUATIONS_INDEXES}
{KAFKA_COLUMNS_WITH_PARTITION}
)
@@ -247,12 +270,12 @@ def DROP_FLAG_EVALUATIONS_TABLE_SQL() -> str:
return f"DROP TABLE IF EXISTS {FLAG_EVALUATIONS_DATA_TABLE} SYNC"
-def _distributed_table_sql(table_name: str, *, typed_columns: str = "") -> str:
+def _distributed_table_sql(table_name: str, *, proxy_columns: str = "") -> str:
return f"""
CREATE TABLE IF NOT EXISTS {table_name}
(
{_FLAG_EVALUATIONS_COLUMNS}
- {typed_columns}
+ {proxy_columns}
{KAFKA_COLUMNS_WITH_PARTITION}
)
ENGINE = {Distributed(data_table=FLAG_EVALUATIONS_DATA_TABLE, sharding_key=FLAG_EVALUATIONS_SHARDING_KEY)}
@@ -264,7 +287,8 @@ WRITABLE_FLAG_EVALUATIONS_TABLE_SQL = lambda: _distributed_table_sql(FLAG_EVALUA
# Read path on DATA nodes, and the name queries use.
DISTRIBUTED_FLAG_EVALUATIONS_TABLE_SQL = lambda: _distributed_table_sql(
- FLAG_EVALUATIONS_TABLE, typed_columns=_FLAG_EVALUATIONS_PROXY_TYPED_COLUMNS
+ FLAG_EVALUATIONS_TABLE,
+ proxy_columns=_FLAG_EVALUATIONS_PROXY_TYPED_COLUMNS + _FLAG_EVALUATIONS_PROXY_PROPERTY_GROUP_COLUMNS,
)
diff --git a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt b/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
index 8710b88ab91e..f85b14b9d8fc 100644
--- a/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
+++ b/posthog/test/repo_invariants/dataclass_frozen_baseline.txt
@@ -13,7 +13,6 @@
1 posthog/cache_utils.py
1 posthog/clickhouse/custom_metrics.py
1 posthog/clickhouse/explain.py
-1 posthog/clickhouse/property_groups.py
1 posthog/clickhouse/test/test_explain.py
1 posthog/dags/common/resources.py
1 posthog/dags/tests/test_events_backfill_to_duckling.py