Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-48035][SQL] Fix try_add/try_multiply being semantic equal to add/multiply #46307

Closed

Conversation

db-scnakandala
Copy link
Contributor

@db-scnakandala db-scnakandala commented Apr 30, 2024

What changes were proposed in this pull request?

  • This PR fixes a correctness bug in commutative operator canonicalization where we currently do not take into account the evaluation mode during operand reordering.
  • As a result, the following condition will be incorrectly true:
val l1 = Literal(1)
val l2 = Literal(2)
val l3 = Literal(3)
val expr1 = Add(Add(l1, l2), l3)
val expr2 = Add(Add(l2, l1, EvalMode.TRY), l3)
expr1.semanticEquals(expr2) 
  • To fix the issue, we now reorder commutative operands only if all operators have the same evaluation mode.

Why are the changes needed?

  • To fix a correctness bug.

Does this PR introduce any user-facing change?

No

How was this patch tested?

  • Added unit tests

Was this patch authored or co-authored using generative AI tooling?

No

@github-actions github-actions bot added the SQL label Apr 30, 2024
@db-scnakandala db-scnakandala marked this pull request as draft April 30, 2024 18:19
@db-scnakandala db-scnakandala force-pushed the db-scnakandala/master branch 2 times, most recently from 87e0dfa to c2a2edd Compare April 30, 2024 18:47
@db-scnakandala db-scnakandala marked this pull request as ready for review April 30, 2024 22:13
@db-scnakandala db-scnakandala changed the title [SPARK-48035] Fix try_add/try_multiply being semantic equal to add/multiply [SPARK-48035][SQL] Fix try_add/try_multiply being semantic equal to add/multiply May 1, 2024
@db-scnakandala
Copy link
Contributor Author

cc: @cloud-fan

…essions/CanonicalizeSuite.scala

Co-authored-by: Hyukjin Kwon <[email protected]>
@HyukjinKwon
Copy link
Member

Merged to master.

// TODO: do not reorder consecutive `Add`s with different `evalMode`
val reorderResult = buildCanonicalizedPlan(
val evalModes = collectEvalModes(this, {case Add(_, _, evalMode) => Seq(evalMode)})
lazy val reorderResult = buildCanonicalizedPlan(
{ case Add(l, r, _) => Seq(l, r) },
Copy link
Contributor

Choose a reason for hiding this comment

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

shall we simply add check here? case Add(l, r, em) if em == evalMode

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is neat! I will create a follow-up PR.

dongjoon-hyun pushed a commit that referenced this pull request May 7, 2024
…equal to add/multiply

### What changes were proposed in this pull request?
- This is a follow-up to the previous PR: #46307.
- With the new changes we do the evalMode check in the `collectOperands` function instead of introducing a new function.

### Why are the changes needed?
- Better code quality and readability.

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
- Existing unit tests.

### Was this patch authored or co-authored using generative AI tooling?
- No

Closes #46414 from db-scnakandala/db-scnakandala/master.

Authored-by: Supun Nakandala <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
JacobZheng0927 pushed a commit to JacobZheng0927/spark that referenced this pull request May 11, 2024
…dd/multiply

### What changes were proposed in this pull request?
- This PR fixes a correctness bug in commutative operator canonicalization where we currently do not take into account the evaluation mode during operand reordering.
- As a result, the following condition will be incorrectly true:
```
val l1 = Literal(1)
val l2 = Literal(2)
val l3 = Literal(3)
val expr1 = Add(Add(l1, l2), l3)
val expr2 = Add(Add(l2, l1, EvalMode.TRY), l3)
expr1.semanticEquals(expr2)
```
- To fix the issue, we now reorder commutative operands only if all operators have the same evaluation mode.

### Why are the changes needed?
- To fix a correctness bug.

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
- Added unit tests

### Was this patch authored or co-authored using generative AI tooling?
No

Closes apache#46307 from db-scnakandala/db-scnakandala/master.

Authored-by: Supun Nakandala <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
JacobZheng0927 pushed a commit to JacobZheng0927/spark that referenced this pull request May 11, 2024
…equal to add/multiply

### What changes were proposed in this pull request?
- This is a follow-up to the previous PR: apache#46307.
- With the new changes we do the evalMode check in the `collectOperands` function instead of introducing a new function.

### Why are the changes needed?
- Better code quality and readability.

### Does this PR introduce _any_ user-facing change?
No

### How was this patch tested?
- Existing unit tests.

### Was this patch authored or co-authored using generative AI tooling?
- No

Closes apache#46414 from db-scnakandala/db-scnakandala/master.

Authored-by: Supun Nakandala <[email protected]>
Signed-off-by: Dongjoon Hyun <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants