-
Notifications
You must be signed in to change notification settings - Fork 282
feat: Add support for round-robin partitioning in native shuffle #3076
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
28 commits
Select commit
Hold shift + click to select a range
64546cb
experimental support for round-robin partitioning in native shuffle
andygrove 2255474
clippy
andygrove c018c84
prettier
andygrove d09650d
Merge remote-tracking branch 'apache/main' into round-robin-native
andygrove 35c7ed6
prettier
andygrove 8cfe107
prettier
andygrove 808d083
implement murmur3 hashing for complex types
andygrove ad99745
tests
andygrove 6359d7f
save
andygrove 704cb9b
save
andygrove 517f36f
new approach
andygrove 6734513
format
andygrove 3c87ebc
prep for review
andygrove 1fb3cc6
add config for max columns
andygrove 9955943
format code
andygrove d5cd1af
fix regression
andygrove b35e402
fix regression
andygrove b40caa6
upmerge
andygrove de575ac
fix merge conflict
andygrove ec1e7b1
format
andygrove cd6c5b8
fix merge conflict
andygrove 4d5af6c
update docs
andygrove 3f72eeb
Update docs/source/contributor-guide/jvm_shuffle.md
andygrove d886068
address review feedback for round-robin partitioning
andygrove 1202acd
prettier
andygrove 7990528
upmerge
andygrove f63118b
revert benchmark changes
andygrove bb6e3c0
address feedback
andygrove 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -382,8 +382,11 @@ impl MultiPartitionShuffleRepartitioner { | |
| // The initial values are not used. | ||
| let scratch = ScratchSpace { | ||
| hashes_buf: match partitioning { | ||
| // Only allocate the hashes_buf if hash partitioning. | ||
| CometPartitioning::Hash(_, _) => vec![0; batch_size], | ||
| // Allocate hashes_buf for hash and round robin partitioning. | ||
| // Round robin hashes all columns to achieve even, deterministic distribution. | ||
| CometPartitioning::Hash(_, _) | CometPartitioning::RoundRobin(_, _) => { | ||
| vec![0; batch_size] | ||
| } | ||
| _ => vec![], | ||
| }, | ||
| partition_ids: vec![0; batch_size], | ||
|
|
@@ -598,6 +601,68 @@ impl MultiPartitionShuffleRepartitioner { | |
| .await?; | ||
| self.scratch = scratch; | ||
| } | ||
| CometPartitioning::RoundRobin(num_output_partitions, max_hash_columns) => { | ||
| // Comet implements "round robin" as hash partitioning on columns. | ||
| // This achieves the same goal as Spark's round robin (even distribution | ||
| // without semantic grouping) while being deterministic for fault tolerance. | ||
| // | ||
| // Note: This produces different partition assignments than Spark's round robin, | ||
| // which sorts by UnsafeRow binary representation before assigning partitions. | ||
| // However, both approaches provide even distribution and determinism. | ||
| let mut scratch = std::mem::take(&mut self.scratch); | ||
| let (partition_starts, partition_row_indices): (&Vec<u32>, &Vec<u32>) = { | ||
| let mut timer = self.metrics.repart_time.timer(); | ||
|
|
||
| let num_rows = input.num_rows(); | ||
|
|
||
| // Collect columns for hashing, respecting max_hash_columns limit | ||
| // max_hash_columns of 0 means no limit (hash all columns) | ||
| // Negative values are normalized to 0 in the planner | ||
| let num_columns_to_hash = if *max_hash_columns == 0 { | ||
| input.num_columns() | ||
| } else { | ||
| (*max_hash_columns).min(input.num_columns()) | ||
|
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. we need to treat properly negative values |
||
| }; | ||
| let columns_to_hash: Vec<ArrayRef> = (0..num_columns_to_hash) | ||
| .map(|i| Arc::clone(input.column(i))) | ||
| .collect(); | ||
|
|
||
| // Use identical seed as Spark hash partitioning. | ||
| let hashes_buf = &mut scratch.hashes_buf[..num_rows]; | ||
| hashes_buf.fill(42_u32); | ||
|
|
||
| // Compute hash for selected columns | ||
| create_murmur3_hashes(&columns_to_hash, hashes_buf)?; | ||
|
|
||
| // Assign partition IDs based on hash (same as hash partitioning) | ||
| let partition_ids = &mut scratch.partition_ids[..num_rows]; | ||
| hashes_buf.iter().enumerate().for_each(|(idx, hash)| { | ||
| partition_ids[idx] = pmod(*hash, *num_output_partitions) as u32; | ||
| }); | ||
|
|
||
| // We now have partition ids for every input row, map that to partition starts | ||
| // and partition indices to eventually write these rows to partition buffers. | ||
| map_partition_ids_to_starts_and_indices( | ||
| &mut scratch, | ||
| *num_output_partitions, | ||
| num_rows, | ||
| ); | ||
|
|
||
| timer.stop(); | ||
| Ok::<(&Vec<u32>, &Vec<u32>), DataFusionError>(( | ||
| &scratch.partition_starts, | ||
| &scratch.partition_row_indices, | ||
| )) | ||
| }?; | ||
|
|
||
| self.buffer_partitioned_batch_may_spill( | ||
| input, | ||
| partition_row_indices, | ||
| partition_starts, | ||
| ) | ||
| .await?; | ||
| self.scratch = scratch; | ||
| } | ||
| other => { | ||
| // this should be unreachable as long as the validation logic | ||
| // in the constructor is kept up-to-date | ||
|
|
@@ -1431,6 +1496,7 @@ mod test { | |
| Arc::new(row_converter), | ||
| owned_rows, | ||
| ), | ||
| CometPartitioning::RoundRobin(num_partitions, 0), | ||
| ] { | ||
| let batches = (0..num_batches).map(|_| batch.clone()).collect::<Vec<_>>(); | ||
|
|
||
|
|
@@ -1483,4 +1549,95 @@ mod test { | |
| let expected = vec![69, 5, 193, 171, 115]; | ||
| assert_eq!(result, expected); | ||
| } | ||
|
|
||
| #[test] | ||
| #[cfg_attr(miri, ignore)] | ||
| fn test_round_robin_deterministic() { | ||
| // Test that round robin partitioning produces identical results when run multiple times | ||
| use std::fs; | ||
| use std::io::Read; | ||
|
|
||
| let batch_size = 1000; | ||
| let num_batches = 10; | ||
| let num_partitions = 8; | ||
|
|
||
| let batch = create_batch(batch_size); | ||
| let batches = (0..num_batches).map(|_| batch.clone()).collect::<Vec<_>>(); | ||
|
|
||
| // Run shuffle twice and compare results | ||
| for run in 0..2 { | ||
| let data_file = format!("/tmp/rr_data_{}.out", run); | ||
| let index_file = format!("/tmp/rr_index_{}.out", run); | ||
|
|
||
| let partitions = std::slice::from_ref(&batches); | ||
| let exec = ShuffleWriterExec::try_new( | ||
| Arc::new(DataSourceExec::new(Arc::new( | ||
| MemorySourceConfig::try_new(partitions, batch.schema(), None).unwrap(), | ||
| ))), | ||
| CometPartitioning::RoundRobin(num_partitions, 0), | ||
| CompressionCodec::Zstd(1), | ||
| data_file.clone(), | ||
| index_file.clone(), | ||
| false, | ||
| 1024 * 1024, | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| let config = SessionConfig::new(); | ||
| let runtime_env = Arc::new( | ||
| RuntimeEnvBuilder::new() | ||
| .with_memory_limit(10 * 1024 * 1024, 1.0) | ||
| .build() | ||
| .unwrap(), | ||
| ); | ||
| let session_ctx = Arc::new(SessionContext::new_with_config_rt(config, runtime_env)); | ||
| let task_ctx = Arc::new(TaskContext::from(session_ctx.as_ref())); | ||
|
|
||
| // Execute the shuffle | ||
| futures::executor::block_on(async { | ||
| let mut stream = exec.execute(0, Arc::clone(&task_ctx)).unwrap(); | ||
| while stream.next().await.is_some() {} | ||
| }); | ||
|
|
||
| if run == 1 { | ||
| // Compare data files | ||
| let mut data0 = Vec::new(); | ||
| fs::File::open("/tmp/rr_data_0.out") | ||
| .unwrap() | ||
| .read_to_end(&mut data0) | ||
| .unwrap(); | ||
| let mut data1 = Vec::new(); | ||
| fs::File::open("/tmp/rr_data_1.out") | ||
| .unwrap() | ||
| .read_to_end(&mut data1) | ||
| .unwrap(); | ||
| assert_eq!( | ||
| data0, data1, | ||
| "Round robin shuffle data should be identical across runs" | ||
| ); | ||
|
|
||
| // Compare index files | ||
| let mut index0 = Vec::new(); | ||
| fs::File::open("/tmp/rr_index_0.out") | ||
| .unwrap() | ||
| .read_to_end(&mut index0) | ||
| .unwrap(); | ||
| let mut index1 = Vec::new(); | ||
| fs::File::open("/tmp/rr_index_1.out") | ||
| .unwrap() | ||
| .read_to_end(&mut index1) | ||
| .unwrap(); | ||
| assert_eq!( | ||
| index0, index1, | ||
| "Round robin shuffle index should be identical across runs" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Clean up | ||
| let _ = fs::remove_file("/tmp/rr_data_0.out"); | ||
| let _ = fs::remove_file("/tmp/rr_index_0.out"); | ||
| let _ = fs::remove_file("/tmp/rr_data_1.out"); | ||
| let _ = fs::remove_file("/tmp/rr_index_1.out"); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
add checkValue:
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.
Added