Repository navigation
Conversation
…T statements
The insert methods could only build `INSERT INTO <table> [(columns)] FORMAT <format>`.
An application could not send a statement such as
`INSERT INTO t SELECT ... FROM input('...')` or `INSERT INTO FUNCTION ...`
with data from a stream.
Add three `insertQuery` overloads (DataStreamWriter, InputStream, and
DataStreamWriter with query parameters). The client sends the statement as
it is in the `query` URL parameter, as the other insert methods do. When a
format is given, the client appends a new line and `FORMAT <format>`; when
it is null, the statement must have its own FORMAT clause and the default
format of the client is not sent. The operation reports no target table to
metrics and spans.
The existing insert methods delegate to the same private `executeInsert`
and keep their behavior. The change is additive.
Relates to ClickHouse#2071
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
Client#insertQuery, which inserts data with anINSERTstatement that the application writes. Before, the insert methods could only makeINSERT INTO <table> [(columns)] FORMAT <format>. That is not enough for statements such as:Relates to #2071 (closed by #2289, which added only column lists; the
input()use case in the last comment of the issue was not covered).Closes #2726
API (additive)
A new method name is necessary:
insert(String, DataStreamWriter, ClickHouseFormat, InsertSettings)already exists with a table name as the first argument.Behavior
sqlas it is in thequeryURL parameter, as the other insert methods do. The request body has only the data. The client does not parse, validate, or escape the SQL.format != null: the client appends a new line andFORMAT <format>. The new line keeps the clause out of a--comment at the end of the SQL.format == null: the SQL must have its ownFORMATclause. The default format of the client is not sent (noX-ClickHouse-Formatheader), so it cannot conflict with the clause.queryParams: values for{name:Type}placeholders, sent asparam_<name>URL parameters with the same value formatting asClient#query.InsertSettings#appCompressedData(true, "gzip")(for example a.parquet.gzfile).tableNameisnull). The javadoc ofSpanRecorder,MetricsRecorder,SpanSupport, andMetricsSupportnow says thattableNamecan benull.Implementation
The retry/metrics/span/cancellation loop of
insert(tableName, columns, writer, format, settings)moved into a privateexecuteInsert(...). The existing method builds the same SQL as before and calls it. TheInputStreamwriter and the query-parameter formatting became private helpers, shared with the existing paths.Compatibility
Additive only. No existing signature or behavior changes.
Tests (
InsertTests, which also runs in the LZ4, ZSTD and HTTP-compression subclasses)testInsertQuery(data provider):INSERT ... SELECT ... FROM input(...) GROUP BY ...with the format appended by the client, aFORMATclause in the SQL, a trailing--comment, a{k:UInt64}query parameter, and theInputStreamoverload. Checks every column of every row.testInsertQueryCompressedParquet:test_sample.parquet.gz(from tests for ORC / Parquet #571, copied toclient-v2test resources) withappCompressedData(true, "gzip")and a transform inSELECT.testInsertQueryIntoTableFunction:INSERT INTO FUNCTION file('<uuid>.csv.gz', ...); checks the gzip magic bytes of the file and the rows read back. Skipped on Cloud.testInsertQueryIntoNullFunction:INSERT INTO FUNCTION null(...); checks the written rows.testInsertQueryServerError: duplicateFORMATclause gives aServerExceptionand no rows.testInsertQueryInvalidArguments: null/blank SQL, null writer, null settings giveIllegalArgumentException.Local run against ClickHouse 26.11: all
insertQuerytests pass in all four classes. The only failures in those classes,testInsertAndReadBackWithSecureConnection(LZ4/ZSTD), need the SSL setup of the Testcontainers environment and fail the same way without this change.Checklist
history/latest/2071.md)🤖 Generated with Claude Code
Note
Medium Risk
Adds a new write path that accepts raw SQL (SQL-injection risk if misused) and refactors the shared insert executor, though existing insert APIs are intended to behave the same.
Overview
Adds
Client#insertQueryso applications can send arbitraryINSERTSQL (e.g.INSERT ... SELECT ... FROM input(...),INSERT INTO FUNCTION file(...)/null(...)) with body data from anInputStreamorDataStreamWriter. The client forwardssqlunchanged (optionalqueryParamsfor{name:Type}placeholders, same formatting asquery), appends\nFORMAT <format>when a format is provided, or requires aFORMATclause in SQL when format is null and clears the default format header so it does not override the statement.Existing
insert(table, columns, ...)behavior is unchanged: it still buildsINSERT INTO ... FORMAT ...but now delegates the retry/metrics/span/cancellation path to a privateexecuteInsert.inputStreamWriterandformatQueryParamsare shared helpers;insertQuerycallsexecuteInsertwithtableNamenull, so metrics/spans document thattableNamemay be absent. Integration tests coverinput()transforms, params, compressed Parquet, table functions, duplicate FORMAT errors, and validation.Reviewed by Cursor Bugbot for commit 4d54a8c. Bugbot is set up for automated code reviews on this repo. Configure here.