From 382c08b0c342235014c05322945221802a3f5320 Mon Sep 17 00:00:00 2001 From: w41ter Date: Thu, 21 Nov 2024 06:38:51 +0000 Subject: [PATCH] [fix](index) Fix create index/index def to sql --- .../org/apache/doris/alter/SchemaChangeHandler.java | 4 ++-- .../apache/doris/analysis/CreateIndexClause.java | 2 +- .../java/org/apache/doris/analysis/IndexDef.java | 2 +- .../main/java/org/apache/doris/catalog/Index.java | 2 +- .../doris/analysis/CreateIndexClauseTest.java | 13 ++++++++++++- .../apache/doris/analysis/DropIndexClauseTest.java | 10 +++++++++- .../org/apache/doris/analysis/IndexDefTest.java | 4 ++-- 7 files changed, 28 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 b7a0fa5cfbc746..bcaeb636009901 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 @@ -1919,7 +1919,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()) { @@ -2749,7 +2749,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/IndexDef.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/IndexDef.java index 39b32662614f15..7f406d2a7e1365 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 @@ -118,7 +118,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/main/java/org/apache/doris/catalog/Index.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Index.java index 40db2f1d5b01d5..8d4cc0ee4aafb2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Index.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Index.java @@ -54,7 +54,7 @@ public class Index implements Writable { public static final int INDEX_ID_INIT_VALUE = -1; @SerializedName(value = "i", alternate = {"indexId"}) - private long indexId = -1; // -1 for compatibale + private long indexId = -1; // -1 for compatiable @SerializedName(value = "in", alternate = {"indexName"}) private String indexName; @SerializedName(value = "c", alternate = {"columns"}) 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..9aa0641b1b94b8 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,22 @@ 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("ALTER TABLE `db`.`table` 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..6a4459e8773db5 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("ALTER TABLE `db`.`table` 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 ccabece832d023..56c78e2430a5ed 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 @@ -77,8 +77,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")); } }