Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<String>> rows = Lists.newArrayList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Void, ConnectContext> {
// 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
Expand Down Expand Up @@ -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());
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}

Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +34,7 @@

public class CatalogStmtTest {
private static StarRocksAssert starRocksAssert;

@BeforeAll
public static void beforeClass() throws Exception {
UtFrameUtils.createMinStarRocksCluster();
Expand All @@ -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";
Expand All @@ -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);
}

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Loading
Loading