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 does not recognize CH errors in the middle of a result #650
api-diagnosticsArea: public API shape, error reporting, logging, metrics, tracingblocked-by-clickhousebugclientModule: the client (client-v2; with legacy: the v1 client)connectivityArea: connection, TLS, pool, timeouts, cancellation, transport, compressionjdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)legacyOnly the v1 stack (clickhouse-client, clickhouse-http-client, clickhouse-jdbc) has the problem
SELECT toString(cityHash64(number))
FROM numbers(10000000000)
where intDiv(1,number-100000)>-1000000000000000
....
1533576702855133923512760187302687605805111004388078728081347082324503662672898950872048444443462125951623157557271411535188035207759932513448237354462061702293231705189966434545137479462205503762548036246277231731
Code: 153, e.displayText() = DB::Exception: Division by zero (version 20.8.18.32 (official build))
Now the error is just one more value. No JAVA exception.
Thanks @den-crane. I was able to reproduce the issue on 21.3 as well. I think the problem is server responded 200, so unless we parse each line of the result(regardless what the output format is), we'll run into this kind of issue.
I also tried different output format and multiple columns, but unfortunately they all have the same issue.
With http response 200, error only happens when 1) there's network issue(IOException); or 2) JDBC driver failed to parse value(e.g. trying the parse the error message as a number). As a result, when output format is TabSeparated*(or similar text-based format) and the ResultSet only contains one column, and either the column type is String or we use resultSet.getString() to retrieve value, there will be no exception.
This cannot be fixed easily on client side without changing output format, because user can write arbitrary SQL - it's hard to tell if there's error or not from result of below SQL:
select'1'as a
union allselect'Code: 153, e.displayText() = DB::Exception: Division by zero: while executing ''FUNCTION intDiv(1 :: 1, minus(number, 100000) :: 4) -> intDiv(1, minus(number, 100000)) Int8 : 2'' (version 21.3.9.83 (official build))'
@den-crane suggested to look into ClickHouse/ClickHouse#21136. Before http interface is enhanced, I hope switching to RowBinary format can fix the issue from client side.
In 0.3.1 and earlier version, query SELECT '2' FROM system.numbers where intDiv(1,number-100000)>-100000000000000 will return below result without any exception - the error message became last line of result set:
SELECT toString(cityHash64(number)) FROM numbers(10000000000) where intDiv(1,number-1000000)>-1000000000000000
java.io.IOException: Reached end of input stream after reading 22 of 51 bytes
Thanks @den-crane. The inner exception should be changed to StreamCorruptedException as it's more specific. There's buffer(size = 4096 but it's customizable) in ClickHouseInputStream, so if there's no fake error(rare but possible) and the error message is not too long, I should be able to extract the error.
Using curl:
echo"SELECT toString(cityHash64(number)) FROM numbers(10000000000) where intDiv(1,number-1000000)>-1000000000000000"| curl -X POST 'http://localhost:8123/' --data-binary @-
...
18400166420351108711
Code: 153. DB::Exception: Division by zero: while executing 'FUNCTION intDiv(1 :: 1, minus(number, 1000000) :: 4) -> intDiv(1, minus(number, 1000000)) Int8 : 2'. (ILLEGAL_DIVISION) (version 22.8.13.20 (official build))
Using Java:
java.io.UncheckedIOException: Failed to read column #1 of 1: toString(cityHash64(number)) String
at com.clickhouse.data.ClickHouseDataProcessor.nextRecord(ClickHouseDataProcessor.java:142)
at com.clickhouse.data.ClickHouseDataProcessor.access$000(ClickHouseDataProcessor.java:23)
at com.clickhouse.data.ClickHouseDataProcessor$RecordsIterator.next(ClickHouseDataProcessor.java:49)
at com.clickhouse.data.ClickHouseDataProcessor$RecordsIterator.next(ClickHouseDataProcessor.java:35)
at com.clickhouse.examples.jdbc.Test.client(Test.java:332)
at com.clickhouse.examples.jdbc.Test.main(Test.java:578)
Caused by: java.io.IOException: Reached end of input stream after reading 22 of 51 bytes
at com.clickhouse.data.stream.AbstractByteArrayInputStream.readBytes(AbstractByteArrayInputStream.java:236)
at com.clickhouse.data.stream.AbstractByteArrayInputStream.readBuffer(AbstractByteArrayInputStream.java:163)
at com.clickhouse.data.ClickHouseInputStream.readString(ClickHouseInputStream.java:1025)
at com.clickhouse.data.ClickHouseInputStream.readUnicodeString(ClickHouseInputStream.java:1061)
at com.clickhouse.data.format.BinaryDataProcessor.readTextString(BinaryDataProcessor.java:668)
at com.clickhouse.data.format.ClickHouseRowBinaryProcessor.readAndFill(ClickHouseRowBinaryProcessor.java:249)
at com.clickhouse.data.ClickHouseDataProcessor.nextRecord(ClickHouseDataProcessor.java:132)
... 5 more
Recommendation: Keep open — fix needed
jdbc-v2, client-v2, and the v1 driver lose a server error that comes after HTTP 200. Open PR #3097 fixes only client-v2 and jdbc-v2 (it closes #2702), and its diff does not apply to main. This issue is the only one for the v1 driver.
Category: Bug
Classification: impact: none · area: API & diagnostics (mid-stream errors)
Status on main: Reproduced on main — after about 7.8 million rows, all three throw a read error with error code 0, and the server error (code 153) is lost. The error text no longer becomes a data row.
Breaking change: No — can be done without a break.
Workaround:wait_end_of_query=1 (clickhouse_setting_wait_end_of_query=1 in the jdbc-v2 URL, QuerySettings.waitEndOfQuery(true) in client-v2) gives the server error (contrast case), but the server then sends no row until the query ends.
Analysis: The server part is done: ClickHouse/ClickHouse#75175 closed with ClickHouse/ClickHouse#88818 (first in 25.11). After HTTP 200, the server now sends the header X-ClickHouse-Exception-Tag, ends the body with a tagged __exception__ frame, and cuts the connection. main does not read that header: client-v2 checks only X-ClickHouse-Exception-Code (HttpAPIClientHelper.java:848), and BinaryStreamReader.java:307 reports the cut stream as a column read error. The v1 transport also checks only that header (ApacheHttpConnectionImpl.java:215-216).
Reproduction: complete program and output
Changed from the issue's steps: the error at number 10000000 in place of 1000000, so that the server always sends HTTP 200 first (with 1000000 the error sometimes comes before it); one case for each module, and a contrast case.
importcom.clickhouse.client.api.Client;
importcom.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
importcom.clickhouse.client.api.query.QueryResponse;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.Properties;
publicclassRepro {
staticfinalStringURL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
staticfinalStringPASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
// The issue's query; the server fails with "Division by zero" (code 153) at number 10000000,// after it has sent HTTP 200 and some million rows.staticfinalStringSQL = "SELECT toString(cityHash64(number)) FROM numbers(10000000000)"
+ " where intDiv(1,number-10000000)>-1000000000000000";
staticStringdescribe(Throwablee) {
Strings = e.getClass().getSimpleName();
if (einstanceofSQLException) s += " (errorCode " + ((SQLException) e).getErrorCode() + ")";
Stringm = String.valueOf(e.getMessage());
s += ": " + (m.length() > 120 ? m.substring(0, 120) + "…" : m);
if (e.getCause() != null) s += " | cause " + e.getCause().getClass().getSimpleName() + ": " + e.getCause().getMessage();
returns;
}
staticvoidjdbc(Stringlabel, StringurlParams) throwsException {
Propertiesprops = newProperties();
props.setProperty("user", "default");
props.setProperty("password", PASSWORD);
StringjdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default" + urlParams;
try (Connectionconn = DriverManager.getConnection(jdbcUrl, props); Statementst = conn.createStatement()) {
longrows = 0;
try (ResultSetrs = st.executeQuery(SQL)) {
while (rs.next()) { rs.getString(1); rows++; }
System.out.println(label + ": no exception, rows=" + rows);
} catch (Exceptione) {
System.out.println(label + ": rows=" + rows + " then " + describe(e));
}
}
}
publicstaticvoidmain(String[] args) throwsException {
jdbc("jdbc-v2", "");
try (Clientclient = newClient.Builder().addEndpoint(URL).setUsername("default").setPassword(PASSWORD).build()) {
longrows = 0;
try (QueryResponser = client.query(SQL).get()) {
ClickHouseBinaryFormatReaderreader = client.newBinaryFormatReader(r);
while (reader.next() != null) { rows++; }
System.out.println("client-v2: no exception, rows=" + rows);
} catch (Exceptione) {
System.out.println("client-v2: rows=" + rows + " then " + describe(e));
}
}
// Contrast: with wait_end_of_query=1 the server sends no row before the error.jdbc("jdbc-v2, wait_end_of_query=1", "?clickhouse_setting_wait_end_of_query=1");
// v1 driver (last: after it, the same JVM keeps the v1 driver); compress=0 because of #3107jdbc("v1 driver", "?clickhouse.jdbc.v1=true&compress=0");
}
}
Output on main (8480ca001), ClickHouse 26.11.4.11:
jdbc-v2: rows=7783675 then SQLException (errorCode 0): Failed to read value for column toString(cityHash64(number)) | cause ClientException: Failed to read value for column toString(cityHash64(number))
client-v2: rows=7783675 then ClientException: Failed to read value for column toString(cityHash64(number)) | cause ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
jdbc-v2, wait_end_of_query=1: rows=0 then SQLException (errorCode 153): Code: 153. DB::Exception: Division by zero: while executing 'FUNCTION intDiv(1_UInt8 :: 1, minus(__table1.number, 100000… | cause ServerException: …
v1 driver: rows=7783559 then SQLException (errorCode 0): org.apache.hc.core5.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected | cause ConnectionClosedException: …
Automated triage by Polyglot AI. A maintainer makes the final decision.
api-diagnosticsArea: public API shape, error reporting, logging, metrics, tracingblocked-by-clickhousebugclientModule: the client (client-v2; with legacy: the v1 client)connectivityArea: connection, TLS, pool, timeouts, cancellation, transport, compressionjdbcModule: the JDBC driver (jdbc-v2; with legacy: the v1 driver)legacyOnly the v1 stack (clickhouse-client, clickhouse-http-client, clickhouse-jdbc) has the problem
Activity
den-crane commentedon May 5, 2021
hm, but only in the case with a single column.
zhicwu commentedon May 5, 2021
Thanks @den-crane. I was able to reproduce the issue on 21.3 as well. I think the problem is server responded 200, so unless we parse each line of the result(regardless what the output format is), we'll run into this kind of issue.
I also tried different output format and multiple columns, but unfortunately they all have the same issue.
zhicwu commentedon May 6, 2021
With http response 200, error only happens when 1) there's network issue(IOException); or 2) JDBC driver failed to parse value(e.g. trying the parse the error message as a number). As a result, when output format is
TabSeparated*(or similar text-based format) and the ResultSet only contains one column, and either the column type is String or we useresultSet.getString()to retrieve value, there will be no exception.This cannot be fixed easily on client side without changing output format, because user can write arbitrary SQL - it's hard to tell if there's error or not from result of below SQL:
@den-crane suggested to look into ClickHouse/ClickHouse#21136. Before http interface is enhanced, I hope switching to
RowBinaryformat can fix the issue from client side.zhicwu commentedon Nov 28, 2021
In 0.3.1 and earlier version, query
SELECT '2' FROM system.numbers where intDiv(1,number-100000)>-100000000000000will return below result without any exception - the error message became last line of result set:Starting from 0.3.2, depending on protocol you chose, the result is slightly different:
den-crane commentedon Feb 10, 2023
clickhouse-jdbc-0.4.0-shaded.jar:zhicwu commentedon Feb 10, 2023
Thanks @den-crane. The inner exception should be changed to
StreamCorruptedExceptionas it's more specific. There's buffer(size = 4096 but it's customizable) in ClickHouseInputStream, so if there's no fake error(rare but possible) and the error message is not too long, I should be able to extract the error.Using curl:
Using Java:
11 remaining items
polyglotAI-bot commentedon Nov 29, 2026
Recommendation: Keep open — fix needed
jdbc-v2, client-v2, and the v1 driver lose a server error that comes after HTTP 200. Open PR #3097 fixes only client-v2 and jdbc-v2 (it closes #2702), and its diff does not apply to
main. This issue is the only one for the v1 driver.main: Reproduced on main — after about 7.8 million rows, all three throw a read error with error code 0, and the server error (code 153) is lost. The error text no longer becomes a data row.X-ClickHouse-Exception-Code, other path); [client-v2] Mid-stream server error with response compression: ServerException message starts with result rows, not the server text #3439 (wrong message when the error comes before HTTP 200).wait_end_of_query=1(clickhouse_setting_wait_end_of_query=1in the jdbc-v2 URL,QuerySettings.waitEndOfQuery(true)in client-v2) gives the server error (contrast case), but the server then sends no row until the query ends.Analysis: The server part is done: ClickHouse/ClickHouse#75175 closed with ClickHouse/ClickHouse#88818 (first in 25.11). After HTTP 200, the server now sends the header
X-ClickHouse-Exception-Tag, ends the body with a tagged__exception__frame, and cuts the connection.maindoes not read that header: client-v2 checks onlyX-ClickHouse-Exception-Code(HttpAPIClientHelper.java:848), andBinaryStreamReader.java:307reports the cut stream as a column read error. The v1 transport also checks only that header (ApacheHttpConnectionImpl.java:215-216).Reproduction: complete program and output
Changed from the issue's steps: the error at number 10000000 in place of 1000000, so that the server always sends HTTP 200 first (with 1000000 the error sometimes comes before it); one case for each module, and a contrast case.
Output on
main(8480ca001), ClickHouse 26.11.4.11:Automated triage by Polyglot AI. A maintainer makes the final decision.