This is an automated email from the ASF dual-hosted git repository.
jerryshao pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git
The following commit(s) were added to refs/heads/main by this push:
new f549bed937 [#14854] fix(jdbc): Return client errors for MySQL,
OceanBase and ClickHouse DDL the source refuses (#14855)
f549bed937 is described below
commit f549bed9373577a036095989de6f987cec474116
Author: Qi Yu <[email protected]>
AuthorDate: Wed Nov 11 12:12:01 2026 +0800
[#14854] fix(jdbc): Return client errors for MySQL, OceanBase and
ClickHouse DDL the source refuses (#14855)
### What changes were proposed in this pull request?
- `JdbcExceptionConverter` (used by MySQL and OceanBase) classifies
these source error codes as `BadRequestException` (HTTP 400): 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.
- `ClickHouseExceptionConverter` maps 6, 36, 42, 44, 69 and 80 to
`BadRequestException`.
- Unknown column/identifier (MySQL 1054, ClickHouse 47) and syntax
errors (MySQL 1064, ClickHouse 62) remain server errors, because
generated statements can also raise them.
- Lock timeouts and deadlocks (MySQL 1205, 1213) remain HTTP 500.
Returning 503 can trigger automatic client retries of DDL requests that
have already partially committed.
The converters also handle metadata reads and have no operation context.
MySQL 1062 and 1264/1265/1366/1406 are retained for constraint and type
changes; ClickHouse 36/44 are retained for engine options and column
definitions. These broad codes can also originate from generated SQL or
server-side failures, so HTTP 400 is a deliberate best-effort
classification, not proof of invalid user input. Current MySQL/OceanBase
non-DDL callers use JDBC metadata, `SHOW`, and `information_schema`
queries; Gravitino's own metadata-store writes use separate SQL
exception converters.
### Why are the changes needed?
Requests the source refuses because of the request itself were returned
as HTTP 500.
Fix #14854
### Does this PR introduce _any_ user-facing change?
The mapped source error codes return HTTP 400 instead of 500, retaining
the source error message.
### How was this patch tested?
- 48 converter tests passed for the comment-only follow-up. The
preceding behavioral revision passed 240 targeted tests across JDBC
common, MySQL, OceanBase, ClickHouse, server exception handling and Java
client error handling.
- Fault injection through OceanBase's actual ALTER loop, REST exception
handling and the Java HTTP client: for both 1205 and 1213 on the second
DDL statement, the request is sent once and returns the original error
as HTTP 500. The previous implementation sent the request twice and
returned a duplicate-column error.
- The lock-error regression test fails with the previous converter and
passes with this revision for both error codes.
- Live database integration tests were not rerun for this revision.
---
.../converter/ClickHouseExceptionConverter.java | 22 ++++++++++
.../TestClickHouseExceptionConverter.java | 39 +++++++++--------
.../jdbc/converter/JdbcExceptionConverter.java | 49 ++++++++++++++++++++++
.../jdbc/converter/TestJdbcExceptionConverter.java | 35 ++++++++++++++++
4 files changed, 127 insertions(+), 18 deletions(-)
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/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/converter/TestJdbcExceptionConverter.java
b/catalogs-contrib/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/converter/TestClickHouseExceptionConverter.java
similarity index 54%
copy from
catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/converter/TestJdbcExceptionConverter.java
copy to
catalogs-contrib/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/converter/TestClickHouseExceptionConverter.java
index d90fa77760..d12006d820 100644
---
a/catalogs/catalog-jdbc-common/src/test/java/org/apache/gravitino/catalog/jdbc/converter/TestJdbcExceptionConverter.java
+++
b/catalogs-contrib/catalog-jdbc-clickhouse/src/test/java/org/apache/gravitino/catalog/clickhouse/converter/TestClickHouseExceptionConverter.java
@@ -16,10 +16,10 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.gravitino.catalog.jdbc.converter;
+package org.apache.gravitino.catalog.clickhouse.converter;
import java.sql.SQLException;
-import org.apache.gravitino.exceptions.ConnectionFailedException;
+import org.apache.gravitino.exceptions.BadRequestException;
import org.apache.gravitino.exceptions.GravitinoRuntimeException;
import org.apache.gravitino.exceptions.NoSuchSchemaException;
import org.junit.jupiter.api.Assertions;
@@ -27,33 +27,36 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
-/** Source messages are literal text, not format templates. */
-public class TestJdbcExceptionConverter {
+/** Tests ClickHouse error classification and preservation of source error
details. */
+public class TestClickHouseExceptionConverter {
- @Test
- void testAccessDeniedForAnyHostKeepsSourceMessage() {
+ private final ClickHouseExceptionConverter converter = new
ClickHouseExceptionConverter();
+
+ @ParameterizedTest
+ @ValueSource(ints = {6, 36, 42, 44, 69, 80})
+ void testRequestRefusedByClickHouseIsBadRequest(int errorCode) {
SQLException failure =
- new SQLException("Access denied for user 'gravitino'@'%' to database
'db'", "42000", 1044);
- GravitinoRuntimeException error = new
JdbcExceptionConverter().toGravitinoException(failure);
- Assertions.assertInstanceOf(ConnectionFailedException.class, error);
+ 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 = {1007, 1050, 1049, 1146, 0})
- void testEveryBranchKeepsPercentSignsLiteral(int errorCode) {
- SQLException failure = new SQLException("object 'a%'b' failed at 100%",
"HY000", errorCode);
- GravitinoRuntimeException error = new
JdbcExceptionConverter().toGravitinoException(failure);
- Assertions.assertSame(failure, error.getCause());
- Assertions.assertEquals(failure.getMessage(), error.getMessage());
+ @ValueSource(ints = {47, 62})
+ void testErrorsGeneratedStatementsCanRaiseStayServerErrors(int errorCode) {
+ Assertions.assertEquals(
+ GravitinoRuntimeException.class,
+ converter
+ .toGravitinoException(new SQLException("Missing columns: 'x'",
"HY000", errorCode))
+ .getClass());
}
@Test
- void testMissingSchemaIsStillClassified() {
+ void testExistingMappingsAreKept() {
Assertions.assertInstanceOf(
NoSuchSchemaException.class,
- new JdbcExceptionConverter()
- .toGravitinoException(new SQLException("Unknown database 'x%'",
"42000", 1049)));
+ 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());
+ }
}