liukun4515 commented on a change in pull request #1408:
URL: https://github.com/apache/arrow-datafusion/pull/1408#discussion_r771152882
##########
File path: datafusion/src/physical_plan/expressions/sum.rs
##########
@@ -187,8 +222,63 @@ macro_rules! typed_sum {
}};
}
+// TODO implement this in arrow-rs with simd
+// https://github.com/apache/arrow-rs/issues/1010
+fn sum_decimal(
+ lhs: &Option<i128>,
+ rhs: &Option<i128>,
+ precision: &usize,
+ scale: &usize,
+) -> ScalarValue {
+ match (lhs, rhs) {
+ (None, None) => ScalarValue::Decimal128(None, *precision, *scale),
+ (None, rhs) => ScalarValue::Decimal128(*rhs, *precision, *scale),
+ (lhs, None) => ScalarValue::Decimal128(*lhs, *precision, *scale),
+ (lhs, rhs) => {
+ ScalarValue::Decimal128(Some(lhs.unwrap() + rhs.unwrap()),
*precision, *scale)
+ }
+ }
+}
+
+fn sum_decimal_with_diff_scale(
+ lhs: &Option<i128>,
+ rhs: &Option<i128>,
+ precision: &usize,
+ lhs_scale: &usize,
+ rhs_scale: &usize,
+) -> ScalarValue {
+ // the lhs_scale must be greater or equal rhs_scale.
+ match (lhs, rhs) {
+ (None, None) => ScalarValue::Decimal128(None, *precision, *lhs_scale),
+ (None, rhs) => {
+ let new_value = rhs.unwrap() * 10_i128.pow((lhs_scale - rhs_scale)
as u32);
+ ScalarValue::Decimal128(Some(new_value), *precision, *lhs_scale)
+ }
+ (lhs, None) => ScalarValue::Decimal128(*lhs, *precision, *lhs_scale),
+ (lhs, rhs) => {
+ let new_value =
+ rhs.unwrap() * 10_i128.pow((lhs_scale - rhs_scale) as u32) +
lhs.unwrap();
+ ScalarValue::Decimal128(Some(new_value), *precision, *lhs_scale)
+ }
+ }
+}
+
pub(super) fn sum(lhs: &ScalarValue, rhs: &ScalarValue) -> Result<ScalarValue>
{
Ok(match (lhs, rhs) {
+ (ScalarValue::Decimal128(v1, p1, s1), ScalarValue::Decimal128(v2, p2,
s2)) => {
+ if s1.eq(s2) {
+ sum_decimal(v1, v2, p1, s1)
+ } else if s1.gt(s2) && p1.ge(p2) {
Review comment:
This is also ugly and confusing to me.
I have refined this part.
For the input args, the left decimal value and right decimal value, there is
two cases:
1. scale is same: just find the max precision as the result precision
2. scale is diff: select the max(scale1,scale2) as the result scale and max
precision as the result precision.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]