← Back to Pull requests

Decode empty protobuf message fields as hidden fields in GPU from_protobuf [databricks] #17138

Closed
unknown closed this pull request without merging on Nov 27, 2026 12:57 UTC
View on GitHub
unknown unknown commented on Nov 25, 2026 06:51 UTC
Author

Fixes #16977.

Description

from_protobuf falls back to the CPU for any message whose descriptor has a field that
Spark leaves out of its schema. The common case is an empty message type: Spark cannot
represent struct<>, so SchemaConverters drops the field, while the GPU planner still
requires every descriptor field to appear in the Spark schema. A workload that carries an
unused empty metadata field therefore loses GPU acceleration for the whole expression.

After this change, such a field no longer forces a fallback. The planner adds the whole
omitted subtree to the JNI schema as hidden fields (isOutput = false), so the decoder
still validates presence and encoding. A message counts as empty for Spark when every one
of its fields is itself such a message, so a wrapper around an empty message is covered
too, bounded by a visited set for recursive types and by the decoder's nesting limit, which
is checked against the absolute depth of the omitted subtree. The
decoder returns an all-null column for a hidden field, and the planner appends hidden
entries last at every level, so GpuFromProtobuf trims the trailing columns of structs
and of list elements to keep the output aligned with the Spark schema. There is no new
user-facing configuration, and results are unchanged.

Every other omission still falls back to the CPU: a missing scalar field, a missing
non-empty message field, a type mismatch, a recursive message type, and omitted oneof or
map fields.

Registered protobuf extensions are a further exclusion. On Spark 4.2 and DBR 18.3,
ProtobufUtils.buildDescriptor returns the descriptor together with the extension registry
Spark built for it, keyed by the full name of the message each extension targets. Those
extension fields are absent from the descriptor's own fields, so the GPU decoder would never
validate them, yet Spark still reads the extended message as an empty schema and drops the
field that holds it. The planner therefore reads that registry through the existing shim hook
and falls back for any message an extension targets, whether the message would be hidden by
this change or holds the omitted field itself. Metadata it cannot read is treated as
extensions being present, so an unreadable registry falls back rather than hiding unvalidated
fields.

The JNI hidden-field support comes from NVIDIA/cudf-spark-jni#5372 and is already present
in the 26.12.0-SNAPSHOT nightly builds, so no JNI change is needed here.

Relation to #16697

#16697 has landed and is merged into this branch. It uses the same isOutput = false
mechanism for a different purpose: it hides the fields a query does not read, which are
present in Spark's output schema, so the all-null column the decoder returns fills the
matching slot. The fields hidden here are absent from Spark's schema altogether, so their
columns are trimmed instead. The conflicts were limited to imports and a class comment.

One adjustment came out of that merge: the trimming gate used to be
schema.isOutput.contains(false), which after #16697 also matches unread visible fields. It
now compares the number of schema entries with the number of fields in the Spark schema, so
only the extra entries are trimmed and the null placeholders of unread fields stay in place.

#17122 has landed too and came in with the same merge without conflicts. #16655 is still
open and touches ProtobufExprShims.scala, but it reworks replacement rule registration
rather than the planner behavior this PR depends on, so it is a merge conflict to resolve
rather than a behavioral interaction.

Tests

Scala unit tests in ProtobufExprShimsSuite cover the planner decisions: an empty message
field and a wrapper whose fields are all empty messages are kept as hidden fields, while a
recursive message type, oneof and map fields, and any other missing field are rejected. Two
tests check that the flattened schema, with a top-level and with a nested hidden entry, is
accepted by the JNI ProtobufSchemaDescriptor constructor. Two more cover the depth limit:
the same descriptor is accepted at depth 9 and rejected at depth 10, and a nine-entry
wrapper subtree is accepted when it starts at depth 1 but rejected at depth 2, which is the
case a relative depth count used to let through. One more test covers registered extensions:
an empty message an extension targets is not hidden, and neither is a message that holds the
omitted field and is itself targeted. SparkProtobufCompatSuite checks the registry reader
separately: it keys on the extended message's full name, ignores entries with no extension,
and fails rather than reporting none when the descriptor carries no registry at all.

