Description
SQLException.getSQLState() returns 22000 ("Data exception") when a query references a table that doesn't exist, instead of a SQLSTATE from the 42xxx class ("Syntax error or access rule violation"), which is the conventional/standard mapping for "table not found" errors.
The root cause is in ExceptionUtils.toSqlState() (jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/ExceptionUtils.java), which maps every ServerException to a hardcoded SQL_STATE_DATA_EXCEPTION = "22000", regardless of the underlying ClickHouse error code:
} else if (cause instanceof ServerException) {
return new SQLException(exceptionMessage, SQL_STATE_DATA_EXCEPTION, ((ServerException) cause).getCode(), cause);
}
So for UNKNOWN_TABLE (ClickHouse error code 60), the SQLSTATE comes back as 22000 instead of something like 42S02 (the conventional SQLSTATE many databases/drivers use for "table not found"), which is misleading for tooling/ORMs that branch on SQLSTATE class.
Steps to reproduce
- Connect via the JDBC v2 driver.
- Execute a query against a table that doesn't exist, e.g. SELECT * FROM not_existing_table.
- Catch the resulting SQLException and inspect getSQLState().
Error Log or Exception StackTrace
A query executed on unknown table in DBeaver shows this error:
SQL Error [22000]: Code: 60. DB::Exception: <Unreadable error message> (transport error: 404)
Expected Behaviour
getSQLState() should return a SQLSTATE from the 42xxx class (e.g. 42S02) for UNKNOWN_TABLE, not 22000. 22xxx should be reserved for genuine data-value exceptions (e.g. divide-by-zero, invalid datetime, string truncation), per SQL:2003/SQLSTATE conventions.
Description
SQLException.getSQLState() returns 22000 ("Data exception") when a query references a table that doesn't exist, instead of a SQLSTATE from the 42xxx class ("Syntax error or access rule violation"), which is the conventional/standard mapping for "table not found" errors.
The root cause is in ExceptionUtils.toSqlState() (jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/ExceptionUtils.java), which maps every ServerException to a hardcoded SQL_STATE_DATA_EXCEPTION = "22000", regardless of the underlying ClickHouse error code:
So for UNKNOWN_TABLE (ClickHouse error code 60), the SQLSTATE comes back as 22000 instead of something like 42S02 (the conventional SQLSTATE many databases/drivers use for "table not found"), which is misleading for tooling/ORMs that branch on SQLSTATE class.
Steps to reproduce
Error Log or Exception StackTrace
A query executed on unknown table in DBeaver shows this error:
Expected Behaviour
getSQLState() should return a SQLSTATE from the 42xxx class (e.g. 42S02) for UNKNOWN_TABLE, not 22000. 22xxx should be reserved for genuine data-value exceptions (e.g. divide-by-zero, invalid datetime, string truncation), per SQL:2003/SQLSTATE conventions.