Skip to content

Commit

Permalink
Use divide_opt` kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
alamb committed Sep 15, 2022
1 parent a19f0ad commit 0493aa1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
43 changes: 19 additions & 24 deletions datafusion-cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions datafusion/physical-expr/src/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::{any::Any, sync::Arc};

use arrow::array::*;
use arrow::compute::kernels::arithmetic::{
add, add_scalar, divide, divide_scalar, modulus, modulus_scalar, multiply,
add, add_scalar, divide_opt, divide_scalar, modulus, modulus_scalar, multiply,
multiply_scalar, subtract, subtract_scalar,
};
use arrow::compute::kernels::boolean::{and_kleene, not, or_kleene};
Expand Down Expand Up @@ -60,7 +60,7 @@ use kernels::{
bitwise_xor, bitwise_xor_scalar,
};
use kernels_arrow::{
add_decimal, add_decimal_scalar, divide_decimal, divide_decimal_scalar,
add_decimal, add_decimal_scalar, divide_decimal_scalar, divide_opt_decimal,
eq_decimal_scalar, gt_decimal_scalar, gt_eq_decimal_scalar, 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,
Expand Down Expand Up @@ -844,7 +844,7 @@ impl BinaryExpr {
Operator::Plus => binary_primitive_array_op!(left, right, add),
Operator::Minus => binary_primitive_array_op!(left, right, subtract),
Operator::Multiply => binary_primitive_array_op!(left, right, multiply),
Operator::Divide => binary_primitive_array_op!(left, right, divide),
Operator::Divide => binary_primitive_array_op!(left, right, divide_opt),
Operator::Modulo => binary_primitive_array_op!(left, right, modulus),
Operator::And => {
if left_data_type == &DataType::Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,18 @@ pub(crate) fn multiply_decimal_scalar(
Ok(array)
}

pub(crate) fn divide_decimal(
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| {
let l_value = left as f64;
let r_value = right as f64;
// TODO: Since this uses f64 division, divide by zero and then casting to i128
// gives a nasty asnwer:
// 170141183460469231731687303715884105727
//https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b5949eb324d9828a802aa11b4fa9d029
let result = ((l_value / r_value) * mul) as i128;
Ok(result)
})?
Expand Down Expand Up @@ -621,7 +625,7 @@ mod tests {
);
let right_decimal_array =
create_decimal_array(&[Some(10), Some(100), Some(55), Some(-123)], 25, 3);
let result = divide_decimal(&left_decimal_array, &right_decimal_array)?;
let result = divide_decimal_opt(&left_decimal_array, &right_decimal_array)?;
let expect = create_decimal_array(
&[Some(123456700), None, Some(22446672), Some(-10037130)],
25,
Expand Down

0 comments on commit 0493aa1

Please sign in to comment.