alamb commented on code in PR #3527: URL: https://github.com/apache/arrow-datafusion/pull/3527#discussion_r974137474
########## datafusion/core/src/physical_optimizer/parallel_sort.rs: ########## @@ -0,0 +1,92 @@ +// 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. + +//! Parralel sort parallelizes sorts if a limit is present after a sort (`ORDER BY LIMIT N`) +use crate::{ + error::Result, + physical_optimizer::PhysicalOptimizerRule, + physical_plan::{ + limit::GlobalLimitExec, + sorts::{sort::SortExec, sort_preserving_merge::SortPreservingMergeExec}, + with_new_children_if_necessary, + }, +}; +use std::sync::Arc; + +/// Optimizer rule that makes sort parallel if a limit is used after sort (`ORDER BY LIMIT N`) +/// The plan will use `SortPreservingMergeExec` to merge the results +#[derive(Default)] +pub struct ParallelSort {} + +impl ParallelSort { + #[allow(missing_docs)] + pub fn new() -> Self { + Self {} + } +} +impl PhysicalOptimizerRule for ParallelSort { + fn optimize( + &self, + plan: Arc<dyn crate::physical_plan::ExecutionPlan>, + config: &crate::execution::context::SessionConfig, + ) -> Result<Arc<dyn crate::physical_plan::ExecutionPlan>> { + if plan.children().is_empty() { + // leaf node, children cannot be replaced + Ok(plan.clone()) + } else { + // recurse down first + let children = plan + .children() + .iter() + .map(|child| self.optimize(child.clone(), config)) + .collect::<Result<Vec<_>>>()?; + let plan = with_new_children_if_necessary(plan, children)?; + let children = plan.children(); + let plan_any = plan.as_any(); + // GlobalLimitExec (SortExec preserve_partitioning=False) + // -> GlobalLimitExec (SortExec preserve_partitioning=True) + let parallel_sort = plan_any.downcast_ref::<GlobalLimitExec>().is_some() + && children.len() == 1 + && children[0].as_any().downcast_ref::<SortExec>().is_some() + && !children[0] + .as_any() + .downcast_ref::<SortExec>() + .unwrap() + .preserve_partitioning(); Review Comment: I wonder if https://docs.rs/datafusion/12.0.0/datafusion/physical_plan/trait.ExecutionPlan.html#tymethod.output_partitioning could be used here instead? But maybe it doesn't matter as we already know it is exactly a SortExec ########## datafusion/core/tests/sql/explain_analyze.rs: ########## @@ -686,8 +686,8 @@ async fn test_physical_plan_display_indent() { let physical_plan = ctx.create_physical_plan(&plan).await.unwrap(); let expected = vec![ "GlobalLimitExec: skip=0, fetch=10", - " SortExec: [the_min@2 DESC]", - " CoalescePartitionsExec", + " SortPreservingMergeExec: [the_min@2 DESC]", Review Comment: 👍 ########## datafusion/core/src/physical_optimizer/parallel_sort.rs: ########## @@ -0,0 +1,92 @@ +// 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. + +//! Parralel sort parallelizes sorts if a limit is present after a sort (`ORDER BY LIMIT N`) +use crate::{ + error::Result, + physical_optimizer::PhysicalOptimizerRule, + physical_plan::{ + limit::GlobalLimitExec, + sorts::{sort::SortExec, sort_preserving_merge::SortPreservingMergeExec}, + with_new_children_if_necessary, + }, +}; +use std::sync::Arc; + +/// Optimizer rule that makes sort parallel if a limit is used after sort (`ORDER BY LIMIT N`) +/// The plan will use `SortPreservingMergeExec` to merge the results Review Comment: 👍 FYI @tustvold -- more use of this operator 💯 ########## datafusion/core/src/physical_optimizer/parallel_sort.rs: ########## @@ -0,0 +1,92 @@ +// 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. + +//! Parralel sort parallelizes sorts if a limit is present after a sort (`ORDER BY LIMIT N`) +use crate::{ + error::Result, + physical_optimizer::PhysicalOptimizerRule, + physical_plan::{ + limit::GlobalLimitExec, + sorts::{sort::SortExec, sort_preserving_merge::SortPreservingMergeExec}, + with_new_children_if_necessary, + }, +}; +use std::sync::Arc; + +/// Optimizer rule that makes sort parallel if a limit is used after sort (`ORDER BY LIMIT N`) +/// The plan will use `SortPreservingMergeExec` to merge the results +#[derive(Default)] +pub struct ParallelSort {} + +impl ParallelSort { + #[allow(missing_docs)] + pub fn new() -> Self { + Self {} + } +} +impl PhysicalOptimizerRule for ParallelSort { + fn optimize( + &self, + plan: Arc<dyn crate::physical_plan::ExecutionPlan>, + config: &crate::execution::context::SessionConfig, + ) -> Result<Arc<dyn crate::physical_plan::ExecutionPlan>> { + if plan.children().is_empty() { + // leaf node, children cannot be replaced + Ok(plan.clone()) + } else { + // recurse down first + let children = plan + .children() + .iter() + .map(|child| self.optimize(child.clone(), config)) + .collect::<Result<Vec<_>>>()?; + let plan = with_new_children_if_necessary(plan, children)?; + let children = plan.children(); + let plan_any = plan.as_any(); + // GlobalLimitExec (SortExec preserve_partitioning=False) + // -> GlobalLimitExec (SortExec preserve_partitioning=True) + let parallel_sort = plan_any.downcast_ref::<GlobalLimitExec>().is_some() + && children.len() == 1 + && children[0].as_any().downcast_ref::<SortExec>().is_some() + && !children[0] + .as_any() + .downcast_ref::<SortExec>() + .unwrap() + .preserve_partitioning(); Review Comment: 👍 -- 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]
