linhongliu-db opened a new pull request, #60414: URL: https://github.com/apache/spark/pull/60414
### What changes were proposed in this pull request? A `LIMIT`/`OFFSET` that is not the root of the query (for example inside `CREATE TABLE AS SELECT`, `INSERT`, a `UNION ALL` branch or a join) over a global `Sort` is planned as `GlobalLimit <- Exchange SinglePartition <- LocalLimit <- Generate <- Sort`. The single-partition shuffle reader concatenates map outputs in fetch order, so the limit can keep arbitrary rows. `InsertSortForLimitAndOffset` (SPARK-50124) re-sorts after the shuffle, but `extractOrderingAndPropagateOrderingColumns` did not walk through `GenerateExec`, so the sort was not inserted when a generator such as `explode` sits between the sort and the limit. The rule now walks through `GenerateExec`, propagating the ordering columns into its required child output so the post-shuffle sort is inserted. A new conf `spark.sql.legacy.orderingAwareLimitOffset.ignoreGenerate` (default `false`) restores the previous behavior. ### Why are the changes needed? A non-root `LIMIT`/`OFFSET` over a sorted generator could keep arbitrary rows on a multi-executor cluster. ### Does this PR introduce _any_ user-facing change? Yes, documented in `docs/sql-migration-guide.md`. Such a limit now keeps the first rows of the sorted input also when a generator sits between the sort and the limit. Set `spark.sql.legacy.orderingAwareLimitOffset.ignoreGenerate=true` to restore the old behavior. ### How was this patch tested? New `InsertSortForLimitAndOffsetSuite` tests covering `explode`/`posexplode`/`explode_outer` and nested generators under a non-root `LIMIT` and `OFFSET`, plus a gate-off case that asserts the sort is not added. Locally the suite (8 tests) passes and scalastyle is clean on JDK 21. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (Isaac) 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]
