Skip to content

Commit acbdcd2

Browse files
authored
refactor(cubesql): Use LazyLock instead of lazy_static, move more regexps to statics (#8675)
* Replace lazy_static with LazyLock in many places * Replace lazy_static with Once for testing logger init * Remove unused testing logging static in config * Move more static regexps to LazyLocks
1 parent 1ce30a4 commit acbdcd2

File tree

15 files changed

+85
-84
lines changed

15 files changed

+85
-84
lines changed

packages/cubejs-backend-native/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.

rust/cubenativeutils/Cargo.lock

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

rust/cubesql/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.

rust/cubesql/cubesql/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ thiserror = "1.0.50"
1616
cubeclient = { path = "../cubeclient" }
1717
pg-srv = { path = "../pg-srv" }
1818
sqlparser = { git = 'https://github.com/cube-js/sqlparser-rs.git', rev = "6a54d27d3b75a04b9f9cbe309a83078aa54b32fd" }
19-
lazy_static = "1.4.0"
2019
base64 = "0.13.0"
2120
tokio = { version = "^1.35", features = ["full", "rt", "tracing"] }
2221
serde = { version = "^1.0", features = ["derive"] }

rust/cubesql/cubesql/src/compile/engine/df/wrapper.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ use itertools::Itertools;
3232
use regex::{Captures, Regex};
3333
use serde::{Deserialize, Serialize};
3434
use std::{
35-
any::Any, cmp::min, collections::HashMap, convert::TryInto, fmt, future::Future, iter,
36-
pin::Pin, result, sync::Arc,
35+
any::Any,
36+
cmp::min,
37+
collections::HashMap,
38+
convert::TryInto,
39+
fmt,
40+
future::Future,
41+
iter,
42+
pin::Pin,
43+
result,
44+
sync::{Arc, LazyLock},
3745
};
3846

3947
#[derive(Debug, Clone, Deserialize)]
@@ -160,12 +168,12 @@ impl SqlQuery {
160168
}
161169

162170
pub fn finalize_query(&mut self, sql_templates: Arc<SqlTemplates>) -> Result<()> {
171+
static REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\$(\d+)\$").unwrap());
172+
163173
let mut params = Vec::new();
164174
let mut rendered_params = HashMap::new();
165-
let regex = Regex::new(r"\$(\d+)\$")
166-
.map_err(|e| DataFusionError::Execution(format!("Can't parse regex: {}", e)))?;
167175
let mut res = Ok(());
168-
let replaced_sql = regex.replace_all(self.sql.as_str(), |c: &Captures<'_>| {
176+
let replaced_sql = REGEX.replace_all(self.sql.as_str(), |c: &Captures<'_>| {
169177
let param = c.get(1).map(|x| x.as_str());
170178
match self.render_param(sql_templates.clone(), param, &rendered_params, params.len()) {
171179
Ok((param_index, param, push_param)) => {
@@ -260,9 +268,7 @@ pub struct SqlGenerationResult {
260268
pub request: TransportLoadRequestQuery,
261269
}
262270

263-
lazy_static! {
264-
static ref DATE_PART_REGEX: Regex = Regex::new("^[A-Za-z_ ]+$").unwrap();
265-
}
271+
static DATE_PART_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new("^[A-Za-z_ ]+$").unwrap());
266272

267273
macro_rules! generate_sql_for_timestamp {
268274
(@generic $value:ident, $value_block:expr, $sql_generator:expr, $sql_query:expr) => {
@@ -950,8 +956,9 @@ impl CubeScanWrapperNode {
950956
ungrouped_scan_node: Option<Arc<CubeScanNode>>,
951957
subqueries: Arc<HashMap<String, String>>,
952958
) -> result::Result<(Vec<AliasedColumn>, SqlQuery), CubeError> {
953-
let non_id_regex = Regex::new(r"[^a-zA-Z0-9_]")
954-
.map_err(|e| CubeError::internal(format!("Can't parse regex: {}", e)))?;
959+
static NON_ID_REGEX: LazyLock<Regex> =
960+
LazyLock::new(|| Regex::new(r"[^a-zA-Z0-9_]").unwrap());
961+
955962
let mut aliased_columns = Vec::new();
956963
for original_expr in exprs {
957964
let expr = if let Some(column_remapping) = column_remapping.as_ref() {
@@ -1001,7 +1008,7 @@ impl CubeScanWrapperNode {
10011008

10021009
let alias = if can_rename_columns {
10031010
let alias = expr_name(&expr, &schema)?;
1004-
let mut truncated_alias = non_id_regex
1011+
let mut truncated_alias = NON_ID_REGEX
10051012
.replace_all(&alias, "_")
10061013
.trim_start_matches("_")
10071014
.to_lowercase();

rust/cubesql/cubesql/src/compile/engine/udf/common.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::{any::type_name, sync::Arc, thread};
1+
use std::{
2+
any::type_name,
3+
sync::{Arc, LazyLock},
4+
thread,
5+
};
26

37
use chrono::{Datelike, Days, Duration, Months, NaiveDate, NaiveDateTime, NaiveTime};
48
use datafusion::{
@@ -3329,17 +3333,18 @@ pub fn create_current_setting_udf() -> ScalarUDF {
33293333
}
33303334

33313335
pub fn create_quote_ident_udf() -> ScalarUDF {
3336+
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-z_][a-z0-9_]*$").unwrap());
3337+
33323338
let fun = make_scalar_function(move |args: &[ArrayRef]| {
33333339
assert!(args.len() == 1);
33343340

33353341
let idents = downcast_string_arg!(args[0], "str", i32);
33363342

3337-
let re = Regex::new(r"^[a-z_][a-z0-9_]*$").unwrap();
33383343
let result = idents
33393344
.iter()
33403345
.map(|ident| {
33413346
ident.map(|ident| {
3342-
if re.is_match(ident) {
3347+
if RE.is_match(ident) {
33433348
return ident.to_string();
33443349
}
33453350
format!("\"{}\"", ident.replace("\"", "\"\""))

rust/cubesql/cubesql/src/compile/parser.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, sync::LazyLock};
22

33
use regex::Regex;
44
use sqlparser::{
@@ -36,9 +36,9 @@ impl Dialect for MySqlDialectWithBackTicks {
3636
}
3737
}
3838

39-
lazy_static! {
40-
static ref SIGMA_WORKAROUND: Regex = Regex::new(r#"(?s)^\s*with\s+nsp\sas\s\(.*nspname\s=\s.*\),\s+tbl\sas\s\(.*relname\s=\s.*\).*select\s+attname.*from\spg_attribute.*$"#).unwrap();
41-
}
39+
static SIGMA_WORKAROUND: LazyLock<Regex> = LazyLock::new(|| {
40+
Regex::new(r#"(?s)^\s*with\s+nsp\sas\s\(.*nspname\s=\s.*\),\s+tbl\sas\s\(.*relname\s=\s.*\).*select\s+attname.*from\spg_attribute.*$"#).unwrap()
41+
});
4242

4343
pub fn parse_sql_to_statements(
4444
query: &String,
@@ -118,13 +118,18 @@ pub fn parse_sql_to_statements(
118118
// Sigma Computing WITH query workaround
119119
// TODO: remove workaround when subquery is supported in JOIN ON conditions
120120
let query = if SIGMA_WORKAROUND.is_match(&query) {
121-
let relnamespace_re = Regex::new(r#"(?s)from\spg_catalog\.pg_class\s+where\s+relname\s=\s(?P<relname>'(?:[^']|'')+'|\$\d+)\s+and\s+relnamespace\s=\s\(select\soid\sfrom\snsp\)"#).unwrap();
122-
let relnamespace_replaced = relnamespace_re.replace(
121+
static RELNAMESPACE_RE: LazyLock<Regex> = LazyLock::new(|| {
122+
Regex::new(r#"(?s)from\spg_catalog\.pg_class\s+where\s+relname\s=\s(?P<relname>'(?:[^']|'')+'|\$\d+)\s+and\s+relnamespace\s=\s\(select\soid\sfrom\snsp\)"#).unwrap()
123+
});
124+
static ATTRELID_RE: LazyLock<Regex> = LazyLock::new(|| {
125+
Regex::new(r#"(?s)left\sjoin\spg_description\son\s+attrelid\s=\sobjoid\sand\s+attnum\s=\sobjsubid\s+where\s+attnum\s>\s0\s+and\s+attrelid\s=\s\(select\soid\sfrom\stbl\)"#).unwrap()
126+
});
127+
128+
let relnamespace_replaced = RELNAMESPACE_RE.replace(
123129
&query,
124130
"from pg_catalog.pg_class join nsp on relnamespace = nsp.oid where relname = $relname",
125131
);
126-
let attrelid_re = Regex::new(r#"(?s)left\sjoin\spg_description\son\s+attrelid\s=\sobjoid\sand\s+attnum\s=\sobjsubid\s+where\s+attnum\s>\s0\s+and\s+attrelid\s=\s\(select\soid\sfrom\stbl\)"#).unwrap();
127-
let attrelid_replaced = attrelid_re.replace(&relnamespace_replaced, "left join pg_description on attrelid = objoid and attnum = objsubid join tbl on attrelid = tbl.oid where attnum > 0");
132+
let attrelid_replaced = ATTRELID_RE.replace(&relnamespace_replaced, "left join pg_description on attrelid = objoid and attnum = objsubid join tbl on attrelid = tbl.oid where attnum > 0");
128133
attrelid_replaced.to_string()
129134
} else {
130135
query

rust/cubesql/cubesql/src/compile/rewrite/converter.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ use std::{
5959
collections::{HashMap, HashSet},
6060
env,
6161
ops::Index,
62-
sync::Arc,
62+
sync::{Arc, LazyLock},
6363
};
6464

6565
pub use super::rewriter::CubeRunner;
@@ -170,8 +170,8 @@ macro_rules! add_plan_list_node {
170170
}};
171171
}
172172

173-
lazy_static! {
174-
static ref EXCLUDED_PARAM_VALUES: HashSet<ScalarValue> = vec![
173+
static EXCLUDED_PARAM_VALUES: LazyLock<HashSet<ScalarValue>> = LazyLock::new(|| {
174+
vec![
175175
ScalarValue::Utf8(Some("second".to_string())),
176176
ScalarValue::Utf8(Some("minute".to_string())),
177177
ScalarValue::Utf8(Some("hour".to_string())),
@@ -182,8 +182,8 @@ lazy_static! {
182182
]
183183
.into_iter()
184184
.chain((0..50).map(|i| ScalarValue::Int64(Some(i))))
185-
.collect();
186-
}
185+
.collect()
186+
});
187187

188188
pub struct LogicalPlanToLanguageConverter {
189189
graph: EGraph<LogicalPlanLanguage, LogicalPlanAnalysis>,

rust/cubesql/cubesql/src/compile/rewrite/rules/members.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use std::{
4646
collections::{HashMap, HashSet},
4747
fmt::Display,
4848
ops::{Index, IndexMut},
49-
sync::Arc,
49+
sync::{Arc, LazyLock},
5050
};
5151

5252
pub struct MemberRules {
@@ -2857,27 +2857,30 @@ pub fn add_member_error(
28572857
]))
28582858
}
28592859

2860-
lazy_static! {
2861-
static ref STANDARD_GRANULARITIES_PARENTS: HashMap<&'static str, Vec<&'static str>> = [
2862-
(
2863-
"year",
2864-
vec!["year", "quarter", "month", "day", "hour", "minute", "second"]
2865-
),
2866-
(
2867-
"quarter",
2868-
vec!["quarter", "month", "day", "hour", "minute", "second"]
2869-
),
2870-
("month", vec!["month", "day", "hour", "minute", "second"]),
2871-
("week", vec!["week", "day", "hour", "minute", "second"]),
2872-
("day", vec!["day", "hour", "minute", "second"]),
2873-
("hour", vec!["hour", "minute", "second"]),
2874-
("minute", vec!["minute", "second"]),
2875-
("second", vec!["second"]),
2876-
]
2877-
.iter()
2878-
.cloned()
2879-
.collect();
2880-
}
2860+
static STANDARD_GRANULARITIES_PARENTS: LazyLock<HashMap<&'static str, Vec<&'static str>>> =
2861+
LazyLock::new(|| {
2862+
[
2863+
(
2864+
"year",
2865+
vec![
2866+
"year", "quarter", "month", "day", "hour", "minute", "second",
2867+
],
2868+
),
2869+
(
2870+
"quarter",
2871+
vec!["quarter", "month", "day", "hour", "minute", "second"],
2872+
),
2873+
("month", vec!["month", "day", "hour", "minute", "second"]),
2874+
("week", vec!["week", "day", "hour", "minute", "second"]),
2875+
("day", vec!["day", "hour", "minute", "second"]),
2876+
("hour", vec!["hour", "minute", "second"]),
2877+
("minute", vec!["minute", "second"]),
2878+
("second", vec!["second"]),
2879+
]
2880+
.iter()
2881+
.cloned()
2882+
.collect()
2883+
});
28812884

28822885
pub fn min_granularity(granularity_a: &String, granularity_b: &String) -> Option<String> {
28832886
let granularity_a = granularity_a.to_lowercase();

rust/cubesql/cubesql/src/compile/test/mod.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -915,14 +915,10 @@ impl TestContext {
915915
}
916916
}
917917

918-
lazy_static! {
919-
pub static ref TEST_LOGGING_INITIALIZED: std::sync::RwLock<bool> =
920-
std::sync::RwLock::new(false);
921-
}
918+
static TEST_LOGGING_INITIALIZED: std::sync::Once = std::sync::Once::new();
922919

923920
pub fn init_testing_logger() {
924-
let mut initialized = TEST_LOGGING_INITIALIZED.write().unwrap();
925-
if !*initialized {
921+
TEST_LOGGING_INITIALIZED.call_once(|| {
926922
let log_level = log::Level::Trace;
927923
let logger = simple_logger::SimpleLogger::new()
928924
.with_level(log::Level::Error.to_level_filter())
@@ -933,8 +929,7 @@ pub fn init_testing_logger() {
933929

934930
log::set_boxed_logger(Box::new(logger)).unwrap();
935931
log::set_max_level(log_level.to_level_filter());
936-
*initialized = true;
937-
}
932+
});
938933
}
939934

940935
pub async fn convert_select_to_query_plan_customized(

0 commit comments

Comments
 (0)