Skip to content

Commit b51a65d

Browse files
authored
fix: Modulus on decimal data type mismatch (#2922)
1 parent 8b041a9 commit b51a65d

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

native/spark-expr/src/math_funcs/modulo_expr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,16 @@ pub fn create_modulo_expr(
100100
SparkCastOptions::new_without_timezone(EvalMode::Legacy, false),
101101
));
102102

103+
// The UDF's return type must match what Arrow's rem function will actually return.
104+
// Since we're operating on Decimal256 inputs, rem will return Decimal256.
105+
let decimal256_return_type = match &data_type {
106+
DataType::Decimal128(p, s) => DataType::Decimal256(*p, *s),
107+
other => other.clone(),
108+
};
103109
let modulo_scalar_func = create_modulo_scalar_function(
104110
left_256,
105111
right_256,
106-
&data_type,
112+
&decimal256_return_type,
107113
registry,
108114
fail_on_error,
109115
)?;

spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,22 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
17721772
}
17731773
}
17741774

1775+
test("Decimal modulus with Decimal256 intermediate type") {
1776+
// regression test for https://github.com/apache/datafusion-comet/issues/2911
1777+
withTable("test") {
1778+
sql("create table test(a decimal(33, 29), b decimal(28, 17)) using parquet")
1779+
sql(
1780+
"insert into test values (-6788.53035340376888409034576923353, " +
1781+
"70948216565.90127985418365471)")
1782+
withSQLConf(
1783+
"spark.comet.enabled" -> "true",
1784+
"spark.sql.decimalOperations.allowPrecisionLoss" -> "true") {
1785+
val df = sql("select a, b, a % b from test")
1786+
df.collect()
1787+
}
1788+
}
1789+
}
1790+
17751791
test("Decimal random number tests") {
17761792
val rand = new scala.util.Random(42)
17771793
def makeNum(p: Int, s: Int): String = {

0 commit comments

Comments
 (0)