Skip to content

Re-land sparse serialization for typed paths of JSON columns, with a fix for the text-format INSERT crash - #133564

Open
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/reland-111429-sparse-json-typed-paths
Open

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/reland-111429-sparse-json-typed-paths

Conversation

@groeneai

@groeneai groeneai commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Performance Improvement

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

Typed paths in JSON columns can now use sparse serialization in MergeTree parts when serialization_info_version = 'with_subcolumns', reducing CPU and memory usage when scanning mostly absent paths.

Description

Re-applies #111429 by @ rorylshanks (reverted in #133486), plus the fix for the crash it caused, #133480.

Closes #133480. Related: #111429, #133486.

Root cause. With with_subcolumns, the table's serialization hints (MergeTreeData::buildSerializationHints) carry a kind per JSON typed path. They exist to feed input-format parsing (IRowInputFormat::setSerializationHints), so a text-format INSERT (file/url/s3 reads; HTTP or inline data with async_insert = 0) created a ColumnSparse inside a typed path, and the JSON text parser (JSONExtractTree), which fills dense columns only, wrote into it: Bad cast from type DB::ColumnSparse to DB::ColumnString in debug builds, a NULL write or heap corruption in release.

Change. Commit 1 is the pure re-land (git revert -m 1 fff0c72d1ffb, same patch-id as #111429). Commit 2 caps the version of the hints at with_missing_columns, so no hint describes a typed path and input formats create dense typed paths. Written parts still choose their kinds from the data, so a part written by such an INSERT still stores a mostly absent typed path Sparse; only parsing straight into sparse typed paths is not done. A top-level Tuple with a JSON element in such a table gets no hint either.

Validation. New test 05857_json_typed_path_sparse_text_input_hints (plain JSON from JSONEachRow and TSV, JSON in a top-level Tuple, a Tuple typed path) aborts on commit 1 and passes 50/50 randomized runs with the fix. An HTTP inline-data INSERT kills the server on commit 1 and succeeds with the fix. The 12 tests of #111429 pass 10/10 each.

The crash was only on master, never in a release.

🤖 Generated with Claude Code


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

groeneai and others added 2 commits November 22, 2026 15:38
…aths declared inside `JSON` columns.""

Re-applies ClickHouse#111429 by @ rorylshanks (merge b6f0aca), reverted in ClickHouse#133486
because a text-format INSERT into a `serialization_info_version = 'with_subcolumns'`
table crashed the server (ClickHouse#133480). This commit is the pure re-land
(`git revert -m 1 fff0c72`); the fix for ClickHouse#133480 follows in the next commit.

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

With `serialization_info_version = 'with_subcolumns'` (ClickHouse#111429) the table's
serialization hints carry a kind per `JSON` typed path. The hints are built only
to be passed to input formats (`IRowInputFormat::setSerializationHints`), so a
text-format INSERT (`file`/`url`/`s3`/object storage reads, HTTP and inline data)
created a `ColumnSparse` inside the typed-path slot of `ColumnObject`, and the
`JSON` text parser (`JSONExtractTree`), which builds its nodes from the type and
inserts into dense column classes at every nesting level, wrote into it: a bad
`assert_cast` abort in debug builds, a NULL write / heap corruption in release
(ClickHouse#133480).

Cap the version of the hints at `with_missing_columns` in
`MergeTreeData::buildSerializationHints`, the hints' single producer, so no hint
ever describes a typed path and input formats always create dense typed paths.
Written parts still choose the typed-path kinds from their data, so a part
written by such an INSERT still stores a mostly-default typed path `Sparse`.
`updateSerializationHintsForPart` already skips a part whose info structure
differs from the hint (now the case for every `JSON` column, and for a `Tuple`
containing one, of such a table), so the incremental updates keep the cap.

The test covers a plain `JSON` column (`JSONEachRow` and `TSV`), `JSON` inside a
top-level `Tuple`, and a typed path of `Tuple` type; each aborts without the fix.

Closes ClickHouse#133480

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

mintlify Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 9c1f064.

Approve preview

@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; 3 findings, all on the re-landed #111429 code; none on the fix commit).

# Sev Finding Verdict Evidence / action
1 ⚠️ The CPU/memory claim has no measurement DISAGREE The first commit has the same patch-id as #111429, whose description carries the JSONBench measurement of this code (74 ms sparse vs 130 ms dense). The fix only changes the input-format hints.
2 ⚠️ 05497 passes if ordinary typed paths stop being Sparse DISAGREE 04614 pins the kinds (Sparse/Default) for Wide and Compact parts and after a merge, so that change fails it; 05497 checks the values of SHARED paths, which is its purpose.
3 💡 Fuzzer lists miss some serialization_info_version values DISAGREE Pre-existing (with_missing_columns was never in the AST fuzzer list; BuzzHouse is untouched); a separate change.

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

Session id: cron:clickhouse-review-slot-9:20261122-161800

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @rorylshanks, could you review this? It re-lands #111429 (sparse typed paths in JSON) and fixes the text-format INSERT crash #133480 by capping the version of MergeTreeData::buildSerializationHints at with_missing_columns, so input formats never create a sparse typed path for the JSON text parser.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The #133480 reproducer and each block of 05857_json_typed_path_sparse_text_input_hints abort 3/3 on the re-land-only commit (clickhouse local --queries-file, debug): Bad cast from type DB::ColumnSparse to DB::ColumnString in StringNode::insertResultToColumn via ObjectJSONNode::traverseAndInsert.
b Root cause explained? With with_subcolumns, MergeTreeData::buildSerializationHints gives every JSON column a per-typed-path kind. Input formats build their columns from these hints, so a ColumnSparse lands in a typed path, and the JSON text parser (JSONExtractTree) writes into it as a dense column.
c Fix matches root cause? Yes. The hints are the only way a typed-path kind reaches an input format, and buildSerializationHints is their single producer. Capping its version at with_missing_columns keeps typed-path kinds out of the hints. Nothing is guarded at the crash site.
d Test intent preserved / new tests added? New test covering 3 shapes (plain JSON from JSONEachRow and TSV, JSON in a top-level Tuple, a Tuple typed path). Each shape first asserts that a typed path is stored Sparse. The tests of #111429 come back with the re-land.
e Both directions demonstrated? Re-land only: every shape aborts 3/3, and an HTTP inline-data INSERT (async_insert=0) kills the server. With the fix: all rc 0 with correct values, and the new test passes 50/50 randomized runs.
f Fix is general across code paths? Every setSerializationHints caller (inline/HTTP insert, file, url, object storage, parallel parsing) gets its hints from the capped producer. Measured with JSONEachRow, TSV, CSV, RowBinary with input_format_binary_read_json_as_string, and HTTP. Native is unaffected.
g Fix generalizes across inputs (params/datatypes/wrappers)? The cap does not depend on the type, because no per-path info reaches the hints at all. Measured with String, Nullable(String) (allow_sparse), UInt64 and Tuple(String, UInt64) typed paths, and with wide and compact parts.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or default changes, and std::min changes nothing for any other serialization_info_version. with_subcolumns exists only on master. Hints of such a table now equal those of a with_missing_columns table.
i Invariants and contracts preserved? Hints live in memory only. updateSerializationHintsForPart applies the same structure check on add and on remove, so the two stay symmetric. Written parts still choose their kinds from the data: a part written by a text-format INSERT still stores its mostly absent typed path Sparse, before and after a merge.

Session id: cron:clickhouse-impl-slot-7:20261122-153500

@clickhouse-gh

clickhouse-gh Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9c1f064]

Summary: ❌

job_name test_name status info comment
Performance Comparison (arm_release, master_head, 3/8) FAIL Performance dashboard
conditional #7 FAIL dashboard
conditional #6 FAIL dashboard

AI Review

Summary

Reintroduces opt-in sparse serialization for declared JSON paths and caps input-format hints so text parsers continue receiving dense typed paths. No actionable findings at PR head 9c1f064bcc50470623dd3ec223a0e3e28db8a6b9.

Consumers traced for the new sparse representation: input formats receive capped hints; scalar functions, aggregation, sorting/merging, and squashing use recursive materialization; subcolumn/sub-object readers create columns matching their serializations; row output derives serialization from the actual columns; NativeWriter materializes typed paths before encoding. Mutation metadata and serialization-cache keys were also checked.

All 12 added/restored SQL tests matched their references using the PR's ARM fast-test binary. Focused probes passed for nested schema evolution, tuple reordering, merges, metadata-version downgrade, sorting, and Native round trips of ordinary, compressed, and constant columns. The linked original PR supplies performance measurements. Server HTTP/TCP paths were inspected but not exercised locally.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements can be tested Allows running workflows for external contributors comp-json-datatype JSON/Dynamic/Object datatype: semi-structured column storage, typed and dynamic paths, sub-object... labels Nov 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 9c1f064bc with master 962432765 (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

1054 object files changed (+308.41 KiB total), 2 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/DataTypes/Serializations/SerializationInfoNamed.cpp.o new 153.13 KiB +153.13 KiB
src/CMakeFiles/dbms.dir/DataTypes/Serializations/SerializationInfoTuple.cpp.o 156.77 KiB 55.50 KiB -101.27 KiB (-64.60%)
src/CMakeFiles/dbms.dir/DataTypes/Serializations/SerializationInfoObject.cpp.o new 82.80 KiB +82.80 KiB
src/CMakeFiles/dbms.dir/DataTypes/DataTypeObject.cpp.o 668.00 KiB 693.55 KiB +25.56 KiB (+3.83%)
src/CMakeFiles/dbms.dir/Columns/ColumnSparse.cpp.o 368.36 KiB 386.86 KiB +18.50 KiB (+5.02%)

716 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

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

Job report

@stresshouse

stresshouse Bot commented Nov 22, 2026

Copy link
Copy Markdown

📊 Cloud Performance Report

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

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

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: a3ce052f-c64a-448f-a184-f4fa535dbc80
  • MIRAI run: 8f57bd9d-03ce-46b1-89a9-81ef269fb4c7
  • PR check IDs:
    • clickbench_829686_1795370987
    • clickbench_829692_1795370986
    • tpch_adapted_1_official_829706_1795370986
    • tpch_adapted_1_official_829737_1795370986

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 9c1f064

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, 3/8) / conditional #6 arm per-binary slow mode, not this diff: if(rand() % 2, [toDateTime(rand())], [toDate(rand())]) 0.153 -> 0.202 s (+31.6%, +28.3% after the restart). This PR does not change if/multiIf or the array copy path. Master builds land in the same slow mode too (master e11b4f4de4 207 ms on 2026-11-16, 3 master builds in 30 days); 60 other PR builds in the last 24 h ran this query at 150-162 ms a fix task is created (investigating at full effort, fixing-PR link to follow here)
Performance Comparison (arm_release, master_head, 3/8) / conditional #7 same as #6 for the multiIf form: 0.153 -> 0.202 s (+31.7%, +29.0% after the restart) a fix task is created (investigating at full effort, fixing-PR link to follow here)
Mergeable Check, PR praktika rollups of the rows above a fix task is created (the rows above)

CIDB has no other ownable FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261122-203024

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-json-datatype JSON/Dynamic/Object datatype: semi-structured column storage, typed and dynamic paths, sub-object... pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sparse typed-path hints reach JSON text parser: text-format INSERT segfaults

1 participant