ryankert01 commented on code in PR #753: URL: https://github.com/apache/mahout/pull/753#discussion_r2650081339
########## qdp/qdp-core/src/readers/arrow_ipc.rs: ########## @@ -0,0 +1,166 @@ +// +// 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. + +//! Arrow IPC format reader implementation. + +use std::fs::File; +use std::path::Path; + +use arrow::array::{Array, Float64Array, FixedSizeListArray, ListArray}; +use arrow::datatypes::DataType; +use arrow::ipc::reader::FileReader as ArrowFileReader; + +use crate::error::{MahoutError, Result}; +use crate::reader::DataReader; + +/// Reader for Arrow IPC files containing FixedSizeList<Float64> or List<Float64> columns. +pub struct ArrowIPCReader { + path: std::path::PathBuf, + read: bool, +} + +impl ArrowIPCReader { + /// Create a new Arrow IPC reader. + /// + /// # Arguments + /// * `path` - Path to the Arrow IPC file (.arrow or .feather) + pub fn new<P: AsRef<Path>>(path: P) -> Result<Self> { + Ok(Self { + path: path.as_ref().to_path_buf(), + read: false, + }) + } +} + +impl DataReader for ArrowIPCReader { + fn read_batch(&mut self) -> Result<(Vec<f64>, usize, usize)> { + if self.read { + return Err(MahoutError::InvalidInput("Reader already consumed".to_string())); + } + self.read = true; + + let file = File::open(&self.path).map_err(|e| { + MahoutError::Io(format!("Failed to open Arrow IPC file: {}", e)) + })?; + + let reader = ArrowFileReader::try_new(file, None).map_err(|e| { + MahoutError::Io(format!("Failed to create Arrow IPC reader: {}", e)) + })?; + + let mut all_data = Vec::new(); + let mut num_samples = 0; + let mut sample_size: Option<usize> = None; + + for batch_result in reader { + let batch = batch_result.map_err(|e| { + MahoutError::Io(format!("Failed to read Arrow batch: {}", e)) + })?; + + if batch.num_columns() == 0 { + return Err(MahoutError::Io("Arrow file has no columns".to_string())); + } + + let column = batch.column(0); + + match column.data_type() { + DataType::FixedSizeList(_, size) => { + let list_array = column + .as_any() + .downcast_ref::<FixedSizeListArray>() + .ok_or_else(|| MahoutError::Io("Failed to downcast to FixedSizeListArray".to_string()))?; + + let current_size = *size as usize; + + if let Some(expected) = sample_size { + if current_size != expected { + return Err(MahoutError::InvalidInput(format!( + "Inconsistent sample sizes: expected {}, got {}", + expected, current_size + ))); + } + } else { + sample_size = Some(current_size); + all_data.reserve(current_size * batch.num_rows()); Review Comment: I will solve this as a quick fix -- 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]
