Skip to content

Commit

Permalink
refactor(cubesql): Use LazyLock instead of lazy_static, move more reg…
Browse files Browse the repository at this point in the history
…exps 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
  • Loading branch information
mcheshkov authored Sep 6, 2024
1 parent 1ce30a4 commit acbdcd2
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 84 deletions.
1 change: 0 additions & 1 deletion packages/cubejs-backend-native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions rust/cubenativeutils/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion rust/cubesql/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion rust/cubesql/cubesql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ thiserror = "1.0.50"
cubeclient = { path = "../cubeclient" }
pg-srv = { path = "../pg-srv" }
sqlparser = { git = 'https://github.com/cube-js/sqlparser-rs.git', rev = "6a54d27d3b75a04b9f9cbe309a83078aa54b32fd" }
lazy_static = "1.4.0"
base64 = "0.13.0"
tokio = { version = "^1.35", features = ["full", "rt", "tracing"] }
serde = { version = "^1.0", features = ["derive"] }
Expand Down
29 changes: 18 additions & 11 deletions rust/cubesql/cubesql/src/compile/engine/df/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ use itertools::Itertools;
use regex::{Captures, Regex};
use serde::{Deserialize, Serialize};
use std::{
any::Any, cmp::min, collections::HashMap, convert::TryInto, fmt, future::Future, iter,
pin::Pin, result, sync::Arc,
any::Any,
cmp::min,
collections::HashMap,
convert::TryInto,
fmt,
future::Future,
iter,
pin::Pin,
result,
sync::{Arc, LazyLock},
};

#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -160,12 +168,12 @@ impl SqlQuery {
}

