diff --git a/datafusion/functions/src/string/repeat.rs b/datafusion/functions/src/string/repeat.rs index a70d0a162562..9d122f6101a7 100644 --- a/datafusion/functions/src/string/repeat.rs +++ b/datafusion/functions/src/string/repeat.rs @@ -88,7 +88,10 @@ fn repeat(args: &[ArrayRef]) -> Result { .iter() .zip(number_array.iter()) .map(|(string, number)| match (string, number) { - (Some(string), Some(number)) => Some(string.repeat(number as usize)), + (Some(string), Some(number)) if number >= 0 => { + Some(string.repeat(number as usize)) + } + (Some(_), Some(_)) => Some("".to_string()), _ => None, }) .collect::>(); diff --git a/datafusion/sqllogictest/test_files/expr.slt b/datafusion/sqllogictest/test_files/expr.slt index 2dc00cbc5001..b6477f0b57d0 100644 --- a/datafusion/sqllogictest/test_files/expr.slt +++ b/datafusion/sqllogictest/test_files/expr.slt @@ -541,6 +541,11 @@ SELECT repeat('Pg', 4) ---- PgPgPgPg +query T +SELECT repeat('Pg', -1) +---- +(empty) + query T SELECT repeat('Pg', CAST(NULL AS INT)) ----