You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/11/04 18:48:56 UTC

[GitHub] [spark] gengliangwang opened a new pull request, #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

gengliangwang opened a new pull request, #38513:
URL: https://github.com/apache/spark/pull/38513

   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a faster review.
     7. If you want to add a new configuration, please read the guideline first for naming configurations in
        'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
     8. If you want to add or modify an error type or message, please read the guideline first in
        'core/src/main/resources/error/README.md'.
   -->
   
   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   This is a follow-up of https://github.com/apache/spark/pull/38379. On second thought, if the canonicalized `Add` has a different type, casting it as the original data type can still match more semantically equivalent `Add`s 
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   A better solution for the issue https://issues.apache.org/jira/browse/SPARK-40903. We can avoid regressions on marking on certain semantically equivalent `Add`s as not equivalent.
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   No
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
   If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why it was difficult to add.
   If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
   -->
   New UT


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on PR #38513:
URL: https://github.com/apache/spark/pull/38513#issuecomment-1306748021

   We should not change query semantics after reordering, as this is canonicalization. It's hard to convince people that different result types still guarantee the same query semantic.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] gengliangwang commented on pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on PR #38513:
URL: https://github.com/apache/spark/pull/38513#issuecomment-1306246938

   > Adding cast only hides the bug
   
   The issue of SPARK-40903 is due to Spark calculating the data type of decimal Add conservatively. What do you mean by "bug"?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] gengliangwang commented on pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on PR #38513:
URL: https://github.com/apache/spark/pull/38513#issuecomment-1304002389

   cc @cloud-fan @srielau @ulysses-you @peter-toth 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] gengliangwang commented on a diff in pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on code in PR #38513:
