sunchao opened a new pull request, #27886:
URL: https://github.com/apache/datafusion/pull/27886

   ## Which issue does this PR close?
   
   - Closes #26687.
   
   ## Rationale for this change
   
   DataFusion reads its own spill files back without validating them (#15454). 
#26687 reported that this stopped working in 55.0.0: 
`StreamDecoder::with_skip_validation` only applied to dictionary batches, so 
every spilled record batch was validated again, including a UTF-8 check of 
every string.
   
   That part is fixed on main. apache/arrow-rs#12094 made `StreamDecoder` honor 
the flag for record batches, and main picked it up with arrow 60.2.0 (#27594).
   
   One case is left. Compressed spill files store `Utf8`, `LargeUtf8`, `Binary` 
and `LargeBinary` columns as lengths (#26960), and the spill reader rebuilds 
them itself with `GenericByteArray::try_new`, which checks every string for 
UTF-8 again. So a `Utf8` value that is not UTF-8 reads back from an 
uncompressed spill file but fails from a `zstd` or `lz4_frame` one:
   
   ```
   Arrow error: Invalid argument error: Encountered non UTF-8 data: invalid 
utf-8 sequence of 1 bytes from index 0
   ```
   
   ## What changes are included in this PR?
   
   `lengths::decode_bytes` builds the array with 
`GenericByteArray::new_unchecked`.
   
   The offsets are still a running sum of the spilled lengths, built with the 
overflow check of `OffsetBuffer::try_from_lengths`, and the check that they end 
at the last value byte stays. The only thing no longer checked is that the 
bytes are valid for the type, which is the same trust the IPC reader gets 
through `with_skip_validation`.
   
   The `spill_io/StreamReader/read_100` benchmark, which exists to measure the 
cost of validating on read, now also runs with `lz4_frame` and `zstd`. Its 
existing uncompressed ID is unchanged.
   
   One thing to know when reviewing: before this change a corrupted compressed 
spill file could surface as a UTF-8 error on read. Now it is trusted, as an 
uncompressed spill file already is.
   
   ## What is the testing strategy for this PR?
   
   `test_spill_read_skips_validation` spills `Utf8` and `LargeUtf8` columns 
holding a value that is not UTF-8 and reads them back, for each spill 
compression. On main it fails for `lz4_frame` with the error above. It also 
covers the uncompressed case from the issue, which had no test in DataFusion.
   
   Benchmark: `cargo bench -p datafusion-physical-plan --features test_utils 
--bench spill_io -- spill_io/StreamReader`, medians of two runs (main first, 
then this branch first), on an AMD Ryzen 9 9950X3D (Linux under WSL2):
   
   | `spill_io/StreamReader/read_100` | main | this PR | main / this PR |
   | --- | --- | --- | --- |
   | uncompressed (not affected) | 51.1, 51.4 ms | 52.5, 50.0 ms | 0.97x, 1.03x 
|
   | `lz4_frame` | 26.6, 29.6 ms | 26.0, 25.7 ms | 1.02x, 1.15x |
   | `zstd` | 21.5, 20.8 ms | 20.9, 19.9 ms | 1.03x, 1.04x |
   
   The benchmark reads from disk and the uncompressed case, which this PR does 
not touch, moves by 3% between runs. So the gain is a few percent at most here; 
the point of the change is that compressed spill files are read back the way 
uncompressed ones are.
   
   ## Are there any user-facing changes?
   
   No.
   
   ---
   
   Written with AI assistance (Claude Code).
   


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