From 8eb54555ea7e26e6d589016bb6e72d6375a35d2d Mon Sep 17 00:00:00 2001 From: Andrew Lamb Date: Tue, 7 Oct 2025 13:08:04 -0400 Subject: [PATCH] Test refactor: avoid creating the same SchemaRef --- .../tests/physical_optimizer/test_utils.rs | 52 ++++++++++++------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/datafusion/core/tests/physical_optimizer/test_utils.rs b/datafusion/core/tests/physical_optimizer/test_utils.rs index b906dfa4b9ae..8ca33f3d4abb 100644 --- a/datafusion/core/tests/physical_optimizer/test_utils.rs +++ b/datafusion/core/tests/physical_optimizer/test_utils.rs @@ -19,7 +19,7 @@ use std::any::Any; use std::fmt::Formatter; -use std::sync::Arc; +use std::sync::{Arc, LazyLock}; use arrow::array::Int32Array; use arrow::compute::SortOptions; @@ -142,37 +142,49 @@ pub(crate) fn parquet_exec_with_stats(file_size: u64) -> Arc { } pub fn schema() -> SchemaRef { - Arc::new(Schema::new(vec![ - Field::new("a", DataType::Int64, true), - Field::new("b", DataType::Int64, true), - Field::new("c", DataType::Int64, true), - Field::new("d", DataType::Int32, true), - Field::new("e", DataType::Boolean, true), - ])) + static SCHEMA: LazyLock = LazyLock::new(|| { + Arc::new(Schema::new(vec![ + Field::new("a", DataType::Int64, true), + Field::new("b", DataType::Int64, true), + Field::new("c", DataType::Int64, true), + Field::new("d", DataType::Int32, true), + Field::new("e", DataType::Boolean, true), + ])) + }); + Arc::clone(&SCHEMA) } pub fn create_test_schema() -> Result { - let nullable_column = Field::new("nullable_col", DataType::Int32, true); - let non_nullable_column = Field::new("non_nullable_col", DataType::Int32, false); - let schema = Arc::new(Schema::new(vec![nullable_column, non_nullable_column])); + static SCHEMA: LazyLock = LazyLock::new(|| { + let nullable_column = Field::new("nullable_col", DataType::Int32, true); + let non_nullable_column = Field::new("non_nullable_col", DataType::Int32, false); + Arc::new(Schema::new(vec![nullable_column, non_nullable_column])) + }); + let schema = Arc::clone(&SCHEMA); Ok(schema) } pub fn create_test_schema2() -> Result { - let col_a = Field::new("col_a", DataType::Int32, true); - let col_b = Field::new("col_b", DataType::Int32, true); - let schema = Arc::new(Schema::new(vec![col_a, col_b])); + static SCHEMA: LazyLock = LazyLock::new(|| { + let col_a = Field::new("col_a", DataType::Int32, true); + let col_b = Field::new("col_b", DataType::Int32, true); + Arc::new(Schema::new(vec![col_a, col_b])) + }); + let schema = Arc::clone(&SCHEMA); Ok(schema) } // Generate a schema which consists of 5 columns (a, b, c, d, e) pub fn create_test_schema3() -> Result { - let a = Field::new("a", DataType::Int32, true); - let b = Field::new("b", DataType::Int32, false); - let c = Field::new("c", DataType::Int32, true); - let d = Field::new("d", DataType::Int32, false); - let e = Field::new("e", DataType::Int32, false); - let schema = Arc::new(Schema::new(vec![a, b, c, d, e])); + static SCHEMA: LazyLock = LazyLock::new(|| { + let a = Field::new("a", DataType::Int32, true); + let b = Field::new("b", DataType::Int32, false); + let c = Field::new("c", DataType::Int32, true); + let d = Field::new("d", DataType::Int32, false); + let e = Field::new("e", DataType::Int32, false); + Arc::new(Schema::new(vec![a, b, c, d, e])) + }); + let schema = Arc::clone(&SCHEMA); Ok(schema) }