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/08/19 04:06:49 UTC

[GitHub] [spark] gengliangwang opened a new pull request, #37576: [SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types

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

   <!--
   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.
   -->
   Currently,  for the following query it behaves differently between ansi mode and non-ansi mode:
   ```
   -- ANSI on
   > select cast('1.2' as int) 
   CAST_INVALID_INPUT ERROR
   ```
   ```
   -- ANSI off
   > select cast('1.2' as int) 
   1
   ```
   
   The change was introduced in https://github.com/apache/spark/commit/ac262cb27255f989f6a6dd864bd5114a928b96da. With the change, we can avoid the following query returning true under ANSI mode
   ```
   -- with ANSI on, both '1.2' and 1 will be cast as long type
   select '1.2' = 1
   ```
   
   However, the fraction strings should be allowed as per the SQL standard
   ![image](https://user-images.githubusercontent.com/1097932/185539714-9ed552e9-3a3a-4be1-b3b0-67d74c2b645e.png)
   ![image](https://user-images.githubusercontent.com/1097932/185539751-fa019c97-4c0e-418c-af4f-910309236a35.png)
   
   This PR is to change the syntax for explicit casting to allow converting fraction strings as integral types. If the cast is implicit(not an explicit cast from SQL query), then it remains unchanged.
   
   ### 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.
   -->
   To be more compliant with the SQL standard. Also, explicit casting string as integral types is now more consistent between the ANSI mode and the non-ANSI mode.
   
   ### 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'.
   -->
   Yes, it allows explicitly casting fraction strings as Integral types in ANSI mode.
   
   ### 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.
   -->
   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] gengliangwang commented on pull request #37576: [SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types

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

   cc @srielau as well


-- 
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 #37576: [SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types

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

   I think we do have a narrow wrist?
   ```
     override def implicitCast(e: Expression, expectedType: AbstractDataType): Option[Expression] = {
       implicitCast(e.dataType, expectedType).map { dt =>
         if (dt == e.dataType) e else Cast(e, dt)
       }
     }
   ```


-- 
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 #37576: [WIP][SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types

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

   It's hard to define which cast(string as int) syntax outside the explicit cast and implicit cast. 
   Given our current Cast implementation is complicated enough, I will close this one for now.


-- 
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 #37576: [SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types

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

   We need a narrow wrist anyway, otherwise we need to change many type coercion rules to pass a new parameter to cast.


-- 
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 #37576: [SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types

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

   @cloud-fan it doesn't cover all the type coercion rules


-- 
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 #37576: [SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types

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

   when adding implicit case for string to int, can we add a `AssertIntegral` expression? so the final expression will be `Cast(AssertIntegral(str_col), ...)`.


-- 
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 #37576: [SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types

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

   > when adding implicit case for string to int, can we add a AssertIntegral expression? so the final expression will be Cast(AssertIntegral(str_col), ...).
   
   The problem is that there are too many entries in the implicit cast(type coercion). Even if we fixed all of them, it is hard to guarantee the future code covers all the implicit cast. I would suggest adding a new flag/expression on the explicit cast. 


-- 
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 a diff in pull request #37576: [SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types

Posted by GitBox <gi...@apache.org>.
cloud-fan commented on code in PR #37576:
URL: https://github.com/apache/spark/pull/37576#discussion_r951126368


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -509,6 +509,10 @@ case class Cast(
     evalMode == EvalMode.TRY
   }
 
+  private def isUserSpecified: Boolean = {
+    getTagValue(Cast.USER_SPECIFIED_CAST).getOrElse(false)

Review Comment:
   We shouldn't rely on tree node tag to define the behavior, as tree node tag is unstable today, and we only use it for non-important use cases, such as pretty print and error message.



-- 
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 #37576: [WIP][SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types

Posted by GitBox <gi...@apache.org>.
gengliangwang closed pull request #37576: [WIP][SPARK-40143][SQL] ANSI mode: allow explicitly casting fraction strings as Integral types
URL: https://github.com/apache/spark/pull/37576


-- 
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