Skip to content

Commit 8d3a23e

Browse files
authored
Merge pull request ClickHouse#79242 from tuanpach/support-alter-database-on-cluster
Support altering database on cluster
2 parents b371084 + fba0373 commit 8d3a23e

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

src/Interpreters/InterpreterAlterQuery.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,13 @@ BlockIO InterpreterAlterQuery::executeToDatabase(const ASTAlterQuery & alter)
265265
throw Exception(ErrorCodes::LOGICAL_ERROR, "Wrong parameter type in ALTER DATABASE query");
266266
}
267267

268+
if (!alter.cluster.empty())
269+
{
270+
DDLQueryOnClusterParams params;
271+
params.access_to_check = getRequiredAccess();
272+
return executeDDLQueryOnCluster(query_ptr, getContext(), params);
273+
}
274+
268275
if (!alter_commands.empty())
269276
{
270277
/// Only ALTER SETTING and ALTER COMMENT is supported.

src/Parsers/ParserAlterQuery.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,14 @@ bool ParserAlterQuery::parseImpl(Pos & pos, ASTPtr & node, Expected & expected)
10681068
{
10691069
if (!parseDatabaseAsAST(pos, expected, query->database))
10701070
return false;
1071+
1072+
String cluster_str;
1073+
if (ParserKeyword(Keyword::ON).ignore(pos, expected))
1074+
{
1075+
if (!ASTQueryWithOnCluster::parse(pos, cluster_str, expected))
1076+
return false;
1077+
}
1078+
query->cluster = cluster_str;
10711079
}
10721080
else
10731081
{

tests/integration/test_alter_database_on_cluster/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<clickhouse>
2+
<remote_servers>
3+
<test_cluster>
4+
<shard>
5+
<internal_replication>true</internal_replication>
6+
<replica>
7+
<host>node1</host>
8+
<port>9000</port>
9+
</replica>
10+
<replica>
11+
<host>node2</host>
12+
<port>9000</port>
13+
</replica>
14+
</shard>
15+
</test_cluster>
16+
</remote_servers>
17+
</clickhouse>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import pytest
2+
3+
from helpers.cluster import ClickHouseCluster, QueryRuntimeException
4+
5+
cluster = ClickHouseCluster(__file__)
6+
7+
node1 = cluster.add_instance(
8+
"node1",
9+
main_configs=["configs/remote_servers.xml"],
10+
with_zookeeper=True,
11+
macros={"shard": 1, "replica": 1},
12+
)
13+
node2 = cluster.add_instance(
14+
"node2",
15+
main_configs=["configs/remote_servers.xml"],
16+
with_zookeeper=True,
17+
macros={"shard": 1, "replica": 2},
18+
)
19+
20+
21+
@pytest.fixture(scope="module")
22+
def started_cluster():
23+
try:
24+
cluster.start()
25+
26+
yield cluster
27+
28+
finally:
29+
cluster.shutdown()
30+
31+
32+
def create_database(node, db_name: str, engine: str, comment: str):
33+
if engine == "Atomic":
34+
node.query(
35+
f"CREATE DATABASE {db_name} ON CLUSTER test_cluster ENGINE = Atomic COMMENT '{comment}'"
36+
)
37+
return
38+
39+
if engine == "Lazy":
40+
node.query(
41+
f"CREATE DATABASE {db_name} ON CLUSTER test_cluster ENGINE = Lazy(1) COMMENT '{comment}'"
42+
)
43+
return
44+
45+
if engine == "Memory":
46+
node.query(
47+
f"CREATE DATABASE {db_name} ON CLUSTER test_cluster ENGINE = Memory COMMENT '{comment}'"
48+
)
49+
return
50+
51+
if engine == "Replicated":
52+
node.query(
53+
f"CREATE DATABASE {db_name} ON CLUSTER test_cluster "
54+
+ r"ENGINE = Replicated('/db/test', '{shard}', '{replica}')"
55+
+ f" COMMENT '{comment}'"
56+
)
57+
return
58+
59+
raise QueryRuntimeException(f"Not supported engine {engine}")
60+
61+
62+
@pytest.mark.parametrize("engine", ["Atomic", "Lazy", "Memory", "Replicated"])
63+
def test_alter_database_comment(started_cluster, engine):
64+
node1.query("DROP DATABASE IF EXISTS test")
65+
node2.query("DROP DATABASE IF EXISTS test")
66+
67+
create_database(node1, "test", engine, "initial comment")
68+
69+
modified_comment = "modified comment"
70+
node1.query(
71+
f"ALTER DATABASE test ON CLUSTER test_cluster (MODIFY COMMENT '{modified_comment}')"
72+
)
73+
74+
assert (
75+
node1.query("SELECT comment FROM system.databases WHERE name='test'").strip()
76+
== modified_comment
77+
)
78+
assert (
79+
node2.query("SELECT comment FROM system.databases WHERE name='test'").strip()
80+
== modified_comment
81+
)
82+
83+
node1.query(f"DETACH DATABASE test ON CLUSTER test_cluster")
84+
85+
assert (
86+
node1.query("SELECT count() FROM system.databases WHERE name='test'").strip()
87+
== "0"
88+
)
89+
assert (
90+
node2.query("SELECT count() FROM system.databases WHERE name='test'").strip()
91+
== "0"
92+
)
93+
94+
node1.query(f"ATTACH DATABASE test ON CLUSTER test_cluster")
95+
96+
assert (
97+
node1.query("SELECT comment FROM system.databases WHERE name='test'").strip()
98+
== modified_comment
99+
)
100+
assert (
101+
node2.query("SELECT comment FROM system.databases WHERE name='test'").strip()
102+
== modified_comment
103+
)
104+
105+
node1.query("DROP DATABASE IF EXISTS test")
106+
node2.query("DROP DATABASE IF EXISTS test")

0 commit comments

Comments
 (0)