-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Reorder the physical plan optimizer rules, extract GlobalSortSelection, make Repartition optional
#4714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reorder the physical plan optimizer rules, extract GlobalSortSelection, make Repartition optional
#4714
Changes from all commits
76cde39
46ddd99
4850383
b95ba86
0466b8d
38026fd
ad2d39e
8eb3f56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -264,6 +264,6 @@ impl ExecutionPlan for CustomExec { | |
| } | ||
|
|
||
| fn statistics(&self) -> Statistics { | ||
| todo!() | ||
| Statistics::default() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ use crate::{ | |
| physical_optimizer::PhysicalOptimizerRule, | ||
| physical_plan::{ | ||
| coalesce_batches::CoalesceBatchesExec, filter::FilterExec, joins::HashJoinExec, | ||
| repartition::RepartitionExec, rewrite::TreeNodeRewritable, | ||
| repartition::RepartitionExec, rewrite::TreeNodeRewritable, Partitioning, | ||
| }, | ||
| }; | ||
| use std::sync::Arc; | ||
|
|
@@ -57,7 +57,16 @@ impl PhysicalOptimizerRule for CoalesceBatches { | |
| // See https://github.com/apache/arrow-datafusion/issues/139 | ||
| let wrap_in_coalesce = plan_any.downcast_ref::<FilterExec>().is_some() | ||
| || plan_any.downcast_ref::<HashJoinExec>().is_some() | ||
| || plan_any.downcast_ref::<RepartitionExec>().is_some(); | ||
| // Don't need to add CoalesceBatchesExec after a round robin RepartitionExec | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 makes sense to me -- to confirm the rationale for this change is that RoundRobinRepartition simply shuffles batches around to different partitions, but doesn't actually change (grow or shrink) the actual RecordBatches |
||
| || plan_any | ||
| .downcast_ref::<RepartitionExec>() | ||
| .map(|repart_exec| { | ||
| !matches!( | ||
| repart_exec.partitioning().clone(), | ||
| Partitioning::RoundRobinBatch(_) | ||
| ) | ||
| }) | ||
| .unwrap_or(false); | ||
| if wrap_in_coalesce { | ||
| Ok(Some(Arc::new(CoalesceBatchesExec::new( | ||
| plan.clone(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // 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. | ||
|
|
||
| //! Select the efficient global sort implementation based on sort details. | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::error::Result; | ||
| use crate::physical_optimizer::PhysicalOptimizerRule; | ||
| use crate::physical_plan::rewrite::TreeNodeRewritable; | ||
| use crate::physical_plan::sorts::sort::SortExec; | ||
| use crate::physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec; | ||
| use crate::physical_plan::ExecutionPlan; | ||
| use crate::prelude::SessionConfig; | ||
|
|
||
| /// Currently for a sort operator, if | ||
| /// - there are more than one input partitions | ||
| /// - and there's some limit which can be pushed down to each of its input partitions | ||
| /// then [SortPreservingMergeExec] with local sort with a limit pushed down will be preferred; | ||
| /// Otherwise, the normal global sort [SortExec] will be used. | ||
| /// Later more intelligent statistics-based decision can also be introduced. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See also #4691 |
||
| /// For example, for a small data set, the global sort may be efficient enough | ||
| #[derive(Default)] | ||
| pub struct GlobalSortSelection {} | ||
|
|
||
| impl GlobalSortSelection { | ||
| #[allow(missing_docs)] | ||
| pub fn new() -> Self { | ||
| Self {} | ||
| } | ||
| } | ||
|
|
||
| impl PhysicalOptimizerRule for GlobalSortSelection { | ||
| fn optimize( | ||
| &self, | ||
| plan: Arc<dyn ExecutionPlan>, | ||
| _config: &SessionConfig, | ||
| ) -> Result<Arc<dyn ExecutionPlan>> { | ||
| plan.transform_up(&|plan| { | ||
| Ok(plan | ||
| .as_any() | ||
| .downcast_ref::<SortExec>() | ||
| .and_then(|sort_exec| { | ||
| if sort_exec.input().output_partitioning().partition_count() > 1 | ||
| && sort_exec.fetch().is_some() | ||
| // It's already preserving the partitioning so that it can be regarded as a local sort | ||
| && !sort_exec.preserve_partitioning() | ||
| { | ||
| let sort = SortExec::new_with_partitioning( | ||
| sort_exec.expr().to_vec(), | ||
| sort_exec.input().clone(), | ||
| true, | ||
| sort_exec.fetch(), | ||
| ); | ||
| let global_sort: Arc<dyn ExecutionPlan> = | ||
| Arc::new(SortPreservingMergeExec::new( | ||
| sort_exec.expr().to_vec(), | ||
| Arc::new(sort), | ||
| )); | ||
| Some(global_sort) | ||
| } else { | ||
| None | ||
| } | ||
| })) | ||
| }) | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "global_sort_selection" | ||
| } | ||
|
|
||
| fn schema_check(&self) -> bool { | ||
| false | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