Skip to content

Preserve ordering and grouping through single-partition coalesce #27676

Open

Description

@kosiew

Related PR

#26852

Problem

CoalescePartitionsExec::compute_properties calls clear_orderings() regardless of input partition count. This already discarded ordering; after #26852, it also discards explicit grouping assertions and their normalized cache. With one input partition, execute forwards the child stream directly or returns its prefix when fetch is set. Neither operation rearranges rows or splits a group into separate runs.

Why it matters

Downstream planning can no longer use valid ordering or grouping guarantees across the single-partition fast path. The current behavior is conservative: no wrong results or measured performance regression are claimed.

Invariant / desired behavior

Single-partition coalesce preserves the child's ordering and grouping guarantees, with or without fetch. Fetch may shorten the final retained group or omit later groups, but retained rows stay in order and equal-key runs stay contiguous. With multiple input partitions, coalesce must not carry forward child ordering or explicit grouping guarantees: interleaving streams can break both.

Proposed direction

In datafusion/physical-plan/src/coalesce_partitions.rs, skip clear_orderings() only for exactly one input partition. Keep per-partition constant handling, output partitioning, scheduling, evaluation type, and other plan-property handling unchanged. Leave the shared EquivalenceProperties::clear_orderings() contract intact.

Scope

In

  • Sequence-property handling and focused tests for CoalescePartitionsExec.

Out

  • Changes to execution, fetch semantics, other operators, or the shared invalidation helper.
  • The separate direct helper test proposed in review suggestion 2.

Acceptance criteria

  • With one input partition, both plain coalesce and fetch-limited coalesce advertise valid child ordering and explicit grouping. Fetch still returns the same prefix.
  • With multiple input partitions, coalesce does not advertise child ordering or explicit grouping that interleaving could invalidate.

Tests / verification

  • Update the existing coalesce_clears_explicit_grouping coverage so it still checks multi-partition clearing but no longer expects single-partition clearing.
  • Add focused cases for one partition without fetch, one partition with fetch cutting a group short, and multiple partitions. Use explicit grouping on a key independent of the advertised ordering so an ordering-derived proof cannot mask lost explicit grouping. Assert ordering and grouping separately; verify the fetched output against the expected prefix.
  • Run cargo test -p datafusion-physical-plan --lib coalesce_partitions::tests.

Activity

  1. akshat-lakhera commented on Nov 24, 2026

    @akshat-lakhera

    Hey @kosiew @alamb,
    I would like to work on this can you assign this to me.

  2. kosiew commented on Nov 25, 2026

    @kosiew
    ContributorAuthor

    @akshat-lakhera
    @mitsa-ng is working on this at the moment.

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