Skip to content
Closed
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
rm try-catch
  • Loading branch information
yaooqinn committed Dec 27, 2019
commit a8d29b6eaa67a4471c74a0d0b652b001d66633bc
Original file line number Diff line number Diff line change
Expand Up @@ -113,93 +113,65 @@ object ExtractIntervalPart {
}
}

abstract class IntervalNumOperation(interval: Expression, num: Expression)
abstract class IntervalNumOperation(interval: Expression, num: Expression, operationName: String)
extends BinaryExpression with ImplicitCastInputTypes with Serializable {

protected val checkOverflow: Boolean = SQLConf.get.ansiEnabled

protected val operation: String =
IntervalUtils.getClass.getName.stripSuffix("$") + "." + {
if (checkOverflow) operationName + "Exact" else operationName
}

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

override def inputTypes: Seq[AbstractDataType] = Seq(CalendarIntervalType, DoubleType)

override def dataType: DataType = CalendarIntervalType

override def nullable: Boolean = true

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

case class MultiplyInterval(interval: Expression, num: Expression)
Copy link
Contributor

Choose a reason for hiding this comment

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

is this also new in Spark 3.0? If it is I think it's OK to always follow the ansi behavior regardless the ansi flag.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, it is

extends IntervalNumOperation(interval, num) {

override def prettyName: String = "multiply_interval"
extends IntervalNumOperation(interval, num, "multiply") {

override def nullSafeEval(interval: Any, num: Any): Any = {
try {
if (checkOverflow) {
multiplyExact(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
} else {
multiply(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
}
} catch {
case _: ArithmeticException if !checkOverflow => null
if (checkOverflow) {
multiplyExact(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
} else {
multiply(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
}
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
nullSafeCodeGen(ctx, ev, (interval, num) => {
val iu = IntervalUtils.getClass.getName.stripSuffix("$")
val operationName = if (checkOverflow) "multiplyExact" else "multiply"
s"""
try {
${ev.value} = $iu.$operationName($interval, $num);
} catch (ArithmeticException e) {
if ($checkOverflow) {
throw e;
} else {
${ev.isNull} = true;
}
}
"""
})
defineCodeGen(ctx, ev, (interval, num) => s"$operation($interval, $num)")
}
}

case class DivideInterval(interval: Expression, num: Expression)
extends IntervalNumOperation(interval, num) {

override def prettyName: String = "divide_interval"
extends IntervalNumOperation(interval, num, "divide") {

override def nullSafeEval(interval: Any, num: Any): Any = {
try {
if (num == 0) return null
if (checkOverflow) {
divideExact(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
} else {
divide(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
}
} catch {
case _: ArithmeticException if !checkOverflow => null
if (num == 0) return null
if (checkOverflow) {
divideExact(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
} else {
divide(interval.asInstanceOf[CalendarInterval], num.asInstanceOf[Double])
}
}

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
nullSafeCodeGen(ctx, ev, (interval, num) => {
val iu = IntervalUtils.getClass.getName.stripSuffix("$")
val operationName = if (checkOverflow) "divideExact" else "divide"
s"""
try {
if ($num == 0) {
${ev.isNull} = true;
} else {
${ev.value} = $iu.$operationName($interval, $num);
}
} catch (ArithmeticException e) {
if ($checkOverflow) {
throw e;
} else {
${ev.isNull} = true;
}
}
"""
|if ($num == 0) {
| ${ev.isNull} = true;
|} else {
| ${ev.value} = $operation($interval, $num);
|}
|""".stripMargin
})
}
}
Expand Down