Lordworms commented on code in PR #14149: URL: https://github.com/apache/datafusion/pull/14149#discussion_r1919373404
########## datafusion/physical-plan/src/sorts/row_serde.rs: ########## @@ -0,0 +1,407 @@ +// 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 arrow::row::RowConverter; +use arrow::row::Rows; +use datafusion_common::error::DataFusionError; +use datafusion_common::Result; +use std::fs::File; +use std::future::Future; +use std::io::BufWriter; +use std::io::Write; +use std::io::{BufReader, Read, Seek, SeekFrom}; +use std::path::Path; + +use std::sync::Arc; + +use tokio::sync::mpsc::Sender; + +use crate::stream::ReceiverStreamBuilder; +/// used for spill Rows +pub struct RowStreamBuilder { + inner: ReceiverStreamBuilder<Rows>, +} + +impl RowStreamBuilder { + pub fn new(capacity: usize) -> Self { + Self { + inner: ReceiverStreamBuilder::new(capacity), + } + } + + pub fn tx(&self) -> Sender<Result<Rows, DataFusionError>> { + self.inner.tx() + } + + pub fn spawn<F>(&mut self, task: F) + where + F: Future<Output = Result<(), DataFusionError>>, + F: Send + 'static, + { + self.inner.spawn(task) + } + + pub fn spawn_blocking<F>(&mut self, f: F) + where + F: FnOnce() -> Result<(), DataFusionError>, + F: Send + 'static, + { + self.inner.spawn_blocking(f) + } +} + +#[derive(Clone, Copy, Debug)] +pub enum CompressionType { + UNCOMPRESSED, + #[cfg(feature = "compress")] + GZIP, +} + +/// +----------------+------------------+----------------+------------------+ +/// | Block1 Data | Block1 Metadata | Block2 Data | Block2 Metadata | ... +/// +----------------+------------------+----------------+------------------+ +/// | FileMetadata | MetadataLength | +/// +----------------+------------------+ +#[derive(Debug)] +pub struct RowWriter { + writer: BufWriter<File>, + block_offsets: Vec<u64>, + current_offset: u64, + compression: CompressionType, +} + +impl RowWriter { + pub fn new( + path: &Path, + compression: Option<CompressionType>, + ) -> Result<Self, DataFusionError> { + let file = File::create(path).map_err(|e| { + DataFusionError::Execution(format!("Failed to create file at {path:?}: {e}")) + })?; + + Ok(Self { + writer: BufWriter::new(file), + block_offsets: Vec::new(), + current_offset: 0, + compression: compression.unwrap_or(CompressionType::UNCOMPRESSED), + }) + } + + pub fn write_rows(&mut self, rows: &Rows) -> Result<(), DataFusionError> { + self.block_offsets.push(self.current_offset); + let (row_data, row_offsets) = self.prepare_row_data(rows)?; + let compressed_data = self.compress(&row_data)?; + + self.writer.write_all(&compressed_data)?; + + self.write_block_metadata(&row_offsets)?; + + self.current_offset += + (compressed_data.len() + self.metadata_size(&row_offsets)) as u64; + + Ok(()) + } + + fn prepare_row_data( + &self, + rows: &Rows, + ) -> Result<(Vec<u8>, Vec<u32>), DataFusionError> { + let mut row_offsets = Vec::with_capacity(rows.num_rows()); + let mut current_offset = 0u32; + let mut row_data = Vec::new(); + + for i in 0..rows.num_rows() { + row_offsets.push(current_offset); + let row = rows.row(i).data(); + row_data.extend_from_slice(row); + current_offset += row.len() as u32; + } + + Ok((row_data, row_offsets)) + } + + fn write_block_metadata( + &mut self, + row_offsets: &[u32], + ) -> Result<(), DataFusionError> { + for &offset in row_offsets { + self.writer.write_all(&offset.to_le_bytes())?; + } + self.writer + .write_all(&(row_offsets.len() as u32).to_le_bytes())?; + Ok(()) + } + + fn metadata_size(&self, row_offsets: &[u32]) -> usize { + 4 + // row count + row_offsets.len() * 4 // row offsets + } + + pub fn finish(mut self) -> Result<(), DataFusionError> { Review Comment: Thanks, didn't notice that. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org