martin-g commented on code in PR #18979:
URL: https://github.com/apache/datafusion/pull/18979#discussion_r2573695209


##########
datafusion/functions/src/math/decimal.rs:
##########
@@ -0,0 +1,127 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::ops::Rem;
+use std::sync::Arc;
+
+use arrow::array::{ArrayRef, AsArray, PrimitiveArray};
+use arrow::datatypes::{ArrowNativeTypeOp, DecimalType};
+use arrow::error::ArrowError;
+use arrow_buffer::ArrowNativeType;
+use datafusion_common::{DataFusionError, Result};
+
+pub(super) fn apply_decimal_op<T, F>(
+    array: &ArrayRef,
+    scale: i8,
+    fn_name: &str,
+    op: F,
+) -> Result<ArrayRef>
+where
+    T: DecimalType,
+    T::Native: ArrowNativeType + ArrowNativeTypeOp,
+    F: Fn(T::Native, T::Native) -> std::result::Result<T::Native, ArrowError>,
+{
+    if scale <= 0 {
+        return Ok(Arc::clone(array));
+    }
+
+    let factor = decimal_scale_factor::<T>(scale, fn_name)?;
+    let decimal = array.as_primitive::<T>();
+    let data_type = array.data_type().clone();
+
+    let result: PrimitiveArray<T> = decimal
+        .try_unary(|value| op(value, factor))?
+        .with_data_type(data_type);
+
+    Ok(Arc::new(result))
+}
+
+fn decimal_scale_factor<T>(scale: i8, fn_name: &str) -> Result<T::Native>
+where
+    T: DecimalType,
+    T::Native: ArrowNativeType + ArrowNativeTypeOp,
+{
+    let base = <T::Native as ArrowNativeType>::from_usize(10).ok_or_else(|| {
+        DataFusionError::Execution(format!(
+            "Decimal scale {scale} is too large for {fn_name}"

Review Comment:
   Here you try to create i32/i64/i128/i256 from `10_usize`, right ?
   It will always succeed, so you can use just `.ok()`.
   
   But if you prefer to raise an error you could use something like:
   ```
   Err(err) => DataFusionError::Execution(format!("Cannot get 10_{} from usize: 
{err:?}", std::any::type_name::<T::Native as ArrowNativeType>()))
   ```



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to