Skip to content

Grouped MIN/MAX over strings slows down with many groups #27914

Backlog

Description

@warrenzhu25

Is your feature request related to a problem or challenge?

Grouped MIN/MAX over strings and binary gets slower per row as the number of groups grows. Feeding the MinMaxBytesAccumulator the way a hash aggregate does (random keys, groups numbered in first-seen order, 4M rows in batches of 8192):

distinct keys time
10K 66 ms
100K 150 ms
1M 551 ms
4M 666 ms

So 1M keys cost over 8x more per row than 10K. This is the shape of an aggregation that picks one value per id, for example SELECT user_id, max(email) FROM t GROUP BY user_id, and it is where a hash aggregate loses to a sort followed by a streaming aggregate. I ran into it in Apache DataFusion Comet (apache/datafusion-comet#7146), where running a string min/max group-by as a hash aggregate instead of sorting its input is several times faster with few groups, but slower with millions of groups.

A perf profile of the 1M-key case shows:

  • about 39% in memcmp, comparing each new value with its group's existing value, which is mostly a cache miss on a random group's heap allocation;
  • about 13% in update_batch itself, much of it the per-row hashing of the HashMap<usize, &[u8]> that MinMaxBytesState::update_batch builds for every batch;
  • about 11% in malloc/free for each group's own Vec<u8>, and about 9% in copies.

The per-batch map was introduced by #18044 to fix the quadratic runtime in #17897. Its only job is to update each group's owned value at most once per batch, so the group index could address the scratch directly instead of being hashed.

Describe the solution you'd like

Replace the per-batch HashMap with a Vec<u32> indexed by group, holding the row of the batch with that group's best new value, plus a list of the groups the batch set. Both are reused across batches and only grow with the number of groups. After the batch, write those groups' values once and reset only their entries, so a batch still costs time in its own rows, not in the number of groups. The cost is 4 bytes per group, which size() should count.

In a prototype this makes the case above 16% to 27% faster from 100K keys up, with no change at 10K, and the existing min_max_bytes benchmark 22% to 37% faster.

Describe alternatives you've considered

  • Sorting the groups each batch touches before comparing with their existing values, hoping for ascending addresses. It was slower (+141% at 10K keys, +22% at 1M): the sort costs more than it saves, and each group's value is allocated when it is first set, not in group order.
  • Storing all values in one buffer instead of a Vec<u8> per group, to remove the malloc/free cost. Likely worth about another 10%, but it needs garbage collection when values are replaced and is a much larger change; DRAFT PoC - Adaptive MinMaxBytesAccumulator with Mode-Sensitive Dense/Sparse Processing and Comprehensive Criterion Benchmarks #18006 explored a large redesign that was not merged.
  • Keeping a short prefix of each group's value inline, so that most comparisons do not touch the heap. This depends on the data (it does not help values that share a long prefix, such as URLs), so it needs a benchmark with realistic value distributions first.

Additional context

The remaining cost at high cardinality is the comparison with each group's existing value, a cache miss inherent to updating random groups, which the change above does not address.

Willingness to contribute

I plan to contribute to this feature independently

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions