You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by sz...@apache.org on 2026/03/30 07:46:40 UTC

(iceberg) branch fix_merge_schema_evolution created (now 1bfeef633c)

This is an automated email from the ASF dual-hosted git repository.

szehon pushed a change to branch fix_merge_schema_evolution
in repository https://gitbox.apache.org/repos/asf/iceberg.git


      at 1bfeef633c Spark: Control merge schema evolution by table property

This branch includes the following new commits:

     new 1bfeef633c Spark: Control merge schema evolution by table property

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(iceberg) 01/01: Spark: Control merge schema evolution by table property

Posted by sz...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

szehon pushed a commit to branch fix_merge_schema_evolution
in repository https://gitbox.apache.org/repos/asf/iceberg.git

commit 1bfeef633c2ced5222fc99a9a8cd2c2ede802b6b
Author: Szehon Ho <sz...@gmail.com>
AuthorDate: Mon Mar 30 00:46:25 2026 -0700

    Spark: Control merge schema evolution by table property
    
    Add a new table property write.spark.auto-schema-evolution (default true)
    that controls whether the AUTOMATIC_SCHEMA_EVOLUTION capability is
    reported to Spark. When set to false, Spark's MERGE WITH SCHEMA
    EVOLUTION no longer evolves the target table schema.
    
    Also add a guard in SparkWriteBuilder to reject mergeSchema write option
    when the property is disabled.
---
 .../java/org/apache/iceberg/TableProperties.java   |  4 ++
 .../spark/extensions/TestMergeSchemaEvolution.java | 45 ++++++++++++++++++++++
 .../apache/iceberg/spark/source/SparkTable.java    | 32 +++++++++++----
 .../iceberg/spark/source/SparkWriteBuilder.java    | 17 ++++++++
 4 files changed, 90 insertions(+), 8 deletions(-)

diff --git a/core/src/main/java/org/apache/iceberg/TableProperties.java b/core/src/main/java/org/apache/iceberg/TableProperties.java
index 1f778984af..cae8853de3 100644
--- a/core/src/main/java/org/apache/iceberg/TableProperties.java
+++ b/core/src/main/java/org/apache/iceberg/TableProperties.java
@@ -346,6 +346,10 @@ public class TableProperties {
   public static final String SPARK_WRITE_ACCEPT_ANY_SCHEMA = "write.spark.accept-any-schema";
   public static final boolean SPARK_WRITE_ACCEPT_ANY_SCHEMA_DEFAULT = false;
 
+  public static final String SPARK_WRITE_AUTO_SCHEMA_EVOLUTION =
+      "write.spark.auto-schema-evolution";
+  public static final boolean SPARK_WRITE_AUTO_SCHEMA_EVOLUTION_DEFAULT = true;
+
   public static final String SPARK_WRITE_ADVISORY_PARTITION_SIZE_BYTES =
       "write.spark.advisory-partition-size-bytes";
 
diff --git a/spark/v4.1/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestMergeSchemaEvolution.java b/spark/v4.1/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestMergeSchemaEvolution.java
index d760d56b7a..aeb857a6df 100644
--- a/spark/v4.1/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestMergeSchemaEvolution.java
+++ b/spark/v4.1/spark-extensions/src/test/java/org/apache/iceberg/spark/extensions/TestMergeSchemaEvolution.java
@@ -19,10 +19,12 @@
 package org.apache.iceberg.spark.extensions;
 
 import static org.apache.spark.sql.functions.col;
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assumptions.assumeThat;
 
 import java.util.Map;
 import org.apache.iceberg.ParameterizedTestExtension;
+import org.apache.iceberg.TableProperties;
 import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList;
 import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
 import org.junit.jupiter.api.AfterEach;
@@ -260,6 +262,49 @@ public class TestMergeSchemaEvolution extends SparkRowLevelOperationsTestBase {
         sql("SELECT id, value FROM %s ORDER BY id", selectTarget()));
   }
 
