parquet-protobuf: recursion truncation breaks repeated and map fields — ClassCastException in specs-compliant mode, file corruption in the old style
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 55/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- java
- Domain
- data-engineering
Research direction
Start with ProtoSchemaConverter.addMessageField and ProtoWriteSupport.createMessageWriter, then read ProtoWriteSupportTest.testRepeatedRecursion and testMapRecursion. Run the new ProtoRecursionTruncationTest cases through a real MessageColumnIO, including both write modes and map recursion. Done means repeated fields and map values no longer crash or corrupt output, while truncated values round-trip as serialized bytes.
Written by the indexing model from the issue text.
Description
Describe the bug
ProtoSchemaConverter (PARQUET-1711) terminates recursive proto message fields at
parquet.proto.maxRecursion depth by replacing them with the serialized proto bytes. The
truncation hardcodes the replacement column as optional binary, ignoring the original field's
repetition:
// ProtoSchemaConverter.addMessageField
if (seen.get(typeName).size() > maxRecursion) {
return builder.primitive(BINARY, Type.Repetition.OPTIONAL).as((LogicalTypeAnnotation) null);
}
ProtoWriteSupport.MessageWriter however still wraps every repeated field's writer in
ArrayWriter (specs-compliant) or RepeatedWriter (old style), and map fields in MapWriter —
all of which emit record structure the optional binary column cannot hold. The mock-based unit
tests (ProtoWriteSupportTest.testRepeatedRecursion / testMapRecursion) never validate against a
real MessageColumnIO, so the mismatch was never caught.
Consequences, each reproduced by an end-to-end write test included in the follow-up PR:
-
Specs-compliant mode, repeated recursive field, data deeper than maxRecursion: the write
crashes withjava.lang.ClassCastException: class org.apache.parquet.io.PrimitiveColumnIO cannot be cast to class org.apache.parquet.io.GroupColumnIO(
ArrayWritercallsstartGroup()/startField("list", 0)against a primitive column). This is
a data-dependent landmine: schema creation and shallow rows succeed; the job dies only when a
row's data actually nests past the limit. -
Old style (writeSpecsCompliant=false), repeated recursive field with more than one element at
the truncation depth: no exception — the write emits inconsistent repetition levels for the
second and following elements, corrupting the file. Depending on the data, reading it back
either fails withParquetDecodingException(Can not read value at ... in block,
EOF/BufferUnderflow underneath) or silently returns a wrong tree (elements lost or attached to
phantom duplicate parent nodes). -
Specs-compliant map field at which the recursion budget runs out (e.g.
google.protobuf.Structmaps reached throughlist_valuebranches): the whole MAP — including
its keys — collapses into a single unreadable binary in the schema, and writing data through
that branch crashes with the sameClassCastException(MapWriternavigatingkey_value
groups over a primitive column). On map paths where the budget happens to trip at a singular
field first (likeStruct's mainfields → struct_valuechain), the schema was already fine —
the collapse is branch-dependent.
Reproducer
Any repeated self-recursive message nested deeper than maxRecursion, e.g. the existing test proto
Trees.WideTree:
Trees.WideTree deep = ...; // chain of children 5 levels deep, 2 children per node
Configuration conf = new Configuration();
ProtoWriteSupport.setWriteSpecsCompliant(conf, true);
ProtoSchemaConverter.setMaxRecursion(conf, 2);
try (ParquetWriter<Message> w = ProtoParquetWriter.<Message>builder(path)
.withMessage(Trees.WideTree.class).withConf(conf).build()) {
w.write(deep); // ClassCastException
}
Affects all released versions since 1.13.0 (PARQUET-1711) through current master (verified on
1.17.1 and master @ e02f65e2).
Proposed fix (PR follows)
Preserve the field's shape when truncating, mirroring how ordinary repeated primitives are handled:
ProtoSchemaConverter.addMessageField:- repeated + specs-compliant → LIST-wrapped binary via the existing
addRepeatedPrimitive
(optional group x (LIST) { repeated group list { required binary element } }); - otherwise
builder.primitive(BINARY, getRepetition(descriptor))(repeated binaryin the old
style; truncated optional fields unchanged, proto2 required fields now keeprequired); - specs-compliant map fields keep their MAP structure unconditionally; a recursive value type
is truncated tooptional binaryinsidekey_valuewhenaddMapFieldrecurses into the value
field (same recursion budget, applied one level deeper where it belongs).
- repeated + specs-compliant → LIST-wrapped binary via the existing
ProtoWriteSupport.createMessageWriter: look through the LIST/MAP wrapper when detecting a
truncated-to-binary message field (getContentType, introduced by the fix for #2142, which
terminates empty message types through the same mechanism) soBinaryWriteris selected for
truncated elements/values; the existingArrayWriter/RepeatedWriter/MapWriterwrapping then
lines up with the schema.
With the fix, each repeated element / map value at the truncation depth round-trips as one binary
containing the serialized subtree (X.parseFrom(bytes) reconstructs it), keys of truncated-value
maps stay queryable, and truncated optional fields are byte-for-byte unchanged.
Existing expected-schema tests were regenerated (WideTree.par, Value.par, Struct.par,
inline schemas, and the testDeepRecursion Struct fan-out series changes from 2n+4 to 2n+5
because a truncated map now retains its key column). New ProtoRecursionTruncationTest (5 tests)
writes through a real MessageColumnIO: repeated recursion in both modes, a map field exhausting
the recursion budget (fails with the ClassCastException before the fix), and the already-working
map-main-path and optional cases as regression guards.
Note on schema compatibility: files previously written with a truncated optional field are
unchanged. A repeated/map truncated field changes its schema shape — but writing more than one
element at the truncation depth crashed (specs) or corrupted the file (old style) before, so no
valid existing files carry the old shape with meaningful multi-element data.
Related
- PARQUET-1711 / #995 — introduced maxRecursion truncation (optional-field case only).
- #2708 / PARQUET-2181 — read-side ClassCastException in parquet-cli on proto files; note that
ProtoParquetReaderitself also cannot read back any truncated field (including the optional
case that writes fine):ProtoMessageConverter.newScalarConverterhas no binary→message path and
throwsClassCastExceptionat converter-tree construction. That read-side gap is orthogonal to
this write-side fix and probably deserves its own issue. - #2142 — empty message types cannot be written at all; the companion PR fixes it with the same
terminate-as-proto-bytes mechanism, and this fix builds on it.
- Dominant language
- Java
- Stars
- 3.1k
- Forks
- 1.6k
- Avg merge
- 4d 12h
- Merged PRs (30d)
- 28
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/parquet-java
-
Type: bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
apache/parquet-java#3792 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
apache/parquet-java#3767 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
apache/parquet-java#3695 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
apache/parquet-java#3667 ·
-
Type: bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
apache/parquet-java#3587 ·
All issues in apache/parquet-java
Similar issues
-
awaiting triage bug Causes friction Hop Gui P1 P2 Transforms
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
apache/flink-agents#1152 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
jenkinsci/blueocean-plugin#5417 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
objectionary/eo-graphs#75 ·