-
Notifications
You must be signed in to change notification settings - Fork 758
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
perf: Faster decimal precision overflow checks #6419
Changes from 2 commits
1c0a8ee
0f6266c
c68432e
5fcd17a
e4b22ce
5de6e14
aebad61
34cce0c
1a5262d
c995213
e8d24ab
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,53 @@ | ||
// 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::builder::{Decimal128Builder, Decimal256Builder}; | ||
use arrow_buffer::i256; | ||
use criterion::*; | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let len = 8192; | ||
let mut builder_128 = Decimal128Builder::with_capacity(len); | ||
let mut builder_256 = Decimal256Builder::with_capacity(len); | ||
for i in 0..len { | ||
if i % 10 == 0 { | ||
builder_128.append_value(i128::max_value()); | ||
builder_256.append_value(i256::from_i128(i128::max_value())); | ||
} else { | ||
builder_128.append_value(i as i128); | ||
builder_256.append_value(i256::from_i128(i as i128)); | ||
} | ||
} | ||
let array_128 = builder_128.finish(); | ||
let array_256 = builder_256.finish(); | ||
|
||
c.bench_function("validate_decimal_precision_128", |b| { | ||
b.iter(|| black_box(array_128.validate_decimal_precision(8).unwrap_or(()))); | ||
}); | ||
c.bench_function("null_if_overflow_precision_128", |b| { | ||
b.iter(|| black_box(array_128.null_if_overflow_precision(8))); | ||
}); | ||
c.bench_function("validate_decimal_precision_256", |b| { | ||
b.iter(|| black_box(array_256.validate_decimal_precision(8).unwrap_or(()))); | ||
}); | ||
c.bench_function("null_if_overflow_precision_256", |b| { | ||
b.iter(|| black_box(array_256.null_if_overflow_precision(8))); | ||
}); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -738,23 +738,32 @@ pub fn validate_decimal_precision(value: i128, precision: u8) -> Result<(), Arro | |
"Max precision of a Decimal128 is {DECIMAL128_MAX_PRECISION}, but got {precision}", | ||
))); | ||
} | ||
|
||
let max = MAX_DECIMAL_FOR_EACH_PRECISION[usize::from(precision) - 1]; | ||
let min = MIN_DECIMAL_FOR_EACH_PRECISION[usize::from(precision) - 1]; | ||
Comment on lines
-742
to
-743
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. I believe that this is where the 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. Hmm, this is interesting. By assigning a new variable, it did memory copying of the i128 value. |
||
|
||
if value > max { | ||
let idx = usize::from(precision) - 1; | ||
if value > MAX_DECIMAL_FOR_EACH_PRECISION[idx] { | ||
Err(ArrowError::InvalidArgumentError(format!( | ||
"{value} is too large to store in a Decimal128 of precision {precision}. Max is {max}" | ||
"{value} is too large to store in a Decimal128 of precision {precision}. Max is {}", | ||
MAX_DECIMAL_FOR_EACH_PRECISION[idx] | ||
))) | ||
} else if value < min { | ||
} else if value < MIN_DECIMAL_FOR_EACH_PRECISION[idx] { | ||
Err(ArrowError::InvalidArgumentError(format!( | ||
"{value} is too small to store in a Decimal128 of precision {precision}. Min is {min}" | ||
"{value} is too small to store in a Decimal128 of precision {precision}. Min is {}", | ||
MIN_DECIMAL_FOR_EACH_PRECISION[idx] | ||
))) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Determines whether the specified `i128` value can be properly | ||
/// interpreted as a Decimal number with precision `precision` | ||
#[inline] | ||
pub fn is_validate_decimal_precision(value: i128, precision: u8) -> bool { | ||
let idx = usize::from(precision) - 1; | ||
precision > DECIMAL128_MAX_PRECISION | ||
&& value >= MIN_DECIMAL_FOR_EACH_PRECISION[idx] | ||
&& value <= MAX_DECIMAL_FOR_EACH_PRECISION[idx] | ||
} | ||
|
||
/// Validates that the specified `i256` of value can be properly | ||
/// interpreted as a Decimal256 number with precision `precision` | ||
#[inline] | ||
|
@@ -764,18 +773,28 @@ pub fn validate_decimal256_precision(value: i256, precision: u8) -> Result<(), A | |
"Max precision of a Decimal256 is {DECIMAL256_MAX_PRECISION}, but got {precision}", | ||
))); | ||
} | ||
let max = MAX_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[usize::from(precision) - 1]; | ||
let min = MIN_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[usize::from(precision) - 1]; | ||
|
||
if value > max { | ||
let idx = usize::from(precision) - 1; | ||
if value > MAX_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[idx] { | ||
Err(ArrowError::InvalidArgumentError(format!( | ||
"{value:?} is too large to store in a Decimal256 of precision {precision}. Max is {max:?}" | ||
"{value:?} is too large to store in a Decimal256 of precision {precision}. Max is {:?}", | ||
MAX_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[idx] | ||
))) | ||
} else if value < min { | ||
} else if value < MIN_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[idx] { | ||
Err(ArrowError::InvalidArgumentError(format!( | ||
"{value:?} is too small to store in a Decimal256 of precision {precision}. Min is {min:?}" | ||
"{value:?} is too small to store in a Decimal256 of precision {precision}. Min is {:?}", | ||
MIN_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[idx] | ||
))) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Determines whether the specified `i256` value can be properly | ||
/// interpreted as a Decimal number with precision `precision` | ||
andygrove marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[inline] | ||
pub fn is_validate_decimal256_precision(value: i256, precision: u8) -> bool { | ||
let idx = usize::from(precision) - 1; | ||
precision > DECIMAL128_MAX_PRECISION | ||
&& value >= MIN_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[idx] | ||
&& value <= MAX_DECIMAL_BYTES_FOR_LARGER_EACH_PRECISION[idx] | ||
} |
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.
Is this used to avoid creating
ArrowError
and the error string for the cases that we don't need the error?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.
Yes