Skip to content

Fix ClickHouseConnection: PostStreamAsync and ClickHouseBulkCopy ignore ChangeDatabase - #684

Open
polyglotAI-bot wants to merge 2 commits into
mainfrom
polyglot/change-database-post-stream-bulk-copy
Open

polyglotAI-bot wants to merge 2 commits into
mainfrom
polyglot/change-database-post-stream-bulk-copy

Conversation

@polyglotAI-bot

Copy link
Copy Markdown
Collaborator

Summary

Fixes #683.

ChangeDatabase stores the database on the connection only. ClickHouseClient.CreateUriBuilder uses queryOverride?.Database ?? Settings.Database. ClickHouseCommand passes connection.Database, but two other paths that go through the connection did not:

  • ClickHouseConnection.PostStreamAsync (both overloads) passed options with only QueryId, or null when no query id was given.
  • ClickHouseBulkCopy.WriteToServerAsync built its InsertOptions without Database.

Both paths therefore ran in the configured database. An unqualified table name resolved in the wrong database: the call failed with UNKNOWN_TABLE, or, if a table with the same name existed there, the rows went into it with no error.

Changes

  • ClickHouseConnection.PostStreamAsync: always build QueryOptions with Database = Database (plus QueryId when given). All other options stay null, so the request is the same as before apart from the database.
  • ClickHouseBulkCopy.WriteToServerAsync: set Database = connection.Database on the InsertOptions. It is read at write time, so a ChangeDatabase after the bulk copy is constructed also applies. InsertOptions.Database drives both the schema probe and the INSERT.
  • Changelog fragment changelog.d/683-change-database-post-stream-bulk-copy.fixes.md.

Without ChangeDatabase, Database returns Settings.Database, so those requests do not change. ClickHouseBulkCopy(string connectionString) owns its connection, so it is not affected.

Tests

  • ConnectionTests.PostStreamAsync_AfterChangeDatabase_RunsInSelectedDatabase: both overloads, with and without a query id (the two branches of the old options helper). Runs SELECT currentDatabase() after ChangeDatabase("system"). On main all 4 cases return default.
  • BulkCopyTests.WriteToServerAsync_AfterChangeDatabase_InsertsIntoSelectedDatabase: RowBinary and RowBinaryWithDefaults. The table exists only in test_secondary and the bulk copy gets the bare name. On main both cases fail with UNKNOWN_TABLE from the schema probe.

The new tests fail on main and pass with the fix. Full ClickHouse.Driver.Tests run on net10.0 against ClickHouse 26.9: 14825 passed, 0 failed, no existing tests changed.

Related, not changed here: Clone() also drops the database chosen with ChangeDatabase (#682).

Checklist

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG

🤖 Generated with Claude Code

…re ChangeDatabase

ChangeDatabase stores the database on the connection only.
ClickHouseClient.CreateUriBuilder falls back to Settings.Database when the
request options carry no database. ClickHouseCommand passes
connection.Database, but PostStreamAsync passed only the query id (or no
options at all), and ClickHouseBulkCopy built its InsertOptions without a
database. Both paths therefore ran in the configured database, so an
unqualified table name could resolve to a table in the wrong database.

PostStreamAsync now always sends the connection's Database, and
ClickHouseBulkCopy reads connection.Database at write time for the schema
probe and the INSERT.

Fixes: #683

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 15, 2026 05:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The public behavior correction still needs the required update to the HTTP user guide.

1 open finding
What changed in this PR

Fixes #683 in the HTTP client so stream requests and bulk copies respect the database selected by ChangeDatabase.

Changes:

  • Pass the connection’s selected database through query and insert options.
  • Add regression tests for both stream overloads and binary insert formats.
  • Add a changelog fragment.
File Description
ClickHouse.Driver/​Copy/​ClickHouseBulkCopy.cs Selects the database at write time.
ClickHouse.Driver/​ADO/​ClickHouseConnection.cs Includes the selected database in stream request options.
ClickHouse.Driver.Tests/​BulkCopy/​BulkCopyTests.cs Tests database selection for both binary formats.
ClickHouse.Driver.Tests/​ADO/​ConnectionTests.cs Tests both stream overloads with and without query IDs.
changelog.d/​683-change-database-post-stream-bulk-copy.fixes.md Records the database-routing correction.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Document that commands, both connection PostStreamAsync overloads, and
ClickHouseBulkCopy use the database selected with ChangeDatabase, and that
the bulk copy reads it when WriteToServerAsync runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

ClickHouseConnection.PostStreamAsync and ClickHouseBulkCopy ignore ChangeDatabase

2 participants