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

Fixed renaming of is_deleted column when the source columns have backticks #620

Merged
merged 2 commits into from
Jun 4, 2024
Merged
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 @@ -109,8 +109,17 @@ public void enterColumnCreateTable(MySqlParser.ColumnCreateTableContext columnCr
Set<String> columnNames = parseCreateTable(columnCreateTableContext, orderByColumns, partitionByColumn);
//this.query.append(" Engine=")
String isDeletedColumn = IS_DELETED_COLUMN;
if(columnNames.contains(isDeletedColumn)) {
isDeletedColumn = "__" + IS_DELETED_COLUMN;
// Iterate through columnNames and match isDeletedColumn with elements in columnNames.
// remove the backticks from elements in columnNames.
for(String columnName: columnNames) {
if(columnName.contains("`")) {
// replace backticks with empty string.
columnName = columnName.replace("`", "");
}
if(columnName.equalsIgnoreCase(isDeletedColumn)) {
isDeletedColumn = "__" + IS_DELETED_COLUMN;
break;
}
}

// Check if the destination is ReplicatedReplacingMergeTree.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,33 @@ public void testAlterDatabaseAddColumnJson() {
Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase(clickhouseExpectedQuery));
}

@Test
public void testRenameIsDeletedColumn() {
String sql = "CREATE TABLE `city` (\n" +
" `ID` int NOT NULL AUTO_INCREMENT,\n" +
" `Name` char(35) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',\n" +
" `CountryCode` char(3) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',\n" +
" `District` char(20) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '',\n" +
" `Population` int NOT NULL DEFAULT '0',\n" +
" `is_deleted` tinyint(1) DEFAULT '0',\n" +
" PRIMARY KEY (`ID`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;";

StringBuffer clickHouseQuery = new StringBuffer();
mySQLDDLParserService.parseSql(sql, "employees", clickHouseQuery);

Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase(
"CREATE TABLE employees.`city`(`ID` Int32 NOT NULL ,`Name` String NOT NULL ,`CountryCode` String NOT NULL ,`District` String NOT NULL ,`Population` Int32 NOT NULL ,`is_deleted` Nullable(Int16),`_version` UInt64,`__is_deleted` UInt8) Engine=ReplacingMergeTree(_version,__is_deleted) ORDER BY (`ID`)"));


String sqlWithoutBackticks = "create table city(id int not null auto_increment, Name char(35) , is_deleted tinyint(1) DEFAULT 0, primary key(id))";

StringBuffer clickHouseQuery2 = new StringBuffer();
mySQLDDLParserService.parseSql(sqlWithoutBackticks, "employees", clickHouseQuery2);

Assert.assertTrue(clickHouseQuery2.toString().equalsIgnoreCase(
"CREATE TABLE employees.city(id Int32 NOT NULL ,Name Nullable(String),is_deleted Nullable(Int16),`_version` UInt64,`__is_deleted` UInt8) Engine=ReplacingMergeTree(_version,__is_deleted) ORDER BY (id)"));
}
// @Test
// public void deleteData() {
// String sql = "DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste'";
Expand Down
Loading