Skip to content

Resolve JSON path presence and Dynamic variant serializations once per block in JSONAllPaths and JSONAllValues - #122387

Open
groeneai wants to merge 6 commits into
ClickHouse:masterfrom
groeneai:json-resolve-paths-and-serializations-per-block
Open

groeneai wants to merge 6 commits into
ClickHouse:masterfrom
groeneai:json-resolve-paths-and-serializations-per-block

Conversation

@groeneai

@groeneai groeneai commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #122226

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Speed up INSERT into a table with a text index over JSONAllPaths(json) or JSONAllValues(json), and the functions JSONAllPaths and JSONAllValues themselves, by resolving each path's presence and each value's serialization once per block instead of once per row.

Description

For every row, JSONAllPaths and JSONDynamicPaths looked each path up by name and called a virtual isNullAt; JSONAllValues made the same isNullAt call and resolved each dynamic value's serialization by type name through DataTypesCache. A text index over them pays this on every inserted block.

Now each dynamic path's local discriminators and each variant's serialization are resolved once per block. The NULL test is the discriminator comparison ColumnDynamic::isNullAt makes; a dynamic value whose serialization is exactly SerializationString is copied as is, as its serializeText does. Other values keep going through serializeText, the shared variant through the Dynamic one. No setting, serialization or format changes.

x86_64, -O3 builds of the base and the patched tree with identical flags (no ThinLTO), servers on disjoint pinned 8-core sets, 1M rows, 8 alternating pairs of query_duration_ms:

query base, ms change swapped A/A
INSERT, sparse, JSONAllPaths index 4732 -25.5% -29.8% +0.9%
INSERT, sparse, JSONAllValues index 5672 -14.9% -18.3% +1.8%
INSERT, dense, JSONAllValues index 2803 -6.4% -10.7% -1.0%
JSONAllValues, dense 548 -84.3% -85.1% +4.3%
JSONAllValues, sparse, 1024-row blocks 1057 -27.9% -20.8% +0.4%
INSERT, no index (control) 3375 -2.0% [-11.2, +8.2] -1.9% +3.7%

The output of all seven JSON*Paths*/JSONAllValues functions and every file of the indexed parts is byte-identical before and after. JSONAllPathsWithTypes and JSONDynamicPathsWithTypes keep their per-row lookups, to keep this change small.

#120403 replaces the getSerialization(name, type) overload used here and cheapens that lookup, as #113344 does for binary serialization.

The performance test is the first for these functions.

All statements
statement base, ms change [95% CI], faster pairs sides swapped A/A
INSERT, sparse, JSONAllPaths index 4732 -25.5% [-29.8, -20.9] 8/8 -29.8% [-32.9, -26.6] 8/8 +0.9%
INSERT, sparse, JSONAllValues index 5672 -14.9% [-20.4, -9.0] 7/8 -18.3% [-20.4, -16.2] 8/8 +1.8%
INSERT, dense, JSONAllPaths index 880 -14.8% [-21.0, -8.1] 8/8 -6.4% [-18.1, +6.9] 5/8 +2.4%
INSERT, dense, JSONAllValues index 2803 -6.4% [-9.7, -3.1] 8/8 -10.7% [-15.3, -5.9] 8/8 -1.0%
INSERT, sparse from String, JSONAllPaths index 5207 -28.1% [-32.7, -23.2] 8/8 -28.9% [-32.3, -25.3] 8/8 +0.3%
INSERT, sparse, no index 3375 -2.0% [-11.2, +8.2] 3/8 -1.9% [-9.8, +6.8] 4/8 +3.7%
INSERT, dense, no index 608 -13.7% [-19.4, -7.5] 8/8 +1.5% [-7.8, +11.8] 3/8 -2.7%
JSONAllPaths, sparse 733 -47.9% 8/8 -44.4% 8/8 -7.6%
JSONAllValues, sparse 958 -48.0% 8/8 -49.6% 8/8 +8.5%
JSONAllValues, dense 548 -84.3% 8/8 -85.1% 8/8 +4.3%
INSERT, JSONAllPaths index, 1024-row blocks 11761 -15.0% 8/8 -6.3% 6/8 +0.2%
JSONAllValues, sparse, 1024-row blocks 1057 -27.9% 8/8 -20.8% 7/8 +0.4%
JSONAllPathsWithTypes, sparse 730 -3.4% 5/8 +2.1% 3/8 +2.3%

Workflow [PR]
Sync PR [sync-upstream/pr/122387]

groeneai and others added 2 commits September 26, 2026 04:40
…r block

JSONAllPaths and JSONDynamicPaths looked every typed and dynamic path up by
name in the ColumnObject path maps and called a virtual isNullAt on it, for
every row: with 202 dynamic paths that is 202 string-hash probes per row even
when the row holds 12 paths. JSONAllValues made the same checks and wrote each
dynamic-path value through SerializationDynamic::serializeText, which resolves
the serialization of the variant by type name through DataTypesCache for every
value. A text index over JSONAllPaths(json) or JSONAllValues(json) pays both
on every inserted block.