+  @TestTemplate
+  public void testMergeWithSchemaEvolutionDisabledByTableProperty() {
+    assumeThat(branch).as("Schema evolution does not work for branches currently").isNull();
+
+    createAndInitTable(
+        "id INT, dep STRING",
+        "{ \"id\": 1, \"dep\": \"hr\" }\n" + "{ \"id\": 2, \"dep\": \"software\" }");
+
+    sql(
+        "ALTER TABLE %s SET TBLPROPERTIES ('%s' 'false')",
+        tableName, TableProperties.SPARK_WRITE_AUTO_SCHEMA_EVOLUTION);
+
+    createOrReplaceView(
+        "source",
+        "id INT, dep STRING, salary INT",
+        "{ \"id\": 1, \"dep\": \"hr\", \"salary\": 100 }\n"
+            + "{ \"id\": 3, \"dep\": \"finance\", \"salary\": 300 }");
+
+    sql(
+        "MERGE WITH SCHEMA EVOLUTION INTO %s AS t USING source AS s "
+            + "ON t.id == s.id "
+            + "WHEN MATCHED THEN "
+            + "  UPDATE SET * "
+            + "WHEN NOT MATCHED THEN "
+            + "  INSERT *",
+        commitTarget());
+
+    // Schema should NOT be evolved - 'salary' column should not be added
+    assertThat(sql("SELECT * FROM %s", selectTarget()).get(0).length)
+        .as("Table should still have only 2 columns (id, dep)")
+        .isEqualTo(2);
+
+    ImmutableList<Object[]> expectedRows =
+        ImmutableList.of(
+            row(1, "hr"), // updated without salary
+            row(2, "software"), // kept
+            row(3, "finance")); // new without salary
+    assertEquals(
+        "Should have expected rows without schema evolution",
+        expectedRows,
+        sql("SELECT id, dep FROM %s ORDER BY id", selectTarget()));
+  }
+
   @Override
   protected Map<String, String> extraTableProperties() {
     return Map.of();
diff --git a/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkTable.java b/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkTable.java
index ae3c0ce0c8..d624c9aca0 100644
--- a/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkTable.java
+++ b/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkTable.java
@@ -83,20 +83,14 @@ public class SparkTable extends BaseSparkTable
 
   private static final Logger LOG = LoggerFactory.getLogger(SparkTable.class);
 
-  private static final Set<TableCapability> CAPABILITIES =
+  private static final Set<TableCapability> BASE_CAPABILITIES =
       ImmutableSet.of(
-          TableCapability.AUTOMATIC_SCHEMA_EVOLUTION,
           TableCapability.BATCH_READ,
           TableCapability.BATCH_WRITE,
           TableCapability.MICRO_BATCH_READ,
           TableCapability.STREAMING_WRITE,
           TableCapability.OVERWRITE_BY_FILTER,
           TableCapability.OVERWRITE_DYNAMIC);
-  private static final Set<TableCapability> CAPABILITIES_WITH_ACCEPT_ANY_SCHEMA =
-      ImmutableSet.<TableCapability>builder()
-          .addAll(CAPABILITIES)
-          .add(TableCapability.ACCEPT_ANY_SCHEMA)
-          .build();
 
   private final Schema schema; // effective schema (not necessarily current table schema)
   private final Snapshot snapshot; // always set unless table is empty
@@ -133,7 +127,7 @@ public class SparkTable extends BaseSparkTable
     this.snapshot = snapshot;
     this.branch = branch;
     this.timeTravel = timeTravel;
-    this.capabilities = acceptAnySchema(table) ? CAPABILITIES_WITH_ACCEPT_ANY_SCHEMA : CAPABILITIES;
+    this.capabilities = computeCapabilities(table);
   }
 
   public SparkTable copyWithBranch(String newBranch) {
@@ -353,6 +347,21 @@ public class SparkTable extends BaseSparkTable
     return new SparkTable(table, snapshotId, timeTravel);
   }
 
