[feature-wip](multi-catalog) get catalog name from TableName (#10435)

This commit is contained in:
Ashin Gau
2022-06-28 10:42:37 +08:00
committed by GitHub
parent 498a80547c
commit 1f2bf39140
3 changed files with 23 additions and 1 deletions

View File

@ -1977,6 +1977,10 @@ tbl_pattern ::=
{:
RESULT = new TablePattern(db, tbl);
:}
| ident_or_star:ctl DOT ident_or_star:db DOT ident_or_star:tbl
{:
RESULT = new TablePattern(ctl, db, tbl);
:}
;
resource_pattern ::=
@ -4157,6 +4161,10 @@ star_expr ::=
{:
RESULT = SelectListItem.createStarItem(new TableName(db, tbl));
:}
| ident:ctl DOT ident:db DOT ident:tbl DOT STAR
{:
RESULT = SelectListItem.createStarItem(new TableName(ctl, db, tbl));
:}
;
opt_table_name ::=
@ -4174,6 +4182,8 @@ table_name ::=
{: RESULT = new TableName(null, tbl); :}
| ident:db DOT ident:tbl
{: RESULT = new TableName(db, tbl); :}
| ident:ctl DOT ident:db DOT ident:tbl
{: RESULT = new TableName(ctl, db, tbl); :}
;
encryptkey_name ::=
@ -5292,6 +5302,8 @@ column_ref ::=
{: RESULT = new SlotRef(new TableName(null, tbl), col); :}
| ident:db DOT ident:tbl DOT ident:col
{: RESULT = new SlotRef(new TableName(db, tbl), col); :}
| ident:ctl DOT ident:db DOT ident:tbl DOT ident:col
{: RESULT = new SlotRef(new TableName(ctl, db, tbl), col); :}
;
column_subscript ::=

View File

@ -28,6 +28,7 @@ import org.apache.doris.common.ErrorReport;
import org.apache.doris.common.FeMetaVersion;
import org.apache.doris.common.io.Text;
import org.apache.doris.common.io.Writable;
import org.apache.doris.common.util.Util;
import org.apache.doris.datasource.InternalDataSource;
import org.apache.doris.persist.gson.GsonUtils;
@ -76,6 +77,9 @@ public class TableName implements Writable {
ctl = InternalDataSource.INTERNAL_DS_NAME;
}
}
if (!ctl.equals(InternalDataSource.INTERNAL_DS_NAME)) {
Util.checkCatalogEnabled();
}
if (Strings.isNullOrEmpty(db)) {
db = analyzer.getDefaultDb();
if (Strings.isNullOrEmpty(db)) {

View File

@ -65,11 +65,17 @@ public class SwitchStmtTest {
GrantStmt grantRole1 = (GrantStmt) UtFrameUtils.parseAndAnalyzeStmt(
"grant grant_priv on tpch.* to role 'role1';", rootCtx);
auth.grant(grantRole1);
// grant with ctl.db.tbl. grant can succeed even if the catalog does not exist
GrantStmt grantRole1WithCtl = (GrantStmt) UtFrameUtils.parseAndAnalyzeStmt(
"grant select_priv on testc.testdb.* to role 'role1';", rootCtx);
auth.grant(grantRole1WithCtl);
// user1 can't switch to hive
auth.createUser((CreateUserStmt) UtFrameUtils.parseAndAnalyzeStmt(
"create user 'user1'@'%' identified by 'pwd1' default role 'role1';", rootCtx));
user1 = new UserIdentity("user1", "%");
user1.analyze(clusterName);
// user1 has the privileges of testc which is granted by ctl.db.tbl format.
Assert.assertTrue(auth.getDbPrivTable().hasPrivsOfCatalog(user1, "testc"));
// create catalog
CreateCatalogStmt hiveCatalog = (CreateCatalogStmt) UtFrameUtils.parseAndAnalyzeStmt(
@ -136,7 +142,7 @@ public class SwitchStmtTest {
Assert.assertEquals(user1ShowResult.size(), 1);
Assert.assertEquals(user1ShowResult.get(0).get(0), InternalDataSource.INTERNAL_DS_NAME);
// mock the login of user1
// mock the login of user2
ConnectContext user2Ctx = UtFrameUtils.createDefaultCtx(user2, "127.0.0.1");
ShowCatalogStmt user2Show = (ShowCatalogStmt) UtFrameUtils.parseAndAnalyzeStmt("show catalogs;", user2Ctx);
List<List<String>> user2ShowResult = catalog.getDataSourceMgr().showCatalogs(user2Show).getResultRows();