Skip to content

Apache DataFusion SQL Query Engine

fix: Avoid integer overflow in split_part() (#20198)

Neil Conway75428f1e0a01
diff --git a/datafusion/functions/src/string/split_part.rs b/datafusion/functions/src/string/split_part.rs
index 74bf7c16c4..e24dbd63d1 100644
--- a/datafusion/functions/src/string/split_part.rs
+++ b/datafusion/functions/src/string/split_part.rs
@@ -48,7 +48,10 @@ use std::sync::Arc;
```"#,
standard_argument(name = "str", prefix = "String"),
argument(name = "delimiter", description = "String or character to split on."),
- argument(name = "pos", description = "Position of the part to return.")
+ argument(
+ name = "pos",
+ description = "Position of the part to return (counting from 1). Negative values count backward from the end of the string."
+ )
)]
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct SplitPartFunc {
@@ -233,7 +236,7 @@ where
std::cmp::Ordering::Less => {
// Negative index: use rsplit().nth() to efficiently get from the end
// rsplit iterates in reverse, so -1 means first from rsplit (index 0)
- let idx: usize = (-n - 1).try_into().map_err(|_| {
+ let idx: usize = (n.unsigned_abs() - 1).try_into().map_err(|_| {
exec_datafusion_err!(
"split_part index {n} exceeds minimum supported value"
)
@@ -324,6 +327,20 @@ mod tests {
Utf8,
StringArray
);
+ test_function!(
+ SplitPartFunc::new(),
+ vec![
+ ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from(
+ "abc~@~def~@~ghi"
+ )))),
+ ColumnarValue::Scalar(ScalarValue::Utf8(Some(String::from("~@~")))),
+ ColumnarValue::Scalar(ScalarValue::Int64(Some(i64::MIN))),
+ ],
+ Ok(Some("")),
+ &str,
+ Utf8,
+ StringArray
+ );
Ok(())
}
diff --git a/docs/source/user-guide/sql/scalar_functions.md b/docs/source/user-guide/sql/scalar_functions.md
index 1b52c7bdab..e09c4cb7cb 100644
--- a/docs/source/user-guide/sql/scalar_functions.md
+++ b/docs/source/user-guide/sql/scalar_functions.md
@@ -1891,7 +1891,7 @@ split_part(str, delimiter, pos)
- **str**: String expression to operate on. Can be a constant, column, or function, and any combination of operators.
- **delimiter**: String or character to split on.
-- **pos**: Position of the part to return.
+- **pos**: Position of the part to return (counting from 1). Negative values count backward from the end of the string.
#### Example