JingsongLi commented on code in PR #1520: URL: https://github.com/apache/paimon-rust/pull/1520#discussion_r4679217302
########## crates/paimon/src/table/scored_split_read.rs: ########## @@ -0,0 +1,232 @@ +// 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::{PaimonTableRead, TableRead, TableReadKind}; +use crate::spec::{row_id_data_field, ROW_ID_FIELD_ID}; +use crate::table::pk_vector_indexed_split_read::{PkVectorIndexedSplit, PkVectorIndexedSplitRead}; +use crate::table::{ArrowRecordBatchStream, DataSplit, RowRange}; +use arrow_array::{ArrayRef, Float32Array, Int64Array, RecordBatch}; +use arrow_schema::{DataType, Field, Schema}; +use futures::{stream, StreamExt}; +use std::sync::Arc; + +fn invalid(message: &str) -> crate::Error { + crate::Error::DataInvalid { + message: message.into(), + source: None, + } +} + +fn scored_batch( + batch: RecordBatch, + count: usize, + name: &str, + scores: ArrayRef, +) -> crate::Result<RecordBatch> { + let schema = batch.schema(); + let mut fields = schema.fields()[..count].to_vec(); + let mut columns = batch.columns()[..count].to_vec(); + fields.push(Arc::new(Field::new(name, DataType::Float32, true))); + columns.push(scores); + RecordBatch::try_new( + Arc::new(Schema::new_with_metadata(fields, schema.metadata().clone())), + columns, + ) + .map_err(|error| invalid(&format!("Invalid scored batch: {error}"))) +} + +impl TableRead<'_> { + /// Read one Java IndexedSplit, appending its nullable Float32 score under + /// `name`. A split without scores produces nulls. Tracked rows use global + /// row IDs; file-position selections use the same DV-aware reader as PK + /// vector search. Physical position reads require a predicate-free reader. + pub fn to_arrow_with_index_score( + &self, + split: &DataSplit, + name: &str, + ) -> crate::Result<ArrowRecordBatchStream> { + let selected = std::slice::from_ref(split); + let ranges = split + .row_ranges() + .ok_or_else(|| invalid("Score projection requires an IndexedSplit"))?; + if name.is_empty() || self.read_type().iter().any(|f| f.name() == name) { + return Err(invalid( + "Index score field name must be nonempty and distinct from the projected fields", + )); + } + let name = name.to_owned(); + let count = self.read_type().len(); + let Some(scores) = split.scores() else { + return Ok(self + .to_arrow(selected)? + .map(move |batch| { + let batch = batch?; + let scores = Arc::new(Float32Array::new_null(batch.num_rows())); + scored_batch(batch, count, &name, scores) + }) + .boxed()); + }; + if ranges.iter().any(|r| r.from() < 0) + || ranges.windows(2).any(|w| w[0].to() >= w[1].from()) Review Comment: **[P2] Preserve scores for disjoint Java ranges supplied in a different order** Java's `IndexedSplit` writes ranges in the order supplied by the caller. `IndexedSplitRecordReader` walks that order to associate each score with its row ID. I wrote a tracked Parquet table and sent raw v1 IndexedSplit bytes with ranges `[2..2, 0..1]` and their corresponding scores through the C API. Plan decoding succeeded, but enabling score projection returned `Scored ranges must be nonnegative, ordered and disjoint`. The stock Java planner emits sorted ranges, so this affects callers that construct or reorder a split before transport. Preserve each score span while sorting disjoint ranges, or look up the scores by row ID from the original input order. -- 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]
