Repository navigation
Fix Unexpected return type in PREWHERE when a Merge table reads another Merge table with different column types - #124037
Conversation
…nother Merge table
`StorageMerge` wraps every child output in `materialize(<col>)` (`addMissingDefaults`), so
when a `Merge` table reads another `Merge` table, the filter pushed down from the outer
`WHERE` holds two different nodes with the same name: the inner `materialize(b)` over the
read column and the outer one over `_CAST(inner, <outer type>)`.
With `optimize_prewhere_after_pushdown = 1` and `move_all_conditions_to_prewhere = 0`, the
second `optimizePrewhere` pass merges the new condition into the existing PREWHERE with
`ActionsDAG::mergeNodes`. It registered every moved node of `second` in a name map and
rebound children by name, so the outer node collapsed into the inner one and `equals` ran
on the inner column with a function resolved for the outer (Nullable) type:
`Unexpected return type from equals. Expected Nullable(UInt8). Got UInt8`. The outputs
were mapped by name with the same defect.
`mergeNodes` now records, for every processed node of `second`, the node it ends up as, and
rebinds children and `out_outputs` through that map by pointer. A node of `second` is
matched by name only against the existing nodes of the destination DAG, and an input also
against the inputs of `second` already moved, so duplicate inputs still collapse while a
computed node never collapses into another node of `second`, including an input it shadows
(`CAST(x, ...) AS x`).
Found by BuzzHouse (amd_debug) on `merge(REGEXP('em$'), '')`, which reads
`system.all_part_log` (a Merge over `system.part_log`) next to `system.grants`.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (round 1: codex, 2 findings; round 2 on the fixed tree: claude, 0 findings).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261005-131100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261005-085500 |
|
Workflow [PR], commit [22c3ece] Summary: ✅
AI ReviewSummaryThis PR fixes Final Verdict
LLVM Coverage ReportMeasured on commit 22c3ece.
Changed lines: Changed C/C++ lines covered: 29/32 (90.62%) · Uncovered code |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a
LOGICAL_ERRORUnexpected return typewhen reading aMergetable whose underlying table is anotherMergetable with different column types, withoptimize_prewhere_after_pushdown = 1andmove_all_conditions_to_prewhere = 0.Description
On 26.8.10.6 (also 26.6.8.7),
SELECT count() FROM m_outer WHERE a = 1 AND b = 2 SETTINGS optimize_prewhere_after_pushdown = 1, move_all_conditions_to_prewhere = 0, wherem_outer(a,bNullable(UInt64)) is aMergeover theMergetablem_inner(UInt32), fails withCode: 49 ... Unexpected return type from equals. Expected Nullable(UInt8). Got UInt8.Found by BuzzHouse (amd_debug), STID 1611-4266: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=117136&sha=4e6ae73c08748ab5f668b4b0e65b72a4c9f4b2cc&name_0=PR&name_1=BuzzHouse%20%28amd_debug%29. There
merge(REGEXP('em$'), '')readssystem.all_part_log(aMergeoversystem.part_log,database LowCardinality(String)) next tosystem.grants(database Nullable(String)). No related issue found.Each
Mergelevel wraps its outputs inmaterialize(<col>), so the pushed-down filter holds two different nodes namedmaterialize(b): the innerUInt32one and the outerNullable(UInt64)one. The second PREWHERE pass merges the new condition into the existing PREWHERE withActionsDAG::mergeNodes, which rebound children by name, soequalswas attached to the inner node while keeping the function resolved for the outer type.mergeNodesnow rebinds the children and outputs of the nodes it moves fromsecondby pointer, and de-duplicates only inputs by name. Matching nodes ofsecondagainst the existing nodes of the destination DAG by name is unchanged.The new test has 8 arms (two type pairs,
enable_multiple_prewhere_read_steps0/1, themerge()table function). Each arm fails on master; the test fails in 20/20 randomized runs on master and passes in 50/50 with the change.Workflow [PR]
Sync PR [sync-upstream/pr/124037]
Version info
26.10.1.1899-master(included in26.10and later)