-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-28470][SQL] Cast to decimal throws ArithmeticException on overflow #25253
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
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 |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ import org.apache.spark.sql.catalyst.expressions.codegen._ | |
| import org.apache.spark.sql.catalyst.expressions.codegen.Block._ | ||
| import org.apache.spark.sql.catalyst.util._ | ||
| import org.apache.spark.sql.catalyst.util.DateTimeUtils._ | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.types._ | ||
| import org.apache.spark.unsafe.UTF8StringBuilder | ||
| import org.apache.spark.unsafe.types.{CalendarInterval, UTF8String} | ||
|
|
@@ -498,22 +499,37 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| b => x.numeric.asInstanceOf[Numeric[Any]].toInt(b).toByte | ||
| } | ||
|
|
||
| private val nullOnOverflow = SQLConf.get.decimalOperationsNullOnOverflow | ||
|
|
||
| /** | ||
| * Change the precision / scale in a given decimal to those set in `decimalType` (if any), | ||
| * returning null if it overflows or modifying `value` in-place and returning it if successful. | ||
| * modifying `value` in-place and returning it if successful. If an overflow occurs, it | ||
| * either returns null or throws an exception according to the value set for | ||
| * `spark.sql.decimalOperations.nullOnOverflow`. | ||
| * | ||
| * NOTE: this modifies `value` in-place, so don't call it on external data. | ||
| */ | ||
| private[this] def changePrecision(value: Decimal, decimalType: DecimalType): Decimal = { | ||
| if (value.changePrecision(decimalType.precision, decimalType.scale)) value else null | ||
| if (value.changePrecision(decimalType.precision, decimalType.scale)) { | ||
| value | ||
| } else { | ||
|
Member
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. not a biggie but: if (value.changePrecision(decimalType.precision, decimalType.scale)) {
value
} else if (nullOnOverflow) {
null
} else {
throw new ArithmeticException(s"${value.toDebugString} cannot be represented as " +
s"Decimal(${decimalType.precision}, ${decimalType.scale}).")
}
Member
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. I like the way in this PR. It is more clear about what to do on overflow.
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. I agree with @gengliangwang but I am fine changing it. Please @HyukjinKwon let me know if you think we should change it, I'll do it. Thanks.
Member
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. ah, that's fine. no big deal. |
||
| if (nullOnOverflow) { | ||
| null | ||
| } else { | ||
| throw new ArithmeticException(s"${value.toDebugString} cannot be represented as " + | ||
| s"Decimal(${decimalType.precision}, ${decimalType.scale}).") | ||
|
Member
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. Nit: should we just use ${decimalType.catalogString} here?
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. this is consistent with other similar error messages. We should change it in all cases, then. WDYT?
Member
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 is trivial. Maybe we can have another PR to fix it. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Create new `Decimal` with precision and scale given in `decimalType` (if any), | ||
| * returning null if it overflows or creating a new `value` and returning it if successful. | ||
| * Create new `Decimal` with precision and scale given in `decimalType` (if any). | ||
| * If overflow occurs, if `spark.sql.decimalOperations.nullOnOverflow` is true, null is returned; | ||
| * otherwise, an `ArithmeticException` is thrown. | ||
| */ | ||
| private[this] def toPrecision(value: Decimal, decimalType: DecimalType): Decimal = | ||
| value.toPrecision(decimalType.precision, decimalType.scale) | ||
| value.toPrecision( | ||
| decimalType.precision, decimalType.scale, Decimal.ROUND_HALF_UP, nullOnOverflow) | ||
|
|
||
|
|
||
| private[this] def castToDecimal(from: DataType, target: DecimalType): Any => Any = from match { | ||
|
|
@@ -963,11 +979,19 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String | |
| |$evPrim = $d; | ||
| """.stripMargin | ||
| } else { | ||
| val overflowCode = if (nullOnOverflow) { | ||
| s"$evNull = true;" | ||
| } else { | ||
| s""" | ||
| |throw new ArithmeticException($d.toDebugString() + " cannot be represented as " + | ||
| | "Decimal(${decimalType.precision}, ${decimalType.scale})."); | ||
| """.stripMargin | ||
| } | ||
| code""" | ||
| |if ($d.changePrecision(${decimalType.precision}, ${decimalType.scale})) { | ||
| | $evPrim = $d; | ||
| |} else { | ||
| | $evNull = true; | ||
| | $overflowCode | ||
| |} | ||
| """.stripMargin | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.