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

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


The following commit(s) were added to refs/heads/master by this push:
     new 65067204f [hive] Only support ignoreIfExistsSame for create table in 
usingExternalTable
65067204f is described below

commit 65067204fd16bbe5e73c180d3f1ad33ae282224f
Author: Jingsong <[email protected]>
AuthorDate: Mon Apr 8 15:41:07 2024 +0800

    [hive] Only support ignoreIfExistsSame for create table in 
usingExternalTable
---
 .../org/apache/paimon/schema/SchemaManager.java    | 43 +++++++++++-----------
 .../java/org/apache/paimon/hive/HiveCatalog.java   | 16 +++++---
 .../org/apache/paimon/hive/CreateTableITCase.java  |  6 +--
 3 files changed, 33 insertions(+), 32 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java 
b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java
index 96c9d92c8..d94da91ef 100644
--- a/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java
+++ b/paimon-core/src/main/java/org/apache/paimon/schema/SchemaManager.java
@@ -125,26 +125,30 @@ public class SchemaManager implements Serializable {
         }
     }
 
-    /** Check TableScheme is be modified. */
-    public void checkTableSchema(TableSchema oldSchema, TableSchema newSchema) 
{
-        boolean isCommon =
-                oldSchema.version() == newSchema.version()
-                        && Objects.equals(oldSchema.fields(), 
newSchema.fields())
-                        && oldSchema.highestFieldId() == 
newSchema.highestFieldId()
-                        && Objects.equals(oldSchema.partitionKeys(), 
newSchema.partitionKeys())
-                        && Objects.equals(oldSchema.primaryKeys(), 
newSchema.primaryKeys());
-
-        if (!isCommon) {
-            throw new IllegalStateException(
-                    "Schema in filesystem exists, please use updating,"
-                            + " latest schema is: "
-                            + oldSchema);
-        }
-    }
-
     /** Create a new schema from {@link Schema}. */
     public TableSchema createTable(Schema schema) throws Exception {
+        return createTable(schema, false);
+    }
+
+    public TableSchema createTable(Schema schema, boolean ignoreIfExistsSame) 
throws Exception {
         while (true) {
+            Optional<TableSchema> latest = latest();
+            if (latest.isPresent()) {
+                TableSchema oldSchema = latest.get();
+                boolean isSame =
+                        Objects.equals(oldSchema.fields(), schema.fields())
+                                && Objects.equals(oldSchema.partitionKeys(), 
schema.partitionKeys())
+                                && Objects.equals(oldSchema.primaryKeys(), 
schema.primaryKeys())
+                                && Objects.equals(oldSchema.options(), 
schema.options());
+                if (ignoreIfExistsSame && isSame) {
+                    return oldSchema;
+                }
+
+                throw new IllegalStateException(
+                        "Schema in filesystem exists, please use updating,"
+                                + " latest schema is: "
+                                + oldSchema);
+            }
 
             List<DataField> fields = schema.fields();
             List<String> partitionKeys = schema.partitionKeys();
@@ -162,11 +166,6 @@ public class SchemaManager implements Serializable {
                             options,
                             schema.comment());
 
-            if (latest().isPresent()) {
-                checkTableSchema(latest().get(), newSchema);
-                return newSchema;
-            }
-
             boolean success = commit(newSchema);
             if (success) {
                 return newSchema;
diff --git 
a/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java
 
b/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java
index 372bfedef..32d25e7db 100644
--- 
a/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java
+++ 
b/paimon-hive/paimon-hive-catalog/src/main/java/org/apache/paimon/hive/HiveCatalog.java
@@ -339,6 +339,14 @@ public class HiveCatalog extends AbstractCatalog {
                         () -> new RuntimeException("There is no paimon table 
in " + tableLocation));
     }
 
+    private boolean usingExternalTable() {
+        TableType tableType =
+                OptionsUtils.convertToEnum(
+                        hiveConf.get(TABLE_TYPE.key(), 
TableType.MANAGED.toString()),
+                        TableType.class);
+        return TableType.EXTERNAL.equals(tableType);
+    }
+
     @Override
     protected void dropTableImpl(Identifier identifier) {
         try {
@@ -347,11 +355,7 @@ public class HiveCatalog extends AbstractCatalog {
 
             // When drop a Hive external table, only the hive metadata is 
deleted and the data files
             // are not deleted.
-            TableType tableType =
-                    OptionsUtils.convertToEnum(
-                            hiveConf.get(TABLE_TYPE.key(), 
TableType.MANAGED.toString()),
-                            TableType.class);
-            if (TableType.EXTERNAL.equals(tableType)) {
+            if (usingExternalTable()) {
                 return;
             }
 
@@ -377,7 +381,7 @@ public class HiveCatalog extends AbstractCatalog {
         // if changes on Hive fails there is no harm to perform the same 
changes to files again
         TableSchema tableSchema;
         try {
-            tableSchema = schemaManager(identifier).createTable(schema);
+            tableSchema = schemaManager(identifier).createTable(schema, 
usingExternalTable());
         } catch (Exception e) {
             throw new RuntimeException(
                     "Failed to commit changes of table "
diff --git 
a/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/CreateTableITCase.java
 
b/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/CreateTableITCase.java
index 88c4762ac..15856c3c0 100644
--- 
a/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/CreateTableITCase.java
+++ 
b/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/CreateTableITCase.java
@@ -148,11 +148,9 @@ public class CreateTableITCase extends HiveTestBase {
         hiveCatalog.createTable(identifier, schema, false);
 
         // Drop hive external table
-        String hiveSql = String.join("\n", Arrays.asList("DROP TABLE " + 
tableName));
-        assertThatCode(() -> 
hiveShell.execute(hiveSql)).doesNotThrowAnyException();
+        hiveShell.execute("DROP TABLE " + tableName);
 
-        assertThatCode(() -> hiveCatalog.createTable(identifier, schema, 
false))
-                .doesNotThrowAnyException();
+        hiveCatalog.createTable(identifier, schema, false);
     }
 
     @Test

Reply via email to