Skip to content

Commit 82b179e

Browse files
authored
Merge pull request #868 from Altinity/866-incorrect-precision-and-scale-parsing-due-to-column-comment-interference
866 incorrect precision and scale parsing due to column comment interference
2 parents da57c3a + 22e4d84 commit 82b179e

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

sink-connector-lightweight/src/main/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImpl.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,10 @@ private String getClickHouseDataType(String parsedDataType, ParseTree colDefTree
378378
// Dont try to get precision/scale for enums
379379
}
380380
else if(parsedDataType.contains("(") && parsedDataType.contains(")") && parsedDataType.contains(",") ) {
381+
String sanitizedDataType = parsedDataType.split("COMMENT")[0].trim();
381382
try {
382-
precision = Integer.parseInt(parsedDataType.substring(parsedDataType.indexOf("(") + 1, parsedDataType.indexOf(",")));
383-
scale = Integer.parseInt(parsedDataType.substring(parsedDataType.indexOf(",") + 1, parsedDataType.indexOf(")")));
383+
precision = Integer.parseInt(sanitizedDataType.substring(sanitizedDataType.indexOf("(") + 1, sanitizedDataType.indexOf(",")));
384+
scale = Integer.parseInt(sanitizedDataType.substring(sanitizedDataType.indexOf(",") + 1, sanitizedDataType.indexOf(")")));
384385
} catch(Exception e) {
385386
log.error("Error parsing precision, scale : columnName" + columnName);
386387
}
@@ -508,7 +509,11 @@ else if(columnDefChild.getText().equalsIgnoreCase(Constants.NOT_NULL)) {
508509
if (columnDefChild.getChildCount() >= 2) {
509510
defaultModifier = "DEFAULT " + columnDefChild.getChild(1).getText();
510511
}
511-
} else {
512+
} else if(columnDefChild instanceof MySqlParser.CommentColumnConstraintContext) {
513+
// Ignore comment for now.
514+
//commentModifier = columnDefChild.getChild(1).getText();
515+
}
516+
else {
512517
columnType = (columnDefChild.getText());
513518
String chDataType = getClickHouseDataType(columnType, columnChild, columnName);
514519
if (chDataType != null) {

sink-connector-lightweight/src/test/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImplTest.java

+20
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,26 @@ public void alterTableRenameTable() {
790790
Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase("RENAME TABLE employees.test_table to employees.test_table_new"));
791791
}
792792

793+
@Test
794+
public void testAlterTableColumnWithComment() {
795+
StringBuffer clickHouseQuery = new StringBuffer();
796+
797+
String sql = "ALTER TABLE test_table ADD COLUMN col1 varchar(255) COMMENT 'test column';";
798+
mySQLDDLParserService.parseSql(sql, "", clickHouseQuery);
799+
800+
Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase("ALTER TABLE employees.test_table ADD COLUMN col1 Nullable(String)"));
801+
}
802+
803+
@Test
804+
public void testAlterTableColumnWithCommentAndDecimalScale() {
805+
StringBuffer clickHouseQuery = new StringBuffer();
806+
807+
String sql = "ALTER TABLE test_table ADD COLUMN col1 decimal(10,2) COMMENT 'test column';";
808+
mySQLDDLParserService.parseSql(sql, "", clickHouseQuery);
809+
810+
Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase("ALTER TABLE employees.test_table ADD COLUMN col1 Nullable(Decimal(10,2))"));
811+
}
812+
793813
@Test
794814
public void testGeneratedColumn() {
795815
StringBuffer clickHouseQuery = new StringBuffer();

0 commit comments

Comments
 (0)