Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions datafusion/spark/src/function/string/substring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,17 @@ fn spark_substring(args: &[ArrayRef]) -> Result<ArrayRef> {
/// - Zero start: treated as 1
/// - Negative start: counts from end of string
///
/// Returns the converted 1-based start position for use with `get_true_start_end`.
/// The result may be `<= 0` when a negative start lands before the string
/// (e.g. `start=-10` on a 3-char string gives `-6`). Such values are passed
/// through to `get_true_start_end`, which clamps them and yields an empty
/// slice — matching Spark's behavior for out-of-range negative positions.
#[inline]
fn spark_start_to_datafusion_start(start: i64, len: usize) -> i64 {
if start >= 0 {
start.max(1)
} else {
let len_i64 = i64::try_from(len).unwrap_or(i64::MAX);
let start = start.saturating_add(len_i64).saturating_add(1);
start.max(1)
start + len_i64 + 1
Comment thread
comphead marked this conversation as resolved.
}
}

Expand Down
11 changes: 9 additions & 2 deletions datafusion/sqllogictest/test_files/spark/string/substring.slt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ ark SQL
(empty)
(empty)
(empty)
Spa
(empty)
NULL
NULL
NULL
Expand Down Expand Up @@ -197,7 +197,14 @@ ark SQL
(empty)
(empty)
(empty)
Spa
(empty)
NULL
NULL
NULL

query T
SELECT substr(column1, -10, 3)
FROM VALUES
('abc'::string)
----
(empty)
Loading