Skip to content

Fix mongodb table function: accept key-value overrides for named collections (database, collection, options) - #89616

Merged
GrigoryPervakov merged 28 commits into
ClickHouse:masterfrom
vanchaklar:master
Feb 12, 2026
Merged

GrigoryPervakov merged 28 commits into
ClickHouse:masterfrom
vanchaklar:master

Conversation

@vanchaklar

@vanchaklar vanchaklar commented Nov 6, 2025 •

Copy link
Copy Markdown
Contributor

Summary

  • Fixes an issue where calling the mongodb() table function with a named collection plus key-value overrides (for example database = '...' or collection = '...') resulted in a BAD_ARGUMENTS error. The table-function parsing previously only accepted a small allow-list of key names and rejected other valid overrides.
  • This change relaxes key-value argument parsing in TableFunctionMongoDB and forwards overrides (except the special structure argument) to the storage configuration code, which already validates and consumes keys such as database, collection, options, and oid_columns.
  • Adds an integration test to cover the named-collection override scenario.

Motivation and background

  • Named collections are an important convenience for centralizing connection settings (URI, user, password, host, etc.). Users commonly want to reuse a named collection and override only a few fields inline in a table function call — e.g. change the database or collection without duplicating credentials/config.
  • Previously, the table function rejected key-value overrides with an error like:
    DB::Exception: Expected key-value defined argument, got collection = 'collection_name'. (BAD_ARGUMENTS)
  • The storage-level configuration logic already supports key-value overrides and named-collection merging; the bug was that the table-function layer blocked many valid keys.

What changed (technical)

  • src/TableFunctions/TableFunctionMongoDB.cpp
    • getKeyValueMongoDBArgument now returns any key-value pair (after verifying the AST is equals(key, value)), instead of rejecting unknown keys.
    • TableFunctionMongoDB::parseArguments still treats structure specially (it must be a literal), but forwards any other key-value AST node into main_arguments. Those arguments are passed unchanged to StorageMongoDB::getConfiguration, where existing validation and URI construction take place.
  • tests/integration/test_table_function_mongodb/test.py
    • Added test_named_collection_overrides which demonstrates using a named collection and overriding database and collection inline, asserting the query succeeds and returns expected results.

Files changed

  • Modified:
    • src/TableFunctions/TableFunctionMongoDB.cpp
  • Tests:
    • tests/integration/test_table_function_mongodb/test.py (added test_named_collection_overrides)

Behavioral / compatibility notes

  • Backwards compatible: existing usages (positional args, URI forms, and previously-accepted named args) are unchanged.
  • The change only relaxes the front-end acceptance of key names so valid overrides can reach storage-level validation. Storage-level validation still rejects unknown or malformed keys.
  • structure remains a special literal argument and continues to be parsed by the table function layer.

How to reproduce the original bug (before this change)

  • This failed previously:
    SELECT _id
    FROM mongodb(
    named_collection_mongo_cred,
    structure = '_id String',
    collection = 'collection_name',
    database = 'db_name_alt'
    )
  • Previously produced:
    DB::Exception: Expected key-value defined argument, got collection = 'collection_name'. (BAD_ARGUMENTS)

Test coverage

  • New integration test:
    • tests/integration/test_table_function_mongodb/test.py::test_named_collection_overrides
    • This test inserts sample documents into a small MongoDB collection and validates:
      SELECT sum(key) FROM mongodb(mongo1, database = 'override_db', collection = 'override_collection', structure='key UInt64, data String') == "3"
  • Recommended CI: run the full MongoDB integration test file or the individual test to verify end-to-end behavior.

How to run the added test locally

  • From the repository root (the test harness will start MongoDB test containers):
pytest -q tests/integration/test_table_function_mongodb/test.py::test_named_collection_overrides
  • To run the full MongoDB integration test file:
pytest -q tests/integration/test_table_function_mongodb/test.py

(These tests require the repository's test harness; use the same environment that CI uses.)

Changelog entry (short)

  • Bug Fix: mongodb table function — accept key-value overrides (database, collection, options) when using a named collection; improve argument parsing and error diagnostics.

Suggested commit message

table function mongodb: accept key-value overrides for named-collection usage

Previously getKeyValueMongoDBArgument rejected most key names which made
named-collection overrides (e.g. database = 'x', collection = 'y') fail
with BAD_ARGUMENTS. Forward key-value overrides (except structure) from
the table function parser to storage configuration parsing and add an
integration test to prevent regression.

- Relax getKeyValueMongoDBArgument to return any key name.
- Forward key-value args into main_arguments in parseArguments.
- Add integration test test_named_collection_overrides.

Risk and mitigations

  • Risk: small — we only relax a front-end filter. All final validation and application of the overrides is performed by StorageMongoDB::getConfiguration. The integration test covers the core scenario.
  • Mitigation: storage-level validation remains unchanged; unknown or invalid keys will still cause appropriate errors.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Allow key-value overrides for named collection parameters in mongodb table function.

Version info

  • Merged into: 26.2.1.627

@CLAassistant

CLAassistant commented Nov 6, 2025 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@GrigoryPervakov GrigoryPervakov self-assigned this Nov 25, 2025
@clickhouse-gh

clickhouse-gh Bot commented Nov 25, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a7533e5]

