XiaoHongbo-Hope commented on code in PR #260:
URL: https://github.com/apache/paimon-rust/pull/260#discussion_r3106493548


##########
crates/paimon/src/arrow/format/vortex.rs:
##########
@@ -0,0 +1,1303 @@
+// 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 projection expression for requested fields.
+        let projected_names: Vec<&str> = read_fields.iter().map(|f| 
f.name()).collect();
+
+        let mut scan_builder = vortex_file.scan().map_err(|e| 
Error::DataInvalid {
+            message: format!("Failed to create Vortex scan: {e}"),
+            source: None,
+        })?;
+
+        // Apply column projection.
+        if !projected_names.is_empty() {

Review Comment:
   Empty projection is a legal upstream path, e.g. DataFusion pushes 
projection=[] for SELECT COUNT(*). Here it is treated as "no projection", so 
the Vortex scan still returns actual columns, which later fails RecordBatch 
validation against the empty requested schema.



-- 
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]

Reply via email to