Commit 2d1d53e
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 <ros@Raushans-MacBook-Air.local>
Co-authored-by: Kumar Ujjawal <ujjawalpathak6@gmail.com>1 parent 6e58d74 commit 2d1d53e
3 files changed
Lines changed: 7 additions & 3 deletions
File tree
- datafusion
- functions/src/math
- sqllogictest/test_files
- docs/source/user-guide/sql
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
32 | 32 | | |
33 | 33 | | |
34 | 34 | | |
35 | | - | |
| 35 | + | |
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
146 | | - | |
| 146 | + | |
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
826 | 826 | | |
827 | 827 | | |
828 | 828 | | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
829 | 833 | | |
830 | 834 | | |
831 | 835 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
420 | 420 | | |
421 | 421 | | |
422 | 422 | | |
423 | | - | |
| 423 | + | |
424 | 424 | | |
425 | 425 | | |
426 | 426 | | |
| |||
0 commit comments