Skip to content

[client-v2] Handle new error tag from ClickHouse #2702

In review

Description

@chernser

Description

Handle new error format from ClickHouse

Activity

  1. added this to the 0.9.7 milestone on Jan 7, 2026
  2. modified the milestones: 0.9.7, 0.9.8 on Jan 21, 2026
  3. modified the milestones: 0.9.8, 0.9.9 on Mar 16, 2026
  4. changed the title [-][client-v2] Handle new error format from ClickHouse[/-] [+][client-v2] Handle new error tag from ClickHouse[/+] on Mar 18, 2026
  5. modified the milestones: 0.9.9, 0.9.10 on Apr 15, 2026
  6. modified the milestones: 0.11.0, 0.12.0 on Sep 23, 2026
  7. removed this from the 0.12.0 milestone on Nov 11, 2026
  8. added
    api-diagnostics
    and removed on Nov 26, 2026
  9. polyglotAI-bot commented on Nov 28, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — fix needed
    Open PR #3097 handles the tagged frame, but its diff does not apply to main (it changes ClickHouseLZ4InputStream, which main no longer has), and a maintainer paused it. It needs a rebase before review.

    Analysis: On a failure after HTTP 200, the server sends the header X-ClickHouse-Exception-Tag, ends the body with a tagged __exception__ frame, and cuts the connection (curl exit 18). main does not read that header: HttpAPIClientHelper.java:848 checks only X-ClickHouse-Exception-Code, and :854-856 returns the 200 body as data. The reader then hits the cut stream, and BinaryStreamReader.java:307 reports it as a column read error.

    Reproduction: complete program and output
    import com.clickhouse.client.api.Client;
    import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
    import com.clickhouse.client.api.query.QueryResponse;
    import com.clickhouse.client.api.query.QuerySettings;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    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", "");
        // The server sends about 58 MB of rows with HTTP 200, then the query fails at number 500000.
        static final String SQL = "SELECT number, repeat(toString(number), 20) AS s, throwIf(number = 500000, 'boom') AS t"
                + " FROM numbers(1000000) SETTINGS max_block_size = 1000";
    
        static String describe(Throwable e) {
            StringBuilder s = new StringBuilder(e.getClass().getName() + ": " + e.getMessage());
            for (Throwable c = e.getCause(); c != null && c != c.getCause(); c = c.getCause()) {
                s.append(" | cause ").append(c.getClass().getName()).append(": ").append(c.getMessage());
            }
            return s.toString().replace('\n', ' ');
        }
    
        public static void main(String[] args) throws Exception {
            try (Client client = new Client.Builder().addEndpoint(URL)
                    .setUsername("default").setPassword(PASSWORD).build()) {
                long rows = 0;
                try (QueryResponse response = client.query(SQL).get()) {
                    ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response);
                    while (reader.next() != null) {
                        rows++;
                    }
                    System.out.println("client-v2 query + reader: no exception, rows=" + rows);
                } catch (Exception e) {
                    System.out.println("client-v2 query + reader: rows=" + rows + " then " + describe(e));
                }
    
                // Contrast: with wait_end_of_query=1 the server sends no row before the error.
                rows = 0;
                try (QueryResponse response = client.query(SQL, new QuerySettings().waitEndOfQuery(true)).get()) {
                    ClickHouseBinaryFormatReader reader = client.newBinaryFormatReader(response);
                    while (reader.next() != null) {
                        rows++;
                    }
                    System.out.println("client-v2 query + reader, wait_end_of_query=1: no exception, rows=" + rows);
                } catch (Exception e) {
                    System.out.println("client-v2 query + reader, wait_end_of_query=1: rows=" + rows + " then " + describe(e));
                }
            }
    
            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);
                 Statement st = conn.createStatement()) {
                long rows = 0;
                try (ResultSet rs = st.executeQuery(SQL)) {
                    while (rs.next()) {
                        rows++;
                    }
                    System.out.println("jdbc-v2 executeQuery: no exception, rows=" + rows);
                } catch (Exception e) {
                    System.out.println("jdbc-v2 executeQuery: rows=" + rows + " then " + describe(e));
                }
            }
        }
    }

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    client-v2 query + reader: rows=467003 then com.clickhouse.client.api.ClientException: Failed to read value for column s | cause org.apache.hc.core5.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
    client-v2 query + reader, wait_end_of_query=1: rows=0 then com.clickhouse.client.api.ServerException: Code: 395. DB::Exception: boom: while executing 'FUNCTION throwIf(…)' … (FUNCTION_THROW_IF_VALUE_IS_NON_ZERO) (version 26.11.2.4 (official build)) …
    jdbc-v2 executeQuery: rows=467003 then java.sql.SQLException: Failed to read value for column s | cause com.clickhouse.client.api.ClientException: Failed to read value for column s | cause org.apache.hc.core5.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  10. added
    client
    jdbc
    on Nov 28, 2026
  11. polyglotAI-bot commented on Dec 4, 2026

    @polyglotAI-bot
    Collaborator

    Checked again on main (1b33bf6dd) with ClickHouse 26.11.7.10, because clickhouse-connect has the same defect (ClickHouse/clickhouse-connect#1290). The defect is still there. Two more entry points than in the reproduction above lose the server error:

    Call (the query fails after HTTP 200 and the first rows) Result
    query() + getInputStream(), format TabSeparated org.apache.hc.core5.http.ConnectionClosedException: Premature end of chunk coded message body: not a ClientException, no code, no server text. With compressServerResponse(false), the bytes that the caller reads include the __exception__ block.
    execute() ExecutionException → ClientException → ConnectionClosedException, no code, no server text
    query() + reader, jdbc-v2 executeQuery() as in the reproduction above
    queryAll() (contrast) ServerException, code 395: it sets waitEndOfQuery(true) (Client.java:2235)

    With compressServerResponse(false), a small result is sufficient: SELECT number FROM system.numbers WHERE throwIf(number = 10000, 'failure at row 10000') = 0 LIMIT 20000 SETTINGS max_block_size = 1000 fails after 10049 rows (44 KB). With the default compression, this query gives a ServerException (HTTP 500), so the 1,000,000-row query above is necessary.

    A check only in the binary reader does not cover getInputStream() and execute().

    Found by automated analysis (Polyglot AI) while checking clickhouse-connect#1290 against the other client repos. Verified against a live server.

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

    api-diagnosticsbugclientjdbc

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions