@@ -392,7 +392,7 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with
392392 val jsonData = """ {"a": 1}"""
393393 val schema = StructType (StructField (" a" , IntegerType ) :: Nil )
394394 checkEvaluation(
395- JsonToStructs (schema, Map .empty, Literal (jsonData), gmtId),
395+ JsonToStructs (schema, Map .empty, Literal (jsonData), gmtId, true ),
396396 InternalRow (1 )
397397 )
398398 }
@@ -401,13 +401,13 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with
401401 val jsonData = """ {"a" 1}"""
402402 val schema = StructType (StructField (" a" , IntegerType ) :: Nil )
403403 checkEvaluation(
404- JsonToStructs (schema, Map .empty, Literal (jsonData), gmtId),
404+ JsonToStructs (schema, Map .empty, Literal (jsonData), gmtId, true ),
405405 null
406406 )
407407
408408 // Other modes should still return `null`.
409409 checkEvaluation(
410- JsonToStructs (schema, Map (" mode" -> PermissiveMode .name), Literal (jsonData), gmtId),
410+ JsonToStructs (schema, Map (" mode" -> PermissiveMode .name), Literal (jsonData), gmtId, true ),
411411 null
412412 )
413413 }
@@ -416,70 +416,70 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with
416416 val input = """ [{"a": 1}, {"a": 2}]"""
417417 val schema = ArrayType (StructType (StructField (" a" , IntegerType ) :: Nil ))
418418 val output = InternalRow (1 ) :: InternalRow (2 ) :: Nil
419- checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId), output)
419+ checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId, true ), output)
420420 }
421421
422422 test(" from_json - input=object, schema=array, output=array of single row" ) {
423423 val input = """ {"a": 1}"""
424424 val schema = ArrayType (StructType (StructField (" a" , IntegerType ) :: Nil ))
425425 val output = InternalRow (1 ) :: Nil
426- checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId), output)
426+ checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId, true ), output)
427427 }
428428
429429 test(" from_json - input=empty array, schema=array, output=empty array" ) {
430430 val input = " [ ]"
431431 val schema = ArrayType (StructType (StructField (" a" , IntegerType ) :: Nil ))
432432 val output = Nil
433- checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId), output)
433+ checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId, true ), output)
434434 }
435435
436436 test(" from_json - input=empty object, schema=array, output=array of single row with null" ) {
437437 val input = " { }"
438438 val schema = ArrayType (StructType (StructField (" a" , IntegerType ) :: Nil ))
439439 val output = InternalRow (null ) :: Nil
440- checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId), output)
440+ checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId, true ), output)
441441 }
442442
443443 test(" from_json - input=array of single object, schema=struct, output=single row" ) {
444444 val input = """ [{"a": 1}]"""
445445 val schema = StructType (StructField (" a" , IntegerType ) :: Nil )
446446 val output = InternalRow (1 )
447- checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId), output)
447+ checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId, true ), output)
448448 }
449449
450450 test(" from_json - input=array, schema=struct, output=null" ) {
451451 val input = """ [{"a": 1}, {"a": 2}]"""
452452 val schema = StructType (StructField (" a" , IntegerType ) :: Nil )
453453 val output = null
454- checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId), output)
454+ checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId, true ), output)
455455 }
456456
457457 test(" from_json - input=empty array, schema=struct, output=null" ) {
458458 val input = """ []"""
459459 val schema = StructType (StructField (" a" , IntegerType ) :: Nil )
460460 val output = null
461- checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId), output)
461+ checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId, true ), output)
462462 }
463463
464464 test(" from_json - input=empty object, schema=struct, output=single row with null" ) {
465465 val input = """ { }"""
466466 val schema = StructType (StructField (" a" , IntegerType ) :: Nil )
467467 val output = InternalRow (null )
468- checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId), output)
468+ checkEvaluation(JsonToStructs (schema, Map .empty, Literal (input), gmtId, true ), output)
469469 }
470470
471471 test(" from_json null input column" ) {
472472 val schema = StructType (StructField (" a" , IntegerType ) :: Nil )
473473 checkEvaluation(
474- JsonToStructs (schema, Map .empty, Literal .create(null , StringType ), gmtId),
474+ JsonToStructs (schema, Map .empty, Literal .create(null , StringType ), gmtId, true ),
475475 null
476476 )
477477 }
478478
479479 test(" SPARK-20549: from_json bad UTF-8" ) {
480480 val schema = StructType (StructField (" a" , IntegerType ) :: Nil )
481481 checkEvaluation(
482- JsonToStructs (schema, Map .empty, Literal (badJson), gmtId),
482+ JsonToStructs (schema, Map .empty, Literal (badJson), gmtId, true ),
483483 null )
484484 }
485485
@@ -491,14 +491,14 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with
491491 c.set(2016 , 0 , 1 , 0 , 0 , 0 )
492492 c.set(Calendar .MILLISECOND , 123 )
493493 checkEvaluation(
494- JsonToStructs (schema, Map .empty, Literal (jsonData1), gmtId),
494+ JsonToStructs (schema, Map .empty, Literal (jsonData1), gmtId, true ),
495495 InternalRow (c.getTimeInMillis * 1000L )
496496 )
497497 // The result doesn't change because the json string includes timezone string ("Z" here),
498498 // which means the string represents the timestamp string in the timezone regardless of
499499 // the timeZoneId parameter.
500500 checkEvaluation(
501- JsonToStructs (schema, Map .empty, Literal (jsonData1), Option (" PST" )),
501+ JsonToStructs (schema, Map .empty, Literal (jsonData1), Option (" PST" ), true ),
502502 InternalRow (c.getTimeInMillis * 1000L )
503503 )
504504
@@ -512,7 +512,8 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with
512512 schema,
513513 Map (" timestampFormat" -> " yyyy-MM-dd'T'HH:mm:ss" ),
514514 Literal (jsonData2),
515- Option (tz.getID)),
515+ Option (tz.getID),
516+ true ),
516517 InternalRow (c.getTimeInMillis * 1000L )
517518 )
518519 checkEvaluation(
@@ -521,7 +522,8 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with
521522 Map (" timestampFormat" -> " yyyy-MM-dd'T'HH:mm:ss" ,
522523 DateTimeUtils .TIMEZONE_OPTION -> tz.getID),
523524 Literal (jsonData2),
524- gmtId),
525+ gmtId,
526+ true ),
525527 InternalRow (c.getTimeInMillis * 1000L )
526528 )
527529 }
@@ -530,7 +532,7 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with
530532 test(" SPARK-19543: from_json empty input column" ) {
531533 val schema = StructType (StructField (" a" , IntegerType ) :: Nil )
532534 checkEvaluation(
533- JsonToStructs (schema, Map .empty, Literal .create(" " , StringType ), gmtId),
535+ JsonToStructs (schema, Map .empty, Literal .create(" " , StringType ), gmtId, true ),
534536 null
535537 )
536538 }
@@ -685,27 +687,23 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with
685687
686688 test(" from_json missing fields" ) {
687689 for (forceJsonNullableSchema <- Seq (false , true )) {
688- withSQLConf(SQLConf .FROM_JSON_FORCE_NULLABLE_SCHEMA .key -> forceJsonNullableSchema.toString) {
689- val input =
690- """ {
691- | "a": 1,
692- | "c": "foo"
693- |}
694- |""" .stripMargin
695- val jsonSchema = new StructType ()
696- .add(" a" , LongType , nullable = false )
697- .add(" b" , StringType , nullable = false )
698- .add(" c" , StringType , nullable = false )
699- val output = InternalRow (1L , null , UTF8String .fromString(" foo" ))
700- checkEvaluation(
701- JsonToStructs (jsonSchema, Map .empty, Literal .create(input, StringType ), gmtId),
702- output
703- )
704- val schema = JsonToStructs (jsonSchema, Map .empty, Literal .create(input, StringType ), gmtId)
705- .dataType
706- val schemaToCompare = if (forceJsonNullableSchema) jsonSchema.asNullable else jsonSchema
707- assert(schemaToCompare == schema)
708- }
690+ val input =
691+ """ {
692+ | "a": 1,
693+ | "c": "foo"
694+ |}
695+ |""" .stripMargin
696+ val jsonSchema = new StructType ()
697+ .add(" a" , LongType , nullable = false )
698+ .add(" b" , StringType , nullable = false )
699+ .add(" c" , StringType , nullable = false )
700+ val output = InternalRow (1L , null , UTF8String .fromString(" foo" ))
701+ val expr = JsonToStructs (
702+ jsonSchema, Map .empty, Literal .create(input, StringType ), gmtId, forceJsonNullableSchema)
703+ checkEvaluation(expr, output)
704+ val schema = expr.dataType
705+ val schemaToCompare = if (forceJsonNullableSchema) jsonSchema.asNullable else jsonSchema
706+ assert(schemaToCompare == schema)
709707 }
710708 }
711709}
0 commit comments