ClickHouse/tests/integration/test_modify_engine_on_restart/test_mv.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

134 lines
3.7 KiB
Python
Raw Normal View History

2023-12-12 18:45:00 +00:00
import pytest
2024-09-27 10:19:39 +00:00
2023-12-12 18:45:00 +00:00
from helpers.cluster import ClickHouseCluster
2024-09-27 10:19:39 +00:00
from test_modify_engine_on_restart.common import check_flags_deleted, set_convert_flags
2023-12-12 18:45:00 +00:00
cluster = ClickHouseCluster(__file__)
ch1 = cluster.add_instance(
"ch1",
main_configs=[
"configs/config.d/clusters.xml",
"configs/config.d/distributed_ddl.xml",
],
with_zookeeper=True,
macros={"replica": "node1"},
stay_alive=True,
)
2023-12-14 20:01:50 +00:00
database_name = "modify_engine_with_mv"
2023-12-16 10:48:25 +00:00
2023-12-12 18:45:00 +00:00
@pytest.fixture(scope="module")
def started_cluster():
try:
cluster.start()
yield cluster
finally:
cluster.shutdown()
2023-12-16 10:48:25 +00:00
2023-12-14 20:01:50 +00:00
def q(node, query):
2023-12-16 10:48:25 +00:00
return node.query(database=database_name, sql=query)
2023-12-12 18:45:00 +00:00
2023-12-14 20:01:50 +00:00
def create_tables():
2023-12-12 18:45:00 +00:00
q(
ch1,
2023-12-16 10:48:25 +00:00
"CREATE TABLE hourly_data(`domain_name` String, `event_time` DateTime, `count_views` UInt64) ENGINE = MergeTree ORDER BY (domain_name, event_time)",
2023-12-12 18:45:00 +00:00
)
q(
ch1,
"CREATE TABLE monthly_aggregated_data\
(`domain_name` String, `month` Date, `sumCountViews` AggregateFunction(sum, UInt64))\
2023-12-16 10:48:25 +00:00
ENGINE = AggregatingMergeTree ORDER BY (domain_name, month)",
2023-12-12 18:45:00 +00:00
)
q(
ch1,
"CREATE MATERIALIZED VIEW monthly_aggregated_data_mv\
TO monthly_aggregated_data\
AS\
SELECT\
toDate(toStartOfMonth(event_time)) AS month,\
domain_name,\
sumState(count_views) AS sumCountViews\
FROM hourly_data\
GROUP BY\
domain_name,\
2023-12-16 10:48:25 +00:00
month",
2023-12-12 18:45:00 +00:00
)
q(
ch1,
"INSERT INTO hourly_data (domain_name, event_time, count_views)\
VALUES ('clickhouse.com', '2019-01-01 10:00:00', 1),\
('clickhouse.com', '2019-02-02 00:00:00', 2),\
('clickhouse.com', '2019-02-01 00:00:00', 3),\
2023-12-16 10:48:25 +00:00
('clickhouse.com', '2020-01-01 00:00:00', 6)",
2023-12-12 18:45:00 +00:00
)
2023-12-16 10:48:25 +00:00
2023-12-14 20:01:50 +00:00
def check_tables(converted):
engine_prefix = ""
2023-12-16 10:48:25 +00:00
if converted:
2023-12-14 20:01:50 +00:00
engine_prefix = "Replicated"
2023-12-12 18:45:00 +00:00
# Check engines
2023-12-16 10:48:25 +00:00
assert (
q(
ch1,
f"SELECT name, engine FROM system.tables WHERE database = '{database_name}'",
).strip()
== f"hourly_data\t{engine_prefix}MergeTree\nmonthly_aggregated_data\t{engine_prefix}AggregatingMergeTree\nmonthly_aggregated_data_mv\tMaterializedView"
)
2023-12-12 18:45:00 +00:00
# Check values
2023-12-16 10:48:25 +00:00
assert (
q(
ch1,
"SELECT sumMerge(sumCountViews) as sumCountViews\
FROM monthly_aggregated_data_mv",
).strip()
== "12"
)
assert q(ch1, "SELECT count() FROM hourly_data").strip() == "4"
2023-12-12 18:45:00 +00:00
2023-12-16 10:48:25 +00:00
if converted:
2023-12-14 20:01:50 +00:00
# Insert new values to check if new dependencies are set correctly
q(
ch1,
"INSERT INTO hourly_data (domain_name, event_time, count_views)\
VALUES ('clickhouse.com', '2019-01-01 10:00:00', 1),\
('clickhouse.com', '2019-02-02 00:00:00', 2),\
('clickhouse.com', '2019-02-01 00:00:00', 3),\
2023-12-16 10:48:25 +00:00
('clickhouse.com', '2020-01-01 00:00:00', 6)",
2023-12-14 20:01:50 +00:00
)
2023-12-12 18:45:00 +00:00
2023-12-16 10:48:25 +00:00
assert (
q(
ch1,
"SELECT sumMerge(sumCountViews) as sumCountViews\
FROM monthly_aggregated_data_mv",
).strip()
== "24"
)
assert q(ch1, "SELECT count() FROM hourly_data").strip() == "8"
2023-12-12 18:45:00 +00:00
def test_modify_engine_on_restart_with_materialized_view(started_cluster):
2023-12-14 20:01:50 +00:00
ch1.query(f"CREATE DATABASE {database_name}")
2023-12-12 18:45:00 +00:00
2023-12-14 20:01:50 +00:00
create_tables()
2023-12-12 18:45:00 +00:00
2023-12-14 20:01:50 +00:00
check_tables(False)
2023-12-12 18:45:00 +00:00
set_convert_flags(ch1, database_name, ["hourly_data", "monthly_aggregated_data"])
2023-12-12 18:45:00 +00:00
ch1.restart_clickhouse()
check_flags_deleted(ch1, database_name, ["hourly_data", "monthly_aggregated_data"])
2023-12-14 20:01:50 +00:00
check_tables(True)