: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
fix(clickhouse): sort feature flag keys before insertion (#95796)
Rory Shanks7d2a81be0d18
diff --git a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.goindex 7260400fc003..db5953feb29b 100644--- a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go+++ b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main.go@@ -9,6 +9,7 @@ import ("io""os""runtime/pprof"+ "slices""strings""unsafe")@@ -249,7 +250,20 @@ func (p *processor) cleanEventProperties(v *value) (*value, error) {if createdFeatureFlags {v.entries = append(v.entries, entry{key: "$feature_flags", value: featureFlags})}- return p.cleanNode(eventPropertyRules, v)+ cleaned, err := p.cleanNode(eventPropertyRules, v)+ if err != nil {+ return nil, err+ }+ for _, property := range cleaned.entries {+ if property.key == "$feature_flags" && property.value.kind == kindObject {+ compare := func(a, b entry) int { return strings.Compare(a.key, b.key) }+ if !slices.IsSortedFunc(property.value.entries, compare) {+ slices.SortFunc(property.value.entries, compare)+ p.mutated = true+ }+ }+ }+ return cleaned, nil}func (p *processor) newValue(kind valueKind) *value {diff --git a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.goindex 8de651923466..5bcc0ca55765 100644--- a/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go+++ b/clickhouse-udfs/util/cmd/json_clean_posthog_event_properties_udf/main_test.go@@ -50,15 +50,21 @@ func TestProcessLinePreservesPersonProperties(t *testing.T) {}func TestProcessLineGroupsFeaturePropertiesAndPreservesExistingFlagValues(t *testing.T) {- input := []byte(`{"$feature/first-flag":"fresh","$feature/number":42,"$feature/enabled":true,"$feature/config":{"nested.value":"dropped"},"$feature_flags":"invalid","$feature_flags":{"existing":"kept","first-flag":"existing"},"$feature_flag_payloads":{"flag":"dropped"},"other":"value"}`)- want := `{"$feature_flags":{"existing":"kept","first-flag":"existing","number":42,"enabled":true,"config":{"nested":{"value":"dropped"}}},"other":"value"}`-- var got bytes.Buffer- if err := processLine(input, &got); err != nil {- t.Fatal(err)+ tests := map[string]string{+ `{"$feature/first-flag":"fresh","$feature/number":42,"$feature/enabled":true,"$feature/config":{"nested.value":"dropped"},"$feature_flags":"invalid","$feature_flags":{"existing":"kept","first-flag":"existing"},"$feature_flag_payloads":{"flag":"dropped"},"other":"value"}`: `{"$feature_flags":{"config":{"nested":{"value":"dropped"}},"enabled":true,"existing":"kept","first-flag":"existing","number":42},"other":"value"}`,+ `{"$feature/zebra":false,"$feature/alpha":"control","other":1}`: `{"other":1,"$feature_flags":{"alpha":"control","zebra":false}}`,+ `{"$feature_flags":{"zebra":false,"alpha":"control"}}`: `{"$feature_flags":{"alpha":"control","zebra":false}}`,+ `{"$feature/a.b":1,"$feature/a":{"b":2},"$feature/Z":true}`: `{"$feature_flags":{"Z":true,"a":{"b":1}}}`,}- if got.String() != want {- t.Fatalf("processLine() = %s, want %s", got.String(), want)++ for input, want := range tests {+ var got bytes.Buffer+ if err := processLine([]byte(input), &got); err != nil {+ t.Fatalf("processLine(%s) returned error: %v", input, err)+ }+ if got.String() != want {+ t.Fatalf("processLine(%s) = %s, want %s", input, got.String(), want)+ }}}diff --git a/docs/internal/clickhouse-utility-udfs.md b/docs/internal/clickhouse-utility-udfs.mdnew file mode 100644index 000000000000..47fb58e0766e--- /dev/null+++ b/docs/internal/clickhouse-utility-udfs.md@@ -0,0 +1,8 @@+# ClickHouse utility UDFs++`JSONCleanPostHogEventProperties` groups `$feature/<key>` event properties into `$feature_flags`.+Before emitting JSON for insertion, it sorts the keys in `$feature_flags` alphabetically using case-sensitive string order.+This also applies to existing `$feature_flags` objects, after cleanup resolves duplicates and expands dotted keys.+Flag values and person-property ordering follow the existing cleanup rules.++See [the utility UDF README](../../clickhouse-udfs/util/README.md) for build and integration-test commands.diff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64index 2437afdd4ec8..ea567bf6da9f 100755Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 differdiff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64index 20e778620c92..818132d8c2a2 100755Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 differ