-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-29520][SS] Fix checks of negative intervals #26177
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
Changes from 5 commits
a2d83b5
f880fd3
1f5d684
ab6036c
6afa006
084c8d5
2aa6480
1840a1d
4dab906
b212976
8cb55a7
8c2bcb1
edad25e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ | |
|
|
||
| package org.apache.spark.sql.catalyst.util | ||
|
|
||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| import org.apache.spark.sql.types.Decimal | ||
| import org.apache.spark.unsafe.types.CalendarInterval | ||
|
|
||
|
|
@@ -88,4 +90,32 @@ object IntervalUtils { | |
| result += MICROS_PER_MONTH * (interval.months % MONTHS_PER_YEAR) | ||
| Decimal(result, 18, 6) | ||
| } | ||
|
|
||
| /** | ||
| * Gets interval duration | ||
| * | ||
| * @param cal - the interval to get duration | ||
|
||
| * @param daysPerMonth - the number of days per one month | ||
| * @param targetUnit - time units of the result | ||
| * @return duration in the specified time units | ||
| */ | ||
| def getDuration( | ||
| cal: CalendarInterval, | ||
| daysPerMonth: Int, | ||
|
||
| targetUnit: TimeUnit): Long = { | ||
| val monthsDuration = Math.multiplyExact(daysPerMonth * DateTimeUtils.MICROS_PER_DAY, cal.months) | ||
| val result = Math.addExact(cal.microseconds, monthsDuration) | ||
| targetUnit.convert(result, TimeUnit.MICROSECONDS) | ||
| } | ||
|
|
||
| /** | ||
| * Checks the interval is negative | ||
| * | ||
| * @param cal - the checked interval | ||
| * @param daysPerMonth - the number of days per one month | ||
| * @return true if duration of the given interval is less than 0 otherwise false | ||
| */ | ||
| def isNegative(cal: CalendarInterval, daysPerMonth: Int): Boolean = { | ||
| getDuration(cal, daysPerMonth, TimeUnit.MICROSECONDS) < 0 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.sql.catalyst.util | ||
|
|
||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| import org.scalatest.Matchers | ||
|
|
||
| import org.apache.spark.SparkFunSuite | ||
| import org.apache.spark.sql.catalyst.util.IntervalUtils._ | ||
| import org.apache.spark.unsafe.types.CalendarInterval | ||
|
|
||
| class IntervalUtilsSuite extends SparkFunSuite with Matchers { | ||
| test("interval duration") { | ||
| def duration(s: String, daysPerMonth: Int, unit: TimeUnit): Long = { | ||
| getDuration(CalendarInterval.fromString(s), daysPerMonth, unit) | ||
| } | ||
|
|
||
| assert(duration("0 seconds", 31, TimeUnit.MILLISECONDS) === 0) | ||
| assert(duration("1 month", 31, TimeUnit.DAYS) === 31) | ||
| assert(duration("1 microsecond", 30, TimeUnit.MICROSECONDS) === 1) | ||
| assert(duration("1 month -30 days", 31, TimeUnit.DAYS) === 1) | ||
|
|
||
| try { | ||
| duration(Integer.MAX_VALUE + " month", 31, TimeUnit.SECONDS) | ||
| fail("Expected to throw an exception for the invalid input") | ||
| } catch { | ||
| case e: ArithmeticException => | ||
| assert(e.getMessage.contains("overflow")) | ||
| } | ||
| } | ||
|
|
||
| test("negative interval") { | ||
| def isNegative(s: String, daysPerMonth: Int): Boolean = { | ||
| IntervalUtils.isNegative(CalendarInterval.fromString(s), daysPerMonth) | ||
| } | ||
|
|
||
| assert(isNegative("-1 months", 28)) | ||
| assert(isNegative("-1 microsecond", 30)) | ||
| assert(isNegative("-1 month 30 days", 31)) | ||
| assert(isNegative("2 months -61 days", 30)) | ||
| assert(isNegative("-1 year -2 seconds", 30)) | ||
| assert(!isNegative("0 months", 28)) | ||
| assert(!isNegative("1 year -360 days", 31)) | ||
| assert(!isNegative("-1 year 380 days", 31)) | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,7 @@ private object Triggers { | |
|
|
||
| def convert(interval: String): Long = { | ||
| val cal = CalendarInterval.fromCaseInsensitiveString(interval) | ||
| if (cal.months > 0) { | ||
| if (cal.months != 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like another way of converting interval to duration: make sure the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can change def getDuration(
interval: CalendarInterval,
targetUnit: TimeUnit,
daysPerMonth: Option[Int] = Some(31)): Long = {
val monthsDuration = daysPerMonth
.map { days =>
Math.multiplyExact(days * DateTimeUtils.MICROS_PER_DAY, interval.months)
}.getOrElse {
if (interval.months == 0) {
0L
} else {
throw new IllegalArgumentException(s"Doesn't support month or year interval: $interval")
}
}
val result = Math.addExact(interval.microseconds, monthsDuration)
targetUnit.convert(result, TimeUnit.MICROSECONDS)
}and call
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but I am not sure that this check should be inside of |
||
| throw new IllegalArgumentException(s"Doesn't support month or year interval: $interval") | ||
| } | ||
| TimeUnit.MICROSECONDS.toMillis(cal.microseconds) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this comment can be removed, as no 31 can be seen here.