Skip to content

Commit b6ace16

Browse files
authored
fix: substr - correct behaivour with negative start pos (#1660)
1 parent 940d4eb commit b6ace16

File tree

2 files changed

+85
-10
lines changed

2 files changed

+85
-10
lines changed

datafusion/src/physical_plan/functions.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,6 +3582,18 @@ mod tests {
35823582
StringArray
35833583
);
35843584
#[cfg(feature = "unicode_expressions")]
3585+
test_function!(
3586+
Substr,
3587+
&[
3588+
lit(ScalarValue::Utf8(Some("joséésoj".to_string()))),
3589+
lit(ScalarValue::Int64(Some(-5))),
3590+
],
3591+
Ok(Some("joséésoj")),
3592+
&str,
3593+
Utf8,
3594+
StringArray
3595+
);
3596+
#[cfg(feature = "unicode_expressions")]
35853597
test_function!(
35863598
Substr,
35873599
&[
@@ -3680,6 +3692,61 @@ mod tests {
36803692
StringArray
36813693
);
36823694
#[cfg(feature = "unicode_expressions")]
3695+
test_function!(
3696+
Substr,
3697+
&[
3698+
lit(ScalarValue::Utf8(Some("alphabet".to_string()))),
3699+
lit(ScalarValue::Int64(Some(0))),
3700+
lit(ScalarValue::Int64(Some(5))),
3701+
],
3702+
Ok(Some("alph")),
3703+
&str,
3704+
Utf8,
3705+
StringArray
3706+
);
3707+
// starting from 5 (10 + -5)
3708+
#[cfg(feature = "unicode_expressions")]
3709+
test_function!(
3710+
Substr,
3711+
&[
3712+
lit(ScalarValue::Utf8(Some("alphabet".to_string()))),
3713+
lit(ScalarValue::Int64(Some(-5))),
3714+
lit(ScalarValue::Int64(Some(10))),
3715+
],
3716+
Ok(Some("alph")),
3717+
&str,
3718+
Utf8,
3719+
StringArray
3720+
);
3721+
// starting from -1 (4 + -5)
3722+
#[cfg(feature = "unicode_expressions")]
3723+
test_function!(
3724+
Substr,
3725+
&[
3726+
lit(ScalarValue::Utf8(Some("alphabet".to_string()))),
3727+
lit(ScalarValue::Int64(Some(-5))),
3728+
lit(ScalarValue::Int64(Some(4))),
3729+
],
3730+
Ok(Some("")),
3731+
&str,
3732+
Utf8,
3733+
StringArray
3734+
);
3735+
// starting from 0 (5 + -5)
3736+
#[cfg(feature = "unicode_expressions")]
3737+
test_function!(
3738+
Substr,
3739+
&[
3740+
lit(ScalarValue::Utf8(Some("alphabet".to_string()))),
3741+
lit(ScalarValue::Int64(Some(-5))),
3742+
lit(ScalarValue::Int64(Some(5))),
3743+
],
3744+
Ok(Some("")),
3745+
&str,
3746+
Utf8,
3747+
StringArray
3748+
);
3749+
#[cfg(feature = "unicode_expressions")]
36833750
test_function!(
36843751
Substr,
36853752
&[

datafusion/src/physical_plan/unicode_expressions.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -457,21 +457,29 @@ pub fn substr<T: StringOffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
457457
start,
458458
count
459459
)))
460-
} else if start <= 0 {
461-
Ok(Some(string.to_string()))
462460
} else {
463461
let graphemes = string.graphemes(true).collect::<Vec<&str>>();
464-
let start_pos = start as usize - 1;
465-
let count_usize = count as usize;
466-
if graphemes.len() < start_pos {
462+
let (start_pos, end_pos) = if start <= 0 {
463+
let end_pos = start + count - 1;
464+
(
465+
0_usize,
466+
if end_pos < 0 {
467+
// we use 0 as workaround for usize to return empty string
468+
0
469+
} else {
470+
end_pos as usize
471+
},
472+
)
473+
} else {
474+
((start - 1) as usize, (start + count - 1) as usize)
475+
};
476+
477+
if end_pos == 0 || graphemes.len() < start_pos {
467478
Ok(Some("".to_string()))
468-
} else if graphemes.len() < start_pos + count_usize {
479+
} else if graphemes.len() < end_pos {
469480
Ok(Some(graphemes[start_pos..].concat()))
470481
} else {
471-
Ok(Some(
472-
graphemes[start_pos..start_pos + count_usize]
473-
.concat(),
474-
))
482+
Ok(Some(graphemes[start_pos..end_pos].concat()))
475483
}
476484
}
477485
}

0 commit comments

Comments
 (0)