Skip to content

fix(jdbc-v2): throw SQLException when the writer schema lookup fails in prepareStatement - #3390

Open
rohan4naik wants to merge 1 commit into
ClickHouse:mainfrom
rohan4naik:fix/prepare-statement-sqlexception-3326
Open

rohan4naik wants to merge 1 commit into
ClickHouse:mainfrom
rohan4naik:fix/prepare-statement-sqlexception-3326

Conversation

@rohan4naik

@rohan4naik rohan4naik commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

With beta.row_binary_for_simple_insert=true, Connection#prepareStatement() for a simple INSERT ... VALUES (?) asks the server for the table schema. When that lookup failed, the unchecked client-v2 exception (ServerException or ClientException) left prepareStatement() as it was, although the method declares throws SQLException.

Cause: ConnectionImpl.prepareStatement(String, int, int, int) called client.getTableSchema(table, schema) without a try/catch.

Fix: catch the failure of that call and re-throw it through ExceptionUtils.toSqlState(e), the conversion the other jdbc-v2 call sites already use (for example WriterStatementImpl#executeLargeUpdate). An unknown table now gives SQL state 42S02 with error code 60, and other server errors give 22000 with the server code. The original exception stays as the cause.

I kept the conversion in jdbc-v2 and did not touch Client#getTableSchema, because client-v2 callers rely on its unchecked exceptions. Nothing changes when the lookup succeeds, and nothing changes with the writer off (no server call in prepareStatement).

Compatibility: code that caught ServerException / ClientException from prepareStatement() with the beta writer on now gets SQLException with that exception as the cause.

Testing

  • New JDBCErrorHandlingTests#testWriterSchemaLookupFailureThrowsSQLException (unknown table, unknown database). It fails without the fix ("Expected SQLException to be thrown, but ServerException was thrown") and passes with it.
  • Ran JDBCErrorHandlingTests and WriterStatementImplTest against a local ClickHouse 26.11 server (TEST_CLICKHOUSE_CONTAINER=none): all pass. I could not run the Docker-based setup locally, so I did not run the whole integration suite; the ConnectionTest / PreparedStatementTest failures I saw locally (no default cluster, no TLS endpoint, non-UTC machine time zone) occur with and without this change.

This change was AI-assisted.

Closes #3326

Checklist


Note

Low Risk
Narrow error-handling change on a beta code path; behavior only shifts from unchecked exceptions to SQLException with preserved causes.

Overview
With beta.row_binary_for_simple_insert=true, Connection#prepareStatement() on simple INSERT statements resolves table schema via the server; failures used to escape as unchecked ServerException / ClientException instead of SQLException.

ConnectionImpl now wraps client.getTableSchema() in try/catch and rethrows through ExceptionUtils.toSqlState(), aligning SQL state and error codes with the rest of jdbc-v2 (e.g. unknown table → 42S02). Successful lookups and the non-writer path are unchanged.

Integration coverage adds testWriterSchemaLookupFailureThrowsSQLException for unknown table and unknown database; a changelog entry documents the fix for #3326.

Reviewed by Cursor Bugbot for commit 14ba55b. Bugbot is set up for automated code reviews on this repo. Configure here.

…in prepareStatement

With beta.row_binary_for_simple_insert=true, prepareStatement() calls
Client#getTableSchema. Its unchecked ServerException / ClientException left
prepareStatement() unchanged. Convert it with ExceptionUtils.toSqlState, as
the other jdbc-v2 call sites do.

Closes ClickHouse#3326
@rohan4naik
rohan4naik requested review from a team and mzitnik as code owners November 19, 2026 05:53
Copilot AI balanced review requested due to automatic review settings November 19, 2026 05:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[jdbc-v2] prepareStatement() throws unchecked ServerException/ClientException, not SQLException, when the RowBinary writer schema lookup fails

2 participants