fix(shuffle): tolerate non-UTF-8 bytes in get_string (lossy decode)#4524
Open
schenksj wants to merge 2 commits into
Open
fix(shuffle): tolerate non-UTF-8 bytes in get_string (lossy decode)#4524schenksj wants to merge 2 commits into
schenksj wants to merge 2 commits into
Conversation
Spark's UnsafeRow.getUTF8String performs no UTF-8 validation, and cast(BinaryType -> StringType) is a zero-copy reinterpret, so a StringType column can legitimately hold arbitrary non-UTF-8 bytes. get_string decoded with from_utf8(..).unwrap(), which panics on such rows even though Spark treats them as opaque. Use from_utf8_lossy (returning Cow<str>): a zero-cost borrow for valid UTF-8 and a String with U+FFFD replacements otherwise -- defined behavior, no UB. Avoids from_utf8_unchecked, which would construct a &str from arbitrary bytes (UB) and propagate into downstream Arrow ops. Adds a standalone unit test that panics without the fix and passes with it. Closes apache#4521 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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 #4521.
Rationale for this change
Spark's
UnsafeRow.getUTF8Stringperforms no UTF-8 validation, andcast(BinaryType -> StringType)is a zero-copy reinterpret — so aStringTypecolumn can legitimately hold arbitrary, non-UTF-8 bytes. Comet's native shuffleget_stringdecoded withfrom_utf8(..).unwrap(), which panics on such rows even though Spark itself treats them as opaque.What changes are included in this PR?
get_stringnow decodes withfrom_utf8_lossyand returnsCow<str>: a zero-cost borrow for valid UTF-8, and aStringwithU+FFFDreplacements for invalid bytes — defined behavior, no UB. This deliberately avoidsfrom_utf8_unchecked, which constructs a&strfrom arbitrary bytes (UB per the Rust reference) and would propagate into downstream Arrow ops that internally callstr::from_utf8_uncheckedon the buffer. Callers pass the result throughappend_value(impl AsRef<str>), whichCow<str>satisfies.How are these changes tested?
Added a standalone unit test
get_string_tolerates_non_utf8_bytesthat builds a SparkUnsafeRowwith aStringTypefield holding0xFF 0xFE 'A'. It panics without the fix (Result::unwrap()on aUtf8Error) and passes with it (lossy-decoded toU+FFFD U+FFFD A).