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): speed up json drop keys with bounded reuse (#97154)

Rory Shanks7c8b628f0383
diff --git a/clickhouse-udfs/util/README.md b/clickhouse-udfs/util/README.md
index 20d3bac055f8..d603127edfbe 100644
--- a/clickhouse-udfs/util/README.md
+++ b/clickhouse-udfs/util/README.md
@@ -13,5 +13,8 @@ Run `./scripts/build.sh` to test the module and build Linux amd64 and arm64 bina
Run `./scripts/integration_test.sh` to execute the stateless fixtures against ClickHouse.
+See [JSONDropKeys benchmarks](../../docs/internal/json-drop-keys-benchmarks.md) for the
+file-based workloads and commands to compare throughput and allocations.
+
These utility UDFs keep stable, unversioned names. Regenerating the deployment manifest does not
change `UDF_VERSION`, which only switches callers between versioned funnel UDF releases.
diff --git a/clickhouse-udfs/util/cmd/json_drop_keys_udf/main.go b/clickhouse-udfs/util/cmd/json_drop_keys_udf/main.go
index 2dba8d147753..2d349242d017 100644
--- a/clickhouse-udfs/util/cmd/json_drop_keys_udf/main.go
+++ b/clickhouse-udfs/util/cmd/json_drop_keys_udf/main.go
@@ -15,8 +15,6 @@ import (
"github.com/valyala/fastjson"
)
-type emptyT struct{}
-
// a struct for hierarchical keys, e.g. if someone wants to drop "properties.foo.bar", works only for objects
type jsonKey map[string]jsonKey
@@ -25,40 +23,18 @@ type node interface {
DropKeys(keys jsonKey) node
}
-type valueKind int
-
-const (
- kindString valueKind = iota
- kindNumber
- kindBool
- kindNull
-)
-
-type valueNode struct {
- kind valueKind
- str string
- num string
- b bool
-}
+type scalarNode fastjson.Value
-func (v *valueNode) Write(buf *bytes.Buffer) {
- switch v.kind {
- case kindString:
- writeJSONString(buf, v.str)
- case kindNumber:
- buf.WriteString(v.num)
- case kindBool:
- if v.b {
- buf.WriteString("true")
- } else {
- buf.WriteString("false")
- }
- case kindNull:
- buf.WriteString("null")
+func (v *scalarNode) Write(buf *bytes.Buffer) {
+ value := (*fastjson.Value)(v)
+ if value.Type() == fastjson.TypeString {
+ writeJSONString(buf, value.GetStringBytes())
+ } else {
+ buf.Write(value.MarshalTo(buf.AvailableBuffer()))
}
}
-func (v *valueNode) DropKeys(jsonKey) node {
+func (v *scalarNode) DropKeys(jsonKey) node {
return v
}
@@ -77,7 +53,7 @@ func (o *objectNode) Write(buf *bytes.Buffer) {
if i > 0 {
buf.WriteByte(',')
}
- writeJSONString(buf, entry.key)
+ writeJSONString(buf, []byte(entry.key))
buf.WriteByte(':')
entry.value.Write(buf)
}
@@ -100,6 +76,7 @@ func (o *objectNode) DropKeys(keysToDrop jsonKey) node {
writeIdx := 0
for _, entry := range o.entries {
if val, toDrop := keysToDrop[entry.key]; toDrop && val == nil {
+ recycleNode(entry.value)
continue
}
o.entries[writeIdx] = entry
@@ -143,10 +120,10 @@ func expandDottedEntries(entries []objectEntry) []objectEntry {
insertDottedKey(nil, &expanded, entry.key, entry.value, index)
}
- for key := range index {
- delete(index, key)
+ if len(index) <= 4096 {
+ clear(index)
+ dottedIndexPool.Put(index)
}
- dottedIndexPool.Put(index)
return expanded
}
@@ -184,12 +161,7 @@ func insertDottedKey(parent *objectNode, entries *[]objectEntry, key string, val
}
func indexByte(s string, c byte) int {
- for i := 0; i < len(s); i++ {
- if s[i] == c {
- return i
- }
- }
- return -1
+ return strings.IndexByte(s, c)
}
type arrayNode struct {
@@ -214,7 +186,7 @@ func (a *arrayNode) DropKeys(keys jsonKey) node {
return a
}
-func writeJSONString(buf *bytes.Buffer, s string) {
+func writeJSONString(buf *bytes.Buffer, s []byte) {
buf.WriteByte('"')
start := 0
for i := 0; i < len(s); i++ {
@@ -223,7 +195,7 @@ func writeJSONString(buf *bytes.Buffer, s string) {
continue
}
if start < i {
- buf.WriteString(s[start:i])
+ buf.Write(s[start:i])
}
switch ch {
case '\\', '"':
@@ -248,20 +220,19 @@ func writeJSONString(buf *bytes.Buffer, s string) {
start = i + 1
}
if start < len(s) {
- buf.WriteString(s[start:])
+ buf.Write(s[start:])
}
buf.WriteByte('"')
}
-var parserPool = sync.Pool{
- New: func() interface{} {
- return &fastjson.Parser{}
- },
+type cachedParser struct {
+ fastjson.Parser
+ maxInput int
}
-var valueNodePool = sync.Pool{
+var parserPool = sync.Pool{
New: func() interface{} {
- return &valueNode{}
+ return &cachedParser{}
},
}
@@ -279,21 +250,27 @@ var arrayNodePool = sync.Pool{
func recycleNode(n node) {
switch v := n.(type) {
- case *valueNode:
- v.str = ""
- v.num = ""
- valueNodePool.Put(v)
case *objectNode:
for _, entry := range v.entries {
recycleNode(entry.value)
}
- v.entries = v.entries[:0]
+ if cap(v.entries) > 4096 {
+ v.entries = nil
+ } else {
+ clear(v.entries[:cap(v.entries)])
+ v.entries = v.entries[:0]
+ }
objectNodePool.Put(v)
case *arrayNode:
for _, child := range v.values {
recycleNode(child)
}
- v.values = v.values[:0]
+ if cap(v.values) > 4096 {
+ v.values = nil
+ } else {
+ clear(v.values[:cap(v.values)])
+ v.values = v.values[:0]
+ }
arrayNodePool.Put(v)
}
}
@@ -346,62 +323,98 @@ func convertFastJSON(value *fastjson.Value) (node, error) {
}
return arrNode, nil
- case fastjson.TypeString:
- vn := valueNodePool.Get().(*valueNode)
- vn.kind = kindString
- vn.str = string(value.GetStringBytes())
- vn.num = ""
- return vn, nil
- case fastjson.TypeNumber:
- num := value.String()
- vn := valueNodePool.Get().(*valueNode)
- vn.kind = kindNumber
- vn.num = num
- vn.str = ""
- return vn, nil
- case fastjson.TypeTrue:
- vn := valueNodePool.Get().(*valueNode)
- vn.kind = kindBool
- vn.b = true
- vn.str = ""
- vn.num = ""
- return vn, nil
- case fastjson.TypeFalse:
- vn := valueNodePool.Get().(*valueNode)
- vn.kind = kindBool
- vn.b = false
- vn.str = ""
- vn.num = ""
- return vn, nil
- case fastjson.TypeNull:
- vn := valueNodePool.Get().(*valueNode)
- vn.kind = kindNull
- vn.str = ""
- vn.num = ""
- return vn, nil
+ case fastjson.TypeString, fastjson.TypeNumber, fastjson.TypeTrue, fastjson.TypeFalse, fastjson.TypeNull:
+ return (*scalarNode)(value), nil
default:
return nil, fmt.Errorf("unexpected fastjson type %v", value.Type())
}
}
func processLine(keys jsonKey, rawLine []byte, buf *bytes.Buffer) error {
- parser := parserPool.Get().(*fastjson.Parser)
+ parser := parserPool.Get().(*cachedParser)
defer parserPool.Put(parser)
+ if parser.maxInput > max(64*1024, 2*len(rawLine)) {
+ parser.Parser = fastjson.Parser{}
+ parser.maxInput = 0
+ }
+ parser.maxInput = max(parser.maxInput, len(rawLine))
+ if buf.Cap() > max(64*1024, 2*len(rawLine)) {
+ *buf = bytes.Buffer{}
+ }
value, err := parser.ParseBytes(rawLine)
if err != nil {
return fmt.Errorf("json parse error: %w", err)
}
- parsed, err := convertFastJSON(value)
- if err != nil {
- return fmt.Errorf("json parse error: %w", err)
- }
- result := parsed.DropKeys(keys)
buf.Reset()
buf.Grow(len(rawLine))
- result.Write(buf)
- recycleNode(result)
+ if keys == nil {
+ keys = jsonKey{}
+ }
+ return writeFilteredJSON(buf, value, keys)
+}
+
+// Nil keys leave dotted names untouched below paths the filter does not visit.
+func writeFilteredJSON(buf *bytes.Buffer, value *fastjson.Value, keys jsonKey) error {
+ switch value.Type() {
+ case fastjson.TypeObject:
+ obj, _ := value.Object()
+ if keys != nil {
+ hasDottedKeys := false
+ obj.Visit(func(key []byte, _ *fastjson.Value) {
+ hasDottedKeys = hasDottedKeys || bytes.IndexByte(key, '.') >= 0
+ })
+ if hasDottedKeys {
+ // Dotted expansion must preserve duplicate entries and their original merge order.
+ parsed, err := convertFastJSON(value)
+ if err != nil {
+ return err
+ }
+ result := parsed.DropKeys(keys)
+ result.Write(buf)
+ recycleNode(result)
+ return nil
+ }
+ }
+ buf.WriteByte('{')
+ first := true
+ var err error
+ obj.Visit(func(key []byte, child *fastjson.Value) {
+ if err != nil {
+ return
+ }
+ childKeys, drop := keys[string(key)]
+ if drop && childKeys == nil {
+ return
+ }
+ if !first {
+ buf.WriteByte(',')
+ }
+ first = false
+ writeJSONString(buf, key)
+ buf.WriteByte(':')
+ err = writeFilteredJSON(buf, child, childKeys)
+ })
+ buf.WriteByte('}')
+ return err
+ case fastjson.TypeArray:
+ values, _ := value.Array()
+ buf.WriteByte('[')
+ for i, child := range values {
+ if i > 0 {
+ buf.WriteByte(',')
+ }
+ if err := writeFilteredJSON(buf, child, keys); err != nil {
+ return err
+ }
+ }
+ buf.WriteByte(']')
+ case fastjson.TypeString:
+ writeJSONString(buf, value.GetStringBytes())
+ default:
+ buf.Write(value.MarshalTo(buf.AvailableBuffer()))
+ }
return nil
}
@@ -519,46 +532,55 @@ func main() {
}()
}
- reader := bufio.NewReaderSize(os.Stdin, 4*1024*1024)
- writer := bufio.NewWriterSize(os.Stdout, 4*1024*1024)
- defer writer.Flush()
- buf := bytes.NewBuffer(make([]byte, 0, 64*1024))
+ if err := run(os.Stdin, os.Stdout, keysToDrop); err != nil {
+ fmt.Fprintln(stdErr, err)
+ os.Exit(1)
+ }
+}
+
+func readLine(reader *bufio.Reader) ([]byte, error) {
+ line, err := reader.ReadSlice('\n')
+ if err != bufio.ErrBufferFull {
+ return line, err
+ }
+ var full []byte
+ for {
+ full = append(full, line...)
+ if err != bufio.ErrBufferFull {
+ return full, err
+ }
+ line, err = reader.ReadSlice('\n')
+ }
+}
+func run(input io.Reader, output io.Writer, keys jsonKey) error {
+ reader := bufio.NewReaderSize(input, 64*1024)
+ writer := bufio.NewWriterSize(output, 64*1024)
+ buf := bytes.NewBuffer(make([]byte, 0, 64*1024))
for {
- line, err := reader.ReadBytes('\n')
+ line, err := readLine(reader)
if err != nil && err != io.EOF {
- fmt.Fprintf(stdErr, "stdin read error: %v\n", err)
- return
+ return fmt.Errorf("stdin read error: %w", err)
}
-
if len(line) == 0 && err == io.EOF {
- return
+ return writer.Flush()
}
-
- hadNewline := false
- n := len(line)
- if n > 0 && line[n-1] == '\n' {
- hadNewline = true
- n--
- }
- if n > 0 && line[n-1] == '\r' {
- n--
+ hadNewline := len(line) > 0 && line[len(line)-1] == '\n'
+ line = bytes.TrimSuffix(line, []byte("\n"))
+ line = bytes.TrimSuffix(line, []byte("\r"))
+ if procErr := processLine(keys, line, buf); procErr != nil {
+ return fmt.Errorf("line processing error: %w", procErr)
}
- line = line[:n]
-
- procErr := processLine(keysToDrop, line, buf)
- if procErr != nil {
- fmt.Fprintf(stdErr, "line processing error: %v\n", procErr)
- os.Exit(1)
+ if _, writeErr := writer.Write(buf.Bytes()); writeErr != nil {
+ return fmt.Errorf("stdout write error: %w", writeErr)
}
-
- _, _ = writer.Write(buf.Bytes())
if hadNewline {
- _, _ = writer.WriteString("\n")
+ if writeErr := writer.WriteByte('\n'); writeErr != nil {
+ return fmt.Errorf("stdout write error: %w", writeErr)
+ }
}
-
if err == io.EOF {
- return
+ return writer.Flush()
}
}
}
diff --git a/clickhouse-udfs/util/cmd/json_drop_keys_udf/main_test.go b/clickhouse-udfs/util/cmd/json_drop_keys_udf/main_test.go
index ab428aefe656..09426a468649 100644
--- a/clickhouse-udfs/util/cmd/json_drop_keys_udf/main_test.go
+++ b/clickhouse-udfs/util/cmd/json_drop_keys_udf/main_test.go
@@ -2,12 +2,15 @@ package main
import (
"bytes"
- "fmt"
+ "errors"
+ "io"
"os"
"path/filepath"
+ "strings"
"testing"
"github.com/stretchr/testify/assert"
+ "github.com/valyala/fastjson"
)
func TestProcessLineErrorsOnMalformedJSON(t *testing.T) {
@@ -21,6 +24,18 @@ func TestDropKeysJSON(t *testing.T) {
name, input, want string
keys []string
}{
+ {
+ name: "dotted key in unfiltered sibling stays literal",
+ input: `{"keep":{"a.b":1},"items":{"a.b":2,"a.c":3}}`,
+ want: `{"keep":{"a.b":1},"items":{"a":{"c":3}}}`,
+ keys: []string{"items.a.b"},
+ },
+ {
+ name: "dotted key at root expands even outside filter",
+ input: `{"keep.a.b":1,"items.a.b":2,"items.a.c":3}`,
+ want: `{"keep":{"a":{"b":1}},"items":{"a":{"c":3}}}`,
+ keys: []string{"items.a.b"},
+ },
{
"empty",
"{}",
@@ -105,6 +120,69 @@ func TestDropKeysJSON(t *testing.T) {
}
}
+func TestDropKeysPreservesDottedScopeAndEncoding(t *testing.T) {
+ for _, tc := range []struct {
+ input, want string
+ keys []string
+ }{
+ {`{"a.b":1,"a":{"c":2},"a.d":3}`, `{"a":{"b":1},"a":{"c":2,"d":3}}`, nil},
+ {`{"a.b":1,"a":2,"a.c":3}`, `{"a":{},"a":2,"a":{"c":3}}`, []string{"a.b"}},
+ {`{"keep":{"a.b":1},"items":[{"a.b":2,"a.c":3}]}`, `{"keep":{"a.b":1},"items":[{"a":{"c":3}}]}`, []string{"items.a.b"}},
+ {`{"a":1,"a":2,"b":3}`, `{"b":3}`, []string{"a"}},
+ {`[[{"a.b":1,"a.c":2}],null,3]`, `[[{"a":{"c":2}}],null,3]`, []string{"a.b"}},
+ {`{"\u0061":1,"text":"\u0000\u001b\u263a\/","number":-1.230e+04}`, `{"text":"\u0000\u001b☺/","number":-1.230e+04}`, []string{"a"}},
+ } {
+ var output bytes.Buffer
+ if err := processLine(makeKeyDict(tc.keys), []byte(tc.input), &output); err != nil {
+ t.Fatal(err)
+ }
+ if output.String() != tc.want {
+ t.Fatalf("input %s: got %s, want %s", tc.input, output.String(), tc.want)
+ }
+ }
+}
+
+func TestDropKeysLargeRowsAndMemoryReuse(t *testing.T) {
+ for _, size := range []int{31, 4*1024*1024 + 17} {
+ row := `{"keep":"` + strings.Repeat("x", size) + `\n\t","drop":1}`
+ want := `{"keep":"` + strings.Repeat("x", size) + `\n\t"}`
+ for _, ending := range []string{"", "\n", "\r\n"} {
+ var output bytes.Buffer
+ if err := run(strings.NewReader(row+ending), &output, makeKeyDict([]string{"drop"})); err != nil {
+ t.Fatal(err)
+ }
+ expected := want
+ if ending != "" {
+ expected += "\n"
+ }
+ if output.String() != expected {
+ t.Fatalf("row size=%d ending=%q changed", size, ending)
+ }
+ }
+ }
+ obj := &objectNode{entries: make([]objectEntry, 1, 32)}
+ obj.entries[0] = objectEntry{key: "drop", value: (*scalarNode)(fastjson.MustParse(`"secret"`))}
+ obj.DropKeys(makeKeyDict([]string{"drop"}))
+ recycleNode(obj)
+ for _, entry := range obj.entries[:cap(obj.entries)] {
+ if entry.key != "" || entry.value != nil {
+ t.Fatal("recycled object retains dropped values")
+ }
+ }
+}
+
+type failingWriter struct{}
+
+func (failingWriter) Write([]byte) (int, error) { return 0, io.ErrClosedPipe }
+func TestDropKeysStreamErrors(t *testing.T) {
+ if err := run(strings.NewReader(`{"drop":[1,]}`), io.Discard, makeKeyDict([]string{"drop"})); err == nil {
+ t.Fatal("malformed discarded value accepted")
+ }
+ if err := run(strings.NewReader(`{}`), failingWriter{}, nil); !errors.Is(err, io.ErrClosedPipe) {
+ t.Fatalf("write error lost: %v", err)
+ }
+}
+
func TestParseSingleQuotedArray(t *testing.T) {
cases := []struct {
name string
@@ -198,48 +276,59 @@ func TestMakeKeyDict(t *testing.T) {
}
func BenchmarkProcessLine(b *testing.B) {
- input := []byte(`{"id":1,"identity":"abc","properties":{"secret":"drop","public":"keep"},"events":[{"identity":"nested","value":1}],"amount":934504962295726700000}`)
- keys := makeKeyDict([]string{"identity", "properties.secret"})
- var buf bytes.Buffer
+ benchmarkProcessLines(b, "testdata/benchmarks/small.jsonl", []string{"identity", "properties.secret"})
+}
- b.ReportAllocs()
- b.SetBytes(int64(len(input)))
+func BenchmarkProcessFixture(b *testing.B) {
+ path := os.Getenv("BENCH_FILE")
+ if path == "" {
+ path = "testdata/benchmarks/events.jsonl"
+ } else if !filepath.IsAbs(path) {
+ path = filepath.Join("../..", path)
+ }
- for i := 0; i < b.N; i++ {
- if err := processLine(keys, input, &buf); err != nil {
- b.Fatal(err)
- }
+ for _, tc := range []struct {
+ name string
+ path string
+ keys []string
+ }{
+ {name: "missing", path: path, keys: []string{"missing"}},
+ {name: "nested", path: path, keys: []string{"properties.secret"}},
+ {name: "subtree", path: path, keys: []string{"properties"}},
+ {name: "array", path: path, keys: []string{"events.identity"}},
+ {name: "dotted", path: "testdata/benchmarks/dotted.jsonl", keys: []string{"items.a.b"}},
+ } {
+ b.Run(tc.name, func(b *testing.B) {
+ benchmarkProcessLines(b, tc.path, tc.keys)
+ })
}
}
-func BenchmarkProcessFixture(b *testing.B) {
- lines, totalBytes := loadBenchmarkLines(b)
- keys := makeKeyDict([]string{"identity", "properties.secret"})
+func benchmarkProcessLines(b *testing.B, path string, keyPaths []string) {
+ b.Helper()
+ lines, totalBytes := loadBenchmarkLines(b, path)
+ keys := makeKeyDict(keyPaths)
var buf bytes.Buffer
+ for _, line := range lines {
+ if err := processLine(keys, line, &buf); err != nil {
+ b.Fatal(err)
+ }
+ }
b.ReportAllocs()
- b.SetBytes(int64(totalBytes))
+ b.SetBytes(int64(totalBytes / len(lines)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
- for _, line := range lines {
- if err := processLine(keys, line, &buf); err != nil {
- b.Fatal(err)
- }
+ if err := processLine(keys, lines[i%len(lines)], &buf); err != nil {
+ b.Fatal(err)
}
}
}
-func loadBenchmarkLines(b *testing.B) ([][]byte, int) {
+func loadBenchmarkLines(b *testing.B, path string) ([][]byte, int) {
b.Helper()
- path := os.Getenv("BENCH_FILE")
- if path == "" {
- return generatedBenchmarkLines()
- } else if !filepath.IsAbs(path) {
- path = filepath.Join("../..", path)
- }
-
data, err := os.ReadFile(path)
if err != nil {
b.Fatal(err)
@@ -263,23 +352,3 @@ func loadBenchmarkLines(b *testing.B) ([][]byte, int) {
return lines, totalBytes
}
-
-func generatedBenchmarkLines() ([][]byte, int) {
- lines := make([][]byte, 0, 256)
- totalBytes := 0
- for i := 0; i < 256; i++ {
- line := []byte(fmt.Sprintf(
- `{"id":%d,"identity":"user-%d","properties":{"secret":"s%d","public":"p%d"},"events":[{"identity":"nested-%d","value":%d}],"amount":934504962295726700000}`,
- i,
- i,
- i,
- i,
- i,
- i,
- ))
- lines = append(lines, line)
- totalBytes += len(line)
- }
-
- return lines, totalBytes
-}
diff --git a/clickhouse-udfs/util/cmd/json_drop_keys_udf/testdata/benchmarks/dotted.jsonl b/clickhouse-udfs/util/cmd/json_drop_keys_udf/testdata/benchmarks/dotted.jsonl
new file mode 100644
index 000000000000..c488cec3438a
--- /dev/null
+++ b/clickhouse-udfs/util/cmd/json_drop_keys_udf/testdata/benchmarks/dotted.jsonl
@@ -0,0 +1 @@
+{"keep":{"a.b":1},"items":[{"a.b":2,"a.c":3}],"root.key":4}
diff --git a/clickhouse-udfs/util/cmd/json_drop_keys_udf/testdata/benchmarks/events.jsonl b/clickhouse-udfs/util/cmd/json_drop_keys_udf/testdata/benchmarks/events.jsonl
new file mode 100644
index 000000000000..ef7922e4aee2
--- /dev/null
+++ b/clickhouse-udfs/util/cmd/json_drop_keys_udf/testdata/benchmarks/events.jsonl
@@ -0,0 +1,16 @@
+{"id":0,"identity":"user-0","properties":{"secret":"generated-secret-0","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":1,"identity":"user-1","properties":{"secret":"generated-secret-1","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":2,"identity":"user-2","properties":{"secret":"generated-secret-2","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":3,"identity":"user-3","properties":{"secret":"generated-secret-3","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":4,"identity":"user-4","properties":{"secret":"generated-secret-4","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":5,"identity":"user-5","properties":{"secret":"generated-secret-5","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":6,"identity":"user-6","properties":{"secret":"generated-secret-6","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":7,"identity":"user-7","properties":{"secret":"generated-secret-7","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":8,"identity":"user-8","properties":{"secret":"generated-secret-8","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":9,"identity":"user-9","properties":{"secret":"generated-secret-9","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":10,"identity":"user-10","properties":{"secret":"generated-secret-10","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":11,"identity":"user-11","properties":{"secret":"generated-secret-11","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":12,"identity":"user-12","properties":{"secret":"generated-secret-12","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":13,"identity":"user-13","properties":{"secret":"generated-secret-13","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":14,"identity":"user-14","properties":{"secret":"generated-secret-14","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
+{"id":15,"identity":"user-15","properties":{"secret":"generated-secret-15","public":"synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload synthetic payload ","url":"https://example.com/page","escaped":"line\nbreak\tquoted\" slash\\"},"events":[{"identity":"nested-0","value":0,"enabled":true},{"identity":"nested-1","value":1,"enabled":true},{"identity":"nested-2","value":2,"enabled":true},{"identity":"nested-3","value":3,"enabled":true},{"identity":"nested-4","value":4,"enabled":true},{"identity":"nested-5","value":5,"enabled":true},{"identity":"nested-6","value":6,"enabled":true},{"identity":"nested-7","value":7,"enabled":true}],"amount":934504962295726700000}
diff --git a/clickhouse-udfs/util/cmd/json_drop_keys_udf/testdata/benchmarks/small.jsonl b/clickhouse-udfs/util/cmd/json_drop_keys_udf/testdata/benchmarks/small.jsonl
new file mode 100644
index 000000000000..fb98e5a4f16f
--- /dev/null
+++ b/clickhouse-udfs/util/cmd/json_drop_keys_udf/testdata/benchmarks/small.jsonl
@@ -0,0 +1 @@
+{"id":1,"identity":"abc","properties":{"secret":"drop","public":"keep"},"events":[{"identity":"nested","value":1}],"amount":934504962295726700000}
diff --git a/docs/internal/json-drop-keys-benchmarks.md b/docs/internal/json-drop-keys-benchmarks.md
new file mode 100644
index 000000000000..92c663f67829
--- /dev/null
+++ b/docs/internal/json-drop-keys-benchmarks.md
@@ -0,0 +1,60 @@
+# JSONDropKeys benchmarks
+
+The Go benchmarks in `clickhouse-udfs/util/cmd/json_drop_keys_udf/main_test.go` measure
+parsing, filtering, and serialization per JSON row. Run them from the repository root:
+
+```sh
+go -C clickhouse-udfs/util test ./cmd/json_drop_keys_udf \
+ -run '^$' -bench '^BenchmarkProcess' -benchmem -benchtime=1s -count=7 -cpu=1
+```
+
+When using the Codex development environment, prefix the command with `.codex/with-flox`.
+To select an installed compiler explicitly, pass its executable path instead of `go`.
+For example, `.codex/with-flox /usr/local/go/bin/go version` verifies the system compiler.
+Use the same compiler and CPU setting for both revisions.
+
+## Workloads
+
+Fixtures live in `clickhouse-udfs/util/cmd/json_drop_keys_udf/testdata/benchmarks/`.
+They are synthetic and contain no customer data.
+
+| Benchmark | Fixture | Removed paths |
+| --------------------------------- | ----------------------------------------------------------------- | ------------------------------- |
+| `BenchmarkProcessLine` | `small.jsonl`: one compact event | `identity`, `properties.secret` |
+| `BenchmarkProcessFixture/missing` | `events.jsonl`: 16 events with nested properties and arrays | `missing` |
+| `BenchmarkProcessFixture/nested` | `events.jsonl` | `properties.secret` |
+| `BenchmarkProcessFixture/subtree` | `events.jsonl` | `properties` |
+| `BenchmarkProcessFixture/array` | `events.jsonl` | `events.identity` |
+| `BenchmarkProcessFixture/dotted` | `dotted.jsonl`: dotted names, an array, and an unfiltered sibling | `items.a.b` |
+
+Each iteration processes one row, cycling through the fixture. `ns/op`, `B/op`, and
+`allocs/op` are per row; `MB/s` uses the average input row size, excluding newlines.
+Fixture loading, filter construction, and a warmup pass happen outside the timed region.
+These benchmarks measure steady-state buffer reuse, not cold-start allocations or peak RSS.
+
+The fixture benchmark previously reported an entire batch per operation. Its new named
+sub-benchmarks report one row per operation; compare revisions using the same benchmark code.
+
+## Custom input
+
+Set `BENCH_FILE` to a JSON Lines file to replace `events.jsonl` for the missing, nested,
+subtree, and array workloads. Absolute paths work directly; relative paths resolve from
+`clickhouse-udfs/util`. The small and dotted benchmarks keep their dedicated fixtures.
+
+```sh
+BENCH_FILE=/tmp/generated-events.jsonl go -C clickhouse-udfs/util test \
+ ./cmd/json_drop_keys_udf -run '^$' -bench '^BenchmarkProcessFixture/nested$' \
+ -benchmem -benchtime=1s -count=7 -cpu=1
+```
+
+For Codex, put the environment assignment after the wrapper:
+
+```sh
+.codex/with-flox env BENCH_FILE=/tmp/generated-events.jsonl /usr/local/go/bin/go \
+ -C clickhouse-udfs/util test ./cmd/json_drop_keys_udf -run '^$' \
+ -bench '^BenchmarkProcessFixture/nested$' -benchmem -benchtime=1s -count=7 -cpu=1
+```
+
+Keep custom datasets outside the repository unless they are safe to publish. When comparing
+revisions, alternate their execution order and retain every sample. Native benchmark timings
+do not include ClickHouse scheduling, transport, or concurrent query load.
diff --git a/posthog/user_scripts/json_drop_keys_udf_aarch64 b/posthog/user_scripts/json_drop_keys_udf_aarch64
index a8811d207214..58cddd6d9062 100755
Binary files a/posthog/user_scripts/json_drop_keys_udf_aarch64 and b/posthog/user_scripts/json_drop_keys_udf_aarch64 differ
diff --git a/posthog/user_scripts/json_drop_keys_udf_x86_64 b/posthog/user_scripts/json_drop_keys_udf_x86_64
index 323fa2efd4c2..c350273b2379 100755
Binary files a/posthog/user_scripts/json_drop_keys_udf_x86_64 and b/posthog/user_scripts/json_drop_keys_udf_x86_64 differ