Skip to content

Fix null-pointer dereference in canSkipConversionToNullable during mutation - #106654

Closed
alexey-milovidov wants to merge 2 commits into
masterfrom
fix/mutatetask-null-statistics-deref
Closed

alexey-milovidov wants to merge 2 commits into
masterfrom
fix/mutatetask-null-statistics-deref

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 7, 2026 •

Copy link
Copy Markdown
Member

canSkipConversionToNullable in MutateTask.cpp dereferenced the result of metadata_snapshot->getColumns().tryGet(command.column_name) without checking for nullptr. tryGet returns nullptr when the column name is not present in the metadata snapshot, which segfaulted (SIGSEGV, access at offset 0x90) inside ColumnStatisticsDescription::empty during MutateTask::prepare for a mutation.

Added the missing null check, conservatively refusing to skip the conversion when the column cannot be found — mirroring the existing null check at the other tryGet(command.column_name) site a few hundred lines above in the same file.

getColumns().tryGet is a top-level-only lookup (no subcolumns, see ColumnsDescription::tryGet), while part->tryGetColumn(command.column_name) resolves subcolumns and dynamic subcolumns. So a READ_COLUMN command can legitimately reach this site with a column_name that is present in the part (e.g. a subcolumn, or a column that has left the latest metadata snapshot while a part at an earlier mutation version still carries it) but absent from metadata_snapshot. A deterministic single-session reproduction is impractical — DROP/RENAME of a column with an unfinished mutation is explicitly rejected, so this only surfaced under the random-ALTER stress workload.

Found by a stress test (Stress test (arm_msan)) on #101039 — CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101039&sha=243b4e230a2da217eab231b78ecc3ed985ffbcbe&name_0=PR&name_1=Stress%20test%20%28arm_msan%29 (the crash is unrelated to that PR's changes — MutateTask.cpp is not touched there). Stack: ColumnStatisticsDescription::empty → canSkipConversionToNullable → canSkipMutationCommandForPart → MutateTask::prepare.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix a possible crash (null-pointer dereference) during a mutation when skipping the conversion of a column to Nullable.

Documentation entry for user-facing changes

  • Documentation is not required for this change

`canSkipConversionToNullable` dereferenced the result of
`metadata_snapshot->getColumns().tryGet(command.column_name)` without a null
check:

    const auto * column_desc = metadata_snapshot->getColumns().tryGet(command.column_name);
    if (!column_desc->statistics.empty())
        return false;

`tryGet` returns `nullptr` when the column name is not present in the
metadata snapshot, so this segfaulted (`SIGSEGV`, access at offset `0x90`)
in `MutateTask::prepare` during a mutation. Add the missing null check,
conservatively refusing to skip the conversion when the column cannot be
found — matching the existing null-check at the other
`tryGet(command.column_name)` site in `MutateTask.cpp`.

Found by a stress test (`Stress test (arm_msan)`) on
#101039 :
the crash is in `ColumnStatisticsDescription::empty` ->
`canSkipConversionToNullable` -> `canSkipMutationCommandForPart` ->
`MutateTask::prepare`, unrelated to that PR's changes.

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

clickhouse-gh Bot commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [be9e06c]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_asan_ubsan, distributed plan, parallel, 1/2) FAIL
04052_local_cancel_scalar_subquery FAIL cidb

AI Review

Summary

This PR adds a defensive null check in canSkipConversionToNullable, so mutation preparation no longer dereferences a missing ColumnDescription when metadata_snapshot->getColumns().tryGet(command.column_name) returns nullptr. The change is conservative: it only disables the metadata-only skip optimization and falls back to the normal mutation path. I did not find any unresolved correctness issues in the changed code.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 7, 2026
Comment thread src/Storages/MergeTree/MutateTask.cpp Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, fix Logical error: Virtual row boundary violated and send a PR with the fix.

@groeneai

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

Picked this up. Sent the fix in #106664 — gates the new MergingSortedAlgorithm virtual-row boundary debug assertions (added in #106565) on apply_virtual_row_conversions = true. The assertions were firing on the inner pk_block path of read-in-order over a Union of subtrees, where each subtree has its own per-source conversion that the outer merge cannot apply, so the boundary recorded in rememberVirtualRowBoundary was a default-zero column and checkVirtualRowBoundary always tripped on the first real row. Includes a deterministic regression test (04323_virtual_row_two_shards_pk_alias.sql) that aborts on master and passes with the fix.

@PedroTadim

Copy link
Copy Markdown
Member

This PR seems to be a duplicate of #105917

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jun 7, 2026
- src/.../MergingSortedAlgorithm.cpp: collapse the 6-line gate explanation
  next to `if (apply_virtual_row_conversions)` into a one-line invariant
  note. The full root-cause explanation (Union of read-in-order subtrees,
  per-source conversions, default-zero fallback in setVirtualRow) lives in
  the PR description and the original commit message of 28c0151.
- tests/.../04323_virtual_row_two_shards_pk_alias.sql: collapse the
  11-line header comment into a one-line "Regression test for STID
  2651-3359 (debug assertion added in ClickHouse#106565)" - test name and the SET
  list make the purpose self-evident, full context lives in ClickHouse#106664.

Also corrects the outdated test-file PR reference noted by clickhouse-gh
inline (ClickHouse#106565 introduced the assertion; the test reproduces the
assertion path covered by this PR, not ClickHouse#106654 which is unrelated).

No code-behavior changes.

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

clickhouse-gh Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.60% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 5/5 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@PedroTadim

Copy link
Copy Markdown
Member

#105917 is going to be merged. Closing this PR as a duplicate

@PedroTadim PedroTadim closed this Jun 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants