Skip to content

fix(physical-plan): compact List<Dictionary> spill chunks when values are not shared across groups - #27938

Open
warrenzhu25 wants to merge 1 commit into
apache:mainfrom
warrenzhu25:fix-list-dict-aggregate-spill
Open

warrenzhu25 wants to merge 1 commit into
apache:mainfrom
warrenzhu25:fix-list-dict-aggregate-spill

Conversation

@warrenzhu25

Copy link
Copy Markdown

Which issue does this PR close?

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.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) physical-plan Changes to the physical-plan crate labels Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aggregate spill: List<Dictionary> state whose values repeat within a group still fails under a memory limit

1 participant