feat: native randstr implementation compatible with Spark#5035
Open
andygrove wants to merge 2 commits into
Open
feat: native randstr implementation compatible with Spark#5035andygrove wants to merge 2 commits into
andygrove wants to merge 2 commits into
Conversation
…er-row UTF-8 scan Move the literal-length/literal-seed/non-negative-length restrictions from convert into getSupportLevel + getUnsupportedReasons so they surface in EXPLAIN and the compatibility docs, matching the rand/randn serde pattern. Build each string in a reused String buffer instead of validating a byte buffer per row.
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?
There is no dedicated tracking issue;
randstrwas previously listed as planned (🔜) in the expression support guide.Closes #.
Rationale for this change
Spark 4.0+
randstr(length[, seed])fell back to Spark because Comet had no native implementation. Spark generates a random alphanumeric string per row viaExpressionImplUtils.randStr, seeding anXORShiftRandomwithseed + partitionIndexper partition and drawingabs(rng.nextInt() % 62)per character (mapped onto0-9/a-z/A-Z). Comet already ports that exactXORShiftRandomfor nativerand/randn, sorandstrcan run natively and produce results that match Spark bit for bit, keeping the enclosing operator on the native pipeline instead of falling back.What changes are included in this PR?
This change was scaffolded with the project's
implement-comet-expressionskill (and audited withaudit-comet-expression).RandStrExpr(native/spark-expr/src/nondetermenistic_funcs/randstr.rs), modeled on therand/uuidpattern: it keeps a per-partitionXorShiftRandomin aMutexso state advances across batches, and encodes eachlength-byte string into a pre-sizedStringBuilder. AddedXorShiftRandom::next_i32()(JavaRandom.nextInt()).RandStr { length, seed }message wired through the expression registry with aRandStrBuilderthat combines the seed with the partition index (matchingrand/randn/shuffle).CometRandStrserde emitting the resolved literal length and seed. BecauseRandStris a Spark 4.0+ class, the serde lives underspark-4.xand is registered viaSpark4xCometExprShim.sparkVersionSpecificStringExpressions, so Spark 3.4/3.5 (which lack the class) still compile.misc_funcsaudit entry recording the cross-version findings.The audit confirmed
ExpressionImplUtils.randStrand theXORShiftRandomseeding are byte-identical across Spark 4.0.1 and 4.1.1, so no intra-4.x shim is needed.lengthandseedare required-foldable; Comet handles non-negative literal length with a literal seed and otherwise falls back to Spark (which also raises the negative-length error).How are these changes tested?
randstr.rs: length and alphanumeric charset, zero length, determinism per (length, seed), and per-batch state continuity.expressions/string/randstr.sql(Spark 4.0+, gated withMinSparkVersion: 4.0): seededrandstr(length, seed)asserts bit-for-bit equality with Spark across single and multiple rows, negative/zero/boundary seeds, lengths 0/1/12/64, and combined with other expressions; a negative length asserts theINVALID_PARAMETER_VALUE.LENGTHfallback; the no-seedrandstr(length)form checks deterministic length and alphabet properties. Verified natively on Spark 4.1.2 (default); on Spark 3.5 the build compiles and the gated file is skipped.