Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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 @@ -68,6 +68,17 @@ private object DateTimeFormatterHelper {
new DateTimeFormatterBuilder().parseCaseInsensitive()
}

def toWeekBasedFormatter(builder: DateTimeFormatterBuilder, locale: Locale): DateTimeFormatter = {
builder
.parseDefaulting(ChronoField.ERA, 1)
.parseDefaulting(ChronoField.DAY_OF_WEEK, 1)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.toFormatter(locale)
.withChronology(IsoChronology.INSTANCE)
.withResolverStyle(ResolverStyle.STRICT)
}

def toFormatter(builder: DateTimeFormatterBuilder, locale: Locale): DateTimeFormatter = {
builder
.parseDefaulting(ChronoField.ERA, 1)
Expand All @@ -80,9 +91,20 @@ private object DateTimeFormatterHelper {
.withResolverStyle(ResolverStyle.STRICT)
}

def isWeekBasedPattern(pattern: String): Boolean = {
Seq("YY", "w").exists(pattern.contains)
}

def buildFormatter(pattern: String, locale: Locale): DateTimeFormatter = {
val builder = createBuilder().appendPattern(pattern)
toFormatter(builder, locale)
// Default values for parser needs to be changed when the pattern
// is week/year based.
// DAY_OF_MONTH and MONTH_OF_YEAR will be added by the week of the year.
if (isWeekBasedPattern(pattern)) {
toWeekBasedFormatter(builder, locale)
} else {
toFormatter(builder, locale)
}
}

lazy val fractionFormatter: DateTimeFormatter = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
val fmt3 = "yy-MM-dd"
val sdf3 = new SimpleDateFormat(fmt3, Locale.US)
sdf3.setTimeZone(TimeZoneGMT)
val fmt4 = "YYYY-ww"

withDefaultTimeZone(TimeZoneGMT) {
for (tz <- Seq(TimeZoneGMT, TimeZonePST, TimeZoneJST)) {
Expand Down Expand Up @@ -853,6 +854,14 @@ class DateExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
MILLISECONDS.toSeconds(DateTimeUtils.daysToMillis(DateTimeUtils.fromJavaDate(date1), tz)))
checkEvaluation(
ToUnixTimestamp(Literal("2015-07-24"), Literal("not a valid format"), timeZoneId), null)
val expectedResults = Map(
TimeZoneGMT -> 1580688000L,
TimeZoneJST -> (1580688000L - 32400L),
TimeZonePST -> (1580688000L + 28800L))
// Sixth week from 2020 is: Monday, February 3, 2020 0:00:00 GMT: 1580688000
checkEvaluation(
ToUnixTimestamp(Literal("2020-06"), Literal(fmt4), timeZoneId),
expectedResults.get(tz).head)

// SPARK-28072 The codegen path for non-literal input should also work
checkEvaluation(
Expand Down