Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-30793][SQL][2.4] Fix truncations of timestamps before the epoch to minutes and seconds #27611

Closed
wants to merge 2 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1025,9 +1025,9 @@ object DateTimeUtils {
millis += offset
millis - millis % (60 * 60 * MILLIS_PER_SECOND) - offset
case TRUNC_TO_MINUTE =>
millis - millis % (60 * MILLIS_PER_SECOND)
millis - Math.floorMod(millis, 60 * MILLIS_PER_SECOND)
case TRUNC_TO_SECOND =>
millis - millis % MILLIS_PER_SECOND
millis - Math.floorMod(millis, MILLIS_PER_SECOND)
case TRUNC_TO_WEEK =>
val dDays = millisToDays(millis, timeZone)
val prevMonday = getNextDateForDayOfWeek(dDays - 7, MONDAY)
Expand Down
Expand Up @@ -729,4 +729,17 @@ class DateFunctionsSuite extends QueryTest with SharedSQLContext {
Row(Timestamp.valueOf("2015-07-24 07:00:00")),
Row(Timestamp.valueOf("2015-07-24 22:00:00"))))
}

test("SPARK-30793: truncate timestamps before the epoch to seconds and minutes") {
def checkTrunc(level: String, expected: String): Unit = {
val df = Seq("1961-04-12 00:01:02.345")
.toDF()
.select($"value".cast("timestamp").as("ts"))
.select(date_trunc(level, $"ts").cast("string"))
checkAnswer(df, Row(expected))
}

checkTrunc("SECOND", "1961-04-12 00:01:02")
checkTrunc("MINUTE", "1961-04-12 00:01:00")
}
}