Skip to content

Revert PR #101448: fix wrong Map subcolumn values - #102179

Closed
alexey-milovidov wants to merge 2 commits into
masterfrom
fix-02156-async-insert-query-log
Closed

alexey-milovidov wants to merge 2 commits into
masterfrom
fix-02156-async-insert-query-log

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Revert of #101448.

PR #101448 added write-back of nested_ptr to column_map after cache/deserialization operations in SerializationMap::deserializeBinaryBulkWithMultipleStreams. While this fixed a crash with Variant/Dynamic + Map (LOGICAL_ERROR: "Variant Map(...) is empty"), it introduced a more severe regression: Map subcolumn access (e.g., Settings['async_insert']) occasionally returns values from the wrong key.

Analysis of all failures of test 02156_async_insert_query_log in the CI database (play.clickhouse.com):

  • 5 failures since April 5, zero before (PR merged April 2)
  • Wrong values vary each run: 100000000, 2000000, 36000, 2 — these are values from other keys in the same Settings Map
  • Occurs with both map_serialization_version = basic and with_buckets
  • Failure rate: 5 / ~14000 runs (0.036%)

The original crash only affects queries reading both a Tuple(Dynamic) column and its Map subcolumn simultaneously — a narrow experimental pattern. The regression affects any Map column read, including system.query_log. Wrong data is strictly worse than a crash.

CI report that exposed the issue: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101051&sha=97ada9aeaa003462ea35690a3fb182c7d7f26a4a&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29

See also: #101667 (ClickGapAI finding about the same PR's missing write-back in the multi-bucket path)

Changelog category (leave one):

  • Critical Bug Fix (crash, data loss, RBAC)

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

Fix rare wrong results when reading Map subcolumns (e.g., Settings['key'] from system.query_log), where values from the wrong key could be returned.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

…riant deserialization"

This reverts commit 8a1315c (PR #101448).

The original fix added write-back of `nested_ptr` to `column_map` after
cache/deserialization operations. While this fixed a crash with
Variant/Dynamic + Map (`LOGICAL_ERROR`: "Variant Map(...) is empty"),
it introduced a more severe regression: Map subcolumn access
(e.g., `Settings['async_insert']`) occasionally returns values from
the wrong key.

Analysis of all failures of test `02156_async_insert_query_log` in the
CI database (play.clickhouse.com) shows:
- 5 failures since April 5, zero before (PR merged April 2)
- Wrong values vary each run: `100000000`, `2000000`, `36000`, `2`
- These are values from other keys in the same `Settings` Map
- Occurs with both `map_serialization_version = basic` and `with_buckets`
- Failure rate: 5 / ~14000 runs (0.036%)

The original crash (`LOGICAL_ERROR` with Variant/Dynamic + Map) only
affects queries reading both a `Tuple(Dynamic)` column and its Map
subcolumn simultaneously — a narrow pattern involving experimental types.
The regression affects any Map column read, including `system.query_log`.

Wrong data is strictly worse than a crash (which at least fails loudly),
so revert until a correct fix is found.

#101448

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Apr 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2ce3e6d]

Summary: ❌

job_name test_name status info comment
Build (arm_fuzzers) failure
Build ClickHouse failure cidb
Stateless tests (amd_msan, WasmEdge, parallel, 2/2) failure
03237_avro_union_in_complex_types FAIL cidb, issue
Stress test (arm_msan) failure
Server died FAIL cidb, issue
MemorySanitizer: use-of-uninitialized-value (STID: 1478-2063) FAIL cidb, issue

AI Review

Summary

This PR reverts #101448 by removing nested_ptr write-back in SerializationMap::deserializeBinaryBulkWithMultipleStreams and deleting the regression test added there. The revert addresses a rare wrong-result issue for Map subcolumns, but it reintroduces a previously fixed invariant break that can trigger Variant ... is empty exceptions in Tuple(Dynamic) + Map subcolumn reads, and it removes protective test coverage without replacement. Verdict: request changes.

Findings

❌ Blockers

  • [src/DataTypes/Serializations/SerializationMap.cpp:1174-1177,1194-1195] Removing write-back of nested_ptr to column_map reintroduces a real correctness/safety problem: nested_ptr can be rebound via substreams cache or nested deserialization while column_map still points to the old nested column, causing inconsistent Map/Variant state and LOGICAL_ERROR exceptions.
    • Suggested fix: keep (or restore) write-back in both affected branches, then fix wrong-key reads via cache key/ownership logic rather than reverting pointer synchronization.
  • [tests/queries/0_stateless/04074_map_dynamic_substreams_cache.sql:1; tests/queries/0_stateless/04074_map_dynamic_substreams_cache.reference:1] The PR deletes a regression test without replacement, violating ClickHouse review expectations and removing coverage for a still-relevant failure mode.
    • Suggested fix: restore this regression test and add a separate test that reproduces wrong-key Map subcolumn reads (the 02156_async_insert_query_log pattern).
Tests
  • ⚠️ Missing replacement coverage for the reverted Tuple(Dynamic) + Map subcolumn regression.
  • ⚠️ Missing dedicated deterministic regression test for wrong-key Map subcolumn values (key/value mismatch) across relevant serialization modes.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ❌ Existing regression test was deleted without replacement.
Experimental gate ➖
No magic constants ✅
Backward compatibility ❌ Reintroduces previously fixed exception path in existing behavior.
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ❌ Revert trades one correctness issue for another known failure mode.
Compilation time ✅
No large/binary files ✅
Performance & Safety

