Skip to content

Commit 0642ce1

Browse files
committed
chore(query): make user setting depend on a lightweight UserSettingValue instead of DataValue
1 parent f2265e1 commit 0642ce1

File tree

8 files changed

+137
-104
lines changed

8 files changed

+137
-104
lines changed

Cargo.lock

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

src/meta/types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,6 @@ pub use user_privilege::UserPrivilegeSet;
196196
pub use user_privilege::UserPrivilegeType;
197197
pub use user_quota::UserQuota;
198198
pub use user_setting::UserSetting;
199+
pub use user_setting::UserSettingValue;
199200
pub use user_stage::*;
200201
pub use with::With;

src/meta/types/src/user_setting.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,52 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use common_datavalues::DataValue;
1615
use common_exception::ErrorCode;
1716
use common_exception::Result;
1817
use serde::Deserialize;
1918
use serde::Serialize;
2019

2120
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
22-
#[serde(default)]
2321
pub struct UserSetting {
2422
// The name of the setting.
2523
pub name: String,
2624
// The value of the setting.
27-
pub value: DataValue,
25+
pub value: UserSettingValue,
2826
}
29-
impl UserSetting {
30-
pub fn create(name: &str, value: DataValue) -> UserSetting {
31-
UserSetting {
32-
name: name.to_string(),
33-
value,
27+
28+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
29+
pub enum UserSettingValue {
30+
UInt64(u64),
31+
String(Vec<u8>),
32+
}
33+
34+
impl UserSettingValue {
35+
pub fn as_u64(&self) -> Result<u64> {
36+
match self {
37+
UserSettingValue::UInt64(v) => Ok(*v),
38+
other => Result::Err(ErrorCode::BadDataValueType(format!(
39+
"Unexpected type:{:?} to get u64 number",
40+
other
41+
))),
42+
}
43+
}
44+
45+
pub fn as_string(&self) -> Result<Vec<u8>> {
46+
match self {
47+
UserSettingValue::String(v) => Ok(v.to_owned()),
48+
other => Result::Err(ErrorCode::BadDataValueType(format!(
49+
"Unexpected type:{:?} to get u64 number",
50+
other
51+
))),
3452
}
3553
}
3654
}
37-
impl Default for UserSetting {
38-
fn default() -> Self {
55+
56+
impl UserSetting {
57+
pub fn create(name: &str, value: UserSettingValue) -> UserSetting {
3958
UserSetting {
40-
name: "".to_string(),
41-
value: DataValue::Null,
59+
name: name.to_string(),
60+
value,
4261
}
4362
}
4463
}

src/query/datavalues/tests/it/types/viewer.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,12 @@ fn test_constant_viewer() -> Result<()> {
6868
}
6969
Ok(())
7070
}
71+
72+
#[test]
73+
fn test_json() {
74+
75+
let value = DataValue::Int64(53);
76+
let ss = serde_json::to_vec(&value).unwrap();
77+
println!("{:?}", ss);
78+
println!("{:?}", unsafe { std::str::from_utf8_unchecked(&ss) } );
79+
}

src/query/management/tests/it/setting.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515
use std::sync::Arc;
1616

1717
use common_base::base::tokio;
18-
use common_datavalues::DataValue;
1918
use common_exception::Result;
2019
use common_management::*;
2120
use common_meta_api::KVApi;
2221
use common_meta_embedded::MetaEmbedded;
2322
use common_meta_types::SeqV;
2423
use common_meta_types::UserSetting;
24+
use common_meta_types::UserSettingValue;
25+
use common_meta_types::Value;
2526

2627
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
2728
async fn test_set_setting() -> Result<()> {
2829
let (kv_api, mgr) = new_setting_api().await?;
2930

3031
{
31-
let setting = UserSetting::create("max_threads", DataValue::UInt64(3));
32+
let setting = UserSetting::create("max_threads", UserSettingValue::UInt64(3));
3233
mgr.set_setting(setting.clone()).await?;
3334
let value = kv_api
3435
.get_kv("__fd_settings/databend_query/max_threads")
@@ -48,7 +49,7 @@ async fn test_set_setting() -> Result<()> {
4849

4950
// Set again.
5051
{
51-
let setting = UserSetting::create("max_threads", DataValue::UInt64(1));
52+
let setting = UserSetting::create("max_threads", UserSettingValue::UInt64(1));
5253
mgr.set_setting(setting.clone()).await?;
5354
let value = kv_api
5455
.get_kv("__fd_settings/databend_query/max_threads")
@@ -68,14 +69,14 @@ async fn test_set_setting() -> Result<()> {
6869

6970
// Get settings.
7071
{
71-
let expect = vec![UserSetting::create("max_threads", DataValue::UInt64(1))];
72+
let expect = vec![UserSetting::create("max_threads", UserSettingValue::UInt64(1))];
7273
let actual = mgr.get_settings().await?;
7374
assert_eq!(actual, expect);
7475
}
7576

7677
// Get setting.
7778
{
78-
let expect = UserSetting::create("max_threads", DataValue::UInt64(1));
79+
let expect = UserSetting::create("max_threads", UserSettingValue::UInt64(1));
7980
let actual = mgr.get_setting("max_threads", None).await?;
8081
assert_eq!(actual.data, expect);
8182
}

src/query/settings/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ test = false
1111
[dependencies]
1212
common-ast = { path = "../../query/ast" }
1313
common-config = { path = "../config" }
14-
common-datavalues = { path = "../datavalues" }
1514
common-exception = { path = "../../common/exception" }
1615
common-meta-types = { path = "../../meta/types" }
1716
common-users = { path = "../users" }

0 commit comments

Comments
 (0)