Repository navigation
Conversation
6ce79b4 to
eb5a24a
Compare
|
@alamb PTAL🙏 |
| /// Unlike [`Self::required_input_ordering`], this property does not require | ||
| /// a particular ordering that can be expressed using the child's schema. | ||
| /// It only requires that an existing input ordering is not discarded or | ||
| /// replaced with an incompatible ordering by physical optimizations. For |
There was a problem hiding this comment.
This property seems like it is some internal invariant of EnforceDistribution rather than something that describes how the ExecutionPlan behaves
I don't think we should add this to the ExecutionPlan trait
|
Isn't LIMIT already handled for subqueries? Perhaps we just need to extend the same logic to handle I don't understand the need to add a new method to ExecutionPlan |
5a7680b to
11ddca6
Compare
9f4b5c8 to
322826b
Compare
322826b to
2f97395
Compare
I tried extending PushDownLimit for OFFSET, but correctness still depended on optimizer pass order. With one pass, Limit → Projection → Sort remains and EnsureRequirements incorrectly removes the sort. So, I simplified the patch: no ExecutionPlan API changes—just use the existing is_limit check to preserve linked sorts below a limit, with a regression test proving the fix. Please review again @alamb. |
|
@alamb Would you please take a look? |
…ache#26763) ## Which issue does this PR close? Closes apache#23534. This follows the closed, unmerged apache#23744. ## Rationale for this change A grouped query over `ORDER BY sort_key OFFSET 1` can return `sorted_first` instead of `physical_second`. A projection hides the sort key, so the physical optimizer drops the sort even though it determines which rows the offset retains. ## What changes are included in this PR? Preserve linked sorts through operators that maintain input order, including when a projection removes their ordering metadata. Remove superseded sorts when enforcing a new ordering, and stop sort removal at explicit limits and operators with embedded fetch limits. Add SQL and physical optimizer regressions for projections, intervening filters, local and global limits, replacement sorts, and optimizer idempotence. An execution test verifies that an embedded filter limit still selects the correct rows after one or two optimizer passes. ## What is the testing strategy for this PR? - The ordered-offset SQL regression and embedded-fetch execution regression fail without their respective fixes. - The full extended suite passes: 12,987 Rust tests and 542 SQL files. - `cargo fmt --all`, `cargo clippy --all-targets --all-features -- -D warnings`, and the complete `./dev/rust_lint.sh` suite pass. - Compared `physical_plan_struct_join_agg_sort` and `physical_sorted_union_order_by_10_int64` against main at `350ca76505c9513d16e7dd9910bcd9bde2c911ed`, using 30 samples each. Neither benchmark showed a statistically significant performance change. ## Are there any user-facing changes? Queries retain the rows selected by ordered LIMIT/OFFSET when a projection hides the sort key. No public API changes.
Which issue does this PR close?
GROUP BYconsumes a subquery containingORDER BY ... OFFSET. #23534.Rationale for this change
EnsureRequirements may remove a descendant SortExec when the child has no visible output ordering.
This is incorrect at a LIMIT boundary because LIMIT and OFFSET depend on the input sequence to determine which rows are returned. When an intervening ProjectionExec removes the sort key, its output_ordering() becomes None, even though it still preserves the row sequence. The optimizer then incorrectly removes the sort below the projection, producing incorrect results.
What changes are included in this PR?
Prevent EnsureRequirements from removing a linked SortExec at a limit boundary.
Add a SQL regression test that verifies the correct row is selected by an ordered subquery with OFFSET.
Configure the regression test with one logical optimizer pass to ensure correctness does not depend on a later logical rewrite.
No public API changes are included.
Are these changes tested?
Yes. The regression test fails without the fix, returning sorted_first instead of physical_second.
The following checks were run:
Are there any user-facing changes?
Yes. Ordered subqueries followed by LIMIT or OFFSET now preserve the sort needed to select the correct rows when an intervening projection hides the sort key.
There are no public API or documentation changes.