Skip to content

Commit 63a6568

Browse files
committed
Roundtrip test for timestamp inferring
1 parent 5782de5 commit 63a6568

File tree

1 file changed

+25
-0
lines changed
  • sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/json

1 file changed

+25
-0
lines changed

sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/json/JsonSuite.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,4 +2607,29 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData {
26072607
assert(schemaOf("""{"a":null}""", """{"a":"2018-12-17T10:11:12.123-01:00"}""")
26082608
=== fromDDL("a timestamp"))
26092609
}
2610+
2611+
test("roundtrip for timestamp type inferring") {
2612+
val customSchema = new StructType(Array(StructField("date", TimestampType, true)))
2613+
withTempDir { dir =>
2614+
val timestampsWithFormatPath = s"${dir.getCanonicalPath}/timestampsWithFormat.json"
2615+
val timestampsWithFormat = spark.read
2616+
.option("timestampFormat", "dd/MM/yyyy HH:mm")
2617+
.json(datesRecords)
2618+
assert(timestampsWithFormat.schema === customSchema)
2619+
2620+
timestampsWithFormat.write
2621+
.format("json")
2622+
.option("timestampFormat", "yyyy-MM-dd HH:mm:ss")
2623+
.option(DateTimeUtils.TIMEZONE_OPTION, "UTC")
2624+
.save(timestampsWithFormatPath)
2625+
2626+
val readBack = spark.read
2627+
.option("timestampFormat", "yyyy-MM-dd HH:mm:ss")
2628+
.option(DateTimeUtils.TIMEZONE_OPTION, "UTC")
2629+
.json(timestampsWithFormatPath)
2630+
2631+
assert(readBack.schema === customSchema)
2632+
checkAnswer(readBack, timestampsWithFormat)
2633+
}
2634+
}
26102635
}

0 commit comments

Comments
 (0)