Decode empty protobuf message fields as hidden fields in GPU from_protobuf [databricks] #17138
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
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.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #17138"
general
weight: 0.35
wjxiz1992
Fixes #16977.
Description
from_protobuffalls back to the CPU for any message whose descriptor has a field thatSpark leaves out of its schema. The common case is an empty message type: Spark cannot
represent
struct<>, soSchemaConvertersdrops the field, while the GPU planner stillrequires 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 decoderstill 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
GpuFromProtobuftrims the trailing columns of structsand 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.buildDescriptorreturns the descriptor together with the extension registrySpark 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 = falsemechanism 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. Itnow 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 registrationrather 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
ProtobufExprShimsSuitecover the planner decisions: an empty messagefield 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
ProtobufSchemaDescriptorconstructor. 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.
SparkProtobufCompatSuitechecks the registry readerseparately: 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_fieldreplaces the former fallbackexpectation. 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
FAILFASTandPERMISSIVE, asserting that the plan containsGpuFromProtobufand noProtobufDataToCatalyst. The repeated-nested shape is what exercises the trimming of a listchild.
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 rowsare already columnar and leaves the existing batches separate.
test_from_protobuf_projection_with_hidden_fieldsputs both kinds of hidden field in onedecode, for
optional Childand forrepeated Child:Childholds an unread scalar beforethe selected one plus an omitted
Empty, and the query reads only the selected scalar, sothe 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_casescovers the two shapes Spark reads as an empty schema: an empty root messageand 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 =and run with100; }
spark.sql.function.protobufExtensions.enabled=true: one with awell-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
PERMISSIVEis the divergenceitself: 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
spark410db183shim this PR changes; they are skipped elsewhere. Theextension points at a separate empty message rather than at
Emptyitself, because extendinga 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 -DskipTestson JDK 17.0.10 against the publishedcudf-spark-jni26.12.0-SNAPSHOT nightly (20261124.043801-53): BUILD SUCCESS.mvn package -pl sql-plugin -am -Dbuildver=352 -Dsuffixes='.Protobuf.Suite': 61 testsrun, 61 succeeded, including the eleven new cases (ten in
ProtobufExprShimsSuite, whichnow runs 20, and one in
SparkProtobufCompatSuite).python3 build/check_shim_compatibility.py coverage: PASS, 0 findings over 4442 checkedrequirements across the supported
sql-pluginrelease profiles.TESTS="protobuf_test.py" ./integration_tests/run_pyspark_from_build.shon a Tesla T4with 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
GpuFromProtobufran on a GPU. Thanksfor the
cuda-compat-12-9pointer; forward compatibility made the R535 host usable.the CPU, with
spark-protobuf_2.13-4.2.0and no plugin. For the descriptor the new testsbuild,
buildDescriptorreturnsfullNamesToExtensions = Map(test.Empty -> List(test.ext))while
Descriptor.getFieldsholds onlyidandempty, and Spark's schema for the root isstruct<id:int>. So the extension is invisible to the descriptor fields the GPU decoderreads, 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
PERMISSIVEand raisesMALFORMED_PROTOBUF_MESSAGEinFAILFAST, which reproduces thedivergence 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.
Scala 2.13 (
mvn -f scala2.13 -Dbuildver=420): BUILD SUCCESS; the same unit suites61/61; and
TESTS="protobuf_test.py" ./integration_tests/run_pyspark_from_build.shagainst aSpark 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 thespark410db183shim that reads theregistry was exercised on the profile that selects it.
All were run on the current head, which merges
mainat939b63acdand thereforeincludes #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(whichincludes #16697) and pure Spark CPU. Both plugin jars were built back to back offline
(
mvn -o package -Dbuildver=352 -DskipTests -pl dist -am) against identicalcudf-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 Emptyat the root, insideChild, or inside eachrepeated Item.read_allaggregates all 12 visible fields;read_subsetaggregates onlyidanditems[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,
GpuProjectopTime task-summed over8 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
HostColumnarToGputasks on both the base and this PR.Takeaways:
with identical peak memory on both queries, so
hasOmittedFieldskeeps the fast path evenafter #16697 sets
isOutput = falseon unread fields.from_protobufcall: 11.0x to 18.0x against the base on Parquet, 4.3x to 6.4x againstpure CPU, and 6.1x to 9.9x against the base on cached input.
15 MiB or 7.2% of per-task peak GPU memory on cached
read_all(209 -> 224 MiB), becausedecodedand the copied visible column coexist untildecodedis closed. The nestedlevels use zero-copy
ColumnViews, but the top level does one realcopyToColumnVector();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
spark410db183shim reads, but it is not available here; that path runs inDatabricks 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
FAILFASTerror-parity case, because the fallback harness compares collected rows and cannotassert 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
Testing
Performance