Skip to content

Commit d937fcc

Browse files
authored
feat(query): UserOption add workload_group (#17913)
* feat(query): UserOption add workload_group * add more test
1 parent a9fb5dd commit d937fcc

File tree

24 files changed

+295
-6
lines changed

24 files changed

+295
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/meta/app/src/principal/user_info.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ pub struct UserOption {
191191
default_role: Option<String>,
192192
network_policy: Option<String>,
193193
password_policy: Option<String>,
194+
workload_group: Option<String>,
194195
disabled: Option<bool>,
195196
must_change_password: Option<bool>,
196197
}
@@ -202,6 +203,7 @@ impl UserOption {
202203
default_role: None,
203204
network_policy: None,
204205
password_policy: None,
206+
workload_group: None,
205207
disabled: None,
206208
must_change_password: None,
207209
}
@@ -231,6 +233,11 @@ impl UserOption {
231233
self
232234
}
233235

236+
pub fn with_workload_group(mut self, workload_group: Option<String>) -> Self {
237+
self.workload_group = workload_group;
238+
self
239+
}
240+
234241
pub fn with_disabled(mut self, disabled: Option<bool>) -> Self {
235242
self.disabled = disabled;
236243
self
@@ -262,6 +269,11 @@ impl UserOption {
262269
self.password_policy.as_ref()
263270
}
264271

272+
// This is workload_group id
273+
pub fn workload_group(&self) -> Option<&String> {
274+
self.workload_group.as_ref()
275+
}
276+
265277
pub fn disabled(&self) -> Option<&bool> {
266278
self.disabled.as_ref()
267279
}
@@ -330,6 +342,8 @@ impl UserOption {
330342
UserOptionItem::UnsetPasswordPolicy => self.password_policy = None,
331343
UserOptionItem::Disabled(v) => self.disabled = Some(*v),
332344
UserOptionItem::MustChangePassword(v) => self.must_change_password = Some(*v),
345+
UserOptionItem::SetWorkloadGroup(v) => self.workload_group = Some(v.clone()),
346+
UserOptionItem::UnsetWorkloadGroup => self.workload_group = None,
333347
}
334348
}
335349
}

src/meta/proto-conv/src/user_from_to_protobuf_impl.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ impl FromToProto for mt::principal::UserOption {
103103
.with_default_role(p.default_role)
104104
.with_network_policy(p.network_policy)
105105
.with_password_policy(p.password_policy)
106+
.with_workload_group(p.workload_group)
106107
.with_disabled(p.disabled)
107108
.with_must_change_password(p.must_change_password))
108109
}
@@ -115,6 +116,7 @@ impl FromToProto for mt::principal::UserOption {
115116
default_role: self.default_role().cloned(),
116117
network_policy: self.network_policy().cloned(),
117118
password_policy: self.password_policy().cloned(),
119+
workload_group: self.workload_group().cloned(),
118120
disabled: self.disabled().cloned(),
119121
must_change_password: self.must_change_password().cloned(),
120122
})

