-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
ARROW-11035: [Rust] Improved performance of casting to utf8 #9014
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -351,17 +351,13 @@ pub fn cast(array: &ArrayRef, to_type: &DataType) -> Result<ArrayRef> { | |
Float32 => cast_bool_to_numeric::<Float32Type>(array), | ||
Float64 => cast_bool_to_numeric::<Float64Type>(array), | ||
Utf8 => { | ||
let from = array.as_any().downcast_ref::<BooleanArray>().unwrap(); | ||
let mut b = StringBuilder::new(array.len()); | ||
for i in 0..array.len() { | ||
if array.is_null(i) { | ||
b.append(false)?; | ||
} else { | ||
b.append_value(if from.value(i) { "1" } else { "0" })?; | ||
} | ||
} | ||
|
||
Ok(Arc::new(b.finish()) as ArrayRef) | ||
let array = array.as_any().downcast_ref::<BooleanArray>().unwrap(); | ||
Ok(Arc::new( | ||
array | ||
.iter() | ||
.map(|value| value.map(|value| if value { "1" } else { "0" })) | ||
.collect::<StringArray>(), | ||
)) | ||
} | ||
_ => Err(ArrowError::ComputeError(format!( | ||
"Casting from {:?} to {:?} not supported", | ||
|
@@ -431,20 +427,15 @@ pub fn cast(array: &ArrayRef, to_type: &DataType) -> Result<ArrayRef> { | |
Float32 => cast_numeric_to_string::<Float32Type>(array), | ||
Float64 => cast_numeric_to_string::<Float64Type>(array), | ||
Binary => { | ||
let from = array.as_any().downcast_ref::<BinaryArray>().unwrap(); | ||
let mut b = StringBuilder::new(array.len()); | ||
for i in 0..array.len() { | ||
if array.is_null(i) { | ||
b.append_null()?; | ||
} else { | ||
match str::from_utf8(from.value(i)) { | ||
Ok(s) => b.append_value(s)?, | ||
Err(_) => b.append_null()?, // not valid UTF8 | ||
} | ||
} | ||
} | ||
|
||
Ok(Arc::new(b.finish()) as ArrayRef) | ||
let array = array.as_any().downcast_ref::<BinaryArray>().unwrap(); | ||
Ok(Arc::new( | ||
array | ||
.iter() | ||
.map(|maybe_value| { | ||
maybe_value.and_then(|value| str::from_utf8(value).ok()) | ||
}) | ||
.collect::<StringArray>(), | ||
)) | ||
} | ||
_ => Err(ArrowError::ComputeError(format!( | ||
"Casting from {:?} to {:?} not supported", | ||
|
@@ -892,31 +883,22 @@ where | |
FROM: ArrowNumericType, | ||
FROM::Native: std::string::ToString, | ||
{ | ||
numeric_to_string_cast::<FROM>( | ||
Ok(Arc::new(numeric_to_string_cast::<FROM>( | ||
array | ||
.as_any() | ||
.downcast_ref::<PrimitiveArray<FROM>>() | ||
.unwrap(), | ||
) | ||
.map(|to| Arc::new(to) as ArrayRef) | ||
))) | ||
} | ||
|
||
fn numeric_to_string_cast<T>(from: &PrimitiveArray<T>) -> Result<StringArray> | ||
fn numeric_to_string_cast<T>(from: &PrimitiveArray<T>) -> StringArray | ||
where | ||
T: ArrowPrimitiveType + ArrowNumericType, | ||
T::Native: std::string::ToString, | ||
{ | ||
let mut b = StringBuilder::new(from.len()); | ||
|
||
for i in 0..from.len() { | ||
if from.is_null(i) { | ||
b.append(false)?; | ||
} else { | ||
b.append_value(&from.value(i).to_string())?; | ||
} | ||
} | ||
|
||
Ok(b.finish()) | ||
from.iter() | ||
.map(|maybe_value| maybe_value.map(|value| value.to_string())) | ||
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. Note, we probably can use lexical like here later #9010 |
||
.collect() | ||
} | ||
|
||
/// Cast numeric types to Utf8 | ||
|
@@ -2714,11 +2696,8 @@ mod tests { | |
fn test_cast_string_array_to_dict() { | ||
use DataType::*; | ||
|
||
let mut builder = StringBuilder::new(10); | ||
builder.append_value("one").unwrap(); | ||
builder.append_null().unwrap(); | ||
builder.append_value("three").unwrap(); | ||
let array: ArrayRef = Arc::new(builder.finish()); | ||
let array = Arc::new(StringArray::from(vec![Some("one"), None, Some("three")])) | ||
as ArrayRef; | ||
|
||
let expected = vec!["one", "null", "three"]; | ||
|
||
|
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the speed up come here from not using the string builder or is using this iterator also faster?
It looks at least better, so if no difference this is better 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I suspect the builder, because the iterator does the same thing as before atm (i.e. same bound checks).
The builders IMO are inefficient atm. Since IMO they are less idiomatic, I do not see any issue in replacing them whenever we can ^_^