Skip to content

fix: retain only valid orderings after order-preserving repartition - #26747

Open
Kristofferjoha wants to merge 2 commits into
apache:mainfrom
Kristofferjoha:fix/repartition-preserved-orderings
Open

Kristofferjoha wants to merge 2 commits into
apache:mainfrom
Kristofferjoha:fix/repartition-preserved-orderings

Conversation

@Kristofferjoha

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #26745.

Rationale for this change

An order-preserving repartition can incorrectly retain independent input orderings that do not hold after merging partitions. This lets the planner remove a necessary sort, causing ORDER BY to return rows in the wrong order.

What changes are included in this PR?

  • Retain only the merge key ordering when repartitioning merges multiple input partitions.
  • Preserve projected cast relationships so valid cast-derived orderings can still be inferred after the merge.
  • Extend ordering checks to use equivalent expressions without assuming that equality implies a strictly order-preserving transformation.
  • Update affected expected plans.

What is the testing strategy for this PR?

  • Add the issue’s reproduction to order.slt, verifying exact row order.
  • Add safety cases in equivalence_properties.slt and monotonic_projection_test.slt.
  • Verify the existing unbounded-stream cast-ordering test continues to pass.

Are there any user-facing changes?

Only that queries affected by #26745 now return rows in the requested order.

@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt) physical-plan Changes to the physical-plan crate labels Oct 25, 2026
@Kristofferjoha

Copy link
Copy Markdown
Contributor Author

@jayzhan211 This should be ready for review. It needed a little more than following #26654, as keeping only the merge key ordering also removed valid cast-derived orderings, causing an existing unbounded-stream query to fail planning. This PR also preserves projected cast relationships so those valid orderings can be inferred again.

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

Labels

physical-expr Changes to the physical-expr crates physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong results: order-preserving RepartitionExec keeps orderings of its input that do not hold after the merge

1 participant