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/12/07 20:58:46 UTC

[GitHub] [spark] amaliujia opened a new pull request, #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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

   <!--
   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.
   -->
   1. Add `types.py` and move to/from type proto to this file.
   2. Improve Cast proto to support both DataType and DataType as a string.
   3.  support cast to the following types in Python:
       ByteType,
       ShortType,
       IntegerType,
       FloatType,
       DayTimeIntervalType,
       MapType,
       StringType,
       DoubleType,
       LongType,
       DecimalType,
       BinaryType,
       BooleanType
   
   ### 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.
     5. If you fix a bug, you can clarify why it is a bug.
   -->
   API coverage
   
   ### 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.
   -->
   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] amaliujia commented on a diff in pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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


##########
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##########
@@ -43,7 +43,11 @@ message Expression {
     Expression expr = 1;
 
     // (Required) the data type that the expr to be casted to.
-    DataType cast_to_type = 2;
+    oneof cast_to_type {

Review Comment:
   Ah thanks. I didn't know this way to evolve the proto.
   
   Given Spark Connect is still alpha component though for now we don't need to be enforced to maintain the backwards compatibility.
   
   But when we are ready to leave from the alpha component then we should follow this way.



-- 
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] grundprinzip commented on a diff in pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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


##########
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/SparkConnectPlanner.scala:
##########
@@ -518,9 +518,16 @@ class SparkConnectPlanner(session: SparkSession) {
   }
 
   private def transformCast(cast: proto.Expression.Cast): Expression = {
-    Cast(
-      transformExpression(cast.getExpr),
-      DataTypeProtoConverter.toCatalystType(cast.getCastToType))
+    cast.getCastToTypeCase match {
+      case proto.Expression.Cast.CastToTypeCase.TYPE =>
+        Cast(
+          transformExpression(cast.getExpr),
+          DataTypeProtoConverter.toCatalystType(cast.getType))
+      case _ =>
+        Cast(
+          transformExpression(cast.getExpr),
+          session.sessionState.sqlParser.parseDataType(cast.getTypeStr))

Review Comment:
   When I looked at cast as a unicorn in the context of expressions, it's one of the few where not all argument types resolve to expressions. I was wondering if we could simplify the approach to make the type argument of cast an expression that can resolve as a string then we can do the matching of the expression in the analyzer.
   
   This is very similar to why you added the oneof to the proto 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] amaliujia commented on a diff in pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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


##########
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/SparkConnectPlanner.scala:
##########
@@ -518,9 +518,16 @@ class SparkConnectPlanner(session: SparkSession) {
   }
 
   private def transformCast(cast: proto.Expression.Cast): Expression = {
-    Cast(
-      transformExpression(cast.getExpr),
-      DataTypeProtoConverter.toCatalystType(cast.getCastToType))
+    cast.getCastToTypeCase match {
+      case proto.Expression.Cast.CastToTypeCase.TYPE =>
+        Cast(
+          transformExpression(cast.getExpr),
+          DataTypeProtoConverter.toCatalystType(cast.getType))
+      case _ =>
+        Cast(
+          transformExpression(cast.getExpr),
+          session.sessionState.sqlParser.parseDataType(cast.getTypeStr))

Review Comment:
   I am not following this? Can you give some example proto/sample code?
   
   Are you saying we do not use `oneof` but add the third string field?



-- 
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] amaliujia commented on a diff in pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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


##########
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##########
@@ -43,7 +43,11 @@ message Expression {
     Expression expr = 1;
 
     // (Required) the data type that the expr to be casted to.
-    DataType cast_to_type = 2;
+    oneof cast_to_type {

Review Comment:
   I think we are ok now. But later there is indeed a need to build a process/good practice etc. for how to evolve proto without breaking older versions (if possible)



-- 
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] amaliujia commented on a diff in pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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


##########
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/SparkConnectPlanner.scala:
##########
@@ -518,9 +518,16 @@ class SparkConnectPlanner(session: SparkSession) {
   }
 
   private def transformCast(cast: proto.Expression.Cast): Expression = {
-    Cast(
-      transformExpression(cast.getExpr),
-      DataTypeProtoConverter.toCatalystType(cast.getCastToType))
+    cast.getCastToTypeCase match {
+      case proto.Expression.Cast.CastToTypeCase.TYPE =>
+        Cast(
+          transformExpression(cast.getExpr),
+          DataTypeProtoConverter.toCatalystType(cast.getType))
+      case _ =>
+        Cast(
+          transformExpression(cast.getExpr),
+          session.sessionState.sqlParser.parseDataType(cast.getTypeStr))

Review Comment:
   I am not following this? Can you give some example proto/sample code?



-- 
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] grundprinzip commented on pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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

   My discussion was not meant to block the PR, this was more a more general observation. 
   
   LGTM, thanks!
   
   cc @HyukjinKwon @zhengruifeng 


-- 
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] zhengruifeng closed pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

Posted by GitBox <gi...@apache.org>.
zhengruifeng closed pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`
URL: https://github.com/apache/spark/pull/38970


-- 
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] zhengruifeng commented on pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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

   merged into master


-- 
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] grundprinzip commented on a diff in pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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


##########
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/planner/SparkConnectPlanner.scala:
##########
@@ -518,9 +518,16 @@ class SparkConnectPlanner(session: SparkSession) {
   }
 
   private def transformCast(cast: proto.Expression.Cast): Expression = {
-    Cast(
-      transformExpression(cast.getExpr),
-      DataTypeProtoConverter.toCatalystType(cast.getCastToType))
+    cast.getCastToTypeCase match {
+      case proto.Expression.Cast.CastToTypeCase.TYPE =>
+        Cast(
+          transformExpression(cast.getExpr),
+          DataTypeProtoConverter.toCatalystType(cast.getType))
+      case _ =>
+        Cast(
+          transformExpression(cast.getExpr),
+          session.sessionState.sqlParser.parseDataType(cast.getTypeStr))

Review Comment:
   Wouldn't it be nice if we could just add a second string argument to the `Cast` expression so it could resolved the expression automatically? Would this make the design easier? Because then you wouldn't even need a custom expression for cast.



##########
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##########
@@ -43,7 +43,11 @@ message Expression {
     Expression expr = 1;
 
     // (Required) the data type that the expr to be casted to.
-    DataType cast_to_type = 2;
+    oneof cast_to_type {

Review Comment:
   FYI: this is a breaking change in the proto message. Ideally, we would use
   
   ```
   reserved 2;
   oneof cast_to_type {
     DataType type = 3;
     string type_str = 4;
   }
   ```
   
   



##########
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##########
@@ -43,7 +43,11 @@ message Expression {
     Expression expr = 1;
 
     // (Required) the data type that the expr to be casted to.
-    DataType cast_to_type = 2;
+    oneof cast_to_type {
+      DataType type = 2;
+      // If this is set, Server will use Catalyst parser to parse this string to DataType.
+      string type_str = 3;

Review Comment:
   This comes back to the discussion that I had in my draft PR if the second argument to the `cast()` function should be modeled as an expression or not. Right now it takes a value but could be modeled as a string 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] grundprinzip commented on a diff in pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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


##########
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##########
@@ -43,7 +43,11 @@ message Expression {
     Expression expr = 1;
 
     // (Required) the data type that the expr to be casted to.
-    DataType cast_to_type = 2;
+    oneof cast_to_type {

Review Comment:
   We're ok now. We can use Buf to check for breaking changes. 



-- 
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] amaliujia commented on pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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

   cc @zhengruifeng 


-- 
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] amaliujia commented on a diff in pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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


##########
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##########
@@ -43,7 +43,11 @@ message Expression {
     Expression expr = 1;
 
     // (Required) the data type that the expr to be casted to.
-    DataType cast_to_type = 2;
+    oneof cast_to_type {
+      DataType type = 2;
+      // If this is set, Server will use Catalyst parser to parse this string to DataType.
+      string type_str = 3;

Review Comment:
   I add this to follow the design principle so far that we should move repeated clients implementation to the server side to reduce client side redundant work.
   
   Otherwise client side will need to implement `string` to `DataType` conversion and each client will need to the same thing.



-- 
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] amaliujia commented on a diff in pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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


##########
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##########
@@ -43,7 +43,11 @@ message Expression {
     Expression expr = 1;
 
     // (Required) the data type that the expr to be casted to.
-    DataType cast_to_type = 2;
+    oneof cast_to_type {

Review Comment:
   Ah thanks. I didn't know this way to evolve the proto.
   
   Given Spark Connect is still alpha component though for now we don't need to be enforced to maintain the backwards compatibility 



-- 
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] zhengruifeng commented on a diff in pull request #38970: [SPARK-41412][CONNECT] Implement `Column.cast`

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


##########
connector/connect/common/src/main/protobuf/spark/connect/expressions.proto:
##########
@@ -43,7 +43,11 @@ message Expression {
     Expression expr = 1;
 
     // (Required) the data type that the expr to be casted to.
-    DataType cast_to_type = 2;
+    oneof cast_to_type {

Review Comment:
   I think it's fine since this message has not been actually used?



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