-
Notifications
You must be signed in to change notification settings - Fork 784
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
Speed up Decimal256
validation based on bytes comparison and add benchmark test
#2360
Changes from all commits
d553a99
a64b8ce
f03830b
3fc0797
3c2a489
5334c79
364929e
6f5b06a
59d26fd
0947468
1961c74
ce67555
fccf3b3
0bd4df4
fac1f71
3aab001
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,71 @@ | ||
// 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. | ||
|
||
#[macro_use] | ||
extern crate criterion; | ||
|
||
use arrow::array::{Array, Decimal128Array, Decimal256Array, Decimal256Builder}; | ||
use criterion::Criterion; | ||
|
||
extern crate arrow; | ||
|
||
use arrow::util::decimal::Decimal256; | ||
|
||
fn validate_decimal128_array(array: Decimal128Array) { | ||
array.with_precision_and_scale(35, 0).unwrap(); | ||
} | ||
|
||
fn validate_decimal256_array(array: Decimal256Array) { | ||
array.with_precision_and_scale(35, 0).unwrap(); | ||
} | ||
|
||
fn validate_decimal128_benchmark(c: &mut Criterion) { | ||
let decimal_array = Decimal128Array::from_iter_values(vec![12324; 20000]); | ||
let data = decimal_array.into_data(); | ||
c.bench_function("validate_decimal128_array 20000", |b| { | ||
b.iter(|| { | ||
let array = Decimal128Array::from(data.clone()); | ||
validate_decimal128_array(array); | ||
}) | ||
}); | ||
} | ||
|
||
fn validate_decimal256_benchmark(c: &mut Criterion) { | ||
let mut decimal_builder = Decimal256Builder::new(20000, 76, 0); | ||
let mut bytes = vec![0; 32]; | ||
bytes[0..16].clone_from_slice(&12324_i128.to_le_bytes()); | ||
for _ in 0..20000 { | ||
decimal_builder | ||
.append_value(&Decimal256::new(76, 0, &bytes)) | ||
.unwrap(); | ||
} | ||
let decimal_array256_data = decimal_builder.finish(); | ||
let data = decimal_array256_data.into_data(); | ||
c.bench_function("validate_decimal256_array 20000", |b| { | ||
b.iter(|| { | ||
let array = Decimal256Array::from(data.clone()); | ||
validate_decimal256_array(array); | ||
}) | ||
}); | ||
} | ||
|
||
criterion_group!( | ||
benches, | ||
validate_decimal128_benchmark, | ||
validate_decimal256_benchmark, | ||
); | ||
criterion_main!(benches); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,10 +29,9 @@ use super::{BasicDecimalIter, BooleanBufferBuilder, FixedSizeBinaryArray}; | |
#[allow(deprecated)] | ||
pub use crate::array::DecimalIter; | ||
use crate::buffer::{Buffer, MutableBuffer}; | ||
use crate::datatypes::DataType; | ||
use crate::datatypes::{validate_decimal256_precision_with_lt_bytes, DataType}; | ||
use crate::datatypes::{ | ||
validate_decimal256_precision, validate_decimal_precision, DECIMAL256_MAX_PRECISION, | ||
DECIMAL_DEFAULT_SCALE, | ||
validate_decimal_precision, DECIMAL256_MAX_PRECISION, DECIMAL_DEFAULT_SCALE, | ||
}; | ||
use crate::error::{ArrowError, Result}; | ||
use crate::util::decimal::{BasicDecimal, Decimal256}; | ||
|
@@ -271,13 +270,11 @@ impl Decimal128Array { | |
Decimal128Array::from(data) | ||
} | ||
|
||
/// Validates decimal values in this array can be properly interpreted | ||
/// with the specified precision. | ||
pub fn validate_decimal_precision(&self, precision: usize) -> Result<()> { | ||
if precision < self.precision { | ||
for v in self.iter().flatten() { | ||
validate_decimal_precision(v.as_i128(), precision)?; | ||
} | ||
// Validates decimal values in this array can be properly interpreted | ||
// with the specified precision. | ||
fn validate_decimal_precision(&self, precision: usize) -> Result<()> { | ||
for v in self.iter().flatten() { | ||
validate_decimal_precision(v.as_i128(), precision)?; | ||
} | ||
Ok(()) | ||
} | ||
|
@@ -317,7 +314,9 @@ impl Decimal128Array { | |
// Ensure that all values are within the requested | ||
// precision. For performance, only check if the precision is | ||
// decreased | ||
self.validate_decimal_precision(precision)?; | ||
if precision < self.precision { | ||
self.validate_decimal_precision(precision)?; | ||
} | ||
|
||
let data_type = Self::TYPE_CONSTRUCTOR(self.precision, self.scale); | ||
assert_eq!(self.data().data_type(), &data_type); | ||
|
@@ -330,15 +329,23 @@ impl Decimal128Array { | |
} | ||
|
||
impl Decimal256Array { | ||
/// Validates decimal values in this array can be properly interpreted | ||
/// with the specified precision. | ||
pub fn validate_decimal_precision(&self, precision: usize) -> Result<()> { | ||
if precision < self.precision { | ||
for v in self.iter().flatten() { | ||
validate_decimal256_precision(&v.to_big_int(), precision)?; | ||
// Validates decimal values in this array can be properly interpreted | ||
// with the specified precision. | ||
fn validate_decimal_precision(&self, precision: usize) -> Result<()> { | ||
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. Do we need this check if 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. good catch |
||
(0..self.len()).try_for_each(|idx| { | ||
if self.is_valid(idx) { | ||
let raw_val = unsafe { | ||
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. This could be extracted as a separate method 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.
|
||
let pos = self.value_offset(idx); | ||
std::slice::from_raw_parts( | ||
self.raw_value_data_ptr().offset(pos as isize), | ||
Self::VALUE_LENGTH as usize, | ||
) | ||
}; | ||
validate_decimal256_precision_with_lt_bytes(raw_val, precision) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
Ok(()) | ||
}) | ||
} | ||
|
||
/// Returns a Decimal array with the same data as self, with the | ||
|
@@ -376,7 +383,9 @@ impl Decimal256Array { | |
// Ensure that all values are within the requested | ||
// precision. For performance, only check if the precision is | ||
// decreased | ||
self.validate_decimal_precision(precision)?; | ||
if precision < self.precision { | ||
self.validate_decimal_precision(precision)?; | ||
} | ||
|
||
let data_type = Self::TYPE_CONSTRUCTOR(self.precision, self.scale); | ||
assert_eq!(self.data().data_type(), &data_type); | ||
|
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.
Personally, I prefer this function to be an independent
fn
, but not a method ofDecimal256Array
,because
precision
to its method is somewhat weird.Decimal256Array
, but not the generic decimal array. Moving it out theimpl Decimal256Array
can make the code cleaner.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.
If this is method is only for
Decimal256Array
, why we should move it out of theimpl
? I am confused about this suggestion.Besides, It's a private method.
If move it out of the
impl Decimal256Array
, how to get thedata
of theSelf
?with_precision_and_scale
also provide theprecision
andscale
, is this also weird?I feel confused about this reason. The method is only used to
Decimal256Array
, why not treat it as private function?