Skip to content

Commit

Permalink
Support catalog name in Show materialized views
Browse files Browse the repository at this point in the history
Signed-off-by: Amogh Margoor <[email protected]>
  • Loading branch information
amoghmargoor committed Feb 27, 2025
1 parent 48cc6fc commit ea1e8d7
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 20 deletions.
9 changes: 8 additions & 1 deletion fe/fe-core/src/main/java/com/starrocks/qe/ShowExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,14 @@ public ShowResultSet visitShowStatement(ShowStmt statement, ConnectContext conte
@Override
public ShowResultSet visitShowMaterializedViewStatement(ShowMaterializedViewsStmt statement, ConnectContext context) {
String dbName = statement.getDb();
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbName);
String catalogName = statement.getCatalogName();
Database db;
if (catalogName == null) {
db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbName);
} else {
db = GlobalStateMgr.getCurrentState().getMetadataMgr().getDb(catalogName, dbName);
}

MetaUtils.checkDbNullAndReport(db, dbName);

List<MaterializedView> materializedViews = Lists.newArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ public Void visitShowMaterializedViewStatement(ShowMaterializedViewsStmt node, C
String db = node.getDb();
db = getDatabaseName(db, context);
node.setDb(db);
String catalogName;
if (node.getCatalogName() != null) {
catalogName = node.getCatalogName();
} else {
catalogName = context.getCurrentCatalog();
}
if (!GlobalStateMgr.getCurrentState().getCatalogMgr().catalogExists(catalogName)) {
ErrorReport.reportSemanticException(ErrorCode.ERR_BAD_CATALOG_ERROR, catalogName);
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,27 @@ public class ShowMaterializedViewsStmt extends ShowStmt {

private String db;

private String catalogName;

private final String pattern;

private Expr where;

public ShowMaterializedViewsStmt(String db) {
this(db, null, null, NodePosition.ZERO);
public ShowMaterializedViewsStmt(String catalogName, String db) {
this(catalogName, db, null, null, NodePosition.ZERO);
}

public ShowMaterializedViewsStmt(String db, String pattern) {
this(db, pattern, null, NodePosition.ZERO);
public ShowMaterializedViewsStmt(String catalogName, String db, String pattern) {
this(catalogName, db, pattern, null, NodePosition.ZERO);
}

public ShowMaterializedViewsStmt(String db, Expr where) {
this(db, null, where, NodePosition.ZERO);
public ShowMaterializedViewsStmt(String catalogName, String db, Expr where) {
this(catalogName, db, null, where, NodePosition.ZERO);
}

public ShowMaterializedViewsStmt(String db, String pattern, Expr where, NodePosition pos) {
public ShowMaterializedViewsStmt(String catalogName, String db, String pattern, Expr where, NodePosition pos) {
super(pos);
this.catalogName = catalogName;
this.db = db;
this.pattern = pattern;
this.where = where;
Expand All @@ -115,6 +118,10 @@ public String getPattern() {
return pattern;
}

public String getCatalogName() {
return catalogName;
}

@Override
public QueryStatement toSelectStmt() throws AnalysisException {
if (where == null) {
Expand Down
16 changes: 12 additions & 4 deletions fe/fe-core/src/main/java/com/starrocks/sql/parser/AstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2082,17 +2082,25 @@ public ParseNode visitCreateMaterializedViewStatement(
public ParseNode visitShowMaterializedViewsStatement(
StarRocksParser.ShowMaterializedViewsStatementContext context) {
String database = null;
String catalog = null;
NodePosition pos = createPos(context);
if (context.qualifiedName() != null) {
database = getQualifiedName(context.qualifiedName()).toString();
QualifiedName qualifiedName = getQualifiedName(context.qualifiedName());
List<String> parts = qualifiedName.getParts();
if (parts.size() == 2) {
catalog = qualifiedName.getParts().get(0);
database = qualifiedName.getParts().get(1);
} else if (parts.size() == 1) {
database = qualifiedName.getParts().get(0);
}
}
if (context.pattern != null) {
StringLiteral stringLiteral = (StringLiteral) visit(context.pattern);
return new ShowMaterializedViewsStmt(database, stringLiteral.getValue(), null, pos);
return new ShowMaterializedViewsStmt(catalog, database, stringLiteral.getValue(), null, pos);
} else if (context.expression() != null) {
return new ShowMaterializedViewsStmt(database, null, (Expr) visit(context.expression()), pos);
return new ShowMaterializedViewsStmt(catalog, database, null, (Expr) visit(context.expression()), pos);
} else {
return new ShowMaterializedViewsStmt(database, null, null, pos);
return new ShowMaterializedViewsStmt(catalog, database, null, null, pos);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@
import com.starrocks.catalog.Table;
import com.starrocks.catalog.system.information.MaterializedViewsSystemTable;
import com.starrocks.qe.ConnectContext;
import com.starrocks.qe.DDLStmtExecutor;
import com.starrocks.qe.ShowExecutor;
import com.starrocks.qe.ShowResultSet;
import com.starrocks.server.GlobalStateMgr;
import com.starrocks.sql.analyzer.AnalyzeTestUtil;
import com.starrocks.sql.analyzer.AstToStringBuilder;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.CreateCatalogStmt;
import com.starrocks.sql.ast.DropCatalogStmt;
import com.starrocks.sql.ast.ShowMaterializedViewsStmt;
import com.starrocks.sql.ast.ShowStmt;
import com.starrocks.sql.ast.StatementBase;
import com.starrocks.utframe.StarRocksAssert;
import com.starrocks.utframe.UtFrameUtils;
import org.junit.Assert;
Expand All @@ -67,18 +76,18 @@ public static void setUp() throws Exception {
public void testNormal() throws Exception {
ctx.setDatabase("testDb");

ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt("");
ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt(null, "");

com.starrocks.sql.analyzer.Analyzer.analyze(stmt, ctx);
Assert.assertEquals("testDb", stmt.getDb());
checkShowMaterializedViewsStmt(stmt);

stmt = new ShowMaterializedViewsStmt("abc", (String) null);
stmt = new ShowMaterializedViewsStmt(null, "abc", (String) null);
com.starrocks.sql.analyzer.Analyzer.analyze(stmt, ctx);
Assert.assertEquals("abc", stmt.getDb());
checkShowMaterializedViewsStmt(stmt);

stmt = new ShowMaterializedViewsStmt("abc", "bcd");
stmt = new ShowMaterializedViewsStmt(null, "abc", "bcd");
com.starrocks.sql.analyzer.Analyzer.analyze(stmt, ctx);
Assert.assertEquals("bcd", stmt.getPattern());
Assert.assertEquals("abc", stmt.getDb());
Expand Down Expand Up @@ -149,7 +158,30 @@ private void checkShowMaterializedViewsStmt(ShowMaterializedViewsStmt stmt) {
@Test(expected = SemanticException.class)
public void testNoDb() throws Exception {
ctx = UtFrameUtils.createDefaultCtx();
ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt("");
ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt(null, "");
com.starrocks.sql.analyzer.Analyzer.analyze(stmt, ctx);
Assert.fail("No exception throws");
}

@Test
public void testCatalogName() throws Exception {
ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt("default_catalog", "testDb");

com.starrocks.sql.analyzer.Analyzer.analyze(stmt, ctx);
Assert.assertEquals("testDb", stmt.getDb());
Assert.assertEquals("default_catalog", stmt.getCatalogName());
checkShowMaterializedViewsStmt(stmt);
stmt = (ShowMaterializedViewsStmt) UtFrameUtils.parseStmtWithNewParser(
"SHOW MATERIALIZED VIEWS FROM default_catalog.testDb;", ctx);

Assert.assertEquals("testDb", stmt.getDb());
Assert.assertEquals("default_catalog", stmt.getCatalogName());
}

@Test(expected = SemanticException.class)
public void testUnknownCatalog() throws Exception {
ctx = UtFrameUtils.createDefaultCtx();
ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt("unknown_catalog", "testDb");
com.starrocks.sql.analyzer.Analyzer.analyze(stmt, ctx);
Assert.fail("No exception throws");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,15 +976,15 @@ public void testShowMaterializedView() throws AnalysisException, DdlException {
ctx.setCurrentUserIdentity(UserIdentity.ROOT);
ctx.setCurrentRoleIds(Sets.newHashSet(PrivilegeBuiltinConstants.ROOT_ROLE_ID));

ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt("testDb", (String) null);
ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt("default_catalog", "testDb", (String) null);

ShowResultSet resultSet = ShowExecutor.execute(stmt, ctx);
verifyShowMaterializedViewResult(resultSet);
}

@Test
public void testShowMaterializedViewFromUnknownDatabase() throws DdlException, AnalysisException {
ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt("emptyDb", (String) null);
ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt("default_catalog", "emptyDb", (String) null);

expectedEx.expect(SemanticException.class);
expectedEx.expectMessage("Unknown database 'emptyDb'");
Expand All @@ -996,12 +996,12 @@ public void testShowMaterializedViewPattern() throws AnalysisException, DdlExcep
ctx.setCurrentUserIdentity(UserIdentity.ROOT);
ctx.setCurrentRoleIds(Sets.newHashSet(PrivilegeBuiltinConstants.ROOT_ROLE_ID));

ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt("testDb", "bcd%");
ShowMaterializedViewsStmt stmt = new ShowMaterializedViewsStmt("default_catalog", "testDb", "bcd%");

ShowResultSet resultSet = ShowExecutor.execute(stmt, ctx);
Assert.assertFalse(resultSet.next());

stmt = new ShowMaterializedViewsStmt("testDb", "%test%");
stmt = new ShowMaterializedViewsStmt("default_catalog", "testDb", "%test%");

resultSet = ShowExecutor.execute(stmt, ctx);
verifyShowMaterializedViewResult(resultSet);
Expand Down

0 comments on commit ea1e8d7

Please sign in to comment.