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 5b0171a4141..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 @@ -689,7 +700,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 +716,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 +873,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 +1961,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..6a59d9b646f 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. @@ -1056,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 @@ -1080,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) }); @@ -2008,6 +2026,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] = [ @@ -2977,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}"); + } + } + } + } }