pub fn finalize_query(&mut self, sql_templates: Arc<SqlTemplates>) -> Result<()> {
static REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\$(\d+)\$").unwrap());

let mut params = Vec::new();
let mut rendered_params = HashMap::new();
let regex = Regex::new(r"\$(\d+)\$")
.map_err(|e| DataFusionError::Execution(format!("Can't parse regex: {}", e)))?;
let mut res = Ok(());
let replaced_sql = regex.replace_all(self.sql.as_str(), |c: &Captures<'_>| {
let replaced_sql = REGEX.replace_all(self.sql.as_str(), |c: &Captures<'_>| {
let param = c.get(1).map(|x| x.as_str());
match self.render_param(sql_templates.clone(), param, &rendered_params, params.len()) {
Ok((param_index, param, push_param)) => {
Expand Down Expand Up @@ -260,9 +268,7 @@ pub struct SqlGenerationResult {
pub request: TransportLoadRequestQuery,
}

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

macro_rules! generate_sql_for_timestamp {
(@generic $value:ident, $value_block:expr, $sql_generator:expr, $sql_query:expr) => {
Expand Down Expand Up @@ -950,8 +956,9 @@ impl CubeScanWrapperNode {
ungrouped_scan_node: Option<Arc<CubeScanNode>>,
subqueries: Arc<HashMap<String, String>>,
) -> result::Result<(Vec<AliasedColumn>, SqlQuery), CubeError> {
let non_id_regex = Regex::new(r"[^a-zA-Z0-9_]")
.map_err(|e| CubeError::internal(format!("Can't parse regex: {}", e)))?;
static NON_ID_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"[^a-zA-Z0-9_]").unwrap());

let mut aliased_columns = Vec::new();
for original_expr in exprs {
let expr = if let Some(column_remapping) = column_remapping.as_ref() {
Expand Down Expand Up @@ -1001,7 +1008,7 @@ impl CubeScanWrapperNode {

let alias = if can_rename_columns {
let alias = expr_name(&expr, &schema)?;
let mut truncated_alias = non_id_regex
let mut truncated_alias = NON_ID_REGEX
.replace_all(&alias, "_")
.trim_start_matches("_")
.to_lowercase();
Expand Down
11 changes: 8 additions & 3 deletions rust/cubesql/cubesql/src/compile/engine/udf/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{any::type_name, sync::Arc, thread};
use std::{
any::type_name,
sync::{Arc, LazyLock},
thread,
};

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

pub fn create_quote_ident_udf() -> ScalarUDF {
static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^[a-z_][a-z0-9_]*$").unwrap());

let fun = make_scalar_function(move |args: &[ArrayRef]| {
assert!(args.len() == 1);

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

let re = Regex::new(r"^[a-z_][a-z0-9_]*$").unwrap();
let result = idents
.iter()
.map(|ident| {
ident.map(|ident| {
if re.is_match(ident) {
if RE.is_match(ident) {
return ident.to_string();
}
format!("\"{}\"", ident.replace("\"", "\"\""))
Expand Down
21 changes: 13 additions & 8 deletions rust/cubesql/cubesql/src/compile/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, sync::LazyLock};

use regex::Regex;
use sqlparser::{
Expand Down Expand Up @@ -36,9 +36,9 @@ impl Dialect for MySqlDialectWithBackTicks {
}
}

lazy_static! {
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();
}
static SIGMA_WORKAROUND: LazyLock<Regex> = LazyLock::new(|| {
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()
});

pub fn parse_sql_to_statements(
query: &String,
Expand Down Expand Up @@ -118,13 +118,18 @@ pub fn parse_sql_to_statements(
// Sigma Computing WITH query workaround
// TODO: remove workaround when subquery is supported in JOIN ON conditions
let query = if SIGMA_WORKAROUND.is_match(&query) {
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();
let relnamespace_replaced = relnamespace_re.replace(
static RELNAMESPACE_RE: LazyLock<Regex> = LazyLock::new(|| {
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()
});
static ATTRELID_RE: LazyLock<Regex> = LazyLock::new(|| {
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()
});

let relnamespace_replaced = RELNAMESPACE_RE.replace(
&query,
"from pg_catalog.pg_class join nsp on relnamespace = nsp.oid where relname = $relname",
);
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();
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");
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");
attrelid_replaced.to_string()
} else {
query
Expand Down
10 changes: 5 additions & 5 deletions rust/cubesql/cubesql/src/compile/rewrite/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ use std::{
collections::{HashMap, HashSet},
env,
ops::Index,
sync::Arc,
sync::{Arc, LazyLock},
};

pub use super::rewriter::CubeRunner;
Expand Down Expand Up @@ -170,8 +170,8 @@ macro_rules! add_plan_list_node {
}};
}

lazy_static! {
static ref EXCLUDED_PARAM_VALUES: HashSet<ScalarValue> = vec![
static EXCLUDED_PARAM_VALUES: LazyLock<HashSet<ScalarValue>> = LazyLock::new(|| {
vec![
ScalarValue::Utf8(Some("second".to_string())),
ScalarValue::Utf8(Some("minute".to_string())),
ScalarValue::Utf8(Some("hour".to_string())),
Expand All @@ -182,8 +182,8 @@ lazy_static! {
]
.into_iter()
.chain((0..50).map(|i| ScalarValue::Int64(Some(i))))
.collect();
}
.collect()
});

pub struct LogicalPlanToLanguageConverter {
graph: EGraph<LogicalPlanLanguage, LogicalPlanAnalysis>,
Expand Down
47 changes: 25 additions & 22 deletions rust/cubesql/cubesql/src/compile/rewrite/rules/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::{
collections::{HashMap, HashSet},
fmt::Display,
ops::{Index, IndexMut},
sync::Arc,
sync::{Arc, LazyLock},
};

pub struct MemberRules {
Expand Down Expand Up @@ -2857,27 +2857,30 @@ pub fn add_member_error(
]))
}

lazy_static! {
static ref STANDARD_GRANULARITIES_PARENTS: HashMap<&'static str, Vec<&'static str>> = [
(
"year",
vec!["year", "quarter", "month", "day", "hour", "minute", "second"]
),
(
"quarter",
vec!["quarter", "month", "day", "hour", "minute", "second"]
),
("month", vec!["month", "day", "hour", "minute", "second"]),
("week", vec!["week", "day", "hour", "minute", "second"]),
("day", vec!["day", "hour", "minute", "second"]),
("hour", vec!["hour", "minute", "second"]),
("minute", vec!["minute", "second"]),
("second", vec!["second"]),
]
.iter()
.cloned()
.collect();
}
static STANDARD_GRANULARITIES_PARENTS: LazyLock<HashMap<&'static str, Vec<&'static str>>> =
LazyLock::new(|| {
[
(
"year",
vec![
"year", "quarter", "month", "day", "hour", "minute", "second",
],
),
(
"quarter",
vec!["quarter", "month", "day", "hour", "minute", "second"],
),
("month", vec!["month", "day", "hour", "minute", "second"]),
("week", vec!["week", "day", "hour", "minute", "second"]),
("day", vec!["day", "hour", "minute", "second"]),
("hour", vec!["hour", "minute", "second"]),
("minute", vec!["minute", "second"]),
("second", vec!["second"]),
]
.iter()
.cloned()
.collect()
});

pub fn min_granularity(granularity_a: &String, granularity_b: &String) -> Option<String> {
let granularity_a = granularity_a.to_lowercase();
Expand Down
11 changes: 3 additions & 8 deletions rust/cubesql/cubesql/src/compile/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,14 +915,10 @@ impl TestContext {
}
}

lazy_static! {
pub static ref TEST_LOGGING_INITIALIZED: std::sync::RwLock<bool> =
std::sync::RwLock::new(false);
}
static TEST_LOGGING_INITIALIZED: std::sync::Once = std::sync::Once::new();

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

log::set_boxed_logger(Box::new(logger)).unwrap();
log::set_max_level(log_level.to_level_filter());
*initialized = true;
}
});
}

pub async fn convert_select_to_query_plan_customized(
Expand Down
5 changes: 0 additions & 5 deletions rust/cubesql/cubesql/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,6 @@ impl ConfigObj for ConfigObjImpl {
}
}

lazy_static! {
pub static ref TEST_LOGGING_INITIALIZED: tokio::sync::RwLock<bool> =
tokio::sync::RwLock::new(false);
}

impl Config {
pub fn default() -> Config {
Config {
Expand Down
2 changes: 0 additions & 2 deletions rust/cubesql/cubesql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

// trace_macros!(false);

#[macro_use]
extern crate lazy_static;
extern crate core;

pub mod compile;
Expand Down
12 changes: 5 additions & 7 deletions rust/cubesql/cubesql/src/sql/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use log::trace;
use rand::Rng;
use std::{
collections::HashMap,
sync::{Arc, RwLock as RwLockSync, Weak},
sync::{Arc, LazyLock, RwLock as RwLockSync, Weak},
time::{Duration, SystemTime},
};
use tokio_util::sync::CancellationToken;
Expand All @@ -23,8 +23,6 @@ use crate::{
RWLockAsync,
};

extern crate lazy_static;

#[derive(Debug, Clone)]
pub struct SessionProperties {
user: Option<String>,
Expand All @@ -37,10 +35,10 @@ impl SessionProperties {
}
}

lazy_static! {
static ref POSTGRES_DEFAULT_VARIABLES: DatabaseVariables = postgres_default_session_variables();
static ref MYSQL_DEFAULT_VARIABLES: DatabaseVariables = mysql_default_session_variables();
}
static POSTGRES_DEFAULT_VARIABLES: LazyLock<DatabaseVariables> =
LazyLock::new(postgres_default_session_variables);
static MYSQL_DEFAULT_VARIABLES: LazyLock<DatabaseVariables> =
LazyLock::new(mysql_default_session_variables);

#[derive(Debug)]
pub enum TransactionState {
Expand Down
14 changes: 8 additions & 6 deletions rust/cubesql/cubesql/src/telemetry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::{compile::DatabaseProtocolDetails, sql::SessionState, CubeError};
use arc_swap::ArcSwap;
use log::{Level, LevelFilter};
use std::{collections::HashMap, fmt::Debug, sync::Arc};

lazy_static! {
static ref REPORTER: ArcSwap<Box<dyn LogReporter>> =
ArcSwap::from_pointee(Box::new(LocalReporter::new()));
}
use std::{
collections::HashMap,
fmt::Debug,
sync::{Arc, LazyLock},
};

static REPORTER: LazyLock<ArcSwap<Box<dyn LogReporter>>> =
LazyLock::new(|| ArcSwap::from_pointee(Box::new(LocalReporter::new())));

pub trait LogReporter: Send + Sync + Debug {
fn log(&self, event: String, properties: HashMap<String, String>, level: Level);
Expand Down
2 changes: 0 additions & 2 deletions rust/cubesqlplanner/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit acbdcd2

Please sign in to comment.