sunchao commented on code in PR #27886:
URL: https://github.com/apache/datafusion/pull/27886#discussion_r4750028253


##########
datafusion/physical-plan/src/spill/lengths.rs:
##########
@@ -200,11 +200,18 @@ where
         );
     }
 
-    let array = GenericByteArray::<T>::try_new(
-        offsets,
-        bytes.values().slice_with_length(start, len),
-        lengths.nulls().cloned(),
-    )?;
+    // SAFETY: the offsets are a running sum of the lengths that ends at the
+    // last value byte (checked above), and the nulls are as long as the
+    // lengths. What is not checked is that the values are valid for `T`:
+    // they were when DataFusion spilled them, the same trust the IPC reader
+    // gets through `with_skip_validation`.
+    let array = unsafe {
+        GenericByteArray::<T>::new_unchecked(

Review Comment:
   Can we keep `try_new` for strings until the writer’s logical type is 
enforced on append and bound to the reader? `Binary` and `Utf8` encode to the 
same struct, so matching the encoded schema and final offset cannot establish 
UTF-8 validity. With a single `SpillManager` declaring `Utf8`, spilling 128 
valid `Binary` rows containing `0xff` and reading them back returns a 
`StringArray` that fails `validate_full()`. The old reader rejected it. I 
reproduced this for LZ4 and Zstd, including the large-offset types. No file 
corruption or unsafe caller is needed, and a subsequent safe `value()` can 
construct an invalid `&str`.



##########
datafusion/physical-plan/src/spill/mod.rs:
##########
@@ -2066,6 +2066,68 @@ mod tests {
         Ok(())
     }
 
+    /// DataFusion wrote its spill files itself, so reading one back must not
+    /// validate it again. String columns holding a value that is not UTF-8
+    /// only read back if no UTF-8 check ran.
+    ///
+    /// Regression test for
+    /// <https://github.com/apache/datafusion/issues/26687>.
+    #[tokio::test]
+    async fn test_spill_read_skips_validation() -> Result<()> {
+        use arrow::array::{BinaryArray, LargeBinaryArray, LargeStringArray};
+
+        // Enough rows for compressed spill files to store byte arrays as
+        // lengths, which are decoded by DataFusion rather than by the IPC
+        // reader.
+        let rows = 2 * MIN_LENGTHS_ROWS;
+        let values = || {
+            std::iter::once(b"\xa0\xa1".as_slice())
+                .chain(std::iter::repeat_n(b"valid".as_slice(), rows - 1))
+        };
+        let binary = BinaryArray::from_iter_values(values());
+        let large_binary = LargeBinaryArray::from_iter_values(values());
+        // SAFETY: the first value is deliberately not UTF-8. The arrays are
+        // only spilled and compared, never read as `str`.
+        let (utf8, large_utf8) = unsafe {
+            (
+                StringArray::new_unchecked(

Review Comment:
   [Arrow’s safety 
contract](https://github.com/apache/arrow-rs/blob/60.2.0/arrow-array/src/array/byte_array.rs#L144-L156)
 requires that `try_new` would succeed. These deliberately invalid bytes 
violate that precondition even if the test only compares buffers. With 
`arrow-array/force_validate` enabled, the constructor panics before the spill 
starts. Could we use valid strings and a test hook or the benchmark to check 
the validation path, and use valid `Binary` input for the schema-mismatch 
regression?



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to