Skip to content

Commit

Permalink
fix: fix string repeat for negative numbers (#10760)
Browse files Browse the repository at this point in the history
* fix: fix string repeat for negative numbers

* style: run cargo fmt
  • Loading branch information
tshauck authored Jun 3, 2024
1 parent 1db3263 commit 59bfe77
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion datafusion/functions/src/string/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ fn repeat<T: OffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
.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::<GenericStringArray<T>>();
Expand Down
5 changes: 5 additions & 0 deletions datafusion/sqllogictest/test_files/expr.slt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
----
Expand Down

0 comments on commit 59bfe77

Please sign in to comment.