Skip to content

fix: avoid overlapping scan output buffers - #810

Open
lizhen-0710 wants to merge 1 commit into
bytedance:mainfrom
lizhen-0710:misc-hive-scan-buffer-reuse
Open

fix: avoid overlapping scan output buffers#810
lizhen-0710 wants to merge 1 commit into
bytedance:mainfrom
lizhen-0710:misc-hive-scan-buffer-reuse

Conversation

@lizhen-0710

@lizhen-0710 lizhen-0710 commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

What problem does this PR solve?

HiveDataSource keeps a backing reader output RowVector in output_ while returning a projected RowVector to the operator pipeline. The returned RowVector holds references to the child vectors from output_. If output_ is reused for the next splitReader::next() call, HiveDataSource also keeps the previous backing RowVector alive while the next batch allocates selective column values. This makes the previous output children and current output allocation overlap, which can temporarily double scan-side memory.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 🚀 Performance improvement (optimization)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)
  • 🔨 Refactoring (no logic changes)
  • 🔧 Build/CI or Infrastructure changes
  • 📝 Documentation only

Description

Recreate the backing reader output before every splitReader::next() call so HiveDataSource drops its reference to the previous backing RowVector before the next batch allocation. The returned output still owns the previous child vectors, but HiveDataSource no longer adds an extra reference that extends their lifetime into the next read.

Also bound SelectiveColumnReader values_ reuse to buffers no larger than 1.5x the current request. This preserves useful same-size reuse while allowing oversized buffers from larger batches to be released before smaller batches allocate replacement values.

Add targeted tests covering the HiveDataSource backing RowVector lifetime and the SelectiveColumnReader reuse, oversize-release, and shared-buffer paths.

Performance Impact

  • No Impact: This change does not affect the critical path (e.g., build system, doc, error handling).

  • Positive Impact: I have run benchmarks.

    Click to view Benchmark Results
    Paste your google-benchmark or TPC-H results here.
    Before: 10.5s
    After:   8.2s  (+20%)
    
  • Negative Impact: Explained below (e.g., trade-off for correctness).

Release Note

Please describe the changes in this PR

Release Note:

Release Note:
- Fixed a crash in `substr` when input is null.
- optimized `group by` performance by 20%.

Checklist (For Author)

  • I have added/updated unit tests (ctest).
  • I have verified the code with local build (Release/Debug).
  • I have run clang-format / linters.
  • (Optional) I have run Sanitizers (ASAN/TSAN) locally for complex C++ changes.
  • No need to test or manual test.

Breaking Changes

  • No

  • Yes (Description: ...)

    Click to view Breaking Changes
    Breaking Changes:
    - Description of the breaking change.
    - Possible solutions or workarounds.
    - Any other relevant information.
    

HiveDataSource keeps a backing reader output RowVector in output_ while returning a projected RowVector to the operator pipeline. The returned RowVector holds references to the child vectors from output_. If output_ is reused for the next splitReader::next() call, HiveDataSource also keeps the previous backing RowVector alive while the next batch allocates selective column values. This makes the previous output children and current output allocation overlap, which can temporarily double scan-side memory.

Recreate the backing reader output before every splitReader::next() call so HiveDataSource drops its reference to the previous backing RowVector before the next batch allocation. The returned output still owns the previous child vectors, but HiveDataSource no longer adds an extra reference that extends their lifetime into the next read.

Also bound SelectiveColumnReader values_ reuse to buffers no larger than 1.5x the current request. This preserves useful same-size reuse while allowing oversized buffers from larger batches to be released before smaller batches allocate replacement values.

Add targeted tests covering the HiveDataSource backing RowVector lifetime and the SelectiveColumnReader reuse, oversize-release, and shared-buffer paths.

Co-authored-by: TRAE CLI <noreply@bytedance.com>
if (!output_) {
output_ = BaseVector::create(readerOutputType_, 0, pool_);
}
output_ = BaseVector::create(readerOutputType_, 0, pool_);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

performance concern: this now allocates a new top-level RowVector for every batch. The overhead is likely small compared with the child value buffers, but it gives up top-level vector reuse. Have you benchmarked small-batch scans?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants