Skip to content

Commit

Permalink
chore: remove lazy_static in favor of LazyLock (delta-io#365)
Browse files Browse the repository at this point in the history
Turns out we don't use lazy static in many places so potentially
removing the dependency is straight forward. Lazy static itself even
gives an example from the standard library.


https://github.com/rust-lang-nursery/lazy-static.rs?tab=readme-ov-file#standard-library

---------

Co-authored-by: Ryan Johnson <[email protected]>
  • Loading branch information
2 people authored and nicklan committed Oct 2, 2024
1 parent 4c214c8 commit 0968281
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 44 deletions.
1 change: 0 additions & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ either = "1.13"
fix-hidden-lifetime-bug = "0.2"
indexmap = "2.5.0"
itertools = "0.13"
lazy_static = "1.5"
roaring = "0.10.6"
serde = { version = "1", features = ["derive", "rc"] }
serde_json = "1"
Expand Down
33 changes: 15 additions & 18 deletions kernel/src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ pub mod deletion_vector;
pub(crate) mod schemas;
pub(crate) mod visitors;

use std::collections::HashMap;

use delta_kernel_derive::Schema;
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::LazyLock;
use visitors::{AddVisitor, MetadataVisitor, ProtocolVisitor};

use self::deletion_vector::DeletionVectorDescriptor;
Expand All @@ -23,21 +22,19 @@ pub(crate) const METADATA_NAME: &str = "metaData";
pub(crate) const PROTOCOL_NAME: &str = "protocol";
pub(crate) const TRANSACTION_NAME: &str = "txn";

lazy_static! {
static ref LOG_SCHEMA: StructType = StructType::new(
vec![
Option::<Add>::get_struct_field(ADD_NAME),
Option::<Remove>::get_struct_field(REMOVE_NAME),
Option::<Metadata>::get_struct_field(METADATA_NAME),
Option::<Protocol>::get_struct_field(PROTOCOL_NAME),
Option::<Transaction>::get_struct_field(TRANSACTION_NAME),
// We don't support the following actions yet
//Option<Cdc>::get_field(CDC_NAME),
//Option<CommitInfo>::get_field(COMMIT_INFO_NAME),
//Option<DomainMetadata>::get_field(DOMAIN_METADATA_NAME),
]
);
}
static LOG_SCHEMA: LazyLock<StructType> = LazyLock::new(|| {
StructType::new(vec![
Option::<Add>::get_struct_field(ADD_NAME),
Option::<Remove>::get_struct_field(REMOVE_NAME),
Option::<Metadata>::get_struct_field(METADATA_NAME),
Option::<Protocol>::get_struct_field(PROTOCOL_NAME),
Option::<Transaction>::get_struct_field(TRANSACTION_NAME),
// We don't support the following actions yet
//Option<Cdc>::get_field(CDC_NAME),
//Option<CommitInfo>::get_field(COMMIT_INFO_NAME),
//Option<DomainMetadata>::get_field(DOMAIN_METADATA_NAME),
])
});

pub(crate) fn get_log_schema() -> &'static StructType {
&LOG_SCHEMA
Expand Down
9 changes: 4 additions & 5 deletions kernel/src/expressions/scalars.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::{DateTime, NaiveDate, NaiveDateTime, TimeZone, Utc};
use std::cmp::Ordering;
use std::fmt::{Display, Formatter};

use chrono::{DateTime, NaiveDate, NaiveDateTime, TimeZone, Utc};
use std::sync::LazyLock;

use crate::schema::{ArrayType, DataType, PrimitiveType, StructField};
use crate::utils::require;
Expand Down Expand Up @@ -296,9 +296,8 @@ impl PrimitiveType {
pub fn parse_scalar(&self, raw: &str) -> Result<Scalar, Error> {
use PrimitiveType::*;

lazy_static::lazy_static! {
static ref UNIX_EPOCH: DateTime<Utc> = DateTime::from_timestamp(0, 0).unwrap();
}
static UNIX_EPOCH: LazyLock<DateTime<Utc>> =
LazyLock::new(|| DateTime::from_timestamp(0, 0).unwrap());

if raw.is_empty() {
return Ok(Scalar::Null(self.data_type()));
Expand Down
15 changes: 7 additions & 8 deletions kernel/src/scan/data_skipping.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;
use std::ops::Not;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use tracing::debug;

Expand Down Expand Up @@ -180,13 +180,12 @@ impl DataSkippingFilter {
table_schema: &SchemaRef,
predicate: &Option<Expr>,
) -> Option<Self> {
lazy_static::lazy_static!(
static ref PREDICATE_SCHEMA: DataType = StructType::new(vec![
StructField::new("predicate", DataType::BOOLEAN, true),
]).into();
static ref STATS_EXPR: Expr = Expr::column("add.stats");
static ref FILTER_EXPR: Expr = Expr::column("predicate").distinct(Expr::literal(false));
);
static PREDICATE_SCHEMA: LazyLock<DataType> = LazyLock::new(|| {
DataType::struct_type(vec![StructField::new("predicate", DataType::BOOLEAN, true)])
});
static STATS_EXPR: LazyLock<Expr> = LazyLock::new(|| Expr::column("add.stats"));
static FILTER_EXPR: LazyLock<Expr> =
LazyLock::new(|| Expr::column("predicate").distinct(Expr::literal(false)));

let predicate = match predicate {
Some(predicate) => predicate,
Expand Down
24 changes: 12 additions & 12 deletions kernel/src/scan/log_replay.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::clone::Clone;
use std::collections::HashSet;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

use either::Either;
use lazy_static::lazy_static;
use tracing::debug;

use super::data_skipping::DataSkippingFilter;
Expand Down Expand Up @@ -77,11 +77,11 @@ impl DataVisitor for AddRemoveVisitor {
}
}

lazy_static! {
// NB: If you update this schema, ensure you update the comment describing it in the doc comment
// for `scan_row_schema` in scan/mod.rs! You'll also need to update ScanFileVisitor as the
// indexes will be off
pub(crate) static ref SCAN_ROW_SCHEMA: Arc<StructType> = Arc::new(StructType::new(vec!(
// NB: If you update this schema, ensure you update the comment describing it in the doc comment
// for `scan_row_schema` in scan/mod.rs! You'll also need to update ScanFileVisitor as the
// indexes will be off
pub(crate) static SCAN_ROW_SCHEMA: LazyLock<Arc<StructType>> = LazyLock::new(|| {
Arc::new(StructType::new(vec![
StructField::new("path", DataType::STRING, false),
StructField::new("size", DataType::LONG, true),
StructField::new("modificationTime", DataType::LONG, true),
Expand All @@ -95,7 +95,7 @@ lazy_static! {
StructField::new("sizeInBytes", DataType::INTEGER, false),
StructField::new("cardinality", DataType::LONG, false),
]),
true
true,
),
StructField::new(
"fileConstantValues",
Expand All @@ -104,11 +104,11 @@ lazy_static! {
MapType::new(DataType::STRING, DataType::STRING, false),
true,
)]),
true
true,
),
)));
static ref SCAN_ROW_DATATYPE: DataType = SCAN_ROW_SCHEMA.as_ref().clone().into();
}
]))
});
static SCAN_ROW_DATATYPE: LazyLock<DataType> = LazyLock::new(|| SCAN_ROW_SCHEMA.clone().into());

impl LogReplayScanner {
/// Create a new [`LogReplayScanner`] instance
Expand Down

0 comments on commit 0968281

Please sign in to comment.