Skip to content

Optimize dictionary interleave when values mappings are shared #12056

Open

Description

@Rich-T-kid

Is your feature request related to a problem or challenge?

interleave recognizes when its dictionary inputs share the same values mapping, but currently routes that case through interleave_fallback_dictionary. The fallback concatenates every values array and offsets the selected keys, duplicating a mapping that could be reused.

For example, given two slices of the same dictionary:

Dictionary A                         Dictionary B
keys: [0, 2]                         keys: [1, 0]
        \                               /
         +---- shared values Arc V ----+

indices: [(A, 1), (B, 0)]

today:
  values = concat(V, V)
  keys   = [2, len(V) + 1]

desired:
  values = Arc::clone(V)
  keys   = [2, 1]

Both outputs are logically equivalent, but the current path copies the values once per source and creates a larger dictionary. This is particularly expensive for wide or sparsely referenced Utf8/Binary dictionaries.

The existing take dictionary path already gathers the primitive keys and clones the existing values Arc.

This shape can reach Arrow from DataFusion. For example:

SELECT user_id, array_agg(event_type)
FROM events
GROUP BY user_id;

If multiple input batches are slices, filters, or takes of the same dictionary-encoded event_type array, DataFusion's array accumulator stores those arrays and later gathers rows with interleave:

Batch 0: Dictionary(keys_0, Arc V) --+
Batch 1: Dictionary(keys_1, Arc V) --+--> array_agg group ordering --> interleave
Batch 2: Dictionary(keys_2, Arc V) --+

Sort-preserving merge, partial sort, TopK, sort-merge join, and ASOF join also use interleave to materialize rows from multiple batches.

Describe the solution you'd like

Add a dictionary fast path that:

  1. Considers only referenced sources with selected non-null keys.
  2. Verifies that those sources share the same values mapping.
  3. Gathers their primitive keys directly, preserving nulls.
  4. Reuses the first values Arc in the output dictionary.

Inputs with distinct mappings should continue through the existing merge or concatenation paths. Ignoring unreferenced and all-null sources is important for callers that retain extra batches or prepend a null sentinel.

Describe alternatives you've considered

The existing dictionary merge path could deduplicate the mappings, but it does substantially more work than necessary when the mappings are already identical. Implementing this in each downstream caller would also duplicate logic that belongs in the Arrow kernel.

Additional context

The existing interleave_kernels benchmarks already create shared-mapping inputs by slicing one base dictionary. The dict_distinct cases provide a control for independently backed dictionaries.

Activity

  1. added
    enhancement
    on Nov 3, 2026
  2. Rich-T-kid commented on Nov 3, 2026

    @Rich-T-kid
    ContributorAuthor

    take

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

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions