Skip to content
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

Remove unnecessary binary decimal arithmetic kernels #4567

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 6 additions & 32 deletions datafusion/physical-expr/src/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,9 @@ use kernels::{
bitwise_xor, bitwise_xor_scalar,
};
use kernels_arrow::{
add_decimal, add_decimal_scalar, divide_decimal_scalar, divide_opt_decimal,
is_distinct_from, is_distinct_from_bool, is_distinct_from_decimal,
is_distinct_from_null, is_distinct_from_utf8, is_not_distinct_from,
is_not_distinct_from_bool, is_not_distinct_from_decimal, is_not_distinct_from_null,
is_not_distinct_from_utf8, modulus_decimal, modulus_decimal_scalar, multiply_decimal,
multiply_decimal_scalar, subtract_decimal, subtract_decimal_scalar,
is_distinct_from, is_distinct_from_bool, is_distinct_from_null,
is_distinct_from_utf8, is_not_distinct_from, is_not_distinct_from_bool,
is_not_distinct_from_null, is_not_distinct_from_utf8,
};

use arrow::datatypes::{DataType, Schema, TimeUnit};
Expand Down Expand Up @@ -131,29 +128,6 @@ macro_rules! compute_decimal_op_dyn_scalar {
}};
}

macro_rules! compute_decimal_op_scalar {
($LEFT:expr, $RIGHT:expr, $OP:ident, $DT:ident) => {{
let ll = as_decimal128_array($LEFT).unwrap();
if let ScalarValue::Decimal128(Some(_), _, _) = $RIGHT {
Ok(Arc::new(paste::expr! {[<$OP _decimal_scalar>]}(
ll,
$RIGHT.try_into()?,
)?))
} else {
// when the $RIGHT is a NULL, generate a NULL array of LEFT's datatype
Ok(Arc::new(new_null_array($LEFT.data_type(), $LEFT.len())))
}
}};
}

macro_rules! compute_decimal_op {
($LEFT:expr, $RIGHT:expr, $OP:ident, $DT:ident) => {{
let ll = $LEFT.as_any().downcast_ref::<$DT>().unwrap();
let rr = $RIGHT.as_any().downcast_ref::<$DT>().unwrap();
Ok(Arc::new(paste::expr! {[<$OP _decimal>]}(ll, rr)?))
}};
}

