Skip to content

feat(warehouse): clickhouse v2 loading - #7303

Merged
achettyiitr merged 7 commits into
masterfrom
feat.clickhouse-v2-loading
Aug 28, 2026
Merged

achettyiitr merged 7 commits into
masterfrom
feat.clickhouse-v2-loading

Conversation

@achettyiitr

@achettyiitr achettyiitr commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

Adds the loading path for the ClickHouse v2 driver. ClickhouseV2 and its connection scaffolding are already on master; this is the part that gets rows into a table.

Loading

load_v2.go keeps v1's two paths: the S3 copy engine for allowlisted workspaces, otherwise download the load files and stream them in.

loadTableFromFiles reads every load file as one stream rather than one file at a time. They are headerless CSV in separate gzip members, so concatenating them and running a single gzip reader over the lot yields one row sequence for the table. Blocks therefore span load files instead of ending early at each one, which keeps the number of inserts — and so the number of parts ClickHouse has to merge — proportional to the rows loaded rather than to the file count.

insertBlock sends Warehouse.clickhouse.v2.commitEvery rows per batch. stmt.ExecContext only appends to a client-side block and the single wire write happens on commit, so commitEvery is what bounds how much of an upload is held in memory at once.

"Transaction" is the driver's word here, not ClickHouse's: BeginTx sends nothing and only health-checks the connection, Commit is batch.Send, and Rollback discards an unsent batch and closes the connection. A block is atomic only until it is sent, so a failure part-way through a load leaves the blocks before it already in the table.

Value binding

binder_v2.go converts each CSV cell into the exact Go type the column needs. Exact types matter because the driver accepts loosely-typed values and then silently corrupts them — int(300) into a UInt8 column stores 44, float64(1.9) into Int64 stores 1, neither with an error. A cell that fails to parse resolves to nil, or to the type's typed default when nullable columns are disabled. castStringToArray never returns a nil slice, which the driver rejects for array columns.

sqlquerywrapper: WithTx hands its context to the callback

WithTx derived a context carrying queryTimeout and then called fn(tx), so every caller shadowed it with the outer context and the timeout was silently inert inside transactions. The callback now receives that context.

This is load-bearing for the ClickHouse insert. The only context batch.Send can observe is the one captured on the batch at PrepareContext, so passing the transaction's context there is what allows a deadline to reach the wire write at all — prepareBatch turns it into a socket deadline, and contextWatchdog closes the connection if it is cancelled mid-send. (stmt.ExecContext's context is discarded by the driver; that call is an in-memory append.)

Postgres now uses the transaction's context in both WithTx bodies. repo/upload.go's body doesn't reference a context, so it is unchanged in behaviour.

s3 credentials no longer reach the query log

loadByCopyCommand passes the access key and secret to the s3 table function as literals, and the v2 middleware was constructed without a secrets regex — so logQuery wrote both at Info once a copy crossed slowQueryThreshold, which a full-table copy does routinely. Now masked, as redshift, snowflake and deltalake already do.

Unlike theirs, s3() takes positional arguments, so there is no label to anchor on: the pattern keeps the load folder and masks the two values that follow it. Reordering the argument list would silently reopen this, so driver_v2_test.go covers it.

Configuration

Everything is under Warehouse.clickhouse.v2.* (commitEvery, poolSize, readTimeout, compress, disableNullable, numWorkersDownloadLoadFiles, s3EngineEnabledWorkspaceIDs, slowQueryThreshold, maxLoadDelay, disableLoadTableStats, queryDebugLogs).

These do not fall back to the v1 keys, so any destination-level overrides in place today revert to defaults when the flag flips. disableNullable and s3EngineEnabledWorkspaceIDs change behaviour rather than just timing, so it is worth checking what is set before enabling this anywhere.

Follow-ups, deliberately not here

  • No wall-clock bound on the batch write. v1 had write_timeout=1800 in the DSN plus execTimeOutInSeconds / commitTimeOutInSeconds enforced through misc.RunWithTimeout. clickhouse-go/v2 has no write-timeout option and sendData sets no deadline of its own, so the ack half of a commit is bounded by ReadTimeout (300s) but the write half is not: a node that stops draining its socket parks a loader indefinitely. This needs its own per-block timeout config — connectTimeout cannot stand in, since it is also DialTimeout and governs the long-running S3 copy.
  • No equivalent of loadTableFailureRetries. A retry wrapped around insertBlock as it stands would skip rows, because the block is read from the shared csv.Reader inside the transaction; a correct one has to buffer the block first, and needs a duplicate story.
  • v1 (clickhouse.go) has the same s3 credential logging gap. Left alone as out of scope for this PR.

