Skip to content

Partial aggregation never re-probes after it starts skipping #27591

Open

Description

@comphead

Is your feature request related to a problem or challenge?

Once SkipAggregationProbe decides to skip partial aggregation, the decision is final. update_state sets is_locked = should_skip, and the stream converts every remaining batch to state rows without aggregating. The probe only sees the rows just past skip_partial_aggregation_probe_rows_threshold, so two kinds of input lose:

  • Keys whose pattern changes after the probe. 400K distinct keys followed by 1.6M rows over 1000 keys:

    SET datafusion.execution.target_partitions = 2;
    EXPLAIN ANALYZE
    SELECT CASE WHEN value <= 400000 THEN value ELSE value % 1000 END AS k, SUM(value)
    FROM generate_series(1, 2000000)
    GROUP BY k;

    The Partial skips 1.79M rows and sends 2.00M rows downstream. With skip_partial_aggregation_probe_ratio_threshold = 1.0 it sends 401K. This happens with or without a memory limit. Query time is about the same on one node (0.012 s and 0.013 s), so the cost is the 5x larger Partial output, which matters most where that output is shuffled over the network.

  • A burst of memory pressure. With fix: count early emitted groups in the partial aggregation skip probe #27351 the probe also counts groups emitted early under memory pressure. If the pool is tight while the probe is still deciding, for example because another operator or a concurrent query holds memory, the bounded table reduces little and the stream starts skipping. It keeps skipping after the memory is released. The same data shows how much the decision depends on the memory at that moment: 20M rows over 50K shuffled keys skip under a 16M fair pool but reduce 50x without a limit.

Describe the solution you'd like

Re-probe after skipping. Trino re-enables partial aggregation after processing 200 times the window it used to disable it, and resets its counters (PartialAggregationController). DuckDB resumes hash table lookups after skipping 128K to 1M rows (RadixHTAdaptivity::MaybeResumeLookups). In DataFusion this could be: after skipping some multiple of skip_partial_aggregation_probe_rows_threshold rows, aggregate the next window into an empty table, check its ratio, and then keep aggregating or skip again.

This is point 3 of #22405. Its prototype deferred re-probing because re-entering aggregation after a skip panicked in multi_group_by/primitive.rs. That looks like the stale hash map after emit(EmitTo::All) that #27501 fixed.

Describe alternatives you've considered

  • Re-probe only when the memory pool frees up. That covers the burst case but not a key pattern that changes.
  • Estimate repetition in the skipped batches with a sketch instead of aggregating them, as Polars does with an F2 sketch before growing its bounded table. That adds hashing to every skipped row.

Additional context

Raised in the review of #27351: #27351 (comment). Related: #22405, #27350, #26124.

Activity

  1. added
    enhancement
    on Nov 19, 2026
  2. yashdotrv commented on Nov 23, 2026

    @yashdotrv
    Contributor

    Hi @comphead, thanks for opening this issue, we run df in production for analytical workloads at my org, and we’ve run into this exact behavior, especially with observability and time-series data where key distributions are non-uniform across partitions. And my understanding of the problem aligns with what you've described:

    • One-Way Latch: SkipAggregationProbe evaluates the initial window (skip_partial_aggregation_probe_rows_threshold, default 100k). If the distinct-to-total ratio exceeds skip_partial_aggregation_probe_ratio_threshold (0.8), or if early emits under transient memory pressure skew the counters, it permanently locks into skip mode (is_locked = should_skip).
    • Impact on Shifting Distributions: Because it never re-evaluates, an initial burst of high-cardinality keys permanently prevents partial aggregation for subsequent batches that may be highly repetitive. This bypasses local reduction entirely and pushes massive, unaggregated state downstream into the final aggregation stage, driving up memory pressure and shuffle costs.

    And I'm aware of how DuckDB/Trino implements this... em working on this, and would contribute a fix... so could you assign this issue to me? also please let me know if you have any existing preferences regarding the re-probe state transitions or configuration knobs or anything in general.

  3. comphead commented on Nov 24, 2026

    @comphead
    ContributorAuthor

    Thanks @yashdotrv for taking this!

  4. comphead commented on Nov 24, 2026

    @comphead
    ContributorAuthor

    @yashdotrv, heads-up that #27677 by benjibunbun already implements re-probing for this issue. It was opened a few hours before your comment. To avoid duplicate work, could you coordinate there? Reviews are welcome. Since you see this with non-uniform keys in production, numbers from your workloads on that branch would also help settle the overhead question in my review.

  5. 2010YOUY01 commented on Nov 24, 2026

    @2010YOUY01
    Member

    Reprobing after some intervals seems like a simple and viable solution.

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions