From 7068326d3fb5df5b1225a1452083121c5739480f Mon Sep 17 00:00:00 2001 From: stalary Date: Sun, 8 May 2022 18:02:06 +0800 Subject: [PATCH] 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 3ca3b871c3b8eeb..2a1779c8d6847ea 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 f19b2619df75789..501b50b41849212 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); + + } }