Skip to content

Commit

Permalink
Merge pull request #719 from Altinity/686_alter_table_with_algorithm
Browse files Browse the repository at this point in the history
Added logic to support ALTER TABLE with ALGORITHM
  • Loading branch information
subkanthi authored Jul 26, 2024
2 parents 7d1aeaf + a3015cc commit 21203c7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,16 @@ public void enterAlterTable(MySqlParser.AlterTableContext alterTableContext) {
parseAlterTable(tree);
} else if (tree instanceof MySqlParser.AlterByAddIndexContext) {
parseAddIndex(tree);
} else if (tree instanceof TerminalNodeImpl) {
} else if(tree instanceof MySqlParser.AlterBySetAlgorithmContext) {
log.info("INSTANT ALGORITHM not supported in ClickHouse");
// Remove any terminating commas and break out of the parser loop.
// If the last character was comma.
if(this.query.charAt(this.query.length() - 1) == ',')
this.query.deleteCharAt(this.query.length() - 1);

break;
}
else if (tree instanceof TerminalNodeImpl) {
if (((TerminalNodeImpl) tree).symbol.getType() == MySqlParser.COMMA) {
this.query.append(",");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,21 @@ public void testAlterDatabaseAddColumn() {
Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase(clickhouseExpectedQuery));
}

@Test
public void testAlterAddColumnWithColumnKeyword() {

String alterDBAddColumn = "alter table db1.table1 add entity varchar(255) , ALGORITHM=INPLACE, LOCK=NONE";
String clickhouseExpectedQuery = "ALTER TABLE db1.table1 ADD COLUMN entity Nullable(String)";
StringBuffer clickHouseQuery = new StringBuffer();

mySQLDDLParserService.parseSql(alterDBAddColumn, "employees", clickHouseQuery);

log.info("CLICKHOUSE QUERY" + clickHouseQuery);

Assert.assertTrue(clickHouseQuery != null && clickHouseQuery.length() != 0);
Assert.assertTrue(clickHouseQuery.toString().equalsIgnoreCase(clickhouseExpectedQuery));
}

@Test
public void testAlterDatabaseAddColumnNullable() {

Expand All @@ -309,7 +324,7 @@ public void testAlterDatabaseAddColumnNullable() {
// Before, After
@Test
public void testAlterDatabaseAddMultipleColumns1() {
String expectedClickHouseQuery = "ALTER TABLE employees.employees ADD COLUMN ship_spec Nullable(String) first, ADD COLUMN somecol Nullable(Int32) after start_build,";
String expectedClickHouseQuery = "ALTER TABLE employees.employees ADD COLUMN ship_spec Nullable(String) first, ADD COLUMN somecol Nullable(Int32) after start_build";
StringBuffer clickHouseQuery = new StringBuffer();
String query = "alter table employees.employees add column ship_spec varchar(150) first, add somecol int after start_build, algorithm=instant;";
mySQLDDLParserService.parseSql(query, "employees", clickHouseQuery);
Expand Down

0 comments on commit 21203c7

Please sign in to comment.