Repository navigation
Fix wrong ORDER BY, DISTINCT and LIMIT BY results over a Merge table of a sorted Iceberg table - #125383
Conversation
…ren cannot deliver `ReadFromMerge::requestReadingInOrder` asked only the `ReadFromMergeTree` steps of each child plan. A child plan with no such step (an Iceberg table read through `ReadFromObjectStorageStep`, whose metadata carries a sorting key from its Iceberg sort order) was accepted without being asked, so the planner replaced the full sort with a merge of unsorted streams. ORDER BY [DESC] [LIMIT n], DISTINCT, LIMIT BY and aggregation in order over `merge()` of such a table returned wrong results, and in debug builds the in-order DISTINCT tripped the `checkEqualRangeEndAssumeSorted` assertion (AST fuzzer, STID 2508-31ed). `recursivelyApplyToReadingSteps` now rejects a reading leaf that is neither a `ReadFromMergeTree` nor a `ReadNothingStep`. `ReadFromMerge::canReadInOrder(direction)` replaces `canReadInReverseOrder` and asks every reading step of every child plan without switching any of them. `requestReadingInOrder` calls it before it switches the children, so in a mixed `Merge` no `MergeTree` child is left reading in order when another child rejects the request. The `topKThroughJoin` probe calls it for both directions, so for an ascending `ORDER BY ... LIMIT` over such a `Merge` it no longer defers to an in-order read that pass 2 declines (which left neither optimization applied). CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=125275&sha=68dabec672d4b2e357a16f21849b2eb329fe931d&name_0=PR&name_1=AST%20fuzzer%20%28amd_debug%2C%20targeted%29 Found on: ClickHouse#125275 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the requestReadingInOrder pre-check comment to one line that does not claim more than the check does. 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 (engine: codex; 2 rounds, 4 findings) plus my own cold review.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261012-102300 |
|
Workflow [PR], commit [7172333] Summary: ✅
AI ReviewSummaryThis PR correctly closes the Findings
Tests
Final Verdict
|
| if (auto * read_from_merge_tree = typeid_cast<ReadFromMergeTree *>(node->step.get())) | ||
| ok &= func(*read_from_merge_tree); | ||
| else if (node->children.empty() && !typeid_cast<ReadNothingStep *>(node->step.get())) | ||
| return false; /// Only a `ReadFromMergeTree` can be asked to read in order; any other read does not promise it. |
There was a problem hiding this comment.
This still lets a nested ReadFromMerge child through as long as its leaves are ReadFromMergeTree, but the nested ReadFromMerge step itself never receives requestReadingInOrder. Later it still sees query_info.input_order_info == nullptr, so its own initializePipeline can narrow() those sorted child streams (should_not_narrow checks only the step's own query_info.input_order_info), which destroys the order before the outer Merge/DISTINCT/LIMIT BY path consumes it. That leaves the same wrong-results class for Merge over another Merge table.
Could we either propagate the request into nested ReadFromMerge nodes themselves, or conservatively reject them here instead of recursing past them?
There was a problem hiding this comment.
🕵 This doesn't happen, for two independent reasons:
recursivelyApplyToReadingStepswalksnode->childrenof the child plan. It does not descend into the step-owned child plans of a nestedReadFromMerge. A nestedReadFromMergeis a source step with no children, so it is a leaf that is notReadFromMergeTreeorReadNothingStep. With this PR, that makescanReadInOrderreturnfalse, and the outerMergeread declines. That is the conservative rejection suggested above.- Even before that,
getReadingStepinoptimizeReadInOrder.cpprequires every selected table to have a sorting key in its metadata.StorageMergenever sets a sorting key, so aMergetable among the selected tables already stops the optimization there.
alexey-milovidov
left a comment
There was a problem hiding this comment.
I don't see modifications for Iceberg here, please explain.
|
🕵 @alexey-milovidov, the Iceberg code doesn't need any changes. The bug is in How a
So the defect shows up for a child whose metadata has a sorting key but whose plan leaf is not The Iceberg reader was already correct: a direct read of the same table does ask I could have forwarded the request to |
|
Confirmed, the explanation above is accurate: no Iceberg change is needed, the fix is in |
8b02267
Related: #112981
Related: #125275
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed wrong results of
ORDER BY,DISTINCT,LIMIT BYand aggregation in order over aMergetable or themergetable function when it reads an Iceberg table with a sort order: the rows came back unsorted or duplicated.Description
On 26.8.2.3,
SELECT k FROM merge(currentDatabase(), '^t$') ORDER BY kover an Iceberg table with a sort order returns the rows unsorted, andLIMIT 1 BY kreturns duplicates.Found by the AST fuzzer as
Logical error: 'Equal values are not contiguous within the range assumed to be sorted' (STID: 2508-31ed)inDistinctSortedStreamTransform: CI report.ReadFromMerge::requestReadingInOrderasked only theReadFromMergeTreesteps of each child plan. An Iceberg child reads throughReadFromObjectStorageStep, so it was accepted without being asked, while its metadata has a sorting key. The planner then replaced the full sort with a merge of unsorted streams. A direct read asksReadFromObjectStorageStepitself, so it does not have this defect.Now
recursivelyApplyToReadingStepsrejects a reading leaf other thanReadFromMergeTreeorReadNothingStep, so theMergeread declines and keeps the full sort.ReadFromMerge::canReadInOrder(direction)replacescanReadInReverseOrderand asks every child before any is switched, so in a mixedMergenoMergeTreechild is left reading in order when another child rejects the request. ThetopKThroughJoinprobe asks it for both directions, so it no longer defers to an in-order read that theMergeread declines. AMergeoverMergeTreetables only is unchanged.One design choice: a sorted Iceberg child could be asked through
ReadFromObjectStorageStep::requestReadingInOrderinstead. I did not, because that read can itself return unsorted rows per stream (#112981).The new test fails on 11 of its 12 result lines on master and passes 100/100 with the fix (50 randomized runs, 50 with fixed settings).
🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/125383]
Version info
26.10.1.2449-master(included in26.10and later)