NoahKusaba commented on PR #12039:
URL: https://github.com/apache/arrow-rs/pull/12039#issuecomment-6996972872

   Thanks for working on this! I've been tracking this as it's been the cause 
of a longstanding issue on ballista apache/datafusion-ballista#1294.
   I had my AI battle test it to make sure it fully addresses it's needs, and 
found a couple of bugs in the process:
   
   **1. Null rows can make the output unreadable** (confirmed with a test)
   
   `compact_view_segments` only rewrites the views of non-null rows. A null row 
can still hold a long view into the original data buffers, for example after 
`nullif`, which only replaces the null buffer. Those buffers aren't written, so 
the view now points past the end of the new buffer. The reader validates every 
view, null or not, and rejects the batch:
   
   ```
   Invalid argument error: Invalid buffer slice at 5: got 360..384 but buffer 0 
has length 336
   ```
   
   To reproduce: build a `StringViewArray` of strings longer than 12 bytes, 
mark some rows null with `StringViewArray::new(views, buffers, Some(nulls))`, 
slice it, and round-trip it through `StreamWriter` with the option on.
   
   Suggested fix: write null rows as empty views (`0`).
   
   **2. The compacted buffer can be too big for other Arrow readers** (from 
reading the code)
   
   Compaction puts all of a column's strings into one buffer and allows it to 
grow to 4 GiB (`u32::MAX`). But the 
[spec](https://arrow.apache.org/docs/format/Columnar.html#variable-size-binary-view-layout)
 defines a view's offset as a *signed* 32-bit integer, so offsets above 2 GiB 
read as negative in C++, Java, PyArrow and other implementations. arrow-rs 
reads them as unsigned, so its own round trip works, which hides the problem. 
`gc()` avoids this by capping buffers at `i32::MAX` and starting a new one. 
Without this option, the writer keeps the original buffers, which arrow-rs 
normally keeps under 2 GiB.
   
   **3. Large sparse arrays aren't compacted at all** (from reading the code)
   
   The size check adds up the input data buffers, not the bytes that will be 
written. So a 100-row slice of an array whose buffers hold 5 GiB is written 
uncompacted, with all 5 GiB, even though it's the case that would benefit most.
   
   Suggested fix for 2 and 3: decide from the compacted size (the merged 
segment total), and split the output at `i32::MAX` the way `gc()` does. 
`append_variadic_buffer_counts` would need to report the same number of buffers.
   
   Also, since this fixes #7185, could the description say `Closes #7185`?


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