Skip to content

Commit

Permalink
Merge pull request #620 from Altinity/619-is_deleted-column-rename-bug
Browse files Browse the repository at this point in the history
Fixed renaming of is_deleted column when the source columns have backticks
  • Loading branch information
subkanthi authored Jun 4, 2024
2 parents ea65b58 + b4f668f commit 9e40e6f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
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

0 comments on commit 9e40e6f

Please sign in to comment.