2019-12-12 12:17:27 +00:00
|
|
|
import contextlib
|
2020-09-16 04:26:10 +00:00
|
|
|
import time
|
|
|
|
from string import Template
|
2019-06-10 09:40:33 +00:00
|
|
|
|
|
|
|
import pymysql.cursors
|
2019-12-12 12:17:27 +00:00
|
|
|
import pytest
|
2020-04-08 05:41:11 +00:00
|
|
|
from helpers.client import QueryRuntimeException
|
2020-09-16 04:26:10 +00:00
|
|
|
from helpers.cluster import ClickHouseCluster
|
2021-12-15 12:08:08 +00:00
|
|
|
from helpers.network import PartitionManager
|
2020-09-09 12:18:02 +00:00
|
|
|
|
2019-06-10 09:40:33 +00:00
|
|
|
cluster = ClickHouseCluster(__file__)
|
2021-12-15 12:08:08 +00:00
|
|
|
clickhouse_node = cluster.add_instance('node1', main_configs=['configs/remote_servers.xml', 'configs/named_collections.xml'], with_mysql=True, stay_alive=True)
|
2019-06-10 09:40:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def started_cluster():
|
|
|
|
try:
|
|
|
|
cluster.start()
|
|
|
|
yield cluster
|
|
|
|
finally:
|
|
|
|
cluster.shutdown()
|
|
|
|
|
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
class MySQLNodeInstance:
|
2021-03-09 07:32:10 +00:00
|
|
|
def __init__(self, user, password, hostname, port):
|
2019-12-12 12:17:27 +00:00
|
|
|
self.user = user
|
|
|
|
self.port = port
|
|
|
|
self.hostname = hostname
|
|
|
|
self.password = password
|
|
|
|
self.mysql_connection = None # lazy init
|
2021-12-15 12:08:08 +00:00
|
|
|
self.ip_address = hostname
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
def query(self, execution_query):
|
|
|
|
if self.mysql_connection is None:
|
2020-09-16 04:26:10 +00:00
|
|
|
self.mysql_connection = pymysql.connect(user=self.user, password=self.password, host=self.hostname,
|
|
|
|
port=self.port)
|
2019-12-12 12:17:27 +00:00
|
|
|
with self.mysql_connection.cursor() as cursor:
|
2020-09-09 12:18:02 +00:00
|
|
|
def execute(query):
|
|
|
|
res = cursor.execute(query)
|
|
|
|
if query.lstrip().lower().startswith(('select', 'show')):
|
|
|
|
# Mimic output of the ClickHouseInstance, which is:
|
|
|
|
# tab-sparated values and newline (\n)-separated rows.
|
|
|
|
rows = []
|
|
|
|
for row in cursor.fetchall():
|
|
|
|
rows.append("\t".join(str(item) for item in row))
|
|
|
|
res = "\n".join(rows)
|
|
|
|
return res
|
|
|
|
|
2020-10-02 16:54:07 +00:00
|
|
|
if isinstance(execution_query, (str, bytes)):
|
2020-09-09 12:18:02 +00:00
|
|
|
return execute(execution_query)
|
|
|
|
else:
|
|
|
|
return [execute(q) for q in execution_query]
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
def close(self):
|
|
|
|
if self.mysql_connection is not None:
|
|
|
|
self.mysql_connection.close()
|
2019-06-10 09:40:33 +00:00
|
|
|
|
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
def test_mysql_ddl_for_mysql_database(started_cluster):
|
2021-03-09 07:32:10 +00:00
|
|
|
with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', started_cluster.mysql_ip, started_cluster.mysql_port)) as mysql_node:
|
2021-02-15 09:35:45 +00:00
|
|
|
mysql_node.query("DROP DATABASE IF EXISTS test_database")
|
2019-12-12 12:17:27 +00:00
|
|
|
mysql_node.query("CREATE DATABASE test_database DEFAULT CHARACTER SET 'utf8'")
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2020-09-16 04:26:10 +00:00
|
|
|
clickhouse_node.query(
|
2021-02-15 09:35:45 +00:00
|
|
|
"CREATE DATABASE test_database ENGINE = MySQL('mysql57:3306', 'test_database', 'root', 'clickhouse')")
|
2019-12-12 12:17:27 +00:00
|
|
|
assert 'test_database' in clickhouse_node.query('SHOW DATABASES')
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2020-09-16 04:26:10 +00:00
|
|
|
mysql_node.query(
|
|
|
|
'CREATE TABLE `test_database`.`test_table` ( `id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;')
|
2019-12-12 12:17:27 +00:00
|
|
|
assert 'test_table' in clickhouse_node.query('SHOW TABLES FROM test_database')
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2020-09-16 04:26:10 +00:00
|
|
|
time.sleep(
|
|
|
|
3) # Because the unit of MySQL modification time is seconds, modifications made in the same second cannot be obtained
|
2019-12-12 12:17:27 +00:00
|
|
|
mysql_node.query('ALTER TABLE `test_database`.`test_table` ADD COLUMN `add_column` int(11)')
|
2020-09-16 04:26:10 +00:00
|
|
|
assert 'add_column' in clickhouse_node.query(
|
|
|
|
"SELECT name FROM system.columns WHERE table = 'test_table' AND database = 'test_database'")
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2020-09-16 04:26:10 +00:00
|
|
|
time.sleep(
|
|
|
|
3) # Because the unit of MySQL modification time is seconds, modifications made in the same second cannot be obtained
|
2019-12-12 12:17:27 +00:00
|
|
|
mysql_node.query('ALTER TABLE `test_database`.`test_table` DROP COLUMN `add_column`')
|
2020-09-16 04:26:10 +00:00
|
|
|
assert 'add_column' not in clickhouse_node.query(
|
|
|
|
"SELECT name FROM system.columns WHERE table = 'test_table' AND database = 'test_database'")
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
mysql_node.query('DROP TABLE `test_database`.`test_table`;')
|
|
|
|
assert 'test_table' not in clickhouse_node.query('SHOW TABLES FROM test_database')
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
clickhouse_node.query("DROP DATABASE test_database")
|
|
|
|
assert 'test_database' not in clickhouse_node.query('SHOW DATABASES')
|
2019-06-21 05:22:04 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
mysql_node.query("DROP DATABASE test_database")
|
2019-06-21 05:22:04 +00:00
|
|
|
|
2019-11-13 18:35:35 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
def test_clickhouse_ddl_for_mysql_database(started_cluster):
|
2021-03-09 07:32:10 +00:00
|
|
|
with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', started_cluster.mysql_ip, started_cluster.mysql_port)) as mysql_node:
|
2019-12-12 12:17:27 +00:00
|
|
|
mysql_node.query("CREATE DATABASE test_database DEFAULT CHARACTER SET 'utf8'")
|
2020-09-16 04:26:10 +00:00
|
|
|
mysql_node.query(
|
|
|
|
'CREATE TABLE `test_database`.`test_table` ( `id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;')
|
2019-11-13 18:35:35 +00:00
|
|
|
|
2020-09-16 04:26:10 +00:00
|
|
|
clickhouse_node.query(
|
2021-02-15 09:35:45 +00:00
|
|
|
"CREATE DATABASE test_database ENGINE = MySQL('mysql57:3306', 'test_database', 'root', 'clickhouse')")
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
assert 'test_table' in clickhouse_node.query('SHOW TABLES FROM test_database')
|
|
|
|
clickhouse_node.query("DROP TABLE test_database.test_table")
|
|
|
|
assert 'test_table' not in clickhouse_node.query('SHOW TABLES FROM test_database')
|
|
|
|
clickhouse_node.query("ATTACH TABLE test_database.test_table")
|
|
|
|
assert 'test_table' in clickhouse_node.query('SHOW TABLES FROM test_database')
|
|
|
|
clickhouse_node.query("DETACH TABLE test_database.test_table")
|
|
|
|
assert 'test_table' not in clickhouse_node.query('SHOW TABLES FROM test_database')
|
|
|
|
clickhouse_node.query("ATTACH TABLE test_database.test_table")
|
|
|
|
assert 'test_table' in clickhouse_node.query('SHOW TABLES FROM test_database')
|
2019-11-13 18:35:35 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
clickhouse_node.query("DROP DATABASE test_database")
|
|
|
|
assert 'test_database' not in clickhouse_node.query('SHOW DATABASES')
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
mysql_node.query("DROP DATABASE test_database")
|
2019-06-21 05:22:04 +00:00
|
|
|
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
def test_clickhouse_dml_for_mysql_database(started_cluster):
|
2021-03-09 07:32:10 +00:00
|
|
|
with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', started_cluster.mysql_ip, started_cluster.mysql_port)) as mysql_node:
|
2019-12-12 12:17:27 +00:00
|
|
|
mysql_node.query("CREATE DATABASE test_database DEFAULT CHARACTER SET 'utf8'")
|
2020-09-16 04:26:10 +00:00
|
|
|
mysql_node.query(
|
|
|
|
'CREATE TABLE `test_database`.`test_table` ( `i``d` int(11) NOT NULL, PRIMARY KEY (`i``d`)) ENGINE=InnoDB;')
|
|
|
|
clickhouse_node.query(
|
2021-02-15 09:35:45 +00:00
|
|
|
"CREATE DATABASE test_database ENGINE = MySQL('mysql57:3306', test_database, 'root', 'clickhouse')")
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
assert clickhouse_node.query("SELECT count() FROM `test_database`.`test_table`").rstrip() == '0'
|
2020-09-09 12:18:02 +00:00
|
|
|
clickhouse_node.query("INSERT INTO `test_database`.`test_table`(`i``d`) select number from numbers(10000)")
|
2019-12-12 12:17:27 +00:00
|
|
|
assert clickhouse_node.query("SELECT count() FROM `test_database`.`test_table`").rstrip() == '10000'
|
2019-06-10 09:40:33 +00:00
|
|
|
|
2020-09-28 15:00:01 +00:00
|
|
|
clickhouse_node.query("DROP DATABASE test_database")
|
|
|
|
assert 'test_database' not in clickhouse_node.query('SHOW DATABASES')
|
|
|
|
|
2019-12-12 12:17:27 +00:00
|
|
|
mysql_node.query("DROP DATABASE test_database")
|
2020-03-31 10:52:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_clickhouse_join_for_mysql_database(started_cluster):
|
2021-03-09 07:32:10 +00:00
|
|
|
with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', started_cluster.mysql_ip, started_cluster.mysql_port)) as mysql_node:
|
2020-03-31 10:52:26 +00:00
|
|
|
mysql_node.query("CREATE DATABASE IF NOT EXISTS test DEFAULT CHARACTER SET 'utf8'")
|
|
|
|
mysql_node.query("CREATE TABLE test.t1_mysql_local ("
|
|
|
|
"pays VARCHAR(55) DEFAULT 'FRA' NOT NULL,"
|
|
|
|
"service VARCHAR(5) DEFAULT '' NOT NULL,"
|
|
|
|
"opco CHAR(3) DEFAULT '' NOT NULL"
|
|
|
|
")")
|
|
|
|
mysql_node.query("CREATE TABLE test.t2_mysql_local ("
|
|
|
|
"service VARCHAR(5) DEFAULT '' NOT NULL,"
|
|
|
|
"opco VARCHAR(5) DEFAULT ''"
|
|
|
|
")")
|
2020-09-16 04:26:10 +00:00
|
|
|
clickhouse_node.query(
|
2021-02-15 09:35:45 +00:00
|
|
|
"CREATE TABLE default.t1_remote_mysql AS mysql('mysql57:3306','test','t1_mysql_local','root','clickhouse')")
|
2020-09-16 04:26:10 +00:00
|
|
|
clickhouse_node.query(
|
2021-02-15 09:35:45 +00:00
|
|
|
"CREATE TABLE default.t2_remote_mysql AS mysql('mysql57:3306','test','t2_mysql_local','root','clickhouse')")
|
2021-03-11 17:53:29 +00:00
|
|
|
clickhouse_node.query("INSERT INTO `default`.`t1_remote_mysql` VALUES ('EN','A',''),('RU','B','AAA')")
|
|
|
|
clickhouse_node.query("INSERT INTO `default`.`t2_remote_mysql` VALUES ('A','AAA'),('Z','')")
|
2021-04-02 10:34:49 +00:00
|
|
|
|
2020-03-31 10:52:26 +00:00
|
|
|
assert clickhouse_node.query("SELECT s.pays "
|
2020-04-08 05:41:11 +00:00
|
|
|
"FROM default.t1_remote_mysql AS s "
|
|
|
|
"LEFT JOIN default.t1_remote_mysql AS s_ref "
|
2021-03-11 17:53:29 +00:00
|
|
|
"ON (s_ref.opco = s.opco AND s_ref.service = s.service) "
|
|
|
|
"WHERE s_ref.opco != '' AND s.opco != '' ").rstrip() == 'RU'
|
2020-03-31 10:52:26 +00:00
|
|
|
mysql_node.query("DROP DATABASE test")
|
2020-04-07 05:14:49 +00:00
|
|
|
|
2020-04-08 05:41:11 +00:00
|
|
|
|
2020-04-07 05:14:49 +00:00
|
|
|
def test_bad_arguments_for_mysql_database_engine(started_cluster):
|
2021-03-09 07:32:10 +00:00
|
|
|
with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', started_cluster.mysql_ip, port=started_cluster.mysql_port)) as mysql_node:
|
2020-04-08 05:41:11 +00:00
|
|
|
with pytest.raises(QueryRuntimeException) as exception:
|
|
|
|
mysql_node.query("CREATE DATABASE IF NOT EXISTS test_bad_arguments DEFAULT CHARACTER SET 'utf8'")
|
2020-09-16 04:26:10 +00:00
|
|
|
clickhouse_node.query(
|
2021-02-15 09:35:45 +00:00
|
|
|
"CREATE DATABASE test_database_bad_arguments ENGINE = MySQL('mysql57:3306', test_bad_arguments, root, 'clickhouse')")
|
2020-04-08 05:41:11 +00:00
|
|
|
assert 'Database engine MySQL requested literal argument.' in str(exception.value)
|
|
|
|
mysql_node.query("DROP DATABASE test_bad_arguments")
|
2020-09-09 12:18:02 +00:00
|
|
|
|
2021-06-28 13:50:43 +00:00
|
|
|
def test_column_comments_for_mysql_database_engine(started_cluster):
|
|
|
|
with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', started_cluster.mysql_ip, started_cluster.mysql_port)) as mysql_node:
|
|
|
|
mysql_node.query("DROP DATABASE IF EXISTS test_database")
|
|
|
|
mysql_node.query("CREATE DATABASE test_database DEFAULT CHARACTER SET 'utf8'")
|
|
|
|
|
|
|
|
clickhouse_node.query(
|
|
|
|
"CREATE DATABASE test_database ENGINE = MySQL('mysql57:3306', 'test_database', 'root', 'clickhouse')")
|
|
|
|
assert 'test_database' in clickhouse_node.query('SHOW DATABASES')
|
|
|
|
|
|
|
|
mysql_node.query(
|
|
|
|
"CREATE TABLE `test_database`.`test_table` ( `id` int(11) NOT NULL, PRIMARY KEY (`id`), `test` int COMMENT 'test comment') ENGINE=InnoDB;")
|
|
|
|
assert 'test comment' in clickhouse_node.query('DESCRIBE TABLE `test_database`.`test_table`')
|
|
|
|
|
2021-06-29 10:07:18 +00:00
|
|
|
time.sleep(
|
|
|
|
3) # Because the unit of MySQL modification time is seconds, modifications made in the same second cannot be obtained
|
2021-06-28 13:50:43 +00:00
|
|
|
mysql_node.query("ALTER TABLE `test_database`.`test_table` ADD COLUMN `add_column` int(11) COMMENT 'add_column comment'")
|
|
|
|
assert 'add_column comment' in clickhouse_node.query(
|
|
|
|
"SELECT comment FROM system.columns WHERE table = 'test_table' AND database = 'test_database'")
|
2021-06-29 07:22:08 +00:00
|
|
|
|
2021-06-30 07:03:03 +00:00
|
|
|
clickhouse_node.query("DROP DATABASE test_database")
|
2021-06-28 13:50:43 +00:00
|
|
|
mysql_node.query("DROP DATABASE test_database")
|
2021-06-29 07:22:08 +00:00
|
|
|
|
2020-09-09 12:18:02 +00:00
|
|
|
|
2020-09-28 10:27:02 +00:00
|
|
|
def test_data_types_support_level_for_mysql_database_engine(started_cluster):
|
2021-03-09 07:32:10 +00:00
|
|
|
with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', started_cluster.mysql_ip, started_cluster.mysql_port)) as mysql_node:
|
2020-09-28 10:27:02 +00:00
|
|
|
mysql_node.query("CREATE DATABASE IF NOT EXISTS test DEFAULT CHARACTER SET 'utf8'")
|
2021-02-15 09:35:45 +00:00
|
|
|
clickhouse_node.query("CREATE DATABASE test_database ENGINE = MySQL('mysql57:3306', test, 'root', 'clickhouse')",
|
2020-09-28 10:27:02 +00:00
|
|
|
settings={"mysql_datatypes_support_level": "decimal,datetime64"})
|
|
|
|
|
2020-09-29 04:17:49 +00:00
|
|
|
assert "SETTINGS mysql_datatypes_support_level = \\'decimal,datetime64\\'" in clickhouse_node.query("SHOW CREATE DATABASE test_database FORMAT TSV")
|
2020-09-28 10:27:02 +00:00
|
|
|
clickhouse_node.query("DETACH DATABASE test_database")
|
|
|
|
|
|
|
|
# without context settings
|
|
|
|
clickhouse_node.query("ATTACH DATABASE test_database")
|
2020-09-29 04:17:49 +00:00
|
|
|
assert "SETTINGS mysql_datatypes_support_level = \\'decimal,datetime64\\'" in clickhouse_node.query("SHOW CREATE DATABASE test_database FORMAT TSV")
|
2020-09-28 10:27:02 +00:00
|
|
|
|
|
|
|
clickhouse_node.query(
|
2021-02-15 09:35:45 +00:00
|
|
|
"CREATE DATABASE test_database_1 ENGINE = MySQL('mysql57:3306', test, 'root', 'clickhouse') SETTINGS mysql_datatypes_support_level = 'decimal,datetime64'",
|
2020-09-28 10:27:02 +00:00
|
|
|
settings={"mysql_datatypes_support_level": "decimal"})
|
|
|
|
|
2020-09-29 04:17:49 +00:00
|
|
|
assert "SETTINGS mysql_datatypes_support_level = \\'decimal,datetime64\\'" in clickhouse_node.query("SHOW CREATE DATABASE test_database_1 FORMAT TSV")
|
2020-09-28 10:27:02 +00:00
|
|
|
clickhouse_node.query("DETACH DATABASE test_database_1")
|
|
|
|
|
|
|
|
# without context settings
|
|
|
|
clickhouse_node.query("ATTACH DATABASE test_database_1")
|
2020-09-29 04:17:49 +00:00
|
|
|
assert "SETTINGS mysql_datatypes_support_level = \\'decimal,datetime64\\'" in clickhouse_node.query("SHOW CREATE DATABASE test_database_1 FORMAT TSV")
|
2020-09-28 10:27:02 +00:00
|
|
|
|
2020-09-28 15:00:01 +00:00
|
|
|
clickhouse_node.query("DROP DATABASE test_database")
|
2020-09-29 06:37:19 +00:00
|
|
|
clickhouse_node.query("DROP DATABASE test_database_1")
|
2020-09-28 15:00:01 +00:00
|
|
|
assert 'test_database' not in clickhouse_node.query('SHOW DATABASES')
|
2020-09-28 10:27:02 +00:00
|
|
|
mysql_node.query("DROP DATABASE test")
|
|
|
|
|
|
|
|
|
2021-01-26 10:37:57 +00:00
|
|
|
# test tool cannot support null by now. TSV format returns \N for null, so cannot compare using == directly
|
|
|
|
# float_values = ['NULL']
|
|
|
|
# float_values = [0] mysql returns 0 while clickhouse returns 0.0, so cannot compare using == directly
|
|
|
|
int32_values = [0, 1, -1, 2147483647, -2147483648]
|
|
|
|
uint32_values = [0, 1] # [FIXME] seems client have issue with value 4294967295, it returns -1 for it
|
|
|
|
mint_values = [0, 1, -1, 8388607, -8388608]
|
|
|
|
umint_values = [0, 1, 16777215]
|
|
|
|
int16_values = [0, 1, -1, 32767, -32768]
|
|
|
|
uint16_values = [0, 1, 65535]
|
|
|
|
int8_values = [0, 1, -1, 127, -128]
|
|
|
|
uint8_values = [0, 1, 255]
|
|
|
|
# string_values = ["'ClickHouse'", 'NULL']
|
2021-08-16 08:10:59 +00:00
|
|
|
string_values = ["'ClickHouse'"]
|
2021-01-26 10:37:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
decimal_values = [0, 0.123, 0.4, 5.67, 8.91011, 123456789.123, -0.123, -0.4, -5.67, -8.91011, -123456789.123]
|
|
|
|
timestamp_values = ["'2015-05-18 07:40:01.123'", "'2019-09-16 19:20:11.123'"]
|
|
|
|
timestamp_values_no_subsecond = ["'2015-05-18 07:40:01'", "'2019-09-16 19:20:11'"]
|
2020-09-09 12:18:02 +00:00
|
|
|
|
2020-09-16 04:26:10 +00:00
|
|
|
|
2020-09-09 12:18:02 +00:00
|
|
|
@pytest.mark.parametrize("case_name, mysql_type, expected_ch_type, mysql_values, setting_mysql_datatypes_support_level",
|
2020-09-16 04:26:10 +00:00
|
|
|
[
|
2021-01-26 10:37:57 +00:00
|
|
|
# test common type mapping
|
|
|
|
# ("common_types", "FLOAT", "Nullable(Float32)", float_values, ""),
|
|
|
|
# ("common_types", "FLOAT UNSIGNED", "Nullable(Float32)", float_values, ""),
|
|
|
|
|
2021-03-19 11:48:28 +00:00
|
|
|
pytest.param("common_types", "INT", "Nullable(Int32)", int32_values, "", id="common_types_1"),
|
|
|
|
pytest.param("common_types", "INT NOT NULL", "Int32", int32_values, "", id="common_types_2"),
|
|
|
|
pytest.param("common_types", "INT UNSIGNED NOT NULL", "UInt32", uint32_values, "", id="common_types_3"),
|
|
|
|
pytest.param("common_types", "INT UNSIGNED", "Nullable(UInt32)", uint32_values, "", id="common_types_4"),
|
|
|
|
pytest.param("common_types", "INT UNSIGNED DEFAULT NULL", "Nullable(UInt32)", uint32_values, "", id="common_types_5"),
|
|
|
|
pytest.param("common_types", "INT UNSIGNED DEFAULT '1'", "Nullable(UInt32)", uint32_values, "", id="common_types_6"),
|
|
|
|
pytest.param("common_types", "INT(10)", "Nullable(Int32)", int32_values, "", id="common_types_7"),
|
|
|
|
pytest.param("common_types", "INT(10) NOT NULL", "Int32", int32_values, "", id="common_types_8"),
|
|
|
|
pytest.param("common_types", "INT(10) UNSIGNED NOT NULL", "UInt32", uint32_values, "", id="common_types_8"),
|
|
|
|
pytest.param("common_types", "INT(10) UNSIGNED", "Nullable(UInt32)", uint32_values, "", id="common_types_9"),
|
|
|
|
pytest.param("common_types", "INT(10) UNSIGNED DEFAULT NULL", "Nullable(UInt32)", uint32_values, "", id="common_types_10"),
|
|
|
|
pytest.param("common_types", "INT(10) UNSIGNED DEFAULT '1'", "Nullable(UInt32)", uint32_values, "", id="common_types_11"),
|
|
|
|
pytest.param("common_types", "INTEGER", "Nullable(Int32)", int32_values, "", id="common_types_12"),
|
|
|
|
pytest.param("common_types", "INTEGER UNSIGNED", "Nullable(UInt32)", uint32_values, "", id="common_types_13"),
|
|
|
|
|
|
|
|
pytest.param("common_types", "MEDIUMINT", "Nullable(Int32)", mint_values, "", id="common_types_14"),
|
|
|
|
pytest.param("common_types", "MEDIUMINT UNSIGNED", "Nullable(UInt32)", umint_values, "", id="common_types_15"),
|
|
|
|
|
|
|
|
pytest.param("common_types", "SMALLINT", "Nullable(Int16)", int16_values, "", id="common_types_16"),
|
|
|
|
pytest.param("common_types", "SMALLINT UNSIGNED", "Nullable(UInt16)", uint16_values, "", id="common_types_17"),
|
|
|
|
|
|
|
|
pytest.param("common_types", "TINYINT", "Nullable(Int8)", int8_values, "", id="common_types_18"),
|
|
|
|
pytest.param("common_types", "TINYINT UNSIGNED", "Nullable(UInt8)", uint8_values, "", id="common_types_19"),
|
|
|
|
|
|
|
|
pytest.param("common_types", "VARCHAR(10)", "Nullable(String)", string_values, "", id="common_types_20"),
|
|
|
|
|
|
|
|
|
|
|
|
pytest.param("decimal_default", "decimal NOT NULL", "Decimal(10, 0)", decimal_values,
|
|
|
|
"decimal,datetime64", id="decimal_1"),
|
|
|
|
pytest.param("decimal_default_nullable", "decimal", "Nullable(Decimal(10, 0))", decimal_values,
|
|
|
|
"decimal,datetime64", id="decimal_2"),
|
|
|
|
pytest.param("decimal_18_6", "decimal(18, 6) NOT NULL", "Decimal(18, 6)", decimal_values,
|
|
|
|
"decimal,datetime64", id="decimal_3"),
|
|
|
|
pytest.param("decimal_38_6", "decimal(38, 6) NOT NULL", "Decimal(38, 6)", decimal_values,
|
|
|
|
"decimal,datetime64", id="decimal_4"),
|
2020-09-16 04:26:10 +00:00
|
|
|
|
|
|
|
# Due to python DB driver roundtrip MySQL timestamp and datetime values
|
|
|
|
# are printed with 6 digits after decimal point, so to simplify tests a bit,
|
|
|
|
# we only validate precision of 0 and 6.
|
2021-03-19 11:48:28 +00:00
|
|
|
pytest.param("timestamp_default", "timestamp", "DateTime", timestamp_values, "decimal,datetime64", id="timestamp_default"),
|
|
|
|
pytest.param("timestamp_6", "timestamp(6)", "DateTime64(6)", timestamp_values, "decimal,datetime64", id="timestamp_6"),
|
|
|
|
pytest.param("datetime_default", "DATETIME NOT NULL", "DateTime64(0)", timestamp_values,
|
|
|
|
"decimal,datetime64", id="datetime_default"),
|
|
|
|
pytest.param("datetime_6", "DATETIME(6) NOT NULL", "DateTime64(6)", timestamp_values,
|
|
|
|
"decimal,datetime64", id="datetime_6_1"),
|
2020-09-16 04:26:10 +00:00
|
|
|
|
|
|
|
# right now precision bigger than 39 is not supported by ClickHouse's Decimal, hence fall back to String
|
2021-03-19 11:48:28 +00:00
|
|
|
pytest.param("decimal_40_6", "decimal(40, 6) NOT NULL", "String", decimal_values,
|
|
|
|
"decimal,datetime64", id="decimal_40_6"),
|
|
|
|
pytest.param("decimal_18_6", "decimal(18, 6) NOT NULL", "String", decimal_values, "datetime64", id="decimal_18_6_1"),
|
|
|
|
pytest.param("decimal_18_6", "decimal(18, 6) NOT NULL", "String", decimal_values, "", id="decimal_18_6_2"),
|
|
|
|
pytest.param("datetime_6", "DATETIME(6) NOT NULL", "DateTime", timestamp_values_no_subsecond,
|
|
|
|
"decimal", id="datetime_6_2"),
|
|
|
|
pytest.param("datetime_6", "DATETIME(6) NOT NULL", "DateTime", timestamp_values_no_subsecond, "", id="datetime_6_3"),
|
2020-09-16 04:26:10 +00:00
|
|
|
])
|
|
|
|
def test_mysql_types(started_cluster, case_name, mysql_type, expected_ch_type, mysql_values,
|
|
|
|
setting_mysql_datatypes_support_level):
|
2020-09-09 12:18:02 +00:00
|
|
|
""" Verify that values written to MySQL can be read on ClickHouse side via DB engine MySQL,
|
|
|
|
or Table engine MySQL, or mysql() table function.
|
|
|
|
Make sure that type is converted properly and values match exactly.
|
|
|
|
"""
|
|
|
|
|
|
|
|
substitutes = dict(
|
2020-09-16 04:26:10 +00:00
|
|
|
mysql_db='decimal_support',
|
|
|
|
table_name=case_name,
|
|
|
|
mysql_type=mysql_type,
|
2021-01-26 10:37:57 +00:00
|
|
|
mysql_values=', '.join('({})'.format(x) for x in mysql_values),
|
2020-09-16 04:26:10 +00:00
|
|
|
ch_mysql_db='mysql_db',
|
|
|
|
ch_mysql_table='mysql_table_engine_' + case_name,
|
|
|
|
expected_ch_type=expected_ch_type,
|
2020-09-09 12:18:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
clickhouse_query_settings = dict(
|
2021-08-16 08:10:59 +00:00
|
|
|
mysql_datatypes_support_level=setting_mysql_datatypes_support_level,
|
|
|
|
output_format_decimal_trailing_zeros=1
|
2020-09-09 12:18:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def execute_query(node, query, **kwargs):
|
|
|
|
def do_execute(query):
|
|
|
|
query = Template(query).safe_substitute(substitutes)
|
|
|
|
res = node.query(query, **kwargs)
|
|
|
|
return res if isinstance(res, int) else res.rstrip('\n\r')
|
|
|
|
|
2020-10-02 16:54:07 +00:00
|
|
|
if isinstance(query, (str, bytes)):
|
2020-09-09 12:18:02 +00:00
|
|
|
return do_execute(query)
|
|
|
|
else:
|
|
|
|
return [do_execute(q) for q in query]
|
|
|
|
|
2021-03-09 07:32:10 +00:00
|
|
|
with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', started_cluster.mysql_ip, port=started_cluster.mysql_port)) as mysql_node:
|
2020-09-09 12:18:02 +00:00
|
|
|
execute_query(mysql_node, [
|
|
|
|
"DROP DATABASE IF EXISTS ${mysql_db}",
|
|
|
|
"CREATE DATABASE ${mysql_db} DEFAULT CHARACTER SET 'utf8'",
|
|
|
|
"CREATE TABLE `${mysql_db}`.`${table_name}` (value ${mysql_type})",
|
|
|
|
"INSERT INTO `${mysql_db}`.`${table_name}` (value) VALUES ${mysql_values}",
|
|
|
|
"SELECT * FROM `${mysql_db}`.`${table_name}`",
|
|
|
|
"FLUSH TABLES"
|
|
|
|
])
|
|
|
|
|
|
|
|
assert execute_query(mysql_node, "SELECT COUNT(*) FROM ${mysql_db}.${table_name}") \
|
2020-09-16 04:26:10 +00:00
|
|
|
== \
|
|
|
|
"{}".format(len(mysql_values))
|
2020-09-09 12:18:02 +00:00
|
|
|
|
|
|
|
# MySQL TABLE ENGINE
|
|
|
|
execute_query(clickhouse_node, [
|
|
|
|
"DROP TABLE IF EXISTS ${ch_mysql_table};",
|
2021-02-15 09:35:45 +00:00
|
|
|
"CREATE TABLE ${ch_mysql_table} (value ${expected_ch_type}) ENGINE = MySQL('mysql57:3306', '${mysql_db}', '${table_name}', 'root', 'clickhouse')",
|
2020-09-09 12:18:02 +00:00
|
|
|
], settings=clickhouse_query_settings)
|
|
|
|
|
|
|
|
# Validate type
|
|
|
|
assert \
|
|
|
|
execute_query(clickhouse_node, "SELECT toTypeName(value) FROM ${ch_mysql_table} LIMIT 1",
|
2020-09-16 04:26:10 +00:00
|
|
|
settings=clickhouse_query_settings) \
|
2020-09-09 12:18:02 +00:00
|
|
|
== \
|
|
|
|
expected_ch_type
|
|
|
|
|
|
|
|
# Validate values
|
|
|
|
assert \
|
|
|
|
execute_query(clickhouse_node, "SELECT value FROM ${ch_mysql_table}",
|
2020-09-16 04:26:10 +00:00
|
|
|
settings=clickhouse_query_settings) \
|
2020-09-09 12:18:02 +00:00
|
|
|
== \
|
|
|
|
execute_query(mysql_node, "SELECT value FROM ${mysql_db}.${table_name}")
|
|
|
|
|
|
|
|
# MySQL DATABASE ENGINE
|
|
|
|
execute_query(clickhouse_node, [
|
|
|
|
"DROP DATABASE IF EXISTS ${ch_mysql_db}",
|
2021-02-15 09:35:45 +00:00
|
|
|
"CREATE DATABASE ${ch_mysql_db} ENGINE = MySQL('mysql57:3306', '${mysql_db}', 'root', 'clickhouse')"
|
2020-09-09 12:18:02 +00:00
|
|
|
], settings=clickhouse_query_settings)
|
|
|
|
|
|
|
|
# Validate type
|
|
|
|
assert \
|
|
|
|
execute_query(clickhouse_node, "SELECT toTypeName(value) FROM ${ch_mysql_db}.${table_name} LIMIT 1",
|
2020-09-16 04:26:10 +00:00
|
|
|
settings=clickhouse_query_settings) \
|
2020-09-09 12:18:02 +00:00
|
|
|
== \
|
|
|
|
expected_ch_type
|
|
|
|
|
|
|
|
# Validate values
|
|
|
|
assert \
|
|
|
|
execute_query(clickhouse_node, "SELECT value FROM ${ch_mysql_db}.${table_name}",
|
2020-09-16 04:26:10 +00:00
|
|
|
settings=clickhouse_query_settings) \
|
2020-09-09 12:18:02 +00:00
|
|
|
== \
|
|
|
|
execute_query(mysql_node, "SELECT value FROM ${mysql_db}.${table_name}")
|
|
|
|
|
|
|
|
# MySQL TABLE FUNCTION
|
|
|
|
# Validate type
|
|
|
|
assert \
|
2020-09-16 04:26:10 +00:00
|
|
|
execute_query(clickhouse_node,
|
2021-02-15 09:35:45 +00:00
|
|
|
"SELECT toTypeName(value) FROM mysql('mysql57:3306', '${mysql_db}', '${table_name}', 'root', 'clickhouse') LIMIT 1",
|
2020-09-16 04:26:10 +00:00
|
|
|
settings=clickhouse_query_settings) \
|
2020-09-09 12:18:02 +00:00
|
|
|
== \
|
|
|
|
expected_ch_type
|
|
|
|
|
|
|
|
# Validate values
|
|
|
|
assert \
|
|
|
|
execute_query(mysql_node, "SELECT value FROM ${mysql_db}.${table_name}") \
|
|
|
|
== \
|
2020-09-16 04:26:10 +00:00
|
|
|
execute_query(clickhouse_node,
|
2021-02-15 09:35:45 +00:00
|
|
|
"SELECT value FROM mysql('mysql57:3306', '${mysql_db}', '${table_name}', 'root', 'clickhouse')",
|
2020-09-16 04:26:10 +00:00
|
|
|
settings=clickhouse_query_settings)
|
2021-09-02 13:01:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_predefined_connection_configuration(started_cluster):
|
|
|
|
with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', started_cluster.mysql_ip, started_cluster.mysql_port)) as mysql_node:
|
|
|
|
mysql_node.query("DROP DATABASE IF EXISTS test_database")
|
|
|
|
mysql_node.query("CREATE DATABASE test_database DEFAULT CHARACTER SET 'utf8'")
|
|
|
|
mysql_node.query('CREATE TABLE `test_database`.`test_table` ( `id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;')
|
|
|
|
|
|
|
|
clickhouse_node.query("DROP DATABASE IF EXISTS test_database")
|
|
|
|
clickhouse_node.query("CREATE DATABASE test_database ENGINE = MySQL(mysql1)")
|
|
|
|
clickhouse_node.query("INSERT INTO `test_database`.`test_table` select number from numbers(100)")
|
|
|
|
assert clickhouse_node.query("SELECT count() FROM `test_database`.`test_table`").rstrip() == '100'
|
|
|
|
|
|
|
|
clickhouse_node.query("DROP DATABASE test_database")
|
|
|
|
clickhouse_node.query_and_get_error("CREATE DATABASE test_database ENGINE = MySQL(mysql2)")
|
|
|
|
clickhouse_node.query_and_get_error("CREATE DATABASE test_database ENGINE = MySQL(unknown_collection)")
|
|
|
|
clickhouse_node.query_and_get_error("CREATE DATABASE test_database ENGINE = MySQL(mysql1, 1)")
|
|
|
|
|
|
|
|
clickhouse_node.query("CREATE DATABASE test_database ENGINE = MySQL(mysql1, port=3306)")
|
|
|
|
assert clickhouse_node.query("SELECT count() FROM `test_database`.`test_table`").rstrip() == '100'
|
2021-12-15 12:08:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_restart_server(started_cluster):
|
|
|
|
with contextlib.closing(MySQLNodeInstance('root', 'clickhouse', started_cluster.mysql_ip, started_cluster.mysql_port)) as mysql_node:
|
|
|
|
mysql_node.query("DROP DATABASE IF EXISTS test_restart")
|
|
|
|
clickhouse_node.query("DROP DATABASE IF EXISTS test_restart")
|
2021-12-16 16:00:36 +00:00
|
|
|
clickhouse_node.query_and_get_error("CREATE DATABASE test_restart ENGINE = MySQL('mysql57:3306', 'test_restart', 'root', 'clickhouse')")
|
2021-12-15 12:08:08 +00:00
|
|
|
assert 'test_restart' not in clickhouse_node.query('SHOW DATABASES')
|
|
|
|
|
|
|
|
mysql_node.query("CREATE DATABASE test_restart DEFAULT CHARACTER SET 'utf8'")
|
|
|
|
mysql_node.query("CREATE TABLE `test_restart`.`test_table` ( `id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB;")
|
|
|
|
clickhouse_node.query("CREATE DATABASE test_restart ENGINE = MySQL('mysql57:3306', 'test_restart', 'root', 'clickhouse')")
|
|
|
|
|
|
|
|
assert 'test_restart' in clickhouse_node.query('SHOW DATABASES')
|
|
|
|
assert 'test_table' in clickhouse_node.query('SHOW TABLES FROM test_restart')
|
|
|
|
|
|
|
|
with PartitionManager() as pm:
|
|
|
|
pm.partition_instances(clickhouse_node, mysql_node, action='REJECT --reject-with tcp-reset')
|
|
|
|
clickhouse_node.restart_clickhouse()
|
|
|
|
clickhouse_node.query_and_get_error('SHOW TABLES FROM test_restart')
|
|
|
|
assert 'test_table' in clickhouse_node.query('SHOW TABLES FROM test_restart')
|