Testing

Unit coverage for the credential masking is in driver_v2_test.go. Integration coverage for the v2 driver lands in feat.clickhouse-v2-tests, which stacks on this branch.

@achettyiitr
achettyiitr force-pushed the feat.clickhouse-v2-connection branch from 1de6d74 to a51afbb Compare August 25, 2026 05:00
@achettyiitr
achettyiitr force-pushed the feat.clickhouse-v2-loading branch from 5259cec to c8688af Compare August 25, 2026 05:40
@achettyiitr
achettyiitr force-pushed the feat.clickhouse-v2-connection branch from 487616e to e6cb13f Compare August 25, 2026 08:13
Base automatically changed from feat.clickhouse-v2-connection to master August 25, 2026 08:48
@achettyiitr
achettyiitr force-pushed the feat.clickhouse-v2-loading branch from c8688af to 9d02d35 Compare August 25, 2026 08:52
Loading for the v2 implementation: the S3 copy-engine path, the download path,
and the block loop that streams every load file for a table through one batch
per commitEvery rows.

Independent of the DDL PR; either can merge first.
@achettyiitr
achettyiitr force-pushed the feat.clickhouse-v2-loading branch from 9d02d35 to 2704d2d Compare August 25, 2026 09:27
@codecov

codecov Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 2.24359% with 305 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.04%. Comparing base (267b973) to head (0f4e62d).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
warehouse/integrations/clickhouse/load_v2.go 0.00% 236 Missing ⚠️
warehouse/integrations/clickhouse/binder_v2.go 0.00% 68 Missing ⚠️
warehouse/integrations/clickhouse/driver_v2.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7303      +/-   ##
==========================================
- Coverage   79.34%   79.04%   -0.30%     
==========================================
  Files         609      611       +2     
  Lines       67963    68289     +326     
==========================================
+ Hits        53922    53982      +60     
- Misses      10876    11136     +260     
- Partials     3165     3171       +6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@saikumarrs saikumarrs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three on the load path, all in places where v2 diverges from v1. The missing write bounds is the one I'd want settled before this goes behind a workspace flag — the other two are cheap.

🔒 Scanned for secrets using gitleaks 8.29.1
achettyiitr and others added 4 commits August 28, 2026 05:37
loadByCopyCommand passes the access key and secret to the s3 table
function as literals, and the v2 middleware was constructed without a
secrets regex, so the slow query log wrote both at Info once a copy
crossed the threshold.

The s3 table function takes positional arguments, so unlike redshift,
snowflake and deltalake there is no label to anchor on: the pattern keeps
the load folder and masks the two values that follow it. Reordering the
argument list would silently reopen this, hence the test.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_014pXCMMrSLnt63xCWqvXhQ4
🔒 Scanned for secrets using gitleaks 8.29.1
🔒 Scanned for secrets using gitleaks 8.29.1
🔒 Scanned for secrets using gitleaks 8.29.1
@achettyiitr
achettyiitr merged commit 3e48618 into master Aug 28, 2026
115 of 117 checks passed
@achettyiitr
achettyiitr deleted the feat.clickhouse-v2-loading branch August 28, 2026 10:32
achettyiitr added a commit that referenced this pull request Aug 28, 2026
# Description

Wires the v2 ClickHouse implementation into the factory behind a flag,
and makes the integration suite run against whichever implementation is
under test.