Resolve, once per block, the local discriminators of each dynamic path and,
for JSONAllValues, the serialization of each variant, with the same
DataTypesCache getSerialization(name, type) call serializeTextImpl makes per
value. A row is NULL exactly when its local discriminator is
NULL_DISCRIMINATOR, which is what ColumnDynamic::isNullAt checks. A value
whose serialization is exactly SerializationString (a String variant) is
copied as is, because SerializationString::serializeText writes the raw bytes.
The shared variant is left unresolved and keeps going through
SerializationDynamic::serializeText: its storage column is a ColumnString of
type-tagged binary values that must be decoded one by one. Typed and dynamic
paths are disjoint, so the dynamic-first lookup of shouldIncludePath was never
needed, and the helper is removed.

JSONAllPathsWithTypes and JSONDynamicPathsWithTypes keep their per-row
lookups, to keep this change small; JSONAllPathsWithTypes measured unchanged.

Measured on x86_64 (-O3 builds of one commit without ThinLTO, servers on
disjoint pinned 8-core sets, 1M rows, 8 alternating pairs, both side orders,
A/A control): INSERT with a JSONAllPaths text index -26% / -30%, with a
JSONAllValues index -15% / -18% (sparse) and -6% / -11% (dense), INSERT from
String with a JSONAllPaths index -28% / -29%, and JSONAllValues over dense
JSON -84% / -85%. INSERT without an index and JSONAllPathsWithTypes show no
change that holds in both side orders, and 1024-row blocks are faster too. The
output of all seven functions and every file of the indexed parts are
byte-identical before and after.

Closes ClickHouse#122226

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the two property comments on the cached discriminators and serializations to one line each, drop the comment restating what PathNullCheck::typed does, and put the existing NULL-skip comment back where it was.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-issue PR origin: picked up an external GitHub issue labels Sep 26, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 2 findings in round 1, 0 in the round-2 recheck) plus my own cold review of each round (5 findings listed here).

# Sev Finding Verdict Evidence / action
1 ⚠️ The performance test timed its INSERTs while background merges of the growing target tables kept running AGREE, fixed @ 6595083 SYSTEM STOP MERGES / SYSTEM START MERGES, as bloom_filter_insert.xml does
2 ⚠️ No benchmark or test exercised the typed String copy in JSONAllValues: every fixture is untyped JSON AGREE, fixed @ 6595083 measured a typed-only fixture: -24.7% in one side order, -3.4% [-8.9, +2.3] swapped, below the keep rule, so the copy and its test were removed
3 ⚠️ The description said JSONAllPathsWithTypes / JSONDynamicPathsWithTypes cannot back a text index, but a keyValuePairs text index accepts their Map(String, String) AGREE, fixed @ 6595083 description and commit message corrected: they are left as they are to keep the change small
4 💡 JSONAllValues resolves every variant of every dynamic path once per block, so a very small block over very wide JSON pays more than before AGREE, not changed 1024-row blocks measured -27.9% / -20.8%
5 💡 The dense JSONAllValues statement of the performance test runs about 34 ms on the new build AGREE, not changed the short-query check is off; this statement carries the largest measured effect
6 💡 The slowest statement of the performance test averages about 1.3-1.5 s per run on a fast x86 host, under the report's 2 s limit AGREE, not changed in line with text_index_build on master; a "too long" mark does not change the job status
7 💡 The dynamic String copy has no separate ablation AGREE, not changed every measured JSONAllValues statement runs it, and existing tests fail when it is broken

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-48:20260926-044503

@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [45f860d]

Summary: ❌

job_name test_name status info comment
Integration tests (arm_binary, 1/5) FAIL
test_storage_nats/test_nats_jet_stream.py::test_nats_restore_failed_connection_without_losses_on_write FAIL cidb

AI Review

Summary

PR #122387 resolves path presence and Dynamic variant serializations once per block for JSONAllPaths, JSONDynamicPaths, and JSONAllValues, and adds performance coverage. Reviewed head 45f860dfb3c48a0d6cd186c866c05f56f86f8a35; no actionable findings.

Missing context / blind spots
  • ⚠️ Current-head CI is still running. No local ClickHouse binary or build was available, so functional tests and benchmarks were not rerun locally. Completion of the current-head CI checks will provide the remaining runtime validation.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 26, 2026
@groeneai
groeneai requested review from Avogar and CurtizJ September 26, 2026 05:27
@clickhouse-gh clickhouse-gh Bot added the comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... label Sep 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 38 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: f4ee7697-d0ec-46ed-9362-a8cfdc822f3d
  • MIRAI run: 54880bfa-cbec-4f59-a41d-3ae4ebdb33b2
  • PR check IDs:
    • clickbench_1972069_1790443785
    • clickbench_1972075_1790443785
    • clickbench_1972081_1790443785
    • tpch_adapted_1_official_1972092_1790443786
    • tpch_adapted_1_official_1972103_1790443785
    • tpch_adapted_1_official_1972139_1790443786

perf.py expands every {name} in a setup or measured query as a substitution
placeholder, so the JSON object literals in both fill queries made it abort the
test before any query ran ("Failed to substitute parameters (KeyError ...)" in
the Performance Comparison report). Double the braces: perf.py renders them
back to single braces, so the executed SQL is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 45f860dfb with master 929e3b414 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

