feat: native randstr implementation compatible with Spark#5035
Merged
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.
comphead
approved these changes
Jul 25, 2026
comphead
left a comment
Contributor
There was a problem hiding this comment.
Thanks @andygrove
Couple of nits
- Capacity math
num_rows * self.length— safe under 64-bitusizebut considerchecked_mulif adversarial hugelengthliterals are ever a concern.
Test gaps
- No golden-value Rust test.
rand.rshasSPARK_SEED_42_FIRST_5;randstr.rsshould have aSPARK_SEED_N_FIRST_Mfixture generated from Spark'sExpressionImplUtils.randStrso byte-level Spark
compatibility is asserted in-process (independent of the SQL-test Spark comparison, which is slow and only runs under a Spark-enabled profile). - No Int-vs-Long seed unit test. Serde does
s.toLongsign extension; a test showingLiteral(-1, IntegerType)andLiteral(-1L, LongType)produce identical output would lock this in. - No explicit partition-index unit test. Only
partition().into()addition is trusted; a small test that runsRandStrExpr::new(len, base_seed + p)and compares against expected Spark-side output for
the same partition would validate the whole path. - SQL tests:
- Missing a check that
randstr(length, seed)output is stable acrossparquet.enable.dictionaryand across split boundaries (add aConfigMatrixcomment header + a multi-partition table if feasible). - Missing a case with
randstrunder a filter / groupBy to confirm nondeterministic caching semantics (two calls inSELECT upper(randstr(6,7)), length(randstr(6,7))exists — good — but no test with
randstrinWHERE/HAVING). - Test for very large
length(e.g.,randstr(10_000_000, 0)) — not required, but a smoke test would catch capacity-math regressions.
- Missing a check that
- Rust
test_randstr_zero_lengthusesseed=42— pin the empty output expected for a wider seed set (or drop the seed dimension entirely; it's irrelevant when length=0).
# Conflicts: # docs/source/user-guide/latest/expressions.md
Address review feedback on the randstr PR: - Rust golden-value test asserting bit-for-bit equality with Spark 4.1.1 across positive, zero, and negative seeds, independent of the SQL tests. - Rust partition-index test exercising the seed + partition_index arithmetic. - Broaden the zero-length test across a wider seed set (seed is irrelevant when length is zero). - Rust large-length smoke test guarding the builder capacity math, and saturating_mul on the capacity hint to avoid overflow on huge lengths. - SQL test asserting an Int seed and its Long literal produce identical output (serde toLong sign extension). - SQL test running randstr through a native projection feeding a filter.
Member
Author
|
Thanks for the thorough review @comphead. I merged latest Rust tests (
SQL tests (
A few I handled differently, happy to revisit:
|
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.