huangxiaopingRD commented on code in PR #8246:
URL: https://github.com/apache/paimon/pull/8246#discussion_r3427138557


##########
paimon-spark/paimon-spark-common/src/main/java/org/apache/paimon/spark/SparkCatalog.java:
##########
@@ -362,6 +369,36 @@ public org.apache.spark.sql.connector.catalog.Table 
alterTable(
         }
     }
 
+    /**
+     * Detects whether the given changes originate from an {@code ALTER TABLE 
... REPLACE COLUMNS}
+     * statement.
+     *
+     * <p>Spark translates {@code REPLACE COLUMNS} into a batch that drops 
every existing column and
+     * re-adds the new set, i.e. a combination of {@link 
TableChange.DeleteColumn} and {@link
+     * TableChange.AddColumn} only. Other column changes such as rename or 
type update are never
+     * produced by {@code REPLACE COLUMNS}, so we match exclusively on these 
two types to avoid
+     * mistaking a legitimate mixed batch (e.g. a programmatic DROP + RENAME) 
for a replace.
+     *
+     * <p>This operation must be rejected because re-adding columns assigns 
brand-new field ids
+     * while existing data files keep the old ids; same-named columns would 
then be treated as new
+     * columns and read back as null, silently corrupting data.
+     */
+    private boolean isReplaceColumns(TableChange[] changes) {
+        boolean hasDeleteColumn = false;
+        boolean hasAddColumn = false;
+        for (TableChange change : changes) {
+            if (change instanceof TableChange.DeleteColumn) {
+                hasDeleteColumn = true;
+            } else if (change instanceof TableChange.AddColumn) {
+                hasAddColumn = true;
+            } else {
+                return false;
+            }
+        }
+
+        return hasDeleteColumn && hasAddColumn;

Review Comment:
   @JingsongLi  Thank you for your review. I will revise it based on your 
suggestions.



-- 
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]

Reply via email to