-
Notifications
You must be signed in to change notification settings - Fork 859
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
Support Utf8View
for bit_length
kernel
#6671
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
507dcfa
Support `Utf8View` for string function `bit_length()`
austin362667 67201b7
Add test & handle view bytes length counting
austin362667 2ad7170
Refine `string_view_array`
austin362667 de5e33a
Make length from `i32` to `u32` & check nullity
austin362667 4ab6cfd
Clean up
austin362667 cdfc465
Refine
austin362667 a3650ef
Use `from_unary` instead
austin362667 098cf3e
Prevent inspect the string data
austin362667 2a0c8b2
Clean up
austin362667 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,9 @@ | |
|
||
use arrow_array::*; | ||
use arrow_array::{cast::AsArray, types::*}; | ||
use arrow_buffer::{ArrowNativeType, NullBuffer, OffsetBuffer}; | ||
use arrow_buffer::{ArrowNativeType, NullBuffer, OffsetBuffer, ScalarBuffer}; | ||
use arrow_schema::{ArrowError, DataType}; | ||
use cast::as_string_array; | ||
use std::sync::Arc; | ||
|
||
fn length_impl<P: ArrowPrimitiveType>( | ||
|
@@ -137,6 +138,22 @@ pub fn bit_length(array: &dyn Array) -> Result<ArrayRef, ArrowError> { | |
let list = array.as_string::<i64>(); | ||
Ok(bit_length_impl::<Int64Type>(list.offsets(), list.nulls())) | ||
} | ||
DataType::Utf8View => { | ||
let list = array.as_string_view(); | ||
let values = (0..list.len()) | ||
.map(|i| { | ||
if list.is_valid(i) { | ||
list.views()[i] as u32 * 8 | ||
} else { | ||
0 | ||
} | ||
}) | ||
.collect::<Vec<u32>>(); | ||
Ok(Arc::new(UInt32Array::new( | ||
values.into(), | ||
array.nulls().cloned(), | ||
))) | ||
} | ||
DataType::Binary => { | ||
let list = array.as_binary::<i32>(); | ||
Ok(bit_length_impl::<Int32Type>(list.offsets(), list.nulls())) | ||
|
@@ -418,7 +435,7 @@ mod tests { | |
assert_eq!(&expected, result); | ||
} | ||
|
||
fn bit_length_cases() -> Vec<(Vec<&'static str>, usize, Vec<i32>)> { | ||
fn bit_length_cases() -> Vec<(Vec<&'static str>, usize, Vec<u32>)> { | ||
// a large array | ||
let values = ["one", "on", "o", ""]; | ||
let values = values.into_iter().cycle().take(4096).collect(); | ||
|
@@ -440,7 +457,7 @@ mod tests { | |
let array = StringArray::from(input); | ||
let result = bit_length(&array).unwrap(); | ||
assert_eq!(len, result.len()); | ||
let result = result.as_any().downcast_ref::<Int32Array>().unwrap(); | ||
let result = result.as_any().downcast_ref::<UInt32Array>().unwrap(); | ||
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. I agree that bit_length should not beed to return signed integer, but it seems better not to change this within this PR. |
||
expected.iter().enumerate().for_each(|(i, value)| { | ||
assert_eq!(*value, result.value(i)); | ||
}); | ||
|
@@ -462,6 +479,21 @@ mod tests { | |
}) | ||
} | ||
|
||
#[test] | ||
fn bit_length_test_utf8view() { | ||
bit_length_cases() | ||
austin362667 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.into_iter() | ||
.for_each(|(input, len, expected)| { | ||
let string_array = StringViewArray::from(input); | ||
let result = bit_length(&string_array).unwrap(); | ||
assert_eq!(len, result.len()); | ||
let result = result.as_any().downcast_ref::<UInt32Array>().unwrap(); | ||
expected.iter().enumerate().for_each(|(i, value)| { | ||
assert_eq!(*value, result.value(i)); | ||
}); | ||
}) | ||
} | ||
|
||
#[test] | ||
fn bit_length_binary() { | ||
let value: Vec<&[u8]> = vec![b"one", &[0xff, 0xf8], b"three"]; | ||
|
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.
As we're doing infallible wrapping arithmetic, we can just compute this for everything, this will vectorize better, and the null mask is preserved below
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.
Wow! Thank you so much @tustvold , you are indeed making me a better Rust programmer. Really appreciate for review. 👍