You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by ag...@apache.org on 2026/07/30 16:12:52 UTC
(datafusion-comet) branch branch-1.0 updated: fix: honor fail_on_error in native make_decimal (#5080) (#5145)
This is an automated email from the ASF dual-hosted git repository.
andygrove pushed a commit to branch branch-1.0
in repository https://gitbox.apache.org/repos/asf/datafusion-comet.git
The following commit(s) were added to refs/heads/branch-1.0 by this push:
new 933c32b2f6 fix: honor fail_on_error in native make_decimal (#5080) (#5145)
933c32b2f6 is described below
commit 933c32b2f6c6f5b87eca7afe9ef4de7186d92653
Author: Andy Grove <ag...@apache.org>
AuthorDate: Thu Jul 30 10:12:45 2026 -0600
fix: honor fail_on_error in native make_decimal (#5080) (#5145)
* fix: honor fail_on_error in native make_decimal
MakeDecimal with nullOnOverflow=false (ANSI mode) must throw
NUMERIC_VALUE_OUT_OF_RANGE on overflow, matching Spark's Decimal.set.
The serde already serialized the flag but the native registration
discarded it, so Comet returned NULL where Spark throws.
* refactor: simplify long_to_decimal with map_or
(cherry picked from commit a1b022b52a5815672ec650c0b8f689e91ea87517)
---
native/spark-expr/src/comet_scalar_funcs.rs | 4 +-
.../src/math_funcs/internal/make_decimal.rs | 91 +++++++++++++++++++---
.../org/apache/comet/CometExpressionSuite.scala | 24 ++++++
.../org/apache/spark/sql/ShimCometTestBase.scala | 8 ++
.../org/apache/spark/sql/ShimCometTestBase.scala | 8 ++
.../org/apache/spark/sql/ShimCometTestBase.scala | 10 ++-
6 files changed, 133 insertions(+), 12 deletions(-)
diff --git a/native/spark-expr/src/comet_scalar_funcs.rs b/native/spark-expr/src/comet_scalar_funcs.rs
index 925f6cb335..80d2fc893d 100644
--- a/native/spark-expr/src/comet_scalar_funcs.rs
+++ b/native/spark-expr/src/comet_scalar_funcs.rs
@@ -141,7 +141,9 @@ pub fn create_comet_physical_fun_with_eval_mode(
make_comet_scalar_udf!("unscaled_value", func, without data_type)
}
"make_decimal" => {
- make_comet_scalar_udf!("make_decimal", spark_make_decimal, data_type)
+ // fail_on_error corresponds to Spark's nullOnOverflow = false (ANSI mode): the
+ // unscaled long must fit the target precision or the query fails.
+ make_comet_scalar_udf!("make_decimal", spark_make_decimal, data_type, fail_on_error)
}
"unhex" => {
let func = Arc::new(spark_unhex);
diff --git a/native/spark-expr/src/math_funcs/internal/make_decimal.rs b/native/spark-expr/src/math_funcs/internal/make_decimal.rs
index f8ed31621e..1961ffd66b 100644
--- a/native/spark-expr/src/math_funcs/internal/make_decimal.rs
+++ b/native/spark-expr/src/math_funcs/internal/make_decimal.rs
@@ -15,13 +15,14 @@
// specific language governing permissions and limitations
// under the License.
+use crate::error::decimal_overflow_error;
use crate::math_funcs::utils::get_precision_scale;
use arrow::datatypes::DataType;
use arrow::{
array::{AsArray, Decimal128Builder},
datatypes::{validate_decimal_precision, Int64Type},
};
-use datafusion::common::{internal_err, Result as DataFusionResult, ScalarValue};
+use datafusion::common::{internal_err, DataFusionError, Result as DataFusionResult, ScalarValue};
use datafusion::physical_plan::ColumnarValue;
use std::sync::Arc;
@@ -29,12 +30,13 @@ use std::sync::Arc;
pub fn spark_make_decimal(
args: &[ColumnarValue],
data_type: &DataType,
+ fail_on_error: bool,
) -> DataFusionResult<ColumnarValue> {
let (precision, scale) = get_precision_scale(data_type);
match &args[0] {
ColumnarValue::Scalar(v) => match v {
ScalarValue::Int64(n) => Ok(ColumnarValue::Scalar(ScalarValue::Decimal128(
- long_to_decimal(n, precision, scale),
+ long_to_decimal(*n, precision, scale, fail_on_error)?,
precision,
scale,
))),
@@ -45,7 +47,7 @@ pub fn spark_make_decimal(
let arr = a.as_primitive::<Int64Type>();
let mut result = Decimal128Builder::new();
for v in arr.into_iter() {
- result.append_option(long_to_decimal(&v, precision, scale))
+ result.append_option(long_to_decimal(v, precision, scale, fail_on_error)?)
}
let result_type = DataType::Decimal128(precision, scale);
@@ -58,14 +60,83 @@ pub fn spark_make_decimal(
}
}
-/// Convert the input long to decimal with the given maximum precision. If overflows, returns null
-/// instead.
+/// Convert the input long to decimal with the given maximum precision. On overflow, errors when
+/// `fail_on_error` is set (Spark's `nullOnOverflow = false`, i.e. ANSI mode) and returns null
+/// otherwise.
#[inline]
-fn long_to_decimal(v: &Option<i64>, precision: u8, scale: i8) -> Option<i128> {
- match v {
- Some(v) if validate_decimal_precision(*v as i128, precision, scale).is_ok() => {
- Some(*v as i128)
+fn long_to_decimal(
+ v: Option<i64>,
+ precision: u8,
+ scale: i8,
+ fail_on_error: bool,
+) -> DataFusionResult<Option<i128>> {
+ v.map_or(Ok(None), |v| {
+ let v = v as i128;
+ match validate_decimal_precision(v, precision, scale) {
+ Ok(()) => Ok(Some(v)),
+ Err(_) if fail_on_error => Err(DataFusionError::External(Box::new(
+ decimal_overflow_error(v, precision, scale),
+ ))),
+ Err(_) => Ok(None),
}
- _ => None,
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use arrow::array::{Array, Int64Array};
+
+ fn overflow_args() -> [ColumnarValue; 1] {
+ // 123456 does not fit Decimal128(3, 0)
+ [ColumnarValue::Array(Arc::new(Int64Array::from(vec![
+ Some(123456),
+ None,
+ Some(99),
+ ])))]
+ }
+
+ #[test]
+ fn test_array_overflow_errors_when_fail_on_error() {
+ let err = spark_make_decimal(&overflow_args(), &DataType::Decimal128(3, 0), true)
+ .expect_err("overflow should error when fail_on_error is set");
+ assert!(
+ err.to_string().contains("NUMERIC_VALUE_OUT_OF_RANGE"),
+ "unexpected error: {err}"
+ );
+ }
+
+ #[test]
+ fn test_array_overflow_nulls_when_not_fail_on_error() {
+ let result = spark_make_decimal(&overflow_args(), &DataType::Decimal128(3, 0), false)
+ .expect("overflow should become null without fail_on_error");
+ let ColumnarValue::Array(array) = result else {
+ panic!("expected array result")
+ };
+ assert!(array.is_null(0));
+ assert!(array.is_null(1));
+ assert!(array.is_valid(2));
+ }
+
+ #[test]
+ fn test_scalar_overflow_errors_when_fail_on_error() {
+ let args = [ColumnarValue::Scalar(ScalarValue::Int64(Some(123456)))];
+ let err = spark_make_decimal(&args, &DataType::Decimal128(3, 0), true)
+ .expect_err("overflow should error when fail_on_error is set");
+ assert!(
+ err.to_string().contains("NUMERIC_VALUE_OUT_OF_RANGE"),
+ "unexpected error: {err}"
+ );
+ }
+
+ #[test]
+ fn test_scalar_null_is_not_an_error() {
+ let args = [ColumnarValue::Scalar(ScalarValue::Int64(None))];
+ let result = spark_make_decimal(&args, &DataType::Decimal128(3, 0), true)
+ .expect("null input should not error");
+ let ColumnarValue::Scalar(ScalarValue::Decimal128(v, 3, 0)) = result else {
+ panic!("expected decimal scalar result")
+ };
+ assert!(v.is_none());
}
}
diff --git a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
index 1f173fb0a6..dbe252c7da 100644
--- a/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
+++ b/spark/src/test/scala/org/apache/comet/CometExpressionSuite.scala
@@ -3267,6 +3267,30 @@ class CometExpressionSuite extends CometTestBase with AdaptiveSparkPlanHelper {
}
}
+ test("make decimal using DataFrame API - overflow throws when nullOnOverflow=false") {
+ // https://github.com/apache/datafusion-comet/issues/5066
+ withTable("t1") {
+ sql("create table t1 using parquet as select cast(123456 as long) as c1 from range(1)")
+
+ withSQLConf(
+ SQLConf.USE_V1_SOURCE_LIST.key -> "parquet",
+ SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false",
+ SQLConf.ADAPTIVE_OPTIMIZER_EXCLUDED_RULES.key -> "org.apache.spark.sql.catalyst.optimizer.ConstantFolding") {
+
+ val df = sql("select * from t1")
+ val makeDecimalColumn =
+ createMakeDecimalColumn(df.col("c1").expr, 3, 0, nullOnOverflow = false)
+ val df1 = df.withColumn("result", makeDecimalColumn)
+
+ val (sparkErr, cometErr) = checkSparkAnswerMaybeThrows(df1)
+ assert(sparkErr.isDefined, "Spark should throw on overflow when nullOnOverflow=false")
+ assert(cometErr.isDefined, "Comet should throw on overflow when nullOnOverflow=false")
+ assert(sparkErr.get.getMessage.contains("cannot be represented as Decimal(3, 0)"))
+ assert(cometErr.get.getMessage.contains("cannot be represented as Decimal(3, 0)"))
+ }
+ }
+ }
+
test("deep AND/OR predicate chains do not overflow the protobuf recursion limit") {
// A left-deep chain of N associative boolean operands serializes to a proto nested N
// levels deep. With N > protobuf's default recursion limit (100), the message overflows
diff --git a/spark/src/test/spark-3.4/org/apache/spark/sql/ShimCometTestBase.scala b/spark/src/test/spark-3.4/org/apache/spark/sql/ShimCometTestBase.scala
index a7dfb42645..ce56eeb436 100644
--- a/spark/src/test/spark-3.4/org/apache/spark/sql/ShimCometTestBase.scala
+++ b/spark/src/test/spark-3.4/org/apache/spark/sql/ShimCometTestBase.scala
@@ -50,4 +50,12 @@ trait ShimCometTestBase {
def createMakeDecimalColumn(child: Expression, precision: Int, scale: Int): Column = {
new Column(MakeDecimal(child, precision, scale))
}
+
+ def createMakeDecimalColumn(
+ child: Expression,
+ precision: Int,
+ scale: Int,
+ nullOnOverflow: Boolean): Column = {
+ new Column(MakeDecimal(child, precision, scale, nullOnOverflow))
+ }
}
diff --git a/spark/src/test/spark-3.5/org/apache/spark/sql/ShimCometTestBase.scala b/spark/src/test/spark-3.5/org/apache/spark/sql/ShimCometTestBase.scala
index 7f22494ad2..fb273a1642 100644
--- a/spark/src/test/spark-3.5/org/apache/spark/sql/ShimCometTestBase.scala
+++ b/spark/src/test/spark-3.5/org/apache/spark/sql/ShimCometTestBase.scala
@@ -51,4 +51,12 @@ trait ShimCometTestBase {
new Column(MakeDecimal(child, precision, scale))
}
+ def createMakeDecimalColumn(
+ child: Expression,
+ precision: Int,
+ scale: Int,
+ nullOnOverflow: Boolean): Column = {
+ new Column(MakeDecimal(child, precision, scale, nullOnOverflow))
+ }
+
}
diff --git a/spark/src/test/spark-4.x/org/apache/spark/sql/ShimCometTestBase.scala b/spark/src/test/spark-4.x/org/apache/spark/sql/ShimCometTestBase.scala
index 5ad4543220..6d035f6ba9 100644
--- a/spark/src/test/spark-4.x/org/apache/spark/sql/ShimCometTestBase.scala
+++ b/spark/src/test/spark-4.x/org/apache/spark/sql/ShimCometTestBase.scala
@@ -49,6 +49,14 @@ trait ShimCometTestBase {
}
def createMakeDecimalColumn(child: Expression, precision: Int, scale: Int): Column = {
- new Column(ExpressionColumnNode.apply(MakeDecimal(child, precision, scale, true)))
+ createMakeDecimalColumn(child, precision, scale, nullOnOverflow = true)
+ }
+
+ def createMakeDecimalColumn(
+ child: Expression,
+ precision: Int,
+ scale: Int,
+ nullOnOverflow: Boolean): Column = {
+ new Column(ExpressionColumnNode.apply(MakeDecimal(child, precision, scale, nullOnOverflow)))
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org