Skip to content

Commit 0a53255

Browse files
authored
Fix CREATE TABLE parsing with multiple spaces (#163)
Fix parsing bug with multiple spaces in CREATE TABLE - Handle multiple consecutive spaces in field definitions - Add test case to reproduce issue #160 - Fixes 'unknown mysql type' error during realtime replication Fixes #160
1 parent c9efc16 commit 0a53255

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

mysql_ch_replicator/enum/ddl_parser.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ def find_enum_or_set_definition_end(line: str) -> Tuple[int, str, str]:
3636
return end_pos, field_type, field_parameters
3737

3838
# Fallback to splitting by space if we can't find the end
39-
definition = line.split(' ')
40-
field_type = definition[0]
39+
# Use split() instead of split(' ') to handle multiple consecutive spaces
40+
definition = line.split()
41+
field_type = definition[0] if definition else ""
4142
field_parameters = ' '.join(definition[1:]) if len(definition) > 1 else ''
4243

4344
return -1, field_type, field_parameters
@@ -62,12 +63,14 @@ def parse_enum_or_set_field(line: str, field_name: str, is_backtick_quoted: bool
6263
if line.lower().startswith('enum(') or line.lower().startswith('set('):
6364
end_pos, field_type, field_parameters = find_enum_or_set_definition_end(line)
6465
else:
65-
definition = line.split(' ')
66-
field_type = definition[0]
66+
# Use split() instead of split(' ') to handle multiple consecutive spaces
67+
definition = line.split()
68+
field_type = definition[0] if definition else ""
6769
field_parameters = ' '.join(definition[1:]) if len(definition) > 1 else ''
6870
else:
6971
# For non-backtick quoted fields
70-
definition = line.split(' ')
72+
# Use split() instead of split(' ') to handle multiple consecutive spaces
73+
definition = line.split()
7174
definition = definition[1:] # Skip the field name which was already extracted
7275

7376
if definition and (

test_mysql_ch_replicator.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,3 +2607,49 @@ def test_ignore_deletes():
26072607
finally:
26082608
# Clean up the temporary config file
26092609
os.unlink(config_file)
2610+
2611+
def test_issue_160_unknown_mysql_type_bug():
2612+
"""
2613+
Test to reproduce the bug from issue #160.
2614+
2615+
Bug Description: Replication fails when adding a new table during realtime replication
2616+
with Exception: unknown mysql type ""
2617+
2618+
This test should FAIL until the bug is fixed.
2619+
When the bug is present: parsing will fail with unknown mysql type and the test will FAIL
2620+
When the bug is fixed: parsing will succeed and the test will PASS
2621+
"""
2622+
# The exact CREATE TABLE statement from the bug report
2623+
create_table_query = """create table test_table
2624+
(
2625+
id bigint not null,
2626+
col_a datetime(6) not null,
2627+
col_b datetime(6) null,
2628+
col_c varchar(255) not null,
2629+
col_d varchar(255) not null,
2630+
col_e int not null,
2631+
col_f decimal(20, 10) not null,
2632+
col_g decimal(20, 10) not null,
2633+
col_h datetime(6) not null,
2634+
col_i date not null,
2635+
col_j varchar(255) not null,
2636+
col_k varchar(255) not null,
2637+
col_l bigint not null,
2638+
col_m varchar(50) not null,
2639+
col_n bigint null,
2640+
col_o decimal(20, 1) null,
2641+
col_p date null,
2642+
primary key (id, col_e)
2643+
);"""
2644+
2645+
# Create a converter instance
2646+
converter = MysqlToClickhouseConverter()
2647+
2648+
# This should succeed when the bug is fixed
2649+
# When the bug is present, this will raise "unknown mysql type """ and the test will FAIL
2650+
mysql_structure, ch_structure = converter.parse_create_table_query(create_table_query)
2651+
2652+
# Verify the parsing worked correctly
2653+
assert mysql_structure.table_name == 'test_table'
2654+
assert len(mysql_structure.fields) == 17 # All columns should be parsed
2655+
assert mysql_structure.primary_keys == ['id', 'col_e']

0 commit comments

Comments
 (0)