|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::math_funcs::utils::get_precision_scale; |
| 19 | +use arrow::{ |
| 20 | + array::{ArrayRef, AsArray}, |
| 21 | + datatypes::Decimal128Type, |
| 22 | +}; |
| 23 | +use arrow_array::{Array, Decimal128Array}; |
| 24 | +use arrow_schema::{DataType, DECIMAL128_MAX_PRECISION}; |
| 25 | +use datafusion::physical_plan::ColumnarValue; |
| 26 | +use datafusion_common::DataFusionError; |
| 27 | +use num::{BigInt, Signed, ToPrimitive}; |
| 28 | +use std::sync::Arc; |
| 29 | + |
| 30 | +// Let Decimal(p3, s3) as return type i.e. Decimal(p1, s1) / Decimal(p2, s2) = Decimal(p3, s3). |
| 31 | +// Conversely, Decimal(p1, s1) = Decimal(p2, s2) * Decimal(p3, s3). This means that, in order to |
| 32 | +// get enough scale that matches with Spark behavior, it requires to widen s1 to s2 + s3 + 1. Since |
| 33 | +// both s2 and s3 are 38 at max., s1 is 77 at max. DataFusion division cannot handle such scale > |
| 34 | +// Decimal256Type::MAX_SCALE. Therefore, we need to implement this decimal division using BigInt. |
| 35 | +pub fn spark_decimal_div( |
| 36 | + args: &[ColumnarValue], |
| 37 | + data_type: &DataType, |
| 38 | +) -> Result<ColumnarValue, DataFusionError> { |
| 39 | + let left = &args[0]; |
| 40 | + let right = &args[1]; |
| 41 | + let (p3, s3) = get_precision_scale(data_type); |
| 42 | + |
| 43 | + let (left, right): (ArrayRef, ArrayRef) = match (left, right) { |
| 44 | + (ColumnarValue::Array(l), ColumnarValue::Array(r)) => (Arc::clone(l), Arc::clone(r)), |
| 45 | + (ColumnarValue::Scalar(l), ColumnarValue::Array(r)) => { |
| 46 | + (l.to_array_of_size(r.len())?, Arc::clone(r)) |
| 47 | + } |
| 48 | + (ColumnarValue::Array(l), ColumnarValue::Scalar(r)) => { |
| 49 | + (Arc::clone(l), r.to_array_of_size(l.len())?) |
| 50 | + } |
| 51 | + (ColumnarValue::Scalar(l), ColumnarValue::Scalar(r)) => (l.to_array()?, r.to_array()?), |
| 52 | + }; |
| 53 | + let left = left.as_primitive::<Decimal128Type>(); |
| 54 | + let right = right.as_primitive::<Decimal128Type>(); |
| 55 | + let (p1, s1) = get_precision_scale(left.data_type()); |
| 56 | + let (p2, s2) = get_precision_scale(right.data_type()); |
| 57 | + |
| 58 | + let l_exp = ((s2 + s3 + 1) as u32).saturating_sub(s1 as u32); |
| 59 | + let r_exp = (s1 as u32).saturating_sub((s2 + s3 + 1) as u32); |
| 60 | + let result: Decimal128Array = if p1 as u32 + l_exp > DECIMAL128_MAX_PRECISION as u32 |
| 61 | + || p2 as u32 + r_exp > DECIMAL128_MAX_PRECISION as u32 |
| 62 | + { |
| 63 | + let ten = BigInt::from(10); |
| 64 | + let l_mul = ten.pow(l_exp); |
| 65 | + let r_mul = ten.pow(r_exp); |
| 66 | + let five = BigInt::from(5); |
| 67 | + let zero = BigInt::from(0); |
| 68 | + arrow::compute::kernels::arity::binary(left, right, |l, r| { |
| 69 | + let l = BigInt::from(l) * &l_mul; |
| 70 | + let r = BigInt::from(r) * &r_mul; |
| 71 | + let div = if r.eq(&zero) { zero.clone() } else { &l / &r }; |
| 72 | + let res = if div.is_negative() { |
| 73 | + div - &five |
| 74 | + } else { |
| 75 | + div + &five |
| 76 | + } / &ten; |
| 77 | + res.to_i128().unwrap_or(i128::MAX) |
| 78 | + })? |
| 79 | + } else { |
| 80 | + let l_mul = 10_i128.pow(l_exp); |
| 81 | + let r_mul = 10_i128.pow(r_exp); |
| 82 | + arrow::compute::kernels::arity::binary(left, right, |l, r| { |
| 83 | + let l = l * l_mul; |
| 84 | + let r = r * r_mul; |
| 85 | + let div = if r == 0 { 0 } else { l / r }; |
| 86 | + let res = if div.is_negative() { div - 5 } else { div + 5 } / 10; |
| 87 | + res.to_i128().unwrap_or(i128::MAX) |
| 88 | + })? |
| 89 | + }; |
| 90 | + let result = result.with_data_type(DataType::Decimal128(p3, s3)); |
| 91 | + Ok(ColumnarValue::Array(Arc::new(result))) |
| 92 | +} |
0 commit comments