Skip to content

Use pipeline aggregation when data is implicitly sorted by group-by keys #19655

Description

@NGA-TRAN

Is your feature request related to a problem or challenge?

We have a use case where the query groups by columns that are implicitly sorted, and we would like DataFusion to recognize that ordering so it can apply pipeline aggregation.

Consider the following query:

SELECT ...
FROM t
GROUP BY a, b;

In our data, the input is explicitly sorted only by a. However, b is always unique within each (a, b) pair, which means the data is implicitly sorted by (a, b) even though only a appears in the physical sort order.

If DataFusion could infer or be informed that the data is effectively sorted by (a, b), it would be able to use pipeline aggregation, resulting in better performance.

This is related to #16899

Describe the solution you'd like

We need to define and propagate the uniqueness-based sort order.

Describe alternatives you've considered

No response

Additional context

No response

Activity

  1. added theissue type on Jan 5, 2026
  2. xavlee commented on Jan 5, 2026

    @xavlee
    Contributor

    take

  3. NGA-TRAN commented on Jan 12, 2026

    @NGA-TRAN
    ContributorAuthor

    Some hints: I suggest we start by running some benchmarking or performance tests to determine whether this is worth pursuing.

    • Create two different schemas on the same dataset: one explicitly sorted on (a, b) and another sorted only on a. The first should produce a group‑by pipeline, while the second will fall back to a group‑by hash.
    • We may also want to run these tests on more realistic query such as (data is explicitly sorted on f_dkey, timestamp but implicitly sorted by f_dkey, date_bin, env)
    SELECT env, time_bin, AVG(max_bin_value) AS avg_max_value
    FROM
    (
        SELECT  f_dkey,
                date_bin(INTERVAL '30 seconds', timestamp) AS time_bin,
                env,
                MAX(value) AS max_bin_value
        FROM
            (
            SELECT
                f.f_dkey,
                d.env,
                d.service,
                d.host,
                f.timestamp,
                f.value
            FROM dimension_table d
            INNER JOIN fact_table_ordered f ON d.d_dkey = f.f_dkey
            WHERE service = 'log'
            ) AS j
        GROUP BY f_dkey, time_bin, env
    ) AS a
    GROUP BY env, time_bin
    ORDER BY env, time_bin;

    @xavlee : When you start working on this, let us chat with @gene-bordegaray first.

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions