|
19 | 19 |
|
20 | 20 | use crate::metrics::ShufflePartitionerMetrics; |
21 | 21 | use crate::partitioners::{ |
22 | | - MultiPartitionShuffleRepartitioner, ShufflePartitioner, SinglePartitionShufflePartitioner, |
| 22 | + EmptySchemaShufflePartitioner, MultiPartitionShuffleRepartitioner, ShufflePartitioner, |
| 23 | + SinglePartitionShufflePartitioner, |
23 | 24 | }; |
24 | 25 | use crate::{CometPartitioning, CompressionCodec}; |
25 | 26 | use async_trait::async_trait; |
@@ -210,6 +211,17 @@ async fn external_shuffle( |
210 | 211 | let schema = input.schema(); |
211 | 212 |
|
212 | 213 | let mut repartitioner: Box<dyn ShufflePartitioner> = match &partitioning { |
| 214 | + _ if schema.fields().is_empty() => { |
| 215 | + log::debug!("found empty schema, overriding {partitioning:?} partitioning with EmptySchemaShufflePartitioner"); |
| 216 | + Box::new(EmptySchemaShufflePartitioner::try_new( |
| 217 | + output_data_file, |
| 218 | + output_index_file, |
| 219 | + Arc::clone(&schema), |
| 220 | + partitioning.partition_count(), |
| 221 | + metrics, |
| 222 | + codec, |
| 223 | + )?) |
| 224 | + } |
213 | 225 | any if any.partition_count() == 1 => { |
214 | 226 | Box::new(SinglePartitionShufflePartitioner::try_new( |
215 | 227 | output_data_file, |
@@ -688,4 +700,160 @@ mod test { |
688 | 700 | } |
689 | 701 | total_rows |
690 | 702 | } |
| 703 | + |
| 704 | + #[test] |
| 705 | + #[cfg_attr(miri, ignore)] |
| 706 | + fn test_empty_schema_shuffle_writer() { |
| 707 | + use std::fs; |
| 708 | + use std::io::Read; |
| 709 | + |
| 710 | + let num_rows = 1000; |
| 711 | + let num_batches = 5; |
| 712 | + let num_partitions = 10; |
| 713 | + |
| 714 | + let schema = Arc::new(Schema::new(Vec::<Field>::new())); |
| 715 | + let batch = RecordBatch::try_new_with_options( |
| 716 | + Arc::clone(&schema), |
| 717 | + vec![], |
| 718 | + &arrow::array::RecordBatchOptions::new().with_row_count(Some(num_rows)), |
| 719 | + ) |
| 720 | + .unwrap(); |
| 721 | + |
| 722 | + let batches = (0..num_batches).map(|_| batch.clone()).collect::<Vec<_>>(); |
| 723 | + let partitions = &[batches]; |
| 724 | + |
| 725 | + let dir = tempfile::tempdir().unwrap(); |
| 726 | + let data_file = dir.path().join("data.out"); |
| 727 | + let index_file = dir.path().join("index.out"); |
| 728 | + |
| 729 | + let exec = ShuffleWriterExec::try_new( |
| 730 | + Arc::new(DataSourceExec::new(Arc::new( |
| 731 | + MemorySourceConfig::try_new(partitions, Arc::clone(&schema), None).unwrap(), |
| 732 | + ))), |
| 733 | + CometPartitioning::RoundRobin(num_partitions, 0), |
| 734 | + CompressionCodec::Zstd(1), |
| 735 | + data_file.to_str().unwrap().to_string(), |
| 736 | + index_file.to_str().unwrap().to_string(), |
| 737 | + false, |
| 738 | + 1024 * 1024, |
| 739 | + ) |
| 740 | + .unwrap(); |
| 741 | + |
| 742 | + let config = SessionConfig::new(); |
| 743 | + let runtime_env = Arc::new(RuntimeEnvBuilder::new().build().unwrap()); |
| 744 | + let ctx = SessionContext::new_with_config_rt(config, runtime_env); |
| 745 | + let task_ctx = ctx.task_ctx(); |
| 746 | + let stream = exec.execute(0, task_ctx).unwrap(); |
| 747 | + let rt = Runtime::new().unwrap(); |
| 748 | + rt.block_on(collect(stream)).unwrap(); |
| 749 | + |
| 750 | + // Verify data file is non-empty (contains IPC batch with row count) |
| 751 | + let mut data = Vec::new(); |
| 752 | + fs::File::open(&data_file) |
| 753 | + .unwrap() |
| 754 | + .read_to_end(&mut data) |
| 755 | + .unwrap(); |
| 756 | + assert!(!data.is_empty(), "Data file should contain IPC data"); |
| 757 | + |
| 758 | + // Verify row count survives roundtrip |
| 759 | + let total_rows = read_all_ipc_blocks(&data); |
| 760 | + assert_eq!( |
| 761 | + total_rows, |
| 762 | + num_rows * num_batches, |
| 763 | + "Row count should survive roundtrip" |
| 764 | + ); |
| 765 | + |
| 766 | + // Verify index file structure: num_partitions + 1 offsets |
| 767 | + let mut index_data = Vec::new(); |
| 768 | + fs::File::open(&index_file) |
| 769 | + .unwrap() |
| 770 | + .read_to_end(&mut index_data) |
| 771 | + .unwrap(); |
| 772 | + let expected_index_size = (num_partitions + 1) * 8; |
| 773 | + assert_eq!(index_data.len(), expected_index_size); |
| 774 | + |
| 775 | + // First offset should be 0 |
| 776 | + let first_offset = i64::from_le_bytes(index_data[0..8].try_into().unwrap()); |
| 777 | + assert_eq!(first_offset, 0); |
| 778 | + |
| 779 | + // Second offset should equal data file length (partition 0 holds all data) |
| 780 | + let data_len = data.len() as i64; |
| 781 | + let second_offset = i64::from_le_bytes(index_data[8..16].try_into().unwrap()); |
| 782 | + assert_eq!(second_offset, data_len); |
| 783 | + |
| 784 | + // All remaining offsets should equal data file length (empty partitions) |
| 785 | + for i in 2..=num_partitions { |
| 786 | + let offset = i64::from_le_bytes(index_data[i * 8..(i + 1) * 8].try_into().unwrap()); |
| 787 | + assert_eq!( |
| 788 | + offset, data_len, |
| 789 | + "Partition {i} offset should equal data length" |
| 790 | + ); |
| 791 | + } |
| 792 | + } |
| 793 | + |
| 794 | + #[test] |
| 795 | + #[cfg_attr(miri, ignore)] |
| 796 | + fn test_empty_schema_shuffle_writer_zero_rows() { |
| 797 | + use std::fs; |
| 798 | + use std::io::Read; |
| 799 | + |
| 800 | + let num_partitions = 4; |
| 801 | + |
| 802 | + let schema = Arc::new(Schema::new(Vec::<Field>::new())); |
| 803 | + let batch = RecordBatch::try_new_with_options( |
| 804 | + Arc::clone(&schema), |
| 805 | + vec![], |
| 806 | + &arrow::array::RecordBatchOptions::new().with_row_count(Some(0)), |
| 807 | + ) |
| 808 | + .unwrap(); |
| 809 | + |
| 810 | + let batches = vec![batch]; |
| 811 | + let partitions = &[batches]; |
| 812 | + |
| 813 | + let dir = tempfile::tempdir().unwrap(); |
| 814 | + let data_file = dir.path().join("data.out"); |
| 815 | + let index_file = dir.path().join("index.out"); |
| 816 | + |
| 817 | + let exec = ShuffleWriterExec::try_new( |
| 818 | + Arc::new(DataSourceExec::new(Arc::new( |
| 819 | + MemorySourceConfig::try_new(partitions, Arc::clone(&schema), None).unwrap(), |
| 820 | + ))), |
| 821 | + CometPartitioning::RoundRobin(num_partitions, 0), |
| 822 | + CompressionCodec::Zstd(1), |
| 823 | + data_file.to_str().unwrap().to_string(), |
| 824 | + index_file.to_str().unwrap().to_string(), |
| 825 | + false, |
| 826 | + 1024 * 1024, |
| 827 | + ) |
| 828 | + .unwrap(); |
| 829 | + |
| 830 | + let config = SessionConfig::new(); |
| 831 | + let runtime_env = Arc::new(RuntimeEnvBuilder::new().build().unwrap()); |
| 832 | + let ctx = SessionContext::new_with_config_rt(config, runtime_env); |
| 833 | + let task_ctx = ctx.task_ctx(); |
| 834 | + let stream = exec.execute(0, task_ctx).unwrap(); |
| 835 | + let rt = Runtime::new().unwrap(); |
| 836 | + rt.block_on(collect(stream)).unwrap(); |
| 837 | + |
| 838 | + // Data file should be empty (no rows to write) |
| 839 | + let mut data = Vec::new(); |
| 840 | + fs::File::open(&data_file) |
| 841 | + .unwrap() |
| 842 | + .read_to_end(&mut data) |
| 843 | + .unwrap(); |
| 844 | + assert!(data.is_empty(), "Data file should be empty with zero rows"); |
| 845 | + |
| 846 | + // Index file should have all-zero offsets |
| 847 | + let mut index_data = Vec::new(); |
| 848 | + fs::File::open(&index_file) |
| 849 | + .unwrap() |
| 850 | + .read_to_end(&mut index_data) |
| 851 | + .unwrap(); |
| 852 | + let expected_index_size = (num_partitions + 1) * 8; |
| 853 | + assert_eq!(index_data.len(), expected_index_size); |
| 854 | + for i in 0..=num_partitions { |
| 855 | + let offset = i64::from_le_bytes(index_data[i * 8..(i + 1) * 8].try_into().unwrap()); |
| 856 | + assert_eq!(offset, 0, "All offsets should be 0 with zero rows"); |
| 857 | + } |
| 858 | + } |
691 | 859 | } |
0 commit comments