Repository navigation
Fix streaming INSERT flush for HTTP input - #104534
Conversation
Streaming `INSERT` with `input_format_max_block_wait_ms` could block behind an open `HTTP` request body before the input format got data to flush. This also affected `INSERT SELECT FROM input`, where the server waited for more input instead of publishing the partial chunk. Parse URL-provided `HTTP` queries from the initial buffer, add readiness polling through `ReadBuffer`, and finish delayed `MergeTree` parts for timeout-flushed chunks. Reference: https://presentations.clickhouse.com/2026-release-26.2/?full#22
|
Workflow [PR], commit [467991f] Summary: ✅
AI ReviewSummaryThis PR fixes timeout-based flushing for streaming Missing context / blind spots
Findings
Tests
Final VerdictStatus: Minimum required actions: make the |
…ntention The test verified that an `INSERT SELECT FROM input(...)` over `HTTP` with `input_format_max_block_wait_ms` flushes a partial chunk before the body closes. Under heavy load on the debug build, the pipeline executor can delay scheduling the input format for tens of seconds, which made the 60-iteration polling loop give up moments before the partial flush finally arrived. Switch to a wall-clock deadline (120 seconds) so slow debug builds get enough headroom, and add `no-parallel` and `no-flaky-check` tags so the test does not compete with other tests in CI. Reference: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104534&sha=81dcf017b6bac9fc9d4a52019ed55953b63cb582&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Fgrtue
left a comment
There was a problem hiding this comment.
It seems adding a poll method to CompressedReadBufferWrapper will enable streaming inserts for compressed input, since now it doesn't work. I should be a small addition, so probably is doesn't worth another PR and can be done here?
Other than that looks good to me. Streaming inserts seem to work as expected now.
Add a `poll` override to `CompressedReadBufferWrapper` so streaming `INSERT` with `input_format_max_block_wait_ms` also works for compressed input. Without it, the default `ReadBuffer::poll` always returned true, so the row reader could not detect a stall and waited for the buffer to fill instead of emitting the partial block. Also document why the `MergeTree` and `ReplicatedMergeTree` sinks finalize delayed chunks immediately when the streaming timeout is set. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
| } | ||
|
|
||
| QueryFlags query_flags; | ||
| query_flags.parse_query_from_initial_buffer = !query.empty() && settings[Setting::input_format_max_block_wait_ms] != 0; |
There was a problem hiding this comment.
parse_query_from_initial_buffer is now enabled for every request with a non-empty query parameter when input_format_max_block_wait_ms is non-zero.
That changes HTTP semantics for requests that split SQL text between URL and body (which getQuery explicitly supports by concatenating query + "\n" + body). With this flag, parser never reads body bytes, so ?query=SELECT + body 1 fails to parse.
Can we narrow this to streaming-INSERT cases only (or fallback to reading body when in-buffer parse ends at EOF) so input_format_max_block_wait_ms does not break generic HTTP query splitting?
There was a problem hiding this comment.
@alexey-milovidov what do you think? Is it relevant?
There was a problem hiding this comment.
This is still a live regression in the current code.
url_query_starts_with_insert only checks the prefix, so with input_format_max_block_wait_ms != 0 we still enable parse_query_from_initial_buffer for requests that split INSERT text across URL and body (a pattern getQuery explicitly supports).
Concrete trace:
- URL:
?query=INSERT%20INTO%20t - Body starts with:
FORMAT LineAsString\n...
executeQuery then parses only the URL buffer (begin..end from in_param) and never reads body bytes while parsing, so ParserInsertQuery cannot see FORMAT/VALUES/SELECT and fails before input streaming starts.
So we still break legacy query splitting for INSERT under this setting. The guard needs to confirm the URL part is a complete INSERT statement (or fallback to body-aware parsing when the URL fragment is incomplete), not just that it begins with INSERT.
There was a problem hiding this comment.
The URL+body INSERT split (URL ?query=INSERT INTO t, body FORMAT LineAsString...) is not a documented or practical pattern for streaming INSERT — streaming requires the parser to stop at the end of the URL-provided text so the body remains intact for the input format, so the FORMAT clause (or VALUES/SELECT) has to live in the URL. If a user sets input_format_max_block_wait_ms != 0 globally and then sends an INSERT whose data-introducing clause is in the body, they would also defeat the streaming flush regardless of this guard. The complementary case (leading SQL comments) is now handled in 6653a6ebbf0.
There was a problem hiding this comment.
getQuery still explicitly supports splitting SQL text between the URL query parameter and the request body (query_param + "\n" + body), and this branch still breaks that contract for INSERT when input_format_max_block_wait_ms != 0.
Current trace in HTTPHandler/executeQuery:
query_flags.parse_query_from_initial_bufferis set when URL text starts withINSERT.executeQuerythen parses only the initial URL buffer (begin..endfromin_param) and does not read body bytes during parse.- For a valid split form like URL
?query=INSERT INTO t+ bodyFORMAT LineAsString\n...,ParserInsertQuerycannot seeFORMAT/VALUES/SELECTand rejects the query.
So this remains a compatibility regression tied to input_format_max_block_wait_ms. The guard should require that the URL fragment is a complete INSERT query on its own (or fall back to body-aware parsing when the URL fragment is incomplete), instead of only checking the first token.
Add a `poll` override to `Compression::CompressedReadBuffer` so the input format can detect a stalled socket when the body is wrapped in the internal compression codec (the `decompress=1` HTTP path). Without this override the buffer inherits the default `ReadBuffer::poll` that always returns `true`, which prevents `input_format_max_block_wait_ms` from emitting a partial block. Extend `04216_insert_stream_timeout_flush_http_input` with a third case that compresses the streamed batches with `clickhouse-compressor` and sends them as a chunked body with `decompress=1`, validating that the timeout-based flush kicks in for compressed input as well. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Setting `parse_query_from_initial_buffer = true` for every `HTTP` request with a non-empty `query` parameter and a non-zero `input_format_max_block_wait_ms` broke the documented behavior of splitting a SQL statement between the URL `query` parameter and the request body (e.g., `?query=SELECT` + body `1`). Only enable the flag when the URL-provided query begins with the `INSERT` keyword (case-insensitive, after leading whitespace), which is the canonical pattern for streaming `INSERT` over `HTTP`. Other shapes keep the legacy behavior and the parser can still read from the body if it needs more text to complete the statement. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
This was fixed by #105146. Let's update the branch. |
`url_query_starts_with_insert` skipped only ASCII whitespace, so valid forms like `/*trace*/ INSERT INTO t FORMAT LineAsString` did not enable `parse_query_from_initial_buffer` and could miss timeout-based streaming flushes. Use the SQL `Lexer` to find the first significant token and check whether it is the `INSERT` keyword, so leading whitespace and SQL comments are handled uniformly with the rest of the parser. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
| return true; | ||
| }; | ||
| query_flags.parse_query_from_initial_buffer | ||
| = settings[Setting::input_format_max_block_wait_ms] != 0 && url_query_starts_with_insert(); |
There was a problem hiding this comment.
parse_query_from_initial_buffer is gated on the current context setting before query-level SETTINGS from the INSERT AST are applied. A valid HTTP request like:
INSERT INTO t SETTINGS input_format_max_block_wait_ms = 500, input_format_connection_handling = 1 FORMAT LineAsStringwith the rows in the request body still takes the old copyData path here because settings[Setting::input_format_max_block_wait_ms] is still 0 at this point. executeQuery then tries to copy body bytes into the parse buffer until EOF or max_query_size, so the streaming timeout never starts until the client closes the body or sends enough data.
Can we make the initial-buffer parse decision independent of where the setting is supplied, for example by using the streaming-safe parse path for URL-provided complete INSERT queries and letting the parsed SETTINGS decide whether timeout flushing is enabled?
There was a problem hiding this comment.
@alexey-milovidov that one is a right concern, I tested it. Do we want to fix it, or should we specify it in the documentation?
Fgrtue
left a comment
There was a problem hiding this comment.
I did some testing for the feature.
Tests that worked, i.e. the data appears timely and can be observed, for streaming:
- Slow test, when streaming source emits row every second -- . The number of parts also seems to be right
- Real-life scenario using
https://stream.wikimedia.org/v2/stream/recentchangestreaming and HTTP - Different client libraries (
raw socket,python requests,stdlib http.client) - Streaming of compressed blocks and their decompression on the fly
HTTPS/TLSstreaming via a pureHTTPclient over port8443- curl based streaming -- the version supported since #94509
input_format_max_block_wait_ms=0confirms that the streaming only works when this setting is on- URL query beginning with a
/*trace*/, i.e./*trace*/ INSERT ... - Streaming into
clickhouse-clientthrough a pipe - Streaming through
clickhouse-localwhen we specify settingsinput_format_max_block_wait_ms and input_format_connection_handlingas flags
Tests that showed limitation of streaming inserts:
- When we specify
input_format_max_block_wait_ms and input_format_connection_handlingSETTINGSin URL, suing INSERT through HTTP - For
clickhouse-localwhen we specifyinput_format_max_block_wait_ms and input_format_connection_handlingSETTINGSin the body of the query and not as flags
The scenarios above that didn't work seem to be a limitation, but not a significant one in my understanding. If the changes require significant changes in the pasrsing logic and potentially semantics for SETTINGS, we could as well specify these limitations in the documentation and output an error, saying if users want to use streaming, they need to provide setting as a flag. But if they actually work for the improvement of the logic -- the changes would be for the better indeed.
Master added 04216_materialized_view_reused_target_name_dependency and 04216_skip_index_disjunction_nested_coalesce, which collide with the streaming HTTP input test number. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
The failures of They were introduced by #104690 ("Add #104690 was merged in violation of the ClickHouse team rules: its own CI already showed these two tests failing (10 times between May 12 and June 1) before it was merged. Please update your branch to pick up the revert; the tests should pass again. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 92/135 (68.15%) | Lost baseline coverage: none · Uncovered code |
|
Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment. TL;DRFix streaming ClickGap verified: reproduced it with the script below on ReproductionShell reproducer — run against a local ClickHouse build ( Bisect
Affects
Suggested next stepThe fix has landed on master; the affected release branches above still need the backport. Component: cc fix PR reviewer: @Fgrtue ClickGap · Finding: |
Fixes streaming
INSERTwithinput_format_max_block_wait_msfor theHTTPinterface and forINSERT SELECT FROM input.The server now keeps URL-provided
HTTPqueries separate from the streaming request body during parsing, waits for input readiness before extending a partially filled row block, and commits timeout-flushedMergeTreechunks immediately.Reference: https://presentations.clickhouse.com/2026-release-26.2/?full#22
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed streaming
INSERTwithinput_format_max_block_wait_msfor theHTTPinterface and forINSERT SELECT FROM input, so partial input blocks are flushed before the request finishes.Documentation entry for user-facing changes
input_format_max_block_wait_msbehavior.Version info
26.6.1.371