Skip to content

Commit

Permalink
feat: multi catalog support && simple hive catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
dantengsky committed Apr 21, 2022
1 parent 852dcf7 commit 9830127
Show file tree
Hide file tree
Showing 120 changed files with 76,175 additions and 439 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions common/ast/src/parser/ast/ast_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ pub trait AstVisitor {
fn visit_table_reference(&mut self, table_reference: &TableReference) -> Result<()> {
match table_reference {
TableReference::Table {
catalog,
database,
table,
alias,
} => self.visit_table(database, table, alias),
} => self.visit_table(catalog, database, table, alias),
TableReference::Subquery { subquery, alias } => {
self.visit_table_subquery(subquery, alias)
}
Expand All @@ -252,8 +253,9 @@ pub trait AstVisitor {

fn visit_table(
&mut self,
_: &Option<Identifier>,
_: &Identifier,
_catalog: &Option<Identifier>,
_database: &Option<Identifier>,
_table: &Identifier,
alias: &Option<TableAlias>,
) -> Result<()> {
self.visit_table_alias(alias.as_ref().unwrap())
Expand Down
7 changes: 6 additions & 1 deletion common/ast/src/parser/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ pub enum Indirection {
pub enum TableReference {
// Table name
Table {
// Could be `db.table` or `table`
// `[catalog.][db.]table`
catalog: Option<Identifier>,
database: Option<Identifier>,
table: Identifier,
alias: Option<TableAlias>,
Expand Down Expand Up @@ -195,11 +196,15 @@ impl Display for TableReference {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
TableReference::Table {
catalog,
database,
table,
alias,
} => {
let mut idents = vec![];
if let Some(ident) = catalog {
idents.push(ident.to_owned());
}
if let Some(ident) = database {
idents.push(ident.to_owned());
}
Expand Down
9 changes: 9 additions & 0 deletions common/ast/src/parser/transformer/transform_sqlparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,25 @@ impl TransformerSqlparser {
let idents = &name.0;
if idents.len() == 1 {
Ok(TableReference::Table {
catalog: None,
database: None,
table: Identifier::from(&idents[0]),
alias: alias.as_ref().map(Self::transform_table_alias),
})
} else if idents.len() == 2 {
Ok(TableReference::Table {
catalog: None,
database: Some(Identifier::from(&idents[0])),
table: Identifier::from(&idents[1]),
alias: alias.as_ref().map(Self::transform_table_alias),
})
} else if idents.len() == 3 {
Ok(TableReference::Table {
catalog: Some(Identifier::from(&idents[0])),
database: Some(Identifier::from(&idents[1])),
table: Identifier::from(&idents[2]),
alias: alias.as_ref().map(Self::transform_table_alias),
})
} else {
Err(ErrorCode::SyntaxException(format!(
"Unsupported SQL statement: {}",
Expand Down
3 changes: 3 additions & 0 deletions common/ast/tests/it/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ fn test_display_query() {
op: JoinOperator::Inner,
condition: JoinCondition::Natural,
left: Box::new(TableReference::Table {
catalog: None,
database: None,
table: Identifier {
name: "left_table".to_owned(),
Expand All @@ -93,6 +94,7 @@ fn test_display_query() {
alias: None,
}),
right: Box::new(TableReference::Table {
catalog: None,
database: None,
table: Identifier {
name: "right_table".to_owned(),
Expand Down Expand Up @@ -158,6 +160,7 @@ fn test_display_query() {
#[test]
fn test_display_table_reference() {
let table_ref = TableReference::Table {
catalog: None,
database: None,
table: Identifier {
name: "table".to_owned(),
Expand Down
6 changes: 6 additions & 0 deletions common/meta/api/src/meta_api_test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ impl MetaApiTestSuite {
let req = RenameTableReq {
if_exists: false,
tenant: tenant.to_string(),
catalog: "default".to_string(),
db: db_name.to_string(),
table_name: tbl_name.to_string(),
new_db: db_name.to_string(),
Expand Down Expand Up @@ -745,6 +746,7 @@ impl MetaApiTestSuite {
let req = RenameTableReq {
if_exists: false,
tenant: tenant.to_string(),
catalog: "default".to_string(),
db: db_name.to_string(),
table_name: tbl_name.to_string(),
new_db: db_name.to_string(),
Expand Down Expand Up @@ -777,6 +779,7 @@ impl MetaApiTestSuite {
let req = RenameTableReq {
if_exists: false,
tenant: tenant.to_string(),
catalog: "default".to_string(),
db: db_name.to_string(),
table_name: tbl_name.to_string(),
new_db: db_name.to_string(),
Expand Down Expand Up @@ -813,6 +816,7 @@ impl MetaApiTestSuite {
let req = RenameTableReq {
if_exists: false,
tenant: tenant.to_string(),
catalog: "default".to_string(),
db: db_name.to_string(),
table_name: tbl_name.to_string(),
new_db: db_name.to_string(),
Expand All @@ -833,6 +837,7 @@ impl MetaApiTestSuite {
let req = RenameTableReq {
if_exists: false,
tenant: tenant.to_string(),
catalog: "default".to_string(),
db: db_name.to_string(),
table_name: tbl_name.to_string(),
new_db: new_db_name.to_string(),
Expand Down Expand Up @@ -871,6 +876,7 @@ impl MetaApiTestSuite {
let req = RenameTableReq {
if_exists: false,
tenant: tenant.to_string(),
catalog: "default".to_string(),
db: db_name.to_string(),
table_name: tbl_name.to_string(),
new_db: new_db_name.to_string(),
Expand Down
1 change: 1 addition & 0 deletions common/meta/types/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ pub struct DropTableReply {}
pub struct RenameTableReq {
pub if_exists: bool,
pub tenant: String,
pub catalog: String,
pub db: String,
pub table_name: String,
pub new_db: String,
Expand Down
31 changes: 19 additions & 12 deletions common/meta/types/src/user_grant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::UserPrivilegeType;
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq)]
pub enum GrantObject {
Global,
Database(String),
Table(String, String),
Database(String, String),
Table(String, String, String),
}

impl GrantObject {
Expand All @@ -33,22 +33,27 @@ impl GrantObject {
pub fn contains(&self, object: &GrantObject) -> bool {
match (self, object) {
(GrantObject::Global, _) => true,
(GrantObject::Database(_), GrantObject::Global) => false,
(GrantObject::Database(lhs), GrantObject::Database(rhs)) => lhs == rhs,
(GrantObject::Database(lhs), GrantObject::Table(rhs, _)) => lhs == rhs,
(GrantObject::Table(lhs_db, lhs_table), GrantObject::Table(rhs_db, rhs_table)) => {
(lhs_db == rhs_db) && (lhs_table == rhs_table)
(GrantObject::Database(_, _), GrantObject::Global) => false,
(GrantObject::Database(lcat, ldb), GrantObject::Database(rcat, rdb)) => {
lcat == rcat && ldb == rdb
}
(GrantObject::Table(_, _), _) => false,
(GrantObject::Database(lcat, ldb), GrantObject::Table(rcat, rdb, _)) => {
lcat == rcat && ldb == rdb
}
(
GrantObject::Table(lcat, lhs_db, lhs_table),
GrantObject::Table(rcat, rhs_db, rhs_table),
) => lcat == rcat && (lhs_db == rhs_db) && (lhs_table == rhs_table),
(GrantObject::Table(_, _, _), _) => false,
}
}

/// Global, database and table has different available privileges
pub fn available_privileges(&self) -> UserPrivilegeSet {
match self {
GrantObject::Global => UserPrivilegeSet::available_privileges_on_global(),
GrantObject::Database(_) => UserPrivilegeSet::available_privileges_on_database(),
GrantObject::Table(_, _) => UserPrivilegeSet::available_privileges_on_table(),
GrantObject::Database(_, _) => UserPrivilegeSet::available_privileges_on_database(),
GrantObject::Table(_, _, _) => UserPrivilegeSet::available_privileges_on_table(),
}
}
}
Expand All @@ -57,8 +62,10 @@ impl fmt::Display for GrantObject {
fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
match self {
GrantObject::Global => write!(f, "*.*"),
GrantObject::Database(ref db) => write!(f, "'{}'.*", db),
GrantObject::Table(ref db, ref table) => write!(f, "'{}'.'{}'", db, table),
GrantObject::Database(ref cat, ref db) => write!(f, "'{}'.'{}'.*", cat, db),
GrantObject::Table(ref cat, ref db, ref table) => {
write!(f, "'{}'.'{}'.'{}'", cat, db, table)
}
}
}
}
Expand Down
64 changes: 32 additions & 32 deletions common/meta/types/tests/it/user_grant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn test_grant_object_contains() -> Result<()> {
let tests: Vec<Test> = vec![
Test {
lhs: GrantObject::Global,
rhs: GrantObject::Table("a".into(), "b".into()),
rhs: GrantObject::Table("default".into(), "a".into(), "b".into()),
expect: true,
},
Test {
Expand All @@ -39,42 +39,42 @@ fn test_grant_object_contains() -> Result<()> {
},
Test {
lhs: GrantObject::Global,
rhs: GrantObject::Database("a".into()),
rhs: GrantObject::Database("default".into(), "a".into()),
expect: true,
},
Test {
lhs: GrantObject::Database("a".into()),
lhs: GrantObject::Database("default".into(), "a".into()),
rhs: GrantObject::Global,
expect: false,
},
Test {
lhs: GrantObject::Database("a".into()),
rhs: GrantObject::Database("b".into()),
lhs: GrantObject::Database("default".into(), "a".into()),
rhs: GrantObject::Database("default".into(), "b".into()),
expect: false,
},
Test {
lhs: GrantObject::Database("a".into()),
rhs: GrantObject::Table("b".into(), "c".into()),
lhs: GrantObject::Database("default".into(), "a".into()),
rhs: GrantObject::Table("default".into(), "b".into(), "c".into()),
expect: false,
},
Test {
lhs: GrantObject::Database("db1".into()),
rhs: GrantObject::Table("db1".into(), "c".into()),
lhs: GrantObject::Database("default".into(), "db1".into()),
rhs: GrantObject::Table("default".into(), "db1".into(), "c".into()),
expect: true,
},
Test {
lhs: GrantObject::Table("db1".into(), "c".into()),
rhs: GrantObject::Table("db1".into(), "c".into()),
lhs: GrantObject::Table("default".into(), "db1".into(), "c".into()),
rhs: GrantObject::Table("default".into(), "db1".into(), "c".into()),
expect: true,
},
Test {
lhs: GrantObject::Table("db1".into(), "c".into()),
lhs: GrantObject::Table("default".into(), "db1".into(), "c".into()),
rhs: GrantObject::Global,
expect: false,
},
Test {
lhs: GrantObject::Table("db1".into(), "c".into()),
rhs: GrantObject::Database("db1".into()),
lhs: GrantObject::Table("default".into(), "db1".into(), "c".into()),
rhs: GrantObject::Database("default".into(), "db1".into()),
expect: false,
},
];
Expand All @@ -98,53 +98,53 @@ fn test_user_grant_entry() -> Result<()> {
make_bitflags!(UserPrivilegeType::{Create}),
);
assert!(grant.verify_privilege(
&GrantObject::Database("db1".into()),
&GrantObject::Database("default".into(), "db1".into()),
UserPrivilegeType::Create
));
assert!(!grant.verify_privilege(
&GrantObject::Database("db1".into()),
&GrantObject::Database("default".into(), "db1".into()),
UserPrivilegeType::Insert
));
assert!(grant.verify_privilege(
&GrantObject::Database("db2".into()),
&GrantObject::Database("default".into(), "db2".into()),
UserPrivilegeType::Create
));

let grant = GrantEntry::new(
GrantObject::Database("db1".into()),
GrantObject::Database("default".into(), "db1".into()),
make_bitflags!(UserPrivilegeType::{Create}),
);
assert!(grant.verify_privilege(
&GrantObject::Table("db1".into(), "table1".into()),
&GrantObject::Table("default".into(), "db1".into(), "table1".into()),
UserPrivilegeType::Create
));
assert!(!grant.verify_privilege(
&GrantObject::Table("db2".into(), "table1".into()),
&GrantObject::Table("default".into(), "db2".into(), "table1".into()),
UserPrivilegeType::Create
));
assert!(grant.verify_privilege(
&GrantObject::Database("db1".into()),
&GrantObject::Database("default".into(), "db1".into()),
UserPrivilegeType::Create
));

let grant = GrantEntry::new(
GrantObject::Database("db1".into()),
GrantObject::Database("default".into(), "db1".into()),
make_bitflags!(UserPrivilegeType::{Create}),
);
assert!(grant.verify_privilege(
&GrantObject::Table("db1".into(), "table1".into()),
&GrantObject::Table("default".into(), "db1".into(), "table1".into()),
UserPrivilegeType::Create
));
assert!(!grant.verify_privilege(
&GrantObject::Table("db2".into(), "table1".into()),
&GrantObject::Table("default".into(), "db2".into(), "table1".into()),
UserPrivilegeType::Create
));
assert!(!grant.verify_privilege(
&GrantObject::Table("db1".into(), "table1".into()),
&GrantObject::Table("default".into(), "db1".into(), "table1".into()),
UserPrivilegeType::Insert
));
assert!(grant.verify_privilege(
&GrantObject::Table("db1".into(), "table1".into()),
&GrantObject::Table("default".into(), "db1".into(), "table1".into()),
UserPrivilegeType::Create
));

Expand All @@ -168,7 +168,7 @@ fn test_user_grant_set() -> Result<()> {
make_bitflags!(UserPrivilegeType::{Insert}).into(),
);
grants.grant_privileges(
&GrantObject::Table("db1".into(), "table1".into()),
&GrantObject::Table("default".into(), "db1".into(), "table1".into()),
make_bitflags!(UserPrivilegeType::{Select | Create}).into(),
);
assert_eq!(2, grants.entries().len());
Expand All @@ -179,23 +179,23 @@ fn test_user_grant_set() -> Result<()> {
);
assert_eq!(2, grants.entries().len());
assert!(grants.verify_privilege(
&GrantObject::Database("db1".into()),
&GrantObject::Database("default".into(), "db1".into()),
UserPrivilegeType::Create
));
assert!(!grants.verify_privilege(
&GrantObject::Database("db1".into()),
&GrantObject::Database("default".into(), "db1".into()),
UserPrivilegeType::Select
));
assert!(grants.verify_privilege(
&GrantObject::Table("db1".into(), "table1".into()),
&GrantObject::Table("default".into(), "db1".into(), "table1".into()),
UserPrivilegeType::Create
));
assert!(!grants.verify_privilege(
&GrantObject::Table("db1".into(), "table1".into()),
&GrantObject::Table("default".into(), "db1".into(), "table1".into()),
UserPrivilegeType::Insert
));
assert!(grants.verify_privilege(
&GrantObject::Table("db1".into(), "table1".into()),
&GrantObject::Table("default".into(), "db1".into(), "table1".into()),
UserPrivilegeType::Select
));
Ok(())
Expand Down
Loading

0 comments on commit 9830127

Please sign in to comment.