mgaido91 commented on a change in pull request #25010: [SPARK-28201][SQL]
Revisit MakeDecimal behavior on overflow
URL: https://github.com/apache/spark/pull/25010#discussion_r298805416
##########
File path:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/decimalExpressions.scala
##########
@@ -46,19 +47,35 @@ case class UnscaledValue(child: Expression) extends
UnaryExpression {
*/
case class MakeDecimal(child: Expression, precision: Int, scale: Int) extends
UnaryExpression {
+ private val nullOnOverflow = SQLConf.get.decimalOperationsNullOnOverflow
+ private lazy val doEval = if (nullOnOverflow) {
+ input: Long => new Decimal().setOrNull(input, precision, scale)
+ } else {
+ input: Long => new Decimal().set(input, precision, scale)
+ }
+
override def dataType: DataType = DecimalType(precision, scale)
- override def nullable: Boolean = true
+ override def nullable: Boolean = child.nullable || nullOnOverflow
override def toString: String = s"MakeDecimal($child,$precision,$scale)"
- protected override def nullSafeEval(input: Any): Any =
- Decimal(input.asInstanceOf[Long], precision, scale)
+ protected override def nullSafeEval(input: Any): Any =
doEval(input.asInstanceOf[Long])
override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
nullSafeCodeGen(ctx, ev, eval => {
+ val setMethod = if (nullOnOverflow) {
+ "setOrNull"
+ } else {
+ "set"
+ }
+ val setNull = if (nullable) {
Review comment:
Not only it is safe, but it doesn't even compile to make that assignment in
the nullable case. If a variable is not nullable, its `isNull` variable is just
the constant `false`: so no assignment has to be done (please see the code in
`nullSafeCodeGen`).
> I was just curious whether there's existing precedence for this in codegen.
You can find similar patterns in `collectionOperations.scala`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]