LukaZdravic opened a new pull request, #60727: URL: https://github.com/apache/spark/pull/60727
### What changes were proposed in this pull request? `LimitPushDown` pushes a smaller `LocalLimit` into UNION ALL, outer joins and Python UDF inputs. If the child was a `GlobalLimit`, the rule removed it. Now the rule keeps a `GlobalLimit` whose child is a global `Sort` (also through `Project`, `Filter`, `Offset` and other order-keeping nodes), and puts the new `LocalLimit` on top of it. Other `GlobalLimit`s are removed as before. The right side of a LEFT SEMI/ANTI join with no condition still drops its `GlobalLimit`, because only whether that side is empty matters there. ### Why are the changes needed? It is a wrong-result bug. `ORDER BY a LIMIT 5` keeps the top 5 rows, but a `LocalLimit` keeps the first rows of each partition. So the outer limit could return rows outside the top 5. ```sql SELECT l.a FROM (SELECT a FROM sorted_t ORDER BY a LIMIT 5) l LEFT JOIN join_r r ON l.a = r.b LIMIT 3 -- before: 248, 997, 998 after: three values from 0 to 4 ``` ### Does this PR introduce _any_ user-facing change? Yes, a bug fix. The query above now returns rows from the top 5. Plans of this shape sort all rows for the top 5 again, where the old plan read a few rows per partition. ### How was this patch tested? - `LimitPushdownSuite`: the `GlobalLimit` stays for UNION ALL, outer joins, Python UDF input, and over `Project`/`Filter`; it is still removed over a local sort (`SORT BY`) and the semi/anti right side. These fail without the fix. - `SQLQuerySuite`: the query above, with LEFT and RIGHT JOIN, returns only values below 5. It returns `248, 997, 998` without the fix. - Also ran `SQLQueryTestSuite`, the TPC-DS/TPC-H plan stability suites, `PlannerSuite`, `AdaptiveQueryExecSuite`, `DataSourceV2Suite`, `ExtractPythonUDFsSuite` and other limit suites. No plan changed. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code This pull request and its description were written by Isaac. -- 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]
