rich7420 commented on code in PR #1343:
URL: https://github.com/apache/mahout/pull/1343#discussion_r3334773901


##########
qdp/qdp-core/src/reader.rs:
##########
@@ -49,6 +49,15 @@ use arrow::array::{Array, Float64Array};
 
 use crate::error::Result;
 
+/// Scalar element type for [`DataReader`] output (`f32` or `f64` only).
+///
+/// Keeps f32 file data as `Vec<f32>` end-to-end once readers implement

Review Comment:
   Nit (non-blocking): the PR description calls this "sealed-by-usage", but as 
written `FloatElem` is implementable by any downstream type satisfying `Copy + 
Send + Sync + 'static` (e.g. `u32`, or `half::f16` via a feature flag). 
Harmless today since no `DataReader` impls exist for other `T`, but the doc 
reads as enforcement.
   
   Either soften the wording (e.g. `/// Currently implemented for f32 and 
f64.`) or actually seal it with a private supertrait:
   
   ```rust
   mod sealed { pub trait Sealed {} impl Sealed for f32 {} impl Sealed for f64 
{} }
   pub trait FloatElem: sealed::Sealed + Copy + Send + Sync + 'static {}
   ```



##########
qdp/qdp-core/tests/reader.rs:
##########
@@ -0,0 +1,115 @@
+//
+// 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.
+
+//! Tests for [`qdp_core::reader::DataReader`], [`StreamingDataReader`], and 
[`FloatElem`].
+
+use qdp_core::MahoutError;
+use qdp_core::Result;
+use qdp_core::reader::{DataReader, FloatElem, StreamingDataReader};
+
+struct BatchReader<T: FloatElem> {
+    data: Vec<T>,
+    num_samples: usize,
+    sample_size: usize,
+    consumed: bool,
+}
+
+impl<T: FloatElem> DataReader<T> for BatchReader<T> {
+    fn read_batch(&mut self) -> Result<(Vec<T>, usize, usize)> {
+        if self.consumed {
+            return Err(MahoutError::InvalidInput(
+                "BatchReader already consumed".to_string(),
+            ));
+        }
+        self.consumed = true;
+        Ok((self.data.clone(), self.num_samples, self.sample_size))
+    }
+}
+
+struct ChunkReader<T: FloatElem> {
+    chunks: Vec<Vec<T>>,
+    index: usize,
+    total_rows: usize,
+}
+
+impl<T: FloatElem> DataReader<T> for ChunkReader<T> {
+    fn read_batch(&mut self) -> Result<(Vec<T>, usize, usize)> {
+        Err(MahoutError::InvalidInput(
+            "ChunkReader supports streaming only".to_string(),
+        ))
+    }
+}
+
+impl<T: FloatElem> StreamingDataReader<T> for ChunkReader<T> {
+    fn read_chunk(&mut self, buffer: &mut [T]) -> Result<usize> {
+        if self.index >= self.chunks.len() {
+            return Ok(0);
+        }
+        let chunk = &self.chunks[self.index];
+        self.index += 1;
+        let n = chunk.len().min(buffer.len());
+        buffer[..n].copy_from_slice(&chunk[..n]);
+        Ok(n)
+    }
+
+    fn total_rows(&self) -> usize {
+        self.total_rows
+    }
+}
+
+#[test]
+fn data_reader_default_elem_type_is_f64() {

Review Comment:
   Nit: this test asserts behavior of `BatchReader<T>`'s float-literal 
inference, not the trait's `T = f64` default — `T` is bound here by the struct 
field, not the trait. The test would still pass if you changed the trait 
default to something else. To exercise the trait default, parameterize on the 
trait instead, e.g.:
   
   ```rust
   fn read_default<R: DataReader>(r: &mut R) -> (Vec<f64>, usize, usize) {
       r.read_batch().unwrap()
   }
   ```
   
   Either rename the test or use a helper like the above.



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