You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2025/01/10 13:58:54 UTC

[I] Optimized spill file format [datafusion]

alamb opened a new issue, #14078:
URL: https://github.com/apache/datafusion/issues/14078

   ### Is your feature request related to a problem or challenge?
   
   DataFusion spills data to local disk for processing datasets that do not fit in available memory, as illustrated in this comment:
   https://github.com/apache/datafusion/blob/918ac1b30be026e1276d638a0dbc10002627e9d7/datafusion/physical-plan/src/sorts/sort.rs#L88-L205
   
   Here is the [code that handles spilling in sort and hash aggregates](https://github.com/apache/datafusion/blob/918ac1b30be026e1276d638a0dbc10002627e9d7/datafusion/physical-plan/src/spill.rs#L93-L110).
   
   The current version of DataFusion spills data to disk using the Arrow IPC format, which is correct and was easy to get working, but comes with non trivial overhead, as @andygrove  found in Comet:
   - https://github.com/apache/datafusion-comet/issues/1189
   
   Some potential sources of overhead are due to the validation applied to arrow IPC files (which may in general have come from untrusted sources) which is unecessary if the data was valid when written by DataFusion
   
   ### Describe the solution you'd like
   
   As part of improving DataFuison's performance of larger than memory datasets, I would like to consider adding a new optimized serialization format for use in spill files. This is similar to how we have added optimized (non Arrow) in memory storage formats for intermediate results in hash aggregation and others
   
   At a high level this would look like:
   - [ ] Add a benchmark for spilling files (maybe write/read data to/from a spill file of various sizes)
   - [ ] Add an optimized Reader/Writer
   - [ ] Update the SortExec and GroupByHashExec to use this new Reader/Writer
   
   ### Describe alternatives you've considered
   
   # Add a customized Reader/Writer
   @andygrove has a PR to add a customized BatchReader to comet that seems to offer significant performance improvements for shuffle reading/writing: 
   - https://github.com/apache/datafusion-comet/pull/1190
   
   We could potentially upstream this code into DataFusion
   
   
   # Optimize the Arrow IPC reader 
   Another option  would be to continue using the Arrow IPC format, but disable validation:
   - https://github.com/apache/arrow-rs/issues/3287
   -  https://github.com/apache/arrow-rs/issues/6933
   
   
   @totoroyyb actually has a recently PR to arrow-rs proposing this approach:
   - https://github.com/apache/arrow-rs/pull/6938
   
   ### Additional context
   
   _No response_


-- 
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: github-unsubscribe@datafusion.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2585193813

   @2010YOUY01  the idea of avoiding Row->Column->Row conversions is a (very) good one


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2585193584

   > I feel I ought to point out though that in order for it to be sound to read a file without validation, DF needs to be sure nobody else could have written/modified it.
   
   In my opinion, DataFusion itself should not go to any significant trouble / effort to protect against the threat model of someone having enough control over the local file system to make arbitrary changes to spill files.  If an adversary already has access to the local file system, the amount of extra safety gained by validating spill files is pretty small in my opinion.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2689518371

   - BTW https://github.com/apache/arrow-rs/pull/7120 is complete so will be able to disable validation for spill files with the next arrow release


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "ding-young (via GitHub)" <gi...@apache.org>.
ding-young commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-3051639268

   It seems like comet has removed their customed BatchReader/Writer (see [PR#1703](https://github.com/apache/datafusion-comet/pull/1703/files)).  


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2836303429

   20% better -- not bad!


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2585169856

   Spilling the row format makes some sense to me, although I suspect IPC will outperform it, presuming a fast enough disk.
   
   I feel I ought to point out though that in order for it to be sound to read a file without validation, DF needs to be sure nobody else could have written/modified it. This may be possible on Unix OSes using some shenanigans with unlinked file descriptors, but I suspect isn't generally possible.
   
   I feel I also ought to point out the mmap use-case is slightly different, as it is effectively in memory already, the performance benefit of skipping IO may be lessened when there are other overheads, e.g. reading the data from disk.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2804443324

   https://www.cs.cit.tum.de/fileadmin/w00cfj/dis/papers/umami.pdf
   Here are some advanced German techniques: this paper discussed  
   1. Implementation of a hash-based spilling operator  
   2. Its spill file format.  
   
   I think the spill format-related optimization is applicable. Here is a TLDR of the idea.
   
   ## Tiered encoding/compression
   
   The optimized spill format supports several encoding/compression schemes with different CPU/IO tradeoffs. On one extreme of the spectrum, we get very little CPU overhead but worse compression ratio; while on the other extreme, we get compression implementations with better compression ratio but higher CPU overhead. For example:
   
   ```text
   tier0 - Plain encoding (fast to encode, larger file size)  
   tier1 - Plain encoding + LZ4 (medium encoding speed, medium compression ratio)  
   tier2 - REE/Dictionary encoding + ZSTD (slow encoding speed, high compression ratio)
   ```
   
   ## Spilling operator integration
   
   The configuration for the spilling format supports `auto` mode in addition to all available tiers. In this case, the operator will use the default scheme for the first couple of batches and collect related metrics (compute time, IO time):
   
   - When compute time >> IO time (e.g., running on a machine with fast SSDs)  
       - CPU-bound: choose light encoding scheme (tier0)
   - When compute time << IO time (e.g., running on a machine with spinning disks or with noisy neighbors that consume lots of IO bandwidth)  
       - IO-bound: choose heavier encoding scheme (tier2)
   
   The paper refers to it as `self-regulating compression`, and we don't have to manually set this option in average cases.  
   
   The tricky part to implement is array encoding like REE or bit-packing for integer arrays. Maybe we can find some reusable code in Arrow Parquet writer implementation or use something like https://github.com/spiraldb/vortex. But it's okay to start without those encodings.
   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-3052679388

   > It seems like comet has removed their customed BatchReader/Writer and switched back to arrow IPC reader/writer (see [PR#1703](https://github.com/apache/datafusion-comet/pull/1703/files)).
   
   So shall we close this issue as complete now?


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "getChan (via GitHub)" <gi...@apache.org>.
getChan commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2829344168

   update : skip validation is applied when reading spill files. by #15454 


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "2010YOUY01 (via GitHub)" <gi...@apache.org>.
2010YOUY01 commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2585043689

   Although we're currently spilling column-wise record batches, I think this will change to row-wise batches in the future. It would be better to benchmark and optimize spilling the Arrow Row format in this issue as well.
   
   The reason is that the spilling operation involves sorting, spilling sorted runs, and reading back those runs for merging. Both sorting and merging benefit from the row format. The current implementation performs several unnecessary conversions between row and column formats, which could become inefficient.
   The preferred way should be:
   1. Convert to row format and do sorting
   2. Maintaining the row format until the final output (I believe `Sort` will benefit more from it, because it will do 2 phase or merging, `Aggregate` will only do 1 phase)
   This is tracked by https://github.com/apache/datafusion/issues/7053


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2584278401

   As a data point, @totoroyyb  reports a 100x faster reading of Arrow IPC data without validation on https://github.com/apache/arrow-rs/issues/6933


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2585752275

   FYI I am working with @@totoroyyb on the arrow IPC work, in case anyone is interested or has time to help:
   - https://github.com/apache/arrow-rs/pull/6938#issuecomment-2585751118


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "andygrove (via GitHub)" <gi...@apache.org>.
andygrove commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2585364461

   > It would be nice to have the option (likely not enabled by default) for the spill files to be compressed. It's almost trivial I think with the current implementation.
   
   We support this in Comet with the Arrow IPC Writer and it is trivial. We support LZ4, Snappy, and ZSTD.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2585194217

   > DataFusion itself should not go to any significant trouble / effort to protect against the threat model of someone having enough control over the local file system to make arbitrary changes to spill files
   
   Agreed, the more reasonable scenario would be a bug in DF causes it to trample its files, maybe some sort of concurrency bug where it opens the spill file before it finishes writing it. TBC I am not making a judgement of whether or not DF should care about this, I am merely stating that what is being proposed is unsound. 


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "Omega359 (via GitHub)" <gi...@apache.org>.
Omega359 commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2585363202

   It would be nice to have the option (likely not enabled by default) for the spill files to be compressed. It's almost trivial I think with the current implementation.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-2810394103

   > The tricky part to implement is array encoding like REE or bit-packing for integer arrays. Maybe we can find some reusable code in Arrow Parquet writer implementation or use something like https://github.com/spiraldb/vortex. But it's okay to start without those encodings.
   
   I think we might get pretty far by simply compressing the arrow stream zstd or snappy  (which would be far simpler to implement as the arrow writer already supports this)
   
   


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-3063722047

   Thanks again @ding-young 


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-3063721375

   > Yes, I think so. Of course, there's still room to seek further performance optimizations, but for now:
   
   
   Indeed -we can always make the code better :)


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb closed issue #14078: Optimized spill file format
URL: https://github.com/apache/datafusion/issues/14078


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [I] Optimized spill file format [datafusion]

Posted by "ding-young (via GitHub)" <gi...@apache.org>.
ding-young commented on issue #14078:
URL: https://github.com/apache/datafusion/issues/14078#issuecomment-3059889330

   > So shall we close this issue as complete now?
   
   Yes, I think so. Of course, there's still room to seek further performance optimizations, but for now:
   
   - Validation skipping is supported,
   - more benchmarks for spill and compression support have been added, and
   - Based on Comet’s case, introducing a custom reader/writer or adopting a completely new format instead of Arrow IPC doesn’t seem like a priority at this point.
   
   So I think it makes sense to close this issue for now.
   
   In the future, potential areas to explore might include:
   
   - Row conversion during sorting,
   - Tuning how many batches we accumulate before spilling (as long as it doesn’t put too much memory pressure), and
   - Maybe supporting compression level tuning or adaptive compression. (what @2010YOUY01 mentioned)
   
   We can always open separate issues for those if/when needed.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org