From 9fbd87ff03537c21e1860bfd6fa84eeb2a70d851 Mon Sep 17 00:00:00 2001 From: Jonah Gao Date: Thu, 5 Dec 2024 00:54:14 +0800 Subject: [PATCH] refactor: replace `OnceLock` with `LazyLock` (#13641) * refactor: Replace OnceLock with LazyLock * Fix typo --- datafusion/common/src/types/builtin.rs | 8 ++++---- datafusion/expr/src/logical_plan/statement.rs | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/datafusion/common/src/types/builtin.rs b/datafusion/common/src/types/builtin.rs index c6105d37c3bd..ec69db790377 100644 --- a/datafusion/common/src/types/builtin.rs +++ b/datafusion/common/src/types/builtin.rs @@ -16,17 +16,17 @@ // under the License. use crate::types::{LogicalTypeRef, NativeType}; -use std::sync::{Arc, OnceLock}; +use std::sync::{Arc, LazyLock}; macro_rules! singleton { ($name:ident, $getter:ident, $ty:ident) => { - // TODO: Use LazyLock instead of getter function when MSRV gets bumped - static $name: OnceLock = OnceLock::new(); + static $name: LazyLock = + LazyLock::new(|| Arc::new(NativeType::$ty)); #[doc = "Getter for singleton instance of a logical type representing"] #[doc = concat!("[`NativeType::", stringify!($ty), "`].")] pub fn $getter() -> LogicalTypeRef { - Arc::clone($name.get_or_init(|| Arc::new(NativeType::$ty))) + Arc::clone(&$name) } }; } diff --git a/datafusion/expr/src/logical_plan/statement.rs b/datafusion/expr/src/logical_plan/statement.rs index a8b53e8a1fb1..93be04c27564 100644 --- a/datafusion/expr/src/logical_plan/statement.rs +++ b/datafusion/expr/src/logical_plan/statement.rs @@ -18,14 +18,10 @@ use arrow::datatypes::DataType; use datafusion_common::{DFSchema, DFSchemaRef}; use std::fmt::{self, Display}; -use std::sync::{Arc, OnceLock}; +use std::sync::{Arc, LazyLock}; use crate::{expr_vec_fmt, Expr, LogicalPlan}; -/// Statements have a unchanging empty schema. -/// TODO: Use `LazyLock` when MSRV is 1.80.0 -static STATEMENT_EMPTY_SCHEMA: OnceLock = OnceLock::new(); - /// Various types of Statements. /// /// # Transactions: @@ -54,7 +50,11 @@ pub enum Statement { impl Statement { /// Get a reference to the logical plan's schema pub fn schema(&self) -> &DFSchemaRef { - STATEMENT_EMPTY_SCHEMA.get_or_init(|| Arc::new(DFSchema::empty())) + // Statements have an unchanging empty schema. + static STATEMENT_EMPTY_SCHEMA: LazyLock = + LazyLock::new(|| Arc::new(DFSchema::empty())); + + &STATEMENT_EMPTY_SCHEMA } /// Return a descriptive string describing the type of this