[SPARK-28470][SQL] Cast to decimal throws ArithmeticException on overflow#25253
[SPARK-28470][SQL] Cast to decimal throws ArithmeticException on overflow#25253mgaido91 wants to merge 3 commits intoapache:masterfrom
Conversation
|
Test build #108155 has finished for PR 25253 at commit
|
| if (value.changePrecision(decimalType.precision, decimalType.scale)) value else null | ||
| if (value.changePrecision(decimalType.precision, decimalType.scale)) { | ||
| value | ||
| } else { |
There was a problem hiding this comment.
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}).")
}There was a problem hiding this comment.
I like the way in this PR. It is more clear about what to do on overflow.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
ah, that's fine. no big deal.
| null | ||
| } else { | ||
| throw new ArithmeticException(s"${value.toDebugString} cannot be represented as " + | ||
| s"Decimal(${decimalType.precision}, ${decimalType.scale}).") |
There was a problem hiding this comment.
Nit: should we just use ${decimalType.catalogString} here?
There was a problem hiding this comment.
this is consistent with other similar error messages. We should change it in all cases, then. WDYT?
There was a problem hiding this comment.
This is trivial. Maybe we can have another PR to fix it.
maropu
left a comment
There was a problem hiding this comment.
LGTM except for minor comments.
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
Show resolved
Hide resolved
| code""" | ||
| |$d.changePrecision(${decimalType.precision}, ${decimalType.scale}); | ||
| |$d.changePrecision( | ||
| | ${decimalType.precision}, ${decimalType.scale}); |
There was a problem hiding this comment.
nit: write this in a single line? |$d.changePrecision(${decimalType.precision}, ${decimalType.scale});
| } else { | ||
| s""" | ||
| |throw new ArithmeticException($d.toDebugString() + " cannot be represented as " + | ||
| | "Decimal(${decimalType.precision}, ${decimalType.scale})"); |
There was a problem hiding this comment.
super nit: you forgot to add a period in the end? better to have the same message between the codegen/interpreter modes: https://github.com/apache/spark/pull/25253/files#diff-258b71121d8d168e4d53cb5b6dc53ffeR518
| } | ||
| code""" | ||
| |if ($d.changePrecision(${decimalType.precision}, ${decimalType.scale})) { | ||
| |if ($d.changePrecision( |
There was a problem hiding this comment.
ditto: can you write it in a single line?
| withSQLConf(SQLConf.DECIMAL_OPERATIONS_NULL_ON_OVERFLOW.key -> "true") { | ||
| checkEvaluation(Cast(Literal("134.12"), DecimalType(3, 2)), null) | ||
| checkEvaluation( | ||
| Cast(Literal(java.sql.Timestamp.valueOf("2019-07-25 22:04:36")), DecimalType(3, 2)), null) |
| checkExceptionInExpression[ArithmeticException]( | ||
| Cast(Literal("134.12"), DecimalType(3, 2)), "cannot be represented") | ||
| checkExceptionInExpression[ArithmeticException]( | ||
| Cast(Literal(java.sql.Timestamp.valueOf("2019-07-25 22:04:36")), DecimalType(3, 2)), |
|
cc: @dongjoon-hyun |
|
Test build #108611 has finished for PR 25253 at commit
|
|
|
||
| /** | ||
| * 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. |
|
Test build #108621 has finished for PR 25253 at commit
|
|
Anyone can check this for sign-off before merging? |
|
Thanks! Merged to master. |
|
late LGTM too. |
What changes were proposed in this pull request?
The flag
spark.sql.decimalOperations.nullOnOverflowis not honored by theCastoperator. This means that a casting which causes an overflow currently returnsnull.The PR makes
Castrespecting that flag, ie. when it is turned to false and a decimal overflow occurs, an exception id thrown.How was this patch tested?
Added UT