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

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 8b7d7e4  [improvement] create/drop index support if [not] exist (#7748)
8b7d7e4 is described below

commit 8b7d7e4dac7ae3af5d27166cd68ca88fa73ee816
Author: shee <[email protected]>
AuthorDate: Sun Jan 16 10:40:44 2022 +0800

    [improvement] create/drop index support if [not] exist (#7748)
    
    create or drop index clause support if [not] exist
---
 .../sql-statements/Data Definition/ALTER TABLE.md  |  8 +++---
 .../sql-statements/Data Definition/CREATE INDEX.md |  4 +--
 .../sql-statements/Data Definition/DROP INDEX.md   |  2 +-
 .../sql-statements/Data Definition/ALTER TABLE.md  |  8 +++---
 .../sql-statements/Data Definition/CREATE INDEX.md |  4 +--
 .../sql-statements/Data Definition/DROP INDEX.md   |  2 +-
 fe/fe-core/src/main/cup/sql_parser.cup             | 16 +++++------
 .../apache/doris/alter/SchemaChangeHandler.java    | 32 ++++++++++++++++++----
 .../org/apache/doris/analysis/DropIndexClause.java |  9 +++++-
 .../java/org/apache/doris/analysis/IndexDef.java   |  8 +++++-
 .../doris/analysis/CreateIndexClauseTest.java      |  2 +-
 .../apache/doris/analysis/DropIndexClauseTest.java |  5 ++--
 .../org/apache/doris/analysis/IndexDefTest.java    |  6 ++--
 13 files changed, 71 insertions(+), 35 deletions(-)

diff --git a/docs/en/sql-reference/sql-statements/Data Definition/ALTER 
TABLE.md b/docs/en/sql-reference/sql-statements/Data Definition/ALTER TABLE.md
index 5bad553..6e7c901 100644
--- a/docs/en/sql-reference/sql-statements/Data Definition/ALTER TABLE.md       
+++ b/docs/en/sql-reference/sql-statements/Data Definition/ALTER TABLE.md       
@@ -231,13 +231,13 @@ under the License.
     Bitmap index supports the following modifications:
     1. create bitmap index
         grammar:
-            ADD INDEX index_name (column [, ...],) [USING BITMAP] [COMMENT 
'balabala'];
+            ADD INDEX [IF NOT EXISTS] index_name (column [, ...],) [USING 
BITMAP] [COMMENT 'balabala'];
         note:
             1. only supports bitmap index for current version
             2. BITMAP index only supports apply on single column
     2. drop index
         grammar:
-            DROP INDEX index_name;
+            DROP INDEX [IF EXISTS] index_name;
 
 ## example
 
@@ -418,9 +418,9 @@ under the License.
     
     [index]
     1. create index on table1 column siteid using bitmap 
-        ALTER TABLE table1 ADD INDEX index_name  [USING BITMAP] (siteid) 
COMMENT 'balabala';
+        ALTER TABLE table1 ADD INDEX [IF NOT EXISTS] index_name  [USING 
BITMAP] (siteid) COMMENT 'balabala';
     2. drop bitmap index of table1
-        ALTER TABLE table1 DROP INDEX index_name;
+        ALTER TABLE table1 DROP INDEX [IF EXISTS] index_name;
 
 ## keyword
 
diff --git a/docs/en/sql-reference/sql-statements/Data Definition/CREATE 
INDEX.md b/docs/en/sql-reference/sql-statements/Data Definition/CREATE INDEX.md
index 2f13d24..df128fd 100644
--- a/docs/en/sql-reference/sql-statements/Data Definition/CREATE INDEX.md      
+++ b/docs/en/sql-reference/sql-statements/Data Definition/CREATE INDEX.md      
@@ -30,7 +30,7 @@ under the License.
 
     This statement is used to create index
     grammer:
-        CREATE INDEX index_name ON table_name (column [, ...],) [USING BITMAP] 
[COMMENT'balabala'];
+        CREATE INDEX [IF NOT EXISTS] index_name ON table_name (column [, 
...],) [USING BITMAP] [COMMENT'balabala'];
     note:
         1. only support bitmap index in current version
         2. BITMAP index only supports apply to single column
@@ -38,7 +38,7 @@ under the License.
 ## example
 
     1. create index on table1 column siteid using bitmap 
-        CREATE INDEX index_name ON table1 (siteid) USING BITMAP COMMENT 
'balabala';
+        CREATE INDEX [IF NOT EXISTS] index_name ON table1 (siteid) USING 
BITMAP COMMENT 'balabala';
 
 ## keyword
 
diff --git a/docs/en/sql-reference/sql-statements/Data Definition/DROP INDEX.md 
b/docs/en/sql-reference/sql-statements/Data Definition/DROP INDEX.md
index 732d665..ece2902 100644
--- a/docs/en/sql-reference/sql-statements/Data Definition/DROP INDEX.md        
+++ b/docs/en/sql-reference/sql-statements/Data Definition/DROP INDEX.md        
@@ -30,7 +30,7 @@ under the License.
 
     This statement is used to delete index from table
     grammer:
-        DROP INDEX index_name ON [db_name.]table_name;
+        DROP INDEX [IF EXISTS] index_name ON [db_name.]table_name;
 
 ## keyword
 
diff --git a/docs/zh-CN/sql-reference/sql-statements/Data Definition/ALTER 
TABLE.md b/docs/zh-CN/sql-reference/sql-statements/Data Definition/ALTER 
TABLE.md
index 039c0f8..1746d2c 100644
--- a/docs/zh-CN/sql-reference/sql-statements/Data Definition/ALTER TABLE.md    
+++ b/docs/zh-CN/sql-reference/sql-statements/Data Definition/ALTER TABLE.md    
@@ -229,14 +229,14 @@ under the License.
     bitmap index 支持如下几种修改方式
     1. 创建bitmap 索引
         语法:
-            ADD INDEX index_name (column [, ...],) [USING BITMAP] [COMMENT 
'balabala'];
+            ADD INDEX [IF NOT EXISTS] index_name (column [, ...],) [USING 
BITMAP] [COMMENT 'balabala'];
         注意:
             1. 目前仅支持bitmap 索引
             1. BITMAP 索引仅在单列上创建
 
     2. 删除索引
         语法:
-            DROP INDEX index_name;
+            DROP INDEX [IF EXISTS] index_name;
 
 ## example
 
@@ -413,9 +413,9 @@ under the License.
         ALTER TABLE example_table RENAME PARTITION p1 p2;
     [index]
     1. 在table1 上为siteid 创建bitmap 索引
-        ALTER TABLE table1 ADD INDEX index_name (siteid) [USING BITMAP] 
COMMENT 'balabala';
+        ALTER TABLE table1 ADD INDEX [IF NOT EXISTS] index_name (siteid) 
[USING BITMAP] COMMENT 'balabala';
     2. 删除table1 上的siteid列的bitmap 索引
-        ALTER TABLE table1 DROP INDEX index_name;
+        ALTER TABLE table1 DROP INDEX [IF EXISTS] index_name;
 
 ## keyword
 
diff --git a/docs/zh-CN/sql-reference/sql-statements/Data Definition/CREATE 
INDEX.md b/docs/zh-CN/sql-reference/sql-statements/Data Definition/CREATE 
INDEX.md
index b65fc58..9a8f051 100644
--- a/docs/zh-CN/sql-reference/sql-statements/Data Definition/CREATE INDEX.md   
+++ b/docs/zh-CN/sql-reference/sql-statements/Data Definition/CREATE INDEX.md   
@@ -30,7 +30,7 @@ under the License.
 
     该语句用于创建索引
     语法:
-        CREATE INDEX index_name ON table_name (column [, ...],) [USING BITMAP] 
[COMMENT'balabala'];
+        CREATE INDEX [IF NOT EXISTS] index_name ON table_name (column [, 
...],) [USING BITMAP] [COMMENT'balabala'];
     注意:
         1. 目前只支持bitmap 索引
         2. BITMAP 索引仅在单列上创建
@@ -38,7 +38,7 @@ under the License.
 ## example
 
     1. 在table1 上为siteid 创建bitmap 索引
-        CREATE INDEX index_name ON table1 (siteid) USING BITMAP COMMENT 
'balabala';
+        CREATE INDEX [IF NOT EXISTS] index_name ON table1 (siteid) USING 
BITMAP COMMENT 'balabala';
 
 ## keyword
 
diff --git a/docs/zh-CN/sql-reference/sql-statements/Data Definition/DROP 
INDEX.md b/docs/zh-CN/sql-reference/sql-statements/Data Definition/DROP INDEX.md
index 45c1d12..67ded37 100644
--- a/docs/zh-CN/sql-reference/sql-statements/Data Definition/DROP INDEX.md     
+++ b/docs/zh-CN/sql-reference/sql-statements/Data Definition/DROP INDEX.md     
@@ -30,7 +30,7 @@ under the License.
 
     该语句用于从一个表中删除指定名称的索引,目前仅支持bitmap 索引
     语法:
-        DROP INDEX index_name ON [db_name.]table_name;
+        DROP INDEX [IF EXISTS] index_name ON [db_name.]table_name;
 
 ## keyword
 
diff --git a/fe/fe-core/src/main/cup/sql_parser.cup 
b/fe/fe-core/src/main/cup/sql_parser.cup
index 4589c2c..0751e61 100644
--- a/fe/fe-core/src/main/cup/sql_parser.cup
+++ b/fe/fe-core/src/main/cup/sql_parser.cup
@@ -1071,9 +1071,9 @@ alter_table_clause ::=
     {:
         RESULT = new CreateIndexClause(null, indexDef, true);
     :}
-    | KW_DROP KW_INDEX ident:indexName
+    | KW_DROP KW_INDEX opt_if_exists:ifExists ident:indexName
     {:
-        RESULT = new DropIndexClause(indexName, null, true);
+        RESULT = new DropIndexClause(indexName, ifExists, null, true);
     :}
     | KW_ENABLE KW_FEATURE STRING_LITERAL:featureName 
opt_enable_feature_properties:properties
     {:
@@ -1302,9 +1302,9 @@ create_stmt ::=
     {:
         RESULT = new CreateMaterializedViewStmt(mvName, selectStmt, 
properties);
     :}
-    | KW_CREATE KW_INDEX ident:indexName KW_ON table_name:tableName LPAREN 
ident_list:cols RPAREN opt_index_type:indexType opt_comment:comment
+    | KW_CREATE KW_INDEX opt_if_not_exists:ifNotExists ident:indexName KW_ON 
table_name:tableName LPAREN ident_list:cols RPAREN opt_index_type:indexType 
opt_comment:comment
     {:
-        RESULT = new AlterTableStmt(tableName, Lists.newArrayList(new 
CreateIndexClause(tableName, new IndexDef(indexName, cols, indexType, comment), 
false)));
+        RESULT = new AlterTableStmt(tableName, Lists.newArrayList(new 
CreateIndexClause(tableName, new IndexDef(indexName, ifNotExists, cols, 
indexType, comment), false)));
     :}
     /* resource */
     | KW_CREATE opt_external:isExternal KW_RESOURCE ident_or_text:resourceName 
opt_properties:properties
@@ -2011,9 +2011,9 @@ drop_stmt ::=
     {:
         RESULT = new DropFileStmt(fileName, dbName, properties);
     :}
-    | KW_DROP KW_INDEX ident:indexName KW_ON table_name:tableName
+    | KW_DROP KW_INDEX opt_if_exists:ifExists ident:indexName KW_ON 
table_name:tableName
     {:
-        RESULT = new AlterTableStmt(tableName, Lists.newArrayList(new 
DropIndexClause(indexName, tableName, false)));
+        RESULT = new AlterTableStmt(tableName, Lists.newArrayList(new 
DropIndexClause(indexName, ifExists, tableName, false)));
     :}
     | KW_DROP KW_MATERIALIZED KW_VIEW opt_if_exists:ifExists ident:mvName 
KW_ON table_name:tableName
     {:
@@ -2416,9 +2416,9 @@ column_definition ::=
     ;
 
 index_definition ::=
-    KW_INDEX ident:indexName LPAREN ident_list:cols RPAREN 
opt_index_type:indexType opt_comment:comment
+    KW_INDEX opt_if_not_exists:ifNotExists ident:indexName LPAREN 
ident_list:cols RPAREN opt_index_type:indexType opt_comment:comment
     {:
-        RESULT = new IndexDef(indexName, cols, indexType, comment);
+        RESULT = new IndexDef(indexName, ifNotExists, cols, indexType, 
comment);
     :}
     ;
 
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 4e839db..4abdf90 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
@@ -1702,9 +1702,13 @@ public class SchemaChangeHandler extends AlterHandler {
                     // modify table properties
                     // do nothing, properties are already in propertyMap
                 } else if (alterClause instanceof CreateIndexClause) {
-                    processAddIndex((CreateIndexClause) alterClause, 
olapTable, newIndexes);
+                    if (processAddIndex((CreateIndexClause) alterClause, 
olapTable, newIndexes)) {
+                        return;
+                    }
                 } else if (alterClause instanceof DropIndexClause) {
-                    processDropIndex((DropIndexClause) alterClause, olapTable, 
newIndexes);
+                    if (processDropIndex((DropIndexClause) alterClause, 
olapTable, newIndexes)) {
+                        return;
+                    }
                 } else {
                     Preconditions.checkState(false);
                 }
@@ -1967,10 +1971,14 @@ public class SchemaChangeHandler extends AlterHandler {
         }
     }
 
-    private void processAddIndex(CreateIndexClause alterClause, OlapTable 
olapTable, List<Index> newIndexes)
+    /**
+     * 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.
+     */
+    private boolean processAddIndex(CreateIndexClause alterClause, OlapTable 
olapTable, List<Index> newIndexes)
             throws UserException {
         if (alterClause.getIndex() == null) {
-            return;
+            return false;
         }
 
         List<Index> existedIndexes = olapTable.getIndexes();
@@ -1979,6 +1987,10 @@ public class SchemaChangeHandler extends AlterHandler {
         newColset.addAll(indexDef.getColumns());
         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);
@@ -1999,9 +2011,14 @@ public class SchemaChangeHandler extends AlterHandler {
         }
 
         newIndexes.add(alterClause.getIndex());
+        return false;
     }
 
-    private void processDropIndex(DropIndexClause alterClause, OlapTable 
olapTable, List<Index> indexes) throws DdlException {
+    /**
+     * 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.
+     */
+    private boolean processDropIndex(DropIndexClause alterClause, OlapTable 
olapTable, List<Index> indexes) throws DdlException {
         String indexName = alterClause.getIndexName();
         List<Index> existedIndexes = olapTable.getIndexes();
         Index found = null;
@@ -2012,6 +2029,10 @@ public class SchemaChangeHandler extends AlterHandler {
             }
         }
         if (found == null) {
+            if (alterClause.isSetIfExists()) {
+                LOG.info("drop index[{}] which does not exist on table[{}]", 
indexName, olapTable.getName());
+                return true;
+            }
             throw new DdlException("index " + indexName + " does not exist");
         }
 
@@ -2023,6 +2044,7 @@ public class SchemaChangeHandler extends AlterHandler {
                 break;
             }
         }
+        return false;
     }
 
     @Override
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 f5f4a20..749ce16 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
@@ -28,11 +28,14 @@ import java.util.Map;
 public class DropIndexClause extends AlterTableClause {
     private final String indexName;
     private final TableName tableName;
+    private boolean ifExists;
+
     private boolean alter;
 
-    public DropIndexClause(String indexName, TableName tableName, boolean 
alter) {
+    public DropIndexClause(String indexName, boolean ifExists, TableName 
tableName, boolean alter) {
         super(AlterOpType.SCHEMA_CHANGE);
         this.indexName = indexName;
+        this.ifExists = ifExists;
         this.tableName = tableName;
         this.alter = alter;
     }
@@ -45,6 +48,10 @@ public class DropIndexClause extends AlterTableClause {
         return tableName;
     }
 
+    public boolean isSetIfExists() {
+        return ifExists;
+    }
+
     public boolean isAlter() {
         return alter;
     }
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 d19ff11..8624bb3 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
@@ -29,12 +29,14 @@ import java.util.TreeSet;
 
 public class IndexDef {
     private String indexName;
+    private boolean ifNotExists;
     private List<String> columns;
     private IndexType indexType;
     private String comment;
 
-    public IndexDef(String indexName, List<String> columns, IndexType 
indexType, String comment) {
+    public IndexDef(String indexName, boolean ifNotExists, List<String> 
columns, IndexType indexType, String comment) {
         this.indexName = indexName;
+        this.ifNotExists = ifNotExists;
         this.columns = columns;
         if (indexType == null) {
             this.indexType = IndexType.BITMAP;
@@ -118,6 +120,10 @@ public class IndexDef {
         return comment;
     }
 
+    public boolean isSetIfNotExists() {
+        return ifNotExists;
+    }
+
     public enum IndexType {
         BITMAP,
 
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 408ecc0..bbabc71 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
@@ -35,7 +35,7 @@ public class CreateIndexClauseTest {
 
     @Test
     public void testNormal() throws AnalysisException {
-        CreateIndexClause clause = new CreateIndexClause(new TableName("db", 
"table"), new IndexDef("index1",
+        CreateIndexClause clause = new CreateIndexClause(new TableName("db", 
"table"), new IndexDef("index1", false,
                 Lists.newArrayList("col1"), IndexDef.IndexType.BITMAP, 
"balabala"), false);
         clause.analyze(analyzer);
         Assert.assertEquals("CREATE INDEX index1 ON `db`.`table` (`col1`) 
USING BITMAP COMMENT 'balabala'",
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 cb9177f..b2f8b95 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
@@ -17,6 +17,7 @@
 
 package org.apache.doris.analysis;
 
+import org.apache.doris.catalog.Catalog;
 import org.apache.doris.common.AnalysisException;
 import org.apache.doris.common.UserException;
 
@@ -35,14 +36,14 @@ public class DropIndexClauseTest {
 
     @Test
     public void testNormal() throws UserException {
-        DropIndexClause clause = new DropIndexClause("index1", new 
TableName("db", "table"), false);
+        DropIndexClause clause = new DropIndexClause("index1", false, new 
TableName("db", "table"), false);
         clause.analyze(analyzer);
         Assert.assertEquals("DROP INDEX index1 ON `db`.`table`", 
clause.toSql());
     }
 
     @Test(expected = AnalysisException.class)
     public void testNoIndex() throws UserException {
-        DropIndexClause clause = new DropIndexClause("", new TableName("db", 
"table"), false);
+        DropIndexClause clause = new DropIndexClause("", false, new 
TableName("db", "table"), false);
         clause.analyze(analyzer);
     }
 }
\ No newline at end of file
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 8f51021..6a0e8be 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
@@ -30,7 +30,7 @@ public class IndexDefTest {
 
     @Before
     public void setUp() throws Exception {
-        def = new IndexDef("index1", Lists.newArrayList("col1"), 
IndexDef.IndexType.BITMAP, "balabala");
+        def = new IndexDef("index1", false, Lists.newArrayList("col1"), 
IndexDef.IndexType.BITMAP, "balabala");
     }
 
     @Test
@@ -46,7 +46,7 @@ public class IndexDefTest {
                             + 
"xxxxxxxxxxxindex1xxxxxxxxxxxxxxxxxindex1xxxxxxxxxxxxxxxxxindex1xxxxxxxxxxxxxxxxxinde"
                             + 
"x1xxxxxxxxxxxxxxxxxindex1xxxxxxxxxxxxxxxxxindex1xxxxxxxxxxxxxxxxxindex1xxxxxxxxxxxxx"
                             + 
"xxxxindex1xxxxxxxxxxxxxxxxxindex1xxxxxxxxxxxxxxxxxindex1xxxxxxxxxxxxxxxxxindex1xxxxx"
-                            + "xxxxxxxxxxxxindex1xxxxxxxxxxxxxxxxx",
+                            + "xxxxxxxxxxxxindex1xxxxxxxxxxxxxxxxx", false,
                     Lists.newArrayList("col1"), IndexDef.IndexType.BITMAP,
                     "balabala");
             def.analyze();
@@ -55,7 +55,7 @@ public class IndexDefTest {
             Assert.assertTrue(e instanceof AnalysisException);
         }
         try {
-            def = new IndexDef("", Lists.newArrayList("col1"), 
IndexDef.IndexType.BITMAP, "balabala");
+            def = new IndexDef("", false, Lists.newArrayList("col1"), 
IndexDef.IndexType.BITMAP, "balabala");
             def.analyze();
             Assert.fail("No exception throws.");
         } catch (AnalysisException e) {

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to