-
Notifications
You must be signed in to change notification settings - Fork 26
Coalesce partitions above and below network coalesce #312
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
Open
gabotechs
wants to merge
1
commit into
main
Choose a base branch
from
gabrielmusat/coalesce-partitions-below-network-coalesce
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
165 changes: 165 additions & 0 deletions
165
src/distributed_planner/coalesce_partitions_below_network_coalesce.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,165 @@ | ||
| use crate::NetworkCoalesceExec; | ||
| use crate::common::require_one_child; | ||
| use datafusion::common::DataFusionError; | ||
| use datafusion::common::tree_node::{Transformed, TreeNode}; | ||
| use datafusion::physical_plan::coalesce_partitions::CoalescePartitionsExec; | ||
| use datafusion::physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec; | ||
| use datafusion::physical_plan::{ExecutionPlan, ExecutionPlanProperties}; | ||
| use std::sync::Arc; | ||
|
|
||
| pub(crate) fn coalesce_partitions_below_network_coalesce( | ||
| plan: Arc<dyn ExecutionPlan>, | ||
| ) -> Result<Arc<dyn ExecutionPlan>, DataFusionError> { | ||
| let result = plan.transform_down(|parent| { | ||
| let Some(child) = parent.children().pop() else { | ||
| return Ok(Transformed::no(parent)); | ||
| }; | ||
|
|
||
| let Some(network_coalesce) = child.as_any().downcast_ref::<NetworkCoalesceExec>() else { | ||
| return Ok(Transformed::no(parent)); | ||
| }; | ||
|
|
||
| let network_coalesce_input = require_one_child(network_coalesce.children())?; | ||
|
|
||
| if network_coalesce_input | ||
| .output_partitioning() | ||
| .partition_count() | ||
| == 1 | ||
| { | ||
| return Ok(Transformed::no(parent)); | ||
| } | ||
|
|
||
| if let Some(sort_merge_exec) = parent.as_any().downcast_ref::<SortPreservingMergeExec>() { | ||
| let child = Arc::clone(child).with_new_children(vec![Arc::new( | ||
| SortPreservingMergeExec::new( | ||
| sort_merge_exec.expr().clone(), | ||
| require_one_child(network_coalesce.children())?, | ||
| ), | ||
| )])?; | ||
|
|
||
| let parent = parent.with_new_children(vec![child])?; | ||
|
|
||
| return Ok(Transformed::yes(parent)); | ||
| } | ||
|
|
||
| if let Some(_coalesce_exec) = parent.as_any().downcast_ref::<CoalescePartitionsExec>() { | ||
| let child = Arc::clone(child).with_new_children(vec![Arc::new( | ||
| CoalescePartitionsExec::new(require_one_child(network_coalesce.children())?), | ||
| )])?; | ||
|
|
||
| let parent = parent.with_new_children(vec![child])?; | ||
|
|
||
| return Ok(Transformed::yes(parent)); | ||
| } | ||
|
|
||
| Ok(Transformed::no(parent)) | ||
| })?; | ||
|
|
||
| Ok(result.data) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::test_utils::in_memory_channel_resolver::InMemoryWorkerResolver; | ||
| use crate::test_utils::parquet::register_parquet_tables; | ||
| use crate::{DistributedExt, DistributedPhysicalOptimizerRule}; | ||
| use datafusion::execution::SessionStateBuilder; | ||
| use datafusion::prelude::SessionContext; | ||
| use itertools::Itertools; | ||
|
|
||
| #[tokio::test] | ||
| async fn coalesce_partitions() { | ||
| let query = r#" | ||
| SELECT DISTINCT "RainToday", "WindGustDir" FROM weather | ||
| "#; | ||
| let plan = sql_to_plan(query).await; | ||
| let mut at_least_one_coalesce = false; | ||
| // No CoalesceBatchExec is placed before sending data over the network. | ||
| plan.transform_down(|plan| { | ||
| let Some(network_coalesce) = plan.as_any().downcast_ref::<NetworkCoalesceExec>() else { | ||
| return Ok(Transformed::no(plan)); | ||
| }; | ||
|
|
||
| at_least_one_coalesce = true; | ||
| let child = require_one_child(network_coalesce.children())?; | ||
| assert!(child.as_any().is::<CoalescePartitionsExec>()); | ||
|
|
||
| Ok(Transformed::no(plan)) | ||
| }) | ||
| .unwrap(); | ||
|
|
||
| assert!(at_least_one_coalesce); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn sort_merge_preserving_exec() { | ||
| let query = r#" | ||
| SELECT DISTINCT "RainToday", "WindGustDir" FROM weather ORDER BY "WindGustDir" DESC | ||
| "#; | ||
| let plan = sql_to_plan(query).await; | ||
| let mut at_least_one_coalesce = false; | ||
| // No CoalesceBatchExec is placed before sending data over the network. | ||
| plan.transform_down(|plan| { | ||
| let Some(network_coalesce) = plan.as_any().downcast_ref::<NetworkCoalesceExec>() else { | ||
| return Ok(Transformed::no(plan)); | ||
| }; | ||
|
|
||
| at_least_one_coalesce = true; | ||
| let child = require_one_child(network_coalesce.children())?; | ||
| assert!(child.as_any().is::<SortPreservingMergeExec>()); | ||
|
|
||
| Ok(Transformed::no(plan)) | ||
| }) | ||
| .unwrap(); | ||
|
|
||
| assert!(at_least_one_coalesce); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn sort_merge_preserving_exec_no_double_inject() { | ||
| let query = r#" | ||
| SELECT DISTINCT "RainToday", "WindGustDir" FROM weather ORDER BY "WindGustDir" DESC | ||
| "#; | ||
| let plan = sql_to_plan(query).await; | ||
| let plan = coalesce_partitions_below_network_coalesce(plan).unwrap(); | ||
| let mut at_least_one_coalesce = false; | ||
| // No CoalesceBatchExec is placed before sending data over the network. | ||
| plan.transform_down(|plan| { | ||
| let Some(network_coalesce) = plan.as_any().downcast_ref::<NetworkCoalesceExec>() else { | ||
| return Ok(Transformed::no(plan)); | ||
| }; | ||
|
|
||
| at_least_one_coalesce = true; | ||
| let child = require_one_child(network_coalesce.children())?; | ||
| assert!(child.as_any().is::<SortPreservingMergeExec>()); | ||
|
|
||
| let grand_child = require_one_child(child.children())?; | ||
| assert!(!grand_child.as_any().is::<SortPreservingMergeExec>()); | ||
|
|
||
| Ok(Transformed::no(plan)) | ||
| }) | ||
| .unwrap(); | ||
|
|
||
| assert!(at_least_one_coalesce); | ||
| } | ||
|
|
||
| async fn sql_to_plan(query: &str) -> Arc<dyn ExecutionPlan> { | ||
| let state = SessionStateBuilder::new() | ||
| .with_default_features() | ||
| .with_physical_optimizer_rule(Arc::new(DistributedPhysicalOptimizerRule)) | ||
| .with_distributed_worker_resolver(InMemoryWorkerResolver::new(3)) | ||
| .build(); | ||
|
|
||
| let ctx = SessionContext::new_with_state(state); | ||
| let mut queries = query.split(";").collect_vec(); | ||
| let last_query = queries.pop().unwrap(); | ||
| for query in queries { | ||
| ctx.sql(query).await.unwrap(); | ||
| } | ||
| register_parquet_tables(&ctx).await.unwrap(); | ||
| let df = ctx.sql(last_query).await.unwrap(); | ||
|
|
||
| df.create_physical_plan().await.unwrap() | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.