2010YOUY01 opened a new pull request, #14823: URL: https://github.com/apache/datafusion/pull/14823
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> Follow up to https://github.com/apache/datafusion/pull/14644, this PR fixes an unsolved failing case for external sort. It's found by https://github.com/apache/datafusion/issues/12136#issuecomment-2656449956. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> Recap for major steps for external sort (there are detailed doc in datafusion/physical-plan/src/sorts/sort.rs) 1. Inside each partition, input batches will be buffered until it reaches the memory limit. 2. When OOM is triggered, all buffered batches will be sorted one by one, and finally merged into one large sorted run (though physically it's chunked into many small batches) 3. Spill the sorted run to disk. After reading all inputs, read back spilled batches and merge into the final result. Since only one batch is needed for each spilled sorted run to generate the final result, the memory overhead is reduced in this step. The problem is: if step 2 is done on batches with `StringViewArray` columns, the sorted array will only reorder the `view`s (string prefix plus pointer into range in the payload buffer, if this element is long), and the underlying buffers won't be moved. When reading back spilled batches from a single sorted run, it's required to read one batch by one batch, otherwise memory pressure won't be reduced. As a result, the spill writer has to write all referenced `buffer`s for each batch, and many buffers will be written multiple times. The size of the spilled batch would explode, and some memory-limited sort query can fail due to this reason. ## Reproducer Under `benchmarks/`, run ``` cargo run --profile release-nonlto --bin dfbench -- sort-tpch -p '/Users/yongting/Code/datafusion/benchmarks/data/tpch_sf10' -o '/tmp/main.json' -q 10 --iterations 1 --partitions 4 --memory-limit 5000M --debug ``` ### Main branch result (20GB spilled) ``` SortExec: expr=[l_orderkey@0 ASC NULLS LAST, l_suppkey@1 ASC NULLS LAST, l_linenumber@2 ASC NULLS LAST, l_comment@3 ASC NULLS LAST], preserve_partitioning=[true], metrics=[output_rows=59986052, elapsed_compute=12.965460651s, spill_count=24, spilled_bytes=19472334146, spilled_rows=51202072] ``` Q10 iteration 0 took 12839.6 ms and returned 59986052 rows Q10 avg time: 12839.60 ms ### PR result (12GB spilled) ``` SortExec: expr=[l_orderkey@0 ASC NULLS LAST, l_suppkey@1 ASC NULLS LAST, l_linenumber@2 ASC NULLS LAST, l_comment@3 ASC NULLS LAST], preserve_partitioning=[true], metrics=[output_rows=59986052, elapsed_compute=15.363745389s, spill_count=48, spilled_bytes=12600523712, spilled_rows=55216152] ``` Q10 iteration 0 took 9424.3 ms and returned 59986052 rows Q10 avg time: 9424.33 ms ## What changes are included in this PR? Before spilling, `StringViewArray` columns must be permutated the way described above, so this PR reorganize the array to sequential order before spilling. <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? Added one unit regression test. This test fails without the change. <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 6. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> -- 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-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org