Skip to content

Commit a2daa3e

Browse files
authored
Merge branch 'main' into share_sql
2 parents de41cd4 + 7d39378 commit a2daa3e

File tree

35 files changed

+850
-211
lines changed

35 files changed

+850
-211
lines changed

common/ast/src/ast/statements/user.rs

+23-28
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct CreateUserStmt {
2929
pub if_not_exists: bool,
3030
pub user: UserIdentity,
3131
pub auth_option: AuthOption,
32-
pub role_options: Vec<RoleOption>,
32+
pub user_options: Vec<UserOptionItem>,
3333
}
3434

3535
impl Display for CreateUserStmt {
@@ -40,10 +40,10 @@ impl Display for CreateUserStmt {
4040
}
4141
write!(f, " {} IDENTIFIED", self.user)?;
4242
write!(f, " {}", self.auth_option)?;
43-
if !self.role_options.is_empty() {
43+
if !self.user_options.is_empty() {
4444
write!(f, " WITH")?;
45-
for role_option in &self.role_options {
46-
write!(f, " {role_option}")?;
45+
for user_option in &self.user_options {
46+
write!(f, " {user_option}")?;
4747
}
4848
}
4949

@@ -76,7 +76,7 @@ pub struct AlterUserStmt {
7676
pub user: Option<UserIdentity>,
7777
// None means no change to make
7878
pub auth_option: Option<AuthOption>,
79-
pub role_options: Vec<RoleOption>,
79+
pub user_options: Vec<UserOptionItem>,
8080
}
8181

8282
impl Display for AlterUserStmt {
@@ -90,9 +90,9 @@ impl Display for AlterUserStmt {
9090
if let Some(auth_option) = &self.auth_option {
9191
write!(f, " IDENTIFIED {}", auth_option)?;
9292
}
93-
if !self.role_options.is_empty() {
93+
if !self.user_options.is_empty() {
9494
write!(f, " WITH")?;
95-
for with_option in &self.role_options {
95+
for with_option in &self.user_options {
9696
write!(f, " {with_option}")?;
9797
}
9898
}
@@ -155,28 +155,22 @@ pub enum AccountMgrLevel {
155155
}
156156

157157
#[derive(Debug, Clone, PartialEq, Eq)]
158-
pub enum RoleOption {
159-
TenantSetting,
160-
NoTenantSetting,
161-
ConfigReload,
162-
NoConfigReload,
158+
pub enum UserOptionItem {
159+
TenantSetting(bool),
160+
ConfigReload(bool),
161+
DefaultRole(String),
163162
}
164163

165-
impl RoleOption {
164+
impl UserOptionItem {
166165
pub fn apply(&self, option: &mut UserOption) {
167166
match self {
168-
Self::TenantSetting => {
169-
option.set_option_flag(UserOptionFlag::TenantSetting);
167+
Self::TenantSetting(enabled) => {
168+
option.switch_option_flag(UserOptionFlag::TenantSetting, *enabled);
170169
}
171-
Self::NoTenantSetting => {
172-
option.unset_option_flag(UserOptionFlag::TenantSetting);
173-
}
174-
Self::ConfigReload => {
175-
option.set_option_flag(UserOptionFlag::ConfigReload);
176-
}
177-
Self::NoConfigReload => {
178-
option.unset_option_flag(UserOptionFlag::ConfigReload);
170+
Self::ConfigReload(enabled) => {
171+
option.switch_option_flag(UserOptionFlag::ConfigReload, *enabled);
179172
}
173+
Self::DefaultRole(v) => option.set_default_role(Some(v.clone())),
180174
}
181175
}
182176
}
@@ -233,13 +227,14 @@ impl Display for AccountMgrSource {
233227
}
234228
}
235229

236-
impl Display for RoleOption {
230+
impl Display for UserOptionItem {
237231
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
238232
match self {
239-
RoleOption::TenantSetting => write!(f, "TENANTSETTING"),
240-
RoleOption::NoTenantSetting => write!(f, "NOTENANTSETTING"),
241-
RoleOption::ConfigReload => write!(f, "CONFIGRELOAD"),
242-
RoleOption::NoConfigReload => write!(f, "NOCONFIGRELOAD"),
233+
UserOptionItem::TenantSetting(true) => write!(f, "TENANTSETTING"),
234+
UserOptionItem::TenantSetting(false) => write!(f, "NOTENANTSETTING"),
235+
UserOptionItem::ConfigReload(true) => write!(f, "CONFIGRELOAD"),
236+
UserOptionItem::ConfigReload(false) => write!(f, "NOCONFIGRELOAD"),
237+
UserOptionItem::DefaultRole(v) => write!(f, "DEFAULT_ROLE = '{}'", v),
243238
}
244239
}
245240
}

common/ast/src/parser/statement.rs

+28-15
Original file line numberDiff line numberDiff line change
@@ -458,18 +458,18 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
458458
CREATE ~ USER ~ ( IF ~ NOT ~ EXISTS )?
459459
~ #user_identity
460460
~ IDENTIFIED ~ ( WITH ~ ^#auth_type )? ~ ( BY ~ ^#literal_string )?
461-
~ ( WITH ~ ^#role_option+ )?
461+
~ ( WITH ~ ^#comma_separated_list1(user_option))?
462462
},
463-
|(_, _, opt_if_not_exists, user, _, opt_auth_type, opt_password, opt_role_options)| {
463+
|(_, _, opt_if_not_exists, user, _, opt_auth_type, opt_password, opt_user_option)| {
464464
Statement::CreateUser(CreateUserStmt {
465465
if_not_exists: opt_if_not_exists.is_some(),
466466
user,
467467
auth_option: AuthOption {
468468
auth_type: opt_auth_type.map(|(_, auth_type)| auth_type),
469469
password: opt_password.map(|(_, password)| password),
470470
},
471-
role_options: opt_role_options
472-
.map(|(_, role_options)| role_options)
471+
user_options: opt_user_option
472+
.map(|(_, user_options)| user_options)
473473
.unwrap_or_default(),
474474
})
475475
},
@@ -478,17 +478,17 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
478478
rule! {
479479
ALTER ~ USER ~ ( #map(rule! { USER ~ "(" ~ ")" }, |_| None) | #map(user_identity, Some) )
480480
~ ( IDENTIFIED ~ ( WITH ~ ^#auth_type )? ~ ( BY ~ ^#literal_string )? )?
481-
~ ( WITH ~ ^#role_option+ )?
481+
~ ( WITH ~ ^#comma_separated_list1(user_option) )?
482482
},
483-
|(_, _, user, opt_auth_option, opt_role_options)| {
483+
|(_, _, user, opt_auth_option, opt_user_option)| {
484484
Statement::AlterUser(AlterUserStmt {
485485
user,
486486
auth_option: opt_auth_option.map(|(_, opt_auth_type, opt_password)| AuthOption {
487487
auth_type: opt_auth_type.map(|(_, auth_type)| auth_type),
488488
password: opt_password.map(|(_, password)| password),
489489
}),
490-
role_options: opt_role_options
491-
.map(|(_, role_options)| role_options)
490+
user_options: opt_user_option
491+
.map(|(_, user_options)| user_options)
492492
.unwrap_or_default(),
493493
})
494494
},
@@ -813,8 +813,8 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
813813
),
814814
rule!(
815815
#show_users : "`SHOW USERS`"
816-
| #create_user : "`CREATE USER [IF NOT EXISTS] '<username>'@'hostname' IDENTIFIED [WITH <auth_type>] [BY <password>] [WITH <role_option> ...]`"
817-
| #alter_user : "`ALTER USER ('<username>'@'hostname' | USER()) [IDENTIFIED [WITH <auth_type>] [BY <password>]] [WITH <role_option> ...]`"
816+
| #create_user : "`CREATE USER [IF NOT EXISTS] '<username>'@'hostname' IDENTIFIED [WITH <auth_type>] [BY <password>] [WITH <user_option>, ...]`"
817+
| #alter_user : "`ALTER USER ('<username>'@'hostname' | USER()) [IDENTIFIED [WITH <auth_type>] [BY <password>]] [WITH <user_option>, ...]`"
818818
| #drop_user : "`DROP USER [IF EXISTS] '<username>'@'hostname'`"
819819
| #show_roles : "`SHOW ROLES`"
820820
| #create_role : "`CREATE ROLE [IF NOT EXISTS] '<role_name>']`"
@@ -1310,12 +1310,25 @@ pub fn database_engine(i: Input) -> IResult<DatabaseEngine> {
13101310
)(i)
13111311
}
13121312

1313-
pub fn role_option(i: Input) -> IResult<RoleOption> {
1313+
pub fn user_option(i: Input) -> IResult<UserOptionItem> {
1314+
let default_role_option = map(
1315+
rule! {
1316+
"DEFAULT_ROLE" ~ "=" ~ #literal_string
1317+
},
1318+
|(_, _, role)| UserOptionItem::DefaultRole(role),
1319+
);
13141320
alt((
1315-
value(RoleOption::TenantSetting, rule! { TENANTSETTING }),
1316-
value(RoleOption::NoTenantSetting, rule! { NOTENANTSETTING }),
1317-
value(RoleOption::ConfigReload, rule! { CONFIGRELOAD }),
1318-
value(RoleOption::NoConfigReload, rule! { NOCONFIGRELOAD }),
1321+
value(UserOptionItem::TenantSetting(true), rule! { TENANTSETTING }),
1322+
value(
1323+
UserOptionItem::TenantSetting(false),
1324+
rule! { NOTENANTSETTING },
1325+
),
1326+
value(UserOptionItem::ConfigReload(true), rule! { CONFIGRELOAD }),
1327+
value(
1328+
UserOptionItem::ConfigReload(false),
1329+
rule! { NOCONFIGRELOAD },
1330+
),
1331+
default_role_option,
13191332
))(i)
13201333
}
13211334

common/ast/tests/it/parser.rs

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ fn test_statement() {
100100
r#"CREATE TABLE t(c1 int null, c2 bigint null, c3 varchar null);"#,
101101
r#"CREATE TABLE t(c1 int not null, c2 bigint not null, c3 varchar not null);"#,
102102
r#"CREATE TABLE t(c1 int default 1);"#,
103+
r#"ALTER USER u1 IDENTIFIED BY '123456';"#,
104+
r#"ALTER USER u1 WITH DEFAULT_ROLE = 'role1';"#,
105+
r#"ALTER USER u1 WITH DEFAULT_ROLE = 'role1', TENANTSETTING;"#,
106+
r#"CREATE USER u1 IDENTIFIED BY '123456' WITH DEFAULT_ROLE='role123', TENANTSETTING"#,
103107
r#"DROP database if exists db1;"#,
104108
r#"select distinct a, count(*) from t where a = 1 and b - 1 < a group by a having a = 1;"#,
105109
r#"select * from t4;"#,

common/ast/tests/it/testdata/statement.txt

+107-2
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,111 @@ CreateTable(
14311431
)
14321432

14331433

1434+
---------- Input ----------
1435+
ALTER USER u1 IDENTIFIED BY '123456';
1436+
---------- Output ---------
1437+
ALTER USER 'u1'@'%' IDENTIFIED BY '123456'
1438+
---------- AST ------------
1439+
AlterUser(
1440+
AlterUserStmt {
1441+
user: Some(
1442+
UserIdentity {
1443+
username: "u1",
1444+
hostname: "%",
1445+
},
1446+
),
1447+
auth_option: Some(
1448+
AuthOption {
1449+
auth_type: None,
1450+
password: Some(
1451+
"123456",
1452+
),
1453+
},
1454+
),
1455+
user_options: [],
1456+
},
1457+
)
1458+
1459+
1460+
---------- Input ----------
1461+
ALTER USER u1 WITH DEFAULT_ROLE = 'role1';
1462+
---------- Output ---------
1463+
ALTER USER 'u1'@'%' WITH DEFAULT_ROLE = 'role1'
1464+
---------- AST ------------
1465+
AlterUser(
1466+
AlterUserStmt {
1467+
user: Some(
1468+
UserIdentity {
1469+
username: "u1",
1470+
hostname: "%",
1471+
},
1472+
),
1473+
auth_option: None,
1474+
user_options: [
1475+
DefaultRole(
1476+
"role1",
1477+
),
1478+
],
1479+
},
1480+
)
1481+
1482+
1483+
---------- Input ----------
1484+
ALTER USER u1 WITH DEFAULT_ROLE = 'role1', TENANTSETTING;
1485+
---------- Output ---------
1486+
ALTER USER 'u1'@'%' WITH DEFAULT_ROLE = 'role1' TENANTSETTING
1487+
---------- AST ------------
1488+
AlterUser(
1489+
AlterUserStmt {
1490+
user: Some(
1491+
UserIdentity {
1492+
username: "u1",
1493+
hostname: "%",
1494+
},
1495+
),
1496+
auth_option: None,
1497+
user_options: [
1498+
DefaultRole(
1499+
"role1",
1500+
),
1501+
TenantSetting(
1502+
true,
1503+
),
1504+
],
1505+
},
1506+
)
1507+
1508+
1509+
---------- Input ----------
1510+
CREATE USER u1 IDENTIFIED BY '123456' WITH DEFAULT_ROLE='role123', TENANTSETTING
1511+
---------- Output ---------
1512+
CREATE USER 'u1'@'%' IDENTIFIED BY '123456' WITH DEFAULT_ROLE = 'role123' TENANTSETTING
1513+
---------- AST ------------
1514+
CreateUser(
1515+
CreateUserStmt {
1516+
if_not_exists: false,
1517+
user: UserIdentity {
1518+
username: "u1",
1519+
hostname: "%",
1520+
},
1521+
auth_option: AuthOption {
1522+
auth_type: None,
1523+
password: Some(
1524+
"123456",
1525+
),
1526+
},
1527+
user_options: [
1528+
DefaultRole(
1529+
"role123",
1530+
),
1531+
TenantSetting(
1532+
true,
1533+
),
1534+
],
1535+
},
1536+
)
1537+
1538+
14341539
---------- Input ----------
14351540
DROP database if exists db1;
14361541
---------- Output ---------
@@ -4554,7 +4659,7 @@ CreateUser(
45544659
"password",
45554660
),
45564661
},
4557-
role_options: [],
4662+
user_options: [],
45584663
},
45594664
)
45604665

@@ -4594,7 +4699,7 @@ AlterUser(
45944699
),
45954700
},
45964701
),
4597-
role_options: [],
4702+
user_options: [],
45984703
},
45994704
)
46004705

common/meta/types/src/user_info.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,49 @@ impl TryFrom<Vec<u8>> for UserInfo {
9494
#[serde(default)]
9595
pub struct UserOption {
9696
flags: BitFlags<UserOptionFlag>,
97+
98+
default_role: Option<String>,
9799
}
98100

99101
impl UserOption {
100102
pub fn new(flags: BitFlags<UserOptionFlag>) -> Self {
101-
Self { flags }
103+
Self {
104+
flags,
105+
default_role: None,
106+
}
107+
}
108+
109+
pub fn empty() -> Self {
110+
Default::default()
111+
}
112+
113+
pub fn with_flags(mut self, flags: BitFlags<UserOptionFlag>) -> Self {
114+
self.flags = flags;
115+
self
116+
}
117+
118+
pub fn with_default_role(mut self, default_role: Option<String>) -> Self {
119+
self.default_role = default_role;
120+
self
121+
}
122+
123+
pub fn with_set_flag(mut self, flag: UserOptionFlag) -> Self {
124+
self.flags.insert(flag);
125+
self
102126
}
103127

104128
pub fn flags(&self) -> &BitFlags<UserOptionFlag> {
105129
&self.flags
106130
}
107131

132+
pub fn default_role(&self) -> Option<&String> {
133+
self.default_role.as_ref()
134+
}
135+
136+
pub fn set_default_role(&mut self, default_role: Option<String>) {
137+
self.default_role = default_role;
138+
}
139+
108140
pub fn set_all_flag(&mut self) {
109141
self.flags = BitFlags::all();
110142
}
@@ -113,6 +145,14 @@ impl UserOption {
113145
self.flags.insert(flag);
114146
}
115147

148+
pub fn switch_option_flag(&mut self, flag: UserOptionFlag, on: bool) {
149+
if on {
150+
self.flags.insert(flag);
151+
} else {
152+
self.flags.remove(flag);
153+
}
154+
}
155+
116156
pub fn unset_option_flag(&mut self, flag: UserOptionFlag) {
117157
self.flags.remove(flag);
118158
}

0 commit comments

Comments
 (0)