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

[GitHub] [arrow-datafusion] Syleechan commented on a diff in pull request #6942: functions: support trunc() function with one or two args

Syleechan commented on code in PR #6942:
URL: https://github.com/apache/arrow-datafusion/pull/6942#discussion_r1266488160


##########
datafusion/physical-expr/src/math_expressions.rs:
##########
@@ -497,6 +497,91 @@ pub fn log(args: &[ArrayRef]) -> Result<ArrayRef> {
     }
 }
 
+/// Truncate(numeric, decimalPrecision) and trunc(numeric) SQL function
+pub fn trunc(args: &[ArrayRef]) -> Result<ArrayRef> {
+    if args.len() != 1 && args.len() != 2 {
+        return Err(DataFusionError::Internal(format!(
+            "truncate function requires one or two arguments, got {}",
+            args.len()
+        )));
+    }
+
+    //if only one arg then invoke toolchain trunc(num) and precision = 0 by default
+    //or then invoke the compute_truncate method to process precision
+    let mut _precision = ColumnarValue::Scalar(Int32(Some(0)));
+
+    let num = &args[0];
+    if args.len() == 2 {
+        _precision = ColumnarValue::Array(args[1].clone());
+    }
+
+    match args[0].data_type() {
+        DataType::Float64 => match _precision {
+            ColumnarValue::Scalar(Int32(Some(_precision))) => Ok(Arc::new(
+                make_function_scalar_inputs!(num, "num", Float64Array, { f64::trunc }),
+            )
+                as ArrayRef),
+            ColumnarValue::Array(_precision) => Ok(Arc::new(make_function_inputs2!(
+                num,
+                _precision,
+                "x",
+                "y",
+                Float64Array,
+                Int32Array,
+                { compute_truncate64 }
+            )) as ArrayRef),
+            _ => Err(DataFusionError::Internal(
+                "trunc function requires a scalar or array for precision".to_string(),
+            )),
+        },
+        DataType::Float32 => match _precision {
+            ColumnarValue::Scalar(Int32(Some(_precision))) => Ok(Arc::new(
+                make_function_scalar_inputs!(num, "num", Float32Array, { f32::trunc }),
+            )
+                as ArrayRef),
+            ColumnarValue::Array(_precision) => Ok(Arc::new(make_function_inputs2!(
+                num,
+                _precision,
+                "x",
+                "y",
+                Float32Array,
+                Int32Array,
+                { compute_truncate32 }
+            )) as ArrayRef),
+            _ => Err(DataFusionError::Internal(
+                "trunc function requires a scalar or array for precision".to_string(),
+            )),
+        },
+        other => Err(DataFusionError::Internal(format!(
+            "Unsupported data type {other:?} for function trunc"
+        ))),
+    }
+}
+
+fn compute_truncate32(x: f32, y: usize) -> f32 {
+    let s = format!("{:.precision$}", x, precision = y);

Review Comment:
   thanks



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