From 60147969558ed77b52180123c3009d2f334f38d3 Mon Sep 17 00:00:00 2001 From: walter Date: Sun, 24 Nov 2024 11:34:10 +0800 Subject: [PATCH] [fix](index) Fix CREATE/DROP INDEX stmt toSql (#44494) Parsing `CREATE INDEX` and `DROP INDEX` will generate an `AlterTableStmt`, and the `AlterTableStmt ::toSql()` result is: `ALTER TABLE CREATE INDEX ()` and `ALTER TABLE
DROP INDEX ON
` This PR corrects the output to `ALTER TABLE
ADD/DROP INDEX ...` --- .../apache/doris/analysis/AlterTableStmt.java | 4 +++ .../doris/analysis/CreateIndexClause.java | 4 +++ .../doris/analysis/DropIndexClause.java | 4 +++ .../doris/analysis/AlterTableStmtTest.java | 32 +++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/AlterTableStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/AlterTableStmt.java index db51dfffd9fcb4..cb3925cc444391 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/AlterTableStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/AlterTableStmt.java @@ -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()); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateIndexClause.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateIndexClause.java index 16f90432e7b7ab..62d051ca318d31 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateIndexClause.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateIndexClause.java @@ -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 { diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/DropIndexClause.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/DropIndexClause.java index 783bed775be0f6..25b4db9d16f00d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/DropIndexClause.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/DropIndexClause.java @@ -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) { diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/AlterTableStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/AlterTableStmtTest.java index 9476df59cd1419..c968f21d670200 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/AlterTableStmtTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/AlterTableStmtTest.java @@ -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 ops = Lists.newArrayList(); ops.add(new CreateIndexClause( new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), @@ -137,8 +138,26 @@ 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 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 ops = Lists.newArrayList(); ops.add(new DropIndexClause("index1", false, new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), true)); @@ -146,4 +165,17 @@ public void testDropIndex() throws UserException { 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 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()); + } }