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/04/27 10:17:16 UTC

[GitHub] [spark] HyukjinKwon opened a new pull request, #36375: [SPARK-39015][SQL] Remove the usage of toSQLValue(v) without an explicit type

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

   ### What changes were proposed in this pull request?
   
   This PR is a backport of https://github.com/apache/spark/pull/36351
   
   This PR proposes to remove the the usage of `toSQLValue(v)` without an explicit type.
   
   `Literal(v)` is intended to be used from end-users so it cannot handle our internal types such as `UTF8String` and `ArrayBasedMapData`. Using this method can lead to unexpected error messages such as:
   
   ```
   Caused by: org.apache.spark.SparkRuntimeException: [UNSUPPORTED_FEATURE] The feature is not supported: literal for 'hair' of class org.apache.spark.unsafe.types.UTF8String.
     at org.apache.spark.sql.errors.QueryExecutionErrors$.literalTypeUnsupportedError(QueryExecutionErrors.scala:241)
     at org.apache.spark.sql.catalyst.expressions.Literal$.apply(literals.scala:99)
     at org.apache.spark.sql.errors.QueryErrorsBase.toSQLValue(QueryErrorsBase.scala:45)
     ...
   ```
   
   Since It is impossible to have the corresponding data type from the internal types as one type can map to multiple external types (e.g., `Long` for `Timestamp`, `TimestampNTZ`, and `LongType`), the removal approach was taken.
   
   ### Why are the changes needed?
   
   To provide the error messages as intended.
   
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes.
   
   ```scala
   import org.apache.spark.sql.Row
   import org.apache.spark.sql.types.StructType
   import org.apache.spark.sql.types.StringType
   import org.apache.spark.sql.types.DataTypes
   
   val arrayStructureData = Seq(
   Row(Map("hair"->"black", "eye"->"brown")),
   Row(Map("hair"->"blond", "eye"->"blue")),
   Row(Map()))
   
   val mapType  = DataTypes.createMapType(StringType, StringType)
   
   val arrayStructureSchema = new StructType().add("properties", mapType)
   
   val mapTypeDF = spark.createDataFrame(
       spark.sparkContext.parallelize(arrayStructureData),arrayStructureSchema)
   
   spark.conf.set("spark.sql.ansi.enabled", true)
   mapTypeDF.selectExpr("element_at(properties, 'hair')").show
   ```
   
   Before:
   
   ```
   Caused by: org.apache.spark.SparkRuntimeException: [UNSUPPORTED_FEATURE] The feature is not supported: literal for 'hair' of class org.apache.spark.unsafe.types.UTF8String.
     at org.apache.spark.sql.errors.QueryExecutionErrors$.literalTypeUnsupportedError(QueryExecutionErrors.scala:241)
     at org.apache.spark.sql.catalyst.expressions.Literal$.apply(literals.scala:99)
     at org.apache.spark.sql.errors.QueryErrorsBase.toSQLValue(QueryErrorsBase.scala:45)
     ...
   ```
   
   After:
   
   ```
   Caused by: org.apache.spark.SparkNoSuchElementException: [MAP_KEY_DOES_NOT_EXIST] Key 'hair' does not exist. To return NULL instead, use 'try_element_at'. If necessary set spark.sql.ansi.enabled to false to bypass this error.
   == SQL(line 1, position 0) ==
   element_at(properties, 'hair')
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   ```
   
   ### How was this patch tested?
   
   Unittest was added. Otherwise, existing test cases should cover.


-- 
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] MaxGekk commented on pull request #36375: [SPARK-39015][SQL][3.3] Remove the usage of toSQLValue(v) without an explicit type

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

   +1, LGTM. Merging to 3.3.
   Thank you, @HyukjinKwon.


-- 
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] MaxGekk closed pull request #36375: [SPARK-39015][SQL][3.3] Remove the usage of toSQLValue(v) without an explicit type

Posted by GitBox <gi...@apache.org>.
MaxGekk closed pull request #36375: [SPARK-39015][SQL][3.3] Remove the usage of toSQLValue(v) without an explicit type
URL: https://github.com/apache/spark/pull/36375


-- 
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] HyukjinKwon commented on a diff in pull request #36375: [SPARK-39015][SQL][3.3] Remove the usage of toSQLValue(v) without an explicit type

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


##########
sql/core/src/test/resources/sql-tests/results/ansi/map.sql.out:
##########
@@ -118,3 +118,15 @@ Key 5 does not exist. To return NULL instead, use 'try_element_at'. If necessary
 == SQL(line 1, position 7) ==
 select element_at(map(1, 'a', 2, 'b'), 5)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+
+-- !query
+select element_at(map('a', 1, 'b', 2), 'c')
+-- !query schema
+struct<>
+-- !query output
+org.apache.spark.SparkNoSuchElementException
+[MAP_KEY_DOES_NOT_EXIST] Key 'c' does not exist. To return NULL instead, use 'try_element_at'. If necessary set spark.sql.ansi.enabled to false to bypass this error.

Review Comment:
   ```suggestion
   Key 'c' does not exist. To return NULL instead, use 'try_element_at'. If necessary set spark.sql.ansi.enabled to false to bypass this error.
   ```



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