Repository navigation
Conversation
m[key] must return the value of the first occurrence of the key in a row. Two key-lookup loops used a cross-row position-prediction shortcut: if the previous row's first match was at relative offset K, offset K was tried first in the current row and accepted if it matched, without ruling out an earlier occurrence. With duplicate keys the prediction could land on a later duplicate, and because the predicted offset depended on preceding rows in the same block, the result was block- and setting-dependent (a SELECT and a WHERE over the same row could disagree, and optimize_functions_to_subcolumns=0 vs 1 could return different values). Drop the prediction and scan each row left to right, taking the first match. The prediction only saved work when the match was at a non-first position, which is exactly the unsafe case; a position-0 match is found by the scan in one comparison, so no correctness-preserving shortcut is faster than the scan. Both paths are fixed so optimize_functions_to_subcolumns stays semantics-preserving: findKeyPositions (the subcolumn path) and executeMatchKeyToIndex (the arrayElement path). Closes: ClickHouse#111203 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Internal second-model review (click to expand)An independent second model reviewed this diff before the PR was opened. Findings: 0 (no blockers, majors, or nits).
The reviewer had no objections to the approach (drop the cross-row position-prediction shortcut; forward-scan first-match in both |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-3:20260721-140500 |
|
cc @antonio2368 @Ergus - could you review this? It fixes |
|
Workflow [PR], commit [cdf7228] Summary: ❌
AI ReviewSummaryThis PR removes the cross-row match-position prediction from both Final Verdict✅ No blockers or majors found. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 18/18 (100.00%) · Uncovered code |
CI finish ledger - cdf7228Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
This PR touches only Session id: cron:our-pr-ci-monitor:20260801-190047 |
Correction + fixing-PR link for the
|
b450d17
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
map[key]returning the value of a later duplicate key instead of the first one. When aMapcolumn contained duplicate keys, the result ofmap[key]could depend on preceding rows in the same block and on theoptimize_functions_to_subcolumnssetting, so aSELECTand aWHEREover the same row could disagree.Description
m[key]must return the value of the FIRST occurrence of the key in a row. Two key-lookup loops used a cross-row position-prediction shortcut: if the previous row's first match was at relative offset K, offset K was tried first in the current row and accepted if it matched, without checking for an earlier occurrence. With duplicate keys the prediction could land on a later duplicate, and because the predicted offset depended on preceding rows, the result was block- and setting-dependent (see thecount()mismatch in the issue).Fix: drop the prediction and scan each row left to right, taking the first match. The prediction only saved work when the match was at a non-first position, which is exactly the unsafe case; when the match is at position 0 the scan finds it in one comparison, so no correctness-preserving shortcut is faster than the scan.
Both affected paths are fixed so the setting stays semantics-preserving:
findKeyPositionsinDataTypeMapHelpers.cpp(theoptimize_functions_to_subcolumnssubcolumn path).executeMatchKeyToIndexinarrayElement.cpp(thearrayElement/m[key]path).New stateless test
04546_map_element_duplicate_keys_first_matchasserts the first-match value is returned with the setting on and off, with and without a filter, and across value types (String, Nullable, LowCardinality, LowCardinality(Nullable), UInt64, Array) and key types (String, LowCardinality(String), UInt64), on both wide and compact parts.Closes #111203