Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: substr - correct behaivour with negative start pos #1660

Merged
merged 1 commit into from
Jan 31, 2022
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
67 changes: 67 additions & 0 deletions datafusion/src/physical_plan/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3582,6 +3582,18 @@ mod tests {
StringArray
);
#[cfg(feature = "unicode_expressions")]
test_function!(
Substr,
&[
lit(ScalarValue::Utf8(Some("joséésoj".to_string()))),
lit(ScalarValue::Int64(Some(-5))),
],
Ok(Some("joséésoj")),
&str,
Utf8,
StringArray
);
#[cfg(feature = "unicode_expressions")]
test_function!(
Substr,
&[
Expand Down Expand Up @@ -3680,6 +3692,61 @@ mod tests {
StringArray
);
#[cfg(feature = "unicode_expressions")]
test_function!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

postgres=# select a from t;
    a
----------
 alphabet
(1 rows)

postgres=# select substring(a, 0, 5) from t;
 substring
-----------
 alph
(1 rows)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ovr, Sorry, I think there are still some problems.

Copy link
Contributor Author

@ovr ovr Jan 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, i did another iteration and looks like I found a way how to get similar behaviour

PG:

select substr('alphabet', 0, 5) as s1, substr('alphabet', -5, 10) as s2, substr('alphabet', -5, 4) as s3;
| s1.    | s2     | s3   |
| :---- | :---- | :--- |
| alph  | alph |         |

DF:

❯ select substr('alphabet', 0, 5) as s1, substr('alphabet', -5, 10) as s2, substr('alphabet', -5, 4) as s3;
+------+------+----+
| s1   | s2   | s3 |
+------+------+----+
| alph | alph |    |
+------+------+----+
1 row in set. Query took 0.016 seconds.

Thanks

Substr,
&[
lit(ScalarValue::Utf8(Some("alphabet".to_string()))),
lit(ScalarValue::Int64(Some(0))),
lit(ScalarValue::Int64(Some(5))),
],
Ok(Some("alph")),
&str,
Utf8,
StringArray
);
// starting from 5 (10 + -5)
#[cfg(feature = "unicode_expressions")]
test_function!(
Substr,
&[
lit(ScalarValue::Utf8(Some("alphabet".to_string()))),
lit(ScalarValue::Int64(Some(-5))),
lit(ScalarValue::Int64(Some(10))),
],
Ok(Some("alph")),
&str,
Utf8,
StringArray
);
// starting from -1 (4 + -5)
#[cfg(feature = "unicode_expressions")]
test_function!(
Substr,
&[
lit(ScalarValue::Utf8(Some("alphabet".to_string()))),
lit(ScalarValue::Int64(Some(-5))),
lit(ScalarValue::Int64(Some(4))),
],
Ok(Some("")),
&str,
Utf8,
StringArray
);
// starting from 0 (5 + -5)
#[cfg(feature = "unicode_expressions")]
test_function!(
Substr,
&[
lit(ScalarValue::Utf8(Some("alphabet".to_string()))),
lit(ScalarValue::Int64(Some(-5))),
lit(ScalarValue::Int64(Some(5))),
],
Ok(Some("")),
&str,
Utf8,
StringArray
);
#[cfg(feature = "unicode_expressions")]
test_function!(
Substr,
&[
Expand Down
28 changes: 18 additions & 10 deletions datafusion/src/physical_plan/unicode_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,21 +457,29 @@ pub fn substr<T: StringOffsetSizeTrait>(args: &[ArrayRef]) -> Result<ArrayRef> {
start,
count
)))
} else if start <= 0 {
Ok(Some(string.to_string()))
} else {
let graphemes = string.graphemes(true).collect::<Vec<&str>>();
let start_pos = start as usize - 1;
let count_usize = count as usize;
if graphemes.len() < start_pos {
let (start_pos, end_pos) = if start <= 0 {
let end_pos = start + count - 1;
(
0_usize,
if end_pos < 0 {
// we use 0 as workaround for usize to return empty string
0
} else {
end_pos as usize
},
Comment on lines +466 to +471
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if end_pos < 0 {
// we use 0 as workaround for usize to return empty string
0
} else {
end_pos as usize
},
0_usize.max(end_pos as usize),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ovr would you like to make this change so we can merge the PR? I can find some time to do so too if that would help.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jimexist It doesn't work because it doesn't reflect comparing with less than 0. -1 (i32) with as usize will produce a maximum number that can be stored in usize.

image

@alamb It doesnt work.

)
} else {
((start - 1) as usize, (start + count - 1) as usize)
};

if end_pos == 0 || graphemes.len() < start_pos {
Ok(Some("".to_string()))
} else if graphemes.len() < start_pos + count_usize {
} else if graphemes.len() < end_pos {
Ok(Some(graphemes[start_pos..].concat()))
} else {
Ok(Some(
graphemes[start_pos..start_pos + count_usize]
.concat(),
))
Ok(Some(graphemes[start_pos..end_pos].concat()))
}
}
}
Expand Down