In protobuf_test.py, test_from_protobuf_empty_message_field replaces the former fallback
expectation. Its 10 cases cover optional present and absent, required, repeated with two
entries and with none, a nested empty message, a repeated message whose element hides one, a
recursively empty wrapper, and a nested field carrying the wrong wire type, under both
FAILFAST and PERMISSIVE, asserting that the plan contains GpuFromProtobuf and no
ProtobufDataToCatalyst. The repeated-nested shape is what exercises the trimming of a list
child.

The malformed-input suite gains 15 cases: a missing required empty message, a truncated
varint, an invalid wire type and a wrong wire type for the field, across top-level, repeated,
nested, repeated-nested and wrapper shapes, plus a required empty message below a hidden
parent and below a visible one, and invalid bytes both in a hidden wrapper and in the empty
message nested below it. That harness runs every case with a valid row, the malformed row and
a null row in one batch, under both modes, so hidden-field validation and the column trimming
are exercised on malformed and null rows. That suite now builds its rows in a single
partition, so the three kinds of row really share a batch; this applies to its pre-existing
cases as well. coalesce(1) is not enough here, because it merges partitions after the rows
are already columnar and leaves the existing batches separate.

test_from_protobuf_projection_with_hidden_fields puts both kinds of hidden field in one
decode, for optional Child and for repeated Child: Child holds an unread scalar before
the selected one plus an omitted Empty, and the query reads only the selected scalar, so
the unread field keeps its null placeholder while the omitted column is trimmed after it and
the ordinal of the selected field has to survive.

_fallback_cases covers the two shapes Spark reads as an empty schema: an empty root message
and a root whose only field is an empty message. The decoder cannot build a struct without
children, so those stay on the CPU. Two more cases register extend Empty { optional Ext ext =
100; }
and run with spark.sql.function.protobufExtensions.enabled=true: one with a
well-formed extension payload and one with a malformed one, both asserting that the root stays
on the CPU and that CPU and GPU agree. The malformed case in PERMISSIVE is the divergence
itself: the CPU returns a null row, so a GPU decode that skipped the extension would return a
decoded row instead. Both run wherever the extension registry exists, which is Spark 4.2 or
later and Databricks 18.3, where it is backported onto Spark 4.1 and read by the
spark410db183 shim this PR changes; they are skipped elsewhere. The
extension points at a separate empty message rather than at Empty itself, because extending
a message with its own type makes the schema recursive and Spark rejects it during analysis.

Two behaviors worth noting. Spark rejects a recursive protobuf schema during analysis, so the
recursion guard is covered by a unit test rather than an integration case. A wrong wire type
for a nested empty message field is an unknown field that protobuf skips rather than rejects,
on the CPU as well, so that one is a parity case instead of a malformed case.

