Skip to content

Commit

Permalink
[fix](index) Fix CREATE/DROP INDEX stmt toSql (apache#44494)
Browse files Browse the repository at this point in the history
Parsing `CREATE INDEX` and `DROP INDEX` will generate an
`AlterTableStmt`, and the `AlterTableStmt ::toSql()` result is:

`ALTER TABLE <table> CREATE INDEX <index>(<column>)` and `ALTER TABLE
<table> DROP INDEX <index> ON <table>`

This PR corrects the output to `ALTER TABLE <table> ADD/DROP INDEX ...`
  • Loading branch information
w41ter committed Nov 26, 2024
1 parent a5a2a56 commit 6014796
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ public String toSql() {
sb.append("DROP ROLLUP ");
}
sb.append(((AddRollupClause) op).getRollupName());
} else if (op instanceof CreateIndexClause) {
sb.append(((CreateIndexClause) op).toSql(true));
} else if (op instanceof DropIndexClause) {
sb.append(((DropIndexClause) op).toSql(true));
} else {
sb.append(op.toSql());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public boolean needChangeMTMVState() {

@Override
public String toSql() {
return toSql(alter);
}

public String toSql(boolean alter) {
if (alter) {
return "ADD " + indexDef.toSql();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public boolean needChangeMTMVState() {

@Override
public String toSql() {
return toSql(alter);
}

public String toSql(boolean alter) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("DROP INDEX ").append("`" + indexName + "`");
if (!alter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public void testEnableFeature() throws UserException {

@Test
public void testCreateIndex() throws UserException {
// ALTER TABLE `db`.`table` ADD INDEX `index1` (`col1`) USING INVERTED COMMENT 'balabala'
List<AlterClause> ops = Lists.newArrayList();
ops.add(new CreateIndexClause(
new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"),
Expand All @@ -137,13 +138,44 @@ public void testCreateIndex() throws UserException {
stmt.toSql());
}

@Test
public void testCreateIndexStmt() throws UserException {
// CREATE INDEX `index1` ON `db`.`table` (`col1`) USING INVERTED COMMENT 'balabala'
CreateIndexClause createIndexClause = new CreateIndexClause(
new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"),
new IndexDef("index1", false, Lists.newArrayList("col1"), IndexDef.IndexType.INVERTED, null, "balabala"),
false);
List<AlterClause> ops = Lists.newArrayList();
ops.add(createIndexClause);
AlterTableStmt stmt = new AlterTableStmt(new TableName(internalCtl, "testDb", "testTbl"), ops);
stmt.analyze(analyzer);
Assert.assertEquals("CREATE INDEX `index1` ON `db`.`table` (`col1`) USING INVERTED COMMENT 'balabala'",
createIndexClause.toSql());
Assert.assertEquals("ALTER TABLE `testDb`.`testTbl` ADD INDEX `index1` (`col1`) USING INVERTED COMMENT 'balabala'",
stmt.toSql());
}

@Test
public void testDropIndex() throws UserException {
// ALTER TABLE `db`.`table` DROP INDEX `index1`
List<AlterClause> ops = Lists.newArrayList();
ops.add(new DropIndexClause("index1", false,
new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), true));
AlterTableStmt stmt = new AlterTableStmt(new TableName(internalCtl, "testDb", "testTbl"), ops);
stmt.analyze(analyzer);
Assert.assertEquals("ALTER TABLE `testDb`.`testTbl` DROP INDEX `index1`", stmt.toSql());
}

@Test
public void testDropIndexStmt() throws UserException {
// DROP INDEX `index1` ON `db`.`table`
DropIndexClause dropIndexClause = new DropIndexClause("index1", false,
new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), false);
List<AlterClause> ops = Lists.newArrayList();
ops.add(dropIndexClause);
AlterTableStmt stmt = new AlterTableStmt(new TableName(internalCtl, "testDb", "testTbl"), ops);
stmt.analyze(analyzer);
Assert.assertEquals("DROP INDEX `index1` ON `db`.`table`", dropIndexClause.toSql());
Assert.assertEquals("ALTER TABLE `testDb`.`testTbl` DROP INDEX `index1`", stmt.toSql());
}
}

0 comments on commit 6014796

Please sign in to comment.