[fix](index) Fix CREATE/DROP INDEX stmt toSql #44494 (#44661)

cherry pick from #44494
This commit is contained in:
walter
2024-11-28 10:10:12 +08:00
committed by GitHub
parent 05b5a3738c
commit 1a2a91a03a
4 changed files with 44 additions and 0 deletions

View File

@ -198,6 +198,10 @@ public class AlterTableStmt extends DdlStmt {
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());
}

View File

@ -88,6 +88,10 @@ public class CreateIndexClause extends AlterTableClause {
@Override
public String toSql() {
return toSql(alter);
}
public String toSql(boolean alter) {
if (alter) {
return "ADD " + indexDef.toSql();
} else {

View File

@ -80,6 +80,10 @@ public class DropIndexClause extends AlterTableClause {
@Override
public String toSql() {
return toSql(alter);
}
public String toSql(boolean alter) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("DROP INDEX ").append("`" + indexName + "`");
if (!alter) {

View File

@ -126,6 +126,7 @@ public class AlterTableStmtTest {
@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"),
@ -137,8 +138,26 @@ public class AlterTableStmtTest {
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));
@ -146,4 +165,17 @@ public class AlterTableStmtTest {
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());
}
}