mingmwang commented on code in PR #5661: URL: https://github.com/apache/arrow-datafusion/pull/5661#discussion_r1155447149
########## datafusion/core/src/physical_optimizer/sort_pushdown.rs: ########## @@ -0,0 +1,416 @@ +// 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::physical_optimizer::utils::{add_sort_above, is_limit, is_union, is_window}; +use crate::physical_plan::filter::FilterExec; +use crate::physical_plan::joins::utils::JoinSide; +use crate::physical_plan::joins::SortMergeJoinExec; +use crate::physical_plan::projection::ProjectionExec; +use crate::physical_plan::repartition::RepartitionExec; +use crate::physical_plan::sorts::sort::SortExec; +use crate::physical_plan::{with_new_children_if_necessary, ExecutionPlan}; +use datafusion_common::tree_node::{Transformed, TreeNode, VisitRecursion}; +use datafusion_common::{DataFusionError, Result}; +use datafusion_expr::JoinType; +use datafusion_physical_expr::expressions::Column; +use datafusion_physical_expr::utils::{ + make_sort_exprs_from_requirements, ordering_satisfy_requirement, + requirements_compatible, +}; +use datafusion_physical_expr::{ + make_sort_requirements_from_exprs, PhysicalSortExpr, PhysicalSortRequirement, +}; +use itertools::izip; +use std::ops::Deref; +use std::sync::Arc; + +/// This is a "data class" we use within the [`EnforceSorting`] rule to push +/// down [`SortExec`] in the plan. In some cases, we can reduce the total +/// computational cost by pushing down `SortExec`s through some executors. +#[derive(Debug, Clone)] +pub(crate) struct SortPushDown { + /// Current plan + pub plan: Arc<dyn ExecutionPlan>, + /// Parent required sort ordering + required_ordering: Option<Vec<PhysicalSortRequirement>>, + /// The adjusted request sort ordering to children. + /// By default they are the same as the plan's required input ordering, but can be adjusted based on parent required sort ordering properties. + adjusted_request_ordering: Vec<Option<Vec<PhysicalSortRequirement>>>, +} + +impl SortPushDown { + pub fn init(plan: Arc<dyn ExecutionPlan>) -> Self { + let request_ordering = plan.required_input_ordering(); + SortPushDown { + plan, + required_ordering: None, + adjusted_request_ordering: request_ordering, + } + } + + pub fn children(&self) -> Vec<SortPushDown> { + izip!( + self.plan.children().into_iter(), + self.adjusted_request_ordering.clone().into_iter(), + ) + .map(|(child, from_parent)| { + let child_request_ordering = child.required_input_ordering(); + SortPushDown { + plan: child, + required_ordering: from_parent, + adjusted_request_ordering: child_request_ordering, + } + }) + .collect() + } +} + +impl TreeNode for SortPushDown { + fn apply_children<F>(&self, op: &mut F) -> Result<VisitRecursion> + where + F: FnMut(&Self) -> Result<VisitRecursion>, + { + let children = self.children(); + for child in children { + match op(&child)? { + VisitRecursion::Continue => {} + VisitRecursion::Skip => return Ok(VisitRecursion::Continue), + VisitRecursion::Stop => return Ok(VisitRecursion::Stop), + } + } + + Ok(VisitRecursion::Continue) + } + + fn map_children<F>(mut self, transform: F) -> Result<Self> Review Comment: The original `self` in `map_children` is immutable, why it was changed to be mutable here ? ```rust fn map_children<F>(self, transform: F) -> Result<Self> ``` -- 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]
