Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import org.apache.spark.sql._
import org.apache.spark.sql.TestingUDT.IntervalData
import org.apache.spark.sql.catalyst.expressions.AttributeReference
import org.apache.spark.sql.catalyst.plans.logical.Filter
import org.apache.spark.sql.catalyst.util.{DateTimeTestUtils, DateTimeUtils}
import org.apache.spark.sql.catalyst.util.DateTimeTestUtils.{withDefaultTimeZone, UTC}
import org.apache.spark.sql.execution.SparkPlan
import org.apache.spark.sql.execution.datasources.{DataSource, FilePartition}
import org.apache.spark.sql.execution.datasources.v2.BatchScanExec
Expand Down Expand Up @@ -408,7 +408,7 @@ abstract class AvroSuite extends QueryTest with SharedSparkSession {
StructField("float", FloatType, true),
StructField("date", DateType, true)
))
DateTimeTestUtils.withDefaultTimeZone(DateTimeUtils.TimeZoneUTC) {
withDefaultTimeZone(UTC) {
val rdd = spark.sparkContext.parallelize(Seq(
Row(1f, null),
Row(2f, new Date(1451948400000L)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,14 @@ abstract class CastSuiteBase extends SparkFunSuite with ExpressionEvalHelper {
}

test("cast string to timestamp") {
new ParVector(ALL_TIMEZONES.toVector).foreach { tz =>
new ParVector(ALL_TIMEZONES.toVector).foreach { zid =>
def checkCastStringToTimestamp(str: String, expected: Timestamp): Unit = {
checkEvaluation(cast(Literal(str), TimestampType, Option(tz.getID)), expected)
checkEvaluation(cast(Literal(str), TimestampType, Option(zid.getId)), expected)
}

checkCastStringToTimestamp("123", null)

val tz = TimeZone.getTimeZone(zid)
var c = Calendar.getInstance(tz)
c.set(2015, 0, 1, 0, 0, 0)
c.set(Calendar.MILLISECOND, 0)
Expand Down Expand Up @@ -262,10 +263,10 @@ abstract class CastSuiteBase extends SparkFunSuite with ExpressionEvalHelper {
val zts = sd + " 00:00:00"
val sts = sd + " 00:00:02"
val nts = sts + ".1"
val ts = withDefaultTimeZone(TimeZoneGMT)(Timestamp.valueOf(nts))
val ts = withDefaultTimeZone(UTC)(Timestamp.valueOf(nts))

for (tz <- ALL_TIMEZONES) {
val timeZoneId = Option(tz.getID)
val timeZoneId = Option(tz.getId)
var c = Calendar.getInstance(TimeZoneGMT)
c.set(2015, 2, 8, 2, 30, 0)
checkEvaluation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
DateTimeUtils.millisToMicros(Timestamp.valueOf(s).getTime - offset)
}

DateTimeTestUtils.withDefaultTimeZone(timeZone) {
DateTimeTestUtils.withDefaultTimeZone(timeZone.toZoneId) {
// Spring time change
checkEvaluation(new Sequence(
Literal(Timestamp.valueOf("2018-03-25 01:30:00")),
Expand Down Expand Up @@ -880,7 +880,7 @@ class CollectionExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper
}

test("Sequence of dates") {
DateTimeTestUtils.withDefaultTimeZone(TimeZone.getTimeZone(UTC)) {
DateTimeTestUtils.withDefaultTimeZone(UTC) {
checkEvaluation(new Sequence(
Literal(Date.valueOf("2018-01-01")),
Literal(Date.valueOf("2018-01-05")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.catalyst.expressions

import java.text.SimpleDateFormat
import java.util.{Calendar, Locale}
import java.util.{Calendar, Locale, TimeZone}

import org.scalatest.exceptions.TestFailedException

Expand Down Expand Up @@ -89,23 +89,23 @@ class CsvExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with P
)

val csvData2 = "2016-01-01T00:00:00"
for (tz <- DateTimeTestUtils.outstandingTimezones) {
c = Calendar.getInstance(tz)
for (zid <- DateTimeTestUtils.outstandingZoneIds) {
c = Calendar.getInstance(TimeZone.getTimeZone(zid))
c.set(2016, 0, 1, 0, 0, 0)
c.set(Calendar.MILLISECOND, 0)
checkEvaluation(
CsvToStructs(
schema,
Map("timestampFormat" -> "yyyy-MM-dd'T'HH:mm:ss"),
Literal(csvData2),
Option(tz.getID)),
Option(zid.getId)),
InternalRow(c.getTimeInMillis * 1000L)
)
checkEvaluation(
CsvToStructs(
schema,
Map("timestampFormat" -> "yyyy-MM-dd'T'HH:mm:ss",
DateTimeUtils.TIMEZONE_OPTION -> tz.getID),
DateTimeUtils.TIMEZONE_OPTION -> zid.getId),
Literal(csvData2),
UTC_OPT),
InternalRow(c.getTimeInMillis * 1000L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(Second(Literal(ts), UTC_OPT), 15)

val c = Calendar.getInstance()
for (tz <- outstandingTimezones) {
val timeZoneId = Option(tz.getID)
c.setTimeZone(tz)
for (zid <- outstandingZoneIds) {
val timeZoneId = Option(zid.getId)
c.setTimeZone(TimeZone.getTimeZone(zid))
(0 to 60 by 5).foreach { s =>
c.set(2015, 18, 3, 3, 5, s)
checkEvaluation(
Expand Down Expand Up @@ -285,9 +285,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(Hour(Literal(ts), UTC_OPT), 13)

val c = Calendar.getInstance()
for (tz <- outstandingTimezones) {
val timeZoneId = Option(tz.getID)
c.setTimeZone(tz)
for (zid <- outstandingZoneIds) {
val timeZoneId = Option(zid.getId)
c.setTimeZone(TimeZone.getTimeZone(zid))
(0 to 24 by 6).foreach { h =>
(0 to 60 by 30).foreach { m =>
(0 to 60 by 30).foreach { s =>
Expand All @@ -312,9 +312,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(Minute(Literal(ts), UTC_OPT), 10)

val c = Calendar.getInstance()
for (tz <- outstandingTimezones) {
val timeZoneId = Option(tz.getID)
c.setTimeZone(tz)
for (zid <- outstandingZoneIds) {
val timeZoneId = Option(zid.getId)
c.setTimeZone(TimeZone.getTimeZone(zid))
(0 to 60 by 5).foreach { m =>
(0 to 60 by 15).foreach { s =>
c.set(2015, 18, 3, 3, m, s)
Expand Down Expand Up @@ -384,9 +384,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {

test("time_add") {
val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US)
for (tz <- outstandingTimezones) {
val timeZoneId = Option(tz.getID)
sdf.setTimeZone(tz)
for (zid <- outstandingZoneIds) {
val timeZoneId = Option(zid.getId)
sdf.setTimeZone(TimeZone.getTimeZone(zid))

checkEvaluation(
TimeAdd(
Expand Down Expand Up @@ -422,9 +422,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {

test("time_sub") {
val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US)
for (tz <- outstandingTimezones) {
val timeZoneId = Option(tz.getID)
sdf.setTimeZone(tz)
for (zid <- outstandingZoneIds) {
val timeZoneId = Option(zid.getId)
sdf.setTimeZone(TimeZone.getTimeZone(zid))

checkEvaluation(
TimeSub(
Expand Down Expand Up @@ -508,9 +508,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {

test("months_between") {
val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US)
for (tz <- outstandingTimezones) {
val timeZoneId = Option(tz.getID)
sdf.setTimeZone(tz)
for (zid <- outstandingZoneIds) {
val timeZoneId = Option(zid.getId)
sdf.setTimeZone(TimeZone.getTimeZone(zid))

checkEvaluation(
MonthsBetween(
Expand Down Expand Up @@ -641,7 +641,7 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
expected)
}

withDefaultTimeZone(TimeZone.getTimeZone(UTC)) {
withDefaultTimeZone(UTC) {
val inputDate = Timestamp.valueOf("2015-07-22 05:30:06")

Seq("yyyy", "YYYY", "year", "YEAR", "yy", "YY").foreach { fmt =>
Expand Down Expand Up @@ -711,8 +711,9 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
val sdf1 = new SimpleDateFormat(fmt1, Locale.US)
val fmt2 = "yyyy-MM-dd HH:mm:ss.SSS"
val sdf2 = new SimpleDateFormat(fmt2, Locale.US)
for (tz <- outstandingTimezones) {
val timeZoneId = Option(tz.getID)
for (zid <- outstandingZoneIds) {
val timeZoneId = Option(zid.getId)
val tz = TimeZone.getTimeZone(zid)
sdf1.setTimeZone(tz)
sdf2.setTimeZone(tz)

Expand Down Expand Up @@ -762,9 +763,10 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
val sdf3 = new SimpleDateFormat(fmt3, Locale.US)
sdf3.setTimeZone(TimeZone.getTimeZone(UTC))

withDefaultTimeZone(TimeZone.getTimeZone(UTC)) {
for (tz <- outstandingTimezones) {
val timeZoneId = Option(tz.getID)
withDefaultTimeZone(UTC) {
for (zid <- outstandingZoneIds) {
val timeZoneId = Option(zid.getId)
val tz = TimeZone.getTimeZone(zid)
sdf1.setTimeZone(tz)
sdf2.setTimeZone(tz)

Expand Down Expand Up @@ -829,9 +831,10 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
val sdf3 = new SimpleDateFormat(fmt3, Locale.US)
sdf3.setTimeZone(TimeZone.getTimeZone(UTC))

withDefaultTimeZone(TimeZone.getTimeZone(UTC)) {
for (tz <- outstandingTimezones) {
val timeZoneId = Option(tz.getID)
withDefaultTimeZone(UTC) {
for (zid <- outstandingZoneIds) {
val timeZoneId = Option(zid.getId)
val tz = TimeZone.getTimeZone(zid)
sdf1.setTimeZone(tz)
sdf2.setTimeZone(tz)

Expand All @@ -847,7 +850,7 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(
ToUnixTimestamp(Literal(date1), Literal(fmt1), timeZoneId),
MICROSECONDS.toSeconds(
DateTimeUtils.daysToMicros(DateTimeUtils.fromJavaDate(date1), tz.toZoneId)))
DateTimeUtils.daysToMicros(DateTimeUtils.fromJavaDate(date1), zid)))
checkEvaluation(
ToUnixTimestamp(
Literal(sdf2.format(new Timestamp(-1000000))),
Expand All @@ -856,7 +859,7 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(ToUnixTimestamp(
Literal(sdf3.format(Date.valueOf("2015-07-24"))), Literal(fmt3), timeZoneId),
MICROSECONDS.toSeconds(DateTimeUtils.daysToMicros(
DateTimeUtils.fromJavaDate(Date.valueOf("2015-07-24")), tz.toZoneId)))
DateTimeUtils.fromJavaDate(Date.valueOf("2015-07-24")), zid)))
val t1 = ToUnixTimestamp(
CurrentTimestamp(), Literal(fmt1)).eval().asInstanceOf[Long]
val t2 = ToUnixTimestamp(
Expand All @@ -871,7 +874,7 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(ToUnixTimestamp(
Literal(date1), Literal.create(null, StringType), timeZoneId),
MICROSECONDS.toSeconds(
DateTimeUtils.daysToMicros(DateTimeUtils.fromJavaDate(date1), tz.toZoneId)))
DateTimeUtils.daysToMicros(DateTimeUtils.fromJavaDate(date1), zid)))
checkEvaluation(
ToUnixTimestamp(
Literal("2015-07-24"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,23 +517,23 @@ class JsonExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper with
)

val jsonData2 = """{"t": "2016-01-01T00:00:00"}"""
for (tz <- DateTimeTestUtils.outstandingTimezones) {
c = Calendar.getInstance(tz)
for (zid <- DateTimeTestUtils.outstandingZoneIds) {
c = Calendar.getInstance(TimeZone.getTimeZone(zid))
c.set(2016, 0, 1, 0, 0, 0)
c.set(Calendar.MILLISECOND, 0)
checkEvaluation(
JsonToStructs(
schema,
Map("timestampFormat" -> "yyyy-MM-dd'T'HH:mm:ss"),
Literal(jsonData2),
Option(tz.getID)),
Option(zid.getId)),
InternalRow(c.getTimeInMillis * 1000L)
)
checkEvaluation(
JsonToStructs(
schema,
Map("timestampFormat" -> "yyyy-MM-dd'T'HH:mm:ss",
DateTimeUtils.TIMEZONE_OPTION -> tz.getID),
DateTimeUtils.TIMEZONE_OPTION -> zid.getId),
Literal(jsonData2),
UTC_OPT),
InternalRow(c.getTimeInMillis * 1000L)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,10 @@ class ExpressionParserSuite extends AnalysisTest {
}

test("timestamp literals") {
DateTimeTestUtils.outstandingTimezones.foreach { timeZone =>
withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> timeZone.getID) {
DateTimeTestUtils.outstandingZoneIds.foreach { zid =>
withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> zid.getId) {
def toMicros(time: LocalDateTime): Long = {
val seconds = time.atZone(timeZone.toZoneId).toInstant.getEpochSecond
val seconds = time.atZone(zid).toInstant.getEpochSecond
TimeUnit.SECONDS.toMicros(seconds)
}
assertEval(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import java.time.{LocalDate, LocalDateTime, LocalTime, ZoneId, ZoneOffset}
import java.util.TimeZone
import java.util.concurrent.TimeUnit

import scala.collection.JavaConverters._

import org.apache.spark.sql.catalyst.util.DateTimeConstants._
import org.apache.spark.sql.catalyst.util.DateTimeUtils.getZoneId

Expand All @@ -39,7 +41,7 @@ object DateTimeTestUtils {

val UTC_OPT = Option("UTC")

val ALL_TIMEZONES: Seq[TimeZone] = TimeZone.getAvailableIDs.toSeq.map(TimeZone.getTimeZone)
val ALL_TIMEZONES: Seq[ZoneId] = ZoneId.getAvailableZoneIds.asScala.map(getZoneId).toSeq
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should only test the outstandingZoneIds instead of ALL_TIMEZONES. The test can be very slow if we test all timezones.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already discussed this some time ago, see #22657 . We replaced ALL_TIMEZONES by outstanding time zones in some places but decided to leave in date-time tests. @srowen proposed to run tests for ALL_TIMEZONES in parallel, see #22672 (#22672 (comment)). Total time of testing all time zone in parallel ~0.24 seconds. Not so much, I think.


val outstandingTimezonesIds: Seq[String] = Seq(
"UTC",
Expand All @@ -50,16 +52,15 @@ object DateTimeTestUtils {
"Antarctica/Vostok",
"Asia/Hong_Kong",
"Europe/Amsterdam")
val outstandingTimezones: Seq[TimeZone] = outstandingTimezonesIds.map(TimeZone.getTimeZone)
val outstandingZoneIds: Seq[ZoneId] = outstandingTimezonesIds.map(DateTimeUtils.getZoneId)
val outstandingZoneIds: Seq[ZoneId] = outstandingTimezonesIds.map(getZoneId)

def withDefaultTimeZone[T](newDefaultTimeZone: TimeZone)(block: => T): T = {
val originalDefaultTimeZone = TimeZone.getDefault
def withDefaultTimeZone[T](newDefaultTimeZone: ZoneId)(block: => T): T = {
val originalDefaultTimeZone = ZoneId.systemDefault()
try {
TimeZone.setDefault(newDefaultTimeZone)
TimeZone.setDefault(TimeZone.getTimeZone(newDefaultTimeZone))
block
} finally {
TimeZone.setDefault(originalDefaultTimeZone)
TimeZone.setDefault(TimeZone.getTimeZone(originalDefaultTimeZone))
}
}

Expand Down
Loading