-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-50226][SQL] Correct MakeDTInterval and MakeYMInterval to catch Java exceptions #48773
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 all commits
3426c34
908e745
b5cd32a
2fa9f9d
abd4255
de44e30
87e3c5c
3b94f96
d7352a4
5508b9c
6a2ed0b
68594ff
30c63f6
8388de2
4f72f7e
9ed5eb2
494d648
b042247
0fcd3cc
2925363
2d84c95
c2d2490
2a36749
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 |
|---|---|---|
|
|
@@ -481,7 +481,7 @@ case class MakeDTInterval( | |
| hours: Expression, | ||
| mins: Expression, | ||
| secs: Expression) | ||
| extends QuaternaryExpression with ImplicitCastInputTypes { | ||
| extends QuaternaryExpression with ImplicitCastInputTypes with SupportQueryContext { | ||
| override def nullIntolerant: Boolean = true | ||
|
|
||
| def this( | ||
|
|
@@ -514,13 +514,15 @@ case class MakeDTInterval( | |
| day.asInstanceOf[Int], | ||
| hour.asInstanceOf[Int], | ||
| min.asInstanceOf[Int], | ||
| sec.asInstanceOf[Decimal]) | ||
| sec.asInstanceOf[Decimal], | ||
| origin.context) | ||
| } | ||
|
|
||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| defineCodeGen(ctx, ev, (day, hour, min, sec) => { | ||
| val errorContext = getContextOrNullCode(ctx) | ||
| val iu = IntervalUtils.getClass.getName.stripSuffix("$") | ||
| s"$iu.makeDayTimeInterval($day, $hour, $min, $sec)" | ||
| s"$iu.makeDayTimeInterval($day, $hour, $min, $sec, $errorContext)" | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -532,6 +534,8 @@ case class MakeDTInterval( | |
| mins: Expression, | ||
| secs: Expression): MakeDTInterval = | ||
| copy(days, hours, mins, secs) | ||
|
|
||
| override def initQueryContext(): Option[QueryContext] = Some(origin.context) | ||
| } | ||
|
|
||
| @ExpressionDescription( | ||
|
|
@@ -556,7 +560,7 @@ case class MakeDTInterval( | |
| group = "datetime_funcs") | ||
| // scalastyle:on line.size.limit | ||
| case class MakeYMInterval(years: Expression, months: Expression) | ||
| extends BinaryExpression with ImplicitCastInputTypes with Serializable { | ||
| extends BinaryExpression with ImplicitCastInputTypes with Serializable with SupportQueryContext { | ||
| override def nullIntolerant: Boolean = true | ||
|
|
||
| def this(years: Expression) = this(years, Literal(0)) | ||
|
|
@@ -568,17 +572,28 @@ case class MakeYMInterval(years: Expression, months: Expression) | |
| override def dataType: DataType = YearMonthIntervalType() | ||
|
|
||
| override def nullSafeEval(year: Any, month: Any): Any = { | ||
| Math.toIntExact(Math.addExact(month.asInstanceOf[Number].longValue(), | ||
| Math.multiplyExact(year.asInstanceOf[Number].longValue(), MONTHS_PER_YEAR))) | ||
| try { | ||
| Math.toIntExact( | ||
| Math.addExact(month.asInstanceOf[Int], | ||
| Math.multiplyExact(year.asInstanceOf[Int], MONTHS_PER_YEAR))) | ||
| } catch { | ||
|
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. shall we add
Contributor
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. Off course we should deduplicate it, but I didn't succeed with it: #48773 (comment).
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. there is already
Contributor
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. Ok, I'll do it a bit later. |
||
| case _: ArithmeticException => | ||
| throw QueryExecutionErrors.withoutSuggestionIntervalArithmeticOverflowError(origin.context) | ||
| } | ||
| } | ||
|
|
||
| override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| defineCodeGen(ctx, ev, (years, months) => { | ||
| nullSafeCodeGen(ctx, ev, (years, months) => { | ||
| val math = classOf[Math].getName.stripSuffix("$") | ||
| val errorContext = getContextOrNullCode(ctx) | ||
| // scalastyle:off line.size.limit | ||
| s""" | ||
| |$math.toIntExact(java.lang.Math.addExact($months, | ||
| | $math.multiplyExact($years, $MONTHS_PER_YEAR))) | ||
| |""".stripMargin | ||
| |try { | ||
| | ${ev.value} = $math.toIntExact($math.addExact($months, $math.multiplyExact($years, $MONTHS_PER_YEAR))); | ||
| |} catch (java.lang.ArithmeticException e) { | ||
| | throw QueryExecutionErrors.withoutSuggestionIntervalArithmeticOverflowError($errorContext); | ||
| |}""".stripMargin | ||
| // scalastyle:on line.size.limit | ||
| }) | ||
| } | ||
|
|
||
|
|
@@ -587,6 +602,10 @@ case class MakeYMInterval(years: Expression, months: Expression) | |
| override protected def withNewChildrenInternal( | ||
| newLeft: Expression, newRight: Expression): Expression = | ||
| copy(years = newLeft, months = newRight) | ||
|
|
||
| override def initQueryContext(): Option[QueryContext] = { | ||
| Some(origin.context) | ||
| } | ||
| } | ||
|
|
||
| // Multiply an year-month interval by a numeric | ||
|
|
@@ -699,8 +718,8 @@ trait IntervalDivide { | |
| context: QueryContext): Unit = { | ||
| if (value == minValue && num.dataType.isInstanceOf[IntegralType]) { | ||
| if (numValue.asInstanceOf[Number].longValue() == -1) { | ||
| throw QueryExecutionErrors.intervalArithmeticOverflowError( | ||
| "Interval value overflows after being divided by -1", "try_divide", context) | ||
| throw QueryExecutionErrors.withSuggestionIntervalArithmeticOverflowError( | ||
| "try_divide", context) | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.