fix: substring with negative start index#4017
Open
kazuyukitanimura wants to merge 10 commits intoapache:mainfrom
Open
fix: substring with negative start index#4017kazuyukitanimura wants to merge 10 commits intoapache:mainfrom
kazuyukitanimura wants to merge 10 commits intoapache:mainfrom
Conversation
andygrove
reviewed
Apr 24, 2026
| let result = DictionaryArray::try_new(dict.keys().clone(), values)?; | ||
| Ok(Arc::new(result) as ArrayRef) | ||
| } | ||
| _ => Ok(Arc::clone(array)), |
Member
There was a problem hiding this comment.
Should this be an error rather than just returning the input data?
Contributor
Author
There was a problem hiding this comment.
Thank you, updated
andygrove
reviewed
Apr 24, 2026
| Ok(Arc::new(builder.finish()) as ArrayRef) | ||
| } | ||
| DataType::Dictionary(_, _) => { | ||
| let dict = as_dictionary_array::<Int32Type>(array); |
Member
There was a problem hiding this comment.
This would panic for a dictionary with Int64Type. Can we add a check for the type?
Contributor
Author
There was a problem hiding this comment.
Parquet dictionary uses Integer, so we are not doing Int64Type including other locations. E.g.https://github.com/apache/datafusion-comet/blob/main/native/spark-expr/src/static_invoke/char_varchar_utils/read_side_padding.rs#L68
andygrove
reviewed
Apr 24, 2026
Comment on lines
+202
to
+216
| fn spark_substr_negative(s: &str, pos: i64, len: u64) -> String { | ||
| let num_chars = s.chars().count() as i64; | ||
| let start = num_chars + pos; | ||
| let end = start.saturating_add(len as i64).min(num_chars); | ||
| let start = start.max(0); | ||
|
|
||
| if start >= end { | ||
| return String::new(); | ||
| } | ||
|
|
||
| s.chars() | ||
| .skip(start as usize) | ||
| .take((end - start) as usize) | ||
| .collect() | ||
| } |
Member
There was a problem hiding this comment.
Claude recommended an optimized version to avoid an intermediate string allocation per row. I have not verified.
fn spark_substr_negative(s: &str, pos: i64, len: u64) -> &str {
let num_chars = s.chars().count() as i64;
let end = (num_chars + pos).saturating_add(len as i64).min(num_chars);
let start = (num_chars + pos).max(0);
if start >= end {
return "";
}
// Translate char indices [start, end) to byte offsets in a single forward pass.
let mut it = s.char_indices();
let byte_start = it.by_ref().nth(start as usize).map(|(b, _)| b).unwrap_or(s.len());
let span = (end - start - 1) as usize;
let byte_end = it.nth(span).map(|(b, _)| b).unwrap_or(s.len());
&s[byte_start..byte_end]
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Closes #3919
Closes #3337
Rationale for this change
What changes are included in this PR?
How are these changes tested?