You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/04/15 15:47:01 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #2241: fix: find the right wider decimal datatype for comparison operation

alamb commented on code in PR #2241:
URL: https://github.com/apache/arrow-datafusion/pull/2241#discussion_r851340950


##########
datafusion/physical-expr/src/coercion_rule/binary_rule.rs:
##########
@@ -178,15 +178,30 @@ fn get_comparison_common_decimal_type(
         }
     };
     match (decimal_type, &other_decimal_type) {
-        (DataType::Decimal(p1, s1), DataType::Decimal(p2, s2)) => {
-            let new_precision = p1.max(p2);
-            let new_scale = s1.max(s2);
-            Some(DataType::Decimal(*new_precision, *new_scale))
+        (d1 @ DataType::Decimal(_, _), d2 @ DataType::Decimal(_, _)) => {
+            get_wider_decimal_type(d1, d2)
         }
         _ => None,
     }
 }
 
+// Find the winder decimal type when both types are decimal.

Review Comment:
   ```suggestion
   // Returns a `DataType::Decimal` that can store any value from either 
   // `lhs_decimal_type` and `rhs_decimal_type`
   ```



##########
datafusion/physical-expr/src/coercion_rule/binary_rule.rs:
##########
@@ -178,15 +178,30 @@ fn get_comparison_common_decimal_type(
         }
     };
     match (decimal_type, &other_decimal_type) {
-        (DataType::Decimal(p1, s1), DataType::Decimal(p2, s2)) => {
-            let new_precision = p1.max(p2);
-            let new_scale = s1.max(s2);
-            Some(DataType::Decimal(*new_precision, *new_scale))
+        (d1 @ DataType::Decimal(_, _), d2 @ DataType::Decimal(_, _)) => {
+            get_wider_decimal_type(d1, d2)
         }
         _ => None,
     }
 }
 
+// Find the winder decimal type when both types are decimal.
+// The result decimal type is (max(s1, s2) + max(p1-s1, p2-s2), max(s1, s2)).
+fn get_wider_decimal_type(
+    lhs_decimal_type: &DataType,
+    rhs_type: &DataType,
+) -> Option<DataType> {
+    match (lhs_decimal_type, rhs_type) {
+        (DataType::Decimal(p1, s1), DataType::Decimal(p2, s2)) => {
+            // max(s1, s2) + max(p1-s1, p2-s2), max(s1, s2)
+            let s = *s1.max(s2);
+            let range = (p1 - s1).max(p2 - s2);

Review Comment:
   👍  I see that the precision includes the scale 👍 



##########
datafusion/physical-expr/src/coercion_rule/binary_rule.rs:
##########
@@ -557,15 +571,17 @@ mod tests {
             DataType::Float32,
             DataType::Float64,
             DataType::Decimal(38, 10),
+            DataType::Decimal(20, 8),
         ];
         let result_types = [
             DataType::Decimal(20, 3),
             DataType::Decimal(20, 3),
             DataType::Decimal(20, 3),
-            DataType::Decimal(20, 3),
-            DataType::Decimal(20, 7),
-            DataType::Decimal(30, 15),
+            DataType::Decimal(23, 3),
+            DataType::Decimal(24, 7),
+            DataType::Decimal(32, 15),
             DataType::Decimal(38, 10),
+            DataType::Decimal(25, 8),

Review Comment:
   👍 



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org