Skip to content

Commit

Permalink
Fix for UNSIGNED NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
bakwc committed Nov 5, 2024
1 parent 908d53f commit e7d3c0f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
23 changes: 13 additions & 10 deletions mysql_ch_replicator/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,19 @@ def convert_record(self, mysql_record, mysql_field_types, clickhouse_field_types
if mysql_field_type == 'json' and 'String' in clickhouse_field_type:
if not isinstance(clickhouse_field_value, str):
clickhouse_field_value = json.dumps(convert_bytes(clickhouse_field_value))
if 'UInt16' in clickhouse_field_type and clickhouse_field_value < 0:
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
if 'UInt32' in clickhouse_field_type and clickhouse_field_value < 0:
clickhouse_field_value = 4294967296 + clickhouse_field_value
if 'UInt64' in clickhouse_field_type and clickhouse_field_value < 0:
clickhouse_field_value = 18446744073709551616 + clickhouse_field_value

if clickhouse_field_value is not None:
if 'UInt16' in clickhouse_field_type and clickhouse_field_value < 0:
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
if 'UInt32' in clickhouse_field_type and clickhouse_field_value < 0:
clickhouse_field_value = 4294967296 + clickhouse_field_value
if 'UInt64' in clickhouse_field_type and clickhouse_field_value < 0:
clickhouse_field_value = 18446744073709551616 + clickhouse_field_value

clickhouse_record.append(clickhouse_field_value)
return tuple(clickhouse_record)

Expand Down
9 changes: 5 additions & 4 deletions test_mysql_ch_replicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,13 +613,14 @@ def test_numeric_types_and_limits():
test5 MEDIUMINT UNSIGNED,
test6 INT UNSIGNED,
test7 BIGINT UNSIGNED,
test8 MEDIUMINT UNSIGNED NULL,
PRIMARY KEY (id)
);
''')

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

Expand All @@ -636,8 +637,8 @@ 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, test5, test6, test7) VALUES "
f"('Peter', -10000, 60000, -120, 250, 16777200, 4294967280, 18446744073709551586);",
f"INSERT INTO {TEST_TABLE_NAME} (name, test1, test2, test3, test4, test5, test6, test7, test8) VALUES "
f"('Peter', -10000, 60000, -120, 250, 16777200, 4294967280, 18446744073709551586, NULL);",
commit=True,
)
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 2)
Expand Down

0 comments on commit e7d3c0f

Please sign in to comment.