Repository navigation
Fix a crash reading a table with lightweight updates when a sorting key column has a DEFAULT - #119540
Conversation
A SELECT on a table with active lightweight updates crashed with a SIGSEGV when the sorting key contained a column that is missing in the data part and has a DEFAULT or MATERIALIZED expression. Such a column is deliberately a null ColumnPtr at that stage: fillMissingColumns leaves it null so that evaluateMissingDefaults can compute it later. executeSortingKeyExpressions materializes the sort-key columns of MergeOnKey patches, but it runs from readPatches for the first reader only, while injectPatchColumns adds the patch key columns to the first read step without removing them from the later ones. On a read step after PREWHERE the column is therefore requested again and left null, addPatchVirtuals declines to replace it because its guard tests whether the name is present rather than whether the element is materialized, and getBlockWithSortingKey then called convertToFullColumnIfConst through the null pointer. The correct, already normalized and row-aligned comparison keys are cached in ReadResult::columns_for_patches, and both applyPatches call sites already pass that block down as additional_columns. Thread it into getBlockWithSortingKey as a dedicated key_columns block and take every sorting-key column from it, so the per-step path and the after-reader path are both covered. getByName is used without a fallback: a silent fallback to the result block would restore exactly the behaviour this removes for any future path where the cache is not populated. No block is mutated, so output values are unchanged on every path; only which column the key comparison reads changes. That cached key is also the correct identity rather than merely a non-null one. A patch part's key columns are written by the patch mutation as plain READ_COLUMN commands with recalculate_dependencies_of_updated_columns = false, so they hold the values from the source snapshot, before that same update's SET was applied; the read side's cached key is likewise evaluated before any patch is applied. Reading the key back from the result block after evaluateMissingDefaults could instead compare against a default computed from already patched inputs. Related: 72d4b5c Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 gate findings plus 2 from my own
Severity: ❌ blocker / Noted, not blocking: Session id: cron:clickhouse-review-slot-8:20260911-193400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20260911-180400 |
|
Workflow [PR], commit [769e4d5] Summary: ✅
AI ReviewSummaryThis PR switches Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 769e4d5.
Changed lines: Changed C/C++ lines covered: 22/22 (100.00%) · Uncovered code |
|
cc @Michicosun @Algunenano, could you review this? A sorting-key column that is missing in the part and has a |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units133 translation units recompiled, 1411 s compile time in total, 133 of them have a recent master baseline. |
|
Backporting to 26.8 to fix SEGV |
…t updates when a sorting key column has a DEFAULT
Backport #119540 to 26.8: Fix a crash reading a table with lightweight updates when a sorting key column has a DEFAULT
|
@groeneai @tuanpach — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
|
Verified the finding, and it is not caused by this PR. I built current master with and without this PR's source change reverted (build ids The defect itself does reproduce on master and is a silent wrong result, so I am picking it up. Evidence and fix direction are in #120401. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a crash when reading a table with active lightweight updates whose sorting key contains a column that is missing in the data part and has a
DEFAULTexpression.Description
A
SELECTon a table with active lightweight updates crashes with aSIGSEGVwhen the sorting key contains a column that is missing in the data part and has aDEFAULT(orMATERIALIZED) expression.Such a column is deliberately a null placeholder until the missing-defaults pass fills it.
executeSortingKeyExpressionsmaterializes the patch key columns for the first reader only, whileinjectPatchColumnsleaves them requested on later steps as well. AfterPREWHEREthe column is null again,addPatchVirtualskeeps the placeholder because its guard tests presence rather than materialization, andgetBlockWithSortingKeydereferenced it unconditionally.The correct, normalized keys are already cached row-aligned in
ReadResult::columns_for_patches, which the caller already holds. It is now threaded intogetBlockWithSortingKeyas a dedicatedkey_columnsblock, and every sorting-key column is taken from it. No block is mutated, so output values are unchanged; only which column the key comparison reads changes. That cached key is also the right identity: the patch part's key is computed from the source snapshot before the update'sSETis applied, and so is the cached one.Reproduced deterministically on master and verified in both directions with a mutation check, expression sort keys included. No result changes across 112 lightweight-update and patch-part tests; 100/100 runs of the extended test.
One narrow shape is left alone. When a sorting-key column's
DEFAULTreads a column that the same update writes and the first read step does not read, the cached key cannot match the patch part's key, so the patch silently does not apply where the query previously aborted. That lightweight-vs-heavy disagreement is pre-existing on master and is the class #118630 addresses.Measured base vs fix for that shape
Sorting-key column
b UInt64 DEFAULT vnum + 1000, where the same lightweightUPDATEalso writesvnumand a second columnwthat is missing from the base part. Twin table mutated with the equivalentALTER TABLE ... UPDATE.Affected releases: 26.8.x.
Related: the first-reader half of this fix, commit
72d4b5c97517f1f10334a8d9bcd54a5fa59813b4.CI report for the sighting: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=117817&sha=80365e1c3236fc29de7cfe28baa5138fbd8f7a17&name_0=PR
Workflow [PR]
Sync PR [sync-upstream/pr/119540]
Version info
26.9.1.1440(included in26.9and later)26.8.4.10