Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

[NSE-130] fix precision loss in divide w/ decimal type #164

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ class ColumnarMultiply(left: Expression, right: Expression, original: Expression
}
}

class ColumnarDivide(left: Expression, right: Expression, original: Expression)
class ColumnarDivide(left: Expression, right: Expression,
original: Expression, resType: DecimalType = null)
extends Divide(left: Expression, right: Expression)
with ColumnarExpression
with Logging {
Expand All @@ -237,8 +238,12 @@ class ColumnarDivide(left: Expression, right: Expression, original: Expression)

(left_type, right_type) match {
case (l: ArrowType.Decimal, r: ArrowType.Decimal) =>
var resultType = DecimalTypeUtil.getResultTypeForOperation(
DecimalTypeUtil.OperationType.DIVIDE, l, r)
var resultType = if (resType != null) {
CodeGeneration.getResultType(resType)
} else {
DecimalTypeUtil.getResultTypeForOperation(
DecimalTypeUtil.OperationType.DIVIDE, l, r)
}
val newLeftNode = left match {
case literal: ColumnarLiteral =>
val leftStr = literal.value.asInstanceOf[Decimal].toDouble.toString
Expand Down Expand Up @@ -374,6 +379,17 @@ object ColumnarBinaryArithmetic {
}
}

def createDivide(left: Expression, right: Expression,
original: Expression, resType: DecimalType): Expression = {
buildCheck(left, right)
original match {
case d: Divide =>
new ColumnarDivide(left, right, d, resType)
case other =>
throw new UnsupportedOperationException(s"not currently supported: $other.")
}
}

def buildCheck(left: Expression, right: Expression): Unit = {
try {
ConverterUtils.checkIfTypeSupported(left.dataType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.apache.spark.internal.Logging
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.aggregate._
import org.apache.spark.sql.catalyst.expressions.BindReferences.bindReferences
import org.apache.spark.sql.types.DecimalType
object ColumnarExpressionConverter extends Logging {

var check_if_no_calculation = true
Expand Down Expand Up @@ -245,12 +246,31 @@ object ColumnarExpressionConverter extends Logging {
expr)
case u: UnaryExpression =>
logInfo(s"${expr.getClass} ${expr} is supported, no_cal is $check_if_no_calculation.")
ColumnarUnaryOperator.create(
replaceWithColumnarExpression(
u.child,
attributeSeq,
convertBoundRefToAttrRef = convertBoundRefToAttrRef),
expr)
if (!u.isInstanceOf[CheckOverflow] || !u.child.isInstanceOf[Divide]) {
ColumnarUnaryOperator.create(
replaceWithColumnarExpression(
u.child,
attributeSeq,
convertBoundRefToAttrRef = convertBoundRefToAttrRef),
expr)
} else {
// CheckOverflow[Divide]: pass resType to Divide to avoid precision loss
Copy link
Collaborator

@zhouyuan zhouyuan Mar 15, 2021

Choose a reason for hiding this comment

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

does this mean divide must have a checkoverflow wrapper in spark?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

look like yes for decimal divide. Below is spark code from spark/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/DecimalPrecision.scala
CheckOverflow(Divide(promotePrecision(e1, widerType), promotePrecision(e2, widerType)), resultType, nullOnOverflow)

val divide = u.child.asInstanceOf[Divide]
val columnarDivide = ColumnarBinaryArithmetic.createDivide(
replaceWithColumnarExpression(
divide.left,
attributeSeq,
convertBoundRefToAttrRef = convertBoundRefToAttrRef),
replaceWithColumnarExpression(
divide.right,
attributeSeq,
convertBoundRefToAttrRef = convertBoundRefToAttrRef),
divide,
u.dataType.asInstanceOf[DecimalType])
ColumnarUnaryOperator.create(
columnarDivide,
expr)
}
case oaps: com.intel.oap.expression.ColumnarScalarSubquery =>
oaps
case s: org.apache.spark.sql.execution.ScalarSubquery =>
Expand Down