Validation performed locally:

  • mvn package -Dbuildver=352 -DskipTests on JDK 17.0.10 against the published

  • cudf-spark-jni 26.12.0-SNAPSHOT nightly (20261124.043801-53): BUILD SUCCESS.
  • mvn package -pl sql-plugin -am -Dbuildver=352 -Dsuffixes='.Protobuf.Suite': 61 tests

  • run, 61 succeeded, including the eleven new cases (ten in ProtobufExprShimsSuite, which
    now runs 20, and one in SparkProtobufCompatSuite).
  • python3 build/check_shim_compatibility.py coverage: PASS, 0 findings over 4442 checked

  • requirements across the supported sql-plugin release profiles.
  • TESTS="protobuf_test.py" ./integration_tests/run_pyspark_from_build.sh on a Tesla T4

  • with Spark 3.5.2: 170 passed, 2 skipped, 0 failed. The skips are the two extension cases,
    which need the extension registry and ran separately below. This covers the new parity and
    malformed cases, so the recursive column trimming in GpuFromProtobuf ran on a GPU. Thanks
    for the cuda-compat-12-9 pointer; forward compatibility made the R535 host usable.
  • The extension behavior this change relies on was checked directly against Spark 4.2.0 on

  • the CPU, with spark-protobuf_2.13-4.2.0 and no plugin. For the descriptor the new tests
    build, buildDescriptor returns fullNamesToExtensions = Map(test.Empty -> List(test.ext))
    while Descriptor.getFields holds only id and empty, and Spark's schema for the root is
    struct<id:int>. So the extension is invisible to the descriptor fields the GPU decoder
    reads, and the field that holds the extended message is exactly one this change would
    otherwise hide. With a malformed payload inside the extension, the CPU returns a null row in
    PERMISSIVE and raises MALFORMED_PROTOBUF_MESSAGE in FAILFAST, which reproduces the
    divergence this fallback closes. With the conf left at its default the registry comes back
    empty and the CPU decodes the same malformed row without complaint, so the new fallback is
    limited to descriptors whose extensions Spark actually validates.
  • The build, the unit suites and the integration run were then repeated for Spark 4.2 with

  • Scala 2.13 (mvn -f scala2.13 -Dbuildver=420): BUILD SUCCESS; the same unit suites
    61/61; and
    TESTS="protobuf_test.py" ./integration_tests/run_pyspark_from_build.sh against a
    Spark 4.2.0 distribution on the same Tesla T4: 172 passed, 0 skipped, 0 failed. Both
    extension cases ran there, so the fallback and the CPU/GPU agreement were observed on a
    GPU with protobufExtensions.enabled=true, and the spark410db183 shim that reads the
    registry was exercised on the profile that selects it.

    All were run on the current head, which merges main at 939b63acd and therefore
    includes #16697, so the run also covers the two hidden-field paths working together.

    Performance

    Benchmarked by @thirtiseven on the exact head, against the merge base 939b63acd (which
    includes #16697) and pure Spark CPU. Both plugin jars were built back to back offline
    (mvn -o package -Dbuildver=352 -DskipTests -pl dist -am) against identical
    cudf-spark-jni (560a5ce8) and cuDF (5dfab722) snapshots. Spark 3.5.2, Scala 2.12,
    JDK 17, local[8], one RTX 5880 Ada, AQE off, maxPartitionBytes=16m, pinned pool 1g.

    Workload: 400,000 rows, about 1.6 KB each, 640 MB of serialized protobuf and 96 MiB of
    Snappy Parquet over 8 partitions. All four schemas carry the same visible payload and differ
    only by an optional Empty at the root, inside Child, or inside each repeated Item.
    read_all aggregates all 12 visible fields; read_subset aggregates only id and
    items[1].x, which exercises #16697 unread-field hiding and this PR's trimming together.
    One cold warmup plus three measured warm runs per cell. All 48 cells produced identical
    aggregate digests. This PR ran fully on the GPU for all four schemas, while the base fell
    back to GpuCpuBridgeExpression(ProtobufDataToCatalyst) on the three empty-message schemas.

    Parquet scan, median of three warm runs, times in ms, GpuProject opTime task-summed over
    8 tasks, peak as base / PR:

    | Query | Schema | CPU | Base | PR | vs Base | vs CPU | Base opTime | PR opTime | Peak (MiB) |
    |---|---|---:|---:|---:|---:|---:|---:|---:|---|
    | read_all | no_empty (control) | 1048.1 | 303.0 | 310.7 | 0.98x | 3.37x | 81.6 | 85.9 | 235.8 / 235.8 |
    | read_all | root_empty | 1053.5 | 3286.1 | 246.5 | 13.3x | 4.27x | 8591.5 | 67.8 | 236.3 / 236.3 |
    | read_all | nested_empty | 959.2 | 3371.3 | 219.5 | 15.4x | 4.37x | 8615.6 | 68.0 | 236.1 / 236.1 |
    | read_all | repeated_nested_empty | 1011.4 | 3790.4 | 210.7 | 18.0x | 4.80x | 9755.2 | 81.3 | 238.2 / 238.2 |
    | read_subset | no_empty (control) | 817.9 | 168.6 | 178.5 | 0.94x | 4.58x | 21.5 | 25.9 | 235.8 / 235.8 |
    | read_subset | root_empty | 768.8 | 1877.3 | 170.6 | 11.0x | 4.51x | 4678.1 | 33.6 | 236.3 / 236.3 |
    | read_subset | nested_empty | 779.9 | 1860.2 | 149.4 | 12.5x | 5.22x | 4629.2 | 33.1 | 236.1 / 236.1 |
    | read_subset | repeated_nested_empty | 872.2 | 2038.9 | 135.8 | 15.0x | 6.42x | 5206.6 | 31.1 | 238.2 / 238.2 |

    Pre-cached CPU input (InMemoryTableScan), which isolates decode and trimming:

    | Query | Schema | CPU | Base | PR | vs Base | vs CPU | Base opTime | PR opTime | Peak (MiB) |
    |---|---|---:|---:|---:|---:|---:|---:|---:|---|
    | read_all | no_empty (control) | 825.1 | 372.1 | 363.3 | 1.02x | 2.27x | 55.4 | 65.5 | 209.0 / 209.0 |
    | read_all | root_empty | 839.8 | 3405.5 | 366.5 | 9.29x | 2.29x | 8628.3 | 59.9 | 209.1 / 224.1 |
    | read_all | nested_empty | 800.7 | 3516.6 | 356.4 | 9.87x | 2.25x | 8634.8 | 64.1 | 209.1 / 224.0 |
    | read_all | repeated_nested_empty | 893.7 | 2821.1 | 355.8 | 7.93x | 2.51x | 9843.3 | 58.6 | 209.7 / 224.7 |
    | read_subset | no_empty (control) | 600.8 | 323.0 | 315.9 | 1.02x | 1.90x | 16.0 | 18.8 | 102.0 / 102.0 |
    | read_subset | root_empty | 591.9 | 1915.0 | 312.2 | 6.13x | 1.90x | 4635.7 | 32.1 | 176.4 / 102.7 |
    | read_subset | nested_empty | 593.7 | 1968.6 | 317.8 | 6.19x | 1.87x | 4632.7 | 32.9 | 176.4 / 102.1 |
    | read_subset | repeated_nested_empty | 721.7 | 2134.7 | 309.9 | 6.89x | 2.33x | 5153.8 | 34.0 | 177.0 / 111.9 |

    In cached mode the 1 GiB pinned pool saturated transiently under 8 concurrent
    HostColumnarToGpu tasks on both the base and this PR.

    Takeaways:

  • No regression on the control schema. Base and PR differ by -9.9 to +8.8 ms, within noise,

  • with identical peak memory on both queries, so hasOmittedFields keeps the fast path even
    after #16697 sets isOutput = false on unread fields.
  • The schemas this PR newly supports stop falling back to the CPU for the whole

  • from_protobuf call: 11.0x to 18.0x against the base on Parquet, 4.3x to 6.4x against
    pure CPU, and 6.1x to 9.9x against the base on cached input.
  • Trimming costs about 13 to 15 ms of task-summed compute, buried in wall time, and about

  • 15 MiB or 7.2% of per-task peak GPU memory on cached read_all (209 -> 224 MiB), because
    decoded and the copied visible column coexist until decoded is closed. The nested
    levels use zero-copy ColumnViews, but the top level does one real copyToColumnVector();
    an earlier revision of this description mentioned only the zero-copy views.

    Full setup, all 48 measured cells and the correctness digests are in
    https://github.com/NVIDIA/cudf-spark/pull/17138#issuecomment-7186314274.

    Notes

    Databricks 18.3 is in the gate for both extension cases, since that runtime backports the
    wrapper the spark410db183 shim reads, but it is not available here; that path runs in
    Databricks premerge, which the diff triggers by touching the db shim directories.

    One gap is worth naming: the claim that the malformed case would catch a regression is an
    inference, not a measurement. It was not run against a build without this fix; the divergence
    it guards against is the one @wjxiz1992 measured on the previous head. There is also no
    FAILFAST error-parity case, because the fallback harness compares collected rows and cannot
    assert the raised error.

    This change was developed with AI assistance. The complete diff and this description were
    reviewed by a human before publishing.

    Checklists

    Documentation

  • Updated for new or modified user-facing features or behaviors

  • No user-facing change
  • Testing

  • Added or modified tests to cover new code paths

  • Covered by existing tests

  • Not required
  • Performance

  • Tests ran and results are added in the PR description

  • Issue filed with a link in the PR description

  • Not required
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: wjxiz1992 Review duration: 3245m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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%
    60%
    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.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #17138"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 20.5% · Safe to merge
    20.5%