alamb commented on code in PR #27925:
URL: https://github.com/apache/datafusion/pull/27925#discussion_r4762185631
##########
datafusion/physical-optimizer/src/projection_pushdown.rs:
##########
@@ -507,6 +513,66 @@ fn leaf_column_name(index: usize) -> String {
format!("{LEAF_COLUMN_PREFIX}_{index}")
}
+/// Calls [`try_split_leaf_projection`] on each node of `plan`, from the root
down.
+///
+/// `order_matters` is true if something above `plan` can rely on its ordering.
+#[cfg_attr(feature = "recursive_protection", recursive::recursive)]
+fn split_projections(
+ plan: Arc<dyn ExecutionPlan>,
+ order_matters: bool,
+) -> Result<Arc<dyn ExecutionPlan>> {
+ let plan = try_split_leaf_projection(&plan,
order_matters)?.unwrap_or(plan);
+
+ let required = plan.required_input_ordering();
+ let only_declared = relies_only_on_declared_ordering(&plan);
+ let mut changed = false;
+ let mut children = vec![];
+ for (child, required) in plan.children().into_iter().zip(required) {
+ let order_matters = order_matters || required.is_some() ||
!only_declared;
+ // A sort replaces the ordering of its input, unless the input is
sorted.
+ let order_matters = match plan.downcast_ref::<SortExec>() {
+ Some(sort) => {
+ order_matters
+ && child
+ .equivalence_properties()
+ .ordering_satisfy(sort.expr().clone())?
+ }
+ None => order_matters,
+ };
+ let new_child = split_projections(Arc::clone(child), order_matters)?;
+ changed |= !Arc::ptr_eq(child, &new_child);
+ children.push(new_child);
+ }
+ if !changed {
+ return Ok(plan);
+ }
+ plan.replace_children(
+ children,
+ ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute),
+ )
+}
+
+/// Returns true for operators known to need no ordering of their input beyond
+/// `required_input_ordering`.
+///
+/// Any other operator can need one that it does not declare. `PartialSortExec`
+/// does, and `AggregateExec` chooses its mode from the ordering of its input.
+/// Nothing detects the wrong rows that a lost ordering then gives.
+fn relies_only_on_declared_ordering(plan: &Arc<dyn ExecutionPlan>) -> bool {
Review Comment:
If this is an important property if should be put on ExectionPlan rather
than this hard coded list -- otherwise extension nodes can't transmit it
I also don't understand how it differs from `required_input_ordering` -- all
those operators don't need a required_input_ordering execept
`SortPreservingMergeExec`
##########
datafusion/physical-optimizer/src/projection_pushdown.rs:
##########
@@ -74,13 +83,10 @@ impl PhysicalOptimizerRule for ProjectionPushdown {
// narrows and holds a computed one. `RepartitionExec` declines both,
it
// is not removable because it renames, and nothing else moved.
let plan = plan.transform_down(remove_unnecessary_projections).data()?;
- plan.transform_down(|plan| {
- Ok(match try_split_leaf_projection(&plan)? {
- Some(new_plan) => Transformed::yes(new_plan),
- None => Transformed::no(plan),
- })
- })
- .data()
+ // `OutputRequirementExec` declares the ordering that the query needs.
Without
+ // one at the root, the caller may rely on any ordering of the output.
+ let order_matters = !plan.is::<OutputRequirementExec>();
Review Comment:
this seems incorrect to me to special case check for `OutputRequirementExec`
-- @jayzhan211 and I just today updated the docs to try and explain what it
does better:
https://github.com/apache/datafusion/blob/5184bdd738d285582893b42f90756ae1f245903d/datafusion/physical-optimizer/src/output_requirements.rs#L35-L90
Basically ordering should matter if the node above this one is relying on
the ordering coming out
##########
datafusion/physical-optimizer/src/projection_pushdown.rs:
##########
@@ -507,6 +513,66 @@ fn leaf_column_name(index: usize) -> String {
format!("{LEAF_COLUMN_PREFIX}_{index}")
}
+/// Calls [`try_split_leaf_projection`] on each node of `plan`, from the root
down.
+///
+/// `order_matters` is true if something above `plan` can rely on its ordering.
+#[cfg_attr(feature = "recursive_protection", recursive::recursive)]
+fn split_projections(
+ plan: Arc<dyn ExecutionPlan>,
+ order_matters: bool,
+) -> Result<Arc<dyn ExecutionPlan>> {
+ let plan = try_split_leaf_projection(&plan,
order_matters)?.unwrap_or(plan);
+
+ let required = plan.required_input_ordering();
+ let only_declared = relies_only_on_declared_ordering(&plan);
+ let mut changed = false;
+ let mut children = vec![];
+ for (child, required) in plan.children().into_iter().zip(required) {
+ let order_matters = order_matters || required.is_some() ||
!only_declared;
+ // A sort replaces the ordering of its input, unless the input is
sorted.
+ let order_matters = match plan.downcast_ref::<SortExec>() {
+ Some(sort) => {
+ order_matters
+ && child
+ .equivalence_properties()
+ .ordering_satisfy(sort.expr().clone())?
+ }
+ None => order_matters,
+ };
+ let new_child = split_projections(Arc::clone(child), order_matters)?;
+ changed |= !Arc::ptr_eq(child, &new_child);
+ children.push(new_child);
+ }
+ if !changed {
+ return Ok(plan);
+ }
+ plan.replace_children(
+ children,
+ ReplaceChildrenOptions::new(ChildrenPropertiesMode::Recompute),
+ )
+}
+
+/// Returns true for operators known to need no ordering of their input beyond
+/// `required_input_ordering`.
+///
+/// Any other operator can need one that it does not declare. `PartialSortExec`
+/// does, and `AggregateExec` chooses its mode from the ordering of its input.
+/// Nothing detects the wrong rows that a lost ordering then gives.
+fn relies_only_on_declared_ordering(plan: &Arc<dyn ExecutionPlan>) -> bool {
Review Comment:
Poking around with the 🤖 it seems like maybe the real issue is that
`PartialSortExec` does not declare `required_input_ordering` or
`maintains_input_order`. even though it relies on its sorted prefix
We could probably add that as a separate PR (and remove the generic check
here)
https://github.com/apache/datafusion/blob/bb1dad19baf34774abd8bbc8f397d0253c0d9945/datafusion/physical-optimizer/src/ensure_requirements/enforce_distribution.rs#L2051-L2058
--
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]