macro_rules! compute_null_op {
($LEFT:expr, $RIGHT:expr, $OP:ident, $DT:ident) => {{
let ll = $LEFT
Expand Down Expand Up @@ -371,7 +345,7 @@ macro_rules! binary_string_array_op {
macro_rules! binary_primitive_array_op {
($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
match $LEFT.data_type() {
DataType::Decimal128(_,_) => compute_decimal_op!($LEFT, $RIGHT, $OP, Decimal128Array),
DataType::Decimal128(_,_) => compute_op!($LEFT, $RIGHT, $OP, Decimal128Array),
DataType::Int8 => compute_op!($LEFT, $RIGHT, $OP, Int8Array),
DataType::Int16 => compute_op!($LEFT, $RIGHT, $OP, Int16Array),
DataType::Int32 => compute_op!($LEFT, $RIGHT, $OP, Int32Array),
Expand All @@ -396,7 +370,7 @@ macro_rules! binary_primitive_array_op {
macro_rules! binary_primitive_array_op_scalar {
($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
let result: Result<Arc<dyn Array>> = match $LEFT.data_type() {
DataType::Decimal128(_,_) => compute_decimal_op_scalar!($LEFT, $RIGHT, $OP, Decimal128Array),
DataType::Decimal128(_,_) => compute_op_scalar!($LEFT, $RIGHT, $OP, Decimal128Array),
DataType::Int8 => compute_op_scalar!($LEFT, $RIGHT, $OP, Int8Array),
DataType::Int16 => compute_op_scalar!($LEFT, $RIGHT, $OP, Int16Array),
DataType::Int32 => compute_op_scalar!($LEFT, $RIGHT, $OP, Int32Array),
Expand All @@ -423,7 +397,7 @@ macro_rules! binary_array_op {
($LEFT:expr, $RIGHT:expr, $OP:ident) => {{
match $LEFT.data_type() {
DataType::Null => compute_null_op!($LEFT, $RIGHT, $OP, NullArray),
DataType::Decimal128(_,_) => compute_decimal_op!($LEFT, $RIGHT, $OP, Decimal128Array),
DataType::Decimal128(_,_) => compute_op!($LEFT, $RIGHT, $OP, Decimal128Array),
DataType::Int8 => compute_op!($LEFT, $RIGHT, $OP, Int8Array),
DataType::Int16 => compute_op!($LEFT, $RIGHT, $OP, Int16Array),
DataType::Int32 => compute_op!($LEFT, $RIGHT, $OP, Int32Array),
Expand Down
238 changes: 23 additions & 215 deletions datafusion/physical-expr/src/expressions/binary/kernels_arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
//! This module contains computation kernels that are eventually
//! destined for arrow-rs but are in datafusion until they are ported.

use arrow::error::ArrowError;
use arrow::{array::*, datatypes::ArrowNumericType};
use datafusion_common::{DataFusionError, Result};
use datafusion_common::Result;

// Simple (low performance) kernels until optimized kernels are added to arrow
// See https://github.com/apache/arrow-rs/issues/960
Expand Down Expand Up @@ -118,200 +117,10 @@ pub(crate) fn is_not_distinct_from_utf8<OffsetSize: OffsetSizeTrait>(
.collect())
}

pub(crate) fn is_distinct_from_decimal(
left: &Decimal128Array,
right: &Decimal128Array,
) -> Result<BooleanArray> {
Ok(left
.iter()
.zip(right.iter())
.map(|(left, right)| match (left, right) {
(None, None) => Some(false),
(None, Some(_)) | (Some(_), None) => Some(true),
(Some(left), Some(right)) => Some(left != right),
})
.collect())
}

pub(crate) fn is_not_distinct_from_decimal(
left: &Decimal128Array,
right: &Decimal128Array,
) -> Result<BooleanArray> {
Ok(left
.iter()
.zip(right.iter())
.map(|(left, right)| match (left, right) {
(None, None) => Some(true),
(None, Some(_)) | (Some(_), None) => Some(false),
(Some(left), Some(right)) => Some(left == right),
})
.collect())
}

/// Creates an Decimal128Array the same size as `left`,
/// by applying `op` to all non-null elements of left and right
pub(crate) fn arith_decimal<F>(
left: &Decimal128Array,
right: &Decimal128Array,
op: F,
) -> Result<Decimal128Array>
where
F: Fn(i128, i128) -> Result<i128>,
{
left.iter()
.zip(right.iter())
.map(|(left, right)| {
if let (Some(left), Some(right)) = (left, right) {
Some(op(left, right)).transpose()
} else {
Ok(None)
}
})
.collect()
}

pub(crate) fn arith_decimal_scalar<F>(
left: &Decimal128Array,
right: i128,
op: F,
) -> Result<Decimal128Array>
where
F: Fn(i128, i128) -> Result<i128>,
{
left.iter()
.map(|left| {
if let Some(left) = left {
Some(op(left, right)).transpose()
} else {
Ok(None)
}
})
.collect()
}

pub(crate) fn add_decimal(
left: &Decimal128Array,
right: &Decimal128Array,
) -> Result<Decimal128Array> {
let array = arith_decimal(left, right, |left, right| Ok(left + right))?
.with_precision_and_scale(left.precision(), left.scale())?;
Ok(array)
}

pub(crate) fn add_decimal_scalar(
left: &Decimal128Array,
right: i128,
) -> Result<Decimal128Array> {
let array = arith_decimal_scalar(left, right, |left, right| Ok(left + right))?
.with_precision_and_scale(left.precision(), left.scale())?;
Ok(array)
}

pub(crate) fn subtract_decimal(
left: &Decimal128Array,
right: &Decimal128Array,
) -> Result<Decimal128Array> {
let array = arith_decimal(left, right, |left, right| Ok(left - right))?
.with_precision_and_scale(left.precision(), left.scale())?;
Ok(array)
}

pub(crate) fn subtract_decimal_scalar(
left: &Decimal128Array,
right: i128,
) -> Result<Decimal128Array> {
let array = arith_decimal_scalar(left, right, |left, right| Ok(left - right))?
.with_precision_and_scale(left.precision(), left.scale())?;
Ok(array)
}

pub(crate) fn multiply_decimal(
left: &Decimal128Array,
right: &Decimal128Array,
) -> Result<Decimal128Array> {
let divide = 10_i128.pow(left.scale() as u32);
let array = arith_decimal(left, right, |left, right| Ok(left * right / divide))?
.with_precision_and_scale(left.precision(), left.scale())?;
Ok(array)
}

pub(crate) fn multiply_decimal_scalar(
left: &Decimal128Array,
right: i128,
) -> Result<Decimal128Array> {
let divide = 10_i128.pow(left.scale() as u32);
let array =
arith_decimal_scalar(left, right, |left, right| Ok(left * right / divide))?
.with_precision_and_scale(left.precision(), left.scale())?;
Ok(array)
}

pub(crate) fn divide_opt_decimal(
left: &Decimal128Array,
right: &Decimal128Array,
) -> Result<Decimal128Array> {
let mul = 10_f64.powi(left.scale() as i32);
let array = arith_decimal(left, right, |left, right| {
if right == 0 {
return Err(DataFusionError::ArrowError(ArrowError::DivideByZero));
}
let l_value = left as f64;
let r_value = right as f64;
let result = ((l_value / r_value) * mul) as i128;
Ok(result)
})?
.with_precision_and_scale(left.precision(), left.scale())?;
Ok(array)
}

pub(crate) fn divide_decimal_scalar(
left: &Decimal128Array,
right: i128,
) -> Result<Decimal128Array> {
if right == 0 {
return Err(DataFusionError::ArrowError(ArrowError::DivideByZero));
}
let mul = 10_f64.powi(left.scale() as i32);
let array = arith_decimal_scalar(left, right, |left, right| {
let l_value = left as f64;
let r_value = right as f64;
let result = ((l_value / r_value) * mul) as i128;
Ok(result)
})?
.with_precision_and_scale(left.precision(), left.scale())?;
Ok(array)
}

pub(crate) fn modulus_decimal(
left: &Decimal128Array,
right: &Decimal128Array,
) -> Result<Decimal128Array> {
let array = arith_decimal(left, right, |left, right| {
if right == 0 {
Err(DataFusionError::ArrowError(ArrowError::DivideByZero))
} else {
Ok(left % right)
}
})?
.with_precision_and_scale(left.precision(), left.scale())?;
Ok(array)
}

pub(crate) fn modulus_decimal_scalar(
left: &Decimal128Array,
right: i128,
) -> Result<Decimal128Array> {
if right == 0 {
return Err(DataFusionError::ArrowError(ArrowError::DivideByZero));
}
let array = arith_decimal_scalar(left, right, |left, right| Ok(left % right))?
.with_precision_and_scale(left.precision(), left.scale())?;
Ok(array)
}

#[cfg(test)]
mod tests {
use super::*;
use arrow::compute::kernels::arithmetic::*;

fn create_decimal_array(
array: &[Option<i128>],
Expand Down Expand Up @@ -355,14 +164,13 @@ mod tests {
);

// is_distinct: left distinct right
let result = is_distinct_from_decimal(&left_decimal_array, &right_decimal_array)?;
let result = is_distinct_from(&left_decimal_array, &right_decimal_array)?;
assert_eq!(
BooleanArray::from(vec![Some(true), Some(true), Some(true), Some(false)]),
result
);
// is_distinct: left distinct right
let result =
is_not_distinct_from_decimal(&left_decimal_array, &right_decimal_array)?;
let result = is_not_distinct_from(&left_decimal_array, &right_decimal_array)?;
assert_eq!(
BooleanArray::from(vec![Some(false), Some(false), Some(false), Some(true)]),
result
Expand Down Expand Up @@ -394,27 +202,27 @@ mod tests {
3,
);
// add
let result = add_decimal(&left_decimal_array, &right_decimal_array)?;
let result = add(&left_decimal_array, &right_decimal_array)?;
let expect =
create_decimal_array(&[Some(246), None, Some(245), Some(247)], 25, 3);
assert_eq!(expect, result);
let result = add_decimal_scalar(&left_decimal_array, 10)?;
let result = add_scalar(&left_decimal_array, 10)?;
let expect =
create_decimal_array(&[Some(133), None, Some(132), Some(134)], 25, 3);
assert_eq!(expect, result);
// subtract
let result = subtract_decimal(&left_decimal_array, &right_decimal_array)?;
let result = subtract(&left_decimal_array, &right_decimal_array)?;
let expect = create_decimal_array(&[Some(0), None, Some(-1), Some(1)], 25, 3);
assert_eq!(expect, result);
let result = subtract_decimal_scalar(&left_decimal_array, 10)?;
let result = subtract_scalar(&left_decimal_array, 10)?;
let expect =
create_decimal_array(&[Some(113), None, Some(112), Some(114)], 25, 3);
assert_eq!(expect, result);
// multiply
let result = multiply_decimal(&left_decimal_array, &right_decimal_array)?;
let result = multiply(&left_decimal_array, &right_decimal_array)?;
let expect = create_decimal_array(&[Some(15), None, Some(15), Some(15)], 25, 3);
assert_eq!(expect, result);
let result = multiply_decimal_scalar(&left_decimal_array, 10)?;
let result = multiply_scalar(&left_decimal_array, 10)?;
let expect = create_decimal_array(&[Some(1), None, Some(1), Some(1)], 25, 3);
assert_eq!(expect, result);
// divide
Expand All @@ -434,14 +242,14 @@ mod tests {
25,
3,
);
let result = divide_opt_decimal(&left_decimal_array, &right_decimal_array)?;
let result = divide_opt(&left_decimal_array, &right_decimal_array)?;
let expect = create_decimal_array(
&[Some(123456700), None, Some(22446672), Some(-10037130), None],
25,
3,
);
assert_eq!(expect, result);
let result = divide_decimal_scalar(&left_decimal_array, 10)?;
let result = divide_scalar(&left_decimal_array, 10)?;
let expect = create_decimal_array(
&[
Some(123456700),
Expand All @@ -455,11 +263,11 @@ mod tests {
);
assert_eq!(expect, result);
// modulus
let result = modulus_decimal(&left_decimal_array, &right_decimal_array)?;
let result = modulus(&left_decimal_array, &right_decimal_array)?;
let expect =
create_decimal_array(&[Some(7), None, Some(37), Some(16), None], 25, 3);
assert_eq!(expect, result);
let result = modulus_decimal_scalar(&left_decimal_array, 10)?;
let result = modulus_scalar(&left_decimal_array, 10)?;
let expect =
create_decimal_array(&[Some(7), None, Some(7), Some(7), Some(7)], 25, 3);
assert_eq!(expect, result);
Expand All @@ -472,14 +280,14 @@ mod tests {
let left_decimal_array = create_decimal_array(&[Some(101)], 10, 1);
let right_decimal_array = create_decimal_array(&[Some(0)], 1, 1);

let err =
divide_opt_decimal(&left_decimal_array, &right_decimal_array).unwrap_err();
assert_eq!("Arrow error: Divide by zero error", err.to_string());
let err = divide_decimal_scalar(&left_decimal_array, 0).unwrap_err();
assert_eq!("Arrow error: Divide by zero error", err.to_string());
let err = modulus_decimal(&left_decimal_array, &right_decimal_array).unwrap_err();
assert_eq!("Arrow error: Divide by zero error", err.to_string());
let err = modulus_decimal_scalar(&left_decimal_array, 0).unwrap_err();
assert_eq!("Arrow error: Divide by zero error", err.to_string());
let array = divide_opt(&left_decimal_array, &right_decimal_array).unwrap();
assert_eq!(array.len(), 1);
assert_eq!(array.null_count(), 1);
let err = divide_scalar(&left_decimal_array, 0).unwrap_err();
assert_eq!("Divide by zero error", err.to_string());
let err = modulus(&left_decimal_array, &right_decimal_array).unwrap_err();
assert_eq!("Divide by zero error", err.to_string());
let err = modulus_scalar(&left_decimal_array, 0).unwrap_err();
assert_eq!("Divide by zero error", err.to_string());
}
}