4 object files changed (+75.81 KiB total), 0 added.

Object file Master PR Δ
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/JSONPaths.cpp.o 672.12 KiB 720.82 KiB +48.70 KiB (+7.25%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/JSONAllValues.cpp.o 434.70 KiB 461.82 KiB +27.11 KiB (+6.24%)

711 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

8 translation units recompiled, 15 s compile time in total, 8 of them have a recent master baseline.

Job report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 9e7bcac

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Performance Comparison (arm_release, master_head, 2/6) / serialization_pool_lookup #0 dashboard confirmed_regression, client_time 0.668 -> 0.822 s (+22.9%), while the shard's own report calls the query success (2 too long only). The dashboard scored it without master history: its C3, C4 and C5 modules are unavailable, so the bar was its 2% floor. The query is GROUP BY over a Dynamic key at 16 threads, which measures SerializationObjectPool lookups and never runs JSONPaths.cpp or JSONAllValues.cpp, the two files this PR changes the question for exactly this class (rows the dashboard scores without master history) is posted to its owner: #119556 (comment). Either the dashboard restores the history, or I send the in-tree fallback described there
Performance Comparison (amd_release, master_head, 2/6) / is_valid_utf8 #0 dashboard confirmed_regression, client_time 14.9 -> 17.4 ms (+16.4%) on a 15 ms query, scored the same way (C3, C4 and C5 unavailable). The shard's own report calls it success, and in the last 14 days it has not flagged this query on either arch. The query is isValidUTF8 over an 8-byte ASCII string the same owner question: #119556 (comment)
Mergeable Check, PR rollups, red only because of the two rows above the same owner question as the two rows above: #119556 (comment)

All 169 check runs completed (148 pass, 19 not selected, 2 red), Finish Workflow is green and
CH Inc sync passed. CIDB has no FAIL or ERROR test row on this head. The shards' too long
counts are informational: they never fail a shard on their own.

Session id: cron:our-pr-ci-monitor:20260926-203259

@groeneai

Copy link
Copy Markdown
Collaborator Author

serialization_pool_lookup #0 (arm_release 2/6) no longer blocks: the dashboard now scores it likely_regression. is_valid_utf8 #0 (amd_release 2/6) is a per-binary layout case instead (the question at #122350 (comment)): the previous head, which differs only in a perf XML, measured -0.8% on an older master base.

groeneai and others added 2 commits November 16, 2026 13:45
…and-serializations-per-block

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master now enables clang-tidy `modernize-use-nodiscard`, which flags this
const member function added by this PR.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@clickhouse-gh clickhouse-gh Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review approves this commit, see the AI Review comment.

The Iceberg configuration of the performance comparison rewrites the
test's MergeTree tables to Iceberg, and a `JSON` column has no Iceberg
equivalent, so all six `Performance Comparison (arm_release, master_head,
iceberg_*)` jobs failed with `Column data of type JSON has no Iceberg
equivalent`. The test measures functions over the `JSON` type, not the
storage, so opt it out with `<test iceberg="0">` rather than the skip
list (changing the skip list would run the whole Iceberg configuration
in this pull request).

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122387&sha=067b87fd8ae813d7fdd4592cb810603d156370e3&name_0=PR

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@clickhouse-gh clickhouse-gh Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review approves this commit, see the AI Review comment.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 45f860d

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Integration tests (arm_binary, 1/5) / test_storage_nats/test_nats_jet_stream.py::test_nats_restore_failed_connection_without_losses_on_write flaky, not from this PR (it changes only JSONAllPaths / JSONAllValues and a perf test): ClickHouse lost some messages: 90850 of 100000 at test_nats_jet_stream.py:1173. The merged reconnect fixes (#112828, #119301) are in the tested tree, so they do not cover this loss a fix task is created (investigating at full effort, fixing-PR link to follow here)
Mergeable Check, PR rollups of the row above a fix task is created (investigating at full effort, fixing-PR link to follow here)

Session id: cron:our-pr-ci-monitor:20261116-200022

@groeneai

Copy link
Copy Markdown
Collaborator Author

Fixing PR for test_nats_restore_failed_connection_without_losses_on_write (ledger above, head 45f860dfb3c4): #119507 (external, open).

In that run the consumer's connection outlived the broker restart as a dead socket: no PONG came back, and the client's ping timer declared it stale 240 s after it had connected. After that, each of the 43 reconnects was torn down by the client within 0.43 ms, with nothing logged by the broker, so the table did not resume before the test gave up. The broker had restored all 100000 messages. The vendored client does not reset its protocol parser on a disconnect when an event loop is attached (nats-io/nats.c#1007), and a parse error leaves the parser state as it is, so a connection cut mid-message fails again on every new socket. The reset is ClickHouse/nats.c#9: master's contrib/nats-io pin b88704df predates it, and 523742ff, which #119507 pins, carries it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... groeneai-origin-issue PR origin: picked up an external GitHub issue pr-performance Pull request with some performance improvements

Projects

None yet

1 participant