diff --git a/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java b/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java index 9df39f407..5dcefed33 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/query/QuerySettings.java @@ -199,7 +199,11 @@ public String getDatabase() { } /** - * Requests the server to wait for the and of the query before sending response. Useful for getting accurate summary. + * Sets the HTTP parameter {@code wait_end_of_query} (server setting {@code http_wait_end_of_query}). When + * {@code true}, the server keeps the full response and sends it only after the query ends. Then the server + * metrics in {@link QueryResponse#getMetrics()} are complete, and the client gets a query error before any data. + * The first row is available only after the query ends. When {@code false} (the server default), the server + * sends the data while the query runs. */ public QuerySettings waitEndOfQuery(Boolean waitEndOfQuery) { serverSetting(ServerSettings.WAIT_END_OF_QUERY, waitEndOfQuery ? "1" : "0"); diff --git a/docs/clickhouse-docs/client.mdx b/docs/clickhouse-docs/client.mdx index 658f0e608..67dd0f753 100644 --- a/docs/clickhouse-docs/client.mdx +++ b/docs/clickhouse-docs/client.mdx @@ -884,7 +884,7 @@ Configuration options for query operations. | `setQueryId(String queryId)` | Sets query ID that will be assigned to the operation. | | `setFormat(ClickHouseFormat format)` | Sets response format. See `RowBinaryWithNamesAndTypes` for the full list. | | `setMaxExecutionTime(Integer maxExecutionTime)` | Sets operation execution time on server. Won't affect read timeout. | -| `waitEndOfQuery(Boolean waitEndOfQuery)` | Requests the server to wait for the end of the query before sending a response. | +| `waitEndOfQuery(Boolean waitEndOfQuery)` | Sets the HTTP parameter `wait_end_of_query`. When `true`, the server sends the response only after the query ends. The server metrics of the response are then complete, but the first row is available only after the query ends. The `Client#queryRecords` methods and the `Client#queryAll` methods that return `List` always set it to `true`. See [Waiting for the End of a Query](/integrations/language-clients/java/jdbc#wait-end-of-query). | | `setUseServerTimeZone(Boolean useServerTimeZone)` | Server timezone (see client config) will be used to parse date/time types in the result of an operation. Default `false`. | | `setUseTimeZone(String timeZone)` | Requests server to use `timeZone` for time conversion. See [session_timezone](/reference/settings/session-settings#session_timezone). | | `serverSetting(String name, String value)` | Sets individual server settings for an operation. | @@ -1201,6 +1201,8 @@ public void inspectResponseMetrics(Client client) throws Exception { } ``` +The server metrics come from the `X-ClickHouse-Summary` response header. For a query with a result set, the server sends this header before the first rows. Thus, the values show only the progress up to that point. To get complete values, set `QuerySettings#waitEndOfQuery(true)`. `Client#queryRecords` sets it, so the metrics of the `Records` that it returns are complete. + ### Spans Reported {#v2-o11y-spans} Tracing in Client V2 produces two types of spans arranged in a parent-child hierarchy: @@ -1451,7 +1453,7 @@ Table below shows what old options are supported in the new client and their new | `ClickHouseClientOption#MAX_RESULT_ROWS` | ✗ | Is server side setting | | `ClickHouseClientOption#RESULT_OVERFLOW_MODE` | ✗ | Is server side setting | | `ClickHouseHttpOption#RECEIVE_QUERY_PROGRESS` | ✗ | Server side setting | -| `ClickHouseHttpOption#WAIT_END_OF_QUERY` | ✗ | Server side setting | +| `ClickHouseHttpOption#WAIT_END_OF_QUERY` | ✗ | HTTP parameter. See `QuerySettings#waitEndOfQuery` | | `ClickHouseHttpOption#REMEMBER_LAST_SET_ROLES` | `Client#setDBRoles` | Runtime config now. See also `QuerySettings#setDBRoles` and `InsertSettings#setDBRoles` | diff --git a/docs/clickhouse-docs/jdbc.mdx b/docs/clickhouse-docs/jdbc.mdx index d6685ac41..fce22a47b 100644 --- a/docs/clickhouse-docs/jdbc.mdx +++ b/docs/clickhouse-docs/jdbc.mdx @@ -130,6 +130,49 @@ We deliberately avoid adding default settings to connection properties to avoid However some users need to pass format settings (for example to read JSON as String) and we recommend using `readonly=2` profile. Read more about read-only profiles [here](/concepts/features/configuration/settings/constraints-on-settings#read-only). +### Waiting for the End of a Query {#wait-end-of-query} + +The HTTP parameter `wait_end_of_query` controls when the server starts to send a response. +The driver does not set it for the statements of the application. Thus, the server default `0` applies, and the server sends the rows of a result set while the query runs. +With `wait_end_of_query=1`, the server keeps the full response. It sends the response only after the query ends. + +To enable it for a connection, set the property `clickhouse_setting_wait_end_of_query` to `1` in the connection properties, or add `clickhouse_setting_wait_end_of_query=1` to the JDBC URL: + +```java +Properties properties = new Properties(); +properties.setProperty("user", "default"); +properties.setProperty("password", getPassword()); +properties.put(ClientConfigProperties.serverSetting("wait_end_of_query"), "1"); // clickhouse_setting_wait_end_of_query + +Connection conn = Driver.connect("jdbc:ch:http://localhost:8123/", properties); +``` + +To enable it for one statement, use `com.clickhouse.jdbc.StatementImpl#getLocalSettings` (see [Operation Identification](#operation-identification)): + +```java +StatementImpl stmt = (StatementImpl) conn.createStatement(); +stmt.getLocalSettings().waitEndOfQuery(true); +``` + +The driver sends the property as the `wait_end_of_query` URL parameter. This parameter is not a setting, and the server reads it before it parses the query. Thus: +- A `readonly=1` profile accepts the parameter. Do not use the setting name `http_wait_end_of_query` in the property: a `readonly=1` profile rejects it. +- The `SETTINGS` clause of a query cannot enable it. The server rejects `SETTINGS wait_end_of_query=1` as an unknown setting. `SETTINGS http_wait_end_of_query=1` has no effect on the response. + +The server setting [`http_wait_end_of_query`](/reference/settings/session-settings#http_wait_end_of_query) gives the default value of the parameter. To enable it for all HTTP queries of a user, set it in the settings profile of that user. + +| Behavior | `wait_end_of_query=0` (default) | `wait_end_of_query=1` | +|----------|---------------------------------|-----------------------| +| First row of a result set | Available when the server sends the first data. | Available only after the query ends. | +| Server resources | The server sends the data while the query runs. | The server keeps the full response. With the default [`http_response_buffer_size`](/reference/settings/session-settings#http_response_buffer_size) `0`, it writes each response to a temporary file on its disk, also a small response. | +| Update count of `executeUpdate` and `executeBatch` | Not changed by this parameter. For a statement without a result set, the server sends the summary after the statement ends. | Not changed by this parameter. | +| Statistics of a query with a result set, for example the span attributes `db.response.returned_rows` and `clickhouse.response.read_rows` | Can be incomplete. The server sends the `X-ClickHouse-Summary` header before the first rows. Thus, the header has only the progress up to that point, and `result_rows` can be `0`. | Complete. | +| Error during a query | If the server already sent rows, `ResultSet.next()` can return rows before it throws the exception. The last of these rows can have wrong values, because the driver reads a part of the error message as row data. | `executeQuery` throws a `SQLException` with the server error code. The application gets no rows. | + +Recommendations: +- Keep the default for queries that return many rows. The application can process the rows while the query runs, and the server does not keep the result. +- Use `wait_end_of_query=1` for queries with small results when the application needs complete statistics, or when it must get an error before it processes rows. +- With `wait_end_of_query=1`, the driver gets no data until the query ends. Make the socket timeout longer than the longest query. The property `socket_timeout` sets it (default `0`, no timeout), and `Connection#setNetworkTimeout` changes it. + ### Client Identification {#client-identification} There are two ways to identify application originated a request: set `com.clickhouse.client.api.ClientConfigProperties#CLIENT_NAME` via diff --git a/docs/features.md b/docs/features.md index a8f7e12b3..89368ec2e 100644 --- a/docs/features.md +++ b/docs/features.md @@ -127,6 +127,7 @@ Compatibility-sensitive traits: - JDBC `ssl_mode` handling is compatibility-sensitive: values are case-insensitive, `none` is aliased to `trust` (the no-verification mode), and an unrecognized value throws `SQLException` during connection configuration. The normalized canonical mode name is forwarded to the underlying `client-v2` transport. - Connection `Properties` values must be strings, with one scoped exception: the `ssl_context` key may carry a live `javax.net.ssl.SSLContext` object. Any other non-string property value still throws `IllegalArgumentException` during connection configuration. A string `ssl_context` (supplied via `setProperty` or a URL query parameter) is rejected with `SQLException`, since a string cannot represent a live context. - INSERT result semantics depend on server-side `async_insert` and `wait_for_async_insert`. The driver does not override these settings, so it follows whatever the server profile or user configuration sets. When `async_insert=1` and `wait_for_async_insert=0`, `Statement.executeUpdate(...)` and `PreparedStatement.executeUpdate(...)` may return `0` (or an under-counted value), and parsing/data errors in the INSERT body may not be reported synchronously as a `SQLException`. Set `async_insert=0` (or `wait_for_async_insert=1`) per connection or statement to restore synchronous row counts and error reporting. +- Result streaming depends on the HTTP parameter `wait_end_of_query` (server setting `http_wait_end_of_query`). The driver does not set it, so it follows whatever the server profile or user configuration sets. With `0` (the server default), the server sends the rows while the query runs, and the summary of a query with a result set can be incomplete. With `clickhouse_setting_wait_end_of_query=1` per connection, or `waitEndOfQuery(true)` on `StatementImpl#getLocalSettings()` per statement, the server sends the response after the query ends. Then the summary is complete, and `executeQuery` throws the server error before it returns rows. The update count of `executeUpdate` does not depend on this parameter. ## `client-v2` OpenTelemetry span recording