liukun4515 commented on a change in pull request #1408:
URL: https://github.com/apache/arrow-datafusion/pull/1408#discussion_r771153639
##########
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:
```
let max_precision = p1.max(p2);
if s1.eq(s2) {
// s1 = s2
sum_decimal(v1, v2, max_precision, s1)
} else if s1.gt(s2) {
// s1 > s2
sum_decimal_with_diff_scale(v1, v2, max_precision, s1, s2)
} else if s1.lt(s2) {
// s1 < s2
sum_decimal_with_diff_scale(v2, v1, max_precision, s2, s1)
} else {
return Err(DataFusionError::Internal(format!(
"Sum is not expected to receive lhs {:?}, rhs {:?}",
lhs, rhs
)));
}
```
--
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]