Skip to content

Commit 7fdfd75

Browse files
committed
fix
1 parent 75099a8 commit 7fdfd75

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

datafusion/functions/src/math/factorial.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use arrow::datatypes::{DECIMAL256_MAX_PRECISION, DataType, Int64Type};
2323
use arrow_buffer::i256;
2424

2525
use datafusion_common::{
26-
DataFusionError, Result, ScalarValue, internal_err, utils::take_function_args,
26+
internal_err, utils::take_function_args, DataFusionError, Result, ScalarValue,
2727
};
2828
use datafusion_expr::{
2929
ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
@@ -172,5 +172,11 @@ fn compute_factorial(n: i64) -> Result<i256> {
172172
})?;
173173
}
174174

175+
if result.to_string().len() > DECIMAL256_MAX_PRECISION as usize {
176+
return Err(DataFusionError::Execution(format!(
177+
"Overflow happened on FACTORIAL({n})"
178+
)));
179+
}
180+
175181
Ok(result)
176182
}

datafusion/sqllogictest/test_files/scalar.slt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,14 @@ select factorial(21);
476476
----
477477
51090942171709440000
478478

479+
query TR
480+
select arrow_typeof(factorial(56)), factorial(56);
481+
----
482+
Decimal256(76, 0) 710998587804863451854045647463724949736497978881168458687447040000000000000
483+
484+
query error DataFusion error: Execution error: Overflow happened on FACTORIAL\(57\)
485+
select factorial(57);
486+
479487
# factorial scalar nulls
480488
query R rowsort
481489
select factorial(null);

0 commit comments

Comments
 (0)