Commit bc6f95b
fix: track join_arrays memory in reservation after SMJ spill (#21962)
## Which issue does this PR close?
Related to the TODO at `materializing_stream.rs:283` (from
[#17429](#17429 (comment))):
spilled `BufferedBatch` join key arrays are not tracked in memory
reservation.
## Rationale for this change
When a `BufferedBatch` is spilled to disk in Sort Merge Join, only the
`RecordBatch` data is written to the IPC file. The `join_arrays`
(evaluated join key columns) remain in memory because the merge-scan
comparator needs them to detect key group boundaries.
Before this fix, these in-memory `join_arrays` were **invisible to the
memory pool**:
allocate_reservation():
try_grow(size_estimation) → FAILS (pool full)
spill batch to disk
→ join_arrays still in memory, but reservation was never grown
→ pool thinks 0 bytes are used for this batch
free_reservation():
if InMemory → shrink(size_estimation)
if Spilled → no-op ← correct (nothing was grown), but join_arrays are
invisible
With many spilled batches for a skewed key (e.g., millions of rows
sharing the same join key), the untracked `join_arrays` memory
accumulates. The memory pool cannot account for this when making spill
decisions for concurrent operators.
## What changes are included in this PR?
**Memory accounting fix** (`materializing_stream.rs`):
- Add `reserved_amount` field to `BufferedBatch` — tracks how much
memory was **actually reserved** in the pool for this batch
- Add `join_arrays_mem()` helper — computes total memory of join key
arrays
- `allocate_reservation()`: after spilling, calls
`try_grow(join_arrays_mem)` to track the remaining in-memory data. If
the pool is too tight for even that, `reserved_amount` stays 0
(best-effort, safe)
- `free_reservation()`: shrinks by `reserved_amount` instead of checking
`InMemory` variant. Invariant: only shrink by what was actually grown —
no underflow risk
| Scenario | `try_grow` | `reserved_amount` | `try_shrink` | Safe? |
|----------|-----------|-------------------|-------------|-------|
| InMemory | Ok(size_estimation) | size_estimation | size_estimation |
Yes |
| Spilled, tracked | Ok(join_arrays_mem) | join_arrays_mem |
join_arrays_mem | Yes |
| Spilled, pool tight | Err | 0 | 0 (no-op) | Yes |
**Tests** (`tests.rs`):
- `spill_many_batches_same_key` — 10+5 batches all sharing key=1,
verifies correctness under heavy spilling
- `spill_string_join_keys` — Utf8 join keys to exercise larger
`join_arrays` footprint
- `spill_mixed_keys_some_match` — multiple distinct keys with partial
matching, tests Full outer join NULL rows from spilled batches
- `spill_join_arrays_memory_accounting` — verifies memory pool is fully
released after join completes (`memory_pool.reserved() == 0`) and
`peak_mem_used > 0`
## Are these changes tested?
Yes. Four new tests added covering heavy spilling with same-key batches,
string join keys, mixed keys with partial matching, and memory pool
accounting verification.
## Are there any user-facing changes?
No.
---------
Co-authored-by: Subham Singhal <subhamsinghal@Subhams-MacBook-Air.local>1 parent 3b634aa commit bc6f95b
2 files changed
Lines changed: 258 additions & 11 deletions
Lines changed: 41 additions & 11 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
238 | 250 | | |
239 | 251 | | |
240 | 252 | | |
| |||
258 | 270 | | |
259 | 271 | | |
260 | 272 | | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
261 | 278 | | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
| 279 | + | |
266 | 280 | | |
267 | 281 | | |
268 | 282 | | |
| |||
274 | 288 | | |
275 | 289 | | |
276 | 290 | | |
| 291 | + | |
| 292 | + | |
277 | 293 | | |
278 | 294 | | |
279 | 295 | | |
| |||
947 | 963 | | |
948 | 964 | | |
949 | 965 | | |
950 | | - | |
951 | | - | |
952 | | - | |
953 | | - | |
954 | | - | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
955 | 969 | | |
956 | | - | |
957 | 970 | | |
958 | 971 | | |
959 | 972 | | |
960 | 973 | | |
961 | 974 | | |
| 975 | + | |
962 | 976 | | |
963 | 977 | | |
964 | 978 | | |
| |||
978 | 992 | | |
979 | 993 | | |
980 | 994 | | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
981 | 1011 | | |
982 | 1012 | | |
983 | 1013 | | |
| |||
1006 | 1036 | | |
1007 | 1037 | | |
1008 | 1038 | | |
1009 | | - | |
| 1039 | + | |
1010 | 1040 | | |
1011 | 1041 | | |
1012 | 1042 | | |
| |||
Lines changed: 217 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2487 | 2487 | | |
2488 | 2488 | | |
2489 | 2489 | | |
| 2490 | + | |
| 2491 | + | |
| 2492 | + | |
| 2493 | + | |
| 2494 | + | |
| 2495 | + | |
| 2496 | + | |
| 2497 | + | |
| 2498 | + | |
| 2499 | + | |
| 2500 | + | |
| 2501 | + | |
| 2502 | + | |
| 2503 | + | |
| 2504 | + | |
| 2505 | + | |
| 2506 | + | |
| 2507 | + | |
| 2508 | + | |
| 2509 | + | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
| 2513 | + | |
| 2514 | + | |
| 2515 | + | |
| 2516 | + | |
| 2517 | + | |
| 2518 | + | |
| 2519 | + | |
| 2520 | + | |
| 2521 | + | |
| 2522 | + | |
| 2523 | + | |
| 2524 | + | |
| 2525 | + | |
| 2526 | + | |
| 2527 | + | |
| 2528 | + | |
| 2529 | + | |
| 2530 | + | |
| 2531 | + | |
| 2532 | + | |
| 2533 | + | |
| 2534 | + | |
| 2535 | + | |
| 2536 | + | |
| 2537 | + | |
| 2538 | + | |
| 2539 | + | |
| 2540 | + | |
| 2541 | + | |
| 2542 | + | |
| 2543 | + | |
| 2544 | + | |
| 2545 | + | |
| 2546 | + | |
| 2547 | + | |
| 2548 | + | |
| 2549 | + | |
| 2550 | + | |
| 2551 | + | |
| 2552 | + | |
| 2553 | + | |
| 2554 | + | |
| 2555 | + | |
| 2556 | + | |
| 2557 | + | |
| 2558 | + | |
| 2559 | + | |
| 2560 | + | |
| 2561 | + | |
| 2562 | + | |
| 2563 | + | |
| 2564 | + | |
| 2565 | + | |
| 2566 | + | |
| 2567 | + | |
| 2568 | + | |
| 2569 | + | |
| 2570 | + | |
| 2571 | + | |
| 2572 | + | |
| 2573 | + | |
| 2574 | + | |
| 2575 | + | |
| 2576 | + | |
| 2577 | + | |
| 2578 | + | |
| 2579 | + | |
| 2580 | + | |
| 2581 | + | |
| 2582 | + | |
| 2583 | + | |
| 2584 | + | |
| 2585 | + | |
| 2586 | + | |
| 2587 | + | |
| 2588 | + | |
| 2589 | + | |
| 2590 | + | |
| 2591 | + | |
| 2592 | + | |
| 2593 | + | |
| 2594 | + | |
| 2595 | + | |
| 2596 | + | |
| 2597 | + | |
| 2598 | + | |
| 2599 | + | |
| 2600 | + | |
| 2601 | + | |
| 2602 | + | |
| 2603 | + | |
| 2604 | + | |
| 2605 | + | |
| 2606 | + | |
| 2607 | + | |
| 2608 | + | |
| 2609 | + | |
| 2610 | + | |
| 2611 | + | |
| 2612 | + | |
| 2613 | + | |
| 2614 | + | |
| 2615 | + | |
| 2616 | + | |
| 2617 | + | |
| 2618 | + | |
| 2619 | + | |
| 2620 | + | |
| 2621 | + | |
| 2622 | + | |
| 2623 | + | |
| 2624 | + | |
| 2625 | + | |
| 2626 | + | |
| 2627 | + | |
| 2628 | + | |
| 2629 | + | |
| 2630 | + | |
| 2631 | + | |
| 2632 | + | |
| 2633 | + | |
| 2634 | + | |
| 2635 | + | |
| 2636 | + | |
| 2637 | + | |
| 2638 | + | |
| 2639 | + | |
| 2640 | + | |
| 2641 | + | |
| 2642 | + | |
| 2643 | + | |
| 2644 | + | |
| 2645 | + | |
| 2646 | + | |
| 2647 | + | |
| 2648 | + | |
| 2649 | + | |
| 2650 | + | |
| 2651 | + | |
| 2652 | + | |
| 2653 | + | |
| 2654 | + | |
| 2655 | + | |
| 2656 | + | |
| 2657 | + | |
| 2658 | + | |
| 2659 | + | |
| 2660 | + | |
| 2661 | + | |
| 2662 | + | |
| 2663 | + | |
| 2664 | + | |
| 2665 | + | |
| 2666 | + | |
| 2667 | + | |
| 2668 | + | |
| 2669 | + | |
| 2670 | + | |
| 2671 | + | |
| 2672 | + | |
| 2673 | + | |
| 2674 | + | |
| 2675 | + | |
| 2676 | + | |
| 2677 | + | |
| 2678 | + | |
| 2679 | + | |
| 2680 | + | |
| 2681 | + | |
| 2682 | + | |
| 2683 | + | |
| 2684 | + | |
| 2685 | + | |
| 2686 | + | |
| 2687 | + | |
| 2688 | + | |
| 2689 | + | |
| 2690 | + | |
| 2691 | + | |
| 2692 | + | |
| 2693 | + | |
| 2694 | + | |
| 2695 | + | |
| 2696 | + | |
| 2697 | + | |
| 2698 | + | |
| 2699 | + | |
| 2700 | + | |
| 2701 | + | |
| 2702 | + | |
| 2703 | + | |
| 2704 | + | |
| 2705 | + | |
| 2706 | + | |
2490 | 2707 | | |
2491 | 2708 | | |
2492 | 2709 | | |
| |||
0 commit comments