Skip to content

Commit

Permalink
Medium int handling
Browse files Browse the repository at this point in the history
  • Loading branch information
bakwc committed Oct 31, 2024
1 parent b7e7a6c commit 2954e00
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 6 additions & 0 deletions mysql_ch_replicator/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ def convert_type(self, mysql_type, parameters):
if is_unsigned:
return 'UInt8'
return 'Int8'
if 'mediumint' in mysql_type:
if is_unsigned:
return 'UInt32'
return 'Int32'
if 'datetime' in mysql_type:
return mysql_type.replace('datetime', 'DateTime64')
if 'longtext' in mysql_type:
Expand Down Expand Up @@ -173,6 +177,8 @@ def convert_record(self, mysql_record, mysql_field_types, clickhouse_field_types
clickhouse_field_value = 65536 + clickhouse_field_value
if 'UInt8' in clickhouse_field_type and clickhouse_field_value < 0:
clickhouse_field_value = 256 + clickhouse_field_value
if 'mediumint' in mysql_field_type.lower() and clickhouse_field_value < 0:
clickhouse_field_value = 16777216 + clickhouse_field_value
clickhouse_record.append(clickhouse_field_value)
return tuple(clickhouse_record)

Expand Down
9 changes: 6 additions & 3 deletions test_mysql_ch_replicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,14 @@ def test_numeric_types_and_limits():
test2 smallint unsigned,
test3 TINYINT,
test4 TINYINT UNSIGNED,
test5 MEDIUMINT UNSIGNED,
PRIMARY KEY (id)
);
''')

mysql.execute(
f"INSERT INTO {TEST_TABLE_NAME} (name, test1, test2, test3, test4) VALUES ('Ivan', -20000, 50000, -30, 100);",
f"INSERT INTO {TEST_TABLE_NAME} (name, test1, test2, test3, test4, test5) VALUES "
f"('Ivan', -20000, 50000, -30, 100, 16777200);",
commit=True,
)

Expand All @@ -631,10 +633,11 @@ def test_numeric_types_and_limits():
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 1)

mysql.execute(
f"INSERT INTO {TEST_TABLE_NAME} (name, test1, test2, test3, test4) VALUES ('Peter', -10000, 60000, -120, 250);",
f"INSERT INTO {TEST_TABLE_NAME} (name, test1, test2, test3, test4, test5) VALUES "
f"('Peter', -10000, 60000, -120, 250, 16777200);",
commit=True,
)
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 2)
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME, 'test2=60000')) == 1)

assert_wait(lambda: len(ch.select(TEST_TABLE_NAME, 'test4=250')) == 1)
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME, 'test5=16777200')) == 2)

0 comments on commit 2954e00

Please sign in to comment.