abhinav-phi opened a new pull request, #69917:
URL: https://github.com/apache/doris/pull/69917
### What problem does this PR solve?
Issue Number: close #69490
Problem Summary:
`EliminateLimitUnderApply` removed a `LIMIT` below a correlated `Apply`
without checking whether the limit can change the result. After
`PullUpProjectUnderApply` exposes a derived table's `LogicalLimit` as the Apply
right child, the rule also dropped `LIMIT 0`, `LIMIT ... OFFSET n` and
`IN`/`NOT IN` limits, which changes the answer of the subquery.
This PR restricts the rule to the only case where the limit cannot affect
the result: a correlated `EXISTS`/`NOT EXISTS` with a positive limit and zero
offset. `LIMIT 0`, an offset, and `IN`/`NOT IN` now keep their limit.
`SubExprAnalyzer` now unwraps row count preserving nodes (`LogicalProject`,
`LogicalSort`, `LogicalSubQueryAlias`) before checking `rootIsLimit*`, so a
limit inside a derived table is validated the same way as a top level limit. As
a result, correlated `EXISTS`/`IN` with a nested `LIMIT 0` or `LIMIT ...
OFFSET` are either folded or rejected consistently with their top level forms
instead of reaching the rewrite as a limit that looks safe to remove.
### Release note
None
### Check List (For Author)
- Test
- [x] Unit Test
- `EliminateLimitUnderApplyTest` (new, 7 cases): positive limit for
correlated EXISTS/NOT EXISTS is eliminated; `LIMIT 0`, offset, `IN`, scalar and
uncorrelated Apply keep the limit.
- `AnalyzeSubQueryTest` (8 new cases): nested `LIMIT 0` is folded,
nested offset and `IN` limits are rejected, and the positive limit is removed
from the rewritten plan.
- [ ] Regression test
- [ ] Manual test
- Behavior changed:
- [x] Yes. Queries that previously had a correlated subquery limit
removed now keep it or are rejected consistently. This is the intended fix, the
previous plans could return wrong results.
- Does this need documentation?
- [x] No.
### Verification
```
Tests run: 7, Failures: 0, Errors: 0, Skipped: 0 -
EliminateLimitUnderApplyTest
Tests run: 20, Failures: 0, Errors: 0, Skipped: 0 - AnalyzeSubQueryTest
Tests run: 27, Failures: 0, Errors: 0, Skipped: 0
BUILD SUCCESS
```
Command: `mvn test -pl fe-core -am
-Dtest=AnalyzeSubQueryTest,EliminateLimitUnderApplyTest -Dcheckstyle.skip=true`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]