Skip to content
Open
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 @@ -35,5 +35,6 @@
- `spark.sql.legacy.castComplexTypesToString.enabled=true` is not honoured by Comet (https://github.com/apache/datafusion-comet/issues/4492).
- `CAST(<float|double> AS DECIMAL)` rounding may differ from Spark (`Incompatible`, gated by `spark.comet.expression.Cast.allowIncompatible`, tracked at https://github.com/apache/datafusion-comet/issues/1371).
- Spark registers the type-name conversion functions (`bigint`, `binary`, `boolean`, `date`, `decimal`, `double`, `float`, `int`, `smallint`, `string`, `timestamp`, `tinyint`) as cast aliases. Each lowers to the same `Cast` node, so Comet handles it via the `cast` implementation with the same compatibility profile.
- Performance (tuned 2026-07-15, PR #4940): float/double-to-decimal casts (`cast_floating_point_to_decimal128`) now convert in a single vectorized `unary_opt` pass that maps out-of-range values (NaN, infinity, precision overflow) to null, replacing the per-element `Decimal128Builder` loop. ANSI raises via an O(1) null-count check plus a rare element-wise rescan. 15-36% faster with no regression on any shape. Benchmark: `benches/cast_float_to_decimal.rs`.

[Spark Expression Support]: ../../user-guide/latest/expressions.md
4 changes: 4 additions & 0 deletions native/spark-expr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ harness = false
[[bench]]
name = "to_json"
harness = false

[[bench]]
name = "cast_float_to_decimal"
harness = false
91 changes: 91 additions & 0 deletions native/spark-expr/benches/cast_float_to_decimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use arrow::array::{Float32Array, Float64Array, RecordBatch};
use arrow::datatypes::{DataType, Field, Schema};
use criterion::{criterion_group, criterion_main, Criterion};
use datafusion::physical_expr::{expressions::Column, PhysicalExpr};
use datafusion_comet_spark_expr::{Cast, EvalMode, SparkCastOptions};
use std::hint::black_box;
use std::sync::Arc;

fn f64_batch(size: usize, null_every: usize, big: bool) -> RecordBatch {
let a: Float64Array = (0..size)
.map(|i| {
if null_every != 0 && i % null_every == 0 {
None
} else if big {
// value * 10^4 overflows Decimal128(15, 4).
Some(1.0e12 + i as f64)
} else {
Some((i % 100_000) as f64 * 0.5)
}
})
.collect();
let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Float64, true)]));
RecordBatch::try_new(schema, vec![Arc::new(a)]).unwrap()
}

fn f32_batch(size: usize) -> RecordBatch {
let a: Float32Array = (0..size)
.map(|i| Some((i % 100_000) as f32 * 0.5))
.collect();
let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Float32, true)]));
RecordBatch::try_new(schema, vec![Arc::new(a)]).unwrap()
}

fn cast(to: DataType, mode: EvalMode) -> Cast {
Cast::new(
Arc::new(Column::new("a", 0)),
to,
SparkCastOptions::new_without_timezone(mode, false),
None,
None,
)
}

