[SPARK-58295][SQL][PYTHON] Add to_base32 and from_base32 functions#57466
Open
SreeramaYeshwanthGowd wants to merge 4 commits into
Open
[SPARK-58295][SQL][PYTHON] Add to_base32 and from_base32 functions#57466SreeramaYeshwanthGowd wants to merge 4 commits into
to_base32 and from_base32 functions#57466SreeramaYeshwanthGowd wants to merge 4 commits into
Conversation
to_base32 and from_base32 built-in…to_base32 and from_base32 functions
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.
What changes were proposed in this pull request?
Add a matched pair of built in scalar functions:
to_base32(binary)encodes bytes as an RFC 4648 Base32 string.from_base32(string)decodes an RFC 4648 Base32 string back to binary.Spark already ships
hex/unhex(base16) andbase64/unbase64, so this completes the standard base16/base32/base64 encoding ladder.API surface added:
to_base32(binary)andfrom_base32(string)functions.to_base32(col)andfunctions.from_base32(col)pyspark.sql.functions.to_base32andfrom_base32Key design choices:
RuntimeReplaceableexpressions backed by aStaticInvoke, mirroring the existingbase64/unbase64functions, so there is no hand written codegen.org.apache.commons.codec.binary.Base32(RFC 4648), the same waybase64delegates tojava.util.Base64.java.util.Base64has no Base32 support, and commons-codec is already a dependency of the catalyst module, so this adds no new dependency.to_base32andfrom_base32match Trino and BigQuery, and follow Spark's establishedto_*/from_*conversion family (for exampleto_json/from_json,to_binary). No major engine usesbase32/unbase32.from_base32decodes leniently, ignoring characters outside the Base32 alphabet, consistent with the existingunbase64behavior. A strict variant is out of scope, matching howunbase64defers strict decoding toto_binary.to_binary,is_valid_utf8,luhn_check), since SparkR is deprecated as of Spark 4.0 and its function surface is frozen.Why are the changes needed?
Base32 (RFC 4648) is the standard encoding for TOTP and 2FA shared secrets (RFC 6238), DNS NSEC3 records, and various identifier schemes. Spark has base16 (
hex) and base64 but no base32, so users fall back to UDFs. Trino (to_base32/from_base32) and Google BigQuery (TO_BASE32/FROM_BASE32) both ship the pair, so this also improves parity with the engines Spark users migrate from.Does this PR introduce any user-facing change?
Yes. It adds two new built in SQL functions,
to_base32andfrom_base32, and the corresponding Scala and PySpark DataFrame API entries. No existing behavior changes.Example:
How was this patch tested?
Added catalyst unit tests in
StringExpressionsSuitecovering the RFC 4648 test vectors, round trip encode and decode, empty input, and null propagation, and a DataFrame API test inStringFunctionsSuite. Added PySpark doctests for both functions. Regeneratedsql-expression-schema.md.Was this patch authored or co-authored using generative AI tooling?
No