ClickHouse/tests/integration/test_dictionaries_dependency/test.py

117 lines
4.6 KiB
Python
Raw Normal View History

2020-07-03 13:36:23 +00:00
import pytest
from helpers.cluster import ClickHouseCluster
cluster = ClickHouseCluster(__file__)
node1 = cluster.add_instance('node1', stay_alive=True)
node2 = cluster.add_instance('node2', stay_alive=True, main_configs=['configs/disable_lazy_load.xml'])
nodes = [node1, node2]
@pytest.fixture(scope="module", autouse=True)
def start_cluster():
try:
cluster.start()
for node in nodes:
2020-09-22 11:56:40 +00:00
node.query("CREATE DATABASE IF NOT EXISTS test")
# Different internal dictionary name with Atomic
node.query("CREATE DATABASE IF NOT EXISTS test_ordinary ENGINE=Ordinary")
2020-07-03 13:36:23 +00:00
node.query("CREATE DATABASE IF NOT EXISTS atest")
node.query("CREATE DATABASE IF NOT EXISTS ztest")
node.query("CREATE TABLE test.source(x UInt64, y UInt64) ENGINE=Log")
node.query("INSERT INTO test.source VALUES (5,6)")
2020-09-22 11:56:40 +00:00
for db in ("test", "test_ordinary"):
node.query("CREATE DICTIONARY {}.dict(x UInt64, y UInt64) PRIMARY KEY x " \
"SOURCE(CLICKHOUSE(HOST 'localhost' PORT 9000 USER 'default' TABLE 'source' DB 'test')) " \
"LAYOUT(FLAT()) LIFETIME(0)".format(db))
2020-07-03 13:36:23 +00:00
yield cluster
finally:
cluster.shutdown()
@pytest.fixture(autouse=True)
def cleanup_after_test():
try:
yield
finally:
for node in nodes:
for i in range(4):
node.query("DROP DICTIONARY IF EXISTS test.other_{}".format(i))
2020-07-03 13:36:23 +00:00
node.query("DROP DICTIONARY IF EXISTS test.adict")
node.query("DROP DICTIONARY IF EXISTS test.zdict")
node.query("DROP DICTIONARY IF EXISTS atest.dict")
node.query("DROP DICTIONARY IF EXISTS ztest.dict")
node.query("DROP TABLE IF EXISTS test.atbl")
node.query("DROP TABLE IF EXISTS test.ztbl")
node.query("DROP TABLE IF EXISTS atest.tbl")
node.query("DROP TABLE IF EXISTS ztest.tbl")
node.query("DROP DATABASE IF EXISTS dict_db")
@pytest.mark.parametrize("node", nodes)
def test_dependency_via_implicit_table(node):
d_names = ["test.adict", "test.zdict", "atest.dict", "ztest.dict"]
for d_name in d_names:
node.query("CREATE DICTIONARY {}(x UInt64, y UInt64) PRIMARY KEY x " \
"SOURCE(CLICKHOUSE(HOST 'localhost' PORT 9000 USER 'default' TABLE 'dict' DB 'test')) " \
2020-07-03 13:36:23 +00:00
"LAYOUT(FLAT()) LIFETIME(0)".format(d_name))
2020-07-03 13:36:23 +00:00
def check():
for d_name in d_names:
assert node.query("SELECT dictGet({}, 'y', toUInt64(5))".format(d_name)) == "6\n"
2020-07-03 13:36:23 +00:00
check()
# Restart must not break anything.
node.restart_clickhouse()
check()
@pytest.mark.parametrize("node", nodes)
def test_dependency_via_explicit_table(node):
tbl_names = ["test.atbl", "test.ztbl", "atest.tbl", "ztest.tbl"]
d_names = ["test.other_{}".format(i) for i in range(0, len(tbl_names))]
for i in range(0, len(tbl_names)):
tbl_name = tbl_names[i]
tbl_database, tbl_shortname = tbl_name.split('.')
d_name = d_names[i]
node.query("CREATE TABLE {}(x UInt64, y UInt64) ENGINE=Dictionary('test.dict')".format(tbl_name))
node.query("CREATE DICTIONARY {}(x UInt64, y UInt64) PRIMARY KEY x " \
"SOURCE(CLICKHOUSE(HOST 'localhost' PORT 9000 USER 'default' TABLE '{}' DB '{}')) " \
2020-07-03 13:36:23 +00:00
"LAYOUT(FLAT()) LIFETIME(0)".format(d_name, tbl_shortname, tbl_database))
2020-07-03 13:36:23 +00:00
def check():
for d_name in d_names:
assert node.query("SELECT dictGet({}, 'y', toUInt64(5))".format(d_name)) == "6\n"
2020-07-03 13:36:23 +00:00
check()
# Restart must not break anything.
node.restart_clickhouse()
check()
@pytest.mark.parametrize("node", nodes)
def test_dependency_via_dictionary_database(node):
node.query("CREATE DATABASE dict_db ENGINE=Dictionary")
2020-09-22 11:56:40 +00:00
d_names = ["test_ordinary.adict", "test_ordinary.zdict", "atest.dict", "ztest.dict"]
2020-07-03 13:36:23 +00:00
for d_name in d_names:
node.query("CREATE DICTIONARY {}(x UInt64, y UInt64) PRIMARY KEY x " \
2020-09-22 11:56:40 +00:00
"SOURCE(CLICKHOUSE(HOST 'localhost' PORT 9000 USER 'default' TABLE 'test_ordinary.dict' DB 'dict_db')) " \
2020-07-03 13:36:23 +00:00
"LAYOUT(FLAT()) LIFETIME(0)".format(d_name))
2020-07-03 13:36:23 +00:00
def check():
for d_name in d_names:
assert node.query("SELECT dictGet({}, 'y', toUInt64(5))".format(d_name)) == "6\n"
2021-10-07 10:21:42 +00:00
for d_name in d_names:
assert node.query("SELECT dictGet({}, 'y', toUInt64(5))".format(d_name)) == "6\n"
2020-07-03 13:36:23 +00:00
# Restart must not break anything.
node.restart_clickhouse()
2021-10-07 10:21:42 +00:00
for d_name in d_names:
assert node.query_with_retry("SELECT dictGet({}, 'y', toUInt64(5))".format(d_name)) == "6\n"