-
Notifications
You must be signed in to change notification settings - Fork 340
perf: vectorize floating-point-to-decimal cast #4940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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( | ||
|
|
@@ -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() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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()); | ||
| } | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rescan iterates Suggested change: add an ANSI input like |
||
| #[test] | ||
| fn test_cast_decimal_to_timestamp() { | ||
| let timezones: [Option<Arc<str>>; 3] = [ | ||
|
|
||
There was a problem hiding this comment.
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.42intoDecimal(4,2)). The rescan atnumeric.rs(new ANSI block) also has to raise for NaN and infinity, since those maketo_i128()returnNoneand get nulled byunary_optexactly like a precision overflow. The legacy-modetest_cast_float_to_decimalatnumeric.rs:1265covers NaN/Inf-to-null, but nothing covers NaN/Inf under ANSI, which is a distinct code path (closure returnsNonefor a non-finite value, then rescan must recomputefits == falseand 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::NANandf64::INFINITYinputs and assertis_err(), and assert the reported value string is"NaN"/"inf"to lock the message.