← Back to Pull requests

fix: decode malformed UTF-8 when adapting Parquet LargeBinary and BinaryView to strings #7718

Open
unknown opened this pull request with 617 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 20, 2026 22:15 UTC
Author

Which issue does this PR close?

Part of #7032 and the ingress policy in #4764. #7106 covers nested values. #6246 handles top-level columns with spark.sql.parquet.binaryAsString=true. This PR covers top-level columns that reach Comet's schema adapter as binary, including reads with an explicit string schema and the setting off.

Rationale for this change

A file can store an unannotated Parquet BINARY column named value with an ARROW:schema hint of Dictionary(Int32, LargeBinary). For spark.read.schema("value string").parquet(path), DataFusion passes the dictionary through to Comet. The scalar cast delegates its LargeBinary values to Arrow, which returns NULL for malformed UTF-8 in safe mode and an error in strict mode. For the bytes 66 80 6F, Spark returns a non-null string that renders as f�o. The same conversion gap affects LargeBinary and BinaryView when the file field is named VALUE and Comet resolves it case-insensitively after DataFusion's exact-name check. DataFusion coercion, Arrow cast

What changes are included in this PR?

Route these file-to-string casts through the existing Parquet conversion used for nested values. The schema adapter and converter share the predicate for supported binary representations. Plain Binary at the top level already uses the scalar replacement decoder when it reaches the adapter. It applies JVM-compatible replacement decoding, including one U+FFFD for an encoded surrogate. Spark keeps the original bytes internally, so hex(value) yields 66806F in Spark and 66EFBFBD6F in Comet under the documented policy.

Top-level dictionaries with Binary or LargeBinary values now use the same Parquet conversion as nested dictionaries. It decodes values before expanding the original keys with Arrow's take. For dictionaries above 64 KiB, Arrow's occupancy bitmap identifies the referenced entries. Comet calculates the full expanded byte count only if take reports an offset overflow.

The nested empty-dictionary panic and its regression test are isolated in #7808 for backport to branch-1.2. This PR calls normalized_keys() only after a take offset overflow. An empty dictionary has no valid keys, so take produces null strings without that call.

When replacing malformed bytes, each live input byte can expand to at most three UTF-8 bytes. If that bound exceeds the offset limit, Comet uses the JVM-compatible decoder to count the exact output bytes of each non-null value before allocating the string builder. It returns an offset error if the batch exceeds the limit and uses the exact size for a batch that fits. For binary views this check runs after Arrow materializes the raw binary values.

The 32-bit offset limit tracked in #7254 applies to these reads. At the default batch size, 8,192 references to one entry of 88,000 FF bytes expand to 2,162,688,000 replacement bytes. A query that previously succeeded with spurious nulls can now fail with a diagnostic suggesting a smaller spark.comet.batchSize. The Parquet and compatibility guides describe which top-level values reach the decoder.

For a same-name plain BINARY field, DataFusion can request Utf8 directly from the reader before the adapter runs. That unchecked path is still open under #7032. Map-key identity is tracked in #7360.

How are these changes tested?

  • ParquetReadSuite extends the dictionary fixture to read Dictionary(Int32, LargeBinary) with an explicit string schema and binaryAsString=false. It compares collected strings with Spark, requires a CometNativeScanExec, and checks hex(value) and value IS NULL for a repeated malformed value, valid bytes and a genuine null.

  • A Rust Parquet round trip exercises case-insensitive resolution for LargeBinary and BinaryView, an exact-name Dictionary(Int8, LargeBinary), and an all-null dictionary. It checks café, NULL and 41 ED A0 80 42 → A�B.

  • A Rust capacity test uses a split UTF-8 code point, a null and an encoded surrogate. It checks that their nine decoded bytes fit a nine-byte limit and produce an offset error at eight bytes. The shared decoder tests check the counted lengths against the expected strings.

  • A Rust schema adapter test exercises the overflow diagnostic for both Binary and LargeBinary dictionaries with 8,191 references to a malformed 88,000-byte dictionary entry and one null key. It checks the complete decoded size and batch-size hint. Arrow checks offsets before allocating the expanded string buffer.
  • Checked Rust and Scala formatting and git diff --check locally. Test execution is delegated to hosted CI.

    Diff: 617 lines (+462 / -155) across 7 files Reviewer: viirya Review duration: 15573m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 617 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    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.384
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #7718"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 50.2% (MEDIUM) · Higher = larger blast radius
    50.2%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 26.8% · Safe to merge
    26.8%