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

liulijia 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 e7707c8  [FOLLOWUP] create table like clause support copy rollup 
(#6580)
e7707c8 is described below

commit e7707c8180fbc6b8e02c4cc8b9f32e2e99ecb156
Author: shee <13843187+qz...@users.noreply.github.com>
AuthorDate: Thu Sep 30 18:26:21 2021 +0800

    [FOLLOWUP] create table like clause support copy rollup (#6580)
    
    * Remove `ALL` key word to make grammar more clear.
    
    Co-authored-by: qzsee <shizhiqian...@meituan.com>
---
 .../Data Definition/CREATE TABLE LIKE.md           | 10 +++----
 .../Data Definition/CREATE TABLE LIKE.md           | 10 +++----
 fe/fe-core/src/main/cup/sql_parser.cup             |  4 +--
 .../apache/doris/analysis/CreateTableLikeStmt.java | 18 ++++++++++--
 .../java/org/apache/doris/catalog/Catalog.java     | 33 ++++++++--------------
 .../apache/doris/catalog/CreateTableLikeTest.java  | 14 +++++----
 6 files changed, 48 insertions(+), 41 deletions(-)

diff --git a/docs/en/sql-reference/sql-statements/Data Definition/CREATE TABLE 
LIKE.md b/docs/en/sql-reference/sql-statements/Data Definition/CREATE TABLE 
LIKE.md
index 47284de..b0cb703 100644
--- a/docs/en/sql-reference/sql-statements/Data Definition/CREATE TABLE LIKE.md 
+++ b/docs/en/sql-reference/sql-statements/Data Definition/CREATE TABLE LIKE.md 
@@ -32,7 +32,7 @@ Use CREATE TABLE ... LIKE to create an empty table based on 
the definition of an
 Syntax:
 
 ```
-    CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [database.]table_name LIKE 
[database.]table_name [WITH ROLLUP r1,r2,r3,...|ALL] 
+    CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [database.]table_name LIKE 
[database.]table_name [WITH ROLLUP (r2,r2,r3,...)] 
 ```
 
 Explain:
@@ -52,19 +52,19 @@ Explain:
 
     3. Under the test1 Database, create an empty table with the same table 
structure as table1, named table2. copy r1 and r2 rollup of table1 
simultaneously
 
-        CREATE TABLE test1.table2 LIKE test1.table1 WITH ROLLUP r1,r2
+        CREATE TABLE test1.table2 LIKE test1.table1 WITH ROLLUP (r1,r2)
 
     4. Under the test1 Database, create an empty table with the same table 
structure as table1, named table2. copy all rollup of table1 simultaneously
 
-        CREATE TABLE test1.table2 LIKE test1.table1 WITH ROLLUP ALL
+        CREATE TABLE test1.table2 LIKE test1.table1 WITH ROLLUP
 
     5. Under the test2 Database, create an empty table with the same table 
structure as table1, named table2. copy r1 and r2 rollup of table1 
simultaneously
 
-        CREATE TABLE test2.table2 LIKE test1.table1 WITH ROLLUP r1,r2
+        CREATE TABLE test2.table2 LIKE test1.table1 WITH ROLLUP (r1,r2)
 
     6. Under the test2 Database, create an empty table with the same table 
structure as table1, named table2. copy all rollup of table1 simultaneously
 
-        CREATE TABLE test2.table2 LIKE test1.table1 WITH ROLLUP ALL
+        CREATE TABLE test2.table2 LIKE test1.table1 WITH ROLLUP
     
     7. Under the test1 Database, create an empty table with the same table 
structure as MySQL's external table1, called table2
 
diff --git a/docs/zh-CN/sql-reference/sql-statements/Data Definition/CREATE 
TABLE LIKE.md b/docs/zh-CN/sql-reference/sql-statements/Data Definition/CREATE 
TABLE LIKE.md
index 82c1378..89c70f0 100644
--- a/docs/zh-CN/sql-reference/sql-statements/Data Definition/CREATE TABLE 
LIKE.md      
+++ b/docs/zh-CN/sql-reference/sql-statements/Data Definition/CREATE TABLE 
LIKE.md      
@@ -32,7 +32,7 @@ under the License.
 语法:
 
 ```
-    CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [database.]table_name LIKE 
[database.]table_name
+    CREATE [EXTERNAL] TABLE [IF NOT EXISTS] [database.]table_name LIKE 
[database.]table_name [WITH ROLLUP (r1,r2,r3,...)]
 ```
 
 说明:
@@ -52,19 +52,19 @@ under the License.
 
     3. 在test1库下创建一张表结构和table1相同的空表,表名为table2,同时复制table1的r1,r2两个rollup
 
-        CREATE TABLE test1.table2 LIKE test1.table1 WITH ROLLUP r1,r2
+        CREATE TABLE test1.table2 LIKE test1.table1 WITH ROLLUP (r1,r2)
 
     4. 在test1库下创建一张表结构和table1相同的空表,表名为table2,同时复制table1的所有rollup
 
-        CREATE TABLE test1.table2 LIKE test1.table1 WITH ROLLUP ALL
+        CREATE TABLE test1.table2 LIKE test1.table1 WITH ROLLUP
 
     5. 在test2库下创建一张表结构和test1.table1相同的空表,表名为table2,同时复制table1的r1,r2两个rollup
 
-        CREATE TABLE test2.table2 LIKE test1.table1 WITH ROLLUP r1,r2
+        CREATE TABLE test2.table2 LIKE test1.table1 WITH ROLLUP (r1,r2)
 
     6. 在test2库下创建一张表结构和test1.table1相同的空表,表名为table2,同时复制table1的所有rollup
 
-        CREATE TABLE test2.table2 LIKE test1.table1 WITH ROLLUP ALL
+        CREATE TABLE test2.table2 LIKE test1.table1 WITH ROLLUP
 
     7. 在test1库下创建一张表结构和MySQL外表table1相同的空表,表名为table2
 
diff --git a/fe/fe-core/src/main/cup/sql_parser.cup 
b/fe/fe-core/src/main/cup/sql_parser.cup
index 6d4f703..bfb936e 100644
--- a/fe/fe-core/src/main/cup/sql_parser.cup
+++ b/fe/fe-core/src/main/cup/sql_parser.cup
@@ -1198,11 +1198,11 @@ create_stmt ::=
         RESULT = new CreateFunctionStmt(functionName, args, parameters, func);
     :}
     /* Table */
-    | KW_CREATE opt_external:isExternal KW_TABLE opt_if_not_exists:ifNotExists 
table_name:name KW_LIKE table_name:existed_name KW_WITH KW_ROLLUP 
ident_list:rollupNames
+    | KW_CREATE opt_external:isExternal KW_TABLE opt_if_not_exists:ifNotExists 
table_name:name KW_LIKE table_name:existed_name KW_WITH KW_ROLLUP LPAREN 
ident_list:rollupNames RPAREN
     {:
         RESULT = new CreateTableLikeStmt(ifNotExists, name, existed_name, 
rollupNames, false);
     :}
-    | KW_CREATE opt_external:isExternal KW_TABLE opt_if_not_exists:ifNotExists 
table_name:name KW_LIKE table_name:existed_name KW_WITH KW_ROLLUP KW_ALL
+    | KW_CREATE opt_external:isExternal KW_TABLE opt_if_not_exists:ifNotExists 
table_name:name KW_LIKE table_name:existed_name KW_WITH KW_ROLLUP
     {:
         RESULT = new CreateTableLikeStmt(ifNotExists, name, existed_name, 
null, true);
     :}
diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateTableLikeStmt.java 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateTableLikeStmt.java
index 4a00f3b..5865cd2 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateTableLikeStmt.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateTableLikeStmt.java
@@ -17,7 +17,10 @@
 
 package org.apache.doris.analysis;
 
+import com.google.common.base.Joiner;
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.doris.catalog.Catalog;
+import org.apache.doris.common.DdlException;
 import org.apache.doris.common.ErrorCode;
 import org.apache.doris.common.ErrorReport;
 import org.apache.doris.common.FeNameFormat;
@@ -43,10 +46,13 @@ public class CreateTableLikeStmt extends DdlStmt {
     private final ArrayList<String> rollupNames;
     private final boolean withAllRollup;
 
-    public CreateTableLikeStmt(boolean ifNotExists, TableName tableName, 
TableName existedTableName, ArrayList<String> rollupNames, boolean 
withAllRollup) {
+    public CreateTableLikeStmt(boolean ifNotExists, TableName tableName, 
TableName existedTableName, ArrayList<String> rollupNames, boolean 
withAllRollup) throws DdlException {
         this.ifNotExists = ifNotExists;
         this.tableName = tableName;
         this.existedTableName = existedTableName;
+        if (!CollectionUtils.isEmpty(rollupNames) && withAllRollup){
+            throw new DdlException("Either all or part of the rollup can be 
copied, not both");
+        }
         this.rollupNames = rollupNames;
         this.withAllRollup = withAllRollup;
     }
@@ -99,7 +105,15 @@ public class CreateTableLikeStmt extends DdlStmt {
 
     @Override
     public String toSql() {
-        return String.format("CREATE TABLE %s LIKE %s", tableName.toSql(), 
existedTableName.toSql());
+        StringBuilder sb = new StringBuilder();
+        sb.append("CREATE TABLE ").append(tableName.toSql()).append(" LIKE 
").append(existedTableName.toSql());
+        if (withAllRollup && CollectionUtils.isEmpty(rollupNames)){
+            sb.append(" WITH ROLLUP");
+        }
+        if (!withAllRollup && !CollectionUtils.isEmpty(rollupNames)){
+            sb.append(" WITH ROLLUP 
(").append(Joiner.on(",").join(rollupNames)).append(")");
+        }
+        return sb.toString();
     }
 
     @Override
diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java 
b/fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java
index 6a248fc..f846b01 100755
--- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Catalog.java
@@ -3082,6 +3082,8 @@ public class Catalog {
                             }
                         }
                     }
+                } else if (!CollectionUtils.isEmpty(stmt.getRollupNames()) || 
stmt.isWithAllRollup()) {
+                    throw new DdlException("Table[" + table.getName() + "] is 
external, not support rollup copy");
                 }
 
                 Catalog.getDdlStmt(stmt, stmt.getDbName(), table, 
createTableStmt, null, null, false, false);
@@ -4112,39 +4114,26 @@ public class Catalog {
 
                 ArrayList<String> rollupNames = stmt.getRollupNames();
                 boolean withAllRollup = stmt.isWithAllRollup();
+                List<Long> addIndexIdList = Lists.newArrayList();
 
-                Map<Long,MaterializedIndexMeta> addMVs = Maps.newHashMap();
-                Map<String, Long> indexNameToId = olapTable.getIndexNameToId();
-
-                boolean needAddRollup = false;
                 if (!CollectionUtils.isEmpty(rollupNames)) {
                     for (String rollupName : rollupNames) {
-                        
addMVs.put(indexNameToId.get(rollupName),olapTable.getIndexMetaByIndexId(indexNameToId.get(rollupName)));
-                    }
-                    needAddRollup = true;
-                }
-
-                if (withAllRollup && rollupNames == null) {
-                    for (Entry<Long, MaterializedIndexMeta> entry : 
olapTable.getIndexIdToMeta().entrySet()) {
-                        if (entry.getKey() == olapTable.getBaseIndexId()) {
-                            continue;
-                        }
-                        addMVs.put(entry.getKey(), entry.getValue());
+                        
addIndexIdList.add(olapTable.getIndexIdByName(rollupName));
                     }
-                    needAddRollup = true;
+                } else if (withAllRollup) {
+                    addIndexIdList = olapTable.getIndexIdListExceptBaseIndex();
                 }
 
-                if (needAddRollup){
+                if (!addIndexIdList.isEmpty()){
                     sb.append("\n").append("rollup (");
                 }
 
-                int size = addMVs.size();
+                int size = addIndexIdList.size();
                 int index = 1;
-                for (Map.Entry<Long, MaterializedIndexMeta> entry : 
addMVs.entrySet()) {
-                    MaterializedIndexMeta materializedIndexMeta = 
entry.getValue();
-                    String indexName = 
olapTable.getIndexNameById(entry.getKey());
+                for (long indexId : addIndexIdList) {
+                    String indexName = olapTable.getIndexNameById(indexId);
                     sb.append("\n").append(indexName).append("(");
-                    List<Column> indexSchema = 
materializedIndexMeta.getSchema();
+                    List<Column> indexSchema = 
olapTable.getSchemaByIndexId(indexId, false);
                     for (int i = 0; i < indexSchema.size(); i++) {
                         Column column = indexSchema.get(i);
                         sb.append(column.getName());
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateTableLikeTest.java 
b/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateTableLikeTest.java
index 1743b61..25d0cd5 100644
--- a/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateTableLikeTest.java
+++ b/fe/fe-core/src/test/java/org/apache/doris/catalog/CreateTableLikeTest.java
@@ -17,11 +17,14 @@
 
 package org.apache.doris.catalog;
 
+import avro.shaded.com.google.common.collect.Lists;
+import org.apache.commons.collections.ListUtils;
 import org.apache.doris.analysis.CreateDbStmt;
 import org.apache.doris.analysis.CreateTableLikeStmt;
 import org.apache.doris.analysis.CreateTableStmt;
 import org.apache.doris.common.DdlException;
 import org.apache.doris.common.ExceptionChecker;
+import org.apache.doris.common.util.ListUtil;
 import org.apache.doris.qe.ConnectContext;
 import org.apache.doris.utframe.UtFrameUtils;
 
@@ -284,8 +287,8 @@ public class CreateTableLikeTest {
                 ")\n" +
                 "PROPERTIES(\"replication_num\" = \"1\");";
 
-        String createTableLikeWithRollupSql1_1 = "create table 
test.table_like_rollup like test.table_with_rollup with rollup r1,r2";
-        String createTableLikeWithRollupSql1_2 = "create table 
test.table_like_rollup1 like test.table_with_rollup with rollup all";
+        String createTableLikeWithRollupSql1_1 = "create table 
test.table_like_rollup like test.table_with_rollup with rollup (r1,r2)";
+        String createTableLikeWithRollupSql1_2 = "create table 
test.table_like_rollup1 like test.table_with_rollup with rollup";
 
         String newDbName10 = "test";
         String existedDbName10 = "test";
@@ -295,8 +298,8 @@ public class CreateTableLikeTest {
         checkCreateOlapTableLike(createTableWithRollup, 
createTableLikeWithRollupSql1_1, newDbName10, existedDbName10, newTblName10_1, 
existedTblName10, 2);
         checkCreateOlapTableLike(createTableWithRollup, 
createTableLikeWithRollupSql1_2, newDbName10, existedDbName10, newTblName10_2, 
existedTblName10, 4);
 
-        String createTableLikeWithRollupSql2_1 = "create table 
test2.table_like_rollup like test.table_with_rollup with rollup r1,r2";
-        String createTableLikeWithRollupSql2_2 = "create table 
test2.table_like_rollup1 like test.table_with_rollup with rollup all";
+        String createTableLikeWithRollupSql2_1 = "create table 
test2.table_like_rollup like test.table_with_rollup with rollup (r1,r2)";
+        String createTableLikeWithRollupSql2_2 = "create table 
test2.table_like_rollup1 like test.table_with_rollup with rollup";
 
         String newDbName11 = "test2";
         String existedDbName11 = "test";
@@ -355,7 +358,8 @@ public class CreateTableLikeTest {
                 ")\n" +
                 "PROPERTIES(\"replication_num\" = \"1\");";
 
-        String createTableLikeWithRollupSq3 = "create table 
test.table_like_rollup like test.table_with_rollup with rollup r11";
+        String createTableLikeWithRollupSq3 = "create table 
test.table_like_rollup like test.table_with_rollup with rollup (r11)";
+
         String newDbName3 = "test";
         String existedDbName3 = "test";
         String newTblName3 = "table_like_rollup";

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

Reply via email to