Summary: ❌

job_name test_name status info comment
Performance Comparison (amd_release, master_head, 4/6) failure
Check Results failure IGNORED

@GrigoryPervakov GrigoryPervakov added can be tested Allows running workflows for external contributors pr-improvement Pull request with some product improvements labels Dec 22, 2025
@vanchaklar

Copy link
Copy Markdown
Contributor Author

@GrigoryPervakov I don't get why the recent tests keep on failing. I tried syncing the repos several times which changed the failure reasons. I wonder if I should change anything at all and wait or I'm just not able to find the issue in my edits.
I would appriciate it if you could give me a direction here. Thank you!

@GrigoryPervakov GrigoryPervakov 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.

Good overall. But need to improve docstrings

@@ -131,6 +148,7 @@ void registerTableFunctionMongoDB(TableFunctionFactory & factory)
.examples = {
{"Fetch collection by URI", "SELECT * FROM mongodb('mongodb://root:clickhouse@localhost:27017/database', 'example_collection', 'key UInt64, data String')", ""},
{"Fetch collection over TLS", "SELECT * FROM mongodb('localhost:27017', 'database', 'example_collection', 'root', 'clickhouse', 'key UInt64, data String', 'tls=true')", ""},
{"Fetch collection over TLS", "SELECT * FROM mongodb(host='localhost', port='27017', database='database', collection='example_collection', user='root', password='clickhouse', structure='key UInt64, data String', options='tls=true')", ""},

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.

nit: Copy-paste example name.
Let's add examples with named collections and overrides

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sorry for the late response. they've cut the internet connection a few weeks ago, I might not be able to do anything for an unknown period. please take care of the world.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sorry for the late response. they've cut the internet connection a few weeks ago, I might not be able to do anything for an unknown period. please take care of the world.

@vanchaklar

Copy link
Copy Markdown
Contributor Author

@GrigoryPervakov It seems to have passed all the tests.

@GrigoryPervakov

Copy link
Copy Markdown
Member

@GrigoryPervakov It seems to have passed all the tests.

Yes, please do not update the branch. I want to look at the PR with the finished pipeline 😁
If there are no related fails, I can merge it. It's rare to get all green checks, unfortunately.

@vanchaklar

Copy link
Copy Markdown
Contributor Author

@GrigoryPervakov It seems to have passed all the tests.

Yes, please do not update the branch. I want to look at the PR with the finished pipeline 😁
If there are no related fails, I can merge it. It's rare to get all green checks, unfortunately.

f21fc0b6f286b10be048dfaf8dc146a476927c9f this commit had it all green

@GrigoryPervakov
GrigoryPervakov added this pull request to the merge queue Feb 12, 2026
Merged via the queue into ClickHouse:master with commit 7a277ed Feb 12, 2026
129 of 130 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 12, 2026
clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Oct 4, 2026
`check_named_collection_dependencies` makes `DROP NAMED COLLECTION` throw
`NAMED_COLLECTION_IS_USED` only for tables that registered a dependency, and a
table engine registers by passing its `StorageID` to
`tryGetNamedCollectionWithOverrides`. `StorageMongoDB`'s `getConfigurationImpl`
had `table_id` in scope but did not pass it: ClickHouse#96181 added the argument in
2311e59, and its branch merge 468aaca with ClickHouse#89616 (which rewrote the
function) kept master's line without it. Every other table engine passes its
id. So the drop succeeded under a live MongoDB table, the table could not be
attached on the next restart (669), its database became unusable, and with
`async_load_databases = 0` the server did not start. The new line is identical
to the hunk in ClickHouse#112805.

`05315_mongodb_connection_options_secrets` (ClickHouse#123240) dropped its two
collections while `e06`, `e07` and `e08` still used them. Stateless CI drops the
test database, but Stress keeps it and restarts the server, so master Stress
jobs failed with `Cannot start clickhouse-server`. The test now expects the
drop to be refused while the tables exist, drops the tables first, and pins
`ast_fuzzer_any_query = 0` on every statement that names a collection: the
stress profile fuzzes DDL, and a fuzzed `DETACH` of such a table hides it from
the catalog, so its dependency is pruned and the collection drop succeeds
(the generic detached-table hole, ClickHouse#121519); `__fuzz_N` clones would otherwise
also hold the collection and make the final drop fail.

Not changed: table functions (`CREATE TABLE ... AS mongodb(nc)`) register no
dependency for any engine except `bigquery`; the experimental
`MaterializedPostgreSQL` engine has the same omission.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-improvement Pull request with some product improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants