This is an automated email from the ASF dual-hosted git repository.

github-merge-queue[bot] pushed a commit to branch 
gh-readonly-queue/main/pr-22278-43f2170ad63d85c79a8029dfdba8bad3c195e5cb
in repository https://gitbox.apache.org/repos/asf/datafusion.git

commit 541e882884b1f836c40eed405de2559602ddec20
Author: Raushan Prabhakar <[email protected]>
AuthorDate: Mon May 18 21:07:31 2026 +0530

    fixing factorial negative values (#22278)
    
    ## Which issue does this PR close?
    
    - Closes #22270
    
    ## Rationale for this change
    
    PostgreSQL returns a domain error for `factorial` on negative inputs
    (`ERROR: factorial of a negative number is undefined`). DataFusion
    incorrectly returned `1` for negative values (e.g. `factorial(-1)`),
    which breaks PostgreSQL-aligned SQL semantics and can silently produce
    wrong results.
    
    ## What changes are included in this PR?
    
    - **`factorial` (core / `datafusion-functions`):** Negative arguments
    now fail with an execution error and message aligned with PostgreSQL:
    `factorial of a negative number is undefined`, instead of returning `1`.
    - **Documentation:** Updated the `#[user_doc]` description so it matches
    behavior (non-negative inputs; error on negative values and on
    overflow).
    - **Tests:** Added a sqllogictest in `math.slt` that expects this error
    for `factorial(-1)`.
    
    **Note:** Spark’s `factorial` in `datafusion-spark` is unchanged
    (PySpark-style nulls for out-of-domain values).
    
    ## Are these changes tested?
    
    Yes. A sqllogictest was added in
    `datafusion/sqllogictest/test_files/math.slt` for `SELECT
    factorial(-1)`.
    
    ## Are there any user-facing changes?
    
    Yes. `factorial` with a negative argument now errors at execution time
    instead of returning `1`. This matches PostgreSQL and fixes incorrect
    results; callers that relied on the old behavior would need to adjust.
    
    No public Rust API breakage in exported types; behavior change is
    SQL/UDF semantics for negative inputs.
    
    ---------
    
    Co-authored-by: Raushan Prabhakar <[email protected]>
---
 datafusion/functions/src/math/factorial.rs     | 4 ++--
 datafusion/sqllogictest/test_files/math.slt    | 4 ++++
 docs/source/user-guide/sql/scalar_functions.md | 2 +-
 3 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/datafusion/functions/src/math/factorial.rs 
b/datafusion/functions/src/math/factorial.rs
index b4c41838e1..3b4f973f19 100644
--- a/datafusion/functions/src/math/factorial.rs
+++ b/datafusion/functions/src/math/factorial.rs
@@ -32,7 +32,7 @@ use datafusion_macros::user_doc;
 
 #[user_doc(
     doc_section(label = "Math Functions"),
-    description = "Factorial. Returns 1 if value is less than 2.",
+    description = "Factorial of a non-negative integer. Errors if the argument 
is negative or the result overflows.",
     syntax_example = "factorial(numeric_expression)",
     sql_example = r#"```sql
 > SELECT factorial(5);
@@ -143,7 +143,7 @@ const FACTORIALS: [i64; 21] = [
 
 fn compute_factorial(n: i64) -> Result<i64> {
     if n < 0 {
-        Ok(1)
+        exec_err!("factorial of a negative number is undefined")
     } else if n < FACTORIALS.len() as i64 {
         Ok(FACTORIALS[n as usize])
     } else {
diff --git a/datafusion/sqllogictest/test_files/math.slt 
b/datafusion/sqllogictest/test_files/math.slt
index e00edf47c1..8f7245365f 100644
--- a/datafusion/sqllogictest/test_files/math.slt
+++ b/datafusion/sqllogictest/test_files/math.slt
@@ -826,6 +826,10 @@ select
 ----
 39 Decimal128(2, 1)
 
+# factorial negative (PostgreSQL-compatible domain error)
+query error DataFusion error: Execution error: factorial of a negative number 
is undefined
+select factorial(-1);
+
 # factorial overflow
 query error DataFusion error: Execution error: Overflow happened on 
FACTORIAL\(350943270\)
 select FACTORIAL(350943270);
diff --git a/docs/source/user-guide/sql/scalar_functions.md 
b/docs/source/user-guide/sql/scalar_functions.md
index 7010a2b7c1..4786163cb5 100644
--- a/docs/source/user-guide/sql/scalar_functions.md
+++ b/docs/source/user-guide/sql/scalar_functions.md
@@ -420,7 +420,7 @@ exp(numeric_expression)
 
 ### `factorial`
 
-Factorial. Returns 1 if value is less than 2.
+Factorial of a non-negative integer. Errors if the argument is negative or the 
result overflows.
 
 ```sql
 factorial(numeric_expression)


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

Reply via email to