JingsongLi commented on code in PR #259: URL: https://github.com/apache/paimon-rust/pull/259#discussion_r3098594546
########## crates/paimon/src/arrow/format/blob.rs: ########## @@ -0,0 +1,748 @@ +// 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 super::{FilePredicates, FormatFileReader}; +use crate::arrow::build_target_arrow_schema; +use crate::io::FileRead; +use crate::spec::{DataField, DataType}; +use crate::table::{ArrowRecordBatchStream, RowRange}; +use crate::Error; +use arrow_array::builder::BinaryBuilder; +use arrow_array::{ArrayRef, RecordBatch, RecordBatchOptions}; +use async_stream::try_stream; +use async_trait::async_trait; +use futures::StreamExt; +use std::ops::Range; +use std::sync::Arc; + +pub(crate) struct BlobFormatReader; + +const BLOB_FOOTER_SIZE: u64 = 5; +const BLOB_FORMAT_VERSION: u8 = 1; +const BLOB_INLINE_HEADER_SIZE: u64 = 4; +const BLOB_TRAILER_SIZE: u64 = 12; +const BLOB_ENTRY_OVERHEAD: u64 = BLOB_INLINE_HEADER_SIZE + BLOB_TRAILER_SIZE; +const DEFAULT_BATCH_SIZE: usize = 128; + +#[async_trait] +impl FormatFileReader for BlobFormatReader { + async fn read_batch_stream( + &self, + reader: Box<dyn FileRead>, + file_size: u64, + read_fields: &[DataField], + _predicates: Option<&FilePredicates>, + batch_size: Option<usize>, + row_selection: Option<Vec<RowRange>>, + ) -> crate::Result<ArrowRecordBatchStream> { + validate_read_fields(read_fields)?; + + let target_schema = build_target_arrow_schema(read_fields)?; + let batch_size = batch_size.unwrap_or(DEFAULT_BATCH_SIZE); + let blob_index = BlobFileIndex::load(reader.as_ref(), file_size).await?; + let mut selection = RowSelectionCursor::new(blob_index.num_rows(), row_selection)?; + let project_values = !read_fields.is_empty(); + + Ok(try_stream! { + while let Some(positions) = selection.next_batch(batch_size) { + let batch = read_blob_batch( + reader.as_ref(), + &blob_index, + &target_schema, + &positions, + project_values, + ).await?; + yield batch; + } + } + .boxed()) + } +} + +fn validate_read_fields(read_fields: &[DataField]) -> crate::Result<()> { + if read_fields.len() > 1 { + return Err(Error::DataInvalid { + message: format!( + ".blob format only supports reading at most one projected column, got {}", + read_fields.len() + ), + source: None, + }); + } + + if let Some(field) = read_fields.first() { + match field.data_type() { + DataType::Blob(_) => Ok(()), + other => Err(Error::DataInvalid { + message: format!( + ".blob format requires a Blob field, got {:?} for column '{}'", + other, + field.name() + ), + source: None, + }), + }?; + } + + Ok(()) +} + +async fn read_blob_batch( + reader: &dyn FileRead, + blob_index: &BlobFileIndex, + target_schema: &Arc<arrow_schema::Schema>, + positions: &[usize], + project_values: bool, +) -> crate::Result<RecordBatch> { + if !project_values { + return RecordBatch::try_new_with_options( + target_schema.clone(), + Vec::new(), + &RecordBatchOptions::new().with_row_count(Some(positions.len())), + ) + .map_err(|e| Error::UnexpectedError { + message: format!("Failed to build empty blob RecordBatch: {e}"), + source: Some(Box::new(e)), + }); + } + + let mut builder = BinaryBuilder::new(); + for &position in positions { Review Comment: Is it possible to read blobs in parallel? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
