MaxGekk commented on a change in pull request #24181: [SPARK-27242][SQL] Make
formatting TIMESTAMP/DATE literals independent from the default time zone
URL: https://github.com/apache/spark/pull/24181#discussion_r268156333
##########
File path:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/LiteralExpressionSuite.scala
##########
@@ -279,4 +280,40 @@ class LiteralExpressionSuite extends SparkFunSuite with
ExpressionEvalHelper {
checkEvaluation(Literal(Array(instant0, instant1)), Array(instant0,
instant1))
}
}
+
+ private def withTimeZones(
+ sessionTimeZone: String,
+ systemTimeZone: String)(f: => Unit): Unit = {
+ withSQLConf(
+ SQLConf.SESSION_LOCAL_TIMEZONE.key -> sessionTimeZone,
+ SQLConf.DATETIME_JAVA8API_EANBLED.key -> "true") {
+ val originTimeZone = TimeZone.getDefault
+ try {
+ TimeZone.setDefault(TimeZone.getTimeZone("GMT-08:00"))
+ f
+ } finally {
+ TimeZone.setDefault(originTimeZone)
+ }
+ }
+ }
+
+ test("format timestamp literal using spark.sql.session.timeZone") {
+ withTimeZones(sessionTimeZone = "GMT+01:00", systemTimeZone = "GMT-08:00")
{
+ val timestamp = LocalDateTime.of(2019, 3, 21, 0, 2, 3, 456000000)
+ .atZone(ZoneOffset.UTC)
+ .toInstant
+ val expected = "TIMESTAMP('2019-03-21 01:02:03.456')"
+ val literalStr = Literal.create(timestamp).sql
+ assert(literalStr === expected)
+ }
+ }
+
+ test("format date literal independently from time zone") {
+ withTimeZones(sessionTimeZone = "GMT-11:00", systemTimeZone = "GMT-10:00")
{
+ val date = LocalDate.of(2019, 3, 21)
+ val expected = "DATE '2019-03-21'"
Review comment:
I could only guess `TIMESTAMP(...)` was introduced when typed literals
wasn't supported by sql parser. Now we can remove the brackets I think. I
leaved them to not touch unrelated things.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]