From b33dd0ac5f9e77df6bb81f5ab7ef04e6869e8424 Mon Sep 17 00:00:00 2001 From: NopeCharon <115794254+NopeCharon@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:01:13 +0530 Subject: [PATCH] GH-1901: Honor the strict flag when merging nested group types GroupType.union(Type, boolean) accepted a strict flag but called mergeFields(GroupType), which always merges strictly. A non-strict MessageType.union therefore only relaxed the primitive type checks for top-level fields: as soon as a conflicting primitive was nested inside a group, the merge failed with IncompatibleSchemaModificationException exactly as in strict mode. ParquetFileWriter.mergeInto(..., false), and with it ParquetInputFormat when parquet.strict.typing is false, hit the same failure. Pass the flag through to mergeFields so non-strict merging behaves the same at every nesting level. Strict merging is unchanged. Remove the package-private mergeFields(GroupType) overload, which had no other callers and silently defaulted to strict merging. Add a test that merges schemas whose conflicting primitive is nested two groups deep, checking that strict merging still fails and non-strict merging keeps the receiver's type and adds the missing field. Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com> --- .../org/apache/parquet/schema/GroupType.java | 12 +------ .../parquet/schema/TestMessageType.java | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/parquet-column/src/main/java/org/apache/parquet/schema/GroupType.java b/parquet-column/src/main/java/org/apache/parquet/schema/GroupType.java index 2763b8553b..497a883918 100644 --- a/parquet-column/src/main/java/org/apache/parquet/schema/GroupType.java +++ b/parquet-column/src/main/java/org/apache/parquet/schema/GroupType.java @@ -385,20 +385,10 @@ protected Type union(Type toMerge, boolean strict) { toMerge.getRepetition(), getName(), toMerge.getLogicalTypeAnnotation(), - mergeFields(toMerge.asGroupType()), + mergeFields(toMerge.asGroupType(), strict), getId()); } - /** - * produces the list of fields resulting from merging toMerge into the fields of this - * - * @param toMerge the group containing the fields to merge - * @return the merged list - */ - List mergeFields(GroupType toMerge) { - return mergeFields(toMerge, true); - } - /** * produces the list of fields resulting from merging toMerge into the fields of this * diff --git a/parquet-column/src/test/java/org/apache/parquet/schema/TestMessageType.java b/parquet-column/src/test/java/org/apache/parquet/schema/TestMessageType.java index d8fae9ac16..45e0be8d88 100644 --- a/parquet-column/src/test/java/org/apache/parquet/schema/TestMessageType.java +++ b/parquet-column/src/test/java/org/apache/parquet/schema/TestMessageType.java @@ -134,6 +134,41 @@ public void testMergeSchema() { "can not merge type optional fixed_len_byte_array(20) a into optional fixed_len_byte_array(10) a"); } + @Test + public void testNonStrictMergeSchemaWithNestedFields() { + MessageType t1 = new MessageType( + "root1", + new GroupType(REQUIRED, "g", new GroupType(REQUIRED, "g1", new PrimitiveType(OPTIONAL, BINARY, "a")))); + MessageType t2 = new MessageType( + "root2", + new GroupType( + REQUIRED, + "g", + new GroupType(REQUIRED, "g1", new PrimitiveType(OPTIONAL, INT32, "a")), + new PrimitiveType(OPTIONAL, BINARY, "b"))); + + assertThatThrownBy(() -> t1.union(t2, true)) + .isInstanceOf(IncompatibleSchemaModificationException.class) + .hasMessage("can not merge type optional int32 a into optional binary a"); + + assertThat(t1.union(t2, false)) + .isEqualTo(new MessageType( + "root1", + new GroupType( + REQUIRED, + "g", + new GroupType(REQUIRED, "g1", new PrimitiveType(OPTIONAL, BINARY, "a")), + new PrimitiveType(OPTIONAL, BINARY, "b")))); + assertThat(t2.union(t1, false)) + .isEqualTo(new MessageType( + "root2", + new GroupType( + REQUIRED, + "g", + new GroupType(REQUIRED, "g1", new PrimitiveType(OPTIONAL, INT32, "a")), + new PrimitiveType(OPTIONAL, BINARY, "b")))); + } + @Test public void testMergeSchemaWithOriginalType() throws Exception { MessageType t5 = new MessageType(