viirya commented on a change in pull request #33525:
URL: https://github.com/apache/spark/pull/33525#discussion_r676909418



##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
##########
@@ -561,7 +561,17 @@ case class JsonToStructs(
 
   override def checkInputDataTypes(): TypeCheckResult = nullableSchema match {
     case _: StructType | _: ArrayType | _: MapType =>
-      super.checkInputDataTypes()
+      val checkMapKeyType = nullableSchema.existsRecursively( dataType => 
dataType match {
+        case MapType(keyType, _, _) if (keyType != StringType) => true

Review comment:
       nit: `if (keyType != StringType)` => `if keyType != StringType`

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
##########
@@ -561,7 +561,17 @@ case class JsonToStructs(
 
   override def checkInputDataTypes(): TypeCheckResult = nullableSchema match {
     case _: StructType | _: ArrayType | _: MapType =>
-      super.checkInputDataTypes()
+      val checkMapKeyType = nullableSchema.existsRecursively( dataType => 
dataType match {

Review comment:
       nit: `checkMapKeyType` => `invalidMapType`?

##########
File path: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala
##########
@@ -853,4 +854,32 @@ class JsonExpressionsSuite extends SparkFunSuite with 
ExpressionEvalHelper with
         checkEvaluation(JsonObjectKeys(Literal(input)), expected)
     }
   }
+
+  def checkMapTypeKeyError(schema: DataType, jsonData: String): Unit = {
+    assert( JsonToStructs(schema, Map.empty, 
Literal(jsonData)).checkInputDataTypes() match {
+      case TypeCheckFailure(_) => true
+      case _ => false
+    })
+  }
+
+  test("SPARK-35320: from_json should fail with a key type different of 
StringType") {
+    checkMapTypeKeyError(MapType(IntegerType, StringType), """{"1": "test"}""")
+  }
+
+  test("SPARK-35320: from_json should fail with a key type different of 
StringType " +
+    "anidated in a StructType") {
+    checkMapTypeKeyError(StructType(Seq(StructField("test", 
MapType(IntegerType, StringType)))),
+      """"test": {"1": "test"}""")
+  }
+
+  test("SPARK-35320: from_json should fail with a key type different of 
StringType " +
+    "anidated in a ArrayType") {
+    checkMapTypeKeyError(ArrayType(MapType(IntegerType, StringType)), 
"""[{"1": "test"}]""")
+  }
+
+  test("SPARK-35320: from_json should fail with a key type different of 
StringType " +
+    "anidated in a MapType") {
+    checkMapTypeKeyError(MapType(StringType, MapType(IntegerType, StringType)),
+    """{"key": {"1" : "test"}}""")
+  }

Review comment:
       Looks like you can have just one test and run through the combination of 
map types and json string.

##########
File path: 
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala
##########
@@ -853,4 +854,32 @@ class JsonExpressionsSuite extends SparkFunSuite with 
ExpressionEvalHelper with
         checkEvaluation(JsonObjectKeys(Literal(input)), expected)
     }
   }
+
+  def checkMapTypeKeyError(schema: DataType, jsonData: String): Unit = {
+    assert( JsonToStructs(schema, Map.empty, 
Literal(jsonData)).checkInputDataTypes() match {

Review comment:
       `assert( JsonToStructs(...` => `assert(JsonToStructs(...`

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
##########
@@ -561,7 +561,17 @@ case class JsonToStructs(
 
   override def checkInputDataTypes(): TypeCheckResult = nullableSchema match {
     case _: StructType | _: ArrayType | _: MapType =>
-      super.checkInputDataTypes()
+      val checkMapKeyType = nullableSchema.existsRecursively( dataType => 
dataType match {

Review comment:
       nit: `nullableSchema.existsRecursively( dataType` => 
`nullableSchema.existsRecursively(dataType`

##########
File path: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/jsonExpressions.scala
##########
@@ -561,7 +561,17 @@ case class JsonToStructs(
 
   override def checkInputDataTypes(): TypeCheckResult = nullableSchema match {
     case _: StructType | _: ArrayType | _: MapType =>
-      super.checkInputDataTypes()
+      val checkMapKeyType = nullableSchema.existsRecursively( dataType => 
dataType match {
+        case MapType(keyType, _, _) if (keyType != StringType) => true
+        case _ => false
+      })
+      if(checkMapKeyType) {

Review comment:
       style: `if(checkMapKeyType)` => `if (checkMapKeyType)`




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to