You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[jdbc-v2] Handle query timeout in the middle of stream #3077
api-diagnosticsArea: public API shape, error reporting, logging, metrics, tracingbugclientModule: the client (client-v2; with legacy: the v1 client)jdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)
Query may have execution timeout limit on server. In this case exception will be just SQLException not a SQLTimeoutException.
JDBC do not make driver throw SQLTimeoutException in this case but it would be good to fix for consistency and better error handling.
My plan is to trace how a server-side execution timeout propagates through jdbc-v2 after the response stream has already been opened, and add a regression test where ClickHouse error 159 is received while consuming the ResultSet.
I will then update the JDBC exception mapping so this path throws SQLTimeoutException while preserving the original cause and vendor error code. I will also verify whether synchronous and asynchronous operations share this path and cover both where applicable.
We need verification test in client-v2 to record in tests that we handle timeout while reading stream correctly (may be specific exception would be needed because we want to add timeout to fault cause)
JDBC the main fix - just another test and proper exception wrapping/handling
Recommendation: Keep open — fix needed
A server timeout after the first rows loses error 159 completely, not only the exception type. Open PR #3097 fixes it at its own head (SQLTimeoutException, HYT00, code 159), but it does not apply to main, its base cannot read the compressed responses of server 26.11, and the maintainer paused it on 2026-09-24.
Category: Bug
Classification: impact: none · area: API & diagnostics (mid-stream errors)
Status on main: Reproduced on main — a timeout after the first rows gives SQLException (HY000, code 0) "Failed to read value for column s"; a timeout before the rows gives SQLTimeoutException.
Breaking change: No — SQLTimeoutException is a subclass of SQLException.
Workaround: with clickhouse_setting_wait_end_of_query=1, the server sends the error before the rows, and all three cases give SQLTimeoutException.
Analysis: On main, client-v2 does not read the error that the server appends to an HTTP 200 response (#2702). The reader gets a truncated chunked body and throws a generic read error (BinaryStreamReader.java:307). StatementImpl.java:378 maps error 159 to SQLTimeoutException only when it arrives before the rows.
Reproduction: complete program and output
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class Repro {
static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
// About 1 MB of rows each 0.2 s: rows arrive before the server stops the query after 1 s.
static final String STREAMING = "SELECT number, randomPrintableASCII(1000) AS s, sleepEachRow(0.0002)"
+ " FROM numbers(100000) SETTINGS max_block_size = 1000";
// No row arrives before the timeout.
static final String AGGREGATE = "SELECT sum(sleepEachRow(0.01)) FROM numbers(1000) SETTINGS max_block_size = 10";
static void runCase(Connection conn, String name, String sql, boolean queryTimeout) {
int rows = 0;
String stage = "executeQuery";
try (Statement st = conn.createStatement()) {
if (queryTimeout) st.setQueryTimeout(1);
String q = queryTimeout ? sql : sql + ", max_execution_time = 1";
try (ResultSet rs = st.executeQuery(q)) {
stage = "next";
while (rs.next()) rows++;
}
System.out.println(name + ": no error, rows=" + rows);
} catch (SQLException e) {
String msg = String.valueOf(e.getMessage());
System.out.println(name + ": " + stage + " after " + rows + " rows: " + e.getClass().getName()
+ " (SQLState " + e.getSQLState() + ", code " + e.getErrorCode() + "): "
+ (msg.length() > 90 ? msg.substring(0, 90) + "…" : msg));
for (Throwable c = e.getCause(); c != null; c = c.getCause()) {
String m = String.valueOf(c.getMessage());
System.out.println(" caused by " + c.getClass().getName() + ": " + (m.length() > 90 ? m.substring(0, 90) + "…" : m));
}
}
}
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.setProperty("user", "default");
props.setProperty("password", PASSWORD);
String jdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
try (Connection conn = DriverManager.getConnection(jdbcUrl, props)) {
runCase(conn, "setQueryTimeout(1), timeout mid-stream ", STREAMING, true);
runCase(conn, "max_execution_time=1, timeout mid-stream ", STREAMING, false);
runCase(conn, "setQueryTimeout(1), timeout before stream ", AGGREGATE, true);
}
}
}
Output on main (8480ca001), ClickHouse 26.11.2.4:
setQueryTimeout(1), timeout mid-stream : next after 4000 rows: java.sql.SQLException (SQLState HY000, code 0): Failed to read value for column s
caused by com.clickhouse.client.api.ClientException: Failed to read value for column s
caused by org.apache.hc.core5.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
max_execution_time=1, timeout mid-stream : next after 4000 rows: java.sql.SQLException (SQLState HY000, code 0): Failed to read value for column s
caused by com.clickhouse.client.api.ClientException: Failed to read value for column s
caused by org.apache.hc.core5.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
setQueryTimeout(1), timeout before stream : executeQuery after 0 rows: java.sql.SQLTimeoutException (SQLState null, code 0): Query execution time exceeded limit (queryId=…)
caused by com.clickhouse.client.api.ServerException: Code: 159. DB::Exception: Timeout exceeded: elapsed 1002.399 ms, maximum: 1000.000 ms. (TI…
Output with PR #3097 at its head (7429ce0a0). This run adds compress=false to the properties, because the PR base cannot read the compressed responses of server 26.11. The PR base is older than #3147, so setQueryTimeout sets no server limit there, and only the max_execution_time case applies:
setQueryTimeout(1), timeout mid-stream : no error, rows=100000
max_execution_time=1, timeout mid-stream : next after 4000 rows: java.sql.SQLTimeoutException (SQLState HYT00, code 159): Code: 159. DB::Exception: Timeout exceeded: elapsed 1003.858 ms, maximum: 1000.000 ms. (TI…
setQueryTimeout(1), timeout before stream : no error, rows=1
Automated triage by Polyglot AI. A maintainer makes the final decision.
api-diagnosticsArea: public API shape, error reporting, logging, metrics, tracingbugclientModule: the client (client-v2; with legacy: the v1 client)jdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)
Activity
jjj-n commentedon Sep 1, 2026
Hi @chernser, I'd like to work on this issue.
My plan is to trace how a server-side execution timeout propagates through jdbc-v2 after the response stream has already been opened, and add a regression test where ClickHouse error 159 is received while consuming the ResultSet.
I will then update the JDBC exception mapping so this path throws SQLTimeoutException while preserving the original cause and vendor error code. I will also verify whether synchronous and asynchronous operations share this path and cover both where applicable.
Does this scope match what you expect for #3077?
chernser commentedon Sep 1, 2026
Good day, @jjj-n !
Sure you can take it!
Thanks!
polyglotAI-bot commentedon Nov 27, 2026
Recommendation: Keep open — fix needed
A server timeout after the first rows loses error 159 completely, not only the exception type. Open PR #3097 fixes it at its own head (
SQLTimeoutException, HYT00, code 159), but it does not apply tomain, its base cannot read the compressed responses of server 26.11, and the maintainer paused it on 2026-09-24.main: Reproduced on main — a timeout after the first rows givesSQLException(HY000, code 0) "Failed to read value for column s"; a timeout before the rows givesSQLTimeoutException.SQLTimeoutExceptionis a subclass ofSQLException.clickhouse_setting_wait_end_of_query=1, the server sends the error before the rows, and all three cases giveSQLTimeoutException.Analysis: On
main, client-v2 does not read the error that the server appends to an HTTP 200 response (#2702). The reader gets a truncated chunked body and throws a generic read error (BinaryStreamReader.java:307).StatementImpl.java:378maps error 159 toSQLTimeoutExceptiononly when it arrives before the rows.Reproduction: complete program and output
Output on
main(8480ca001), ClickHouse 26.11.2.4:Output with PR #3097 at its head (
7429ce0a0). This run addscompress=falseto the properties, because the PR base cannot read the compressed responses of server 26.11. The PR base is older than #3147, sosetQueryTimeoutsets no server limit there, and only themax_execution_timecase applies:Automated triage by Polyglot AI. A maintainer makes the final decision.