Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Use more idiomatic versions #898

Merged
merged 1 commit into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/buffer/immutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ impl<T: NativeType> Buffer<T> {
iterator.collect::<Vec<_>>().into()
}

/// # Safety
/// This method assumes that the iterator's size is correct and is undefined behavior
/// to use it on an iterator that reports an incorrect length.
/// Creates a [`Buffer`] from an fallible [`Iterator`] with a trusted length.
#[inline]
pub fn try_from_trusted_len_iter<E, I: TrustedLen<Item = std::result::Result<T, E>>>(
iterator: I,
Expand Down
60 changes: 33 additions & 27 deletions src/compute/arithmetics/decimal/add.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Defines the addition arithmetic kernels for [`PrimitiveArray`] representing decimals.
use crate::{
array::PrimitiveArray,
buffer::Buffer,
compute::{
arithmetics::{ArrayAdd, ArrayCheckedAdd, ArraySaturatingAdd},
arity::{binary, binary_checked},
Expand Down Expand Up @@ -188,17 +187,29 @@ pub fn adaptive_add(
) -> Result<PrimitiveArray<i128>> {
check_same_len(lhs, rhs)?;

if let (DataType::Decimal(lhs_p, lhs_s), DataType::Decimal(rhs_p, rhs_s)) =
(lhs.data_type(), rhs.data_type())
{
// The resulting precision is mutable because it could change while
// looping through the iterator
let (mut res_p, res_s, diff) = adjusted_precision_scale(*lhs_p, *lhs_s, *rhs_p, *rhs_s);

let shift = 10i128.pow(diff as u32);
let mut max = max_value(res_p);

let iter = lhs.values().iter().zip(rhs.values().iter()).map(|(l, r)| {
let (lhs_p, lhs_s, rhs_p, rhs_s) =
if let (DataType::Decimal(lhs_p, lhs_s), DataType::Decimal(rhs_p, rhs_s)) =
(lhs.data_type(), rhs.data_type())
{
(*lhs_p, *lhs_s, *rhs_p, *rhs_s)
} else {
return Err(ArrowError::InvalidArgumentError(
"Incorrect data type for the array".to_string(),
));
};

// The resulting precision is mutable because it could change while
// looping through the iterator
let (mut res_p, res_s, diff) = adjusted_precision_scale(lhs_p, lhs_s, rhs_p, rhs_s);

let shift = 10i128.pow(diff as u32);
let mut max = max_value(res_p);

let values = lhs
.values()
.iter()
.zip(rhs.values().iter())
.map(|(l, r)| {
// Based on the array's scales one of the arguments in the sum has to be shifted
// to the left to match the final scale
let res = if lhs_s > rhs_s {
Expand All @@ -220,19 +231,14 @@ pub fn adaptive_add(
max = max_value(res_p);
}
res
});
let values = Buffer::from_trusted_len_iter(iter);

let validity = combine_validities(lhs.validity(), rhs.validity());

Ok(PrimitiveArray::<i128>::new(
DataType::Decimal(res_p, res_s),
values,
validity,
))
} else {
Err(ArrowError::InvalidArgumentError(
"Incorrect data type for the array".to_string(),
))
}
})
.collect::<Vec<_>>();

let validity = combine_validities(lhs.validity(), rhs.validity());

Ok(PrimitiveArray::<i128>::new(
DataType::Decimal(res_p, res_s),
values.into(),
validity,
))
}
58 changes: 32 additions & 26 deletions src/compute/arithmetics/decimal/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::{
array::PrimitiveArray,
buffer::Buffer,
compute::{
arithmetics::{ArrayCheckedDiv, ArrayDiv},
arity::{binary, binary_checked, unary},
Expand Down Expand Up @@ -248,18 +247,30 @@ pub fn adaptive_div(
) -> Result<PrimitiveArray<i128>> {
check_same_len(lhs, rhs)?;

if let (DataType::Decimal(lhs_p, lhs_s), DataType::Decimal(rhs_p, rhs_s)) =
(lhs.data_type(), rhs.data_type())
{
// The resulting precision is mutable because it could change while
// looping through the iterator
let (mut res_p, res_s, diff) = adjusted_precision_scale(*lhs_p, *lhs_s, *rhs_p, *rhs_s);

let shift = 10i128.pow(diff as u32);
let shift_1 = 10i128.pow(res_s as u32);
let mut max = max_value(res_p);

let iter = lhs.values().iter().zip(rhs.values().iter()).map(|(l, r)| {
let (lhs_p, lhs_s, rhs_p, rhs_s) =
if let (DataType::Decimal(lhs_p, lhs_s), DataType::Decimal(rhs_p, rhs_s)) =
(lhs.data_type(), rhs.data_type())
{
(*lhs_p, *lhs_s, *rhs_p, *rhs_s)
} else {
return Err(ArrowError::InvalidArgumentError(
"Incorrect data type for the array".to_string(),
));
};

// The resulting precision is mutable because it could change while
// looping through the iterator
let (mut res_p, res_s, diff) = adjusted_precision_scale(lhs_p, lhs_s, rhs_p, rhs_s);

let shift = 10i128.pow(diff as u32);
let shift_1 = 10i128.pow(res_s as u32);
let mut max = max_value(res_p);

let values = lhs
.values()
.iter()
.zip(rhs.values().iter())
.map(|(l, r)| {
let numeral: i128 = l * shift_1;

// Based on the array's scales one of the arguments in the sum has to be shifted
Expand All @@ -285,19 +296,14 @@ pub fn adaptive_div(
}

res
});
let values = Buffer::from_trusted_len_iter(iter);
})
.collect::<Vec<_>>();

let validity = combine_validities(lhs.validity(), rhs.validity());
let validity = combine_validities(lhs.validity(), rhs.validity());

Ok(PrimitiveArray::<i128>::new(
DataType::Decimal(res_p, res_s),
values,
validity,
))
} else {
Err(ArrowError::InvalidArgumentError(
"Incorrect data type for the array".to_string(),
))
}
Ok(PrimitiveArray::<i128>::new(
DataType::Decimal(res_p, res_s),
values.into(),
validity,
))
}
58 changes: 32 additions & 26 deletions src/compute/arithmetics/decimal/mul.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::{
array::PrimitiveArray,
buffer::Buffer,
compute::{
arithmetics::{ArrayCheckedMul, ArrayMul, ArraySaturatingMul},
arity::{binary, binary_checked, unary},
Expand Down Expand Up @@ -260,18 +259,30 @@ pub fn adaptive_mul(
) -> Result<PrimitiveArray<i128>> {
check_same_len(lhs, rhs)?;

if let (DataType::Decimal(lhs_p, lhs_s), DataType::Decimal(rhs_p, rhs_s)) =
(lhs.data_type(), rhs.data_type())
{
// The resulting precision is mutable because it could change while
// looping through the iterator
let (mut res_p, res_s, diff) = adjusted_precision_scale(*lhs_p, *lhs_s, *rhs_p, *rhs_s);

let shift = 10i128.pow(diff as u32);
let shift_1 = 10i128.pow(res_s as u32);
let mut max = max_value(res_p);

let iter = lhs.values().iter().zip(rhs.values().iter()).map(|(l, r)| {
let (lhs_p, lhs_s, rhs_p, rhs_s) =
if let (DataType::Decimal(lhs_p, lhs_s), DataType::Decimal(rhs_p, rhs_s)) =
(lhs.data_type(), rhs.data_type())
{
(*lhs_p, *lhs_s, *rhs_p, *rhs_s)
} else {
return Err(ArrowError::InvalidArgumentError(
"Incorrect data type for the array".to_string(),
));
};

// The resulting precision is mutable because it could change while
// looping through the iterator
let (mut res_p, res_s, diff) = adjusted_precision_scale(lhs_p, lhs_s, rhs_p, rhs_s);

let shift = 10i128.pow(diff as u32);
let shift_1 = 10i128.pow(res_s as u32);
let mut max = max_value(res_p);

let values = lhs
.values()
.iter()
.zip(rhs.values().iter())
.map(|(l, r)| {
// Based on the array's scales one of the arguments in the sum has to be shifted
// to the left to match the final scale
let res = if lhs_s > rhs_s {
Expand All @@ -297,19 +308,14 @@ pub fn adaptive_mul(
}

res
});
let values = Buffer::from_trusted_len_iter(iter);
})
.collect::<Vec<_>>();

let validity = combine_validities(lhs.validity(), rhs.validity());
let validity = combine_validities(lhs.validity(), rhs.validity());

Ok(PrimitiveArray::<i128>::new(
DataType::Decimal(res_p, res_s),
values,
validity,
))
} else {
Err(ArrowError::InvalidArgumentError(
"Incorrect data type for the array".to_string(),
))
}
Ok(PrimitiveArray::<i128>::new(
DataType::Decimal(res_p, res_s),
values.into(),
validity,
))
}
60 changes: 33 additions & 27 deletions src/compute/arithmetics/decimal/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use crate::{
array::PrimitiveArray,
buffer::Buffer,
compute::{
arithmetics::{ArrayCheckedSub, ArraySaturatingSub, ArraySub},
arity::{binary, binary_checked},
Expand Down Expand Up @@ -187,17 +186,29 @@ pub fn adaptive_sub(
) -> Result<PrimitiveArray<i128>> {
check_same_len(lhs, rhs)?;

if let (DataType::Decimal(lhs_p, lhs_s), DataType::Decimal(rhs_p, rhs_s)) =
(lhs.data_type(), rhs.data_type())
{
// The resulting precision is mutable because it could change while
// looping through the iterator
let (mut res_p, res_s, diff) = adjusted_precision_scale(*lhs_p, *lhs_s, *rhs_p, *rhs_s);

let shift = 10i128.pow(diff as u32);
let mut max = max_value(res_p);

let iter = lhs.values().iter().zip(rhs.values().iter()).map(|(l, r)| {
let (lhs_p, lhs_s, rhs_p, rhs_s) =
if let (DataType::Decimal(lhs_p, lhs_s), DataType::Decimal(rhs_p, rhs_s)) =
(lhs.data_type(), rhs.data_type())
{
(*lhs_p, *lhs_s, *rhs_p, *rhs_s)
} else {
return Err(ArrowError::InvalidArgumentError(
"Incorrect data type for the array".to_string(),
));
};

// The resulting precision is mutable because it could change while
// looping through the iterator
let (mut res_p, res_s, diff) = adjusted_precision_scale(lhs_p, lhs_s, rhs_p, rhs_s);

let shift = 10i128.pow(diff as u32);
let mut max = max_value(res_p);

let values = lhs
.values()
.iter()
.zip(rhs.values().iter())
.map(|(l, r)| {
// Based on the array's scales one of the arguments in the sum has to be shifted
// to the left to match the final scale
let res: i128 = if lhs_s > rhs_s {
Expand All @@ -220,19 +231,14 @@ pub fn adaptive_sub(
}

res
});
let values = Buffer::from_trusted_len_iter(iter);

let validity = combine_validities(lhs.validity(), rhs.validity());

Ok(PrimitiveArray::<i128>::new(
DataType::Decimal(res_p, res_s),
values,
validity,
))
} else {
Err(ArrowError::InvalidArgumentError(
"Incorrect data type for the array".to_string(),
))
}
})
.collect::<Vec<_>>();

let validity = combine_validities(lhs.validity(), rhs.validity());

Ok(PrimitiveArray::<i128>::new(
DataType::Decimal(res_p, res_s),
values.into(),
validity,
))
}
Loading