URL: https://github.com/apache/spark/pull/38513#discussion_r1015000035


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -481,12 +481,12 @@ case class Add(
     // TODO: do not reorder consecutive `Add`s with different `evalMode`
     val reorderResult =
       orderCommutative({ case Add(l, r, _) => Seq(l, r) }).reduce(Add(_, _, evalMode))
-    if (resolved && reorderResult.resolved && reorderResult.dataType == dataType) {
-      reorderResult
+    if (resolved && reorderResult.resolved && reorderResult.dataType != dataType) {

Review Comment:
   @ulysses-you the current code seems fine. If there is a new data type in the future, we can still avoid the issue.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] cloud-fan commented on pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on PR #38513:
URL: https://github.com/apache/spark/pull/38513#issuecomment-1305100077

   I think canonicalization should not change the data type in the first place. Adding cast only hides the bug. What's worse, it doesn't help with the goal of canonicalization: match plans/expressions that are semantically equal, due to the extra cast.
   
   Can we be stricter on when we can reorder? e.g. add an allowlist and only reorder under certain cases, e.g. integral add with ansi off.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] gengliangwang closed pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
gengliangwang closed pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary
URL: https://github.com/apache/spark/pull/38513


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] gengliangwang commented on a diff in pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on code in PR #38513:
URL: https://github.com/apache/spark/pull/38513#discussion_r1014999188


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -481,12 +481,12 @@ case class Add(
     // TODO: do not reorder consecutive `Add`s with different `evalMode`
     val reorderResult =
       orderCommutative({ case Add(l, r, _) => Seq(l, r) }).reduce(Add(_, _, evalMode))
-    if (resolved && reorderResult.resolved && reorderResult.dataType == dataType) {
-      reorderResult
+    if (resolved && reorderResult.resolved && reorderResult.dataType != dataType) {
+      // SPARK-40903: Append cast for the canonicalization of decimal Add if the result data type is
+      // changed. Otherwise, it may cause data checking error within ComplexTypeMergingExpression.
+      Cast(reorderResult, dataType)

Review Comment:
   Yes, the cast is better after seconds thought.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] gengliangwang commented on pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
gengliangwang commented on PR #38513:
URL: https://github.com/apache/spark/pull/38513#issuecomment-1308060566

   It's true that having a cast in the canonicalized expression is hacky. I am closing this one and keeping the solution as it is in https://github.com/apache/spark/pull/38379


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] peter-toth commented on a diff in pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
peter-toth commented on code in PR #38513:
URL: https://github.com/apache/spark/pull/38513#discussion_r1014619447


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -481,12 +481,12 @@ case class Add(
     // TODO: do not reorder consecutive `Add`s with different `evalMode`
     val reorderResult =
       orderCommutative({ case Add(l, r, _) => Seq(l, r) }).reduce(Add(_, _, evalMode))
-    if (resolved && reorderResult.resolved && reorderResult.dataType == dataType) {
-      reorderResult
+    if (resolved && reorderResult.resolved && reorderResult.dataType != dataType) {
+      // SPARK-40903: Append cast for the canonicalization of decimal Add if the result data type is
+      // changed. Otherwise, it may cause data checking error within ComplexTypeMergingExpression.
+      Cast(reorderResult, dataType)

Review Comment:
   This seems to be the same idea that has come up previously: https://github.com/apache/spark/pull/38379#discussion_r1004195334 but my concerns (https://github.com/apache/spark/pull/38379#discussion_r1004215562) were wrong, so LGTM.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] ulysses-you commented on a diff in pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #38513:
URL: https://github.com/apache/spark/pull/38513#discussion_r1015003195


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -481,12 +481,12 @@ case class Add(
     // TODO: do not reorder consecutive `Add`s with different `evalMode`
     val reorderResult =
       orderCommutative({ case Add(l, r, _) => Seq(l, r) }).reduce(Add(_, _, evalMode))
-    if (resolved && reorderResult.resolved && reorderResult.dataType == dataType) {
-      reorderResult
+    if (resolved && reorderResult.resolved && reorderResult.dataType != dataType) {

Review Comment:
   I'm not worry about new data types, just want to avoid unnecessary logic for some unrelated data types like integer, float ..



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] ulysses-you commented on a diff in pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
ulysses-you commented on code in PR #38513:
URL: https://github.com/apache/spark/pull/38513#discussion_r1014959493


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -481,12 +481,12 @@ case class Add(
     // TODO: do not reorder consecutive `Add`s with different `evalMode`
     val reorderResult =
       orderCommutative({ case Add(l, r, _) => Seq(l, r) }).reduce(Add(_, _, evalMode))
-    if (resolved && reorderResult.resolved && reorderResult.dataType == dataType) {
-      reorderResult
+    if (resolved && reorderResult.resolved && reorderResult.dataType != dataType) {

Review Comment:
   for safe, can we do this for decimal type only ? e.g
   ```scala
   left match {
     case _: DecimalType if resolved && reorderResult.resolved && reorderResult.dataType != dataType =>
       Cast(reorderResult, dataType)
     case _ => reorderResult
   }
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] [spark] peter-toth commented on a diff in pull request #38513: [SPARK-40903][SQL][FOLLOWUP] Cast canonicalized Add as its original data type if necessary

Posted by GitBox <gi...@apache.org>.
peter-toth commented on code in PR #38513:
URL: https://github.com/apache/spark/pull/38513#discussion_r1014619447


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala:
##########
@@ -481,12 +481,12 @@ case class Add(
     // TODO: do not reorder consecutive `Add`s with different `evalMode`
     val reorderResult =
       orderCommutative({ case Add(l, r, _) => Seq(l, r) }).reduce(Add(_, _, evalMode))
-    if (resolved && reorderResult.resolved && reorderResult.dataType == dataType) {
-      reorderResult
+    if (resolved && reorderResult.resolved && reorderResult.dataType != dataType) {
+      // SPARK-40903: Append cast for the canonicalization of decimal Add if the result data type is
+      // changed. Otherwise, it may cause data checking error within ComplexTypeMergingExpression.
+      Cast(reorderResult, dataType)

Review Comment:
   This seems to be the same idea that came up previously: https://github.com/apache/spark/pull/38379#discussion_r1004195334 but my concerns (https://github.com/apache/spark/pull/38379#discussion_r1004215562) were wrong, so LGTM.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org