You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "viirya (via GitHub)" <gi...@apache.org> on 2023/04/26 07:33:16 UTC

[GitHub] [arrow-rs] viirya opened a new pull request, #4136: Support fixed point multiplication for DictionaryArray of Decimals

viirya opened a new pull request, #4136:
URL: https://github.com/apache/arrow-rs/pull/4136

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #4135.
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


-- 
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


[GitHub] [arrow-rs] alamb commented on a diff in pull request #4136: Support fixed point multiplication for DictionaryArray of Decimals

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #4136:
URL: https://github.com/apache/arrow-rs/pull/4136#discussion_r1179307327


##########
arrow-arith/src/arithmetic.rs:
##########
@@ -1434,6 +1434,100 @@ pub fn multiply_dyn_checked(
     }
 }
 
+#[cfg(feature = "dyn_arith_dict")]
+fn get_precision_scale(dt: &DataType) -> Result<(u8, i8), ArrowError> {
+    match dt {
+        DataType::Decimal128(precision, scale) => Ok((*precision, *scale)),
+        _ => Err(ArrowError::ComputeError(
+            "Cannot get precision and scale from non-decimal type".to_string(),
+        )),
+    }
+}
+
+#[cfg(feature = "dyn_arith_dict")]
+/// Perform `left * right` operation on two decimal arrays. If either left or right value is
+/// null then the result is also null.
+///
+/// This performs decimal multiplication which allows precision loss if an exact representation
+/// is not possible for the result, according to the required scale. In the case, the result
+/// will be rounded to the required scale.
+///
+/// If the required scale is greater than the product scale, an error is returned.
+///
+/// This doesn't detect overflow. Once overflowing, the result will wrap around.
+///
+/// It is implemented for compatibility with precision loss `multiply` function provided by
+/// other data processing engines. For multiplication with precision loss detection, use
+/// `multiply_dyn` or `multiply_dyn_checked` instead.
+pub fn multiply_fixed_point_dyn(
+    left: &dyn Array,
+    right: &dyn Array,
+    required_scale: i8,
+) -> Result<ArrayRef, ArrowError> {
+    match (left.data_type(), right.data_type()) {
+        (
+            DataType::Dictionary(_, lhs_value_type),
+            DataType::Dictionary(_, rhs_value_type),
+        ) if matches!(lhs_value_type.as_ref(), &DataType::Decimal128(_, _))
+            && matches!(rhs_value_type.as_ref(), &DataType::Decimal128(_, _)) =>
+        {
+            downcast_dictionary_array!(
+                left => match left.values().data_type() {
+                    DataType::Decimal128(_, _) => {
+                        let lhs_precision_scale = get_precision_scale(lhs_value_type.as_ref())?;

Review Comment:
   Would it be possible to refactor the common precision, scale and divisor calculation into  a function to avoid the replication with this code:
   
   https://github.com/apache/arrow-rs/blob/9fa8125fbe14a3a85b4995617945bda51ee3b055/arrow-arith/src/arithmetic.rs#L1508-L1528
   
   That would make it easier to see and ensure the new code was covered



-- 
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


[GitHub] [arrow-rs] alamb commented on a diff in pull request #4136: Support fixed point multiplication for DictionaryArray of Decimals

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #4136:
URL: https://github.com/apache/arrow-rs/pull/4136#discussion_r1180468465


##########
arrow-arith/src/arithmetic.rs:
##########
@@ -1451,27 +1559,17 @@ pub fn multiply_fixed_point_checked(
     right: &PrimitiveArray<Decimal128Type>,
     required_scale: i8,
 ) -> Result<PrimitiveArray<Decimal128Type>, ArrowError> {
-    let product_scale = left.scale() + right.scale();
-    let precision = min(
-        left.precision() + right.precision() + 1,
-        DECIMAL128_MAX_PRECISION,
-    );
+    let (precision, product_scale, divisor) = get_fixed_point_info(

Review Comment:
   Than you for the refactoring -- while the overall volume of code removed is relatively low, I think the new structure makes the connection clearer.



-- 
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


[GitHub] [arrow-rs] viirya commented on a diff in pull request #4136: Support fixed point multiplication for DictionaryArray of Decimals

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya commented on code in PR #4136:
URL: https://github.com/apache/arrow-rs/pull/4136#discussion_r1180064607


##########
arrow-arith/src/arithmetic.rs:
##########
@@ -1434,6 +1434,100 @@ pub fn multiply_dyn_checked(
     }
 }
 
+#[cfg(feature = "dyn_arith_dict")]
+fn get_precision_scale(dt: &DataType) -> Result<(u8, i8), ArrowError> {
+    match dt {
+        DataType::Decimal128(precision, scale) => Ok((*precision, *scale)),
+        _ => Err(ArrowError::ComputeError(
+            "Cannot get precision and scale from non-decimal type".to_string(),
+        )),
+    }
+}
+
+#[cfg(feature = "dyn_arith_dict")]
+/// Perform `left * right` operation on two decimal arrays. If either left or right value is
+/// null then the result is also null.
+///
+/// This performs decimal multiplication which allows precision loss if an exact representation
+/// is not possible for the result, according to the required scale. In the case, the result
+/// will be rounded to the required scale.
+///
+/// If the required scale is greater than the product scale, an error is returned.
+///
+/// This doesn't detect overflow. Once overflowing, the result will wrap around.
+///
+/// It is implemented for compatibility with precision loss `multiply` function provided by
+/// other data processing engines. For multiplication with precision loss detection, use
+/// `multiply_dyn` or `multiply_dyn_checked` instead.
+pub fn multiply_fixed_point_dyn(
+    left: &dyn Array,
+    right: &dyn Array,
+    required_scale: i8,
+) -> Result<ArrayRef, ArrowError> {
+    match (left.data_type(), right.data_type()) {
+        (
+            DataType::Dictionary(_, lhs_value_type),
+            DataType::Dictionary(_, rhs_value_type),
+        ) if matches!(lhs_value_type.as_ref(), &DataType::Decimal128(_, _))
+            && matches!(rhs_value_type.as_ref(), &DataType::Decimal128(_, _)) =>
+        {
+            downcast_dictionary_array!(
+                left => match left.values().data_type() {
+                    DataType::Decimal128(_, _) => {
+                        let lhs_precision_scale = get_precision_scale(lhs_value_type.as_ref())?;

Review Comment:
   Added a function for that.



-- 
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


[GitHub] [arrow-rs] viirya merged pull request #4136: Support fixed point multiplication for DictionaryArray of Decimals

Posted by "viirya (via GitHub)" <gi...@apache.org>.
viirya merged PR #4136:
URL: https://github.com/apache/arrow-rs/pull/4136


-- 
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