-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix: substr - correct behaivour with negative start pos #1660
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
| &[ | ||
|
|
@@ -3680,6 +3692,61 @@ mod tests { | |
| StringArray | ||
| ); | ||
| #[cfg(feature = "unicode_expressions")] | ||
| test_function!( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @ovr, Sorry, I think there are still some problems.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: DF: 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, | ||
| &[ | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||
| ) | ||||||||||||||||
| } 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())) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||

Uh oh!
There was an error while loading. Please reload this page.