-
Notifications
You must be signed in to change notification settings - Fork 297
fix: add EmptySchemaShufflePartitioner and test from #3858 #3893
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
base: main
Are you sure you want to change the base?
Changes from all commits
8d71b65
c2a6c50
3c76d50
a9da7b6
2c7650b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| // 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 crate::metrics::ShufflePartitionerMetrics; | ||
| use crate::partitioners::ShufflePartitioner; | ||
| use crate::ShuffleBlockWriter; | ||
| use arrow::array::RecordBatch; | ||
| use arrow::datatypes::SchemaRef; | ||
| use datafusion::common::DataFusionError; | ||
| use std::fs::OpenOptions; | ||
| use std::io::{BufWriter, Seek, Write}; | ||
| use tokio::time::Instant; | ||
|
|
||
| /// A partitioner for zero-column schemas (e.g. queries where ColumnPruning removes all columns). | ||
| /// This handles shuffles for operations like COUNT(*) that produce empty-schema record batches | ||
| /// but contain a valid row count. Accumulates the total row count and writes a single | ||
| /// zero-column IPC batch to partition 0. All other partitions get empty entries in the index file. | ||
| pub(crate) struct EmptySchemaShufflePartitioner { | ||
| output_data_file: String, | ||
| output_index_file: String, | ||
| schema: SchemaRef, | ||
| shuffle_block_writer: ShuffleBlockWriter, | ||
| num_output_partitions: usize, | ||
| total_rows: usize, | ||
| metrics: ShufflePartitionerMetrics, | ||
| } | ||
|
|
||
| impl EmptySchemaShufflePartitioner { | ||
| pub(crate) fn try_new( | ||
| output_data_file: String, | ||
| output_index_file: String, | ||
| schema: SchemaRef, | ||
| num_output_partitions: usize, | ||
| metrics: ShufflePartitionerMetrics, | ||
| codec: crate::CompressionCodec, | ||
| ) -> datafusion::common::Result<Self> { | ||
| debug_assert!( | ||
| schema.fields().is_empty(), | ||
| "EmptySchemaShufflePartitioner requires a zero-column schema" | ||
| ); | ||
| let shuffle_block_writer = ShuffleBlockWriter::try_new(schema.as_ref(), codec)?; | ||
| Ok(Self { | ||
| output_data_file, | ||
| output_index_file, | ||
| schema, | ||
| shuffle_block_writer, | ||
| num_output_partitions, | ||
| total_rows: 0, | ||
| metrics, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[async_trait::async_trait] | ||
| impl ShufflePartitioner for EmptySchemaShufflePartitioner { | ||
| async fn insert_batch(&mut self, batch: RecordBatch) -> datafusion::common::Result<()> { | ||
| let start_time = Instant::now(); | ||
|
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'm starting to think if we need to wrap timings into macros and make them optional 🤔
Contributor
Author
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. Timers have cost, but in the grand scheme of Spark jobs that last hours or days, they're not the highest priority to optimize. |
||
| let num_rows = batch.num_rows(); | ||
| if num_rows > 0 { | ||
| self.total_rows += num_rows; | ||
| self.metrics.baseline.record_output(num_rows); | ||
| } | ||
| self.metrics.input_batches.add(1); | ||
| self.metrics | ||
| .baseline | ||
| .elapsed_compute() | ||
| .add_duration(start_time.elapsed()); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn shuffle_write(&mut self) -> datafusion::common::Result<()> { | ||
| let start_time = Instant::now(); | ||
|
|
||
| let output_data = OpenOptions::new() | ||
| .write(true) | ||
| .create(true) | ||
| .truncate(true) | ||
| .open(&self.output_data_file) | ||
| .map_err(|e| DataFusionError::Execution(format!("shuffle write error: {e:?}")))?; | ||
| let mut output_data = BufWriter::new(output_data); | ||
|
|
||
| // Write a single zero-column batch with the accumulated row count to partition 0 | ||
| if self.total_rows > 0 { | ||
| let batch = RecordBatch::try_new_with_options( | ||
| self.schema.clone(), | ||
| vec![], | ||
| &arrow::array::RecordBatchOptions::new().with_row_count(Some(self.total_rows)), | ||
| )?; | ||
| self.shuffle_block_writer.write_batch( | ||
| &batch, | ||
| &mut output_data, | ||
| &self.metrics.encode_time, | ||
| )?; | ||
| } | ||
|
|
||
| let mut write_timer = self.metrics.write_time.timer(); | ||
| output_data.flush()?; | ||
| let data_file_length = output_data.stream_position()?; | ||
|
|
||
| // Write index file: partition 0 spans [0, data_file_length), all others are empty | ||
| let index_file = OpenOptions::new() | ||
| .write(true) | ||
| .create(true) | ||
| .truncate(true) | ||
| .open(&self.output_index_file) | ||
| .map_err(|e| DataFusionError::Execution(format!("shuffle write error: {e:?}")))?; | ||
| let mut index_writer = BufWriter::new(index_file); | ||
| index_writer.write_all(&0i64.to_le_bytes())?; | ||
| for _ in 0..self.num_output_partitions { | ||
|
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.
Contributor
Author
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. The shuffle writer must write index entries for all target partitions, even if we're accumulating everything into a single batch in the first partition. |
||
| index_writer.write_all(&(data_file_length as i64).to_le_bytes())?; | ||
| } | ||
| index_writer.flush()?; | ||
| write_timer.stop(); | ||
|
|
||
| self.metrics | ||
| .baseline | ||
| .elapsed_compute() | ||
| .add_duration(start_time.elapsed()); | ||
| Ok(()) | ||
| } | ||
| } | ||
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.
would be useful to attach a data flow graph or something, so can figure how data transforms across shuffle phases?
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'm not sure what you have in mind for this one because this partitioner targets a very narrow type of queries. I think there are other resources to read about general Spark shuffle behavior.