fix(spark): return empty binary for encode('', 'utf-16') #412
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.37) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 222 lines diff across 2 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.323
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #412"
general
weight: 0.35
andygrove
Which issue does this PR close?
Rationale for this change
encode('', 'utf-16')returnsfeff(only the byte order mark), while Spark returns an empty binary. Spark 4.0 returns the input bytes unchanged when the input is empty, and Spark 3.5 goes throughString.getBytes(charset), where the JDK returns an empty array for an empty string without running the encoder, so no BOM is written.What changes are included in this PR?
encode_string, theUTF-16/UTF16branch now skips the byte order mark when the input string is empty. Non-empty input is unchanged and still starts withFE FF.#[user_doc]description ofencodeto say the BOM is omitted for an empty string, and regenerateddocs/functions/scalar/binary_string.mdwithdev/update_function_docs.sh.Are these changes tested?
Yes:
test_utf16_empty_string_has_no_bominencode.rs. It coversutf-16, the legacyutf16alias, empty binary input, and that non-empty input still gets the BOM.string/encode.slt, covering the queries from the issue. The empty result is asserted throughoctet_length(...), because the sqllogictest format can't express an empty binary row.The new tests fail without the fix and pass with it.
cargo fmt --all,cargo clippy --all-targets --all-features -- -D warnings,cargo test -p datafusion-sparkandcargo test -p datafusion-spark-sqllogictestall pass.The expected behavior comes from reading the Spark 4.0.0 and 3.5 sources (see the rationale above). I did not run these cases against a live Spark.
Are there any user-facing changes?
Yes:
encode('', 'utf-16')now returns an empty binary instead offeff, matching Spark. There are no public Rust API changes.