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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]