rdblue commented on a change in pull request #4154:
URL: https://github.com/apache/iceberg/pull/4154#discussion_r812483258
##########
File path:
spark/v3.2/spark/src/main/java/org/apache/iceberg/spark/source/SparkWriteBuilder.java
##########
@@ -130,8 +131,31 @@ public Write build() {
Preconditions.checkArgument(handleTimestampWithoutZone ||
!SparkUtil.hasTimestampWithoutZone(table.schema()),
SparkUtil.TIMESTAMP_WITHOUT_TIMEZONE_ERROR);
- Schema writeSchema = SparkSchemaUtil.convert(table.schema(), dsSchema);
- TypeUtil.validateWriteSchema(table.schema(), writeSchema,
writeConf.checkNullability(), writeConf.checkOrdering());
+ Schema writeSchema;
+ boolean mergeSchema = writeInfo.options().getBoolean("mergeSchema",
+ writeInfo.options().getBoolean("merge-schema", false));
+ if (mergeSchema) {
+ // convert the dataset schema and assign fresh ids for new fields
+ Schema newSchema = SparkSchemaUtil.convertWithFreshIds(table.schema(),
dsSchema);
+
+ // update the table to get final id assignments and validate the changes
+ UpdateSchema update = table.updateSchema().unionByNameWith(newSchema);
+ Schema mergedSchema = update.apply();
+
+ // reconvert the dsSchema without assignment to use the ids assigned by
UpdateSchema
+ writeSchema = SparkSchemaUtil.convert(mergedSchema, dsSchema);
+
+ TypeUtil.validateWriteSchema(
+ mergedSchema, writeSchema, writeConf.checkNullability(),
writeConf.checkOrdering());
+
+ // if the validation passed, update the table schema
+ update.commit();
+ } else {
+ writeSchema = SparkSchemaUtil.convert(table.schema(), dsSchema);
Review comment:
Done!
--
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]