Repository navigation
Fix NULL propagation when extracting Nullable, Dynamic, Variant and LowCardinality(Nullable(...)) subcolumns from Nullable(Tuple(...)) columns - #102942
Conversation
|
Workflow [PR], commit [e8a3e28] Summary: ✅
AI ReviewSummaryThis PR fixes Final VerdictStatus: ✅ Approve |
NULL propagation when extracting Nullable subcolumns from Nullable(Tuple(...)) columnsNULL propagation when extracting Nullable subcolumns from Nullable() columns (e.g. Nullable(JSON), Nullable(Tuple))
NULL propagation when extracting Nullable subcolumns from Nullable() columns (e.g. Nullable(JSON), Nullable(Tuple))NULL propagation when extracting Nullable subcolumns from Nullable(Tuple(...)) columns
| size_t new_rows = column->size() - prev_size; | ||
| if (new_rows == 0) | ||
| return; |
There was a problem hiding this comment.
There are two problems here.
1) column->size() - prev_size is not a reliable way to get the number of rows read in this range. The nested column could already be cached (because we read it during deserialization of another subcolumn). When insert_only_rows_in_current_range_from_substreams_cache is false (the default), insertDataFromCachedColumn may set result_column = cached_column, meaning column ends up pointing to the entire cached column that already contains rows from this range. In that case new_rows will be wrong (or zero), and we won't apply the parent null map.
2) We call column->assumeMutable() and modify the null map in place, but this column might be from cache and shared with another subcolumn, so we would corrupt data visible to other readers.
Example showing problem 1:
CREATE TABLE test (x Tuple(a Nullable(Tuple(b Nullable(UInt32))))) ENGINE = MergeTree SETTINGS index_granularity = 1;
INSERT INTO test SELECT number % 2 ? tuple(NULL) : tuple(tuple(number)) FROM numbers(4);
SELECT x.a, x.a.b FROM test;┌─x.a──────┬─x.a.b─┐
│ (0) │ 0 │
│ ᴺᵁᴸᴸ │ 1 │ -- x.a.b should be NULL here
│ (2) │ 2 │
│ ᴺᵁᴸᴸ │ 3 │ -- x.a.b should be NULL here
└──────────┴───────┘
The parent null map was not applied to x.a.b.
To fix both issues, let's set insert_only_rows_in_current_range_from_substreams_cache = true before calling the nested deserialization. This forces insertDataFromCachedColumn to copy only the current range's rows via insertRangeFrom instead of sharing the column pointer, so new_rows is reliable and the column has exclusive ownership, making the subsequent mutation safe. This is slightly less performant (an extra copy), but correct. The same approach is used in SerializationVariantElement and SerializationSparse for the same reason.
Note: since settings is passed by reference, the flag should be set on a local copy to avoid leaking it to the caller.
There was a problem hiding this comment.
Also, let's keep the order of serialization for consistency - first read the nullmap, then the nested column and then apply the nullmap. It's also good for reading subcolumns in Compact parts so we don't seek backwards in the file.
Avogar
left a comment
There was a problem hiding this comment.
We also need to handle special case when nested subcolumn type is not Nullable but Dynamic/Variant that can also have nulls:
select if(number % 2, NULL, tuple(number::Dynamic)::Tuple(a Dynamic)) as t, t.a from numbers(4) settings short_circuit_function_evaluation='disable';
┌─t────────┬─t.a─┐
1. │ { ↴│ 0 │
│↳ "a": 0↴│ │
│↳} │ │
2. │ ᴺᵁᴸᴸ │ 1 │
3. │ { ↴│ 2 │
│↳ "a": 2↴│ │
│↳} │ │
4. │ ᴺᵁᴸᴸ │ 3 │
└──────────┴─────┘
select if(number % 2, NULL, tuple(number::Dynamic)::Tuple(a Variant(UInt64))) as t, t.a from numbers(4) settings short_circuit_function_evaluation='disable';
┌─t────────┬─t.a─┐
1. │ { ↴│ 0 │
│↳ "a": 0↴│ │
│↳} │ │
2. │ ᴺᵁᴸᴸ │ 1 │
3. │ { ↴│ 2 │
│↳ "a": 2↴│ │
│↳} │ │
4. │ ᴺᵁᴸᴸ │ 3 │
└──────────┴─────┘
We need special branches for this when we combine null maps
NULL propagation when extracting Nullable subcolumns from Nullable(Tuple(...)) columnsNULL propagation when extracting Nullable, Dynamic, Variant and LowCardinality subcolumns from Nullable(Tuple(...)) columns
|
📊 Cloud Performance Report 🟢 AI verdict: This PR is a correctness fix for reading subcolumns extracted from Nullable(Tuple(...)) columns: it adds a new read-path serialization that applies the parent null map to nested Nullable/LowCardinality/Variant/Dynamic subcolumns. Neither ClickBench nor the TPC-H schema uses Nullable(Tuple) columns, so none of these queries exercise the changed code. Q22 (clickbench) and Q8 (tpch Q8) were downgraded as off-path — Q22's source runs were very noisy and Q8 is a trending join query the diff can't touch. The three large improvements that remain flagged (clickbench Q23 -32%, tpch Q7 -34%, tpch Q20 -48%) are statistically clear and held by the high-magnitude trust gate, but since the changed code is off these queries' execution path, treat them as likely build/environment variance rather than a real speedup from this change. clickbench🟢 1 improved · Flagged queries (2 of 43)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 2 improved · Flagged queries (3 of 22)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. Debug info
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 119/147 (80.95%) | Lost baseline coverage (was covered on master, now uncovered in this PR): 2 line(s) · Uncovered code |
Issue ClickHouse#105356 was fixed by ClickHouse#102942, which adds read-path tests for extracting Nullable, Dynamic, Variant and LowCardinality(Nullable(...)) subcolumns from Nullable(Tuple(...)) columns. None of those tests exercise a horizontal merge, which is the path that originally aborted the server with 'Sort order of blocks violated'. A row stored as outer-tuple = NULL can still carry a non-NULL inner element on disk. When that element is a sort-key subcolumn, the merge must re-apply the outer null map; otherwise the reconstructed key disagrees with the on-disk order and the next OPTIMIZE FINAL aborts in CheckSortedTransform. This test covers the two carriers ClickHouse#102942 fixes (Nullable(Decimal) and LowCardinality(Nullable(String))) via SummingMergeTree + OPTIMIZE FINAL. Deterministic literals (no generateRandom) so it does not depend on architecture or randomized settings. Verified: aborts on the pre-ClickHouse#102942 binary, passes on current master. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Issue ClickHouse#105356 was fixed by ClickHouse#102942, which adds read-path tests for extracting Nullable, Dynamic, Variant and LowCardinality(Nullable(...)) subcolumns from Nullable(Tuple(...)) columns. None of those tests exercise a horizontal merge, which is the path that originally aborted the server with 'Sort order of blocks violated'. A row stored as outer-tuple = NULL can still carry a non-NULL inner element on disk. When that element is a sort-key subcolumn, the merge must re-apply the outer null map; otherwise the reconstructed key disagrees with the on-disk order and the next OPTIMIZE FINAL aborts in CheckSortedTransform. This test covers the two carriers ClickHouse#102942 fixes (Nullable(Decimal) and LowCardinality(Nullable(String))) via SummingMergeTree + OPTIMIZE FINAL. Deterministic literals (no generateRandom) so it does not depend on architecture or randomized settings. Verified: aborts on the pre-ClickHouse#102942 binary, passes on current master. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Given,
We get:
But should be:
Additionally:
We get:
But should be:
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix
NULLpropagation when reading subcolumns extracted fromNullable(Tuple(...))columns. For example, fortup Nullable(Tuple(s Nullable(String))),SELECT tup.snow correctly returnsNULLin rows where the outer tuple isNULLinstead of garbage values. This covers all element types that can representNULL:Nullable,Dynamic,VariantandLowCardinality(Nullable(...)). Closes #105356.Documentation entry for user-facing changes
Version info
26.6.1.917