Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#14854] fix(jdbc): Return client errors for MySQL, OceanBase and ClickHouse DDL the source refuses (#14855)

Qi Yuf549bed93735
diff --git a/catalogs-contrib/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/converter/ClickHouseExceptionConverter.java b/catalogs-contrib/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/converter/ClickHouseExceptionConverter.java
index c78ab9cb1c..e379bdd8c2 100644
--- a/catalogs-contrib/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/converter/ClickHouseExceptionConverter.java
+++ b/catalogs-contrib/catalog-jdbc-clickhouse/src/main/java/org/apache/gravitino/catalog/clickhouse/converter/ClickHouseExceptionConverter.java
@@ -18,8 +18,11 @@
*/
package org.apache.gravitino.catalog.clickhouse.converter;
+import com.google.common.collect.ImmutableSet;
import java.sql.SQLException;
+import java.util.Set;
import org.apache.gravitino.catalog.jdbc.converter.JdbcExceptionConverter;
+import org.apache.gravitino.exceptions.BadRequestException;
import org.apache.gravitino.exceptions.GravitinoRuntimeException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.apache.gravitino.exceptions.NoSuchTableException;
@@ -33,6 +36,22 @@ public class ClickHouseExceptionConverter extends JdbcExceptionConverter {
static final int ERROR_CODE_TABLE_ALREADY_EXISTS = 57;
static final int ERROR_CODE_TABLE_IS_DROPPED = 218;
+ /**
+ * ClickHouse error codes treated as request errors. BAD_ARGUMENTS (36) and ILLEGAL_COLUMN (44)
+ * are intentionally included for invalid engine options and column definitions, although they can
+ * also report server-side or generated-query failures. Without operation context, the same
+ * mapping applies to metadata reads. Unknown identifiers (47) and syntax errors (62) remain
+ * excluded because generated statements can also raise them.
+ */
+ private static final Set<Integer> INVALID_REQUEST_CODES =
+ ImmutableSet.of(
+ 6, // CANNOT_PARSE_TEXT, for example a default that does not parse as the column type
+ 36, // BAD_ARGUMENTS, for example ORDER BY on an engine that does not support it
+ 42, // NUMBER_OF_ARGUMENTS_DOESNT_MATCH, for example an engine without its parameters
+ 44, // ILLEGAL_COLUMN, for example a duplicate column
+ 69, // ARGUMENT_OUT_OF_BOUND, for example a precision or scale out of range
+ 80); // INCORRECT_QUERY, for example an engine that cannot be created directly
+
@SuppressWarnings("FormatStringAnnotation")
@Override
public GravitinoRuntimeException toGravitinoException(SQLException sqlException) {
@@ -47,6 +66,9 @@ public class ClickHouseExceptionConverter extends JdbcExceptionConverter {
case ERROR_CODE_TABLE_IS_DROPPED:
return new NoSuchTableException(sqlException, sqlException.getMessage());
default:
+ if (INVALID_REQUEST_CODES.contains(errorCode)) {
+ return new BadRequestException(sqlException, "%s", sqlException.getMessage());
+ }
return new GravitinoRuntimeException(sqlException, sqlException.getMessage());
}
}
diff --git a/catalogs-contrib/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/converter/TestClickHouseExceptionConverter.java b/catalogs-contrib/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/converter/TestClickHouseExceptionConverter.java
new file mode 100644
index 0000000000..d12006d820
--- /dev/null
+++ b/catalogs-contrib/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/converter/TestClickHouseExceptionConverter.java
@@ -0,0 +1,62 @@
+/*
+ * 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.gravitino.catalog.clickhouse.converter;
+
+import java.sql.SQLException;
+import org.apache.gravitino.exceptions.BadRequestException;
+import org.apache.gravitino.exceptions.GravitinoRuntimeException;
+import org.apache.gravitino.exceptions.NoSuchSchemaException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+/** Tests ClickHouse error classification and preservation of source error details. */
+public class TestClickHouseExceptionConverter {
+
+ private final ClickHouseExceptionConverter converter = new ClickHouseExceptionConverter();
+
+ @ParameterizedTest
+ @ValueSource(ints = {6, 36, 42, 44, 69, 80})
+ void testRequestRefusedByClickHouseIsBadRequest(int errorCode) {
+ SQLException failure =
+ new SQLException("Storage Join requires at least 3 parameters", "HY000", errorCode);
+ GravitinoRuntimeException error = converter.toGravitinoException(failure);
+ Assertions.assertInstanceOf(BadRequestException.class, error);
+ Assertions.assertSame(failure, error.getCause());
+ Assertions.assertEquals(failure.getMessage(), error.getMessage());
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = {47, 62})
+ void testErrorsGeneratedStatementsCanRaiseStayServerErrors(int errorCode) {
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class,
+ converter
+ .toGravitinoException(new SQLException("Missing columns: 'x'", "HY000", errorCode))
+ .getClass());
+ }
+
+ @Test
+ void testExistingMappingsAreKept() {
+ Assertions.assertInstanceOf(
+ NoSuchSchemaException.class,
+ converter.toGravitinoException(new SQLException("Database x does not exist", "HY000", 81)));
+ }
+}
diff --git a/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/converter/JdbcExceptionConverter.java b/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/converter/JdbcExceptionConverter.java
index 466dd19664..084970a18b 100644
--- a/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/converter/JdbcExceptionConverter.java
+++ b/catalogs/catalog-jdbc-common/src/main/java/org/apache/gravitino/catalog/jdbc/converter/JdbcExceptionConverter.java
@@ -18,7 +18,10 @@
*/
package org.apache.gravitino.catalog.jdbc.converter;
+import com.google.common.collect.ImmutableSet;
import java.sql.SQLException;
+import java.util.Set;
+import org.apache.gravitino.exceptions.BadRequestException;
import org.apache.gravitino.exceptions.ConnectionFailedException;
import org.apache.gravitino.exceptions.GravitinoRuntimeException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
@@ -29,11 +32,52 @@ import org.apache.gravitino.exceptions.TableAlreadyExistsException;
/** Interface for converter JDBC exceptions to Gravitino exceptions. */
public class JdbcExceptionConverter {
+ /**
+ * MySQL error codes commonly raised when the source refuses requested DDL. Duplicate-entry (1062)
+ * and data conversion/range errors (1264, 1265, 1366, 1406) are included because existing data
+ * can prevent a requested constraint or column type change. Unknown column (1054) and syntax
+ * errors (1064) are left out because generated statements can also raise them.
+ */
+ private static final Set<Integer> INVALID_REQUEST_CODES =
+ ImmutableSet.of(
+ 1059, // identifier name is too long
+ 1060, // duplicate column name
+ 1061, // duplicate key name
+ 1062, // duplicate entry, for example a unique key on duplicate values
+ 1063, // incorrect column specifier, for example AUTO_INCREMENT on a string column
+ 1067, // invalid default value
+ 1068, // multiple primary key defined
+ 1072, // key column doesn't exist in table
+ 1074, // column length too big
+ 1075, // incorrect table definition; only one auto column, which must be a key
+ 1091, // can't drop; check that the column or key exists
+ 1101, // BLOB, TEXT, GEOMETRY or JSON column can't have a default value
+ 1113, // a table must have at least 1 column
+ 1117, // too many columns
+ 1118, // row size too large
+ 1138, // invalid use of NULL value, for example NOT NULL on a column with NULLs
+ 1166, // incorrect column name
+ 1170, // BLOB/TEXT column used in key specification without a key length
+ 1171, // all parts of a PRIMARY KEY must be NOT NULL
+ 1264, // out of range value
+ 1265, // data truncated
+ 1280, // incorrect index name
+ 1366, // incorrect value for the column
+ 1406, // data too long for column
+ 1425, // too big scale
+ 1426, // too big precision
+ 3730, // cannot drop a table referenced by a foreign key constraint
+ 3780); // referencing and referenced columns in a foreign key are incompatible
+
/**
* Convert JDBC exception to GravitinoException. The default implementation of this method is
* based on MySQL exception code, and if the catalog does not compatible with MySQL exception
* code, this method needs to be rewritten.
*
+ * <p>This converter is shared by DDL and metadata reads. Classification uses source error codes
+ * without operation context, so a bad-request result does not establish whether user input,
+ * generated SQL, or the source server caused the failure.
+ *
* @param sqlException The sql exception to map
* @return The best attempt at a corresponding connector exception or generic with the
* SQLException as the cause
@@ -51,10 +95,15 @@ public class JdbcExceptionConverter {
case 1051:
return new NoSuchTableException(sqlException, "%s", sqlException.getMessage());
default:
+ if (INVALID_REQUEST_CODES.contains(sqlException.getErrorCode())) {
+ return new BadRequestException(sqlException, "%s", sqlException.getMessage());
+ }
if (sqlException.getMessage() != null
&& sqlException.getMessage().contains("Access denied")) {
return new ConnectionFailedException(sqlException, "%s", sqlException.getMessage());
}
+ // Keep lock timeouts (1205) and deadlocks (1213) as generic failures: HTTP 503 can
+ // trigger automatic retries of DDL requests that have already partially committed.
return new GravitinoRuntimeException(sqlException, "%s", sqlException.getMessage());
}
}
diff --git a/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/converter/TestJdbcExceptionConverter.java b/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/converter/TestJdbcExceptionConverter.java
index d90fa77760..8d4dad0b7b 100644
--- a/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/converter/TestJdbcExceptionConverter.java
+++ b/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/converter/TestJdbcExceptionConverter.java
@@ -19,6 +19,7 @@
package org.apache.gravitino.catalog.jdbc.converter;
import java.sql.SQLException;
+import org.apache.gravitino.exceptions.BadRequestException;
import org.apache.gravitino.exceptions.ConnectionFailedException;
import org.apache.gravitino.exceptions.GravitinoRuntimeException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
@@ -56,4 +57,38 @@ public class TestJdbcExceptionConverter {
new JdbcExceptionConverter()
.toGravitinoException(new SQLException("Unknown database 'x%'", "42000", 1049)));
}
+
+ @ParameterizedTest
+ @ValueSource(
+ ints = {
+ 1059, 1060, 1061, 1062, 1063, 1067, 1068, 1072, 1074, 1075, 1091, 1101, 1113, 1117, 1118,
+ 1138, 1166, 1170, 1171, 1264, 1265, 1280, 1366, 1406, 1425, 1426, 3730, 3780
+ })
+ void testRequestRefusedBySourceIsBadRequest(int errorCode) {
+ SQLException failure = new SQLException("refused at 100%", "42000", errorCode);
+ GravitinoRuntimeException error = new JdbcExceptionConverter().toGravitinoException(failure);
+ Assertions.assertInstanceOf(BadRequestException.class, error);
+ Assertions.assertSame(failure, error.getCause());
+ Assertions.assertEquals(failure.getMessage(), error.getMessage());
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = {1205, 1213})
+ void testLockTimeoutAndDeadlockStayServerErrors(int errorCode) {
+ SQLException failure = new SQLException("Lock failure at 100%", "HY000", errorCode);
+ GravitinoRuntimeException error = new JdbcExceptionConverter().toGravitinoException(failure);
+ Assertions.assertEquals(GravitinoRuntimeException.class, error.getClass());
+ Assertions.assertSame(failure, error.getCause());
+ Assertions.assertEquals(failure.getMessage(), error.getMessage());
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = {1054, 1064})
+ void testErrorsGeneratedStatementsCanRaiseStayServerErrors(int errorCode) {
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class,
+ new JdbcExceptionConverter()
+ .toGravitinoException(new SQLException("Unknown column 'x'", "42S22", errorCode))
+ .getClass());
+ }
}