[SPARK-57956][SQL] Fix AQE incorrectly eliminating a global LIMIT over a non-materialized query stage#57347
Open
vboo123 wants to merge 1 commit into
Open
[SPARK-57956][SQL] Fix AQE incorrectly eliminating a global LIMIT over a non-materialized query stage#57347vboo123 wants to merge 1 commit into
vboo123 wants to merge 1 commit into
Conversation
…r a non-materialized query stage LogicalQueryStage.maxRows was derived from stats.rowCount, which before a query stage is materialized falls back to the logical plan's cost estimate. That estimate can under-count (e.g. return 0), and EliminateLimits then treats it as a hard upper bound and wrongly removes a global LIMIT, changing the result cardinality. maxRows now trusts the runtime row count only when the stage is materialized, otherwise falling back to logicalPlan.maxRows, which is always a sound upper bound. Generated using Kiro (Claude Opus 4.8)
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.
What changes were proposed in this pull request?
This PR fixes
LogicalQueryStage.maxRowsto only use runtime row counts when the underlying query stage is materialized. Before materialization,computeStats()returnsNoneand the code falls back tologicalPlan.stats, which is a cost estimate, not a hard upper bound. That estimate was being surfaced asmaxRows, causingEliminateLimitsto incorrectly remove a global LIMIT.The fix adds a materialization check: if the stage is materialized, use the runtime
rowCount(an exact upper bound). Otherwise, fall back tologicalPlan.maxRows, which is always a sound upper bound derived from plan structure rather than cardinality estimates.Why are the changes needed?
With AQE enabled, a root global LIMIT can be dropped during adaptive re-optimization, returning too many rows. For example:
This query returns 2 rows instead of 1.
The root cause:
EliminateLimitsremoves aGlobalLimitwhenchild.maxRows <= limit. The child here is aProjectover aLeftOuter Joinwhose left input is aLogicalQueryStagewrappingOffset 1 +- Range. Before that stage materializes,computeStats()falls back tologicalPlan.statsand yieldsrowCount = 0.Join.maxRowsforLeftOuterismax(left * right, left), which correctly propagates the 0 upward.EliminateLimitsthen seesmaxRows = 0 <= 1and drops the LIMIT.The failure is timing-dependent because it depends on whether the stage has materialized when re-optimization runs. This was introduced by SPARK-36424 (AQE EliminateLimits support).
Does this PR introduce any user-facing change?
No API or behavior change. This fixes a correctness bug where queries with a LIMIT over certain AQE plans could return too many rows.
How was this patch tested?
Added a regression test in
AdaptiveQueryExecSuite: "SPARK-57956: AQE must not eliminate a global LIMIT over a row-increasing outer join". Because the trigger is timing-dependent, the test runs the query 20 times and asserts each result has exactly 1 row.Verified:
Ran
AdaptiveQueryExecSuitelocally. Full test matrix relies on GitHub Actions.Was this patch authored or co-authored using generative AI tooling?
Generated using Kiro (Claude Opus 4.8)