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.

fix(clickhouse): bound JSON UDF depth and preserve arrays (#94885)

Rory Shanksb1c7c53bc74f
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.go
index fd0ccfbe0d90..7260400fc003 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
@@ -36,8 +36,8 @@ type pathRule struct {
var eventPropertyRules = makeEventPropertyRules()
const (
- // Leave headroom below ClickHouse's 1024-level JSON parser ceiling so destination casts cannot poison a Kafka block.
- maxJSONDepth = 1000
+ // Match fastjson's parser ceiling so every stored document remains readable by the blob-cleanup UDF.
+ maxJSONDepth = 300
unparseablePropertiesKey = "$unparseable_properties"
)
diff --git a/clickhouse-udfs/util/cmd/json_strip_empty_strings_and_nulls_udf/main.go b/clickhouse-udfs/util/cmd/json_strip_empty_strings_and_nulls_udf/main.go
index b514291bd13b..25c880cea768 100644
--- a/clickhouse-udfs/util/cmd/json_strip_empty_strings_and_nulls_udf/main.go
+++ b/clickhouse-udfs/util/cmd/json_strip_empty_strings_and_nulls_udf/main.go
@@ -52,23 +52,22 @@ func appendCleanJSON(dst []byte, value *fastjson.Value) ([]byte, bool) {
case fastjson.TypeArray:
values, _ := value.Array()
dst = append(dst, '[')
- kept := 0
- for _, child := range values {
- itemStart := len(dst)
- if kept > 0 {
+ for i, child := range values {
+ if i > 0 {
dst = append(dst, ',')
}
+ itemStart := len(dst)
var keep bool
dst, keep = appendCleanJSON(dst, child)
- if keep {
- kept++
- } else {
+ if !keep {
dst = dst[:itemStart]
+ if child.Type() == fastjson.TypeObject {
+ dst = append(dst, '{', '}')
+ } else {
+ dst = child.MarshalTo(dst)
+ }
}
}
- if kept == 0 {
- return dst[:start], false
- }
return append(dst, ']'), true
case fastjson.TypeNumber:
dst = value.MarshalTo(dst)
diff --git a/clickhouse-udfs/util/cmd/json_strip_empty_strings_and_nulls_udf/main_test.go b/clickhouse-udfs/util/cmd/json_strip_empty_strings_and_nulls_udf/main_test.go
index e2089ec3f248..e1f9ab73aada 100644
--- a/clickhouse-udfs/util/cmd/json_strip_empty_strings_and_nulls_udf/main_test.go
+++ b/clickhouse-udfs/util/cmd/json_strip_empty_strings_and_nulls_udf/main_test.go
@@ -8,11 +8,11 @@ import (
func TestProcessLine(t *testing.T) {
tests := map[string]string{
- `{"empty":"","null":null,"object":{"nested":null},"array":[null,"",{},[]]}`: `{}`,
- `{"keep":0,"false":false,"text":" ","nested":{"drop":null,"keep":"x"},"array":[null,"",{},[],{"drop":null,"keep":1}]}`: `{"keep":0,"false":false,"text":" ","nested":{"keep":"x"},"array":[{"keep":1}]}`,
+ `{"empty":"","null":null,"object":{"nested":null},"array":[null,"",{},[]]}`: `{"array":[null,"",{},[]]}`,
+ `{"keep":0,"false":false,"text":" ","nested":{"drop":null,"keep":"x"},"array":[null,"",{},[],{"drop":null,"keep":1}]}`: `{"keep":0,"false":false,"text":" ","nested":{"keep":"x"},"array":[null,"",{},[],{"keep":1}]}`,
`{"\u000b":"x"}`: `{"\u000b":"x"}`,
`{"a":1,"a":null,"a":2}`: `{"a":1,"a":2}`,
- `[null,"",[],{},0,false,"x"]`: `[0,false,"x"]`,
+ `[null,"",[],{},0,false,"x"]`: `[null,"",[],{},0,false,"x"]`,
`934504962295726700000`: `"934504962295726700000"`,
}
@@ -43,7 +43,7 @@ func TestRunChunked(t *testing.T) {
if err := runChunked(strings.NewReader(input), &output); err != nil {
t.Fatal(err)
}
- if got, want := output.String(), "{}\n{\"a\":1}\n[2]\n"; got != want {
+ if got, want := output.String(), "{}\n{\"a\":1}\n[null,2]\n"; got != want {
t.Fatalf("runChunked output = %q, want %q", got, want)
}
}
diff --git a/clickhouse-udfs/util/testdata/json_strip_empty_strings_and_nulls/stateless/README.md b/clickhouse-udfs/util/testdata/json_strip_empty_strings_and_nulls/stateless/README.md
index ea6a76c360f9..fdd1054f4e39 100644
--- a/clickhouse-udfs/util/testdata/json_strip_empty_strings_and_nulls/stateless/README.md
+++ b/clickhouse-udfs/util/testdata/json_strip_empty_strings_and_nulls/stateless/README.md
@@ -1,3 +1,3 @@
# JSONStripEmptyStringsAndNulls Stateless Fixtures
-These cases cover recursive removal from objects and arrays, useless roots becoming `{}`, retained zero/false/whitespace values, and malformed JSON failure.
+These cases cover recursive object cleanup without shifting array positions, useless roots becoming `{}`, retained zero/false/whitespace values, and malformed JSON failure.
diff --git a/clickhouse-udfs/util/testdata/json_strip_empty_strings_and_nulls/stateless/cleanup.reference b/clickhouse-udfs/util/testdata/json_strip_empty_strings_and_nulls/stateless/cleanup.reference
index 3ad5f952bb11..416e254a19e2 100644
--- a/clickhouse-udfs/util/testdata/json_strip_empty_strings_and_nulls/stateless/cleanup.reference
+++ b/clickhouse-udfs/util/testdata/json_strip_empty_strings_and_nulls/stateless/cleanup.reference
@@ -1,3 +1,3 @@
-{}
-{"keep":0,"false":false,"text":" ","nested":{"keep":"x"},"array":[{"keep":1}]}
-[{"a":1},false]
+{"array":[null,"",{},[]]}
+{"keep":0,"false":false,"text":" ","nested":{"keep":"x"},"array":[null,"",{},[],{"keep":1}]}
+[{},{"a":1},[],null,"",false]
diff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64
index 72cb0bf18a52..2437afdd4ec8 100755
Binary files a/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 and b/posthog/user_scripts/json_clean_posthog_event_properties_udf_aarch64 differ
diff --git a/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64 b/posthog/user_scripts/json_clean_posthog_event_properties_udf_x86_64
index ebc6730a7c4a..20e778620c92 100755
Binary 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
diff --git a/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf_aarch64 b/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf_aarch64
index c61a43fdf7fe..ed304f689584 100755
Binary files a/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf_aarch64 and b/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf_aarch64 differ
diff --git a/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf_x86_64 b/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf_x86_64
index 9f1065a14dec..c082de94b575 100755
Binary files a/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf_x86_64 and b/posthog/user_scripts/json_strip_empty_strings_and_nulls_udf_x86_64 differ