Skip to content

feat: allow scaling RangePartitioning - #24766

Open
goutamadwant wants to merge 2 commits into
apache:mainfrom
goutamadwant:feat/range-partitioning-scaling
Open

feat: allow scaling RangePartitioning#24766
goutamadwant wants to merge 2 commits into
apache:mainfrom
goutamadwant:feat/range-partitioning-scaling

Conversation

@goutamadwant

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Distributed and adaptive planners need to lower a range-partitioned plan's task count without discarding its higher-resolution boundary sample. Today scaling loses the range layout and falls back to unknown partitioning.

What changes are included in this PR?

  • Add validated sample-backed construction, fallible scaling, and sample/current/max partition-count accessors while preserving existing exact construction and the borrowed split_points() API.
  • Cache deterministic effective split points and retain the shared maximum-resolution sample through projection, repartition, and join partitioning rewrites.
  • Preserve sample metadata through physical protobuf and FFI round trips.
  • Keep protobuf field 2 as the effective split points for older readers, while new readers validate the sample/count representation against it.

Are these changes tested?

Yes. Tests cover deterministic scaling and invalid counts, equality by effective boundaries, projection and plan rewrites, legacy/current/malformed protobuf payloads, and FFI unit and cross-library round trips. The full required workspace test suite and workspace-wide clippy with all targets and features passed. After refreshing the unchanged patch to current main, the focused RangePartitioning tests passed again.

Are there any user-facing changes?

Yes. RangePartitioning gains try_new_with_samples, samples, max_partition_count, and scale. Existing exact construction and split_points() callers remain source-compatible.

FFI_RangePartitioning now carries samples and the current partition count, so this PR requires the api change label.

@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates ffi Changes to the ffi crate physical-plan Changes to the physical-plan crate labels Aug 29, 2026
@github-actions

github-actions Bot commented Aug 29, 2026

Copy link
Copy Markdown

Thank you for opening this pull request!

Reviewer note: cargo-semver-checks reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch).

Details
     Cloning apache/main
    Building datafusion-ffi v55.0.0 (current)
       Built [  56.808s] (current)
     Parsing datafusion-ffi v55.0.0 (current)
      Parsed [   0.060s] (current)
    Building datafusion-ffi v55.0.0 (baseline)
       Built [  56.273s] (baseline)
     Parsing datafusion-ffi v55.0.0 (baseline)
      Parsed [   0.061s] (baseline)
    Checking datafusion-ffi v55.0.0 -> v55.0.0 (no change; assume patch)
     Checked [   0.352s] 223 checks: 223 pass, 31 skip
     Summary no semver update required
    Finished [ 114.985s] datafusion-ffi
    Building datafusion-physical-expr v55.0.0 (current)
       Built [  29.330s] (current)
     Parsing datafusion-physical-expr v55.0.0 (current)
      Parsed [   0.050s] (current)
    Building datafusion-physical-expr v55.0.0 (baseline)
       Built [  28.862s] (baseline)
     Parsing datafusion-physical-expr v55.0.0 (baseline)
      Parsed [   0.049s] (baseline)
    Checking datafusion-physical-expr v55.0.0 -> v55.0.0 (no change; assume patch)
     Checked [   0.464s] 223 checks: 223 pass, 31 skip
     Summary no semver update required
    Finished [  59.931s] datafusion-physical-expr
    Building datafusion-physical-plan v55.0.0 (current)
       Built [  38.577s] (current)
     Parsing datafusion-physical-plan v55.0.0 (current)
      Parsed [   0.147s] (current)
    Building datafusion-physical-plan v55.0.0 (baseline)
       Built [  38.260s] (baseline)
     Parsing datafusion-physical-plan v55.0.0 (baseline)
      Parsed [   0.148s] (baseline)
    Checking datafusion-physical-plan v55.0.0 -> v55.0.0 (no change; assume patch)
     Checked [   0.916s] 223 checks: 223 pass, 31 skip
     Summary no semver update required
    Finished [  79.419s] datafusion-physical-plan
    Building datafusion-proto-models v55.0.0 (current)
       Built [  25.263s] (current)
     Parsing datafusion-proto-models v55.0.0 (current)
      Parsed [   0.126s] (current)
    Building datafusion-proto-models v55.0.0 (baseline)
       Built [  25.133s] (baseline)
     Parsing datafusion-proto-models v55.0.0 (baseline)
      Parsed [   0.128s] (baseline)
    Checking datafusion-proto-models v55.0.0 -> v55.0.0 (no change; assume patch)
     Checked [   2.530s] 223 checks: 222 pass, 1 fail, 0 warn, 31 skip

--- failure constructible_struct_adds_field: struct exhaustively constructible through public API adds field ---

Description:
A pub struct that could be exhaustively constructed with a literal using only public API has a new pub field, breaking existing exhaustive literals.
        ref: https://doc.rust-lang.org/reference/expressions/struct-expr.html
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.50.0/src/lints/constructible_struct_adds_field.ron

