Skip to content

Commit 884c533

Browse files
committed
Address comments.
1 parent 760edad commit 884c533

File tree

3 files changed

+35
-81
lines changed

3 files changed

+35
-81
lines changed

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/JsonExpressionsSuite.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import java.util.Calendar
2121

2222
import org.apache.spark.SparkFunSuite
2323
import org.apache.spark.sql.catalyst.InternalRow
24+
import org.apache.spark.sql.catalyst.analysis.UnresolvedException
2425
import org.apache.spark.sql.catalyst.util.{DateTimeTestUtils, DateTimeUtils, GenericArrayData, PermissiveMode}
2526
import org.apache.spark.sql.types._
2627
import org.apache.spark.unsafe.types.UTF8String
@@ -610,4 +611,26 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
610611
"""{"t":"2015-12-31T16:00:00"}"""
611612
)
612613
}
614+
615+
test("to_json: verify MapType's value type instead of key type") {
616+
// Keys in map are treated as strings when converting to JSON. The type doesn't matter at all.
617+
val mapType1 = MapType(CalendarIntervalType, IntegerType)
618+
val schema1 = StructType(StructField("a", mapType1) :: Nil)
619+
val struct1 = Literal.create(null, schema1)
620+
checkEvaluation(
621+
StructsToJson(Map.empty, struct1, gmtId),
622+
null
623+
)
624+
625+
// The value type must be valid for converting to JSON.
626+
val mapType2 = MapType(IntegerType, CalendarIntervalType)
627+
val schema2 = StructType(StructField("a", mapType2) :: Nil)
628+
val struct2 = Literal.create(null, schema2)
629+
intercept[UnresolvedException[_]] {
630+
checkEvaluation(
631+
StructsToJson(Map.empty, struct2, gmtId),
632+
null
633+
)
634+
}
635+
}
613636
}

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/json/JacksonUtilsSuite.scala

Lines changed: 0 additions & 81 deletions
This file was deleted.

sql/core/src/test/scala/org/apache/spark/sql/JsonFunctionsSuite.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,18 @@ class JsonFunctionsSuite extends QueryTest with SharedSQLContext {
257257
"A type of keys and values in map() must be string, but got"))
258258
}
259259

260+
test("SPARK-21954: JacksonUtils should verify MapType's value type instead of key type") {
261+
// interval type is invalid for converting to JSON. However, the keys of a map are treated
262+
// as strings, so its type doesn't matter.
263+
checkAnswer(
264+
sql("SELECT to_json(struct(map(interval 1 second, 'a')))"),
265+
Row("""{"col1":{"interval 1 seconds":"a"}}""") :: Nil)
266+
val e = intercept[AnalysisException] {
267+
sql("SELECT to_json(struct(map('a', interval 1 second)))")
268+
}
269+
assert(e.getMessage.contains("Unable to convert column col1 of type calendarinterval to JSON"))
270+
}
271+
260272
test("SPARK-19967 Support from_json in SQL") {
261273
val df1 = Seq("""{"a": 1}""").toDS()
262274
checkAnswer(

0 commit comments

Comments
 (0)