anoopj commented on code in PR #16958:
URL: https://github.com/apache/iceberg/pull/16958#discussion_r3670191480
##########
api/src/main/java/org/apache/iceberg/types/TypeUtil.java:
##########
@@ -157,6 +157,24 @@ public static Schema selectNot(Schema schema, Set<Integer>
fieldIds) {
return project(schema, projectedIds);
}
+ /**
+ * Returns a copy of the schema with the type of each field in {@code
replacementsById} replaced
+ * by its mapped type. Fields not in the map are unchanged.
+ *
+ * @param schema a schema
+ * @param replacementsById a map from field ID to the type that should
replace the field's type
+ * @return a schema with the replaced field types
+ */
+ public static Schema replaceFieldTypes(Schema schema, Map<Integer, Type>
replacementsById) {
+ Types.StructType struct = visit(schema, new
ReplaceTypeById(replacementsById)).asStructType();
+ if (struct.equals(schema.asStruct())) {
+ return schema;
+ }
+
+ return new Schema(
+ schema.schemaId(), struct.fields(), schema.getAliases(),
schema.identifierFieldIds());
Review Comment:
replaceFieldTypes only swaps field types. It rebuilds each changed field
with NestedField.from(field).ofType(replacement), which preserves the field ID,
name, and optionality, and it carries identifierFieldIds through to the new
Schema. So "drops an identifier field" and "makes it optional" aren't reachable
through this API because the field and its required-ness are untouched. The one
reachable case is making an identifier field non-primitive, and that's already
caught: the new Schema constructor runs validateIdentifierField, which rejects
it.
Added tests in both directions
--
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]