Stacked on
[#7303](#7303) — review
that one first.

### The flag

`manager.go` resolves ClickHouse through a small `newClickhouse` helper.
With `Warehouse.clickhouse.useV2Driver` unset — the default — nothing
changes: it returns `clickhouse.New` exactly as before.

The factories only receive a destination type, so the choice resolves
once at construction and applies process-wide. That makes the rollout
unit a deployment rather than a destination, which is worth knowing
before turning it on anywhere.

### One suite, both implementations

Every subtest is shared, so coverage cannot drift between v1 and v2.
Which implementation runs is chosen by `CLICKHOUSE_USE_V2`, and the
warehouse matrix carries a `clickhouse-v2` leg that sets it, so each
gets its own job, its own log and its own re-run. Unset means v1, so a
bare `go test ./warehouse/integrations/clickhouse/...` keeps working.

### Verification connects with the driver under test

Previously every connection came from `sql.Open("clickhouse", …)`,
registered by `github.com/ClickHouse/clickhouse-go` — v1. The suite ran
twice but always verified through a v1 handle.

That hides things rather than merely being untidy. v1 cannot represent
every type v2 can create, and a read often survives anyway because the
query coerces the column to `String`; only a write surfaces the gap, and
then it fails inside the driver rather than at the server. Any v2-only
type is unverifiable this way.

Connections now go through a single `connectDB` closure that picks the
driver from the implementation under test. That also removes the DSN
string which was duplicated at seven call sites, and
`initializeClickhouseClusterMode` takes the same closure so its
distributed-table DDL runs over the right driver too.

### Also here

- `secure_test.go` — TLS coverage against a real server, with the
certificate pair generated per run into `t.TempDir()` rather than
committed, following the pattern in
`warehouse/integrations/tunnelling/connect_test.go`. It uses dockertest
rather than testcompose so the existing suite is left alone.
- `docker-compose.clickhouse-v2.yml` and
`docker-compose.clickhouse-cluster-v2.yml` — v2 runs against ClickHouse
25.8, where v1 runs against 21.

## Linear Ticket

- Resolves INT-7018, INT-7019

## Security

- [x] The code changed/added as part of this pull request won't create
any security issues with how the software is being used.

---------

Co-authored-by: Claude Opus 5 <[email protected]>
This was referenced Aug 31, 2026
itsmihir pushed a commit that referenced this pull request Aug 31, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.85.0-rc.1](v1.84.0...v1.85.0-rc.1)
(2026-08-31)


### Features

* backend config v2 resolver with stub mapper
([#7275](#7275))
([267b973](267b973))
* **warehouse:** clickhouse v2 connection and schema
([#7301](#7301))
([2eb1fa6](2eb1fa6))
* **warehouse:** clickhouse v2 ddl operations
([#7302](#7302))
([f2887c5](f2887c5))
* **warehouse:** clickhouse v2 loading
([#7303](#7303))
([3e48618](3e48618))
* **warehouse:** enable the clickhouse v2 driver behind a flag
([#7311](#7311))
([3ce1da5](3ce1da5))


### Bug Fixes

* race conditions while counting initial pending events can cause wrong
counting
([#7292](#7292))
([3efa718](3efa718))
* replay destinations not using the config corresponding to their
connected source type
([#7307](#7307))
([ee7aa93](ee7aa93))
* **router:** abort router-transform on proactive invalid_grant instead
of retrying (INT-6779)
([#7175](#7175))
([4d7348d](4d7348d))
* tracking plan merged config is cached without its event type
([#7314](#7314))
([624dd5c](624dd5c))


### Miscellaneous

* setattr/delattr hardening
([#7291](#7291))
([2855af4](2855af4))
* sync release v1.84.0 to main branch
([#7306](#7306))
([fa102b7](fa102b7))
* webhook reasons
([#7294](#7294))
([c298d59](c298d59))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: rudderstack-github-actions[bot] <236995729+rudderstack-github-actions[bot]@users.noreply.github.com>
itsmihir pushed a commit that referenced this pull request Sep 1, 2026
🤖 I have created a release *beep* *boop*
---


##
[1.85.0](v1.84.0...v1.85.0)
(2026-08-31)


### Features

* backend config v2 resolver with stub mapper
([#7275](#7275))
([267b973](267b973))
* **warehouse:** clickhouse v2 connection and schema
([#7301](#7301))
([2eb1fa6](2eb1fa6))
* **warehouse:** clickhouse v2 ddl operations
([#7302](#7302))
([f2887c5](f2887c5))
* **warehouse:** clickhouse v2 loading
([#7303](#7303))
([3e48618](3e48618))
* **warehouse:** enable the clickhouse v2 driver behind a flag
([#7311](#7311))
([3ce1da5](3ce1da5))


### Bug Fixes

* race conditions while counting initial pending events can cause wrong
counting
([#7292](#7292))
([3efa718](3efa718))
* replay destinations not using the config corresponding to their
connected source type
([#7307](#7307))
([ee7aa93](ee7aa93))
* **router:** abort router-transform on proactive invalid_grant instead
of retrying (INT-6779)
([#7175](#7175))
([4d7348d](4d7348d))
* tracking plan merged config is cached without its event type
([#7314](#7314))
([624dd5c](624dd5c))


### Miscellaneous

* setattr/delattr hardening
([#7291](#7291))
([2855af4](2855af4))
* sync release v1.84.0 to main branch
([#7306](#7306))
([fa102b7](fa102b7))
* webhook reasons
([#7294](#7294))
([c298d59](c298d59))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: rudderstack-github-actions[bot] <236995729+rudderstack-github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants