Skip to content

Ignore ON CLUSTER in Replicated database - #84176

Closed
k-morozov wants to merge 7 commits into
ClickHouse:masterfrom
k-morozov:feature/ignore_on_cluster
Closed

k-morozov wants to merge 7 commits into
ClickHouse:masterfrom
k-morozov:feature/ignore_on_cluster

Conversation

@k-morozov

@k-morozov k-morozov commented Jul 21, 2025 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • New Feature

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

Ignore ON CLUSTER clause in Replicated database.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@k-morozov k-morozov changed the title [WIP] new setting for ignore ON_CLUSTER [WIP] new setting for ignore ON CLUSTER Jul 21, 2025
@k-morozov k-morozov changed the title [WIP] new setting for ignore ON CLUSTER [WIP] new setting to ignore ON CLUSTER Jul 23, 2025
@k-morozov k-morozov changed the title [WIP] new setting to ignore ON CLUSTER [WIP] ignore ON CLUSTER in Replicated database Jul 25, 2025
@k-morozov k-morozov changed the title [WIP] ignore ON CLUSTER in Replicated database Ignore ON CLUSTER in Replicated database Jul 25, 2025
@k-morozov
k-morozov marked this pull request as ready for review July 25, 2025 11:42
@tuanpach

Copy link
Copy Markdown
Member

We already have a function to remove ON CLUSTER. So I don't think we need this change.

query_on_cluster->cluster.clear();

Besides, this PR checks only the Replicated DB. We should have used shouldReplicateQuery.

@tuanpach tuanpach self-assigned this Jul 26, 2025
@tuanpach tuanpach closed this Aug 22, 2025
@k-morozov

Copy link
Copy Markdown
Contributor Author

We already have a function to remove ON CLUSTER. So I don't think we need this change.

@tuanpach
The existing removal works well only in particular configuration: when database_name is equal cluster and shouldReplicateQuery is true.

auto * query_on_cluster = dynamic_cast<ASTQueryWithOnCluster *>(query_ptr.get());
if (database_name != query_on_cluster->cluster)
    return false;

auto database = DatabaseCatalog::instance().tryGetDatabase(database_name);
if (database && database->shouldReplicateQuery(context, query_ptr))
{
    /// It's Replicated database and query is replicated on database level,
    /// so ON CLUSTER clause is redundant.
    query_on_cluster->cluster.clear();
    return true;
}

However, clusters could works with cluster and database names that are different. So maybeRemoveOnCluster will return false without additional checks.
For example:

:) select * from system.databases \G

SELECT *
FROM system.databases

Query id: c3283f62-34e0-432c-8c4f-c750df300558

...

Row 5:
──────
name:          repl_db
engine:        Replicated
data_path:     /var/lib/clickhouse/store/
metadata_path: /var/lib/clickhouse/store/b38/b38e5ec5-8223-4c0b-84ba-0d6c42d5d61b/
uuid:          b38e5ec5-8223-4c0b-84ba-0d6c42d5d61b
engine_full:   Replicated('/clickhouse/repl_db', '{shard}', '{replica}')
comment:       

...

6 rows in set. Elapsed: 0.002 sec. 


:) CREATE TABLE repl_db.table ON CLUSTER '{cluster}' (a Int) ENGINE=MergeTree ORDER BY a;

CREATE TABLE repl_db.`table` ON CLUSTER `{cluster}`
(
    `a` Int
)
ENGINE = MergeTree
ORDER BY a

Query id: fc740a42-c44a-455b-8337-6872069c4bcf


Elapsed: 0.151 sec. 

Received exception from server (version 24.8.14):
Code: 80. DB::Exception: Received from localhost:9000. DB::Exception: There was an error on [r***]: Code: 80. DB::Exception: It's not initial query. ON CLUSTER is not allowed for Replicated database. (INCORRECT_QUERY) (version 24.8.14.39 (official build)). (INCORRECT_QUERY)

I suggest removing ON CLUSTER for all query in Replicated database (not only for the specific configuration). We could do it under the new setting.

@tuanpach

Copy link
Copy Markdown
Member

Hi @k-morozov

Sorry, I missed your message during the vacation.

You mean we should remove the check

if (database_name != query_on_cluster->cluster)
    return false;

So if the query is replicated, we don't need ON CLUSTER?

@kirillgarbar

kirillgarbar commented Dec 2, 2025 •

Copy link
Copy Markdown
Contributor

Hello @tuanpach, I'd like to reopen this PR. Allowing ON CLUSTER queries for replicated database can be useful for a seamless migration of a database to replicated engine, so users can still use their old ON CLUSTER ddl queries without change.

Right now Konstantin's changes basically make database_name != query_on_cluster->cluster check useless. Just removing the check without additional casts to DatabaseReplicated would work fine, but this will mean that every ON CLUSTER 'some_cluster' query to replicated database would work only on replicas with replicated database, and other replicas in cluster will be ignored. In some hardly probable cases this behaviour may seem unexpected. In that case it can be fixed by adding ignore_on_cluster_for_replicated_database setting, and by expanding the check to (database_name != query_on_cluster->cluster && !setting).

So I suggest to just remove the check and ignore cluster implicitly, or to make the check weaker by adding ignore setting.

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