From be59d5456dc9ae581019c4232ce9a7f545469835 Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Wed, 13 Dec 2023 13:04:54 +0800 Subject: [PATCH 1/3] alias --- src/common/proc_macro/src/session_config.rs | 19 ++++++++++++++++ src/common/src/session_config/mod.rs | 24 ++++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/common/proc_macro/src/session_config.rs b/src/common/proc_macro/src/session_config.rs index 0ca0b1c81f2f..d93b04dfc9fc 100644 --- a/src/common/proc_macro/src/session_config.rs +++ b/src/common/proc_macro/src/session_config.rs @@ -21,6 +21,7 @@ use syn::DeriveInput; #[derive(FromAttributes)] struct Parameter { pub rename: Option, + pub alias: Option, pub default: syn::Expr, pub flags: Option, pub check_hook: Option, @@ -39,6 +40,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream { let mut get_match_branches = vec![]; let mut reset_match_branches = vec![]; let mut show_all_list = vec![]; + let mut alias_to_entry_name_branches = vec![]; for field in fields { let field_ident = field.ident.expect_or_abort("Field need to be named"); @@ -62,6 +64,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream { Parameter::from_attributes(&field.attrs).expect_or_abort("Failed to parse attribute"); let Parameter { rename, + alias, default, flags, check_hook: check_hook_name, @@ -78,6 +81,12 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream { quote! {stringify!(#ident)} }; + if let Some(alias) = alias { + alias_to_entry_name_branches.push(quote! { + #alias => #entry_name.to_string(), + }) + } + let flags = flags.map(|f| f.value()).unwrap_or_default(); let flags: Vec<_> = flags.split('|').map(|str| str.trim()).collect(); @@ -236,6 +245,13 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream { Default::default() } + fn alias_to_entry_name(key_name: &str) -> String { + match key_name { + #(#alias_to_entry_name_branches)* + _ => key_name.to_string(), + } + } + #(#struct_impl_get)* #(#struct_impl_set)* @@ -244,6 +260,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream { /// Set a parameter given it's name and value string. pub fn set(&mut self, key_name: &str, value: String, reporter: &mut impl ConfigReporter) -> SessionConfigResult<()> { + let key_name = Self::alias_to_entry_name(key_name); match key_name.to_ascii_lowercase().as_ref() { #(#set_match_branches)* _ => Err(SessionConfigError::UnrecognizedEntry(key_name.to_string())), @@ -252,6 +269,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream { /// Get a parameter by it's name. pub fn get(&self, key_name: &str) -> SessionConfigResult { + let key_name = Self::alias_to_entry_name(key_name); match key_name.to_ascii_lowercase().as_ref() { #(#get_match_branches)* _ => Err(SessionConfigError::UnrecognizedEntry(key_name.to_string())), @@ -260,6 +278,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream { /// Reset a parameter by it's name. pub fn reset(&mut self, key_name: &str, reporter: &mut impl ConfigReporter) -> SessionConfigResult<()> { + let key_name = Self::alias_to_entry_name(key_name); match key_name.to_ascii_lowercase().as_ref() { #(#reset_match_branches)* _ => Err(SessionConfigError::UnrecognizedEntry(key_name.to_string())), diff --git a/src/common/src/session_config/mod.rs b/src/common/src/session_config/mod.rs index 82c5bd0a6f12..49587a884bd0 100644 --- a/src/common/src/session_config/mod.rs +++ b/src/common/src/session_config/mod.rs @@ -55,7 +55,7 @@ pub struct ConfigMap { /// If `RW_IMPLICIT_FLUSH` is on, then every INSERT/UPDATE/DELETE statement will block /// until the entire dataflow is refreshed. In other words, every related table & MV will /// be able to see the write. - #[parameter(default = false, rename = "rw_implicit_flush")] + #[parameter(default = false, rename = "rw_implicit_flush", alias = "implicit_flush" | "flush")] implicit_flush: bool, /// If `CREATE_COMPACTION_GROUP_FOR_MV` is on, dedicated compaction groups will be created in @@ -304,3 +304,25 @@ pub trait ConfigReporter { impl ConfigReporter for () { fn report_status(&mut self, _key: &str, _new_val: String) {} } + +#[cfg(test)] +mod test { + use super::*; + + #[derive(SessionConfig)] + struct TestConfig { + #[parameter(default = 1, alias = "test_param_alias" | "alias_param_test")] + test_param: i32, + } + + #[test] + fn test_session_config_alias() { + let mut config = TestConfig::default(); + config.set("test_param", "2".to_string(), &mut ()).unwrap(); + assert_eq!(config.get("test_param_alias").unwrap(), "2"); + config + .set("alias_param_test", "3".to_string(), &mut ()) + .unwrap(); + assert_eq!(config.get("test_param_alias").unwrap(), "3"); + } +} From 2b76887dbbf4550197cab29fa1be811af9afce13 Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Wed, 13 Dec 2023 13:06:51 +0800 Subject: [PATCH 2/3] fix --- src/common/src/session_config/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/src/session_config/mod.rs b/src/common/src/session_config/mod.rs index 49587a884bd0..d5c93944f81a 100644 --- a/src/common/src/session_config/mod.rs +++ b/src/common/src/session_config/mod.rs @@ -55,7 +55,7 @@ pub struct ConfigMap { /// If `RW_IMPLICIT_FLUSH` is on, then every INSERT/UPDATE/DELETE statement will block /// until the entire dataflow is refreshed. In other words, every related table & MV will /// be able to see the write. - #[parameter(default = false, rename = "rw_implicit_flush", alias = "implicit_flush" | "flush")] + #[parameter(default = false, rename = "rw_implicit_flush")] implicit_flush: bool, /// If `CREATE_COMPACTION_GROUP_FOR_MV` is on, dedicated compaction groups will be created in From 65d55107e7222354f4a049edf258af91512c6a9d Mon Sep 17 00:00:00 2001 From: Yuhao Su Date: Wed, 13 Dec 2023 16:02:02 +0800 Subject: [PATCH 3/3] str --- src/common/proc_macro/src/session_config.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common/proc_macro/src/session_config.rs b/src/common/proc_macro/src/session_config.rs index d93b04dfc9fc..aba8fe7043e4 100644 --- a/src/common/proc_macro/src/session_config.rs +++ b/src/common/proc_macro/src/session_config.rs @@ -83,7 +83,7 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream { if let Some(alias) = alias { alias_to_entry_name_branches.push(quote! { - #alias => #entry_name.to_string(), + #alias => #entry_name, }) } @@ -245,10 +245,10 @@ pub(crate) fn derive_config(input: DeriveInput) -> TokenStream { Default::default() } - fn alias_to_entry_name(key_name: &str) -> String { + fn alias_to_entry_name(key_name: &str) -> &str { match key_name { #(#alias_to_entry_name_branches)* - _ => key_name.to_string(), + _ => key_name, } }