-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add spilling to RepartitionExec #18014
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a52c473
initial implementation of spilling RepartitionExec
adriangb 4ec4fca
add spilling to RepartitionExec
adriangb 200eeec
lint
adriangb e3b19b9
fix
adriangb 760db1d
add e2e test
adriangb 6e01420
fix typo
adriangb 85d9241
Update datafusion/core/tests/memory_limit/repartition_mem_limit.rs
adriangb 8f81a36
handle empty batches, add note about expect
adriangb f5af078
address pr feedback
adriangb 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
116 changes: 116 additions & 0 deletions
116
datafusion/core/tests/memory_limit/repartition_mem_limit.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,116 @@ | ||||||||
| // 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 std::sync::Arc; | ||||||||
|
|
||||||||
| use arrow::array::{ArrayRef, Int32Array, RecordBatch}; | ||||||||
| use datafusion::{ | ||||||||
| assert_batches_sorted_eq, | ||||||||
| prelude::{SessionConfig, SessionContext}, | ||||||||
| }; | ||||||||
| use datafusion_catalog::MemTable; | ||||||||
| use datafusion_common::tree_node::{Transformed, TreeNode}; | ||||||||
| use datafusion_execution::runtime_env::RuntimeEnvBuilder; | ||||||||
| use datafusion_physical_plan::{repartition::RepartitionExec, ExecutionPlanProperties}; | ||||||||
| use futures::TryStreamExt; | ||||||||
| use itertools::Itertools; | ||||||||
|
|
||||||||
| /// End to end test for spilling in RepartitionExec. | ||||||||
| /// The idea is to make a real world query with a relatively low memory limit and | ||||||||
| /// then drive one partition at a time, simulating dissimilar execution speed in partitions. | ||||||||
| /// Just as some examples of real world scenarios where this can happen consider | ||||||||
| /// lopsided groups in a group by especially if one partitions spills and others don't, | ||||||||
| /// or in distributed systems if one upstream node is slower than others. | ||||||||
| #[tokio::test] | ||||||||
| async fn test_repartition_memory_limit() { | ||||||||
| let runtime = RuntimeEnvBuilder::new() | ||||||||
| .with_memory_limit(1024 * 1024, 1.0) | ||||||||
| .build() | ||||||||
| .unwrap(); | ||||||||
| let config = SessionConfig::new() | ||||||||
| .with_batch_size(32) | ||||||||
| .with_target_partitions(2); | ||||||||
| let ctx = SessionContext::new_with_config_rt(config, Arc::new(runtime)); | ||||||||
| let batches = vec![RecordBatch::try_from_iter(vec![( | ||||||||
| "c1", | ||||||||
| Arc::new(Int32Array::from_iter_values((0..10).cycle().take(100_000))) as ArrayRef, | ||||||||
| )]) | ||||||||
| .unwrap()]; | ||||||||
| let table = Arc::new(MemTable::try_new(batches[0].schema(), vec![batches]).unwrap()); | ||||||||
| ctx.register_table("t", table).unwrap(); | ||||||||
| let plan = ctx | ||||||||
| .state() | ||||||||
| .create_logical_plan("SELECT c1, count(*) as c FROM t GROUP BY c1;") | ||||||||
| .await | ||||||||
| .unwrap(); | ||||||||
| let plan = ctx.state().create_physical_plan(&plan).await.unwrap(); | ||||||||
|
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. I recommend also verifying here that there are exactly two partitions in the final plan
Suggested change
|
||||||||
| assert_eq!(plan.output_partitioning().partition_count(), 2); | ||||||||
| // Execute partition 0, this should cause items going into the rest of the partitions to queue up and because | ||||||||
| // of the low memory limit should spill to disk. | ||||||||
| let batches0 = Arc::clone(&plan) | ||||||||
| .execute(0, ctx.task_ctx()) | ||||||||
| .unwrap() | ||||||||
| .try_collect::<Vec<_>>() | ||||||||
| .await | ||||||||
| .unwrap(); | ||||||||
|
|
||||||||
| let mut metrics = None; | ||||||||
| Arc::clone(&plan) | ||||||||
| .transform_down(|node| { | ||||||||
| if node.as_any().is::<RepartitionExec>() { | ||||||||
| metrics = node.metrics(); | ||||||||
| } | ||||||||
| Ok(Transformed::no(node)) | ||||||||
| }) | ||||||||
| .unwrap(); | ||||||||
|
|
||||||||
| let metrics = metrics.unwrap(); | ||||||||
| assert!(metrics.spilled_bytes().unwrap() > 0); | ||||||||
| assert!(metrics.spilled_rows().unwrap() > 0); | ||||||||
| assert!(metrics.spill_count().unwrap() > 0); | ||||||||
|
|
||||||||
| // Execute the other partition | ||||||||
| let batches1 = Arc::clone(&plan) | ||||||||
| .execute(1, ctx.task_ctx()) | ||||||||
| .unwrap() | ||||||||
| .try_collect::<Vec<_>>() | ||||||||
| .await | ||||||||
| .unwrap(); | ||||||||
|
|
||||||||
| let all_batches = batches0 | ||||||||
| .into_iter() | ||||||||
| .chain(batches1.into_iter()) | ||||||||
| .collect_vec(); | ||||||||
| #[rustfmt::skip] | ||||||||
| let expected = &[ | ||||||||
| "+----+-------+", | ||||||||
| "| c1 | c |", | ||||||||
| "+----+-------+", | ||||||||
| "| 0 | 10000 |", | ||||||||
| "| 1 | 10000 |", | ||||||||
| "| 2 | 10000 |", | ||||||||
| "| 3 | 10000 |", | ||||||||
| "| 4 | 10000 |", | ||||||||
| "| 5 | 10000 |", | ||||||||
| "| 6 | 10000 |", | ||||||||
| "| 7 | 10000 |", | ||||||||
| "| 8 | 10000 |", | ||||||||
| "| 9 | 10000 |", | ||||||||
| "+----+-------+", | ||||||||
| ]; | ||||||||
| assert_batches_sorted_eq!(expected, &all_batches); | ||||||||
| } | ||||||||
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.
This is a neat test. However, for some reason it takes over 5 seconds to run on laptop.
I suspect it is because it is creating something like 1M / 32 = 31,250 files
Is there any way to we can reduce the runtime (like maybe only use 1000 rows, or 10,000 rows?)
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.
Done, takes 0.62s on my laptop now