From d3c70a92d251bbbae5cb87a22e9e8f36615f6fb0 Mon Sep 17 00:00:00 2001 From: Bhargava Vadlamani Date: Sat, 14 Nov 2026 16:33:26 -0500 Subject: [PATCH 1/2] perf cast kernels --- .../expression-audits/conversion_funcs.md | 5 + .../spark-expr/src/conversion_funcs/cast.rs | 85 ++++++++++++--- .../src/conversion_funcs/numeric.rs | 101 +++++++++++++++++- 3 files changed, 172 insertions(+), 19 deletions(-) diff --git a/docs/source/contributor-guide/expression-audits/conversion_funcs.md b/docs/source/contributor-guide/expression-audits/conversion_funcs.md index c86b3dcbce0..2deefa17119 100644 --- a/docs/source/contributor-guide/expression-audits/conversion_funcs.md +++ b/docs/source/contributor-guide/expression-audits/conversion_funcs.md @@ -40,6 +40,11 @@ - Performance (tuned 2026-07-14, PR [#4920](https://github.com/apache/datafusion-comet/pull/4920)): narrowing integer casts (`spark_cast_int_to_int`) map the values buffer in a single pass with Arrow `unary`/`try_unary` and carry the null buffer over untouched, replacing an element-by-element `Option`/`Result` iterator-collect. Up to 100x faster on narrowing casts. Benchmark: `benches/cast_numeric.rs`. - Performance (tuned 2026-07-15, PR [#4940](https://github.com/apache/datafusion-comet/pull/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`. - Performance (tuned 2026-07-15, PR [#4939](https://github.com/apache/datafusion-comet/pull/4939)): integer-to-decimal casts (`cast_int_to_decimal128_internal`) now convert in a single vectorized `unary_opt` pass that maps overflowing values to null, replacing the per-element `Decimal128Builder` loop. ANSI raises via an O(1) null-count check plus a rare element-wise rescan. 28-50% faster with no regression on any shape. Benchmark: `benches/cast_int_to_decimal.rs`. +- Performance (tuned 2026-07-22, PR [#4912](https://github.com/apache/datafusion-comet/pull/4912)): binary-to-string casts in Spark 4's `ToPrettyString` styles (`cast_binary_to_string`) encode straight into one pre-sized `GenericStringBuilder` with table-driven uppercase hex, replacing an owned `String` per row (and per byte for the hex styles). The hex styles are 15-27x faster, basic 2.5x and base64 1.2x. The default and UTF-8 styles are unchanged. Benchmark: `benches/cast_binary_to_string.rs`. +- Performance (tuned 2026-07-22, PR [#4916](https://github.com/apache/datafusion-comet/pull/4916)): string-to-decimal casts (`parse_string_to_decimal`) validate and accumulate the digits into an `i128` in one byte pass, replacing six scans and two `str::parse` calls, check the first byte before the inf/nan comparisons, and use a constant power-of-ten table. 36-41% faster. Benchmark: `benches/cast_from_string.rs`. +- Performance (tuned 2026-07-22, PR [#4917](https://github.com/apache/datafusion-comet/pull/4917)): string-to-date casts (`date_parser`) validate the calendar date with integer arithmetic and the existing `days_from_civil` helper instead of building a chrono `NaiveDate` per row, with a fast path for the canonical `yyyy-mm-dd` form. 55% faster on canonical input and 24% on mixed input. Benchmark: `benches/cast_string_to_date.rs`. +- Performance (tuned 2026-07-22, PR [#4924](https://github.com/apache/datafusion-comet/pull/4924)): LEGACY decimal-to-string casts (`cast_decimal128_to_utf8`) render the coefficient into a stack digit buffer in 19-digit chunks and append it to a reused buffer, replacing a heap `to_string`, `format!` and `"0".repeat` per row. 21-74% faster. Benchmark: `benches/cast_decimal_to_string.rs`. +- Performance (tuned 2026-07-23, PR [#4918](https://github.com/apache/datafusion-comet/pull/4918)): float/double-to-string casts (`spark_cast_float64_to_utf8`, `spark_cast_float32_to_utf8`) format straight into a `GenericStringBuilder` with a reused scratch buffer instead of a `String` per row. About 42% faster. Benchmark: `benches/cast_float_to_string.rs`. - Performance (tuned 2026-09-30, PR [#5130](https://github.com/apache/datafusion-comet/pull/5130)): string-to-timestamp and string-to-timestamp_ntz (`timestamp_parser` / `timestamp_ntz_parser`) classify the input shape once with a single byte scan, replacing up to 34 sequential regex `is_match` calls per row (27 for the NTZ variant). The shapes only accept ASCII digits, so the cast no longer runs a regex at all. 40-86% faster (1.7x-6.9x, median 3.0x) across all 118 benchmark cases, none regressed. Benchmark: `benches/cast_string_to_timestamp.rs`. - Performance (tuned 2026-10-14, PR [#6969](https://github.com/apache/datafusion-comet/pull/6969)): casts to timestamp from int, decimal, float, and boolean (`cast_int_to_timestamp`, `cast_decimal_to_timestamp`, `cast_float_to_timestamp`, `cast_boolean_to_timestamp`) use Arrow `unary` / `try_unary` / `unary_opt` / `from_unary` instead of a per-row `TimestampMicrosecondBuilder`. Integer casts are about 9-12x faster and boolean casts about 5-7x. Float casts are 14-35% faster with few nulls and about 4-16x faster when most rows are null, because `try_unary` and `unary_opt` skip null slots. Decimal uses one `unary_opt` pass: about 9% faster with no nulls (under 10%; the 256-bit division dominates), about 19% with sparse nulls, about 2.7x with dense nulls, and about 16x when every row is null. No shape regressed. Benchmarks: `benches/cast_int_to_timestamp.rs`, `benches/cast_non_int_numeric_timestamp.rs`. diff --git a/native/spark-expr/src/conversion_funcs/cast.rs b/native/spark-expr/src/conversion_funcs/cast.rs index 5b0171a4141..01f547c2f40 100644 --- a/native/spark-expr/src/conversion_funcs/cast.rs +++ b/native/spark-expr/src/conversion_funcs/cast.rs @@ -689,7 +689,6 @@ fn cast_array_to_string( spark_cast_options: &SparkCastOptions, ) -> DataFusionResult { let mut builder = StringBuilder::with_capacity(array.len(), array.len() * 16); - let mut str = String::with_capacity(array.len() * 16); let casted_values = cast_array( Arc::clone(array.values()), @@ -706,25 +705,26 @@ fn cast_array_to_string( if array.is_null(row_index) { builder.append_null(); } else { - str.clear(); let start = offsets[row_index] as usize; let end = offsets[row_index + 1] as usize; - str.push('['); - let mut first = true; - for idx in start..end { - if !first { - str.push_str(", "); - } - if string_values.is_null(idx) { - str.push_str(&spark_cast_options.null_string); - } else { - str.push_str(string_values.value(idx)); + // Write the row straight into the builder's value buffer, then end it with an empty + // `append_value`, instead of assembling it in a scratch `String` and copying it again. + let written = builder.write_char('[').and_then(|()| { + for idx in start..end { + if idx > start { + builder.write_str(", ")?; + } + if string_values.is_null(idx) { + builder.write_str(&spark_cast_options.null_string)?; + } else { + builder.write_str(string_values.value(idx))?; + } } - first = false; - } - str.push(']'); - builder.append_value(&str); + builder.write_char(']') + }); + written.expect("writing to a string builder cannot fail"); + builder.append_value(""); } } Ok(Arc::new(builder.finish())) @@ -862,7 +862,25 @@ fn write_upper_hex(out: &mut W, byte: u8) -> std::fmt::Result { /// Writes `byte` reinterpreted as a signed decimal, as Spark does when printing a byte array. #[inline] fn write_i8(out: &mut W, byte: u8) -> std::fmt::Result { - write!(out, "{}", byte as i8) + let value = byte as i8; + let mut magnitude = value.unsigned_abs(); + // At most "-128": a sign and three digits, filled from the end. + let mut buf = [0u8; 4]; + let mut start = buf.len(); + loop { + start -= 1; + buf[start] = b'0' + magnitude % 10; + magnitude /= 10; + if magnitude == 0 { + break; + } + } + if value < 0 { + start -= 1; + buf[start] = b'-'; + } + // SAFETY: `buf[start..]` holds only ASCII digits and an optional '-', so it is valid UTF-8. + out.write_str(unsafe { std::str::from_utf8_unchecked(&buf[start..]) }) } /// Writes the bytes of `value` between square brackets, encoded with `encode` and joined by @@ -1932,6 +1950,39 @@ mod tests { assert_eq!(r#"[]"#, string_array.value(3)); } + #[test] + fn test_cast_sliced_array_with_null_rows_to_string() { + // A null row writes no bytes, so the rows after it must still start at the right offset, + // and a sliced list reads its values from a non-zero offset into the child array. + let list_array = ListArray::from_iter_primitive::(vec![ + Some(vec![Some(0)]), + Some(vec![Some(1), None]), + None, + Some(vec![]), + None, + Some(vec![Some(-2), Some(3)]), + ]); + let sliced = list_array.slice(1, 5); + let string_array = + cast_array_to_string(&sliced, &SparkCastOptions::new(EvalMode::Legacy, "UTC")).unwrap(); + let string_array = string_array.as_string::(); + assert_eq!(string_array.len(), 5); + assert_eq!(string_array.value(0), "[1, null]"); + assert!(string_array.is_null(1)); + assert_eq!(string_array.value(2), "[]"); + assert!(string_array.is_null(3)); + assert_eq!(string_array.value(4), "[-2, 3]"); + } + + #[test] + fn test_write_i8_matches_signed_display_for_every_byte() { + for byte in 0..=u8::MAX { + let mut out = String::new(); + write_i8(&mut out, byte).unwrap(); + assert_eq!(out, (byte as i8).to_string(), "byte {byte}"); + } + } + #[test] fn test_cast_array_of_nulls_to_array() { let offsets_buffer = OffsetBuffer::::new(vec![0, 2, 3, 3].into()); diff --git a/native/spark-expr/src/conversion_funcs/numeric.rs b/native/spark-expr/src/conversion_funcs/numeric.rs index 9a9a7bf915e..2918b51acbe 100644 --- a/native/spark-expr/src/conversion_funcs/numeric.rs +++ b/native/spark-expr/src/conversion_funcs/numeric.rs @@ -31,7 +31,7 @@ use arrow::datatypes::{ Decimal128Type, Float32Type, Float64Type, Int16Type, Int32Type, Int64Type, Int8Type, TimestampMicrosecondType, }; -use num::{cast::AsPrimitive, Float, ToPrimitive}; +use num::{cast::AsPrimitive, Bounded, Float, ToPrimitive}; use std::fmt::{self, Write}; use std::sync::Arc; @@ -769,7 +769,7 @@ fn cast_int_to_decimal128_internal( ) -> SparkResult where T: ArrowPrimitiveType, - T::Native: Into, + T::Native: Into + Bounded, { let multiplier = 10_i128.pow(scale as u32); @@ -780,6 +780,17 @@ where .filter(|scaled| is_validate_decimal_precision(*scaled, precision)) }; + // When both ends of the input type fit, no value can overflow in any eval mode, so one + // branch-free `unary` pass replaces the per-row check. It also scales the slots under nulls, + // which is safe because they hold values of the same type, and keeps the input null buffer. + if fits(T::Native::min_value().into()).is_some() + && fits(T::Native::max_value().into()).is_some() + { + let result: Decimal128Array = + array.unary::<_, Decimal128Type>(|v| Into::::into(v) * multiplier); + return Ok(Arc::new(result.with_precision_and_scale(precision, scale)?)); + } + // Single vectorized pass: a value that overflows the multiply or 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, replacing the per-element builder loop without a second pass. @@ -2008,6 +2019,92 @@ mod tests { ); } + #[test] + fn test_cast_int_to_decimal128_whole_range_fits_in_every_mode() { + // DECIMAL(3, 0) holds every TINYINT and DECIMAL(19, 0) every BIGINT, so the batch takes + // the branch-free path: the extremes scale exactly, nulls stay null and ANSI never raises. + for eval_mode in [EvalMode::Legacy, EvalMode::Try, EvalMode::Ansi] { + let array: ArrayRef = Arc::new(Int8Array::from(vec![ + Some(i8::MIN), + None, + Some(0), + Some(i8::MAX), + ])); + let result = cast_int_to_decimal128( + &array, + eval_mode, + &DataType::Int8, + &DataType::Decimal128(3, 0), + 3, + 0, + ) + .unwrap(); + let d = result.as_primitive::(); + assert_eq!(d.value(0), -128, "{eval_mode:?}"); + assert!(d.is_null(1), "{eval_mode:?}"); + assert_eq!(d.value(2), 0, "{eval_mode:?}"); + assert_eq!(d.value(3), 127, "{eval_mode:?}"); + assert_eq!(d.data_type(), &DataType::Decimal128(3, 0)); + + let array: ArrayRef = Arc::new(Int64Array::from(vec![Some(i64::MIN), Some(i64::MAX)])); + let result = cast_int_to_decimal128( + &array, + eval_mode, + &DataType::Int64, + &DataType::Decimal128(23, 4), + 23, + 4, + ) + .unwrap(); + let d = result.as_primitive::(); + assert_eq!(d.value(0), i64::MIN as i128 * 10_000, "{eval_mode:?}"); + assert_eq!(d.value(1), i64::MAX as i128 * 10_000, "{eval_mode:?}"); + } + } + + #[test] + fn test_cast_int_to_decimal128_one_digit_short_keeps_per_row_check() { + // DECIMAL(2, 0) cannot hold -128 or 127, so the per-row check still runs: values that fit + // convert, the ones that don't become null (LEGACY, TRY) or raise (ANSI). + let array: ArrayRef = Arc::new(Int8Array::from(vec![Some(99), Some(127), None, Some(-99)])); + for eval_mode in [EvalMode::Legacy, EvalMode::Try] { + let result = cast_int_to_decimal128( + &array, + eval_mode, + &DataType::Int8, + &DataType::Decimal128(2, 0), + 2, + 0, + ) + .unwrap(); + let d = result.as_primitive::(); + assert_eq!(d.value(0), 99, "{eval_mode:?}"); + assert!(d.is_null(1), "{eval_mode:?}"); + assert!(d.is_null(2), "{eval_mode:?}"); + assert_eq!(d.value(3), -99, "{eval_mode:?}"); + } + let err = cast_int_to_decimal128( + &array, + EvalMode::Ansi, + &DataType::Int8, + &DataType::Decimal128(2, 0), + 2, + 0, + ) + .unwrap_err(); + assert!( + matches!( + err, + SparkError::NumericValueOutOfRange { + ref value, + precision: 2, + scale: 0 + } if value == "127" + ), + "unexpected error: {err:?}" + ); + } + #[test] fn test_cast_int_to_timestamp() { let timezones: [Option>; 6] = [ From f08e80569d49dc2e7f6f0b6a47c66c3e1dcfeb75 Mon Sep 17 00:00:00 2001 From: Bhargava Vadlamani Date: Sat, 14 Nov 2026 23:31:06 -0500 Subject: [PATCH 2/2] perf: native boolean casts and an i64 fast path for float to decimal --- .../expression-audits/conversion_funcs.md | 5 -- .../src/conversion_funcs/boolean.rs | 63 ++++++++++++++++++- .../spark-expr/src/conversion_funcs/cast.rs | 15 ++++- .../src/conversion_funcs/numeric.rs | 52 ++++++++++++++- 4 files changed, 123 insertions(+), 12 deletions(-) diff --git a/docs/source/contributor-guide/expression-audits/conversion_funcs.md b/docs/source/contributor-guide/expression-audits/conversion_funcs.md index 2deefa17119..c86b3dcbce0 100644 --- a/docs/source/contributor-guide/expression-audits/conversion_funcs.md +++ b/docs/source/contributor-guide/expression-audits/conversion_funcs.md @@ -40,11 +40,6 @@ - Performance (tuned 2026-07-14, PR [#4920](https://github.com/apache/datafusion-comet/pull/4920)): narrowing integer casts (`spark_cast_int_to_int`) map the values buffer in a single pass with Arrow `unary`/`try_unary` and carry the null buffer over untouched, replacing an element-by-element `Option`/`Result` iterator-collect. Up to 100x faster on narrowing casts. Benchmark: `benches/cast_numeric.rs`. - Performance (tuned 2026-07-15, PR [#4940](https://github.com/apache/datafusion-comet/pull/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`. - Performance (tuned 2026-07-15, PR [#4939](https://github.com/apache/datafusion-comet/pull/4939)): integer-to-decimal casts (`cast_int_to_decimal128_internal`) now convert in a single vectorized `unary_opt` pass that maps overflowing values to null, replacing the per-element `Decimal128Builder` loop. ANSI raises via an O(1) null-count check plus a rare element-wise rescan. 28-50% faster with no regression on any shape. Benchmark: `benches/cast_int_to_decimal.rs`. -- Performance (tuned 2026-07-22, PR [#4912](https://github.com/apache/datafusion-comet/pull/4912)): binary-to-string casts in Spark 4's `ToPrettyString` styles (`cast_binary_to_string`) encode straight into one pre-sized `GenericStringBuilder` with table-driven uppercase hex, replacing an owned `String` per row (and per byte for the hex styles). The hex styles are 15-27x faster, basic 2.5x and base64 1.2x. The default and UTF-8 styles are unchanged. Benchmark: `benches/cast_binary_to_string.rs`. -- Performance (tuned 2026-07-22, PR [#4916](https://github.com/apache/datafusion-comet/pull/4916)): string-to-decimal casts (`parse_string_to_decimal`) validate and accumulate the digits into an `i128` in one byte pass, replacing six scans and two `str::parse` calls, check the first byte before the inf/nan comparisons, and use a constant power-of-ten table. 36-41% faster. Benchmark: `benches/cast_from_string.rs`. -- Performance (tuned 2026-07-22, PR [#4917](https://github.com/apache/datafusion-comet/pull/4917)): string-to-date casts (`date_parser`) validate the calendar date with integer arithmetic and the existing `days_from_civil` helper instead of building a chrono `NaiveDate` per row, with a fast path for the canonical `yyyy-mm-dd` form. 55% faster on canonical input and 24% on mixed input. Benchmark: `benches/cast_string_to_date.rs`. -- Performance (tuned 2026-07-22, PR [#4924](https://github.com/apache/datafusion-comet/pull/4924)): LEGACY decimal-to-string casts (`cast_decimal128_to_utf8`) render the coefficient into a stack digit buffer in 19-digit chunks and append it to a reused buffer, replacing a heap `to_string`, `format!` and `"0".repeat` per row. 21-74% faster. Benchmark: `benches/cast_decimal_to_string.rs`. -- Performance (tuned 2026-07-23, PR [#4918](https://github.com/apache/datafusion-comet/pull/4918)): float/double-to-string casts (`spark_cast_float64_to_utf8`, `spark_cast_float32_to_utf8`) format straight into a `GenericStringBuilder` with a reused scratch buffer instead of a `String` per row. About 42% faster. Benchmark: `benches/cast_float_to_string.rs`. - Performance (tuned 2026-09-30, PR [#5130](https://github.com/apache/datafusion-comet/pull/5130)): string-to-timestamp and string-to-timestamp_ntz (`timestamp_parser` / `timestamp_ntz_parser`) classify the input shape once with a single byte scan, replacing up to 34 sequential regex `is_match` calls per row (27 for the NTZ variant). The shapes only accept ASCII digits, so the cast no longer runs a regex at all. 40-86% faster (1.7x-6.9x, median 3.0x) across all 118 benchmark cases, none regressed. Benchmark: `benches/cast_string_to_timestamp.rs`. - Performance (tuned 2026-10-14, PR [#6969](https://github.com/apache/datafusion-comet/pull/6969)): casts to timestamp from int, decimal, float, and boolean (`cast_int_to_timestamp`, `cast_decimal_to_timestamp`, `cast_float_to_timestamp`, `cast_boolean_to_timestamp`) use Arrow `unary` / `try_unary` / `unary_opt` / `from_unary` instead of a per-row `TimestampMicrosecondBuilder`. Integer casts are about 9-12x faster and boolean casts about 5-7x. Float casts are 14-35% faster with few nulls and about 4-16x faster when most rows are null, because `try_unary` and `unary_opt` skip null slots. Decimal uses one `unary_opt` pass: about 9% faster with no nulls (under 10%; the 256-bit division dominates), about 19% with sparse nulls, about 2.7x with dense nulls, and about 16x when every row is null. No shape regressed. Benchmarks: `benches/cast_int_to_timestamp.rs`, `benches/cast_non_int_numeric_timestamp.rs`. diff --git a/native/spark-expr/src/conversion_funcs/boolean.rs b/native/spark-expr/src/conversion_funcs/boolean.rs index c1fef4238c3..18c5a6e2531 100644 --- a/native/spark-expr/src/conversion_funcs/boolean.rs +++ b/native/spark-expr/src/conversion_funcs/boolean.rs @@ -17,8 +17,8 @@ use crate::conversion_funcs::utils::timestamp_micros_with_tz; use crate::SparkResult; -use arrow::array::{ArrayRef, AsArray, PrimitiveArray}; -use arrow::datatypes::{DataType, TimestampMicrosecondType}; +use arrow::array::{ArrayRef, AsArray, PrimitiveArray, StringBuilder}; +use arrow::datatypes::{ArrowPrimitiveType, DataType, TimestampMicrosecondType}; use std::sync::Arc; /// Boolean -> Decimal is intentionally absent: it has no native path and is routed through the @@ -43,6 +43,34 @@ pub(crate) fn cast_boolean_to_timestamp( Ok(timestamp_micros_with_tz(micros, target_tz)) } +/// true is 1 and false is 0, as in Spark. `from_unary` maps every slot in one pass and keeps the +/// null buffer, where Arrow's cast checks each row for null. +pub(crate) fn cast_boolean_to_numeric(array_ref: &ArrayRef) -> ArrayRef +where + T: ArrowPrimitiveType, + T::Native: From, +{ + Arc::new(PrimitiveArray::::from_unary( + array_ref.as_boolean(), + T::Native::from, + )) +} + +/// "true" or "false", as in Spark, into a builder sized for the longest value, where Arrow's cast +/// formats each row through a dynamic formatter. +pub(crate) fn cast_boolean_to_utf8(array_ref: &ArrayRef) -> ArrayRef { + let bool_array = array_ref.as_boolean(); + let mut builder = StringBuilder::with_capacity(bool_array.len(), bool_array.len() * 5); + for value in bool_array.iter() { + match value { + Some(true) => builder.append_value("true"), + Some(false) => builder.append_value("false"), + None => builder.append_null(), + } + } + Arc::new(builder.finish()) +} + #[cfg(test)] mod tests { use super::*; @@ -264,4 +292,35 @@ mod tests { } } } + + #[test] + fn test_bool_casts_match_arrow_cast() { + // The native kernels replaced Arrow's cast for these targets; on a sliced input with nulls + // they must give the same arrays. + let input: ArrayRef = Arc::new(BooleanArray::from(vec![ + Some(false), + Some(true), + None, + Some(false), + None, + Some(true), + ])); + let sliced = input.slice(1, 5); + for to_type in [ + DataType::Int8, + DataType::Int16, + DataType::Int32, + DataType::Int64, + DataType::Float32, + DataType::Float64, + DataType::Utf8, + ] { + for array in [&input, &sliced] { + let actual = + cast_array(Arc::clone(array), &to_type, &test_input_spark_opts()).unwrap(); + let expected = arrow::compute::cast(array, &to_type).unwrap(); + assert_eq!(&actual, &expected, "{to_type}"); + } + } + } } diff --git a/native/spark-expr/src/conversion_funcs/cast.rs b/native/spark-expr/src/conversion_funcs/cast.rs index 01f547c2f40..270fc0f6759 100644 --- a/native/spark-expr/src/conversion_funcs/cast.rs +++ b/native/spark-expr/src/conversion_funcs/cast.rs @@ -16,7 +16,8 @@ // under the License. use crate::conversion_funcs::boolean::{ - cast_boolean_to_timestamp, is_df_cast_from_bool_spark_compatible, + cast_boolean_to_numeric, cast_boolean_to_timestamp, cast_boolean_to_utf8, + is_df_cast_from_bool_spark_compatible, }; use crate::conversion_funcs::numeric::{ cast_decimal128_to_float32, cast_decimal128_to_float64, cast_decimal128_to_utf8, @@ -47,7 +48,10 @@ use arrow::array::{ StringArray, StructArray, }; use arrow::buffer::{BooleanBuffer, Buffer, NullBuffer, OffsetBuffer}; -use arrow::datatypes::{format_decimal_str, DataType, GenericBinaryType, Schema}; +use arrow::datatypes::{ + format_decimal_str, DataType, Float32Type, Float64Type, GenericBinaryType, Int16Type, + Int32Type, Int64Type, Int8Type, Schema, +}; use arrow::error::ArrowError; use arrow::{ array::{ @@ -471,6 +475,13 @@ pub(crate) fn cast_array( (Int8 | Int16 | Int32 | Int64, Timestamp(_, tz)) => cast_int_to_timestamp(&array, tz), (Float32 | Float64, Timestamp(_, tz)) => cast_float_to_timestamp(&array, tz, eval_mode), (Boolean, Timestamp(_, tz)) => cast_boolean_to_timestamp(&array, tz), + (Boolean, Int8) => Ok(cast_boolean_to_numeric::(&array)), + (Boolean, Int16) => Ok(cast_boolean_to_numeric::(&array)), + (Boolean, Int32) => Ok(cast_boolean_to_numeric::(&array)), + (Boolean, Int64) => Ok(cast_boolean_to_numeric::(&array)), + (Boolean, Float32) => Ok(cast_boolean_to_numeric::(&array)), + (Boolean, Float64) => Ok(cast_boolean_to_numeric::(&array)), + (Boolean, Utf8) => Ok(cast_boolean_to_utf8(&array)), (Decimal128(_, scale), Timestamp(_, tz)) => cast_decimal_to_timestamp(&array, tz, *scale), // NTZ → TIMESTAMP: interpret NTZ local-epoch value as session-TZ local time, convert to UTC. // Must come before the is_datafusion_spark_compatible fallthrough which would diff --git a/native/spark-expr/src/conversion_funcs/numeric.rs b/native/spark-expr/src/conversion_funcs/numeric.rs index 2918b51acbe..6a59d9b646f 100644 --- a/native/spark-expr/src/conversion_funcs/numeric.rs +++ b/native/spark-expr/src/conversion_funcs/numeric.rs @@ -1067,6 +1067,9 @@ where // digits are. The slack generously covers the same scaling error that TIE_GUARD below // bounds, so borderline values still take the exact path. let overflow_bound = 10_f64.powi(precision as i32) * (1.0 + 1e-12); + // Up to precision 18 the bound above keeps a rounded value below 2^63, so it converts + // through `i64`, one instruction, instead of the library call `to_i128` makes. + let fits_i64 = precision <= 18; // A value with no decimal 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 @@ -1091,9 +1094,13 @@ where const TIE_GUARD: f64 = 3.552713678800501e-15; // 2^-48 let r = x.round(); if (x - r).abs() + x.abs() * TIE_GUARD < 0.5 { - return r - .to_i128() - .filter(|u| is_validate_decimal_precision(*u, precision)); + // `r` is finite here: NaN fails the comparison above. + let unscaled = if fits_i64 { + Some(r as i64 as i128) + } else { + r.to_i128() + }; + return unscaled.filter(|u| is_validate_decimal_precision(*u, precision)); } float_to_decimal128(f, precision, scale) }); @@ -3074,4 +3081,43 @@ mod tests { let out = cast_float_to_timestamp(&array, &tz, EvalMode::Ansi).unwrap(); assert_ts(&out, &[Some(1), Some(-1)], Some("UTC")); } + + #[test] + fn test_cast_float_to_decimal_i64_path_matches_i128_path() { + // Precision 18 rounds through `i64` and precision 19 still through `to_i128`; wherever the + // value fits both, the unscaled results must agree, up to the edge of precision 18. + let mut inputs = vec![ + 0.0, + -0.0, + 0.5, + -0.5, + 1.5, + -2.5, + 99_999_999_999_999.99, + -99_999_999_999_999.99, + 1e14, + 999_999_999_999_999.9, + ]; + let mut x: u64 = 0x2545_F491_4F6C_DD1D; + for _ in 0..10_000 { + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + inputs.push((x as i64 as f64) / 1e5); + } + for scale in [0i8, 4] { + let array: ArrayRef = Arc::new(Float64Array::from(inputs.clone())); + let narrow = cast_float64_to_decimal128(&array, 18, scale, EvalMode::Legacy).unwrap(); + let wide = cast_float64_to_decimal128(&array, 19, scale, EvalMode::Legacy).unwrap(); + let (narrow, wide) = ( + narrow.as_primitive::(), + wide.as_primitive::(), + ); + for (i, input) in inputs.iter().enumerate() { + if !narrow.is_null(i) { + assert_eq!(narrow.value(i), wide.value(i), "{input} at scale {scale}"); + } + } + } + } }