Skip to content

JDBC v2: SQLException.getSQLState() returns 22000 (Data Exception) for all server errors, including UNKNOWN_TABLE #3104

Description

@aalopatin

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

  1. Connect via the JDBC v2 driver.
  2. Execute a query against a table that doesn't exist, e.g. SELECT * FROM not_existing_table.
  3. 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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions