diff --git a/fe/fe-core/src/main/java/com/starrocks/qe/ShowExecutor.java b/fe/fe-core/src/main/java/com/starrocks/qe/ShowExecutor.java index c66e5380772a2f..634d58240d817f 100644 --- a/fe/fe-core/src/main/java/com/starrocks/qe/ShowExecutor.java +++ b/fe/fe-core/src/main/java/com/starrocks/qe/ShowExecutor.java @@ -687,7 +687,7 @@ public ShowResultSet visitDescTableStmt(DescribeStmt statement, ConnectContext c @Override public ShowResultSet visitShowCreateDbStatement(ShowCreateDbStmt statement, ConnectContext context) { - String catalogName = statement.getCatalogName(); + String catalogName = context.getCurrentCatalog(); String dbName = statement.getDb(); List> rows = Lists.newArrayList(); diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AuthorizerStmtVisitor.java b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AuthorizerStmtVisitor.java index 846abc44cab4f2..7d2498ecbaf07b 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AuthorizerStmtVisitor.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/AuthorizerStmtVisitor.java @@ -14,6 +14,7 @@ package com.starrocks.sql.analyzer; +import com.google.common.base.Strings; import com.google.common.collect.Lists; import com.starrocks.authorization.AccessDeniedException; import com.starrocks.authorization.AuthorizationMgr; @@ -251,6 +252,8 @@ import java.util.Set; import java.util.regex.Pattern; +import static com.starrocks.sql.common.ErrorMsgProxy.PARSER_ERROR_MSG; + public class AuthorizerStmtVisitor implements AstVisitorExtendInterface { // For show tablet detail command, if user has any privilege on the corresponding table, user can run it // TODO(yiming): match "/dbs", not only show tablet detail cmd, need to change privilege check for other proc node @@ -541,12 +544,16 @@ public Void visitUseDbStatement(UseDbStmt statement, ConnectContext context) { @Override public Void visitShowCreateDbStatement(ShowCreateDbStmt statement, ConnectContext context) { + String catalogName = context.getCurrentCatalog(); + if (Strings.isNullOrEmpty(catalogName)) { + throw new SemanticException(PARSER_ERROR_MSG.noCatalogSelected()); + } + try { - Authorizer.checkAnyActionOnDb(context, - statement.getCatalogName(), statement.getDb()); + Authorizer.checkAnyActionOnDb(context, catalogName, statement.getDb()); } catch (AccessDeniedException e) { AccessDeniedException.reportAccessDenied( - statement.getCatalogName(), + catalogName, context.getCurrentUserIdentity(), context.getCurrentRoleIds(), PrivilegeType.ANY.name(), ObjectType.DATABASE.name(), statement.getDb()); } @@ -555,15 +562,18 @@ public Void visitShowCreateDbStatement(ShowCreateDbStmt statement, ConnectContex @Override public Void visitRecoverDbStatement(RecoverDbStmt statement, ConnectContext context) { - // Need to check the `CREATE_DATABASE` action on corresponding catalog + String catalogName = context.getCurrentCatalog(); + if (Strings.isNullOrEmpty(catalogName)) { + throw new SemanticException(PARSER_ERROR_MSG.noCatalogSelected()); + } + try { - Authorizer.checkCatalogAction(context, - statement.getCatalogName(), PrivilegeType.CREATE_DATABASE); + Authorizer.checkCatalogAction(context, catalogName, PrivilegeType.CREATE_DATABASE); } catch (AccessDeniedException e) { AccessDeniedException.reportAccessDenied( - statement.getCatalogName(), + catalogName, context.getCurrentUserIdentity(), context.getCurrentRoleIds(), - PrivilegeType.CREATE_DATABASE.name(), ObjectType.CATALOG.name(), statement.getCatalogName()); + PrivilegeType.CREATE_DATABASE.name(), ObjectType.CATALOG.name(), catalogName); } return null; } diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/BasicDbStmtAnalyzer.java b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/BasicDbStmtAnalyzer.java index 4be736dd28ac03..548a011390ba5b 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/BasicDbStmtAnalyzer.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/BasicDbStmtAnalyzer.java @@ -15,7 +15,6 @@ package com.starrocks.sql.analyzer; import com.google.common.base.Strings; -import com.starrocks.common.util.Util; import com.starrocks.qe.ConnectContext; import com.starrocks.sql.ast.AstVisitorExtendInterface; import com.starrocks.sql.ast.RecoverDbStmt; @@ -42,32 +41,26 @@ public void analyze(StatementBase statement, ConnectContext session) { @Override public Void visitUseDbStatement(UseDbStmt statement, ConnectContext context) { - String catalogName = Util.normalizeName(getCatalogNameIfNotSet(statement.getCatalogName(), context)); - statement.setCatalogName(catalogName); + if (Strings.isNullOrEmpty(context.getCurrentCatalog())) { + throw new SemanticException(PARSER_ERROR_MSG.noCatalogSelected()); + } return null; } @Override public Void visitRecoverDbStatement(RecoverDbStmt statement, ConnectContext context) { - statement.setCatalogName(getCatalogNameIfNotSet(statement.getCatalogName(), context)); + if (Strings.isNullOrEmpty(context.getCurrentCatalog())) { + throw new SemanticException(PARSER_ERROR_MSG.noCatalogSelected()); + } return null; } @Override public Void visitShowCreateDbStatement(ShowCreateDbStmt statement, ConnectContext context) { - statement.setCatalogName(getCatalogNameIfNotSet(statement.getCatalogName(), context)); - return null; - } - - private String getCatalogNameIfNotSet(String currentCatalogName, ConnectContext context) { - String result = currentCatalogName; - if (currentCatalogName == null) { - if (Strings.isNullOrEmpty(context.getCurrentCatalog())) { - throw new SemanticException(PARSER_ERROR_MSG.noCatalogSelected()); - } - result = context.getCurrentCatalog(); + if (Strings.isNullOrEmpty(context.getCurrentCatalog())) { + throw new SemanticException(PARSER_ERROR_MSG.noCatalogSelected()); } - return result; + return null; } } } diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/ShowStmtAnalyzer.java b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/ShowStmtAnalyzer.java index aa3b9170168f3f..d3cbfc5fff7f22 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/ShowStmtAnalyzer.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/analyzer/ShowStmtAnalyzer.java @@ -53,7 +53,6 @@ import com.starrocks.sql.ast.ShowAnalyzeStatusStmt; import com.starrocks.sql.ast.ShowBasicStatsMetaStmt; import com.starrocks.sql.ast.ShowColumnStmt; -import com.starrocks.sql.ast.ShowCreateDbStmt; import com.starrocks.sql.ast.ShowCreateExternalCatalogStmt; import com.starrocks.sql.ast.ShowCreateRoutineLoadStmt; import com.starrocks.sql.ast.ShowCreateTableStmt; @@ -320,14 +319,6 @@ String getDatabaseName(String db, ConnectContext session) { return db; } - @Override - public Void visitShowCreateDbStatement(ShowCreateDbStmt node, ConnectContext context) { - String dbName = node.getDb(); - dbName = getDatabaseName(dbName, context); - node.setDb(dbName); - return null; - } - @Override public Void visitShowDataStatement(ShowDataStmt node, ConnectContext context) { String dbName = node.getDbName(); diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/ast/AstVisitorExtendInterface.java b/fe/fe-core/src/main/java/com/starrocks/sql/ast/AstVisitorExtendInterface.java index 84f96be695ce5d..b2d0e62b1e38e8 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/ast/AstVisitorExtendInterface.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/ast/AstVisitorExtendInterface.java @@ -108,17 +108,11 @@ default R visitAlterDatabaseQuotaStatement(AlterDatabaseQuotaStmt statement, C c return visitDDLStatement(statement, context); } - default R visitShowCreateDbStatement(ShowCreateDbStmt statement, C context) { - return visitShowStatement(statement, context); - } default R visitAlterDatabaseRenameStatement(AlterDatabaseRenameStatement statement, C context) { return visitDDLStatement(statement, context); } - default R visitRecoverDbStatement(RecoverDbStmt statement, C context) { - return visitDDLStatement(statement, context); - } default R visitShowDataDistributionStatement(ShowDataDistributionStmt statement, C context) { return visitShowStatement(statement, context); @@ -254,20 +248,6 @@ default R visitDropMaterializedViewStatement(DropMaterializedViewStmt statement, return visitDDLStatement(statement, context); } - // ---------------------------------------- Catalog Statement ------------------------------------------------------ - - default R visitCreateCatalogStatement(CreateCatalogStmt statement, C context) { - return visitDDLStatement(statement, context); - } - - default R visitShowCreateExternalCatalogStatement(ShowCreateExternalCatalogStmt statement, C context) { - return visitShowStatement(statement, context); - } - - default R visitAlterCatalogStatement(AlterCatalogStmt statement, C context) { - return visitDDLStatement(statement, context); - } - // ------------------------------------------- DML Statement ------------------------------------------------------- default R visitInsertStatement(InsertStmt statement, C context) { diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/parser/AstBuilder.java b/fe/fe-core/src/main/java/com/starrocks/sql/parser/AstBuilder.java index f992854a44ccea..3c98c88a061970 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/parser/AstBuilder.java +++ b/fe/fe-core/src/main/java/com/starrocks/sql/parser/AstBuilder.java @@ -773,7 +773,7 @@ public ParseNode visitDropDbStatement(com.starrocks.sql.parser.StarRocksParser.D @Override public ParseNode visitShowCreateDbStatement(com.starrocks.sql.parser.StarRocksParser.ShowCreateDbStatementContext context) { String dbName = ((Identifier) visit(context.identifier())).getValue(); - return new ShowCreateDbStmt(dbName, createPos(context)); + return new ShowCreateDbStmt(normalizeName(dbName), createPos(context)); } @Override @@ -787,7 +787,7 @@ public ParseNode visitAlterDatabaseRenameStatement( @Override public ParseNode visitRecoverDbStmt(com.starrocks.sql.parser.StarRocksParser.RecoverDbStmtContext context) { String dbName = ((Identifier) visit(context.identifier())).getValue(); - return new RecoverDbStmt(dbName, createPos(context)); + return new RecoverDbStmt(normalizeName(dbName), createPos(context)); } @Override @@ -2361,7 +2361,7 @@ public ParseNode visitCreateExternalCatalogStatement( com.starrocks.sql.parser.StarRocksParser.CreateExternalCatalogStatementContext context) { boolean ifNotExists = context.IF() != null; Identifier identifier = (Identifier) visit(context.identifierOrString()); - String catalogName = identifier.getValue(); + String catalogName = normalizeName(identifier.getValue()); String comment = null; if (context.comment() != null) { comment = ((StringLiteral) visit(context.comment())).getStringValue(); @@ -2390,7 +2390,7 @@ public ParseNode visitDropExternalCatalogStatement( public ParseNode visitShowCreateExternalCatalogStatement( com.starrocks.sql.parser.StarRocksParser.ShowCreateExternalCatalogStatementContext context) { Identifier identifier = (Identifier) visit(context.catalogName); - String catalogName = identifier.getValue(); + String catalogName = normalizeName(identifier.getValue()); return new ShowCreateExternalCatalogStmt(catalogName, createPos(context)); } @@ -2406,7 +2406,7 @@ public ParseNode visitShowCatalogsStatement(com.starrocks.sql.parser.StarRocksPa @Override public ParseNode visitAlterCatalogStatement(com.starrocks.sql.parser.StarRocksParser.AlterCatalogStatementContext context) { - String catalogName = ((Identifier) visit(context.catalogName)).getValue(); + String catalogName = normalizeName(((Identifier) visit(context.catalogName)).getValue()); AlterClause alterClause = (AlterClause) visit(context.modifyPropertiesClause()); return new AlterCatalogStmt(catalogName, alterClause, createPos(context)); } diff --git a/fe/fe-core/src/test/java/com/starrocks/analysis/CatalogStmtTest.java b/fe/fe-core/src/test/java/com/starrocks/analysis/CatalogStmtTest.java index 998a653559b103..5f1ddc8c2fcd1a 100644 --- a/fe/fe-core/src/test/java/com/starrocks/analysis/CatalogStmtTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/analysis/CatalogStmtTest.java @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. - package com.starrocks.analysis; import com.starrocks.common.DdlException; @@ -35,6 +34,7 @@ public class CatalogStmtTest { private static StarRocksAssert starRocksAssert; + @BeforeAll public static void beforeClass() throws Exception { UtFrameUtils.createMinStarRocksCluster(); @@ -48,7 +48,8 @@ public static void beforeClass() throws Exception { @Test public void testCreateCatalogParserAndAnalyzer() { - String sql_1 = "CREATE EXTERNAL CATALOG catalog_1 PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; + String sql_1 = + "CREATE EXTERNAL CATALOG catalog_1 PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; StatementBase stmt = AnalyzeTestUtil.analyzeSuccess(sql_1); Assertions.assertTrue(stmt instanceof CreateCatalogStmt); String sql_2 = "CREATE EXTERNAL CATALOG catalog_2"; @@ -57,14 +58,19 @@ public void testCreateCatalogParserAndAnalyzer() { AnalyzeTestUtil.analyzeFail(sql_3); String sql_4 = "CREATE EXTERNAL CATALOG catalog_4 properties(\"aaa\"=\"bbb\")"; AnalyzeTestUtil.analyzeFail(sql_4); - String sql_5 = "CREATE EXTERNAL CATALOG default PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; + String sql_5 = + "CREATE EXTERNAL CATALOG default PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; AnalyzeTestUtil.analyzeFail(sql_5); String sql_6 = "CREATE EXTERNAL CATALOG catalog_5 properties(\"type\"=\"hive\")"; StatementBase stmt2 = AnalyzeTestUtil.analyzeSuccess(sql_6); - Assertions.assertEquals("CREATE EXTERNAL CATALOG 'catalog_5' PROPERTIES(\"type\" = \"hive\")", stmt2.toSql()); - String sql_7 = "CREATE EXTERNAL CATALOG IF NOT EXISTS catalog_6 PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; + Assertions.assertEquals("CREATE EXTERNAL CATALOG 'catalog_5' PROPERTIES(\"type\" = \"hive\")", + AstToStringBuilder.toString(stmt2)); + String sql_7 = + "CREATE EXTERNAL CATALOG IF NOT EXISTS catalog_6 PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; StatementBase stmt3 = AnalyzeTestUtil.analyzeSuccess(sql_7); - Assertions.assertEquals("CREATE EXTERNAL CATALOG IF NOT EXISTS 'catalog_6' PROPERTIES(\"hive.metastore.uris\" = \"thrift://127.0.0.1:9083\", \"type\" = \"hive\")", stmt3.toSql()); + Assertions.assertEquals( + "CREATE EXTERNAL CATALOG 'catalog_6' PROPERTIES(\"hive.metastore.uris\" = \"thrift://127.0.0.1:9083\", \"type\" = \"hive\")", + AstToStringBuilder.toString(stmt3)); Assertions.assertTrue(stmt3 instanceof CreateCatalogStmt); } @@ -96,7 +102,8 @@ public void testDropCatalogParserAndAnalyzer() { @Test public void testCreateCatalog() throws Exception { - String sql = "CREATE EXTERNAL CATALOG hive_catalog PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; + String sql = + "CREATE EXTERNAL CATALOG hive_catalog PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; StatementBase stmt = AnalyzeTestUtil.analyzeSuccess(sql); Assertions.assertTrue(stmt instanceof CreateCatalogStmt); ConnectContext connectCtx = new ConnectContext(); @@ -122,7 +129,8 @@ public void testCreateCatalog() throws Exception { @Test public void testCreateCatalogWithAccessControl() throws Exception { - String sql = "CREATE EXTERNAL CATALOG hive_catalog PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\", \"catalog.access.control\"=\"allowall\")"; + String sql = + "CREATE EXTERNAL CATALOG hive_catalog PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\", \"catalog.access.control\"=\"allowall\")"; StatementBase stmt = AnalyzeTestUtil.analyzeSuccess(sql); Assertions.assertTrue(stmt instanceof CreateCatalogStmt); ConnectContext connectCtx = new ConnectContext(); @@ -140,8 +148,10 @@ public void testCreateCatalogWithAccessControl() throws Exception { @Test public void testCreateExistedCatalog() throws Exception { - String sql = "CREATE EXTERNAL CATALOG hive_catalog PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; - String sql_2 = "CREATE EXTERNAL CATALOG IF NOT EXISTS hive_catalog PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; + String sql = + "CREATE EXTERNAL CATALOG hive_catalog PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; + String sql_2 = + "CREATE EXTERNAL CATALOG IF NOT EXISTS hive_catalog PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; StatementBase stmt = AnalyzeTestUtil.analyzeSuccess(sql); Assertions.assertTrue(stmt instanceof CreateCatalogStmt); ConnectContext connectCtx = new ConnectContext(); @@ -168,10 +178,10 @@ public void testCreateExistedCatalog() throws Exception { @Test public void testDropCatalog() throws Exception { // test drop ddl DROP CATALOG catalog_name - String createSql = "CREATE EXTERNAL CATALOG hive_catalog PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; + String createSql = + "CREATE EXTERNAL CATALOG hive_catalog PROPERTIES(\"type\"=\"hive\", \"hive.metastore.uris\"=\"thrift://127.0.0.1:9083\")"; String dropSql = "DROP CATALOG hive_catalog"; - CatalogMgr catalogMgr = GlobalStateMgr.getCurrentState().getCatalogMgr(); ConnectorMgr connectorMgr = GlobalStateMgr.getCurrentState().getConnectorMgr(); MetadataMgr metadataMgr = GlobalStateMgr.getCurrentState().getMetadataMgr(); diff --git a/fe/fe-core/src/test/java/com/starrocks/qe/RedirectStatusTest.java b/fe/fe-core/src/test/java/com/starrocks/qe/RedirectStatusTest.java index 77f16432c50e42..7adf38138796bd 100644 --- a/fe/fe-core/src/test/java/com/starrocks/qe/RedirectStatusTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/qe/RedirectStatusTest.java @@ -1123,7 +1123,8 @@ public void testCreateAnalyzeJobStmt() { @Test public void testCreateCatalogStmt() { - CreateCatalogStmt stmt = new CreateCatalogStmt("test_catalog", "hive", java.util.Collections.emptyMap(), false); + CreateCatalogStmt stmt = + new CreateCatalogStmt("test_catalog", "hive", java.util.Collections.emptyMap(), false, NodePosition.ZERO); Assertions.assertEquals(RedirectStatus.FORWARD_WITH_SYNC, RedirectStatus.getRedirectStatus(stmt)); } diff --git a/fe/fe-core/src/test/java/com/starrocks/qe/UseDbTest.java b/fe/fe-core/src/test/java/com/starrocks/qe/UseDbTest.java new file mode 100644 index 00000000000000..bcbf56f276b155 --- /dev/null +++ b/fe/fe-core/src/test/java/com/starrocks/qe/UseDbTest.java @@ -0,0 +1,182 @@ +// Copyright 2021-present StarRocks, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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 com.starrocks.qe; + +import com.starrocks.authorization.MockedLocalMetaStore; +import com.starrocks.authorization.RBACMockedMetadataMgr; +import com.starrocks.catalog.Database; +import com.starrocks.catalog.UserIdentity; +import com.starrocks.common.util.UUIDUtil; +import com.starrocks.server.CatalogMgr; +import com.starrocks.server.GlobalStateMgr; +import com.starrocks.server.MetadataMgr; +import com.starrocks.sql.analyzer.Analyzer; +import com.starrocks.sql.ast.UseDbStmt; +import com.starrocks.sql.parser.SqlParser; +import mockit.Mock; +import mockit.MockUp; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class UseDbTest { + @Test + public void testUseDbWithoutCurrentCatalog() throws Exception { + GlobalStateMgr globalStateMgr = GlobalStateMgr.getCurrentState(); + MockedLocalMetaStore localMetastore = new MockedLocalMetaStore(globalStateMgr, globalStateMgr.getRecycleBin(), null); + localMetastore.init(); + globalStateMgr.setLocalMetastore(localMetastore); + + RBACMockedMetadataMgr metadataMgr = + new RBACMockedMetadataMgr(localMetastore, globalStateMgr.getConnectorMgr()); + globalStateMgr.setMetadataMgr(metadataMgr); + + ConnectContext context = new ConnectContext(); + context.setCurrentUserIdentity(UserIdentity.ROOT); + context.setCurrentRoleIds(UserIdentity.ROOT); + context.setQueryId(UUIDUtil.genUUID()); + + String sql = "use db1;"; + UseDbStmt useDbStmt = (UseDbStmt) SqlParser.parseSingleStatement(sql, context.getSessionVariable().getSqlMode()); + Analyzer.analyze(useDbStmt, context); + + StmtExecutor stmtExecutor = new StmtExecutor(context, useDbStmt); + stmtExecutor.execute(); + + Assertions.assertEquals("default_catalog", context.getCurrentCatalog()); + Assertions.assertEquals("db1", context.getDatabase()); + } + + @Test + public void testUseDbWithCatalog() throws Exception { + new MockUp() { + @Mock + public boolean catalogExists(String catalogName) { + if (catalogName.equals("catalog1")) { + return true; + } else { + return false; + } + } + }; + + GlobalStateMgr globalStateMgr = GlobalStateMgr.getCurrentState(); + MockedLocalMetaStore localMetastore = new MockedLocalMetaStore(globalStateMgr, globalStateMgr.getRecycleBin(), null); + localMetastore.init(); + globalStateMgr.setLocalMetastore(localMetastore); + + RBACMockedMetadataMgr metadataMgr = + new RBACMockedMetadataMgr(localMetastore, globalStateMgr.getConnectorMgr()); + globalStateMgr.setMetadataMgr(metadataMgr); + + ConnectContext context = new ConnectContext(); + context.setCurrentUserIdentity(UserIdentity.ROOT); + context.setCurrentRoleIds(UserIdentity.ROOT); + context.setQueryId(UUIDUtil.genUUID()); + context.setCurrentCatalog("default_catalog"); + + String sql = "use catalog1.db1;"; + UseDbStmt useDbStmt = (UseDbStmt) SqlParser.parseSingleStatement(sql, context.getSessionVariable().getSqlMode()); + Analyzer.analyze(useDbStmt, context); + + StmtExecutor stmtExecutor = new StmtExecutor(context, useDbStmt); + stmtExecutor.execute(); + + Assertions.assertEquals("catalog1", context.getCurrentCatalog()); + Assertions.assertEquals("db1", context.getDatabase()); + } + + /** + * Test case: Use database with non-existent catalog + * Test point: Verify that using a non-existent catalog throws appropriate error + */ + @Test + public void testUseDbWithNonExistentCatalog() throws Exception { + new MockUp() { + @Mock + public boolean catalogExists(String catalogName) { + return false; // All catalogs are non-existent + } + }; + + GlobalStateMgr globalStateMgr = GlobalStateMgr.getCurrentState(); + MockedLocalMetaStore localMetastore = new MockedLocalMetaStore(globalStateMgr, globalStateMgr.getRecycleBin(), null); + localMetastore.init(); + globalStateMgr.setLocalMetastore(localMetastore); + + RBACMockedMetadataMgr metadataMgr = + new RBACMockedMetadataMgr(localMetastore, globalStateMgr.getConnectorMgr()); + globalStateMgr.setMetadataMgr(metadataMgr); + + ConnectContext context = new ConnectContext(); + context.setCurrentUserIdentity(UserIdentity.ROOT); + context.setCurrentRoleIds(UserIdentity.ROOT); + context.setQueryId(UUIDUtil.genUUID()); + context.setCurrentCatalog("default_catalog"); + + String sql = "use nonexistent_catalog.db1;"; + UseDbStmt useDbStmt = (UseDbStmt) SqlParser.parseSingleStatement(sql, context.getSessionVariable().getSqlMode()); + Analyzer.analyze(useDbStmt, context); + + StmtExecutor stmtExecutor = new StmtExecutor(context, useDbStmt); + stmtExecutor.execute(); + + // Should have error state due to non-existent catalog + Assertions.assertTrue(context.getState().isError()); + Assertions.assertTrue(context.getState().getErrorMessage().contains("Unknown catalog")); + } + + /** + * Test case: Use database statement execution state + * Test point: Verify that execution state is set correctly after successful execution + */ + @Test + public void testUseDbExecutionState() throws Exception { + new MockUp() { + @Mock + public Database getDb(ConnectContext context, String catalogName, String dbName) { + if (dbName.equals("db1")) { + return new Database(1, "db1"); + } + return null; + } + }; + + GlobalStateMgr globalStateMgr = GlobalStateMgr.getCurrentState(); + MockedLocalMetaStore localMetastore = new MockedLocalMetaStore(globalStateMgr, globalStateMgr.getRecycleBin(), null); + localMetastore.init(); + globalStateMgr.setLocalMetastore(localMetastore); + + RBACMockedMetadataMgr metadataMgr = + new RBACMockedMetadataMgr(localMetastore, globalStateMgr.getConnectorMgr()); + globalStateMgr.setMetadataMgr(metadataMgr); + + ConnectContext context = new ConnectContext(); + context.setCurrentUserIdentity(UserIdentity.ROOT); + context.setCurrentRoleIds(UserIdentity.ROOT); + context.setQueryId(UUIDUtil.genUUID()); + context.setCurrentCatalog("default_catalog"); + + String sql = "use db1;"; + UseDbStmt useDbStmt = (UseDbStmt) SqlParser.parseSingleStatement(sql, context.getSessionVariable().getSqlMode()); + Analyzer.analyze(useDbStmt, context); + + StmtExecutor stmtExecutor = new StmtExecutor(context, useDbStmt); + stmtExecutor.execute(); + + // Execution state should be OK after successful execution + Assertions.assertEquals(QueryState.MysqlStateType.OK, context.getState().getStateType()); + Assertions.assertFalse(context.getState().isError()); + } +} diff --git a/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/AuthorizerStmtVisitorTest.java b/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/AuthorizerStmtVisitorTest.java new file mode 100644 index 00000000000000..056a7954a4bb21 --- /dev/null +++ b/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/AuthorizerStmtVisitorTest.java @@ -0,0 +1,135 @@ +// Copyright 2021-present StarRocks, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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 com.starrocks.sql.analyzer; + +import com.starrocks.common.Config; +import com.starrocks.common.FeConstants; +import com.starrocks.qe.ConnectContext; +import com.starrocks.sql.ast.RecoverDbStmt; +import com.starrocks.sql.ast.ShowCreateDbStmt; +import com.starrocks.sql.parser.SqlParser; +import com.starrocks.utframe.UtFrameUtils; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class AuthorizerStmtVisitorTest { + + private static ConnectContext connectContext; + + @BeforeAll + public static void beforeClass() throws Exception { + FeConstants.runningUnitTest = true; + Config.dynamic_partition_enable = false; + UtFrameUtils.createMinStarRocksCluster(); + connectContext = UtFrameUtils.createDefaultCtx(); + } + + @AfterAll + public static void afterClass() throws Exception { + // Cleanup is handled automatically by the test framework + } + + @Test + public void testShowCreateDbStatementWithNullCatalog() throws Exception { + // Test case to cover line 549 in AuthorizerStmtVisitor.java + // This should trigger the noCatalogSelected exception when current catalog is null + ShowCreateDbStmt stmt = + (ShowCreateDbStmt) SqlParser.parse("SHOW CREATE DATABASE test_db", connectContext.getSessionVariable()).get(0); + + // Create a new context with null catalog to trigger the exception + ConnectContext testContext = new ConnectContext(); + testContext.setCurrentUserIdentity(connectContext.getCurrentUserIdentity()); + testContext.setCurrentRoleIds(connectContext.getCurrentRoleIds()); + // Set catalog to null to trigger the exception + testContext.setCurrentCatalog(null); + + AuthorizerStmtVisitor visitor = new AuthorizerStmtVisitor(); + + try { + visitor.visitShowCreateDbStatement(stmt, testContext); + Assertions.fail("Expected SemanticException to be thrown"); + } catch (Exception e) { + Assertions.assertTrue(e.getMessage().contains("No catalog selected")); + } + } + + @Test + public void testRecoverDbStatementWithNullCatalog() throws Exception { + // Test case to cover line 567 in AuthorizerStmtVisitor.java + // This should trigger the noCatalogSelected exception when current catalog is null + RecoverDbStmt stmt = + (RecoverDbStmt) SqlParser.parse("RECOVER DATABASE test_db", connectContext.getSessionVariable()).get(0); + + // Create a new context with null catalog to trigger the exception + ConnectContext testContext = new ConnectContext(); + testContext.setCurrentUserIdentity(connectContext.getCurrentUserIdentity()); + testContext.setCurrentRoleIds(connectContext.getCurrentRoleIds()); + // Set catalog to null to trigger the exception + testContext.setCurrentCatalog(null); + + AuthorizerStmtVisitor visitor = new AuthorizerStmtVisitor(); + + try { + visitor.visitRecoverDbStatement(stmt, testContext); + Assertions.fail("Expected SemanticException to be thrown"); + } catch (Exception e) { + Assertions.assertTrue(e.getMessage().contains("No catalog selected")); + } + } + + @Test + public void testShowCreateDbStatementWithValidCatalog() throws Exception { + // Test case to ensure normal flow works when catalog is set + ShowCreateDbStmt stmt = + (ShowCreateDbStmt) SqlParser.parse("SHOW CREATE DATABASE test_db", connectContext.getSessionVariable()).get(0); + + // Use the context with valid catalog + connectContext.setCurrentCatalog("default_catalog"); + + AuthorizerStmtVisitor visitor = new AuthorizerStmtVisitor(); + + try { + visitor.visitShowCreateDbStatement(stmt, connectContext); + // Should not throw noCatalogSelected exception (may throw other exceptions due to missing database) + } catch (Exception e) { + // Should not be a noCatalogSelected exception + Assertions.assertFalse(e.getMessage().contains("No catalog selected"), + "Should not throw noCatalogSelected exception when catalog is set: " + e.getMessage()); + } + } + + @Test + public void testRecoverDbStatementWithValidCatalog() throws Exception { + // Test case to ensure normal flow works when catalog is set + RecoverDbStmt stmt = + (RecoverDbStmt) SqlParser.parse("RECOVER DATABASE test_db", connectContext.getSessionVariable()).get(0); + + // Use the context with valid catalog + connectContext.setCurrentCatalog("default_catalog"); + + AuthorizerStmtVisitor visitor = new AuthorizerStmtVisitor(); + + try { + visitor.visitRecoverDbStatement(stmt, connectContext); + // Should not throw noCatalogSelected exception (may throw other exceptions due to missing database) + } catch (Exception e) { + // Should not be a noCatalogSelected exception + Assertions.assertFalse(e.getMessage().contains("No catalog selected"), + "Should not throw noCatalogSelected exception when catalog is set: " + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/BasicDbStmtAnalyzerTest.java b/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/BasicDbStmtAnalyzerTest.java new file mode 100644 index 00000000000000..e81e86b744ead8 --- /dev/null +++ b/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/BasicDbStmtAnalyzerTest.java @@ -0,0 +1,161 @@ +// Copyright 2021-present StarRocks, Inc. All rights reserved. +// +// Licensed 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 +// +// https://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 com.starrocks.sql.analyzer; + +import com.starrocks.common.Config; +import com.starrocks.common.FeConstants; +import com.starrocks.qe.ConnectContext; +import com.starrocks.sql.ast.RecoverDbStmt; +import com.starrocks.sql.ast.ShowCreateDbStmt; +import com.starrocks.sql.ast.UseDbStmt; +import com.starrocks.sql.parser.SqlParser; +import com.starrocks.utframe.UtFrameUtils; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class BasicDbStmtAnalyzerTest { + + private static ConnectContext connectContext; + + @BeforeAll + public static void beforeClass() throws Exception { + FeConstants.runningUnitTest = true; + Config.dynamic_partition_enable = false; + UtFrameUtils.createMinStarRocksCluster(); + connectContext = UtFrameUtils.createDefaultCtx(); + } + + @AfterAll + public static void afterClass() throws Exception { + // Cleanup is handled automatically by the test framework + } + + @Test + public void testUseDbStatementWithNullCatalog() throws Exception { + // Test case to cover line 45 in BasicDbStmtAnalyzer.java + // This should trigger the noCatalogSelected exception when current catalog is null + UseDbStmt stmt = (UseDbStmt) SqlParser.parse("USE test_db", connectContext.getSessionVariable()).get(0); + + // Create a new context with null catalog to trigger the exception + ConnectContext testContext = new ConnectContext(); + testContext.setCurrentUserIdentity(connectContext.getCurrentUserIdentity()); + testContext.setCurrentRoleIds(connectContext.getCurrentRoleIds()); + // Set catalog to null to trigger the exception + testContext.setCurrentCatalog(null); + + try { + BasicDbStmtAnalyzer.analyze(stmt, testContext); + Assertions.fail("Expected SemanticException to be thrown"); + } catch (Exception e) { + Assertions.assertTrue(e.getMessage().contains("No catalog selected")); + } + } + + @Test + public void testRecoverDbStatementWithNullCatalog() throws Exception { + // Test case to cover line 53 in BasicDbStmtAnalyzer.java + // This should trigger the noCatalogSelected exception when current catalog is null + RecoverDbStmt stmt = + (RecoverDbStmt) SqlParser.parse("RECOVER DATABASE test_db", connectContext.getSessionVariable()).get(0); + + // Create a new context with null catalog to trigger the exception + ConnectContext testContext = new ConnectContext(); + testContext.setCurrentUserIdentity(connectContext.getCurrentUserIdentity()); + testContext.setCurrentRoleIds(connectContext.getCurrentRoleIds()); + // Set catalog to null to trigger the exception + testContext.setCurrentCatalog(null); + + try { + BasicDbStmtAnalyzer.analyze(stmt, testContext); + Assertions.fail("Expected SemanticException to be thrown"); + } catch (Exception e) { + Assertions.assertTrue(e.getMessage().contains("No catalog selected")); + } + } + + @Test + public void testShowCreateDbStatementWithNullCatalog() throws Exception { + // Test case to cover line 61 in BasicDbStmtAnalyzer.java + // This should trigger the noCatalogSelected exception when current catalog is null + ShowCreateDbStmt stmt = + (ShowCreateDbStmt) SqlParser.parse("SHOW CREATE DATABASE test_db", connectContext.getSessionVariable()).get(0); + + // Create a new context with null catalog to trigger the exception + ConnectContext testContext = new ConnectContext(); + testContext.setCurrentUserIdentity(connectContext.getCurrentUserIdentity()); + testContext.setCurrentRoleIds(connectContext.getCurrentRoleIds()); + // Set catalog to null to trigger the exception + testContext.setCurrentCatalog(null); + + try { + BasicDbStmtAnalyzer.analyze(stmt, testContext); + Assertions.fail("Expected SemanticException to be thrown"); + } catch (Exception e) { + Assertions.assertTrue(e.getMessage().contains("No catalog selected")); + } + } + + @Test + public void testUseDbStatementWithValidCatalog() throws Exception { + // Test case to ensure normal flow works when catalog is set + UseDbStmt stmt = (UseDbStmt) SqlParser.parse("USE test_db", connectContext.getSessionVariable()).get(0); + + // Use the context with valid catalog + connectContext.setCurrentCatalog("default_catalog"); + + try { + BasicDbStmtAnalyzer.analyze(stmt, connectContext); + // Should not throw exception + } catch (Exception e) { + Assertions.fail("Should not throw exception when catalog is set: " + e.getMessage()); + } + } + + @Test + public void testRecoverDbStatementWithValidCatalog() throws Exception { + // Test case to ensure normal flow works when catalog is set + RecoverDbStmt stmt = + (RecoverDbStmt) SqlParser.parse("RECOVER DATABASE test_db", connectContext.getSessionVariable()).get(0); + + // Use the context with valid catalog + connectContext.setCurrentCatalog("default_catalog"); + + try { + BasicDbStmtAnalyzer.analyze(stmt, connectContext); + // Should not throw exception + } catch (Exception e) { + Assertions.fail("Should not throw exception when catalog is set: " + e.getMessage()); + } + } + + @Test + public void testShowCreateDbStatementWithValidCatalog() throws Exception { + // Test case to ensure normal flow works when catalog is set + ShowCreateDbStmt stmt = + (ShowCreateDbStmt) SqlParser.parse("SHOW CREATE DATABASE test_db", connectContext.getSessionVariable()).get(0); + + // Use the context with valid catalog + connectContext.setCurrentCatalog("default_catalog"); + + try { + BasicDbStmtAnalyzer.analyze(stmt, connectContext); + // Should not throw exception + } catch (Exception e) { + Assertions.fail("Should not throw exception when catalog is set: " + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/TableObjectCaseInsensitiveTest.java b/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/TableObjectCaseInsensitiveTest.java index c2c6aa5808350c..33651a3d6c672b 100644 --- a/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/TableObjectCaseInsensitiveTest.java +++ b/fe/fe-core/src/test/java/com/starrocks/sql/analyzer/TableObjectCaseInsensitiveTest.java @@ -30,7 +30,9 @@ import com.starrocks.sql.ast.CreateDbStmt; import com.starrocks.sql.ast.CreateViewStmt; import com.starrocks.sql.ast.DropDbStmt; +import com.starrocks.sql.ast.RecoverDbStmt; import com.starrocks.sql.ast.SetStmt; +import com.starrocks.sql.ast.ShowCreateDbStmt; import com.starrocks.sql.ast.ShowTableStmt; import com.starrocks.sql.ast.UseDbStmt; import com.starrocks.sql.parser.SqlParser; @@ -424,4 +426,26 @@ public void testDropDb() { dropDbStmt = (DropDbStmt) SqlParser.parseSingleStatement(sql, SqlModeHelper.MODE_DEFAULT); Assertions.assertEquals("test_db", dropDbStmt.getDbName()); } + + @Test + public void testRecoverDb() { + String sql = "RECOVER DATABASE TEST_db"; + RecoverDbStmt recoverDbStmt = (RecoverDbStmt) SqlParser.parseSingleStatement(sql, SqlModeHelper.MODE_DEFAULT); + Assertions.assertEquals("test_db", recoverDbStmt.getDbName()); + + sql = "RECOVER DATABASE TEST_DB"; + recoverDbStmt = (RecoverDbStmt) SqlParser.parseSingleStatement(sql, SqlModeHelper.MODE_DEFAULT); + Assertions.assertEquals("test_db", recoverDbStmt.getDbName()); + } + + @Test + public void testShowCreateDb() { + String sql = "SHOW CREATE DATABASE TEST_db"; + ShowCreateDbStmt showCreateDbStmt = (ShowCreateDbStmt) SqlParser.parseSingleStatement(sql, SqlModeHelper.MODE_DEFAULT); + Assertions.assertEquals("test_db", showCreateDbStmt.getDb()); + + sql = "SHOW CREATE DATABASE TEST_DB"; + showCreateDbStmt = (ShowCreateDbStmt) SqlParser.parseSingleStatement(sql, SqlModeHelper.MODE_DEFAULT); + Assertions.assertEquals("test_db", showCreateDbStmt.getDb()); + } } diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/ast/AlterCatalogStmt.java b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/AlterCatalogStmt.java similarity index 84% rename from fe/fe-core/src/main/java/com/starrocks/sql/ast/AlterCatalogStmt.java rename to fe/fe-parser/src/main/java/com/starrocks/sql/ast/AlterCatalogStmt.java index c8cc8e9c7b09cd..eb95cb8ca118b0 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/ast/AlterCatalogStmt.java +++ b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/AlterCatalogStmt.java @@ -11,19 +11,18 @@ // 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 com.starrocks.sql.ast; import com.starrocks.sql.parser.NodePosition; -import static com.starrocks.common.util.Util.normalizeName; - public class AlterCatalogStmt extends DdlStmt { private final String catalogName; private final AlterClause alterClause; public AlterCatalogStmt(String catalogName, AlterClause alterClause, NodePosition pos) { super(pos); - this.catalogName = normalizeName(catalogName); + this.catalogName = catalogName; this.alterClause = alterClause; } @@ -37,6 +36,6 @@ public AlterClause getAlterClause() { @Override public R accept(AstVisitor visitor, C context) { - return ((AstVisitorExtendInterface) visitor).visitAlterCatalogStatement(this, context); + return visitor.visitAlterCatalogStatement(this, context); } } diff --git a/fe/fe-parser/src/main/java/com/starrocks/sql/ast/AstVisitor.java b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/AstVisitor.java index 6c62005def1a66..e995e61c71c03b 100644 --- a/fe/fe-parser/src/main/java/com/starrocks/sql/ast/AstVisitor.java +++ b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/AstVisitor.java @@ -638,6 +638,14 @@ default R visitShowWarehousesStatement(ShowWarehousesStmt statement, C context) return visitShowStatement(statement, context); } + default R visitShowCreateDbStatement(ShowCreateDbStmt statement, C context) { + return visitShowStatement(statement, context); + } + + default R visitRecoverDbStatement(RecoverDbStmt statement, C context) { + return visitDDLStatement(statement, context); + } + default R visitDropCNGroupStatement(DropCnGroupStmt statement, C context) { return visitDDLStatement(statement, context); } @@ -661,4 +669,16 @@ default R visitShowCreateSecurityIntegrationStatement(ShowCreateSecurityIntegrat default R visitShowSecurityIntegrationStatement(ShowSecurityIntegrationStatement statement, C context) { return visitShowStatement(statement, context); } + + default R visitCreateCatalogStatement(CreateCatalogStmt statement, C context) { + return visitDDLStatement(statement, context); + } + + default R visitShowCreateExternalCatalogStatement(ShowCreateExternalCatalogStmt statement, C context) { + return visitShowStatement(statement, context); + } + + default R visitAlterCatalogStatement(AlterCatalogStmt statement, C context) { + return visitDDLStatement(statement, context); + } } \ No newline at end of file diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/ast/CreateCatalogStmt.java b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/CreateCatalogStmt.java similarity index 65% rename from fe/fe-core/src/main/java/com/starrocks/sql/ast/CreateCatalogStmt.java rename to fe/fe-parser/src/main/java/com/starrocks/sql/ast/CreateCatalogStmt.java index 1fc00fac76497e..a0203dab04e4a8 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/ast/CreateCatalogStmt.java +++ b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/CreateCatalogStmt.java @@ -14,13 +14,10 @@ package com.starrocks.sql.ast; -import com.starrocks.common.util.PrintableMap; import com.starrocks.sql.parser.NodePosition; import java.util.Map; -import static com.starrocks.common.util.Util.normalizeName; - public class CreateCatalogStmt extends DdlStmt { public static final String TYPE = "type"; @@ -28,17 +25,12 @@ public class CreateCatalogStmt extends DdlStmt { private final String comment; private final Map properties; private String catalogType; - private final boolean ifNotExists; - public CreateCatalogStmt(String catalogName, String comment, Map properties, boolean ifNotExists) { - this(catalogName, comment, properties, ifNotExists, NodePosition.ZERO); - } - public CreateCatalogStmt(String catalogName, String comment, Map properties, boolean ifNotExists, NodePosition pos) { super(pos); - this.catalogName = normalizeName(catalogName); + this.catalogName = catalogName; this.comment = comment; this.properties = properties; this.ifNotExists = ifNotExists; @@ -70,21 +62,6 @@ public boolean isIfNotExists() { @Override public R accept(AstVisitor visitor, C context) { - return ((AstVisitorExtendInterface) visitor).visitCreateCatalogStatement(this, context); - } - - @Override - public String toSql() { - StringBuilder sb = new StringBuilder(); - sb.append("CREATE EXTERNAL CATALOG "); - if (ifNotExists) { - sb.append("IF NOT EXISTS "); - } - sb.append("'").append(catalogName).append("' "); - if (comment != null) { - sb.append("COMMENT \"").append(comment).append("\" "); - } - sb.append("PROPERTIES(").append(new PrintableMap<>(properties, " = ", true, false)).append(")"); - return sb.toString(); + return visitor.visitCreateCatalogStatement(this, context); } } diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/ast/RecoverDbStmt.java b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/RecoverDbStmt.java similarity index 63% rename from fe/fe-core/src/main/java/com/starrocks/sql/ast/RecoverDbStmt.java rename to fe/fe-parser/src/main/java/com/starrocks/sql/ast/RecoverDbStmt.java index 1fd1a2d6cdc163..ac812afabec5ea 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/ast/RecoverDbStmt.java +++ b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/RecoverDbStmt.java @@ -4,7 +4,7 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// https://www.apache.org/licenses/LICENSE-2.0 +// https://www.apache.org/licenses/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, @@ -12,16 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. - package com.starrocks.sql.ast; import com.starrocks.sql.parser.NodePosition; -import static com.starrocks.common.util.Util.normalizeName; - public class RecoverDbStmt extends DdlStmt { - private String catalog; - private String dbName; + private final String dbName; public RecoverDbStmt(String dbName) { this(dbName, NodePosition.ZERO); @@ -29,27 +25,16 @@ public RecoverDbStmt(String dbName) { public RecoverDbStmt(String dbName, NodePosition pos) { super(pos); - this.dbName = normalizeName(dbName); + this.dbName = dbName; } public String getDbName() { return dbName; } - public void setDbName(String dbname) { - this.dbName = normalizeName(dbname); - } - - public String getCatalogName() { - return this.catalog; - } - - public void setCatalogName(String catalogName) { - this.catalog = normalizeName(catalogName); - } - @Override public R accept(AstVisitor visitor, C context) { - return ((AstVisitorExtendInterface) visitor).visitRecoverDbStatement(this, context); + return visitor.visitRecoverDbStatement(this, context); } } + diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/ast/ShowCreateDbStmt.java b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/ShowCreateDbStmt.java similarity index 70% rename from fe/fe-core/src/main/java/com/starrocks/sql/ast/ShowCreateDbStmt.java rename to fe/fe-parser/src/main/java/com/starrocks/sql/ast/ShowCreateDbStmt.java index 96b0eedcb88acf..cbc916b66b76a4 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/ast/ShowCreateDbStmt.java +++ b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/ShowCreateDbStmt.java @@ -16,15 +16,12 @@ import com.starrocks.sql.parser.NodePosition; -import static com.starrocks.common.util.Util.normalizeName; - // Show create database statement // Syntax: // SHOW CREATE DATABASE db public class ShowCreateDbStmt extends ShowStmt { - private String catalog; - private String db; + private final String db; public ShowCreateDbStmt(String db) { this(db, NodePosition.ZERO); @@ -32,25 +29,13 @@ public ShowCreateDbStmt(String db) { public ShowCreateDbStmt(String db, NodePosition pos) { super(pos); - this.db = normalizeName(db); - } - - public String getCatalogName() { - return this.catalog; - } - - public void setCatalogName(String catalogName) { - this.catalog = normalizeName(catalogName); + this.db = db; } public String getDb() { return db; } - public void setDb(String db) { - this.db = normalizeName(db); - } - @Override public String toString() { return toSql(); @@ -58,6 +43,6 @@ public String toString() { @Override public R accept(AstVisitor visitor, C context) { - return ((AstVisitorExtendInterface) visitor).visitShowCreateDbStatement(this, context); + return visitor.visitShowCreateDbStatement(this, context); } -} +} \ No newline at end of file diff --git a/fe/fe-core/src/main/java/com/starrocks/sql/ast/ShowCreateExternalCatalogStmt.java b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/ShowCreateExternalCatalogStmt.java similarity index 84% rename from fe/fe-core/src/main/java/com/starrocks/sql/ast/ShowCreateExternalCatalogStmt.java rename to fe/fe-parser/src/main/java/com/starrocks/sql/ast/ShowCreateExternalCatalogStmt.java index 4d06e4ae244c2d..668eb7522f56a6 100644 --- a/fe/fe-core/src/main/java/com/starrocks/sql/ast/ShowCreateExternalCatalogStmt.java +++ b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/ShowCreateExternalCatalogStmt.java @@ -12,13 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. - package com.starrocks.sql.ast; import com.starrocks.sql.parser.NodePosition; -import static com.starrocks.common.util.Util.normalizeName; - // SHOW CREATE EXTERNAL CATALOG statement. public class ShowCreateExternalCatalogStmt extends ShowStmt { private final String catalogName; @@ -29,7 +26,7 @@ public ShowCreateExternalCatalogStmt(String catalogName) { public ShowCreateExternalCatalogStmt(String catalogName, NodePosition pos) { super(pos); - this.catalogName = normalizeName(catalogName); + this.catalogName = catalogName; } public String getCatalogName() { @@ -38,6 +35,6 @@ public String getCatalogName() { @Override public R accept(AstVisitor visitor, C context) { - return ((AstVisitorExtendInterface) visitor).visitShowCreateExternalCatalogStatement(this, context); + return visitor.visitShowCreateExternalCatalogStatement(this, context); } } diff --git a/fe/fe-parser/src/main/java/com/starrocks/sql/ast/UseDbStmt.java b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/UseDbStmt.java index 53737e4beef862..f6d18ee5ac974d 100644 --- a/fe/fe-parser/src/main/java/com/starrocks/sql/ast/UseDbStmt.java +++ b/fe/fe-parser/src/main/java/com/starrocks/sql/ast/UseDbStmt.java @@ -22,7 +22,7 @@ * Queries from JDBC will be handled by COM_QUERY protocol, it will generate UseDbStmt. */ public class UseDbStmt extends StatementBase { - private String catalog; + private final String catalog; private final String database; public UseDbStmt(String catalog, String database, NodePosition pos) { @@ -35,10 +35,6 @@ public String getCatalogName() { return this.catalog; } - public void setCatalogName(String catalogName) { - this.catalog = catalogName; - } - public String getDbName() { return this.database; }