src/meta/proto-conv/src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ const META_CHANGE_LOG: &[(u64, &str)] = &[
156156
(124, "2025-04-01: Add: add headers in udf.proto/UDFServer"),
157157
(125, "2025-04-16: Add: add index_type in table.proto/TableIndex"),
158158
(126, "2025-05-11: Add: add iceberg Storage catalog type and add address field in Glue catalog"),
159+
(127, "2025-05-18: Add: UserOption::workload_group"),
159160
// Dear developer:
160161
// If you're gonna add a new metadata version, you'll have to add a test for it.
161162
// You could just copy an existing test file(e.g., `../tests/it/v024_table_meta.rs`)

src/meta/proto-conv/tests/it/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,4 @@ mod v123_parquet_format_params;
118118
mod v124_udf_server_headers;
119119
mod v125_table_index;
120120
mod v126_iceberg_storage_catalog_option;
121+
mod v127_user_option_workload_group;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2023 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use fastrace::func_name;
16+
17+
use crate::common;
18+
19+
// These bytes are built when a new version in introduced,
20+
// and are kept for backward compatibility test.
21+
//
22+
// *************************************************************
23+
// * These messages should never be updated, *
24+
// * only be added when a new version is added, *
25+
// * or be removed when an old version is no longer supported. *
26+
// *************************************************************
27+
//
28+
// The message bytes are built from the output of `test_pb_from_to()`
29+
#[test]
30+
fn test_decode_v127_user_iption() -> anyhow::Result<()> {
31+
let user_option_v127 = vec![
32+
8, 1, 18, 5, 114, 111, 108, 101, 49, 26, 8, 109, 121, 112, 111, 108, 105, 99, 121, 58, 4,
33+
109, 121, 119, 103, 160, 6, 127, 168, 6, 24,
34+
];
35+
36+
let want = || {
37+
databend_common_meta_app::principal::UserOption::default()
38+
.with_set_flag(databend_common_meta_app::principal::UserOptionFlag::TenantSetting)
39+
.with_default_role(Some("role1".into()))
40+
.with_network_policy(Some("mypolicy".to_string()))
41+
.with_workload_group(Some("mywg".to_string()))
42+
};
43+
44+
common::test_pb_from_to(func_name!(), want())?;
45+
common::test_load_old(func_name!(), user_option_v127.as_slice(), 127, want())
46+
}

src/meta/protos/proto/user.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ message UserOption {
128128
optional string password_policy = 4;
129129
optional bool disabled = 5;
130130
optional bool must_change_password = 6;
131+
optional string workload_group = 7;
131132
}
132133

133134
message UserInfo {

src/query/ast/src/ast/statements/user.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ pub enum UserOptionItem {
266266
SetPasswordPolicy(String),
267267
UnsetPasswordPolicy,
268268
MustChangePassword(bool),
269+
SetWorkloadGroup(String),
270+
UnsetWorkloadGroup,
269271
}
270272

271273
impl Display for UserOptionItem {
@@ -277,6 +279,8 @@ impl Display for UserOptionItem {
277279
UserOptionItem::SetNetworkPolicy(v) => write!(f, "SET NETWORK POLICY = '{}'", v),
278280
UserOptionItem::UnsetNetworkPolicy => write!(f, "UNSET NETWORK POLICY"),
279281
UserOptionItem::SetPasswordPolicy(v) => write!(f, "SET PASSWORD POLICY = '{}'", v),
282+
UserOptionItem::SetWorkloadGroup(v) => write!(f, "SET WORKLOAD GROUP = '{}'", v),
283+
UserOptionItem::UnsetWorkloadGroup => write!(f, "UNSET WORKLOAD GROUP"),
280284
UserOptionItem::UnsetPasswordPolicy => write!(f, "UNSET PASSWORD POLICY"),
281285
UserOptionItem::Disabled(v) => write!(f, "DISABLED = {}", v),
282286
UserOptionItem::MustChangePassword(v) => write!(f, "MUST_CHANGE_PASSWORD = {}", v),

src/query/ast/src/parser/statement.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4630,6 +4630,18 @@ pub fn user_option(i: Input) -> IResult<UserOptionItem> {
46304630
},
46314631
|(_, _, val)| UserOptionItem::MustChangePassword(val),
46324632
);
4633+
let set_workload_group = map(
4634+
rule! {
4635+
SET ~ WORKLOAD ~ ^GROUP ~ ^"=" ~ ^#literal_string
4636+
},
4637+
|(_, _, _, _, wg)| UserOptionItem::SetWorkloadGroup(wg),
4638+
);
4639+
let unset_workload_group = map(
4640+
rule! {
4641+
UNSET ~ WORKLOAD ~ ^GROUP
4642+
},
4643+
|(_, _, _)| UserOptionItem::UnsetWorkloadGroup,
4644+
);
46334645

46344646
rule!(
46354647
#tenant_setting
@@ -4641,6 +4653,8 @@ pub fn user_option(i: Input) -> IResult<UserOptionItem> {
46414653
| #unset_password_policy
46424654
| #set_disabled_option
46434655
| #must_change_password
4656+
| #set_workload_group
4657+
| #unset_workload_group
46444658
)(i)
46454659
}
46464660

src/query/ast/tests/it/parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,9 @@ fn test_statement() {
194194
r#"ALTER USER u1 WITH DEFAULT_ROLE = role1, DISABLED=true, TENANTSETTING;"#,
195195
r#"ALTER USER u1 WITH SET NETWORK POLICY = 'policy1';"#,
196196
r#"ALTER USER u1 WITH UNSET NETWORK POLICY;"#,
197+
r#"CREATE USER u1 IDENTIFIED BY '123456' WITH SET WORKLOAD GROUP='W1'"#,
198+
r#"ALTER USER u1 WITH SET WORKLOAD GROUP = 'W1';"#,
199+
r#"ALTER USER u1 WITH UNSET WORKLOAD GROUP;"#,
197200
r#"CREATE USER u1 IDENTIFIED BY '123456' WITH DEFAULT_ROLE='role123', TENANTSETTING"#,
198201
r#"CREATE USER u1 IDENTIFIED BY '123456' WITH SET NETWORK POLICY='policy1'"#,
199202
r#"CREATE USER u1 IDENTIFIED BY '123456' WITH disabled=true"#,

0 commit comments

Comments
 (0)