dengzhhu653 commented on code in PR #6383:
URL: https://github.com/apache/hive/pull/6383#discussion_r2987058598


##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/tools/schematool/DbErrorCodes.java:
##########
@@ -0,0 +1,145 @@
+/*
+ * 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.hadoop.hive.metastore.tools.schematool;
+
+import com.google.common.collect.ImmutableSet;
+
+import java.sql.SQLException;
+import java.util.Set;
+
+/**
+ * Per-database ignorable DDL error codes for idempotent schema upgrades.
+ * This is used by {@link IdempotentDDLExecutor} to determine whether a given 
{@link SQLException}
+ * can be safely ignored (e.g. because it indicates an object already exists 
or is already gone).
+ * <p> 
+ * Use {@link #forDbType(String)} to get the right instance.
+ */
+public interface DbErrorCodes {
+
+  /** @return true if the exception can be safely ignored (object already 
exists or already gone). */
+  boolean isIgnorable(SQLException e);
+
+  /**
+   * Checks both {@link SQLException#getSQLState()} and {@code 
String.valueOf(getErrorCode())}
+   * against {@code duplicateCodes} and {@code missingCodes}.
+   */
+  class Codes implements DbErrorCodes {
+    private final Set<String> duplicateCodes;
+    private final Set<String> missingCodes;
+
+    Codes(Set<String> duplicateCodes, Set<String> missingCodes) {
+      this.duplicateCodes = duplicateCodes;
+      this.missingCodes = missingCodes;
+    }
+
+    @Override
+    public boolean isIgnorable(SQLException e) {
+      String state = e.getSQLState();
+      String code = String.valueOf(e.getErrorCode());
+      return duplicateCodes.contains(state) || duplicateCodes.contains(code)
+          || missingCodes.contains(state)   || missingCodes.contains(code);
+    }
+  }
+
+  DbErrorCodes POSTGRES = new Codes(
+      ImmutableSet.of(
+          "42P07",  // duplicate table
+          "42701",  // duplicate column
+          "42710"   // duplicate object (e.g. constraint, index)
+      ),
+      ImmutableSet.of(
+          "42P01",  // undefined table
+          "42703",  // undefined column
+          "42704"   // undefined object
+      )
+  );
+
+  DbErrorCodes DERBY = new Codes(
+      ImmutableSet.of(
+          "X0Y32",  // table/view already exists
+          "X0Y68",  // index already exists
+          "42Z93"   // duplicate constraint (same column set already 
constrained)
+      ),
+      ImmutableSet.of(
+          "42Y55",  // table/view does not exist
+          "42X14",  // column does not exist
+          "42X65",  // index does not exist
+          "42X86"   // constraint does not exist on table (ALTER TABLE DROP 
CONSTRAINT)
+      )
+  );
+
+  DbErrorCodes MYSQL = new Codes(

Review Comment:
   We might only take care of `MySQL` and `Oracle`?



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

Reply via email to