Skip to content

[client-v2] use_form_request_for_query puts the SQL statement into a multipart form field, so SQL over 128 KiB fails with "HTML Form Exception: Field value too long" #3437

Backlog

Description

@claude

Description

When client.http.use_form_request_for_query (ClientConfigProperties.HTTP_SEND_PARAMS_IN_BODY) is enabled and a query has statement parameters, HttpAPIClientHelper.createRequest() builds a multipart/form-data body that contains both the param_* values and the SQL statement itself:

// client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java:648-659
if (useMultipart) {
    req.removeHeaders(HttpHeaders.CONTENT_ENCODING);
    MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
    addStatementParams(requestConfig, multipartEntityBuilder::addTextBody);
    multipartEntityBuilder.addTextBody(ClickHouseHttpProto.QPARAM_QUERY_STMT, body); // <-- SQL becomes a form field
    httpEntity = multipartEntityBuilder.build();
    ...
}

The server parses that body with Poco's HTMLForm, which caps each individual field value at http_max_field_value_size (128 KiB by default). So:

  1. Regression for large SQL. Without the option the SQL is the raw request body and is only bounded by max_query_size. With use_form_request_for_query=true plus any parameter, the same statement becomes a form field, and anything over 128 KiB is rejected — e.g. a big inline IN (...) list, or generated SQL that also binds a parameter. The option is advertised as a way to avoid long-URL failures (Client.java:1292), so it silently introduces a new, smaller limit on the statement.
  2. The escape hatch does not escape for big parameters either. Moving a parameter out of the URL into the form replaces a URL-length/field-size failure with the same 128 KiB form-field cap. A 200 KB parameter fails both ways; a 100 KB one works both ways.

Raising the cap through per-request server settings does not help, because the form is parsed before per-query settings are applied (and a readonly user cannot set it).

The error surfaced by client-v2 is also opaque — the response body is reported as <Unreadable error message>:

com.clickhouse.client.api.ServerException: Code: 1000. DB::Exception: <Unreadable error message> (transport error: 500)

while the raw server response is:

Poco::Exception. Code: 1000, e.code() = 0, HTML Form Exception: Field value too long (version 26.11.2.4 (official build))

ClickHouse server version

26.11.2.4 (official build), local single node over HTTP on localhost:8123.

Reproduction

client-v2/src/test/java/com/clickhouse/client/api/MultipartFieldSizeTest.java:

package com.clickhouse.client.api;

import com.clickhouse.client.api.query.GenericRecord;
import com.clickhouse.client.api.query.QuerySettings;
import org.testng.annotations.Test;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class MultipartFieldSizeTest {

    private Client newClient(boolean multipart) {
        return new Client.Builder()
                .addEndpoint("http://localhost:8123")
                .setUsername("default")
                .setPassword("")
                .setOption(ClientConfigProperties.HTTP_SEND_PARAMS_IN_BODY.getKey(), String.valueOf(multipart))
                .build();
    }

    private void run(String name, boolean multipart, String sql, Map<String, Object> params) {
        try (Client client = newClient(multipart)) {
            List<GenericRecord> records = client.queryAll(sql, params, new QuerySettings());
            System.out.println(name + ": OK " + records.get(0).getString(1));
        } catch (Exception e) {
            String msg = e.getMessage();
            if (e.getCause() != null) {
                msg = msg + " | cause: " + e.getCause().getMessage();
            }
            System.out.println(name + ": ERROR " + msg.split("\n")[0]);
        }
    }

    @Test
    public void testLargeSql() throws Exception {
        String padding = new String(new char[200_000]).replace('\0', 'x');
        String bigSql = "SELECT {p:UInt8} AS p, length('" + padding + "') AS len";
        Map<String, Object> p = Collections.singletonMap("p", 1);
        run("large SQL, no multipart", false, bigSql, p);
        run("large SQL, multipart",    true,  bigSql, p);

        String big = new String(new char[200_000]).replace('\0', 'y');
        String q = "SELECT length({p:String}) AS len";
        run("large param, no multipart", false, q, Collections.singletonMap("p", big));
        run("large param, multipart",    true,  q, Collections.singletonMap("p", big));

        String mid = new String(new char[100_000]).replace('\0', 'z');
        run("100KB param, multipart", true, q, Collections.singletonMap("p", mid));

        rawMultipart(bigSql); // show the server-side error text the client hides
    }

    private void rawMultipart(String sql) throws Exception {
        String boundary = "----b0undary";
        String payload = "--" + boundary + "\r\nContent-Disposition: form-data; name=\"query\"\r\n\r\n"
                + sql + "\r\n--" + boundary + "--\r\n";
        java.net.HttpURLConnection c = (java.net.HttpURLConnection) new java.net.URL("http://localhost:8123/").openConnection();
        c.setRequestMethod("POST");
        c.setDoOutput(true);
        c.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
        c.getOutputStream().write(payload.getBytes("UTF-8"));
        java.io.InputStream in = c.getResponseCode() >= 400 ? c.getErrorStream() : c.getInputStream();
        java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
        byte[] buf = new byte[4096];
        int n;
        while ((n = in.read(buf)) > 0) bos.write(buf, 0, n);
        System.out.println("raw multipart status=" + c.getResponseCode() + " body=" + bos.toString("UTF-8").trim());
    }
}