Failed in:
  field PhysicalRangePartitioning.sample_point in /home/runner/work/datafusion/datafusion/datafusion/proto-models/src/generated/prost.rs:2371
  field PhysicalRangePartitioning.partition_count in /home/runner/work/datafusion/datafusion/datafusion/proto-models/src/generated/prost.rs:2374
  field PhysicalRangePartitioning.sample_point in /home/runner/work/datafusion/datafusion/datafusion/proto-models/src/generated/prost.rs:2371
  field PhysicalRangePartitioning.partition_count in /home/runner/work/datafusion/datafusion/datafusion/proto-models/src/generated/prost.rs:2374

     Summary semver requires new major version: 1 major and 0 minor checks failed
    Finished [  54.524s] datafusion-proto-models

@github-actions github-actions Bot added the auto detected api change Auto detected API change label Aug 29, 2026
@codecov-commenter

codecov-commenter commented Aug 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.74074% with 78 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.51%. Comparing base (c56a7ab) to head (4013966).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/physical-expr/src/partitioning.rs 86.26% 15 Missing and 24 partials ⚠️
datafusion/proto-models/src/generated/pbjson.rs 0.00% 26 Missing ⚠️
datafusion/ffi/src/physical_expr/partitioning.rs 82.05% 3 Missing and 4 partials ⚠️
datafusion/ffi/src/plan_properties.rs 75.00% 1 Missing and 1 partial ⚠️
datafusion/physical-plan/src/joins/utils.rs 92.85% 2 Missing ⚠️
datafusion/physical-plan/src/repartition/mod.rs 86.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24766      +/-   ##
==========================================
+ Coverage   81.47%   81.51%   +0.03%     
==========================================
  Files        1122     1123       +1     
  Lines      404140   405483    +1343     
  Branches   404140   405483    +1343     
==========================================
+ Hits       329284   330516    +1232     
- Misses      55546    55618      +72     
- Partials    19310    19349      +39     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jayzhan211 jayzhan211 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @goutamadwant, there is a suggestion:

RangePartitioning::PartialEq compares only ordering + effective split_points, but samples is not derivable from split_points and is exactly the state scale() reads. So a == b does not imply a.scale(k) == b.scale(k), and doesn't even imply both succeed — your own test asserts try_new_with_samples(ord, [10..90], 4) == try_new(ord, [30,50,70]), yet the first scales to 10 and the second errors at 4.

There's already a consumer that turns this into wrong results. can_interleave gates on partition == *reference:

// datafusion/physical-plan/src/union.rs:931
matches!(reference, Partitioning::Hash(_, _) | Partitioning::Range(_))
    && inputs
        .map(|plan| plan.borrow().output_partitioning().clone())
        .all(|partition| partition == *reference)

and InterleaveExec::compute_properties then adopts one input's metadata for the whole output:

// datafusion/physical-plan/src/union.rs:698
let output_partitioning = inputs[0].output_partitioning().clone();

Concretely: input A = try_new_with_samples(ord, [10,20,...,90], 4), input B = try_new(ord, [30,50,70]). They compare equal, interleaving is allowed (correct — the effective boundaries do match), and the InterleaveExec output now advertises max_partition_count() == 10. A distributed planner — the use case this PR is for — calls scale(10) on that output and gets [10,20,...,90], boundaries B's rows were never placed against. Because range partitioning is a declared, unvalidated property, that's silently wrong rows per partition, not an error.

Suggest making equality structural:

-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, PartialEq)]
 pub struct RangePartitioning {
     ordering: LexOrdering,
     samples: Arc<[SplitPoint]>,
     split_points: Arc<[SplitPoint]>,
     partition_count: usize,
 }
@@
-impl PartialEq for RangePartitioning {
-    fn eq(&self, other: &Self) -> bool {
-        self.ordering == other.ordering && self.split_points == other.split_points
-    }
-}

The one place that genuinely wants effective-boundary comparison already spells it out and is unaffected:

// datafusion/physical-plan/src/distribution_requirements.rs:348
(Partitioning::Range(left), Partitioning::Range(right)) => {
    left.split_points() == right.split_points()
        && ...
}

so co-partitioned joins keep the permissive behavior; only can_interleave gets stricter, which is the conservative direction. test_range_partitioning_equality_uses_effective_split_points would then need to flip to assert_ne!.

If you'd rather keep permissive equality, the alternative is for InterleaveExec::compute_properties to reduce the output to the coarsest sample set common to all inputs rather than inheriting inputs[0]'s — but that's more machinery for the same guarantee.

self.ordering,
split_points,
self.partition_count()
)

@jayzhan211 jayzhan211 Aug 30, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Range([a@0 ASC], [(30), (50), (70)], 4) is identical for a partitioning that can scale to 10 and one that can't scale past 4. Since EXPLAIN output is the primary debugging surface for partitioning bugs, appending the max when it differs (e.g. , max 10) would pay for itself.

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

Labels

auto detected api change Auto detected API change ffi Changes to the ffi crate physical-expr Changes to the physical-expr crates physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow for scaling RangePartitioning

3 participants