Skip to content

Apache DataFusion SQL Query Engine

Test refactor: avoid creating the same SchemaRef (#17951)

Andrew Lambab22ad8bd2ce
diff --git a/datafusion/core/tests/physical_optimizer/test_utils.rs b/datafusion/core/tests/physical_optimizer/test_utils.rs
index b906dfa4b9..8ca33f3d4a 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<DataSourceExec> {
}
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<SchemaRef> = 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<SchemaRef> {
- 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<SchemaRef> = 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<SchemaRef> {
- 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<SchemaRef> = 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<SchemaRef> {
- 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<SchemaRef> = 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)
}