This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new fa289c25d4 perf(arrow-array): scan the values buffer in
`PrimitiveArray::validate_decimal_precision` (#12371)
fa289c25d4 is described below
commit fa289c25d4095108ff36d3f1c85b4f9bfe032df5
Author: Oleks V <[email protected]>
AuthorDate: Sun Nov 22 06:42:44 2026 +0000
perf(arrow-array): scan the values buffer in
`PrimitiveArray::validate_decimal_precision` (#12371)
# Which issue does this PR close?
- Closes https://github.com/apache/arrow-rs/issues/12368
# Rationale for this change
`PrimitiveArray::validate_decimal_precision` read the validity bit of
every slot and made an out-of-line call to
`DecimalType::validate_decimal_precision` for every valid value. In a
samply profile of `main` without LTO, for 8192 `Decimal128` values that
fit, 91.6% of the samples are self time in that callee, and 66% of all
samples are on its prologue and epilogue, which save and restore eight
registers. With 10% random nulls, the callee takes 62% and the loop
takes 38%.
# What changes are included in this PR?
- `validate_decimal_precision` scans the values buffer, null slots
included, with the inline `is_valid_decimal_precision`. A null slot can
hold any value, so it reads a validity bit only at a value that doesn't
fit. It returns the same error as before, for the first valid value that
doesn't fit. The `unsafe` block goes away.
- The first commit adds `decimal_overflow` cases whose values fit
precision 10 at scale 2, so `validate_decimal_precision(10)` reads every
row: `fits`, `fits nulls` (10% random nulls), `null slots overflow` (the
same nulls, each holding a value that doesn't fit) and `last row
overflow`, for `Decimal128` and `Decimal256`. The existing cases return
at the precision-below-scale check before they read a value. They stay
as they are.
# Are these changes tested?
`test_decimal_validate_precision_ignores_null_slots` covers null slots
that hold values that don't fit, including one before the first valid
value that doesn't fit, and sliced arrays, for all four decimal types.
It passes against the previous implementation and fails on one that also
checks null slots.
`decimal_overflow` on linux/arm64, a 4-CPU VM on an Apple M5 Max, with
the bot's toolchain, features and thin LTO. It used a local variant of
the Docker setup in https://github.com/apache/arrow-rs/pull/12351 that
takes both trees from the build context, because the baseline needs the
new cases. `base` is the first commit:
```
group base
changed
----- ----
-------
null_if_overflow_precision_128 1.00 4.1±0.05µs
? ?/sec 1.00 4.1±0.06µs ? ?/sec
null_if_overflow_precision_256 1.00 7.4±0.09µs
? ?/sec 1.01 7.5±0.09µs ? ?/sec
validate_decimal_precision_128 1.00 22.1±0.38ns
? ?/sec 1.00 22.1±0.47ns ? ?/sec
validate_decimal_precision_128 fits 3.39 11.1±0.24µs
? ?/sec 1.00 3.3±0.04µs ? ?/sec
validate_decimal_precision_128 fits nulls 3.86 14.6±0.10µs
? ?/sec 1.00 3.8±0.04µs ? ?/sec
validate_decimal_precision_128 last row overflow 3.35 11.3±0.08µs
? ?/sec 1.00 3.4±0.07µs ? ?/sec
validate_decimal_precision_128 null slots overflow 2.03 14.6±0.25µs
? ?/sec 1.00 7.2±0.21µs ? ?/sec
validate_decimal_precision_256 1.03 22.7±0.47ns
? ?/sec 1.00 22.0±0.33ns ? ?/sec
validate_decimal_precision_256 fits 2.16 13.2±0.12µs
? ?/sec 1.00 6.1±0.09µs ? ?/sec
validate_decimal_precision_256 fits nulls 2.86 17.3±0.15µs
? ?/sec 1.00 6.1±0.07µs ? ?/sec
validate_decimal_precision_256 last row overflow 2.15 13.4±0.17µs
? ?/sec 1.00 6.2±0.09µs ? ?/sec
validate_decimal_precision_256 null slots overflow 1.76 17.3±0.13µs
? ?/sec 1.00 9.8±0.15µs ? ?/sec
```
Inputs that get slower: arrays where many null slots hold values that
don't fit, because the scan takes its slow path at each of them. With
10% such nulls, `null slots overflow` above is still faster. The
builders, `unary_opt` (and so `null_if_overflow_precision`) and
`try_unary` write zeros into null slots, so this needs a values buffer
kept under a new null mask, for example from `nullif` or
`PrimitiveArray::new`, with many of those slots out of range.
# Are there any significant user-facing changes?
No.
# AI usage disclosure
Claude Code wrote the change, the test and the bench cases, profiled
with samply, and ran the benchmarks. I reviewed the changes and the
results.
---
arrow-array/benches/decimal_overflow.rs | 48 ++++++++++++++++++++++++++++++--
arrow-array/src/array/primitive_array.rs | 44 ++++++++++++++++++++++++-----
2 files changed, 83 insertions(+), 9 deletions(-)
diff --git a/arrow-array/benches/decimal_overflow.rs
b/arrow-array/benches/decimal_overflow.rs
index 377c696a12..9cf7e81e0e 100644
--- a/arrow-array/benches/decimal_overflow.rs
+++ b/arrow-array/benches/decimal_overflow.rs
@@ -3,8 +3,11 @@
// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
use arrow_array::builder::{Decimal128Builder, Decimal256Builder};
-use arrow_buffer::i256;
+use arrow_array::{Decimal128Array, Decimal256Array};
+use arrow_buffer::{NullBuffer, i256};
use criterion::*;
+use rand::prelude::StdRng;
+use rand::{RngExt, SeedableRng};
use std::hint;
fn criterion_benchmark(c: &mut Criterion) {
@@ -37,5 +40,46 @@ fn criterion_benchmark(c: &mut Criterion) {
});
}
-criterion_group!(benches, criterion_benchmark);
+/// Arrays at scale 2 whose values fit precision 10, except where the name
says otherwise, so that
+/// `validate_decimal_precision(10)` reads every row. The shapes with nulls
have 10% random nulls.
+fn validate_fitting_benchmark(c: &mut Criterion) {
+ const PRECISION: u8 = 10;
+ const OVERFLOW: i128 = 10_i128.pow(12);
+ let len = 8192;
+ let mut rng = StdRng::seed_from_u64(42);
+ let fits: Vec<i128> = (0..len as i128).map(|i| (i % 100_000) *
100).collect();
+ let nulls: NullBuffer = (0..len).map(|_| !rng.random_bool(0.1)).collect();
+ let null_slots_overflow = nulls
+ .iter()
+ .zip(&fits)
+ .map(|(valid, &v)| if valid { v } else { OVERFLOW })
+ .collect();
+ let mut last_row_overflow = fits.clone();
+ last_row_overflow[len - 1] = OVERFLOW;
+
+ let shapes = [
+ ("fits", fits.clone(), None),
+ ("fits nulls", fits, Some(nulls.clone())),
+ ("null slots overflow", null_slots_overflow, Some(nulls)),
+ ("last row overflow", last_row_overflow, None),
+ ];
+ for (name, values, nulls) in shapes {
+ let values_256 = values.iter().map(|&v| i256::from_i128(v)).collect();
+ let array_128 = Decimal128Array::new(values.into(), nulls.clone())
+ .with_precision_and_scale(38, 2)
+ .unwrap();
+ let array_256 = Decimal256Array::new(values_256, nulls)
+ .with_precision_and_scale(76, 2)
+ .unwrap();
+
+ c.bench_function(&format!("validate_decimal_precision_128 {name}"),
|b| {
+ b.iter(||
hint::black_box(&array_128).validate_decimal_precision(PRECISION));
+ });
+ c.bench_function(&format!("validate_decimal_precision_256 {name}"),
|b| {
+ b.iter(||
hint::black_box(&array_256).validate_decimal_precision(PRECISION));
+ });
+ }
+}
+
+criterion_group!(benches, criterion_benchmark, validate_fitting_benchmark);
criterion_main!(benches);
diff --git a/arrow-array/src/array/primitive_array.rs
b/arrow-array/src/array/primitive_array.rs
index f9cd7a2084..5c334c1a6c 100644
--- a/arrow-array/src/array/primitive_array.rs
+++ b/arrow-array/src/array/primitive_array.rs
@@ -1712,14 +1712,14 @@ impl<T: DecimalType + ArrowPrimitiveType>
PrimitiveArray<T> {
self.scale()
)));
}
- (0..self.len()).try_for_each(|idx| {
- if self.is_valid(idx) {
- let decimal = unsafe { self.value_unchecked(idx) };
- T::validate_decimal_precision(decimal, precision, self.scale())
- } else {
- Ok(())
+ // A null slot can hold any value, so read the validity bit only for a
value that
+ // doesn't fit
+ for (idx, &value) in self.values().iter().enumerate() {
+ if !T::is_valid_decimal_precision(value, precision) &&
self.is_valid(idx) {
+ return T::validate_decimal_precision(value, precision,
self.scale());
}
- })
+ }
+ Ok(())
}
/// Validates the Decimal Array, if the value of slot is overflow for the
specified precision, and
@@ -2667,6 +2667,36 @@ mod tests {
check::<Decimal256Type>();
}
+ #[test]
+ fn test_decimal_validate_precision_ignores_null_slots() {
+ fn check<T: DecimalType>()
+ where
+ T::Native: From<i32>,
+ {
+ // Rows 1 and 3 are null and hold values that don't fit precision 3
+ let values = [1, -1000, 2, -2000, 3, 1000, 4].map(T::Native::from);
+ let nulls = NullBuffer::from(&[true, false, true, false, true,
true, true]);
+ let array = PrimitiveArray::<T>::new(values.into_iter().collect(),
Some(nulls))
+ .with_precision_and_scale(4, 0)
+ .unwrap();
+ let too_large = format!(
+ "Invalid argument error: 1000 is too large to store in a {} of
precision 3. Max is 999",
+ T::PREFIX
+ );
+ for array in [array.clone(), array.slice(2, 5)] {
+ let error = array.validate_decimal_precision(3).unwrap_err();
+ assert_eq!(error.to_string(), too_large);
+ }
+ array.slice(0, 5).validate_decimal_precision(3).unwrap();
+ array.slice(1, 3).validate_decimal_precision(3).unwrap();
+ }
+
+ check::<Decimal32Type>();
+ check::<Decimal64Type>();
+ check::<Decimal128Type>();
+ check::<Decimal256Type>();
+ }
+
#[test]
fn test_decimal_from_iter_values() {
let array = Decimal128Array::from_iter_values(vec![-100, 0, 101]);