This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch branch-2.1
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/branch-2.1 by this push:
     new f59c2855386 branch-2.1: [fix](index)Add duplicated indexes check in 
add index #46155 (#46210)
f59c2855386 is described below

commit f59c2855386d202bb031dc0527b84a41bb8617c1
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Tue Dec 31 22:46:29 2024 +0800

    branch-2.1: [fix](index)Add duplicated indexes check in add index #46155 
(#46210)
    
    Cherry-picked from #46155
    
    Co-authored-by: qiye <l...@selectdb.com>
---
 .../apache/doris/alter/SchemaChangeHandler.java    | 47 +++++++++++++---------
 .../doris/alter/SchemaChangeHandlerTest.java       | 36 +++++++++++++++++
 2 files changed, 65 insertions(+), 18 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 117297b0a79..15caeb55ad7 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
@@ -2556,7 +2556,7 @@ public class SchemaChangeHandler extends AlterHandler {
 
     /**
      * Returns true if the index already exists, there is no need to create 
the job to add the index.
-     * Otherwise return false, there is need to create a job to add the index.
+     * Otherwise, return false, there is need to create a job to add the index.
      */
     private boolean processAddIndex(CreateIndexClause alterClause, OlapTable 
olapTable, List<Index> newIndexes)
             throws UserException {
@@ -2570,23 +2570,11 @@ public class SchemaChangeHandler extends AlterHandler {
         Set<String> newColset = Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER);
         newColset.addAll(indexDef.getColumns());
         Set<Long> existedIndexIdSet = Sets.newHashSet();
-        for (Index existedIdx : existedIndexes) {
-            if 
(existedIdx.getIndexName().equalsIgnoreCase(indexDef.getIndexName())) {
-                if (indexDef.isSetIfNotExists()) {
-                    LOG.info("create index[{}] which already exists on 
table[{}]", indexDef.getIndexName(),
-                            olapTable.getName());
-                    return true;
-                }
-                throw new DdlException("index `" + indexDef.getIndexName() + 
"` already exist.");
-            }
-            Set<String> existedIdxColSet = 
Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER);
-            existedIdxColSet.addAll(existedIdx.getColumns());
-            if (existedIdx.getIndexType() == indexDef.getIndexType() && 
newColset.equals(existedIdxColSet)) {
-                throw new DdlException(
-                        indexDef.getIndexType() + " index for columns (" + 
String.join(",", indexDef.getColumns())
-                                + " ) already exist.");
-            }
-            existedIndexIdSet.add(existedIdx.getIndexId());
+        if (checkDuplicateIndexes(existedIndexes, indexDef, newColset, 
existedIndexIdSet, olapTable)) {
+            return true;
+        }
+        if (checkDuplicateIndexes(newIndexes, indexDef, newColset, 
existedIndexIdSet, olapTable)) {
+            return true;
         }
 
         // The restored olap table may not reset the index id, which comes 
from the upstream,
@@ -2621,6 +2609,29 @@ public class SchemaChangeHandler extends AlterHandler {
         return false;
     }
 
+    private boolean checkDuplicateIndexes(List<Index> indexes, IndexDef 
indexDef, Set<String> newColset,
+                                          Set<Long> existedIndexIdSet, 
OlapTable olapTable) throws DdlException {
+        for (Index index : indexes) {
+            if 
(index.getIndexName().equalsIgnoreCase(indexDef.getIndexName())) {
+                if (indexDef.isSetIfNotExists()) {
+                    LOG.info("create index[{}] which already exists on 
table[{}]", indexDef.getIndexName(),
+                            olapTable.getName());
+                    return true;
+                }
+                throw new DdlException("index `" + indexDef.getIndexName() + 
"` already exist.");
+            }
+            Set<String> existedIdxColSet = 
Sets.newTreeSet(String.CASE_INSENSITIVE_ORDER);
+            existedIdxColSet.addAll(index.getColumns());
+            if (index.getIndexType() == indexDef.getIndexType() && 
newColset.equals(existedIdxColSet)) {
+                throw new DdlException(
+                        indexDef.getIndexType() + " index for columns (" + 
String.join(",", indexDef.getColumns())
+                                + ") already exist.");
+            }
+            existedIndexIdSet.add(index.getIndexId());
+        }
+        return false;
+    }
+
     /**
      * Returns true if the index does not exist, there is no need to create 
the job to drop the index.
      * Otherwise return false, there is need to create a job to drop the index.
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/alter/SchemaChangeHandlerTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/alter/SchemaChangeHandlerTest.java
index 649d7e4facb..8f7e2d6b2f6 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/alter/SchemaChangeHandlerTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/alter/SchemaChangeHandlerTest.java
@@ -556,4 +556,40 @@ public class SchemaChangeHandlerTest extends 
TestWithFeService {
             tbl.readUnlock();
         }
     }
+
+    @Test
+    public void testAddDuplicateInvertedIndexException() throws Exception {
+
+        LOG.info("dbName: {}", Env.getCurrentInternalCatalog().getDbNames());
+
+        Database db = 
Env.getCurrentInternalCatalog().getDbOrMetaException("test");
+        OlapTable tbl = (OlapTable) db.getTableOrMetaException("sc_dup", 
Table.TableType.OLAP);
+        tbl.readLock();
+        try {
+            Assertions.assertNotNull(tbl);
+            Assertions.assertEquals("Doris", tbl.getEngine());
+            Assertions.assertEquals(0, tbl.getIndexes().size());
+        } finally {
+            tbl.readUnlock();
+        }
+
+        String addInvertedIndexStmtStr = "alter table test.sc_dup add index 
idx_error_msg(error_msg), "
+                + "add index idx_error_msg1(error_msg)";
+        AlterTableStmt addInvertedIndexStmt = (AlterTableStmt) 
parseAndAnalyzeStmt(addInvertedIndexStmtStr);
+        try {
+            
Env.getCurrentEnv().getAlterInstance().processAlterTable(addInvertedIndexStmt);
+        } catch (Exception e) {
+            // Verify the error message contains relevant info
+            Assertions.assertTrue(e.getMessage().contains("INVERTED index for 
columns (error_msg) already exist"));
+        }
+        addInvertedIndexStmtStr = "alter table test.sc_dup add index 
idx_error_msg(error_msg), "
+                + "add index idx_error_msg(error_msg)";
+        addInvertedIndexStmt = (AlterTableStmt) 
parseAndAnalyzeStmt(addInvertedIndexStmtStr);
+        try {
+            
Env.getCurrentEnv().getAlterInstance().processAlterTable(addInvertedIndexStmt);
+        } catch (Exception e) {
+            // Verify the error message contains relevant info
+            Assertions.assertTrue(e.getMessage().contains("index 
`idx_error_msg` already exist."));
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to