branch-4.0: [fix](nereids) clamp the merged limit of MERGE_TOP_N by the parent offset #64306#64352
Open
github-actions[bot] wants to merge 1 commit into
Open
branch-4.0: [fix](nereids) clamp the merged limit of MERGE_TOP_N by the parent offset #64306#64352github-actions[bot] wants to merge 1 commit into
github-actions[bot] wants to merge 1 commit into
Conversation
…fset (#64306) `MergeTopNs` (the `MERGE_TOP_N` rewrite rule) merges a parent `TopN` into its child `TopN` when their order keys are compatible. When the parent `TopN` carries a non-zero `OFFSET`, the merged limit was computed as `min(parent.limit, child.limit)`, which ignores that the parent offset consumes rows from the child's output. The merged `TopN` therefore keeps too many rows and the query returns a wrong result. Example: ```sql SELECT * FROM (SELECT k, v FROM t ORDER BY k LIMIT 5) s ORDER BY k LIMIT 3 OFFSET 4; ``` The inner `ORDER BY k LIMIT 5` yields 5 rows; the outer `LIMIT 3 OFFSET 4` skips 4 of them, so only 1 row should remain. Before this PR the rule merged the two `TopN` into `OFFSET 4 LIMIT 3` (instead of `OFFSET 4 LIMIT 1`), so it returned 3 rows. Fix: clamp the merged limit by `max(child.limit - parent.offset, 0)`, the same semantics already used by `MergeLimits.mergeLimit` for consecutive limits. The bug only triggers when the outer `TopN` has a non-zero offset (offset = 0 makes both formulas equal). The existing unit test `MergeTopNsTest.testOffset` asserted the buggy value (`limit == 10`, while the correct value is `9`); this PR corrects that assertion as well. ### Release note Fix the wrong result produced by the `MERGE_TOP_N` optimization when an outer `ORDER BY ... LIMIT` carries a non-zero `OFFSET` over an inner `ORDER BY ... LIMIT`.
Contributor
|
Thank you for your contribution to Apache Doris. Please clearly describe your PR:
|
Contributor
|
run buildall |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cherry-picked from #64306