From 688ef8fe230409bdc5a56ae0059b5e5e9f9e2ac5 Mon Sep 17 00:00:00 2001 From: walter Date: Tue, 26 Nov 2024 17:44:53 +0800 Subject: [PATCH] [fix](index) Fix create index/index def to sql #44392 (#44581) cherry pick from #44392 --- .../doris/alter/SchemaChangeHandler.java | 4 ++-- .../doris/analysis/CreateIndexClause.java | 2 +- .../doris/analysis/DropIndexClause.java | 2 +- .../org/apache/doris/analysis/IndexDef.java | 2 +- .../doris/analysis/AlterTableStmtTest.java | 23 +++++++++++++++++++ .../doris/analysis/CreateIndexClauseTest.java | 12 +++++++++- .../doris/analysis/DropIndexClauseTest.java | 10 +++++++- .../apache/doris/analysis/IndexDefTest.java | 4 ++-- 8 files changed, 50 insertions(+), 9 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java b/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java index 879034d06dc119..72148a5c2824b7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java +++ b/fe/fe-core/src/main/java/org/apache/doris/alter/SchemaChangeHandler.java @@ -1833,7 +1833,7 @@ public void process(String rawSql, List alterClauses, Database db, // index id -> index schema Map> indexSchemaMap = new HashMap<>(); - //for multi add colmuns clauses + //for multi add columns clauses //index id -> index col_unique_id supplier Map colUniqueIdSupplierMap = new HashMap<>(); for (Map.Entry> entry : olapTable.getIndexIdToSchema(true).entrySet()) { @@ -2601,7 +2601,7 @@ private boolean processAddIndex(CreateIndexClause alterClause, OlapTable olapTab // the column name in CreateIndexClause is not check case sensitivity, // when send index description to BE, there maybe cannot find column by name, // so here update column name in CreateIndexClause after checkColumn for indexDef, - // there will use the column name in olapTable insead of the column name in CreateIndexClause. + // there will use the column name in olapTable instead of the column name in CreateIndexClause. alterIndex.setColumns(indexDef.getColumns()); alterIndex.setColumnUniqueIds(indexDef.getColumnUniqueIds()); newIndexes.add(alterIndex); 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 86df87453ad575..16f90432e7b7ab 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 @@ -89,7 +89,7 @@ public boolean needChangeMTMVState() { @Override public String toSql() { if (alter) { - return indexDef.toSql(); + return "ADD " + indexDef.toSql(); } else { return "CREATE " + indexDef.toSql(tableName.toSql()); } 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 f72136ee2b3f41..783bed775be0f6 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 @@ -81,7 +81,7 @@ public boolean needChangeMTMVState() { @Override public String toSql() { StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.append("DROP INDEX ").append(indexName); + stringBuilder.append("DROP INDEX ").append("`" + indexName + "`"); if (!alter) { stringBuilder.append(" ON ").append(tableName.toSql()); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/IndexDef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/IndexDef.java index d57ca506786163..d19fdb379197e8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/IndexDef.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/IndexDef.java @@ -119,7 +119,7 @@ public String toSql() { public String toSql(String tableName) { StringBuilder sb = new StringBuilder("INDEX "); - sb.append(indexName); + sb.append("`" + indexName + "`"); if (tableName != null && !tableName.isEmpty()) { sb.append(" ON ").append(tableName); } 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 9f32556ce9c201..9476df59cd1419 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 @@ -123,4 +123,27 @@ public void testEnableFeature() throws UserException { stmt.toSql()); Assert.assertEquals("testDb", stmt.getTbl().getDb()); } + + @Test + public void testCreateIndex() throws UserException { + List ops = Lists.newArrayList(); + ops.add(new CreateIndexClause( + new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), + new IndexDef("index1", false, Lists.newArrayList("col1"), IndexDef.IndexType.INVERTED, null, "balabala"), + true)); + AlterTableStmt stmt = new AlterTableStmt(new TableName(internalCtl, "testDb", "testTbl"), ops); + stmt.analyze(analyzer); + Assert.assertEquals("ALTER TABLE `testDb`.`testTbl` ADD INDEX `index1` (`col1`) USING INVERTED COMMENT 'balabala'", + stmt.toSql()); + } + + @Test + public void testDropIndex() throws UserException { + List 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()); + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateIndexClauseTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateIndexClauseTest.java index 7341b8e7bcfd1e..86e633d2dfebe6 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateIndexClauseTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/CreateIndexClauseTest.java @@ -40,11 +40,21 @@ public void testNormal() throws AnalysisException { new IndexDef("index1", false, Lists.newArrayList("col1"), IndexDef.IndexType.INVERTED, null, "balabala"), false); clause.analyze(analyzer); - Assert.assertEquals("CREATE INDEX index1 ON `db`.`table` (`col1`) USING INVERTED COMMENT 'balabala'", + Assert.assertEquals("CREATE INDEX `index1` ON `db`.`table` (`col1`) USING INVERTED COMMENT 'balabala'", clause.toSql()); } + @Test + public void testAlter() throws AnalysisException { + CreateIndexClause clause = new CreateIndexClause( + new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), + new IndexDef("index1", false, Lists.newArrayList("col1"), IndexDef.IndexType.INVERTED, null, "balabala"), + true); + clause.analyze(analyzer); + Assert.assertEquals("ADD INDEX `index1` (`col1`) USING INVERTED COMMENT 'balabala'", clause.toSql()); + } + @Test(expected = AnalysisException.class) public void testDuplIndex() throws AnalysisException { CreateIndexClause clause = new CreateIndexClause( diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/DropIndexClauseTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/DropIndexClauseTest.java index da43a1fe563ea0..e1659cd03cfcfa 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/DropIndexClauseTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/DropIndexClauseTest.java @@ -39,7 +39,15 @@ public void testNormal() throws UserException { DropIndexClause clause = new DropIndexClause("index1", false, new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), false); clause.analyze(analyzer); - Assert.assertEquals("DROP INDEX index1 ON `db`.`table`", clause.toSql()); + Assert.assertEquals("DROP INDEX `index1` ON `db`.`table`", clause.toSql()); + } + + @Test + public void testAlter() throws UserException { + DropIndexClause clause = new DropIndexClause("index1", false, + new TableName(InternalCatalog.INTERNAL_CATALOG_NAME, "db", "table"), true); + clause.analyze(analyzer); + Assert.assertEquals("DROP INDEX `index1`", clause.toSql()); } @Test(expected = AnalysisException.class) diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/IndexDefTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/IndexDefTest.java index a4eeec679b24d1..deccb435b71de5 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/IndexDefTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/IndexDefTest.java @@ -64,8 +64,8 @@ public void testAnalyzeExpection() throws AnalysisException { @Test public void toSql() { - Assert.assertEquals("INDEX index1 (`col1`) USING INVERTED COMMENT 'balabala'", def.toSql()); - Assert.assertEquals("INDEX index1 ON table1 (`col1`) USING INVERTED COMMENT 'balabala'", + Assert.assertEquals("INDEX `index1` (`col1`) USING INVERTED COMMENT 'balabala'", def.toSql()); + Assert.assertEquals("INDEX `index1` ON table1 (`col1`) USING INVERTED COMMENT 'balabala'", def.toSql("table1")); } }