Skip to content
Draft
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 @@ -139,4 +139,5 @@ class VeloxLiteralSuite extends VeloxWholeStageTransformerSuite {
validateFallbackResult("SELECT struct(cast(null as struct<a: string>))")
validateFallbackResult("SELECT array(struct(1, 'a'), null)")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,46 @@ class ArithmeticAnsiValidateSuite extends FunctionsValidateSuite {
}
}

test("decimal add overflow") {
// Normal decimal add should succeed and match Spark results
runQueryAndCompare(
"SELECT CAST(1.0 AS DECIMAL(10,2)) + CAST(2.0 AS DECIMAL(10,2))") {
checkGlutenPlan[ProjectExecTransformer]
}

// Overflow: max DECIMAL(38,0) + 1 should throw in ANSI mode
if (isSparkVersionGE("4.0")) {
intercept[SparkException] {
sql("SELECT CAST(99999999999999999999999999999999999999 AS DECIMAL(38,0)) + " +
"CAST(1 AS DECIMAL(38,0))").collect()
}
} else {
intercept[ArithmeticException] {
sql("SELECT CAST(99999999999999999999999999999999999999 AS DECIMAL(38,0)) + " +
"CAST(1 AS DECIMAL(38,0))").collect()
}
}
}

test("decimal subtract overflow") {
// Normal decimal subtract should succeed and match Spark results
runQueryAndCompare(
"SELECT CAST(5.0 AS DECIMAL(10,2)) - CAST(2.0 AS DECIMAL(10,2))") {
checkGlutenPlan[ProjectExecTransformer]
}

// Overflow: -max DECIMAL(38,0) - 1 should throw in ANSI mode
if (isSparkVersionGE("4.0")) {
intercept[SparkException] {
sql("SELECT CAST(-99999999999999999999999999999999999999 AS DECIMAL(38,0)) - " +
"CAST(1 AS DECIMAL(38,0))").collect()
}
} else {
intercept[ArithmeticException] {
sql("SELECT CAST(-99999999999999999999999999999999999999 AS DECIMAL(38,0)) - " +
"CAST(1 AS DECIMAL(38,0))").collect()
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -621,17 +621,37 @@ object ExpressionConverter extends SQLConfHelper with Logging {
substraitExprName,
expr.children.map(replaceWithExpressionTransformer0(_, attributeSeq, expressionsMap)),
expr)
case CheckOverflow(b: BinaryArithmetic, decimalType, _)
case CheckOverflow(b: BinaryArithmetic, decimalType, nullOnOverflow)
if !BackendsApiManager.getSettings.transformCheckOverflow &&
DecimalArithmeticUtil.isDecimalArithmetic(b) =>
val arithmeticExprName =
val baseExprName =
BackendsApiManager.getSparkPlanExecApiInstance.getDecimalArithmeticExprName(
getAndCheckSubstraitName(b, expressionsMap))
// When nullOnOverflow is false, it's ANSI mode - use checked_ prefix for overflow errors
val arithmeticExprName = if (!nullOnOverflow) {
"checked_" + baseExprName
} else {
baseExprName
}
val left =
replaceWithExpressionTransformer0(b.left, attributeSeq, expressionsMap)
val right =
replaceWithExpressionTransformer0(b.right, attributeSeq, expressionsMap)
DecimalArithmeticExpressionTransformer(arithmeticExprName, left, right, decimalType, b)
// Velox path: ANSI mode decimal Add/Subtract uses checked_ variants
// that throw on overflow instead of returning null.
case c @ CheckOverflow(b: BinaryArithmetic, _, nullOnOverflow)
if BackendsApiManager.getSettings.transformCheckOverflow &&
DecimalArithmeticUtil.isDecimalArithmetic(b) &&
!nullOnOverflow &&
(b.isInstanceOf[Add] || b.isInstanceOf[Subtract]) =>
val baseExprName =
BackendsApiManager.getSparkPlanExecApiInstance.getDecimalArithmeticExprName(
getAndCheckSubstraitName(b, expressionsMap))
val checkedExprName = "checked_" + baseExprName
val childTransformer =
genRescaleDecimalTransformer(checkedExprName, b, attributeSeq, expressionsMap)
CheckOverflowTransformer(substraitExprName, childTransformer, c)
case c: CheckOverflow =>
CheckOverflowTransformer(
substraitExprName,
Expand Down