@@ -20,20 +20,45 @@ package org.apache.spark.sql.catalyst.json
20
20
import com .fasterxml .jackson .core .JsonFactory
21
21
22
22
import org .apache .spark .SparkFunSuite
23
- import org .apache .spark .sql .types .TimestampType
23
+ import org .apache .spark .sql .types ._
24
24
25
- class JsonInferSchemaSuite extends SparkFunSuite {
26
- def checkTimestampType (pattern : String , json : String ): Unit = {
27
- val options = new JSONOptions (Map (" timestampFormat" -> pattern), " GMT" , " " )
28
- val inferSchema = new JsonInferSchema (options)
25
+ class JsonInferSchemaSuite extends SparkFunSuite {
26
+
27
+ def checkType (options : Map [String , String ], json : String , `type` : DataType ): Unit = {
28
+ val jsonOptions = new JSONOptions (options, " GMT" , " " )
29
+ val inferSchema = new JsonInferSchema (jsonOptions)
29
30
val factory = new JsonFactory ()
30
- options .setJacksonOptions(factory)
31
+ jsonOptions .setJacksonOptions(factory)
31
32
val parser = CreateJacksonParser .string(factory, json)
33
+ parser.nextToken()
34
+ val expectedType = StructType (Seq (StructField (" a" , `type`, true )))
35
+
36
+ assert(inferSchema.inferField(parser) === expectedType)
37
+ }
38
+
39
+ def checkTimestampType (pattern : String , json : String ): Unit = {
40
+ checkType(Map (" timestampFormat" -> pattern), json, TimestampType )
41
+ }
42
+
43
+ test(" inferring timestamp type" ) {
44
+ checkTimestampType(" yyyy" , """ {"a": "2018"}""" )
45
+ checkTimestampType(" yyyy-MM" , """ {"a": "2018-12"}""" )
46
+ checkTimestampType(" yyyy-MM-dd" , """ {"a": "2018-12-02"}""" )
47
+ checkTimestampType(
48
+ " yyyy-MM-dd'T'HH:mm:ss.SSS" ,
49
+ """ {"a": "2018-12-02T21:04:00.123"}""" )
50
+ checkTimestampType(
51
+ " yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX" ,
52
+ """ {"a": "2018-12-02T21:04:00.123567+01:00"}""" )
53
+ }
32
54
33
- assert(inferSchema.inferField(parser) === TimestampType )
55
+ def checkDateType (pattern : String , json : String ): Unit = {
56
+ checkType(Map (" dateFormat" -> pattern), json, DateType )
34
57
}
35
58
36
- test(" Timestamp field types are inferred correctly via custom data format" ) {
37
- checkTimestampType(" yyyy-MM-dd'T'HH:mm:ss.SSSXXX" , " 2018-12-02T21:04:00.123" )
59
+ test(" inferring date type" ) {
60
+ checkDateType(" yyyy" , """ {"a": "2018"}""" )
61
+ checkDateType(" yyyy-MM" , """ {"a": "2018-12"}""" )
62
+ checkDateType(" yyyy-MM-dd" , """ {"a": "2018-12-02"}""" )
38
63
}
39
64
}
0 commit comments