-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(arrow-cast): fast path for Dictionary->View cast for large types and cross cast #9768
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
Open
Abhisheklearn12
wants to merge
3
commits into
apache:main
Choose a base branch
from
Abhisheklearn12:feat/dict-view-fast-path-8985
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+241
−3
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
cdfedb4
feat(arrow-cast): fast path for Dictionary->View cast for large types…
Abhisheklearn12 ef75fcf
fix: remove needless borrows in invalid UTF-8 test cases
Abhisheklearn12 fce2027
fix: propagate InvalidArgumentError from binary_dict_to_string_view
Abhisheklearn12 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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,9 +36,6 @@ pub(crate) fn dictionary_cast<K: ArrowDictionaryKeyType>( | |
| } | ||
| // `unpack_dictionary` can handle Utf8View/BinaryView types, but incurs unnecessary data | ||
| // copy of the value buffer. Fast path which avoids copying underlying values buffer. | ||
| // TODO: handle LargeUtf8/LargeBinary -> View (need to check offsets can fit) | ||
| // TODO: handle cross types (String -> BinaryView, Binary -> StringView) | ||
| // (need to validate utf8?) | ||
| (Utf8, Utf8View) => view_from_dict_values::<K, Utf8Type, StringViewType>( | ||
| array.keys(), | ||
| array.values().as_string::<i32>(), | ||
|
|
@@ -47,6 +44,35 @@ pub(crate) fn dictionary_cast<K: ArrowDictionaryKeyType>( | |
| array.keys(), | ||
| array.values().as_binary::<i32>(), | ||
| ), | ||
| // LargeUtf8/LargeBinary -> View: fast path only when i64 offsets fit in u32 (buffer < 4GiB). | ||
| // If the buffer is too large, fall back to the general path. | ||
| (LargeUtf8, Utf8View) => { | ||
| let values = array.values().as_string::<i64>(); | ||
| if values.values().len() < u32::MAX as usize { | ||
| view_from_dict_values::<K, LargeUtf8Type, StringViewType>(array.keys(), values) | ||
| } else { | ||
| unpack_dictionary(array, to_type, cast_options) | ||
| } | ||
| } | ||
| (LargeBinary, BinaryView) => { | ||
| let values = array.values().as_binary::<i64>(); | ||
| if values.values().len() < u32::MAX as usize { | ||
| view_from_dict_values::<K, LargeBinaryType, BinaryViewType>(array.keys(), values) | ||
| } else { | ||
| unpack_dictionary(array, to_type, cast_options) | ||
| } | ||
| } | ||
| // Cross casts: Utf8 -> BinaryView is always zero-copy safe (valid UTF-8 is valid binary). | ||
| (Utf8, BinaryView) => view_from_dict_values::<K, Utf8Type, BinaryViewType>( | ||
| array.keys(), | ||
| array.values().as_string::<i32>(), | ||
| ), | ||
| // Cross cast: Binary -> Utf8View requires UTF-8 validation of the dictionary values. | ||
| (Binary, Utf8View) => binary_dict_to_string_view::<K>( | ||
|
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. I feel this arm specifically should be benchmarked as it introduces new logic compared to the other arms |
||
| array.keys(), | ||
| array.values().as_binary::<i32>(), | ||
| cast_options, | ||
| ), | ||
| _ => unpack_dictionary(array, to_type, cast_options), | ||
| } | ||
| } | ||
|
|
@@ -108,6 +134,66 @@ fn dictionary_to_dictionary_cast<K: ArrowDictionaryKeyType>( | |
| Ok(new_array) | ||
| } | ||
|
|
||
| /// Cast `Dict<K, Binary>` to `Utf8View`, validating UTF-8 for each dictionary value. | ||
| /// | ||
| /// Fast path when all values are valid UTF-8: reuses the values buffer without copying. | ||
| /// When some values are invalid and `cast_options.safe` is true, rows pointing to those | ||
| /// values become null. When `cast_options.safe` is false, returns an error immediately. | ||
| fn binary_dict_to_string_view<K: ArrowDictionaryKeyType>( | ||
| keys: &PrimitiveArray<K>, | ||
| values: &GenericByteArray<BinaryType>, | ||
| cast_options: &CastOptions, | ||
| ) -> Result<ArrayRef, ArrowError> { | ||
| match GenericStringArray::<i32>::try_from_binary(values.clone()) { | ||
| Ok(_) => { | ||
| // All dictionary values are valid UTF-8: reuse the buffer zero-copy. | ||
| view_from_dict_values::<K, BinaryType, StringViewType>(keys, values) | ||
| } | ||
| Err(e) => { | ||
| if !cast_options.safe { | ||
| return Err(e); | ||
| } | ||
| // safe=true: validate each dictionary value individually so we can nullify | ||
| // only the rows whose key points to an invalid UTF-8 value. | ||
| let valid: Vec<bool> = (0..values.len()) | ||
| .map(|i| !values.is_null(i) && std::str::from_utf8(values.value(i)).is_ok()) | ||
| .collect(); | ||
|
|
||
| let value_buffer = values.values(); | ||
| let value_offsets = values.value_offsets(); | ||
| let mut builder = StringViewBuilder::with_capacity(keys.len()); | ||
| builder.append_block(value_buffer.clone()); | ||
|
|
||
| for key in keys.iter() { | ||
| match key { | ||
| Some(v) => { | ||
| let idx = v.to_usize().ok_or_else(|| { | ||
| ArrowError::ComputeError("Invalid dictionary index".to_string()) | ||
| })?; | ||
| if valid[idx] { | ||
| // Safety: | ||
| // (1) idx is a valid index into value_offsets (Arrow invariant) | ||
| // (2) offsets are monotonically increasing, so end >= offset | ||
| // (3) the slice [offset..end] is within the buffer | ||
| // (4) the bytes are valid UTF-8 (checked above for valid[idx]) | ||
| unsafe { | ||
| let offset = value_offsets.get_unchecked(idx).as_usize(); | ||
| let end = value_offsets.get_unchecked(idx + 1).as_usize(); | ||
| let length = end - offset; | ||
| builder.append_view_unchecked(0, offset as u32, length as u32); | ||
| } | ||
| } else { | ||
| builder.append_null(); | ||
| } | ||
| } | ||
| None => builder.append_null(), | ||
| } | ||
| } | ||
| Ok(Arc::new(builder.finish())) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn view_from_dict_values<K: ArrowDictionaryKeyType, V: ByteArrayType, T: ByteViewType>( | ||
| keys: &PrimitiveArray<K>, | ||
| values: &GenericByteArray<V>, | ||
|
|
||
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
Oops, something went wrong.
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.
This check reads a little odd to me as usually this could mean
unpack_dictionarymay also fail if offsets don't fit?