Skip to content

fix: skip volatile expressions when projecting equivalence classes - #27646

Draft
galaxytemple wants to merge 1 commit into
apache:mainfrom
galaxytemple:fix/project-volatile-equivalence
Draft

galaxytemple wants to merge 1 commit into
apache:mainfrom
galaxytemple:fix/project-volatile-equivalence

Conversation

@galaxytemple

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

When a projection outputs the same volatile expression more than once (e.g. SELECT floor(random() * 3) AS x, floor(random() * 3) AS y), EquivalenceGroup::project groups both targets under the same source expression key and places x and y in the same equivalence class (x = y).
And this false x = y equivalence causes downstream ORDER BY x, y (including across UNION ALL after #27347) to drop y from SortExec and return rows unsorted on y.

What changes are included in this PR?

  • Skip volatile source expressions (is_volatile(source)) at the start of the mapping.iter() loop in EquivalenceGroup::project so they do not form equivalence classes across projected targets.
  • Add a unit test in class.rs and a sqllogictest in union.slt.

What is the testing strategy for this PR?

Unit test (class.rs):

  • Projects a Volatility::Volatile UDF expression f(a) to two target columns (x and y) via ProjectionMapping and asserts that EquivalenceGroup::project returns an empty group (projected.is_empty()) instead of x=y.

Sqllogictest (union.slt):

  • Runs ORDER BY x, y on a UNION ALL query projecting floor(random() * 3) AS x, floor(random() * 3) AS y and checks the physical plan to verify that x and y are not treated as equivalent (both remain in SortExec).

Are there any user-facing changes?

No API changes. Bug fix only.

# Conflicts:
#	datafusion/physical-expr/src/equivalence/class.rs
@github-actions github-actions Bot added physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt) labels Nov 21, 2026
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 sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Projecting the same volatile expression twice creates a false equivalence and breaks ORDER BY

1 participant