Skip to content

Fix exception in CoalescingMergeTree with const columns from skip index expressions - #96880

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-coalescing-merge-tree-const-column-from-index
Feb 14, 2026
Merged

alexey-milovidov merged 3 commits into
masterfrom
fix-coalescing-merge-tree-const-column-from-index

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 14, 2026 •

Copy link
Copy Markdown
Member

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 logical error exception in CoalescingMergeTree when a skip index expression produces a constant column (e.g., bloom_filter on ifNotFinite(1, c0) for an integer column).

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

No documentation changes needed.


Summary

When a skip index expression produces a constant result (e.g., ifNotFinite(1, c0) on an integer column always returns 1), executing the combined sorting key + skip indices expression adds a ColumnConst column to the block. In SummingSortedAlgorithm::defineColumns, the isColumnConst check was inside the if (!aggregate_all_columns) branch, so it only applied to SummingMergeTree. For CoalescingMergeTree (which uses aggregate_all_columns = true), the ColumnConst column was incorrectly added to columns_to_aggregate, causing a logical error: Bad cast from type DB::ColumnConst to DB::ColumnVector<int>.

The fix moves the isColumnConst check before the aggregate_all_columns branching so it applies unconditionally to all merge tree engine variants.

Closes #91958

🤖 Generated with Claude Code

Version info

  • Merged into: 26.2.1.681

…ndex expressions

When a skip index expression produces a constant result (e.g.,
`ifNotFinite(1, c0)` on an integer column), executing the combined
sorting key + skip indices expression adds a `ColumnConst` column to
the block. In `SummingSortedAlgorithm::defineColumns`, the
`isColumnConst` check was inside the `if (!aggregate_all_columns)`
branch, so it only applied to `SummingMergeTree`. For
`CoalescingMergeTree` (which uses `aggregate_all_columns = true`), the
`ColumnConst` column was incorrectly added to `columns_to_aggregate`.
When `finishGroup` called `insertResultInto`, it tried to `assert_cast`
the `ColumnConst` to `ColumnVector<int>`, causing the logical error.

The fix moves the `isColumnConst` check before the
`aggregate_all_columns` branching so it applies unconditionally.

Closes #91958

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

clickhouse-gh Bot commented Feb 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e4fa666]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Feb 14, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Looks very reasonable.

@alexey-milovidov
alexey-milovidov merged commit f93fe42 into master Feb 14, 2026
131 checks passed
@alexey-milovidov alexey-milovidov self-assigned this Feb 14, 2026
@alexey-milovidov
alexey-milovidov deleted the fix-coalescing-merge-tree-const-column-from-index branch February 14, 2026 23:50
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 15, 2026
huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request Oct 11, 2026
With optimize_on_insert, writeTempPartImpl evaluated the combined sorting
key and skip index expression on the inserted block and then merged the
whole block with the engine's algorithm. The merge treated the skip index
input columns as data columns: SummingMergeTree summed them (length(name)
5+5+5 = 15), grouped two mapKeys(statsMap.a|b) inputs into a fake nested
map, CoalescingMergeTree kept the last non-NULL a + b, AggregatingMergeTree
applied the SimpleAggregateFunction and GraphiteMergeTree rolled them up.
The index was then built from values that are not in the stored rows, so
queries using it skipped granules with matching rows. For SummingMergeTree
a summed index input also counted in the "all summed columns are zero"
rule, so rows that must be deleted were kept.

Background merges are not affected because MergeTask evaluates the skip
index expressions after merging. Do the same on insert: when the block is
merged and skip indexes are materialized, evaluate only the sorting key
before the merge, then project the merged block back to the inserted
columns and evaluate the combined expression on it. Reusing the combined
builder keeps one source for index inputs and avoids duplicate columns
when an index expression equals a sorting key expression. All other paths
run the old code.

ClickHouse#96880 hit the same root cause with a constant index input in
CoalescingMergeTree and guarded it in SummingSortedAlgorithm; that guard
stays because FINAL and PREWHERE still use it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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 pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logical error: 'Bad cast from type DB::ColumnConst to DB::ColumnVector<int>' with CoalescingMergeTree and bloom_filter index

2 participants