Abhisheklearn12 opened a new pull request, #9768:
URL: https://github.com/apache/arrow-rs/pull/9768
# Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases. You can
link an issue to this PR using the GitHub syntax.
-->
- Closes #8985
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in
the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your
changes and offer better suggestions for fixes.
-->
`unpack_dictionary` handled all Dictionary→View casts correctly but incurred
an unnecessary copy of the values buffer on every cast. For Dictionary arrays
with many repeated values (the common use case), this copies data for every
logical row rather than once.
A fast path already existed for `Utf8->Utf8View` and `Binary->BinaryView`
via `view_from_dict_values`, which reuses the values buffer zero-copy and only
writes 16-byte view structs per row. This PR extends that to the remaining
cases called out in the TODO comments.
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is
sometimes worth providing a summary of the individual changes in this PR.
-->
- Add `(LargeUtf8, Utf8View)` fast path in `dictionary_cast`: reuses the
values buffer zero-copy when i64 offsets fit in `u32` (buffer < 4 GiB), falls
back to `unpack_dictionary` when the buffer is too large
- Add `(LargeBinary, BinaryView)` fast path with the same offset-fit check
- Add `(Utf8, BinaryView)` cross cast fast path: UTF-8 strings are always
valid binary so the buffer is reused unconditionally
- Add `(Binary, Utf8View)` cross cast via new
`binary_dict_to_string_view`: validates UTF-8 of dictionary values and reuses
the buffer zero-copy when all valid; respects `CastOptions::safe` , nullifies
rows pointing to invalid dictionary values when `safe=true`, returns
`CastError` when `safe=false`
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are
they covered by existing tests)?
-->
Yes. Added 6 tests in `arrow-cast/src/cast/mod.rs`:
- `test_dict_large_utf8_to_utf8view`-> `LargeUtf8->Utf8View` fast path,
including null keys and values longer than 12 bytes (buffered views)
- `test_dict_large_binary_to_binary_view` -> `LargeBinary->BinaryView`
fast path, including null keys
- `test_dict_utf8_to_binary_view` -> `Utf8->BinaryView` cross cast
- `test_dict_binary_to_utf8view_valid` -> `Binary->Utf8View` when all
dictionary values are valid UTF-8 (zero-copy fast path)
- `test_dict_binary_to_utf8view_invalid_utf8_strict` -> `Binary->Utf8view`
with invalid UTF-8 and `safe=false` returns `CastError`
- `test_dict_binary_to_utf8view_invalid_utf8_safe` -> `Binary->Utf8View`
with invalid UTF-8 and `safe=true` nullifies every row whose key points to an
invalid dictionary value, preserving valid rows
# Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
If there are any breaking changes to public APIs, please call them out.
-->
Yes. Casting `Dictionary<_, LargeUtf8>->Utf8View`, `Dictionary<_,
LargeBinary>->BinaryView`, `Dictionary<_, Utf8> >BinaryView`, and
`Dictionary<_, Binary>->Utf8View` is now significantly faster for large arrays
with repeated values. The dictionary values buffer is reused without copying
instead of being fully unpacked row-by-row.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]