Skip to content

Commit 9dbdf0a

Browse files
committed
Test for date type
1 parent bd47207 commit 9dbdf0a

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,45 @@ package org.apache.spark.sql.catalyst.json
2020
import com.fasterxml.jackson.core.JsonFactory
2121

2222
import org.apache.spark.SparkFunSuite
23-
import org.apache.spark.sql.types.TimestampType
23+
import org.apache.spark.sql.types._
2424

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)
2930
val factory = new JsonFactory()
30-
options.setJacksonOptions(factory)
31+
jsonOptions.setJacksonOptions(factory)
3132
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+
}
3254

33-
assert(inferSchema.inferField(parser) === TimestampType)
55+
def checkDateType(pattern: String, json: String): Unit = {
56+
checkType(Map("dateFormat" -> pattern), json, DateType)
3457
}
3558

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"}""")
3863
}
3964
}

0 commit comments

Comments
 (0)