pepijnve commented on code in PR #16196: URL: https://github.com/apache/datafusion/pull/16196#discussion_r2121550224
########## datafusion/physical-optimizer/src/wrap_leaves_cancellation.rs: ########## @@ -0,0 +1,106 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::PhysicalOptimizerRule; +use datafusion_common::config::ConfigOptions; +use datafusion_common::Result; +use datafusion_physical_plan::execution_plan::EmissionType; +use datafusion_physical_plan::yield_stream::YieldStreamExec; +use datafusion_physical_plan::ExecutionPlan; +use std::fmt::{Debug, Formatter}; +use std::sync::Arc; + +/// `WrapLeaves` is a `PhysicalOptimizerRule` that traverses a physical plan +/// and, for every operator whose `emission_type` is `Final`, wraps its direct +/// children inside a `YieldStreamExec`. This ensures that pipeline‐breaking +/// operators (i.e. those with `Final` emission) have a “yield point” immediately +/// upstream, without having to wait until the leaves. +pub struct WrapLeaves {} + +impl WrapLeaves { + /// Create a new instance of the WrapLeaves rule. + pub fn new() -> Self { + Self {} + } + + /// Recursively walk the plan: + /// - If `plan.children_any().is_empty()`, it’s a leaf, so wrap it. + /// - Otherwise, recurse into its children, rebuild the node with + /// `with_new_children_any(...)`, and return that. + #[allow(clippy::only_used_in_recursion)] + fn wrap_recursive( + &self, + plan: Arc<dyn ExecutionPlan>, + has_pipeline_breaking_above: bool, + ) -> Result<Arc<dyn ExecutionPlan>> { + let children = plan.children(); Review Comment: Is it worth trying to use the TreeNode transformation facilities here? Something like below. It's just as much code, but makes use of the facilities provided by the crate. ``` fn wrap_leaves( plan: Arc<dyn ExecutionPlan>, ) -> common::Result<Transformed<Arc<dyn ExecutionPlan>>> { if plan.children().is_empty() { // Leaf node: wrap it in `YieldStreamExec` and stop recursing Ok(Transformed::new(Arc::new(YieldStreamExec::new(plan)), true, TreeNodeRecursion::Jump)) } else { Ok(Transformed::no(plan)) } } fn wrap_leaves_of_pipeline_breakers( plan: Arc<dyn ExecutionPlan>, ) -> common::Result<Transformed<Arc<dyn ExecutionPlan>>> { let is_pipeline_breaker = plan.properties().emission_type == EmissionType::Final; if is_pipeline_breaker { let mut transformed = plan.transform_down(wrap_leaves)?; transformed.tnr = TreeNodeRecursion::Jump; Ok(transformed) } else { Ok(Transformed::no(plan)) } } impl PhysicalOptimizerRule for WrapLeaves { fn optimize(&self, plan: Arc<dyn ExecutionPlan>, _: &ConfigOptions) -> common::Result<Arc<dyn ExecutionPlan>> { plan.transform_down(wrap_leaves_of_pipeline_breakers).data() } } ``` -- 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: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org