Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
address comments
  • Loading branch information
yaooqinn committed Jan 2, 2020
commit aba10b22bc3928dd88cb20ae0db340413ec0b3b6
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ object ExtractIntervalPart {
abstract class IntervalNumOperation(interval: Expression, num: Expression, operationName: String)
extends BinaryExpression with ImplicitCastInputTypes with Serializable {

protected val methodStr: String =
IntervalUtils.getClass.getName.stripSuffix("$") + "." + operationName + "Exact"

override def left: Expression = interval
override def right: Expression = num

Expand All @@ -125,41 +122,39 @@ abstract class IntervalNumOperation(interval: Expression, num: Expression, opera

override def nullable: Boolean = true

override def prettyName: String = operationName + "_interval"
}

case class MultiplyInterval(interval: Expression, num: Expression)
extends IntervalNumOperation(interval, num, "multiply") {

override def nullSafeEval(interval: Any, num: Any): Any = {
multiplyExact(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
override def nullSafeEval(interval: Any, num: Any): Any = operationName match {
case "multiply" =>
multiplyExact(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
case "divide" =>
if (num == 0) return null
Copy link
Contributor

Choose a reason for hiding this comment

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

shall we throw exception for divide by 0?

Copy link
Member Author

Choose a reason for hiding this comment

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

I did this because other numerics return null whether ansi or not

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a new operator, I think it's OK to always follow SQL standard here, since we are moving into this direction.

Copy link
Member Author

Choose a reason for hiding this comment

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

ok

Copy link
Member Author

Choose a reason for hiding this comment

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

shall we fix numerics / 0 when ANSI is on too in a followup?

divideExact(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
defineCodeGen(ctx, ev, (interval, num) => s"$methodStr($interval, $num)")
val methodStr: String =
IntervalUtils.getClass.getName.stripSuffix("$") + "." + operationName + "Exact"
operationName match {
case "multiply" => defineCodeGen(ctx, ev, (interval, num) => s"$methodStr($interval, $num)")
case "divide" => nullSafeCodeGen(ctx, ev, (interval, num) => {
s"""
|if ($num == 0) {
| ${ev.isNull} = true;
|} else {
| ${ev.value} = $methodStr($interval, $num);
|}
|""".stripMargin
})
}
}
}

case class DivideInterval(interval: Expression, num: Expression)
extends IntervalNumOperation(interval, num, "divide") {
override def prettyName: String = operationName + "_interval"
}

override def nullSafeEval(interval: Any, num: Any): Any = {
if (num == 0) return null
divideExact(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
}
case class MultiplyInterval(interval: Expression, num: Expression)
extends IntervalNumOperation(interval, num, "multiply")

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
nullSafeCodeGen(ctx, ev, (interval, num) => {
s"""
|if ($num == 0) {
| ${ev.isNull} = true;
|} else {
| ${ev.value} = $methodStr($interval, $num);
|}
|""".stripMargin
})
}
}
case class DivideInterval(interval: Expression, num: Expression)
extends IntervalNumOperation(interval, num, "divide")

// scalastyle:off line.size.limit
@ExpressionDescription(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ object IntervalUtils {

/**
* Unary minus, return the negated the calendar interval value.
*
*/
def negate(interval: CalendarInterval): CalendarInterval = {
new CalendarInterval(-interval.months, -interval.days, -interval.microseconds)
Expand Down Expand Up @@ -462,7 +461,6 @@ object IntervalUtils {
* Return a new calendar interval instance of the left interval minus the right one.
*
* @throws ArithmeticException if the result overflows any field value
*
*/
def subtractExact(left: CalendarInterval, right: CalendarInterval): CalendarInterval = {
val months = Math.subtractExact(left.months, right.months)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,12 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("multiply") {
def check(interval: String, num: Double, expected: String,
modes: Seq[String] = Seq("true", "false"), checkException: Boolean = false): Unit = {
modes.foreach { v =>
def check(
interval: String,
num: Double,
expected: String,
checkException: Boolean = false): Unit = {
Seq("true", "false").foreach { v =>
withSQLConf(SQLConf.ANSI_ENABLED.key -> v) {
if (checkException) {
checkExceptionInExpression[ArithmeticException](
Expand All @@ -222,15 +225,25 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
check("-100 years -1 millisecond", 0.5, "-50 years -500 microseconds")
check("2 months 4 seconds", -0.5, "-1 months -2 seconds")
check("1 month 2 microseconds", 1.5, "1 months 15 days 3 microseconds")
check("2 months", Int.MaxValue, CalendarInterval.MAX_VALUE.toString, checkException = true)
check("2 months", Int.MaxValue, null, checkException = true)
}

test("divide") {
def check(interval: String, num: Double, expected: String): Unit = {
def check(
interval: String,
num: Double,
expected: String,
checkException: Boolean = false): Unit = {
Seq("true", "false").foreach { v =>
withSQLConf(SQLConf.ANSI_ENABLED.key -> v) {
checkEvaluation(DivideInterval(Literal(stringToInterval(interval)), Literal(num)),
if (expected == null) null else stringToInterval(expected))
if (checkException) {
checkExceptionInExpression[ArithmeticException](
DivideInterval(Literal(stringToInterval(interval)), Literal(num)),
"integer overflow")
} else {
checkEvaluation(DivideInterval(Literal(stringToInterval(interval)), Literal(num)),
if (expected == null) null else stringToInterval(expected))
}
}
}
}
Expand All @@ -243,6 +256,7 @@ class IntervalExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
check("-1 month 2 microseconds", -0.25, "4 months -8 microseconds")
check("1 month 3 microsecond", 1.5, "20 days 2 microseconds")
check("1 second", 0, null)
check(s"${Int.MaxValue} months", 0.9, null, checkException = true)
}

test("make interval") {
Expand Down