2019-09-02 14:50:57 +00:00
|
|
|
import os
|
2020-09-16 04:26:10 +00:00
|
|
|
|
|
|
|
import pytest
|
2019-09-02 14:50:57 +00:00
|
|
|
from helpers.cluster import ClickHouseCluster
|
2020-09-16 04:26:10 +00:00
|
|
|
from helpers.test_tools import TSV
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
from .generate_dictionaries import (
|
|
|
|
generate_structure,
|
|
|
|
generate_dictionaries,
|
|
|
|
DictionaryTestTable,
|
|
|
|
)
|
2019-09-02 14:50:57 +00:00
|
|
|
|
|
|
|
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
|
|
|
cluster = None
|
|
|
|
instance = None
|
|
|
|
test_table = None
|
|
|
|
|
|
|
|
|
|
|
|
def setup_module(module):
|
|
|
|
global cluster
|
|
|
|
global instance
|
|
|
|
global test_table
|
|
|
|
|
|
|
|
structure = generate_structure()
|
2022-03-22 16:39:58 +00:00
|
|
|
dictionary_files = generate_dictionaries(
|
|
|
|
os.path.join(SCRIPT_DIR, "configs/dictionaries"), structure
|
|
|
|
)
|
2019-09-02 14:50:57 +00:00
|
|
|
|
2020-08-12 08:55:04 +00:00
|
|
|
cluster = ClickHouseCluster(__file__)
|
2022-03-22 16:39:58 +00:00
|
|
|
instance = cluster.add_instance("instance", dictionaries=dictionary_files)
|
|
|
|
test_table = DictionaryTestTable(
|
|
|
|
os.path.join(SCRIPT_DIR, "configs/dictionaries/source.tsv")
|
|
|
|
)
|
2019-09-02 14:50:57 +00:00
|
|
|
|
|
|
|
|
2020-08-12 08:55:04 +00:00
|
|
|
@pytest.fixture(scope="module", autouse=True)
|
2019-09-02 14:50:57 +00:00
|
|
|
def started_cluster():
|
|
|
|
try:
|
|
|
|
cluster.start()
|
|
|
|
test_table.create_clickhouse_source(instance)
|
2022-03-22 16:39:58 +00:00
|
|
|
for line in TSV(instance.query("select name from system.dictionaries")).lines:
|
|
|
|
print(line, end=" ")
|
2019-09-02 14:50:57 +00:00
|
|
|
|
|
|
|
yield cluster
|
|
|
|
|
|
|
|
finally:
|
|
|
|
cluster.shutdown()
|
|
|
|
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
@pytest.fixture(
|
|
|
|
params=[
|
|
|
|
# name, keys, use_parent
|
|
|
|
("clickhouse_hashed", ("id",), True),
|
|
|
|
("clickhouse_flat", ("id",), True),
|
|
|
|
("clickhouse_complex_integers_key_hashed", ("key0", "key1"), False),
|
|
|
|
("clickhouse_complex_mixed_key_hashed", ("key0_str", "key1"), False),
|
|
|
|
("clickhouse_range_hashed", ("id", "StartDate", "EndDate"), False),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"clickhouse_hashed",
|
|
|
|
"clickhouse_flat",
|
|
|
|
"clickhouse_complex_integers_key_hashed",
|
|
|
|
"clickhouse_complex_mixed_key_hashed",
|
|
|
|
"clickhouse_range_hashed",
|
|
|
|
],
|
2019-09-02 14:50:57 +00:00
|
|
|
)
|
|
|
|
def dictionary_structure(started_cluster, request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
|
|
|
def test_select_all(dictionary_structure):
|
|
|
|
name, keys, use_parent = dictionary_structure
|
|
|
|
query = instance.query
|
|
|
|
|
|
|
|
structure = test_table.get_structure_for_keys(keys, use_parent)
|
2022-03-22 16:39:58 +00:00
|
|
|
query(
|
|
|
|
"""
|
2019-09-02 14:50:57 +00:00
|
|
|
DROP TABLE IF EXISTS test.{0}
|
2022-03-22 16:39:58 +00:00
|
|
|
""".format(
|
|
|
|
name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
create_query = "CREATE TABLE test.{0} ({1}) engine = Dictionary({0})".format(
|
|
|
|
name, structure
|
|
|
|
)
|
2019-09-02 14:50:57 +00:00
|
|
|
TSV(query(create_query))
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = TSV(query("select * from test.{0}".format(name)))
|
2019-09-02 14:50:57 +00:00
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
diff = test_table.compare_by_keys(
|
|
|
|
keys, result.lines, use_parent, add_not_found_rows=True
|
|
|
|
)
|
2020-10-02 16:54:07 +00:00
|
|
|
print(test_table.process_diff(diff))
|
2019-09-02 14:50:57 +00:00
|
|
|
assert not diff
|
|
|
|
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
@pytest.fixture(
|
|
|
|
params=[
|
|
|
|
# name, keys, use_parent
|
|
|
|
("clickhouse_cache", ("id",), True),
|
|
|
|
("clickhouse_complex_integers_key_cache", ("key0", "key1"), False),
|
|
|
|
("clickhouse_complex_mixed_key_cache", ("key0_str", "key1"), False),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"clickhouse_cache",
|
|
|
|
"clickhouse_complex_integers_key_cache",
|
|
|
|
"clickhouse_complex_mixed_key_cache",
|
|
|
|
],
|
2019-09-02 14:50:57 +00:00
|
|
|
)
|
|
|
|
def cached_dictionary_structure(started_cluster, request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
|
|
|
def test_select_all_from_cached(cached_dictionary_structure):
|
|
|
|
name, keys, use_parent = cached_dictionary_structure
|
|
|
|
query = instance.query
|
|
|
|
|
|
|
|
structure = test_table.get_structure_for_keys(keys, use_parent)
|
2022-03-22 16:39:58 +00:00
|
|
|
query(
|
|
|
|
"""
|
2019-09-02 14:50:57 +00:00
|
|
|
DROP TABLE IF EXISTS test.{0}
|
2022-03-22 16:39:58 +00:00
|
|
|
""".format(
|
|
|
|
name
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
create_query = "CREATE TABLE test.{0} ({1}) engine = Dictionary({0})".format(
|
|
|
|
name, structure
|
|
|
|
)
|
2019-09-02 14:50:57 +00:00
|
|
|
TSV(query(create_query))
|
|
|
|
|
|
|
|
for i in range(4):
|
2022-03-22 16:39:58 +00:00
|
|
|
result = TSV(query("select * from test.{0}".format(name)))
|
|
|
|
diff = test_table.compare_by_keys(
|
|
|
|
keys, result.lines, use_parent, add_not_found_rows=False
|
|
|
|
)
|
2020-10-02 16:54:07 +00:00
|
|
|
print(test_table.process_diff(diff))
|
2019-09-02 14:50:57 +00:00
|
|
|
assert not diff
|
|
|
|
|
|
|
|
key = []
|
|
|
|
for key_name in keys:
|
2022-03-22 16:39:58 +00:00
|
|
|
if key_name.endswith("str"):
|
2019-09-02 14:50:57 +00:00
|
|
|
key.append("'" + str(i) + "'")
|
|
|
|
else:
|
|
|
|
key.append(str(i))
|
|
|
|
if len(key) == 1:
|
2022-03-22 16:39:58 +00:00
|
|
|
key = "toUInt64(" + str(i) + ")"
|
2019-09-02 14:50:57 +00:00
|
|
|
else:
|
2022-03-22 16:39:58 +00:00
|
|
|
key = str("(" + ",".join(key) + ")")
|
2019-09-02 14:50:57 +00:00
|
|
|
query("select dictGetUInt8('{0}', 'UInt8_', {1})".format(name, key))
|
|
|
|
|
2022-03-22 16:39:58 +00:00
|
|
|
result = TSV(query("select * from test.{0}".format(name)))
|
|
|
|
diff = test_table.compare_by_keys(
|
|
|
|
keys, result.lines, use_parent, add_not_found_rows=True
|
|
|
|
)
|
2020-10-02 16:54:07 +00:00
|
|
|
print(test_table.process_diff(diff))
|
2019-09-02 14:50:57 +00:00
|
|
|
assert not diff
|