Repository navigation
feat(spark): add to_binary and try_to_binary - #24266
Conversation
Implements `to_binary(str[, fmt])` and `try_to_binary(str[, fmt])` for the datafusion-spark crate. `fmt` is a case-insensitive literal of `hex`, `utf-8`, `utf8` or `base64`, defaulting to `hex`. The conversion is done in the kernel rather than by delegating to existing functions: Spark rewrites `to_binary` onto `Unhex`/`Encode`/`UnBase64` with `failOnError = true`, but the equivalents here disagree on which inputs error and which return NULL, so a single `fail_on_error` flag drives both variants. `unbase64` also has no kernel to call -- it exists only as a `simplify()` rewrite -- and core's base64 decoder is private. Hex decoding reuses the existing `unhex_scalar`, which is made `pub(crate)` rather than duplicated. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
cc @Jefffrey This is my first time contribution. Can you please take a look? |
Jefffrey
left a comment
There was a problem hiding this comment.
ci errors are because base64 has a 0.23.1 now, so need to update the lockfile (just rerun cargo check/build and commit again)
|
@Jefffrey Sorry for the delay. I have resolved the comments. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #24266 +/- ##
========================================
Coverage 81.22% 81.23%
========================================
Files 1113 1114 +1
Lines 392508 392714 +206
Branches 392508 392714 +206
========================================
+ Hits 318833 319024 +191
- Misses 54916 54930 +14
- Partials 18759 18760 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-authored-by: Jeffrey Vo <jeffrey.vo.australia@gmail.com>
|
Thanks @Jefffrey |
|
thanks @arsathS |
|
Sorry to disturb @Jefffrey. When can we start using these functions which are getting added? Should we need to wait for the release? If so what will be the frequency of the releases which are made? |
|
itll like come in v56 release targeted for october |
Which issue does this PR close?
datafusion-sparkSpark Compatible Functions #15914Rationale for this change
Spark's
to_binary(str[, fmt])converts a string to binary, choosing how basedon
fmt:hex,utf-8/utf8, orbase64.try_to_binaryis the samefunction except that input it cannot convert yields NULL instead of raising an
error.
Neither is available in the
datafusion-sparkcrate, and no core DataFusionfunction can stand in for them:
decodeaccepts onlybase64,base64padandhex, so there is noway to ask it for Spark's
utf-8conversiondecodealways raises on input it cannot convert, so there is nothingto build
try_to_binary's NULL-returning behaviour fromWhat changes are included in this PR?
SparkToBinaryandSparkTryToBinaryUDFs indatafusion/spark/src/function/string/to_binary.rsto_binary(str[, fmt])wherefmtis a case-insensitive literal ofhex,utf-8,utf8orbase64, defaulting tohexhexdecodes two characters per byte, left-padding an odd-length inputwith
0, matchingUnhexutf-8/utf8returns the string's own UTF-8 bytesbase64matches Java's MIME decoder, which Spark uses: the standardalphabet, padding optional, and the unused trailing bits of a short final
group ignored
fmtyields NULL in both functionsto_binaryraises on a malformed value or an unrecognisedfmt;try_to_binaryreturns NULL for both, matchingnullOnInvalidFormatfmtmust be foldable, as Spark requiresunhex_scalaris madepub(crate)and reused for thehexpath ratherthan duplicating the decoder
mod.rs(make_udf_function!,export_functions!,functions())Are these changes tested?
Yes.
to_binary.rs(each format, hex as the default, emptyinput, NULL value and NULL format, invalid value, invalid format, column
input, and a column where one row is invalid)
spark/string/to_binary.sltandspark/string/try_to_binary.sltAre there any user-facing changes?
No. These are new functions in the
datafusion-sparkcrate only.