Skip to content

Commit 97eaf8c

Browse files
authored
Use DateTime64 for timestamp mysql type (instead of string) (#81)
1 parent 507f2e8 commit 97eaf8c

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

mysql_ch_replicator/converter.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ def strip_sql_comments(sql_statement):
158158
return sqlparse.format(sql_statement, strip_comments=True).strip()
159159

160160

161+
def convert_timestamp_to_datetime64(input_str):
162+
163+
# Define the regex pattern
164+
pattern = r'^timestamp(?:\((\d+)\))?$'
165+
166+
# Attempt to match the pattern
167+
match = re.match(pattern, input_str.strip(), re.IGNORECASE)
168+
169+
if match:
170+
# If a precision is provided, include it in the replacement
171+
precision = match.group(1)
172+
if precision is not None:
173+
return f'DateTime64({precision})'
174+
else:
175+
return 'DateTime64'
176+
else:
177+
raise ValueError(f"Invalid input string format: '{input_str}'")
178+
179+
161180
class MysqlToClickhouseConverter:
162181
def __init__(self, db_replicator: 'DbReplicator' = None):
163182
self.db_replicator = db_replicator
@@ -238,6 +257,8 @@ def convert_type(self, mysql_type, parameters):
238257
return 'Int32'
239258
if 'real' in mysql_type:
240259
return 'Float64'
260+
if mysql_type.startswith('timestamp'):
261+
return convert_timestamp_to_datetime64(mysql_type)
241262
if mysql_type.startswith('time'):
242263
return 'String'
243264
if 'varbinary' in mysql_type:

test_mysql_ch_replicator.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import os
23
import shutil
34
import time
@@ -887,13 +888,14 @@ def test_different_types_2():
887888
test2 point,
888889
test3 binary(16),
889890
test4 set('1','2','3','4','5','6','7'),
891+
test5 timestamp(0),
890892
PRIMARY KEY (id)
891893
);
892894
''')
893895

894896
mysql.execute(
895-
f"INSERT INTO {TEST_TABLE_NAME} (test1, test2, test3, test4) VALUES "
896-
f"(0, POINT(10.0, 20.0), 'azaza', '1,3,5');",
897+
f"INSERT INTO {TEST_TABLE_NAME} (test1, test2, test3, test4, test5) VALUES "
898+
f"(0, POINT(10.0, 20.0), 'azaza', '1,3,5', '2023-08-15 14:30:00');",
897899
commit=True,
898900
)
899901

@@ -910,8 +912,8 @@ def test_different_types_2():
910912
assert_wait(lambda: len(ch.select(TEST_TABLE_NAME)) == 1)
911913

912914
mysql.execute(
913-
f"INSERT INTO {TEST_TABLE_NAME} (test1, test2, test4) VALUES "
914-
f"(1, POINT(15.0, 14.0), '2,4,5');",
915+
f"INSERT INTO {TEST_TABLE_NAME} (test1, test2, test4, test5) VALUES "
916+
f"(1, POINT(15.0, 14.0), '2,4,5', '2023-08-15 14:40:00');",
915917
commit=True,
916918
)
917919

@@ -925,6 +927,10 @@ def test_different_types_2():
925927
assert ch.select(TEST_TABLE_NAME, 'test1=True')[0]['test4'] == '2,4,5'
926928
assert ch.select(TEST_TABLE_NAME, 'test1=False')[0]['test4'] == '1,3,5'
927929

930+
value = ch.select(TEST_TABLE_NAME, 'test1=True')[0]['test5']
931+
assert isinstance(value, datetime.datetime)
932+
assert str(value) == '2023-08-15 14:40:00+00:00'
933+
928934
mysql.execute(
929935
f"INSERT INTO {TEST_TABLE_NAME} (test1, test2) VALUES "
930936
f"(0, NULL);",

0 commit comments

Comments
 (0)