fn criterion_benchmark(c: &mut Criterion) {
let size = 8192;
let dec = DataType::Decimal128(15, 4);

let f64_no_nulls = f64_batch(size, 0, false);
let f64_nulls = f64_batch(size, 10, false);
let f64_big = f64_batch(size, 0, true);
let f32 = f32_batch(size);

let c_legacy = cast(dec.clone(), EvalMode::Legacy);
let c_ansi = cast(dec.clone(), EvalMode::Ansi);

c.bench_function("cast_float_to_decimal: f64 -> dec(15,4)", |b| {
b.iter(|| black_box(c_legacy.evaluate(black_box(&f64_no_nulls)).unwrap()))
});
c.bench_function("cast_float_to_decimal: f64 -> dec(15,4), nulls", |b| {
b.iter(|| black_box(c_legacy.evaluate(black_box(&f64_nulls)).unwrap()))
});
c.bench_function("cast_float_to_decimal: f32 -> dec(15,4)", |b| {
b.iter(|| black_box(c_legacy.evaluate(black_box(&f32)).unwrap()))
});
c.bench_function("cast_float_to_decimal: f64 -> dec(15,4) ansi", |b| {
b.iter(|| black_box(c_ansi.evaluate(black_box(&f64_no_nulls)).unwrap()))
});
c.bench_function("cast_float_to_decimal: f64 -> dec(15,4) overflow", |b| {
b.iter(|| black_box(c_legacy.evaluate(black_box(&f64_big)).unwrap()))
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
103 changes: 75 additions & 28 deletions native/spark-expr/src/conversion_funcs/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,40 +828,45 @@ where
<T as ArrowPrimitiveType>::Native: AsPrimitive<f64>,
{
let input = array.as_any().downcast_ref::<PrimitiveArray<T>>().unwrap();
let mut cast_array = PrimitiveArray::<Decimal128Type>::builder(input.len());

let mul = 10_f64.powi(scale as i32);

for i in 0..input.len() {
if input.is_null(i) {
cast_array.append_null();
continue;
}

let input_value = input.value(i).as_();
if let Some(v) = (input_value * mul).round().to_i128() {
if is_validate_decimal_precision(v, precision) {
cast_array.append_value(v);
continue;
// Single vectorized pass: a value with no in-range integer form (NaN / infinity) or that
// does not fit the output precision maps to null. `unary_opt` only applies the closure to
// non-null slots and carries the input null buffer over, so it replaces the per-element
// builder loop without a second pass.
let result: Decimal128Array = input.unary_opt::<_, Decimal128Type>(|v| {
let f: f64 = v.as_();
(f * mul)
.round()
.to_i128()
.filter(|x| is_validate_decimal_precision(*x, precision))
});

// ANSI must raise on out-of-range values instead of nulling them. `unary_opt` only nulls
// non-null inputs that overflow, so a null count beyond the input's signals an overflow to
// report. This check is O(1); the element-wise rescan runs only on the rare error path and
// reports the first offending value with Spark's exact error.
if eval_mode == EvalMode::Ansi && result.null_count() > input.null_count() {
for i in 0..input.len() {
if !input.is_null(i) {
let input_value: f64 = input.value(i).as_();
let fits = (input_value * mul)
.round()
.to_i128()
.map(|x| is_validate_decimal_precision(x, precision))
.unwrap_or(false);
if !fits {
return Err(SparkError::NumericValueOutOfRange {
value: input_value.to_string(),
precision,
scale,
});
}
}
};

if eval_mode == EvalMode::Ansi {
return Err(SparkError::NumericValueOutOfRange {
value: input_value.to_string(),
precision,
scale,
});
}
cast_array.append_null();
}

let res = Arc::new(
cast_array
.with_precision_and_scale(precision, scale)?
.finish(),
) as ArrayRef;
Ok(res)
Ok(Arc::new(result.with_precision_and_scale(precision, scale)?))
}

pub(crate) fn spark_cast_nonintegral_numeric_to_integral(
Expand Down Expand Up @@ -1287,6 +1292,48 @@ mod tests {
assert!(casted.is_null(9));
}

#[test]
#[cfg_attr(miri, ignore)]
fn test_cast_float_to_decimal_no_overflow_fast_path() {
// All values fit precision 10, scale 2, so the vectorized fast path is taken and the
// input null is preserved.
let a: ArrayRef = Arc::new(Float64Array::from(vec![
Some(42.0),
Some(-1.5),
None,
Some(0.0),
]));
let b =
cast_floating_point_to_decimal128::<Float64Type>(&a, 10, 2, EvalMode::Legacy).unwrap();
let d = b.as_primitive::<Decimal128Type>();
assert_eq!(d.value(0), 4200); // 42.00
assert_eq!(d.value(1), -150); // -1.50
assert!(d.is_null(2));
assert_eq!(d.value(3), 0);
assert_eq!(d.data_type(), &DataType::Decimal128(10, 2));
}

#[test]
#[cfg_attr(miri, ignore)]
fn test_cast_float_to_decimal_ansi_no_overflow() {
let a: ArrayRef = Arc::new(Float64Array::from(vec![Some(42.0), None, Some(-1.5)]));
let b =
cast_floating_point_to_decimal128::<Float64Type>(&a, 10, 2, EvalMode::Ansi).unwrap();
let d = b.as_primitive::<Decimal128Type>();
assert_eq!(d.value(0), 4200);
assert!(d.is_null(1));
assert_eq!(d.value(2), -150);
}

#[test]
#[cfg_attr(miri, ignore)]
fn test_cast_float_to_decimal_ansi_overflow_errors() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

test_cast_float_to_decimal_ansi_overflow_errors (diff, new test) only exercises a finite precision overflow (4242.42 into Decimal(4,2)). The rescan at numeric.rs (new ANSI block) also has to raise for NaN and infinity, since those make to_i128() return None and get nulled by unary_opt exactly like a precision overflow. The legacy-mode test_cast_float_to_decimal at numeric.rs:1265 covers NaN/Inf-to-null, but nothing covers NaN/Inf under ANSI, which is a distinct code path (closure returns None for a non-finite value, then rescan must recompute fits == false and error). If a future change made the rescan use a finite-only overflow predicate, this would silently regress with no failing test.

Suggested change: extend the ANSI error test with f64::NAN and f64::INFINITY inputs and assert is_err(), and assert the reported value string is "NaN" / "inf" to lock the message.

let a: ArrayRef = Arc::new(Float64Array::from(vec![Some(1.0), Some(f64::NAN)]));
assert!(cast_floating_point_to_decimal128::<Float64Type>(&a, 10, 2, EvalMode::Ansi).is_err());
let a: ArrayRef = Arc::new(Float64Array::from(vec![Some(f64::INFINITY)]));
assert!(cast_floating_point_to_decimal128::<Float64Type>(&a, 10, 2, EvalMode::Ansi).is_err());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

test_cast_float_to_decimal_ansi_overflow_errors asserts only result.is_err(). The whole point of the rescan is to reproduce Spark's exact NumericValueOutOfRange { value, precision, scale } with the first offending value in row order. A bare is_err() would still pass if the rescan returned the wrong variant, the wrong value, or the second offender instead of the first.

Suggested change: match on the error and assert the fields.

match cast_floating_point_to_decimal128::<Float64Type>(&a, 4, 2, EvalMode::Ansi) {
    Err(SparkError::NumericValueOutOfRange { value, precision, scale }) => {
        assert_eq!(value, "4242.42");
        assert_eq!(precision, 4);
        assert_eq!(scale, 2);
    }
    other => panic!("expected NumericValueOutOfRange, got {other:?}"),
}

// 4242.42 * 10^2 = 424242 does not fit precision 4 -> ANSI error.
let a: ArrayRef = Arc::new(Float64Array::from(vec![Some(1.0), Some(4242.42)]));
let result = cast_floating_point_to_decimal128::<Float64Type>(&a, 4, 2, EvalMode::Ansi);
assert!(result.is_err());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The rescan iterates 0..input.len() and returns the first row where fits is false, matching the old loop that erred on the first offending row. No test pins this. A batch with two overflowing rows where the second has a different value would not distinguish "first" from "last".

Suggested change: add an ANSI input like [Some(1.0), Some(overflow_a), Some(overflow_b)] where overflow_a and overflow_b stringify differently, and assert the error reports overflow_a.

#[test]
fn test_cast_decimal_to_timestamp() {
let timezones: [Option<Arc<str>>; 3] = [
Expand Down
Loading