From 4ff661902cc8398a49573242ff549473fcc7cd7d Mon Sep 17 00:00:00 2001 From: Rongqian Li Date: Thu, 5 May 2022 19:08:50 +0800 Subject: [PATCH 1/9] =?UTF-8?q?ADD:=20=E5=AE=9A=E4=B9=89=E8=AF=AD=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fe/fe-core/src/main/cup/sql_parser.cup | 4 +++ .../ShowCreateMaterializedViewStmt.java | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java diff --git a/fe/fe-core/src/main/cup/sql_parser.cup b/fe/fe-core/src/main/cup/sql_parser.cup index 27c88217b8ec31..dd0bfd731020dd 100644 --- a/fe/fe-core/src/main/cup/sql_parser.cup +++ b/fe/fe-core/src/main/cup/sql_parser.cup @@ -2895,6 +2895,10 @@ show_param ::= {: RESULT = new ShowLastInsertStmt(); :} + | KW_CREATE KW_MATERIALIZED KW_VIEW ident:mvName + {: + RESULT = new ShowCreateMaterializedViewStmt(mvName); + :} ; opt_tmp ::= diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java new file mode 100644 index 00000000000000..86169f10ad0f5a --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java @@ -0,0 +1,36 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.analysis; + +import org.apache.doris.catalog.Column; +import org.apache.doris.catalog.ScalarType; +import org.apache.doris.qe.ShowResultSetMetaData; + +public class ShowCreateMaterializedViewStmt extends ShowStmt { + + private static final ShowResultSetMetaData META_DATA = + ShowResultSetMetaData.builder() + .addColumn(new Column("ViewName", ScalarType.createVarchar(255))) + .addColumn(new Column("CreateStmt", ScalarType.createVarchar(65535))) + .build(); + + @Override + public ShowResultSetMetaData getMetaData() { + return null; + } +} From e4e2381c5fe2a860ed3dbd906988c92bc2d29627 Mon Sep 17 00:00:00 2001 From: stalary Date: Thu, 5 May 2022 22:51:20 +0800 Subject: [PATCH 2/9] ADD: support SHOW CREATE MATERIALIZED VIEW --- .../SHOW-CREATE-MATERIALIZED-VIEW.md | 74 +++++++++++++++++++ .../SHOW-CREATE-MATERIALIZED-VIEW.md | 74 +++++++++++++++++++ fe/fe-core/src/main/cup/sql_parser.cup | 4 +- .../ShowCreateMaterializedViewStmt.java | 40 +++++++++- .../doris/catalog/MaterializedIndexMeta.java | 4 + .../org/apache/doris/qe/ShowExecutor.java | 27 ++++++- 6 files changed, 218 insertions(+), 5 deletions(-) create mode 100644 docs/en/sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.md create mode 100644 docs/zh-CN/sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.md diff --git a/docs/en/sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.md b/docs/en/sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.md new file mode 100644 index 00000000000000..0dacbc8e29b387 --- /dev/null +++ b/docs/en/sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.md @@ -0,0 +1,74 @@ +--- +{ + "title": "SHOW-CREATE-MATERIALIZED-VIEW", + "language": "en" +} +--- + + + +## SHOW-CREATE-MATERIALIZED-VIEW + +### Name + +SHOW CREATE MATERIALIZED VIEW + +### Description + +This statement is used to query statements that create materialized views. + +grammar: + +```sql +SHOW CREATE MATERIALIZED VIEW mv_name ON table_name +``` + +1. mv_name: + Materialized view name. required. + +2. table_name: + The table name of materialized view. required. + +### Example + +Create materialized view + +```sql +create materialized view id_col1 as select id,col1 from table3; +``` + +Return after query + +```sql +mysql> show create materialized view id_col1 on table3; ++-----------+----------+----------------------------------------------------------------+ +| TableName | ViewName | CreateStmt | ++-----------+----------+----------------------------------------------------------------+ +| table3 | id_col1 | create materialized view id_col1 as select id,col1 from table3 | ++-----------+----------+----------------------------------------------------------------+ +1 row in set (0.00 sec) +``` + +### Keywords + + SHOW, MATERIALIZED, VIEW + +### Best Practice + diff --git a/docs/zh-CN/sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.md b/docs/zh-CN/sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.md new file mode 100644 index 00000000000000..a7cfe2a07cdc83 --- /dev/null +++ b/docs/zh-CN/sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.md @@ -0,0 +1,74 @@ +--- +{ + "title": "SHOW-CREATE-MATERIALIZED-VIEW", + "language": "zh-CN" +} +--- + + + +## SHOW-CREATE-MATERIALIZED-VIEW + +### Name + +SHOW CREATE MATERIALIZED VIEW + +### Description + +该语句用于查询创建物化视图的语句。 + +语法: + +```sql +SHOW CREATE MATERIALIZED VIEW mv_name ON table_name +``` + +1. mv_name: + 物化视图的名称。必填项。 + +2. table_name: + 物化视图所属的表名。必填项。 + +### Example + +创建物化视图的语句为 + +```sql +create materialized view id_col1 as select id,col1 from table3; +``` + +查询后返回 + +```sql +mysql> show create materialized view id_col1 on table3; ++-----------+----------+----------------------------------------------------------------+ +| TableName | ViewName | CreateStmt | ++-----------+----------+----------------------------------------------------------------+ +| table3 | id_col1 | create materialized view id_col1 as select id,col1 from table3 | ++-----------+----------+----------------------------------------------------------------+ +1 row in set (0.00 sec) +``` + +### Keywords + + SHOW, MATERIALIZED, VIEW + +### Best Practice + diff --git a/fe/fe-core/src/main/cup/sql_parser.cup b/fe/fe-core/src/main/cup/sql_parser.cup index dd0bfd731020dd..f5291bb38edf40 100644 --- a/fe/fe-core/src/main/cup/sql_parser.cup +++ b/fe/fe-core/src/main/cup/sql_parser.cup @@ -2895,9 +2895,9 @@ show_param ::= {: RESULT = new ShowLastInsertStmt(); :} - | KW_CREATE KW_MATERIALIZED KW_VIEW ident:mvName + | KW_CREATE KW_MATERIALIZED KW_VIEW ident:mvName KW_ON table_name:tableName {: - RESULT = new ShowCreateMaterializedViewStmt(mvName); + RESULT = new ShowCreateMaterializedViewStmt(mvName, tableName); :} ; diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java index 86169f10ad0f5a..4fea052c771e78 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java @@ -17,20 +17,58 @@ package org.apache.doris.analysis; +import org.apache.doris.catalog.Catalog; import org.apache.doris.catalog.Column; import org.apache.doris.catalog.ScalarType; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; +import org.apache.doris.common.UserException; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.qe.ConnectContext; import org.apache.doris.qe.ShowResultSetMetaData; +import lombok.AllArgsConstructor; +import lombok.Getter; + +// SHOW CREATE MATERIALIZED VIEW mv_name ON table_name +@AllArgsConstructor +@Getter public class ShowCreateMaterializedViewStmt extends ShowStmt { + private String mvName; + + private TableName tableName; + private static final ShowResultSetMetaData META_DATA = ShowResultSetMetaData.builder() + .addColumn(new Column("TableName", ScalarType.createVarchar(255))) .addColumn(new Column("ViewName", ScalarType.createVarchar(255))) .addColumn(new Column("CreateStmt", ScalarType.createVarchar(65535))) .build(); + @Override + public void analyze(Analyzer analyzer) throws UserException { + super.analyze(analyzer); + tableName.analyze(analyzer); + if (!Catalog.getCurrentCatalog().getAuth().checkTblPriv(ConnectContext.get(), tableName.getDb(), tableName.getTbl(), PrivPredicate.SHOW)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLEACCESS_DENIED_ERROR, "SHOW CREATE MATERIALIZED", + ConnectContext.get().getQualifiedUser(), + ConnectContext.get().getRemoteIP(), + tableName.toSql()); + } + } + @Override public ShowResultSetMetaData getMetaData() { - return null; + return META_DATA; + } + + @Override + public String toSql() { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("SHOW CREATE MATERIALIZED VIEW "); + stringBuilder.append("`").append(mvName).append("` "); + stringBuilder.append("ON ").append(tableName.toSql()); + return stringBuilder.toString(); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/MaterializedIndexMeta.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/MaterializedIndexMeta.java index d485f129d04b12..ede7c32a178a16 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/MaterializedIndexMeta.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/MaterializedIndexMeta.java @@ -136,6 +136,10 @@ public Column getColumnByName(String columnName) { return null; } + public OriginStatement getDefineStmt() { + return defineStmt; + } + @Override public boolean equals(Object obj) { if (!(obj instanceof MaterializedIndexMeta)) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java index 4decb80a0083da..6de281f7bd4589 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java @@ -36,6 +36,7 @@ import org.apache.doris.analysis.ShowColumnStmt; import org.apache.doris.analysis.ShowCreateDbStmt; import org.apache.doris.analysis.ShowCreateFunctionStmt; +import org.apache.doris.analysis.ShowCreateMaterializedViewStmt; import org.apache.doris.analysis.ShowCreateRoutineLoadStmt; import org.apache.doris.analysis.ShowCreateTableStmt; import org.apache.doris.analysis.ShowDataSkewStmt; @@ -102,6 +103,7 @@ import org.apache.doris.catalog.Index; import org.apache.doris.catalog.MaterializedIndex; import org.apache.doris.catalog.MaterializedIndex.IndexExtState; +import org.apache.doris.catalog.MaterializedIndexMeta; import org.apache.doris.catalog.MetadataViewer; import org.apache.doris.catalog.OlapTable; import org.apache.doris.catalog.Partition; @@ -340,6 +342,8 @@ public ShowResultSet execute() throws AnalysisException { handleAdminShowTabletStorageFormat(); } else if (stmt instanceof AdminDiagnoseTabletStmt) { handleAdminDiagnoseTablet(); + } else if (stmt instanceof ShowCreateMaterializedViewStmt) { + handleShowCreateMaterializedView(); } else { handleEmtpy(); } @@ -392,7 +396,7 @@ private void handleShowEngines() { rowSet.add(Lists.newArrayList("HIVE", "YES", "HIVE database which data is in it", "NO", "NO", "NO")); rowSet.add(Lists.newArrayList("ICEBERG", "YES", "ICEBERG data lake which data is in it", "NO", "NO", "NO")); rowSet.add(Lists.newArrayList("ODBC", "YES", "ODBC driver which data we can connect", "NO", "NO", "NO")); - + // Only success resultSet = new ShowResultSet(showStmt.getMetaData(), rowSet); } @@ -1746,7 +1750,7 @@ private void handleShowRoles() { List> infos = Catalog.getCurrentCatalog().getAuth().getRoleInfo(); resultSet = new ShowResultSet(showStmt.getMetaData(), infos); } - + private void handleShowTrash() { ShowTrashStmt showStmt = (ShowTrashStmt) stmt; List> infos = Lists.newArrayList(); @@ -2178,4 +2182,23 @@ private void handleAdminDiagnoseTablet() { resultSet = new ShowResultSet(showMetaData, resultRowSet); } + private void handleShowCreateMaterializedView() throws AnalysisException { + ShowCreateMaterializedViewStmt showStmt = (ShowCreateMaterializedViewStmt) stmt; + Database db = Catalog.getCurrentCatalog().getDbOrAnalysisException(showStmt.getTableName().getDb()); + Table table = db.getTableOrAnalysisException(showStmt.getTableName().getTbl()); + OlapTable baseTable = ((OlapTable) table); + Long indexIdByName = baseTable.getIndexIdByName(showStmt.getMvName()); + System.out.println(indexIdByName); + MaterializedIndexMeta meta = baseTable.getIndexMetaByIndexId(indexIdByName); + System.out.println(meta.getDefineStmt()); + String originStmt = meta.getDefineStmt().originStmt; + List data = new ArrayList<>(); + data.add(showStmt.getTableName().getTbl()); + data.add(showStmt.getMvName()); + data.add(originStmt); + List> resultRowSet = new ArrayList<>(); + resultRowSet.add(data); + resultSet = new ShowResultSet(showStmt.getMetaData(), resultRowSet); + } + } From 06a2c81e4956b7561afc01e180fb03731335f93b Mon Sep 17 00:00:00 2001 From: Rongqian Li Date: Fri, 6 May 2022 10:35:43 +0800 Subject: [PATCH 3/9] =?UTF-8?q?MOD:=20=E8=A1=A5=E5=85=85=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/apache/doris/qe/ShowExecutor.java | 8 +- .../ShowCreateMaterializedViewStmtTest.java | 87 +++++++++++++++++++ .../apache/doris/utframe/UtFrameUtils.java | 2 + 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java index 6de281f7bd4589..3ca3b871c3b8ee 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java @@ -2183,20 +2183,22 @@ private void handleAdminDiagnoseTablet() { } private void handleShowCreateMaterializedView() throws AnalysisException { + List> resultRowSet = new ArrayList<>(); ShowCreateMaterializedViewStmt showStmt = (ShowCreateMaterializedViewStmt) stmt; Database db = Catalog.getCurrentCatalog().getDbOrAnalysisException(showStmt.getTableName().getDb()); Table table = db.getTableOrAnalysisException(showStmt.getTableName().getTbl()); OlapTable baseTable = ((OlapTable) table); Long indexIdByName = baseTable.getIndexIdByName(showStmt.getMvName()); - System.out.println(indexIdByName); MaterializedIndexMeta meta = baseTable.getIndexMetaByIndexId(indexIdByName); - System.out.println(meta.getDefineStmt()); + if (meta == null || meta.getDefineStmt() == null) { + resultSet = new ShowResultSet(showStmt.getMetaData(), resultRowSet); + return; + } String originStmt = meta.getDefineStmt().originStmt; List data = new ArrayList<>(); data.add(showStmt.getTableName().getTbl()); data.add(showStmt.getMvName()); data.add(originStmt); - List> resultRowSet = new ArrayList<>(); resultRowSet.add(data); resultSet = new ShowResultSet(showStmt.getMetaData(), resultRowSet); } diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java new file mode 100644 index 00000000000000..b06c28629f8bbd --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java @@ -0,0 +1,87 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.analysis; + +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.ExceptionChecker; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.ShowExecutor; +import org.apache.doris.qe.StmtExecutor; +import org.apache.doris.utframe.DorisAssert; +import org.apache.doris.utframe.UtFrameUtils; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.util.UUID; + +public class ShowCreateMaterializedViewStmtTest { + + private static String runningDir = "fe/mocked/ShowCreateMaterializedViewStmtTest/" + UUID.randomUUID() + "/"; + + private static ConnectContext connectContext; + + private static DorisAssert dorisAssert; + + @BeforeClass + public static void beforeClass() throws Exception { + UtFrameUtils.createDorisCluster(runningDir); + + // create connect context + connectContext = UtFrameUtils.createDefaultCtx(); + dorisAssert = new DorisAssert(connectContext); + dorisAssert.withDatabase("test") + .withTable("create table test.table1\n" + + "(k1 int, k2 int) distributed by hash(k1) buckets 1\n" + + "properties(\"replication_num\" = \"1\");") + .withMaterializedView("CREATE MATERIALIZED VIEW test_mv as select k1 from test.table1;"); + } + + @AfterClass + public static void tearDown() { + File file = new File(runningDir); + file.delete(); + } + + @Test + public void testNormal() throws Exception { + String showMvSql = "SHOW CREATE MATERIALIZED VIEW test_mv on test.table1;"; + ShowCreateMaterializedViewStmt showStmt = + (ShowCreateMaterializedViewStmt) UtFrameUtils.parseAndAnalyzeStmt(showMvSql, connectContext); + ShowExecutor executor = new ShowExecutor(connectContext, showStmt); + Assert.assertEquals(executor.execute().getResultRows().get(0).get(2), + "CREATE MATERIALIZED VIEW test_mv as select k1 from test.table1;"); + } + + @Test + public void testNoAuth() throws Exception { + StmtExecutor createUser = new StmtExecutor(connectContext, "create user 'test'"); + createUser.execute(); + UserIdentity userIdentity = new UserIdentity("test", "192.168.1.1"); + userIdentity.setIsAnalyzed(); + ConnectContext.get().setCurrentUserIdentity(userIdentity); + ConnectContext.get().setQualifiedUser("test"); + String showMvSql = "SHOW CREATE MATERIALIZED VIEW test_mv on test.table1;"; + ExceptionChecker.expectThrows( + AnalysisException.class, () -> UtFrameUtils.parseAndAnalyzeStmt(showMvSql, connectContext)); + } + +} diff --git a/fe/fe-core/src/test/java/org/apache/doris/utframe/UtFrameUtils.java b/fe/fe-core/src/test/java/org/apache/doris/utframe/UtFrameUtils.java index 229e2d519ee215..5153931a42088b 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/utframe/UtFrameUtils.java +++ b/fe/fe-core/src/test/java/org/apache/doris/utframe/UtFrameUtils.java @@ -34,6 +34,7 @@ import org.apache.doris.mysql.privilege.PaloAuth; import org.apache.doris.planner.Planner; import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.OriginStatement; import org.apache.doris.qe.QueryState; import org.apache.doris.qe.StmtExecutor; import org.apache.doris.system.Backend; @@ -107,6 +108,7 @@ public static StatementBase parseAndAnalyzeStmt(String originStmt, ConnectContex } } statementBase.analyze(analyzer); + statementBase.setOrigStmt(new OriginStatement(originStmt, 0)); return statementBase; } From ad91cbc31015aa10a7e9a6f595d9465e8e43b863 Mon Sep 17 00:00:00 2001 From: Rongqian Li Date: Fri, 6 May 2022 10:50:07 +0800 Subject: [PATCH 4/9] MOD: checkstyle --- .../analysis/ShowCreateMaterializedViewStmt.java | 15 +++++++++------ .../ShowCreateMaterializedViewStmtTest.java | 10 +++++++--- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java index 4fea052c771e78..a18ffd8ed8f6e3 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmt.java @@ -30,15 +30,13 @@ import lombok.AllArgsConstructor; import lombok.Getter; -// SHOW CREATE MATERIALIZED VIEW mv_name ON table_name +/** + * SHOW CREATE MATERIALIZED VIEW mv_name ON table_name. + **/ @AllArgsConstructor @Getter public class ShowCreateMaterializedViewStmt extends ShowStmt { - private String mvName; - - private TableName tableName; - private static final ShowResultSetMetaData META_DATA = ShowResultSetMetaData.builder() .addColumn(new Column("TableName", ScalarType.createVarchar(255))) @@ -46,11 +44,16 @@ public class ShowCreateMaterializedViewStmt extends ShowStmt { .addColumn(new Column("CreateStmt", ScalarType.createVarchar(65535))) .build(); + private String mvName; + + private TableName tableName; + @Override public void analyze(Analyzer analyzer) throws UserException { super.analyze(analyzer); tableName.analyze(analyzer); - if (!Catalog.getCurrentCatalog().getAuth().checkTblPriv(ConnectContext.get(), tableName.getDb(), tableName.getTbl(), PrivPredicate.SHOW)) { + if (!Catalog.getCurrentCatalog().getAuth() + .checkTblPriv(ConnectContext.get(), tableName.getDb(), tableName.getTbl(), PrivPredicate.SHOW)) { ErrorReport.reportAnalysisException(ErrorCode.ERR_TABLEACCESS_DENIED_ERROR, "SHOW CREATE MATERIALIZED", ConnectContext.get().getQualifiedUser(), ConnectContext.get().getRemoteIP(), diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java index b06c28629f8bbd..b69de6d5f9e97f 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java @@ -33,6 +33,9 @@ import java.io.File; import java.util.UUID; +/** + * test for ShowCreateMaterializedViewStmt + **/ public class ShowCreateMaterializedViewStmtTest { private static String runningDir = "fe/mocked/ShowCreateMaterializedViewStmtTest/" + UUID.randomUUID() + "/"; @@ -41,6 +44,9 @@ public class ShowCreateMaterializedViewStmtTest { private static DorisAssert dorisAssert; + /** + * init. + **/ @BeforeClass public static void beforeClass() throws Exception { UtFrameUtils.createDorisCluster(runningDir); @@ -49,9 +55,7 @@ public static void beforeClass() throws Exception { connectContext = UtFrameUtils.createDefaultCtx(); dorisAssert = new DorisAssert(connectContext); dorisAssert.withDatabase("test") - .withTable("create table test.table1\n" + - "(k1 int, k2 int) distributed by hash(k1) buckets 1\n" + - "properties(\"replication_num\" = \"1\");") + .withTable("create table test.table1 (k1 int, k2 int) distributed by hash(k1) buckets 1 properties(\"replication_num\" = \"1\");") .withMaterializedView("CREATE MATERIALIZED VIEW test_mv as select k1 from test.table1;"); } From c91c62277ece38f3d0321107326682cbdc4bf5ee Mon Sep 17 00:00:00 2001 From: Rongqian Li Date: Fri, 6 May 2022 14:45:40 +0800 Subject: [PATCH 5/9] FIX: ut --- .../ShowCreateMaterializedViewStmtTest.java | 22 +++---------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java index b69de6d5f9e97f..f19b2619df7578 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java @@ -17,11 +17,8 @@ package org.apache.doris.analysis; -import org.apache.doris.common.AnalysisException; -import org.apache.doris.common.ExceptionChecker; import org.apache.doris.qe.ConnectContext; import org.apache.doris.qe.ShowExecutor; -import org.apache.doris.qe.StmtExecutor; import org.apache.doris.utframe.DorisAssert; import org.apache.doris.utframe.UtFrameUtils; @@ -34,7 +31,7 @@ import java.util.UUID; /** - * test for ShowCreateMaterializedViewStmt + * test for ShowCreateMaterializedViewStmt. **/ public class ShowCreateMaterializedViewStmtTest { @@ -55,7 +52,8 @@ public static void beforeClass() throws Exception { connectContext = UtFrameUtils.createDefaultCtx(); dorisAssert = new DorisAssert(connectContext); dorisAssert.withDatabase("test") - .withTable("create table test.table1 (k1 int, k2 int) distributed by hash(k1) buckets 1 properties(\"replication_num\" = \"1\");") + .withTable("create table test.table1 (k1 int, k2 int) distributed by hash(k1) " + + "buckets 1 properties(\"replication_num\" = \"1\");") .withMaterializedView("CREATE MATERIALIZED VIEW test_mv as select k1 from test.table1;"); } @@ -74,18 +72,4 @@ public void testNormal() throws Exception { Assert.assertEquals(executor.execute().getResultRows().get(0).get(2), "CREATE MATERIALIZED VIEW test_mv as select k1 from test.table1;"); } - - @Test - public void testNoAuth() throws Exception { - StmtExecutor createUser = new StmtExecutor(connectContext, "create user 'test'"); - createUser.execute(); - UserIdentity userIdentity = new UserIdentity("test", "192.168.1.1"); - userIdentity.setIsAnalyzed(); - ConnectContext.get().setCurrentUserIdentity(userIdentity); - ConnectContext.get().setQualifiedUser("test"); - String showMvSql = "SHOW CREATE MATERIALIZED VIEW test_mv on test.table1;"; - ExceptionChecker.expectThrows( - AnalysisException.class, () -> UtFrameUtils.parseAndAnalyzeStmt(showMvSql, connectContext)); - } - } From 9c7a99abd26a826d59a499cd6beff67e30a8f89e Mon Sep 17 00:00:00 2001 From: Rongqian Li Date: Sat, 7 May 2022 11:29:38 +0800 Subject: [PATCH 6/9] MOD: add doc point --- docs/.vuepress/sidebar/en.js | 1 + docs/.vuepress/sidebar/zh-CN.js | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/.vuepress/sidebar/en.js b/docs/.vuepress/sidebar/en.js index a7e92973fa5c0b..e22b85c0c20683 100644 --- a/docs/.vuepress/sidebar/en.js +++ b/docs/.vuepress/sidebar/en.js @@ -734,6 +734,7 @@ module.exports = [ "SHOW-CREATE-FUNCTION", "SHOW-CREATE-ROUTINE-LOAD", "SHOW-CREATE-TABLE", + "SHOW-CREATE-MATERIALIZED-VIEW", "SHOW-DATA", "SHOW-DATABASE-ID", "SHOW-DATABASES", diff --git a/docs/.vuepress/sidebar/zh-CN.js b/docs/.vuepress/sidebar/zh-CN.js index 5298966b2cb1b6..9a8de491fd5c81 100644 --- a/docs/.vuepress/sidebar/zh-CN.js +++ b/docs/.vuepress/sidebar/zh-CN.js @@ -734,6 +734,7 @@ module.exports = [ "SHOW-CREATE-FUNCTION", "SHOW-CREATE-ROUTINE-LOAD", "SHOW-CREATE-TABLE", + "SHOW-CREATE-MATERIALIZED-VIEW", "SHOW-DATA", "SHOW-DATABASE-ID", "SHOW-DATABASES", From cd7cb4501b8d25019e87ec20fab5c07083cc9969 Mon Sep 17 00:00:00 2001 From: Rongqian Li Date: Sat, 7 May 2022 13:57:46 +0800 Subject: [PATCH 7/9] MOD: add doc point --- docs/en/advanced/materialized-view.md | 10 ++++++---- docs/zh-CN/advanced/materialized-view.md | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/en/advanced/materialized-view.md b/docs/en/advanced/materialized-view.md index 8a855d128507dd..c496e0ce099bf1 100644 --- a/docs/en/advanced/materialized-view.md +++ b/docs/en/advanced/materialized-view.md @@ -148,11 +148,13 @@ You can see that the current `mv_test` table has three materialized views: mv\_1 If the user no longer needs the materialized view, you can delete the materialized view by 'DROP' commen. -The specific syntax can be viewed through the following command: +You can view the specific syntax[SHOW CREATE MATERIALIZED VIEW](../sql-manual/sql-reference/Data-Definition-Statements/Drop/DROP-MATERIALIZED-VIEW.html) -``` -HELP DROP MATERIALIZED VIEW -``` +### View the materialized view that has been created + +Users can view the created materialized views by using commands + +You can view the specific syntax[SHOW CREATE MATERIALIZED VIEW](../sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.html) ## Best Practice 1 diff --git a/docs/zh-CN/advanced/materialized-view.md b/docs/zh-CN/advanced/materialized-view.md index 4064f4686bd69d..0cd8e2d41b5870 100644 --- a/docs/zh-CN/advanced/materialized-view.md +++ b/docs/zh-CN/advanced/materialized-view.md @@ -146,7 +146,11 @@ MySQL [test]> desc mv_test all; 具体的语法可查看[DROP MATERIALIZED VIEW](../sql-manual/sql-reference/Data-Definition-Statements/Drop/DROP-MATERIALIZED-VIEW.md) +### 查看已创建的物化视图 +用户可以通过命令查看已创建的物化视图的 + +具体的语法可查看[SHOW CREATE MATERIALIZED VIEW](../sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.html) ## 最佳实践1 From 5976b0ee5811dc16621880c289660a154a96d516 Mon Sep 17 00:00:00 2001 From: stalary Date: Sat, 7 May 2022 21:56:41 +0800 Subject: [PATCH 8/9] MOD: use md --- docs/en/advanced/materialized-view.md | 4 ++-- docs/zh-CN/advanced/materialized-view.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/en/advanced/materialized-view.md b/docs/en/advanced/materialized-view.md index c496e0ce099bf1..ea3d51ead8cc9b 100644 --- a/docs/en/advanced/materialized-view.md +++ b/docs/en/advanced/materialized-view.md @@ -148,13 +148,13 @@ You can see that the current `mv_test` table has three materialized views: mv\_1 If the user no longer needs the materialized view, you can delete the materialized view by 'DROP' commen. -You can view the specific syntax[SHOW CREATE MATERIALIZED VIEW](../sql-manual/sql-reference/Data-Definition-Statements/Drop/DROP-MATERIALIZED-VIEW.html) +You can view the specific syntax[SHOW CREATE MATERIALIZED VIEW](../sql-manual/sql-reference/Data-Definition-Statements/Drop/DROP-MATERIALIZED-VIEW.md) ### View the materialized view that has been created Users can view the created materialized views by using commands -You can view the specific syntax[SHOW CREATE MATERIALIZED VIEW](../sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.html) +You can view the specific syntax[SHOW CREATE MATERIALIZED VIEW](../sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.md) ## Best Practice 1 diff --git a/docs/zh-CN/advanced/materialized-view.md b/docs/zh-CN/advanced/materialized-view.md index 0cd8e2d41b5870..355b3c8383fdf0 100644 --- a/docs/zh-CN/advanced/materialized-view.md +++ b/docs/zh-CN/advanced/materialized-view.md @@ -150,7 +150,7 @@ MySQL [test]> desc mv_test all; 用户可以通过命令查看已创建的物化视图的 -具体的语法可查看[SHOW CREATE MATERIALIZED VIEW](../sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.html) +具体的语法可查看[SHOW CREATE MATERIALIZED VIEW](../sql-manual/sql-reference/Show-Statements/SHOW-CREATE-MATERIALIZED-VIEW.md) ## 最佳实践1 From 81251c69d83eb62dc7693ba6eec4ad44cc26dadb Mon Sep 17 00:00:00 2001 From: stalary Date: Sun, 8 May 2022 18:02:06 +0800 Subject: [PATCH 9/9] MOD: review --- .../org/apache/doris/qe/ShowExecutor.java | 26 ++++++++++--------- .../ShowCreateMaterializedViewStmtTest.java | 22 ++++++++++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java index 3ca3b871c3b8ee..2a1779c8d6847e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ShowExecutor.java @@ -2187,19 +2187,21 @@ private void handleShowCreateMaterializedView() throws AnalysisException { ShowCreateMaterializedViewStmt showStmt = (ShowCreateMaterializedViewStmt) stmt; Database db = Catalog.getCurrentCatalog().getDbOrAnalysisException(showStmt.getTableName().getDb()); Table table = db.getTableOrAnalysisException(showStmt.getTableName().getTbl()); - OlapTable baseTable = ((OlapTable) table); - Long indexIdByName = baseTable.getIndexIdByName(showStmt.getMvName()); - MaterializedIndexMeta meta = baseTable.getIndexMetaByIndexId(indexIdByName); - if (meta == null || meta.getDefineStmt() == null) { - resultSet = new ShowResultSet(showStmt.getMetaData(), resultRowSet); - return; + if (table instanceof OlapTable) { + OlapTable baseTable = ((OlapTable) table); + Long indexIdByName = baseTable.getIndexIdByName(showStmt.getMvName()); + if (indexIdByName != null) { + MaterializedIndexMeta meta = baseTable.getIndexMetaByIndexId(indexIdByName); + if (meta != null && meta.getDefineStmt() != null) { + String originStmt = meta.getDefineStmt().originStmt; + List data = new ArrayList<>(); + data.add(showStmt.getTableName().getTbl()); + data.add(showStmt.getMvName()); + data.add(originStmt); + resultRowSet.add(data); + } + } } - String originStmt = meta.getDefineStmt().originStmt; - List data = new ArrayList<>(); - data.add(showStmt.getTableName().getTbl()); - data.add(showStmt.getMvName()); - data.add(originStmt); - resultRowSet.add(data); resultSet = new ShowResultSet(showStmt.getMetaData(), resultRowSet); } diff --git a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java index f19b2619df7578..501b50b4184921 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/analysis/ShowCreateMaterializedViewStmtTest.java @@ -17,6 +17,8 @@ package org.apache.doris.analysis; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.ExceptionChecker; import org.apache.doris.qe.ConnectContext; import org.apache.doris.qe.ShowExecutor; import org.apache.doris.utframe.DorisAssert; @@ -72,4 +74,24 @@ public void testNormal() throws Exception { Assert.assertEquals(executor.execute().getResultRows().get(0).get(2), "CREATE MATERIALIZED VIEW test_mv as select k1 from test.table1;"); } + + @Test + public void testNoView() throws Exception { + String showMvSql = "SHOW CREATE MATERIALIZED VIEW test_mv_empty on test.table1;"; + ShowCreateMaterializedViewStmt showStmt = + (ShowCreateMaterializedViewStmt) UtFrameUtils.parseAndAnalyzeStmt(showMvSql, connectContext); + ShowExecutor executor = new ShowExecutor(connectContext, showStmt); + Assert.assertTrue(executor.execute().getResultRows().isEmpty()); + } + + @Test + public void testNoTable() throws Exception { + String showMvSql = "SHOW CREATE MATERIALIZED VIEW test_mv on test.table1_error;"; + ShowCreateMaterializedViewStmt showStmt = + (ShowCreateMaterializedViewStmt) UtFrameUtils.parseAndAnalyzeStmt(showMvSql, connectContext); + ShowExecutor executor = new ShowExecutor(connectContext, showStmt); + ExceptionChecker.expectThrowsWithMsg(AnalysisException.class, + "Unknown table 'table1_error' in default_cluster:test", executor::execute); + + } }