warrenzhu25 opened a new pull request, #27938:
URL: https://github.com/apache/datafusion/pull/27938

   ## Which issue does this PR close?
   
   - Closes #27240.
   
   ## Rationale for this change
   
   When spilling aggregate states that contain `List<Dictionary>` (e.g., 
`ARRAY_AGG` over dictionary-encoded columns), each spill chunk is currently 
sliced without compacting the nested dictionary if the average total key count 
per dictionary value is `>= 2`.
   
   However, values in `ARRAY_AGG` often repeat many times *within* a single 
group while being disjoint *across* groups. Treating such a nested dictionary 
as shared causes every spilled chunk to serialize the entire uncompacted 
dictionary values array repeatedly, inflating spill size by orders of magnitude 
and triggering excessive disk usage / OOM.
   
   ## What changes are included in this PR?
   
   - In `datafusion/physical-plan/src/spill/spilled_row_sizes.rs`:
     - Detect `List`, `LargeList`, and `FixedSizeList` columns whose child 
array is a `Dictionary`.
     - Count distinct *row* references per dictionary value in a single pass 
(with early exit once `row_value_refs >= 2 * num_values`, indicating values are 
genuinely shared across groups).
     - When dictionary values are not shared across rows (`row_value_refs < 2 * 
num_values`), mark the column for compaction via 
`ColumnSize::CompactedListDictionary(Vec<usize>)` and account for each row's 
offset/keys plus each distinct dictionary value referenced by that row once.
     - Handle sliced `FixedSizeListArray` (`base = list.offset() * size`) and 
null list rows / null dictionary keys.
   - In `datafusion/sqllogictest/test_files/aggregate_memory_spill.slt`:
     - Add end-to-end regression tests for spilling `ARRAY_AGG` over 
dictionary-encoded strings (1 and 2 partitions, 24M memory limit) verifying 
that spill size stays in MB rather than blowing up.
   
   ## What is the testing strategy for this PR?
   
   - Unit tests in `datafusion/physical-plan/src/spill/spilled_row_sizes.rs` 
(`cargo test -p datafusion-physical-plan spilled_row_sizes`, 14/14 passing):
     - `nested_dictionary_repeated_within_group_is_compacted`
     - `nested_dictionary_shared_across_groups_stays_shared`
     - `sliced_and_nullable_nested_dictionaries`
   - End-to-end sqllogictest in 
`datafusion/sqllogictest/test_files/aggregate_memory_spill.slt` (`cargo test 
--test sqllogictests -- aggregate_memory_spill`).
   - Ran `cargo fmt --all` and `cargo clippy -p datafusion-physical-plan 
--all-targets -- -D warnings`.
   
   ## Are there any user-facing changes?
   
   No public API changes. Aggregate spilling with `List<Dictionary>` columns 
now compacts non-shared nested dictionaries per spill chunk instead of 
repeatedly serializing the full dictionary.
   
   ---
   *Note: AI assistance was used to help investigate, implement, and test this 
change; all changes were reviewed and verified by the author.*


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to