← Back to Pull requests

fix(spark): return empty binary for encode('', 'utf-16') #412

Closed
unknown closed this pull request without merging on Nov 30, 2026 15:52 UTC
View on GitHub
unknown unknown commented on Nov 25, 2026 00:29 UTC
Author

Which issue does this PR close?

  • Closes #408.
  • Rationale for this change

    encode('', 'utf-16') returns feff (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 through String.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?

  • In encode_string, the UTF-16 / UTF16 branch now skips the byte order mark when the input string is empty. Non-empty input is unchanged and still starts with FE FF.

  • Updated the #[user_doc] description of encode to say the BOM is omitted for an empty string, and regenerated docs/functions/scalar/binary_string.md with dev/update_function_docs.sh.
  • Are these changes tested?

    Yes:

  • Unit test test_utf16_empty_string_has_no_bom in encode.rs. It covers utf-16, the legacy utf16 alias, empty binary input, and that non-empty input still gets the BOM.

  • New cases in string/encode.slt, covering the queries from the issue. The empty result is asserted through octet_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-spark and cargo test -p datafusion-spark-sqllogictest all 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 of feff, matching Spark. There are no public Rust API changes.

    Diff: 222 lines (+166 / -56) across 2 files Reviewer: andygrove Review duration: 8122m
    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
    0.37
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 36.8% (MEDIUM) · Higher = larger blast radius
    36.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.9% · Safe to merge
    15.9%