JingsongLi commented on code in PR #260: URL: https://github.com/apache/paimon-rust/pull/260#discussion_r3109361344
########## crates/paimon/src/arrow/format/vortex.rs: ########## @@ -0,0 +1,1367 @@ +// 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, FormatFileWriter}; +use crate::io::{FileRead, OutputFile}; +use crate::spec::{DataField, Datum, Predicate, PredicateOperator}; +use crate::table::{ArrowRecordBatchStream, RowRange}; +use crate::Error; +use arrow_array::RecordBatch; +use arrow_schema::SchemaRef; +use async_trait::async_trait; +use futures::future::BoxFuture; +use futures::StreamExt; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::Arc; +use vortex::array::arrow::{FromArrowArray, IntoArrowArray}; +use vortex::array::dtype::arrow::FromArrowType; +use vortex::array::dtype::DType; +use vortex::array::expr::{ + and_collect, col, eq, gt, gt_eq, is_null, lit, lt, lt_eq, not, not_eq, or_collect, Expression, +}; +use vortex::array::stream::{ArrayStreamAdapter, ArrayStreamExt}; +use vortex::array::ArrayRef; +use vortex::buffer::{Alignment, ByteBuffer}; +use vortex::error::VortexResult; +use vortex::file::{OpenOptionsSessionExt, WriteOptionsSessionExt}; +use vortex::io::{IoBuf, VortexReadAt, VortexWrite}; +use vortex::scan::selection::Selection; +use vortex::session::VortexSession; +use vortex::VortexSessionDefault; + +// --------------------------------------------------------------------------- +// Constants +// --------------------------------------------------------------------------- + +/// Maximum number of concurrent read requests for Vortex file IO. +const DEFAULT_READ_CONCURRENCY: usize = 10; + +// --------------------------------------------------------------------------- +// IO Adapters +// --------------------------------------------------------------------------- + +/// Adapts paimon's `FileRead` to Vortex's `VortexReadAt`. +struct PaimonVortexReadAt { + file_size: u64, + reader: Arc<dyn FileRead>, +} + +impl VortexReadAt for PaimonVortexReadAt { + fn uri(&self) -> Option<&Arc<str>> { + None + } + + fn concurrency(&self) -> usize { + DEFAULT_READ_CONCURRENCY + } + + fn size(&self) -> futures::future::BoxFuture<'static, VortexResult<u64>> { + let size = self.file_size; + Box::pin(async move { Ok(size) }) + } + + fn read_at( + &self, + offset: u64, + length: usize, + alignment: Alignment, + ) -> BoxFuture<'static, VortexResult<vortex::array::buffer::BufferHandle>> { + let reader = Arc::clone(&self.reader); + Box::pin(async move { + let bytes = reader + .read(offset..offset + length as u64) + .await + .map_err(|e| vortex::error::vortex_err!("paimon read error: {e}"))?; + // Zero-copy when the Bytes pointer is already aligned; falls back to copy otherwise. + let buffer = ByteBuffer::from(bytes).aligned(alignment); + Ok(vortex::array::buffer::BufferHandle::new_host(buffer)) + }) + } +} + +// --------------------------------------------------------------------------- +// VortexFormatReader +// --------------------------------------------------------------------------- + +pub(crate) struct VortexFormatReader; + +#[async_trait] +impl FormatFileReader for VortexFormatReader { + 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> { + let session = VortexSession::default(); + + let source = Arc::new(PaimonVortexReadAt { + file_size, + reader: Arc::from(reader), + }); + + let vortex_file = session + .open_options() + .with_file_size(file_size) + .open(source) + .await + .map_err(|e| Error::DataInvalid { + message: format!("Failed to open Vortex file: {e}"), + source: None, + })?; + + // Build the target Arrow schema for the projected fields. + let target_schema = crate::arrow::build_target_arrow_schema(read_fields)?; + + // Empty projection (e.g. SELECT COUNT(*)): return a single zero-column batch + // with the correct row count, matching Parquet/ORC/Avro behavior. + if read_fields.is_empty() { + let row_count = vortex_file.row_count() as usize; + let batch = RecordBatch::try_new_with_options( + target_schema, + vec![], + &arrow_array::RecordBatchOptions::new().with_row_count(Some(row_count)), Review Comment: We should consider `row_selection`. Predicates are inherently designed for coarse-grained filtering; unlike in Python, the returned data is not filtered on a per-row basis. -- 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]
