Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions datafusion/functions/src/math/factorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions datafusion/sqllogictest/test_files/math.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading