comphead commented on code in PR #19575:
URL: https://github.com/apache/datafusion/pull/19575#discussion_r2656670276


##########
datafusion/functions/src/math/factorial.rs:
##########
@@ -92,50 +92,79 @@ impl ScalarUDFImpl for FactorialFunc {
     }
 }
 
+const FACTORIALS: [i64; 21] = [
+    1,
+    1,
+    2,
+    6,
+    24,
+    120,
+    720,
+    5040,
+    40320,
+    362880,
+    3628800,
+    39916800,
+    479001600,
+    6227020800,
+    87178291200,
+    1307674368000,
+    20922789888000,
+    355687428096000,
+    6402373705728000,
+    121645100408832000,
+    2432902008176640000,
+];
+
 /// Factorial SQL function
 fn factorial(args: &[ArrayRef]) -> Result<ArrayRef> {
     match args[0].data_type() {
         Int64 => {
-            let arg = downcast_named_arg!((&args[0]), "value", Int64Array);
-            Ok(arg
-                .iter()
-                .map(|a| match a {
-                    Some(a) => (2..=a)
-                        .try_fold(1i64, i64::checked_mul)
-                        .ok_or_else(|| {
-                            
arrow_datafusion_err!(ArrowError::ComputeError(format!(
-                                "Overflow happened on FACTORIAL({a})"
-                            )))
-                        })
-                        .map(Some),
-                    _ => Ok(None),
-                })
-                .collect::<Result<Int64Array>>()
-                .map(Arc::new)? as ArrayRef)
+            let result: Int64Array =
+                args[0].as_primitive::<Int64Type>().try_unary(|a| {
+                    if a < 0 {
+                        Ok(1)
+                    } else if a < FACTORIALS.len() as i64 {
+                        Ok(FACTORIALS[a as usize])
+                    } else {
+                        Err(ArrowError::ComputeError(format!(

Review Comment:
   it should prob to be `exec_err!`



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