wombatu-kun commented on code in PR #16612:
URL: https://github.com/apache/iceberg/pull/16612#discussion_r3369437922
##########
core/src/test/java/org/apache/iceberg/TestSchemaParser.java:
##########
@@ -155,4 +156,46 @@ public void
testPrimitiveTypeDefaultValues(Type.PrimitiveType type, Literal<?> d
assertThat(serialized.findField("col_with_default").writeDefault())
.isEqualTo(defaultValue.value());
}
+
+ @Test
+ public void testStructDefaultValues() {
+ String schemaJson =
+ "{\"type\":\"struct\",\"schema-id\":0,\"fields\":["
+ + "{\"id\":1,\"name\":\"col\",\"required\":false,"
+ + "\"type\":{\"type\":\"struct\",\"fields\":["
+ +
"{\"id\":2,\"name\":\"a\",\"required\":false,\"type\":\"string\","
+ + "\"initial-default\":\"test\",\"write-default\":\"test\"},"
+ +
"{\"id\":3,\"name\":\"b\",\"required\":false,\"type\":\"int\"}]},"
+ + "\"initial-default\":{},\"write-default\":{}},"
+ +
"{\"id\":4,\"name\":\"col2\",\"required\":false,\"type\":\"int\","
+ + "\"initial-default\":34,\"write-default\":35}]}";
+
+ Schema schema = SchemaParser.fromJson(schemaJson);
+ Types.NestedField col = schema.findField("col");
+ Types.StructType colType = col.type().asStructType();
+
+ assertThat(col.initialDefault()).isInstanceOf(StructLike.class);
+ assertThat(col.writeDefault()).isInstanceOf(StructLike.class);
+ assertThat(colType.field("a").initialDefault()).isEqualTo("test");
+ assertThat(colType.field("a").writeDefault()).isEqualTo("test");
+ assertThat(schema.findField("col2").initialDefault()).isEqualTo(34);
+ assertThat(schema.findField("col2").writeDefault()).isEqualTo(35);
+
+ String serializedJson = SchemaParser.toJson(schema);
+ assertThat(serializedJson).contains("\"initial-default\":{}");
+ assertThat(serializedJson).contains("\"write-default\":{}");
+
assertThat(SchemaParser.fromJson(serializedJson).sameSchema(schema)).isTrue();
+ }
+
+ @Test
+ public void testInvalidPrimitiveDefaultValue() {
Review Comment:
This is the only negative test, and it covers pre-existing primitive parsing
in `SingleValueParser.fromJson`, not the validation this PR adds. The new
struct-empty checks at `SchemaParser.java:225` (`non-null struct defaults must
be empty`) and `Types.java:889` (`isEmptyStructDefault`) have no coverage. Add
a case feeding a non-empty struct default, e.g.
`"initial-default":{"2":"test"}` on the struct field, asserting it throws
`non-null struct defaults must be empty`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]