jayzhan211 commented on code in PR #27772:
URL: https://github.com/apache/datafusion/pull/27772#discussion_r4731396595
##########
datafusion/physical-optimizer/src/window_topn.rs:
##########
@@ -317,16 +318,27 @@ impl PhysicalOptimizerRule for WindowTopN {
return Ok(plan);
}
- plan.transform_down(|node| {
+ let rewritten = Arc::clone(&plan).transform_down(|node| {
Ok(
if let Some(transformed) = WindowTopN::try_transform(&node) {
Transformed::yes(transformed)
} else {
Transformed::no(node)
},
)
- })
- .data()
+ })?;
+ // `PartitionedTopKExec` emits its rows ordered by the partition keys
in
+ // the default sort options, whatever order its input had. Once
+ // `EnsureRequirements` has made the plan's requirements hold, an
+ // operator above may rely on the order the window had, and nothing
+ // would restore it.
Review Comment:
Reworded around your point. I kept "a requirement that held before" rather
than "the same order as originally", so the rule still rewrites when nothing
above relies on the window's order, for example a query without `ORDER BY`.
##########
datafusion/physical-optimizer/src/window_topn.rs:
##########
@@ -317,16 +318,27 @@ impl PhysicalOptimizerRule for WindowTopN {
return Ok(plan);
}
- plan.transform_down(|node| {
+ let rewritten = Arc::clone(&plan).transform_down(|node| {
Ok(
if let Some(transformed) = WindowTopN::try_transform(&node) {
Transformed::yes(transformed)
} else {
Transformed::no(node)
},
)
- })
- .data()
+ })?;
+ // `PartitionedTopKExec` emits its rows ordered by the partition keys
in
+ // the default sort options, whatever order its input had. Once
+ // `EnsureRequirements` has made the plan's requirements hold, an
+ // operator above may rely on the order the window had, and nothing
+ // would restore it.
+ if rewritten.transformed
+ && input_requirements_hold(&plan)
+ && !input_requirements_hold(&rewritten.data)
+ {
+ return Ok(plan);
Review Comment:
`plan` is the whole plan the rule receives, and `input_requirements_hold`
checks, for every node in it, that its children meet that node's required
ordering and distribution. So it covers the parent and every node above it. The
parent alone isn't enough: in the test, the rewrite replaces the `FilterExec`,
its parent is a `ProjectionExec` that requires nothing, and the requirement
that breaks is the `OutputRequirementExec` for `ORDER BY pk` above that.
--
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]