Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

866 incorrect precision and scale parsing due to column comment interference #868

Open
wants to merge 2 commits into
base: 2.5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -487,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);


}


Expand Down Expand Up @@ -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();
Expand Down
Loading