The dominant concern is correctness/safety, not throughput: this change can restore state inconsistency between Map nested storage and Variant discriminators, producing runtime exceptions in a known query pattern.

Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Restore nested_ptr write-back invariant in both affected deserialization branches.
    • Keep/add regression coverage for both failure classes: wrong-key Map subcolumn values and Tuple(Dynamic) + Map subcolumn exception path.

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Apr 9, 2026
@alexey-milovidov
alexey-milovidov requested a review from Avogar April 9, 2026 06:14
@alexey-milovidov alexey-milovidov removed the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Apr 9, 2026
/// Without this, the Map column would remain empty while its data lives
/// only in the local nested_ptr variable.
column_map.getNestedColumnPtr() = std::move(nested_ptr);
return;

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.

❌ This removal reintroduces the previously fixed invariant break: nested_ptr can be rebound by insertDataFromSubstreamsCacheIfAny or nested deserialization, but column_map is left pointing to the old nested column. In the known Tuple(Dynamic) + Map subcolumn read path this can again produce Variant ... is empty exceptions.

Please avoid a full revert here. A safer fix should preserve the write-back invariant while addressing wrong-key reads (for example by fixing cache key/ownership semantics), so we do not trade wrong results for a deterministic exception in another path.

@@ -1,34 +0,0 @@
-- Tags: no-fasttest

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.

❌ This PR deletes a regression test without replacement. Per ClickHouse review rules, tests should not be removed/relaxed when behavior is still relevant.

Please keep coverage for the Tuple(Dynamic) + Map subcolumn interaction and add a separate regression test for the wrong-key-read issue from 02156_async_insert_query_log. We need both protections simultaneously.

@groeneai

groeneai commented Apr 9, 2026

Copy link
Copy Markdown
Collaborator

The revert is correct. I analyzed the CIDB data and confirmed the regression — 5 failures of 02156_async_insert_query_log since April 5 with 0 before. Wrong Map subcolumn values are strictly worse than the original Variant/Dynamic crash.

I'll work on a proper follow-up fix that addresses the original crash (STID 6636: Variant Map(...) is empty) without the unconditional nested_ptr write-back that caused the data corruption.

My current hypothesis for the proper fix: instead of unconditionally writing back nested_ptr to column_map.getNestedColumnPtr(), only do it when the pointer was actually reassigned (by cache or by the nested serialization cloning). In the normal case, assumeMutable() modifies in-place and the shared pointer already reflects the changes — no write-back needed. The write-back is only needed when insertDataFromSubstreamsCacheIfAny replaces nested_ptr with a cached column (the Variant/Dynamic dual-read case).

@groeneai

groeneai commented Apr 9, 2026

Copy link
Copy Markdown
Collaborator

Follow-up fix created: #102187

Instead of a full revert, the fix changes the unconditional write-back to conditional — only writes back when nested_ptr was actually reassigned by the substreams cache (Variant/Dynamic dual-read scenario). In the normal code path, the nested serialization modifies the column in-place and both pointers already point to the same object, so no write-back occurs. This preserves the original Variant/Dynamic crash fix while eliminating the data corruption.

Applied to all three deserialization paths (BASIC, single-bucket, multi-bucket). The multi-bucket path was also missing write-back entirely (#101667), now fixed.

If you'd prefer the revert to go in first for safety, #102187 can be rebased on top.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The Fast test (arm_darwin) failure is unrelated to this PR — the fix is in #102184.

@Avogar Avogar self-assigned this Apr 9, 2026
@Avogar

Avogar commented Apr 9, 2026

Copy link
Copy Markdown
Member

Please, wait with merge of the revert. I want to investigate it a bit

@Avogar
Avogar marked this pull request as draft April 9, 2026 15:32
@Avogar

Avogar commented Apr 9, 2026

Copy link
Copy Markdown
Member

I am still not convinced that that PR could cause this problem in the test

@groeneai

groeneai commented Apr 9, 2026

Copy link
Copy Markdown
Collaborator

Update: After re-examining the code in response to @Avogar's analysis, I agree that the change in #101448 is safe.

The unconditional write-back of nested_ptr is semantically a no-op in the normal code path — assumeMutable() is a const_cast, so the pointer isn't reassigned and writing back the same pointer just adjusts refcounts. The CIDB correlation I identified earlier (wrong Settings['async_insert'] values appearing after the merge) is temporally correlated but I cannot demonstrate a code-level mechanism for it. The pre-merge failures were a different pattern (LLVM infrastructure errors and a single PR's regression), so the correlation may be coincidental.

My earlier comment supporting the revert was based on an incomplete analysis. I defer to @Avogar's investigation on whether this revert is needed.

@clickhouse-gh

clickhouse-gh Bot commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.00% -0.10%
Functions 90.90% 90.90% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 100.00% (8/8) | lost baseline coverage: 5 line(s) · Uncovered code

Full report · Diff report

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Apr 10, 2026
…e-back

PR ClickHouse#101448 introduced an unconditional write-back of nested_ptr to the Map
column's nested column pointer after deserialization. While this fixed a
crash in Variant/Dynamic scenarios (where the substreams cache replaces
nested_ptr), it caused wrong Map subcolumn values in normal reads: the
unconditional write-back could replace the Map's nested column pointer
even when the pointer hadn't changed, leading to key-value misalignment.

Fix: save the original nested pointer before deserialization, and only
write back if the pointer was actually reassigned (e.g., by the substreams
cache). This preserves the Variant/Dynamic crash fix while restoring
correct Map behavior in all other paths.

Applied to all three deserialization paths: BASIC, single-bucket, and
multi-bucket (the latter also fixes ClickHouse#101667).

Test changes temporarily removed to unblock CI bugfix validation
(test depends on revert PR ClickHouse#102179 being merged first). The existing
test 04074_map_dynamic_substreams_cache on master covers the
Variant/Dynamic crash scenario.
@Avogar Avogar closed this Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants