2010YOUY01 opened a new pull request, #13377: URL: https://github.com/apache/datafusion/pull/13377
## 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. --> First step to fix https://github.com/apache/datafusion/issues/13089 ## Rationale for this change Now `record_batch.get_array_memory_size()` will overestimate memory usage: If multiple array are pointing to the same underlying buffer, they will be counted repeatedly. A more detailed explanation can be found in this PR's comment. This function is used for spilled execution to estimate physical memory usage, this overestimation caused many bugs in memory-limited sort/aggregation/join. For example, if there is a `RecordBatch` with 10 columns, all of 10 columns are sharing the same `Buffer`, then `record_batch.get_array_memory_size()` will return a 10X estimation, to make memory-limited query fail quite easily. I believe https://github.com/apache/datafusion/issues/13089 is caused by this issue, and likely https://github.com/apache/datafusion/issues/9417 https://github.com/apache/datafusion/issues/10511 https://github.com/apache/datafusion/issues/12136 https://github.com/apache/datafusion/issues/11390 <!-- 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. --> ## What changes are included in this PR? Introduced a new `get_record_batch_memory_size()` to avoid double count, by using a internal `HashSet` to recognize reused buffers. While @waynexia is working on a comprehensive solution in `arrow-rs` https://github.com/apache/arrow-rs/issues/6439, I think it's useful to introduce this temporary fix in DataFusion due to: - After fixing `record_batch.get_array_memory_size()` with memory overcounting, it's non trivial to fix all tests at once (manual memory tracking is tricky, when I was trying to make one external aggregate query to run, it took me a while to figure out why one test case fail after a change) - If we can adopt this temporary fix, we can gradually swap out `record_batch.get_array_memory_size()`, and add regression tests for memory-limited query bugs. After we have a fix in arrow, the temporary fix function can be deprecated and replace with the origin one more easily. <!-- 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? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. 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)? --> Yes ## Are there any user-facing changes? No <!-- 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