+  private static Set<TableCapability> computeCapabilities(Table table) {
+    ImmutableSet.Builder<TableCapability> caps = ImmutableSet.builder();
+    caps.addAll(BASE_CAPABILITIES);
+
+    if (autoSchemaEvolution(table)) {
+      caps.add(TableCapability.AUTOMATIC_SCHEMA_EVOLUTION);
+    }
+
+    if (acceptAnySchema(table)) {
+      caps.add(TableCapability.ACCEPT_ANY_SCHEMA);
+    }
+
+    return caps.build();
+  }
+
   private static boolean acceptAnySchema(Table table) {
     return PropertyUtil.propertyAsBoolean(
         table.properties(),
@@ -360,6 +369,13 @@ public class SparkTable extends BaseSparkTable
         TableProperties.SPARK_WRITE_ACCEPT_ANY_SCHEMA_DEFAULT);
   }
 
+  private static boolean autoSchemaEvolution(Table table) {
+    return PropertyUtil.propertyAsBoolean(
+        table.properties(),
+        TableProperties.SPARK_WRITE_AUTO_SCHEMA_EVOLUTION,
+        TableProperties.SPARK_WRITE_AUTO_SCHEMA_EVOLUTION_DEFAULT);
+  }
+
   // returns latest snapshot for branch or current snapshot if branch is yet to be created
   private static Snapshot determineLatestSnapshot(Table table, String branch) {
     if (branch != null && table.refs().containsKey(branch)) {
diff --git a/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkWriteBuilder.java b/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkWriteBuilder.java
index 4858f7793c..1929a914f8 100644
--- a/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkWriteBuilder.java
+++ b/spark/v4.1/spark/src/main/java/org/apache/iceberg/spark/source/SparkWriteBuilder.java
@@ -22,6 +22,7 @@ import org.apache.iceberg.IsolationLevel;
 import org.apache.iceberg.MetadataColumns;
 import org.apache.iceberg.Schema;
 import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
 import org.apache.iceberg.TableUtil;
 import org.apache.iceberg.UpdateSchema;
 import org.apache.iceberg.expressions.Expression;
@@ -38,6 +39,7 @@ import org.apache.iceberg.spark.source.SparkWriteBuilder.Mode.CopyOnWriteOperati
 import org.apache.iceberg.spark.source.SparkWriteBuilder.Mode.DynamicOverwrite;
 import org.apache.iceberg.spark.source.SparkWriteBuilder.Mode.OverwriteByFilter;
 import org.apache.iceberg.types.TypeUtil;
+import org.apache.iceberg.util.PropertyUtil;
 import org.apache.spark.sql.SparkSession;
 import org.apache.spark.sql.connector.read.Scan;
 import org.apache.spark.sql.connector.write.BatchWrite;
@@ -129,6 +131,7 @@ class SparkWriteBuilder implements WriteBuilder, SupportsDynamicOverwrite, Suppo
   @Override
   public Write build() {
     validateRowLineage();
+    validateAutoSchemaEvolution();
     Schema writeSchema = mergeSchema ? mergeAndValidateWriteSchema() : validateWriteSchema();
     SparkUtil.validatePartitionTransforms(table.spec());
     String appId = spark.sparkContext().applicationId();
@@ -188,6 +191,20 @@ class SparkWriteBuilder implements WriteBuilder, SupportsDynamicOverwrite, Suppo
         mode);
   }
 
+  private void validateAutoSchemaEvolution() {
+    if (mergeSchema) {
+      boolean autoSchemaEvolution =
+          PropertyUtil.propertyAsBoolean(
+              table.properties(),
+              TableProperties.SPARK_WRITE_AUTO_SCHEMA_EVOLUTION,
+              TableProperties.SPARK_WRITE_AUTO_SCHEMA_EVOLUTION_DEFAULT);
+      Preconditions.checkArgument(
+          autoSchemaEvolution,
+          "Schema evolution is disabled for this table via '%s'",
+          TableProperties.SPARK_WRITE_AUTO_SCHEMA_EVOLUTION);
+    }
+  }
+
   private Schema validateWriteSchema() {
     Schema writeSchema = SparkSchemaUtil.convert(table.schema(), info.schema(), caseSensitive);
     TypeUtil.validateWriteSchema(table.schema(), writeSchema, checkNullability, checkOrdering);