Run with mvn -pl client-v2 test -Dtest=MultipartFieldSizeTest (surefire redirects stdout to client-v2/target/surefire-reports/com.clickhouse.client.api.MultipartFieldSizeTest-output.txt).

Expected: large SQL, multipart succeeds, like large SQL, no multipart does — turning the option on should not shrink the maximum statement size.

Actual (on main, commit 67775d0d3):

large SQL, no multipart: OK 1
large SQL, multipart: ERROR Failed to get query response | cause: Code: 1000. DB::Exception: <Unreadable error message> (transport error: 500) (queryId= )
large param, no multipart: ERROR Failed to get query response | cause: Code: 1000. DB::Exception: <Unreadable error message> (transport error: 500) (queryId= )
large param, multipart: ERROR Failed to get query response | cause: Code: 1000. DB::Exception: <Unreadable error message> (transport error: 500) (queryId= )
100KB param, multipart: OK 100000
raw multipart status=500 body=Poco::Exception. Code: 1000, e.code() = 0, HTML Form Exception: Field value too long (version 26.11.2.4 (official build))

In an earlier run that left response compression at its default, the non-multipart 200 KB parameter case reported the message verbatim — Code: 1000, e.code() = 0, HTML Form Exception: Field value too long — confirming the same cause for the URL path.

The 100 KB control passing while 200 KB fails pins the failure on the 128 KiB per-field cap rather than on total body size.

Suggested fix

Roughly in order of how much they solve:

  • Keep the SQL out of the form. In HttpAPIClientHelper.createRequest() (client-v2/src/main/java/com/clickhouse/client/api/internal/HttpAPIClientHelper.java:648-659), send the statement as the raw request body (or as a URL query= entry when small) and put only param_* values into the multipart body. That removes failure mode 1 entirely and restores parity with the non-multipart path.
  • Add http_max_field_value_size to the URL query string (not to settings that travel in the form/body) when the multipart path is taken, so it is in effect before the form is parsed — with the documented caveat that a readonly user cannot raise it.
  • Surface a usable error. The client reports <Unreadable error message> for this 500; the Poco text is in the response body and should be propagated.
  • At minimum, document the 128 KiB per-field limit on ClientConfigProperties.HTTP_SEND_PARAMS_IN_BODY (ClientConfigProperties.java:306) and in Client.java:1292, and consider a client-side check that names the oversized part.

Link

Same defect reported for clickhouse-js: https://github.com/ClickHouse/clickhouse-js/issues/1219
Same server cap on other client paths: #3256 (INSERT statement in the URL), #2856 (parameters in the URL / HTTP 414).
Also reported for the Rust client: ClickHouse/clickhouse-rs#501

Activity

  1. polyglotAI-bot commented on Nov 26, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — fix needed
    No open PR. The option docs also need the 128 KiB per-field limit (see Analysis).

    • Category: Bug
    • Classification: impact: none · area: Queries (form data for queries)
    • Status on main: Reproduced on main — the issue's test cases, run as a main on 8480ca001 with ClickHouse 26.11.2.4, fail as it shows: the 200 KB SQL fails only with the option, and the 200 KB parameter fails in both modes. The error text is also lost with compressServerResponse(false).
    • Breaking change: No.
    • Duplicates / related: No others.

    Analysis: HttpAPIClientHelper.java:656 puts the SQL text into the multipart body as the form field query, and the server limits each form field to http_max_field_value_size (128 KiB); without the option, the SQL is the raw request body (:663). A parameter over 128 KiB fails in the form and in the URL, but docs/clickhouse-docs/client.mdx:231 and ClientConfigProperties.java:304-306 do not give this limit. The lost error text is a second defect: the lookup string 1000. (HttpAPIClientHelper.java:430, :495) does not match the server text Code: 1000, e.code() = 0, …, so the client reports <Unreadable error message> (:548).

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

  2. added
    client
    documentation
    queries
    on Nov 26, 2026
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

    bugclientdocumentationqueries

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions