Capture global ORDER BY requirement under ScalarSubqueryExec root#23146
Draft
sgrebnov wants to merge 1 commit into
Draft
Capture global ORDER BY requirement under ScalarSubqueryExec root#23146sgrebnov wants to merge 1 commit into
sgrebnov wants to merge 1 commit into
Conversation
require_top_ordering_helper bailed on ScalarSubqueryExec because it has multiple children (main input + subquery plans), so the rule stamped an empty OutputRequirementExec (order_by=[], dist_by=Unspecified) at the root and never recorded the query's global ORDER BY requirement. Descend through child 0 (the order-transparent main input: maintains_input_order()[0] == true, no required input ordering) to find and protect the top SortExec, reattaching the subquery children unchanged.
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.
Which issue does this PR close?
After upgrading from DataFusion 53 to 54 queries with ScalarSubquery started returning unordered results (
ORDER BYwas not respected)Rationale for this change
OutputRequirementswalks the plan viarequire_top_ordering_helperto capture the query's globalORDER BYas anOutputRequirementExec. The helper bails on any node with more than one child (children.len() != 1).ScalarSubqueryExecis such a node — child 0 is the order-transparent main input and the rest are uncorrelated subquery plans — so when it is the plan root the rule stops immediately and stamps an emptyOutputRequirementExec(order_by=[], dist_by=Unspecified)at the top, never recording the global ordering requirement.ScalarSubqueryExecis order-transparent on its main input (maintains_input_order()[0] == true, no required input ordering), so the search should descend through child 0, as it does for other single-child order-preserving operators.What changes are included in this PR?
require_top_ordering_helpernow special-casesScalarSubqueryExec: it descends through child 0 to find and wrap the topSortExec, reattaching the subquery children unchanged.Are these changes tested?
A new unit test (
add_mode_descends_through_scalar_subquerythat asserts theOutputRequirementExeccarrying the real ordering is placed below theScalarSubqueryExec; without the fix the rule produces the emptyorder_by=[], dist_by=Unspecifiedrequirement at the root.Existing
subquery.slt/ TPC-H plan snapshots are unchanged: for built-in sourcesEnforceSortingindependently rebuilds the order-preserving merge, so final plans are identical. WIP: adding visible/end-to-end coverage on top of the unit test.Are there any user-facing changes?
No.