From b0e80c03f6ce65ffac6d250249fbcb930df5bac2 Mon Sep 17 00:00:00 2001 From: Kanthi Subramanian Date: Fri, 11 Oct 2024 17:25:44 -0400 Subject: [PATCH 1/2] Added fix to retrieve data type for columns with comments --- .../embedded/ddl/parser/MySqlDDLParserListenerImpl.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sink-connector-lightweight/src/main/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImpl.java b/sink-connector-lightweight/src/main/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImpl.java index 52ed4b08f..888ca7515 100644 --- a/sink-connector-lightweight/src/main/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImpl.java +++ b/sink-connector-lightweight/src/main/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImpl.java @@ -362,9 +362,10 @@ private String getClickHouseDataType(String parsedDataType, ParseTree colDefTree // Dont try to get precision/scale for enums } else if(parsedDataType.contains("(") && parsedDataType.contains(")") && parsedDataType.contains(",") ) { + String sanitizedDataType = parsedDataType.split("COMMENT")[0].trim(); try { - precision = Integer.parseInt(parsedDataType.substring(parsedDataType.indexOf("(") + 1, parsedDataType.indexOf(","))); - scale = Integer.parseInt(parsedDataType.substring(parsedDataType.indexOf(",") + 1, parsedDataType.indexOf(")"))); + precision = Integer.parseInt(sanitizedDataType.substring(sanitizedDataType.indexOf("(") + 1, sanitizedDataType.indexOf(","))); + scale = Integer.parseInt(sanitizedDataType.substring(sanitizedDataType.indexOf(",") + 1, sanitizedDataType.indexOf(")"))); } catch(Exception e) { log.error("Error parsing precision, scale : columnName" + columnName); } From 22b35c54c68b6901c7425cd1f18b9c5b06b40adf Mon Sep 17 00:00:00 2001 From: Kanthi Subramanian Date: Fri, 11 Oct 2024 18:15:42 -0400 Subject: [PATCH 2/2] Fix for DDL translation for columns with comments --- .../parser/MySqlDDLParserListenerImpl.java | 6 +++++- .../MySqlDDLParserListenerImplTest.java | 21 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/sink-connector-lightweight/src/main/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImpl.java b/sink-connector-lightweight/src/main/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImpl.java index 888ca7515..a196b5aa0 100644 --- a/sink-connector-lightweight/src/main/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImpl.java +++ b/sink-connector-lightweight/src/main/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImpl.java @@ -488,7 +488,11 @@ else if(columnDefChild.getText().equalsIgnoreCase(Constants.NOT_NULL)) { if (columnDefChild.getChildCount() >= 2) { defaultModifier = "DEFAULT " + columnDefChild.getChild(1).getText(); } - } else { + } else if(columnDefChild instanceof MySqlParser.CommentColumnConstraintContext) { + // Ignore comment for now. + //commentModifier = columnDefChild.getChild(1).getText(); + } + else { columnType = (columnDefChild.getText()); String chDataType = getClickHouseDataType(columnType, columnChild, columnName); if (chDataType != null) { diff --git a/sink-connector-lightweight/src/test/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImplTest.java b/sink-connector-lightweight/src/test/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImplTest.java index c6df9bfb8..372368a78 100644 --- a/sink-connector-lightweight/src/test/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImplTest.java +++ b/sink-connector-lightweight/src/test/java/com/altinity/clickhouse/debezium/embedded/ddl/parser/MySqlDDLParserListenerImplTest.java @@ -599,7 +599,6 @@ public void testAddIndex() { String sql = "alter table add_test add index if not exists ix_add_test_col1 using btree (col1) comment 'test index';\n"; mySQLDDLParserService.parseSql(sql, "table1", clickHouseQuery); - } @@ -680,6 +679,26 @@ public void alterTableRenameTable() { Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase("RENAME TABLE employees.test_table to employees.test_table_new")); } + @Test + public void testAlterTableColumnWithComment() { + StringBuffer clickHouseQuery = new StringBuffer(); + + String sql = "ALTER TABLE test_table ADD COLUMN col1 varchar(255) COMMENT 'test column';"; + mySQLDDLParserService.parseSql(sql, "", clickHouseQuery); + + Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase("ALTER TABLE employees.test_table ADD COLUMN col1 Nullable(String)")); + } + + @Test + public void testAlterTableColumnWithCommentAndDecimalScale() { + StringBuffer clickHouseQuery = new StringBuffer(); + + String sql = "ALTER TABLE test_table ADD COLUMN col1 decimal(10,2) COMMENT 'test column';"; + mySQLDDLParserService.parseSql(sql, "", clickHouseQuery); + + Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase("ALTER TABLE employees.test_table ADD COLUMN col1 Nullable(Decimal(10,2))")); + } + @Test public void testGeneratedColumn() { StringBuffer clickHouseQuery = new StringBuffer();