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

j1wonpark pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/amoro.git


The following commit(s) were added to refs/heads/master by this push:
     new abf3b4013 [AMORO-4419][Common] Correct nullability in table-operation 
DDL (#4421)
abf3b4013 is described below

commit abf3b40131057d614c073d29cfc0c2274645808a
Author: hutiefang76 <[email protected]>
AuthorDate: Fri Oct 16 18:07:05 2026 -0700

    [AMORO-4419][Common] Correct nullability in table-operation DDL (#4421)
    
    Correct nullability in table-operation DDL
    
    Co-authored-by: Jiwon Park <[email protected]>
---
 .../descriptor/SparkMetadataChangeHandler.java     |  2 +-
 .../descriptor/TestServerTableDescriptor.java      |  2 +-
 .../descriptor/TestSparkMetadataChangeHandler.java | 88 ++++++++++++++++++++++
 3 files changed, 90 insertions(+), 2 deletions(-)

diff --git 
a/amoro-common/src/main/java/org/apache/amoro/table/descriptor/SparkMetadataChangeHandler.java
 
b/amoro-common/src/main/java/org/apache/amoro/table/descriptor/SparkMetadataChangeHandler.java
index 7f9870f60..fae10d578 100644
--- 
a/amoro-common/src/main/java/org/apache/amoro/table/descriptor/SparkMetadataChangeHandler.java
+++ 
b/amoro-common/src/main/java/org/apache/amoro/table/descriptor/SparkMetadataChangeHandler.java
@@ -86,7 +86,7 @@ public class SparkMetadataChangeHandler implements 
MetadataChangeHandler {
 
   @Override
   public String changeColumnsRequire(String columnName, boolean required) {
-    String template = ALTER_TABLE + ALTER_COLUMN + (required ? IS_OPTIONAL : 
NOT_OPTIONAL);
+    String template = ALTER_TABLE + ALTER_COLUMN + (required ? NOT_OPTIONAL : 
IS_OPTIONAL);
     return String.format(template, tableName, columnName);
   }
 
diff --git 
a/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestServerTableDescriptor.java
 
b/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestServerTableDescriptor.java
index ef6f4d262..f099b1b87 100644
--- 
a/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestServerTableDescriptor.java
+++ 
b/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestServerTableDescriptor.java
@@ -146,7 +146,7 @@ public abstract class TestServerTableDescriptor {
         tableOperations
             .get(6)
             .getDdl()
-            .equalsIgnoreCase("ALTER TABLE test_table ALTER COLUMN renamed_col 
DROP NOT NULL"));
+            .equalsIgnoreCase("ALTER TABLE test_table ALTER COLUMN renamed_col 
SET NOT NULL"));
 
     Assert.assertTrue(
         tableOperations
diff --git 
a/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestSparkMetadataChangeHandler.java
 
b/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestSparkMetadataChangeHandler.java
new file mode 100644
index 000000000..728a7c679
--- /dev/null
+++ 
b/amoro-common/src/test/java/org/apache/amoro/table/descriptor/TestSparkMetadataChangeHandler.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.amoro.table.descriptor;
+
+import org.apache.amoro.table.TableIdentifier;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Guards the nullability contract of the reversed DDL: {@code required == 
true} means the column is
+ * NOT NULL, so becoming required must produce {@code SET NOT NULL} and 
becoming optional must
+ * produce {@code DROP NOT NULL}.
+ */
+public class TestSparkMetadataChangeHandler {
+
+  private static final String TABLE_NAME = "tbl";
+
+  @Test
+  public void changeColumnsRequireToRequired() {
+    SparkMetadataChangeHandler handler = new 
SparkMetadataChangeHandler(TABLE_NAME);
+    Assertions.assertEquals(
+        "ALTER TABLE tbl ALTER COLUMN c SET NOT NULL", 
handler.changeColumnsRequire("c", true));
+  }
+
+  @Test
+  public void changeColumnsRequireToOptional() {
+    SparkMetadataChangeHandler handler = new 
SparkMetadataChangeHandler(TABLE_NAME);
+    Assertions.assertEquals(
+        "ALTER TABLE tbl ALTER COLUMN c DROP NOT NULL", 
handler.changeColumnsRequire("c", false));
+  }
+
+  @Test
+  public void ddlReverserOptionalToRequired() {
+    List<DDLInfo> ddls = reverseColumnRequired(false, true);
+    Assertions.assertEquals(1, ddls.size());
+    Assertions.assertEquals("ALTER TABLE tbl ALTER COLUMN c SET NOT NULL", 
ddls.get(0).getDdl());
+  }
+
+  @Test
+  public void ddlReverserRequiredToOptional() {
+    List<DDLInfo> ddls = reverseColumnRequired(true, false);
+    Assertions.assertEquals(1, ddls.size());
+    Assertions.assertEquals("ALTER TABLE tbl ALTER COLUMN c DROP NOT NULL", 
ddls.get(0).getDdl());
+  }
+
+  /** Drives {@link DDLReverser} with a fake extractor holding two metadata 
snapshots. */
+  private static List<DDLInfo> reverseColumnRequired(boolean preRequired, 
boolean currentRequired) {
+    TableMetaExtract<String> extractor =
+        table -> {
+          TableMetaExtract.InternalTableMeta pre =
+              new TableMetaExtract.InternalTableMeta(
+                  0L, Collections.singletonList(schema(0, preRequired)), 
Collections.emptyMap());
+          TableMetaExtract.InternalTableMeta current =
+              new TableMetaExtract.InternalTableMeta(
+                  1L,
+                  Collections.singletonList(schema(0, currentRequired)),
+                  Collections.emptyMap());
+          return Arrays.asList(pre, current);
+        };
+
+    return new DDLReverser<>(extractor)
+        .reverse("table", TableIdentifier.of("catalog", "db", TABLE_NAME));
+  }
+
+  private static TableMetaExtract.InternalSchema schema(int id, boolean 
required) {
+    return new TableMetaExtract.InternalSchema(id, null, "c", "int", null, 
required);
+  }
+}

Reply via email to