-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-12170: [Rust][DataFusion] Introduce repartition optimization #9865
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
Closed
Closed
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
ad51f3f
WIP
Dandandan 0cfa2c6
WIP
Dandandan a30afc3
Add test
Dandandan 823cf54
WIP
Dandandan c2f4de8
WIP
Dandandan 065abf4
WIP
Dandandan 69d1bd9
WIP
Dandandan 4ce8ec6
Fix memec
Dandandan 9180921
Merge branch 'master' into reparition-opt
Dandandan 97e071d
Fix test
Dandandan 556779e
Fmt
Dandandan dfc8b6c
Reorganize
Dandandan 5c9cadf
Reorganize
Dandandan 24c4941
Fix
Dandandan 0a50e91
Reorganize
Dandandan 4595484
Update tests expectations
Dandandan bd83b96
Update tests expectations
Dandandan 5f05dae
Update tests expectations
Dandandan 3aa5bb1
Add CoalesceBatches / AddMergeExec as optimizers
Dandandan 2262c39
Fix tests
Dandandan beb5863
Merge remote-tracking branch 'upstream/master' into reparition-opt
Dandandan 15009ab
Docs, test
Dandandan 7aafebd
Merge remote-tracking branch 'upstream/master' into reparition-opt
Dandandan 86f8dda
Fmt
Dandandan 691b354
Exclude empty exec from repartition optimizer
Dandandan e5558d5
Add method to add physical optimizer rule as well
Dandandan c8dd45e
Merge remote-tracking branch 'upstream/master' into reparition-opt
Dandandan fb2183b
Disable rule for concurrency of 1
Dandandan bf434a9
Change to Distribution::SinglePartition
Dandandan d9c7a2c
Use derive(Debug)
Dandandan 1cad621
Revert debug implementation
Dandandan c6da67d
Merge remote-tracking branch 'upstream/master' into reparition-opt
Dandandan c674970
Merge branch 'reparition-opt' of github.com:Dandandan/arrow into repa…
Dandandan 6bf6a72
Merge remote-tracking branch 'upstream/master' into reparition-opt
Dandandan 40fc828
Merge remote-tracking branch 'upstream/master' into reparition-opt
Dandandan 2a1e53f
Fix topkexec
Dandandan 4bb4dc5
Remove print
Dandandan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
rust/datafusion/src/physical_optimizer/coalesce_batches.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // 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. | ||
|
|
||
| //! CoalesceBatches optimizer that groups batches together rows | ||
| //! in bigger batches to avoid overhead with small batches | ||
|
|
||
| use super::optimizer::PhysicalOptimizerRule; | ||
| use crate::{ | ||
| error::Result, | ||
| physical_plan::{ | ||
| coalesce_batches::CoalesceBatchesExec, filter::FilterExec, | ||
| hash_join::HashJoinExec, repartition::RepartitionExec, | ||
| }, | ||
| }; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Optimizer that introduces CoalesceBatchesExec to avoid overhead with small batches | ||
| pub struct CoalesceBatches {} | ||
|
|
||
| impl CoalesceBatches { | ||
| #[allow(missing_docs)] | ||
| pub fn new() -> Self { | ||
| Self {} | ||
| } | ||
| } | ||
| impl PhysicalOptimizerRule for CoalesceBatches { | ||
| fn optimize( | ||
| &self, | ||
| plan: Arc<dyn crate::physical_plan::ExecutionPlan>, | ||
| config: &crate::execution::context::ExecutionConfig, | ||
| ) -> Result<Arc<dyn crate::physical_plan::ExecutionPlan>> { | ||
| // wrap operators in CoalesceBatches to avoid lots of tiny batches when we have | ||
| // highly selective filters | ||
| let children = plan | ||
| .children() | ||
| .iter() | ||
| .map(|child| self.optimize(child.clone(), config)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
|
|
||
| let plan_any = plan.as_any(); | ||
| //TODO we should do this in a more generic way either by wrapping all operators | ||
Dandandan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // or having an API so that operators can declare when their inputs or outputs | ||
| // need to be wrapped in a coalesce batches operator. | ||
| // See https://issues.apache.org/jira/browse/ARROW-11068 | ||
| 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(); | ||
|
|
||
| //TODO we should also do this for HashAggregateExec but we need to update tests | ||
| // as part of this work - see https://issues.apache.org/jira/browse/ARROW-11068 | ||
| // || plan_any.downcast_ref::<HashAggregateExec>().is_some(); | ||
|
|
||
| if plan.children().is_empty() { | ||
| // leaf node, children cannot be replaced | ||
| Ok(plan.clone()) | ||
| } else { | ||
| let plan = plan.with_new_children(children)?; | ||
| Ok(if wrap_in_coalesce { | ||
| //TODO we should add specific configuration settings for coalescing batches and | ||
| // we should do that once https://issues.apache.org/jira/browse/ARROW-11059 is | ||
| // implemented. For now, we choose half the configured batch size to avoid copies | ||
| // when a small number of rows are removed from a batch | ||
| let target_batch_size = config.batch_size / 2; | ||
| Arc::new(CoalesceBatchesExec::new(plan.clone(), target_batch_size)) | ||
| } else { | ||
| plan.clone() | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "coalesce_batches" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // 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. | ||
|
|
||
| //! AddMergeExec adds MergeExec to merge plans | ||
| //! with more partitions into one partition when the node | ||
| //! needs a single partition | ||
| use super::optimizer::PhysicalOptimizerRule; | ||
| use crate::{ | ||
| error::Result, | ||
| physical_plan::{merge::MergeExec, Distribution}, | ||
| }; | ||
| use std::sync::Arc; | ||
|
|
||
| /// Introduces MergeExec | ||
| pub struct AddMergeExec {} | ||
|
|
||
| impl AddMergeExec { | ||
| #[allow(missing_docs)] | ||
| pub fn new() -> Self { | ||
| Self {} | ||
| } | ||
| } | ||
|
|
||
| impl PhysicalOptimizerRule for AddMergeExec { | ||
| fn optimize( | ||
| &self, | ||
| plan: Arc<dyn crate::physical_plan::ExecutionPlan>, | ||
| config: &crate::execution::context::ExecutionConfig, | ||
| ) -> Result<Arc<dyn crate::physical_plan::ExecutionPlan>> { | ||
| if plan.children().is_empty() { | ||
| // leaf node, children cannot be replaced | ||
| Ok(plan.clone()) | ||
| } else { | ||
| let children = plan | ||
| .children() | ||
| .iter() | ||
| .map(|child| self.optimize(child.clone(), config)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| match plan.required_child_distribution() { | ||
| Distribution::UnspecifiedDistribution => plan.with_new_children(children), | ||
| Distribution::SinglePartition => plan.with_new_children( | ||
| children | ||
| .iter() | ||
| .map(|child| { | ||
| if child.output_partitioning().partition_count() == 1 { | ||
| child.clone() | ||
| } else { | ||
| Arc::new(MergeExec::new(child.clone())) | ||
| } | ||
| }) | ||
| .collect(), | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn name(&self) -> &str { | ||
| "add_merge_exec" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // 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. | ||
|
|
||
| //! This module contains a query optimizer that operates against a physical plan and applies | ||
| //! rules to a physical plan, such as "Repartition". | ||
|
|
||
| pub mod coalesce_batches; | ||
| pub mod merge_exec; | ||
| pub mod optimizer; | ||
| pub mod repartition; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I realize you are just moving code around so this comment is outside the context of this PR....
However, I wonder if it would be more performant to do the coalescing directly in the filter kernel code -- the way coalsce is written today requires copying the the (filtered) output into a different (coalesced) array
I think @ritchie46 had some code that allowed incrementally building up output in several chunks as part of polars which may be relevant
I think this code is good, but I wanted to plant a seed 🌱 for future optimizations
Uh oh!
There was an error while loading. Please reload this page.
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.
I think that might be a useful direction indeed!
I think indeed it can be more efficient in some cases for nodes to write to mutable buffers than produce smaller batches and concatenate them afterwards, although currently it does not seem to me like it would be a enormous performance improvement based on what I saw in profiling info.
Probably not something in the scope of this PR indeed as it's already getting pretty big.
Some other notes: