Skip to content

fix: gc StringView/BinaryView arrays before spilling to prevent write amplification#21325

Open
damahua wants to merge 2 commits intoapache:mainfrom
damahua:gc-view-arrays-spill
Open

fix: gc StringView/BinaryView arrays before spilling to prevent write amplification#21325
damahua wants to merge 2 commits intoapache:mainfrom
damahua:gc-view-arrays-spill

Conversation

@damahua
Copy link
Copy Markdown

@damahua damahua commented Apr 2, 2026

Which issue does this PR close?

Related to #19444 (sort spill StringView gc) and #20500 (repartition StringView gc). This PR extends the same fix to hash aggregation and sort-merge join spill paths, and adds BinaryViewArray support to the sort operator.

Rationale

After operations like take or slice, StringViewArray and BinaryViewArray retain shared references to all original data buffers. When these batches are written to spill files individually, the IPC writer must include every referenced buffer for every batch, causing massive write amplification.

The sort operator already had a fix for this (organize_stringview_arrays in sort.rs), but the hash aggregation and sort-merge join spill paths were missing it. Additionally, the sort operator's fix only handled StringViewArray, not BinaryViewArray.

Hash aggregation spill path

In row_hash.rs, IncrementalSortIterator produces output chunks via take_record_batch. Each chunk shares the same StringView data buffers as the parent emitted batch. Without gc(), spilling N chunks writes N copies of all shared buffers.

Sort-merge join spill path

In bitwise_stream.rs, inner_key_buffer contains sliced batches that share StringView data buffers with the original unsliced batches.

Changes

  1. New gc_view_arrays() utility in spill/mod.rs — compacts both StringViewArray and BinaryViewArray in a RecordBatch, returning the batch unchanged (no allocation) when no view-type columns exist
  2. Hash aggregation spill (row_hash.rs) — gc each IncrementalSortIterator output batch before writing to the spill file
  3. Sort-merge join spill (bitwise_stream.rs) — gc sliced inner_key_buffer batches before spilling
  4. Sort operator (sort.rs) — extended existing organize_stringview_arrays to also handle BinaryViewArray

A/B Benchmark Results

Workload: SELECT group_key, COUNT(*), SUM(value) FROM t GROUP BY group_key

  • 100,000 rows, 50,000 unique groups (high cardinality → forces spilling)
  • group_key: Utf8View (StringViewArray) with 50+ byte non-inline strings
  • Memory pool: 20 MB (FairSpillPool), single partition, batch_size=8192
  • Same source commit, only gc patch differs. N=3 runs, deterministic data (0 variance).
Metric Baseline (no gc) With gc Change
Spilled bytes 39.50 MB 7.90 MB -80.0%
Output bytes 103.3 MB 35.5 MB -65.6%
Query time 321 ± 11 ms 324 ± 9 ms ~same
Peak memory 19.82 MB 19.82 MB same
Spill count 2 2 same

With a tighter 8 MB pool: baseline OOMs (ResourcesExhausted: Failed to allocate additional 10.4 MB for GroupedHashAggregateStream) because inflated StringView buffers cause the sort memory estimate to exceed the pool. Optimized completes successfully (5 spills, 20.9 MB spilled).

Tests

  • All 1,299 existing datafusion-physical-plan lib tests pass (1 pre-existing zstd feature failure)
  • All 29 memory_limit integration tests pass
  • All 56 sort_merge_join tests pass
  • Added 4 new tests:
    • test_gc_view_arrays_reduces_spill_size — verifies gc compacts taken StringView/BinaryView batches
    • test_gc_view_arrays_write_amplification — demonstrates 8.5× write amplification without gc
    • test_gc_view_arrays_noop_for_non_view_types — verifies no overhead for non-view types
    • bench_stringview_aggregate_spill — end-to-end benchmark with EXPLAIN ANALYZE metrics

damahua and others added 2 commits April 2, 2026 11:34
… amplification

After operations like `take` or `interleave`, view-type arrays retain
shared references to large original data buffers. When these batches are
written to spill files individually, the IPC writer duplicates all
referenced buffers for every batch — measured at 8.5× write amplification
with 10 chunks of 1000 rows from a 10,000-row StringView dataset.

Changes:
- Add `gc_view_arrays()` utility in spill/mod.rs that compacts both
  StringViewArray and BinaryViewArray in a RecordBatch
- Apply gc to hash aggregation spill path (IncrementalSortIterator
  output chunks share parent batch buffers via take_record_batch)
- Apply gc to sort-merge join bitwise_stream spill path (inner_key_buffer
  contains sliced batches sharing original batch buffers)
- Extend sort operator's organize_stringview_arrays to also handle
  BinaryViewArray (was previously StringView-only)

The sort operator already had StringView gc (organize_stringview_arrays),
but the hash aggregation and sort-merge join spill paths were missing it.
This is the same class of bug fixed by PR apache#19444 for sort spilling.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…B test

Adds a targeted benchmark that exercises the hash aggregation spill path
with StringViewArray columns (Utf8View, non-inline 50+ byte strings).
Uses EXPLAIN ANALYZE to capture spill_count and spilled_bytes metrics.

A/B results (20 MB pool, 100K rows, 50K groups, N=3):
  Baseline: 39.50 MB spilled (5× write amplification from shared buffers)
  Optimized: 7.90 MB spilled (80% reduction)
  Query time: unchanged (~320 ms)

With 8 MB pool: baseline OOMs during sort reservation, optimized succeeds.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions github-actions bot added core Core DataFusion crate physical-plan Changes to the physical-plan crate labels Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant