Skip to content

Commit 2d57a0b

Browse files
authored
refactor: replace OnceLock with LazyLock (#14880)
this commit replaces all the remaining references of OnceLock with LazyLock
1 parent 6797375 commit 2d57a0b

File tree

11 files changed

+263
-261
lines changed

11 files changed

+263
-261
lines changed

datafusion/core/tests/fuzz_cases/pruning.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use std::sync::{Arc, OnceLock};
18+
use std::sync::{Arc, LazyLock};
1919

2020
use arrow::array::{Array, RecordBatch, StringArray};
2121
use arrow::datatypes::{DataType, Field, Schema};
@@ -204,65 +204,24 @@ impl Utf8Test {
204204

205205
/// all combinations of interesting charactes with lengths ranging from 1 to 4
206206
fn values() -> &'static [String] {
207-
VALUES.get_or_init(|| {
208-
let mut rng = rand::thread_rng();
209-
210-
let characters = [
211-
"z",
212-
"0",
213-
"~",
214-
"ß",
215-
"℣",
216-
"%", // this one is useful for like/not like tests since it will result in randomly inserted wildcards
217-
"_", // this one is useful for like/not like tests since it will result in randomly inserted wildcards
218-
"\u{7F}",
219-
"\u{7FF}",
220-
"\u{FF}",
221-
"\u{10FFFF}",
222-
"\u{D7FF}",
223-
"\u{FDCF}",
224-
// null character
225-
"\u{0}",
226-
];
227-
let value_lengths = [1, 2, 3];
228-
let mut values = vec![];
229-
for length in &value_lengths {
230-
values.extend(
231-
characters
232-
.iter()
233-
.cloned()
234-
.combinations(*length)
235-
// now get all permutations of each combination
236-
.flat_map(|c| c.into_iter().permutations(*length))
237-
// and join them into strings
238-
.map(|c| c.join("")),
239-
);
240-
}
241-
println!("Generated {} values", values.len());
242-
// randomly pick 100 values
243-
values.shuffle(&mut rng);
244-
values.truncate(100);
245-
values
246-
})
207+
&VALUES
247208
}
248209

249210
/// return the in memory object store
250211
fn memory_store() -> &'static Arc<dyn ObjectStore> {
251-
MEMORY_STORE.get_or_init(|| Arc::new(InMemory::new()))
212+
&MEMORY_STORE
252213
}
253214

254215
/// return the schema of the created test files
255216
fn schema() -> Arc<Schema> {
256-
let schema = SCHEMA.get_or_init(|| {
257-
Arc::new(Schema::new(vec![Field::new("a", DataType::Utf8, false)]))
258-
});
217+
let schema = &SCHEMA;
259218
Arc::clone(schema)
260219
}
261220

262221
/// Return a list of test files with UTF8 data and combinations of
263222
/// [`Self::values`]
264223
async fn test_files() -> Vec<TestFile> {
265-
let files_mutex = TESTFILES.get_or_init(|| Mutex::new(vec![]));
224+
let files_mutex = &TESTFILES;
266225
let mut files = files_mutex.lock().await;
267226
if !files.is_empty() {
268227
return (*files).clone();
@@ -385,16 +344,57 @@ async fn write_parquet_file(
385344
}
386345

387346
/// The string values for [Utf8Test::values]
388-
static VALUES: OnceLock<Vec<String>> = OnceLock::new();
347+
static VALUES: LazyLock<Vec<String>> = LazyLock::new(|| {
348+
let mut rng = rand::thread_rng();
349+
350+
let characters = [
351+
"z",
352+
"0",
353+
"~",
354+
"ß",
355+
"℣",
356+
"%", // this one is useful for like/not like tests since it will result in randomly inserted wildcards
357+
"_", // this one is useful for like/not like tests since it will result in randomly inserted wildcards
358+
"\u{7F}",
359+
"\u{7FF}",
360+
"\u{FF}",
361+
"\u{10FFFF}",
362+
"\u{D7FF}",
363+
"\u{FDCF}",
364+
// null character
365+
"\u{0}",
366+
];
367+
let value_lengths = [1, 2, 3];
368+
let mut values = vec![];
369+
for length in &value_lengths {
370+
values.extend(
371+
characters
372+
.iter()
373+
.cloned()
374+
.combinations(*length)
375+
// now get all permutations of each combination
376+
.flat_map(|c| c.into_iter().permutations(*length))
377+
// and join them into strings
378+
.map(|c| c.join("")),
379+
);
380+
}
381+
println!("Generated {} values", values.len());
382+
// randomly pick 100 values
383+
values.shuffle(&mut rng);
384+
values.truncate(100);
385+
values
386+
});
389387
/// The schema for the [Utf8Test::schema]
390-
static SCHEMA: OnceLock<Arc<Schema>> = OnceLock::new();
388+
static SCHEMA: LazyLock<Arc<Schema>> =
389+
LazyLock::new(|| Arc::new(Schema::new(vec![Field::new("a", DataType::Utf8, false)])));
391390

392391
/// The InMemory object store
393-
static MEMORY_STORE: OnceLock<Arc<dyn ObjectStore>> = OnceLock::new();
392+
static MEMORY_STORE: LazyLock<Arc<dyn ObjectStore>> =
393+
LazyLock::new(|| Arc::new(InMemory::new()));
394394

395395
/// List of in memory parquet files with UTF8 data
396-
// Use a mutex rather than OnceLock to allow for async initialization
397-
static TESTFILES: OnceLock<Mutex<Vec<TestFile>>> = OnceLock::new();
396+
// Use a mutex rather than LazyLock to allow for async initialization
397+
static TESTFILES: LazyLock<Mutex<Vec<TestFile>>> = LazyLock::new(|| Mutex::new(vec![]));
398398

399399
/// Holds a temporary parquet file path and its size
400400
#[derive(Debug, Clone)]

datafusion/expr/src/udaf.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ where
338338
/// # Basic Example
339339
/// ```
340340
/// # use std::any::Any;
341-
/// # use std::sync::OnceLock;
341+
/// # use std::sync::LazyLock;
342342
/// # use arrow::datatypes::DataType;
343343
/// # use datafusion_common::{DataFusionError, plan_err, Result};
344344
/// # use datafusion_expr::{col, ColumnarValue, Signature, Volatility, Expr, Documentation};
@@ -360,14 +360,14 @@ where
360360
/// }
361361
/// }
362362
///
363-
/// static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
364-
///
365-
/// fn get_doc() -> &'static Documentation {
366-
/// DOCUMENTATION.get_or_init(|| {
363+
/// static DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
367364
/// Documentation::builder(DOC_SECTION_AGGREGATE, "calculates a geometric mean", "geo_mean(2.0)")
368365
/// .with_argument("arg1", "The Float64 number for the geometric mean")
369366
/// .build()
370-
/// })
367+
/// });
368+
///
369+
/// fn get_doc() -> &'static Documentation {
370+
/// &DOCUMENTATION
371371
/// }
372372
///
373373
/// /// Implement the AggregateUDFImpl trait for GeoMeanUdf

datafusion/expr/src/udf.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ impl ReturnInfo {
433433
/// # Basic Example
434434
/// ```
435435
/// # use std::any::Any;
436-
/// # use std::sync::OnceLock;
436+
/// # use std::sync::LazyLock;
437437
/// # use arrow::datatypes::DataType;
438438
/// # use datafusion_common::{DataFusionError, plan_err, Result};
439439
/// # use datafusion_expr::{col, ColumnarValue, Documentation, ScalarFunctionArgs, Signature, Volatility};
@@ -453,14 +453,14 @@ impl ReturnInfo {
453453
/// }
454454
/// }
455455
///
456-
/// static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
457-
///
458-
/// fn get_doc() -> &'static Documentation {
459-
/// DOCUMENTATION.get_or_init(|| {
456+
/// static DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
460457
/// Documentation::builder(DOC_SECTION_MATH, "Add one to an int32", "add_one(2)")
461458
/// .with_argument("arg1", "The int32 number to add one to")
462459
/// .build()
463-
/// })
460+
/// });
461+
///
462+
/// fn get_doc() -> &'static Documentation {
463+
/// &DOCUMENTATION
464464
/// }
465465
///
466466
/// /// Implement the ScalarUDFImpl trait for AddOne

datafusion/expr/src/udwf.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ where
235235
/// # Basic Example
236236
/// ```
237237
/// # use std::any::Any;
238-
/// # use std::sync::OnceLock;
238+
/// # use std::sync::LazyLock;
239239
/// # use arrow::datatypes::{DataType, Field};
240240
/// # use datafusion_common::{DataFusionError, plan_err, Result};
241241
/// # use datafusion_expr::{col, Signature, Volatility, PartitionEvaluator, WindowFrame, ExprFunctionExt, Documentation};
@@ -257,14 +257,14 @@ where
257257
/// }
258258
/// }
259259
///
260-
/// static DOCUMENTATION: OnceLock<Documentation> = OnceLock::new();
260+
/// static DOCUMENTATION: LazyLock<Documentation> = LazyLock::new(|| {
261+
/// Documentation::builder(DOC_SECTION_ANALYTICAL, "smooths the windows", "smooth_it(2)")
262+
/// .with_argument("arg1", "The int32 number to smooth by")
263+
/// .build()
264+
/// });
261265
///
262266
/// fn get_doc() -> &'static Documentation {
263-
/// DOCUMENTATION.get_or_init(|| {
264-
/// Documentation::builder(DOC_SECTION_ANALYTICAL, "smooths the windows", "smooth_it(2)")
265-
/// .with_argument("arg1", "The int32 number to smooth by")
266-
/// .build()
267-
/// })
267+
/// &DOCUMENTATION
268268
/// }
269269
///
270270
/// /// Implement the WindowUDFImpl trait for SmoothIt

datafusion/functions-aggregate/src/bit_and_or_xor.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use datafusion_expr::{
4242
use datafusion_expr::aggregate_doc_sections::DOC_SECTION_GENERAL;
4343
use datafusion_functions_aggregate_common::aggregate::groups_accumulator::prim_op::PrimitiveGroupsAccumulator;
4444
use std::ops::{BitAndAssign, BitOrAssign, BitXorAssign};
45-
use std::sync::OnceLock;
45+
use std::sync::LazyLock;
4646

4747
/// This macro helps create group accumulators based on bitwise operations typically used internally
4848
/// and might not be necessary for users to call directly.
@@ -134,46 +134,46 @@ macro_rules! make_bitwise_udaf_expr_and_func {
134134
};
135135
}
136136

137-
static BIT_AND_DOC: OnceLock<Documentation> = OnceLock::new();
137+
static BIT_AND_DOC: LazyLock<Documentation> = LazyLock::new(|| {
138+
Documentation::builder(
139+
DOC_SECTION_GENERAL,
140+
"Computes the bitwise AND of all non-null input values.",
141+
"bit_and(expression)",
142+
)
143+
.with_standard_argument("expression", Some("Integer"))
144+
.build()
145+
});
138146

139147
fn get_bit_and_doc() -> &'static Documentation {
140-
BIT_AND_DOC.get_or_init(|| {
141-
Documentation::builder(
142-
DOC_SECTION_GENERAL,
143-
"Computes the bitwise AND of all non-null input values.",
144-
"bit_and(expression)",
145-
)
146-
.with_standard_argument("expression", Some("Integer"))
147-
.build()
148-
})
148+
&BIT_AND_DOC
149149
}
150150

151-
static BIT_OR_DOC: OnceLock<Documentation> = OnceLock::new();
151+
static BIT_OR_DOC: LazyLock<Documentation> = LazyLock::new(|| {
152+
Documentation::builder(
153+
DOC_SECTION_GENERAL,
154+
"Computes the bitwise OR of all non-null input values.",
155+
"bit_or(expression)",
156+
)
157+
.with_standard_argument("expression", Some("Integer"))
158+
.build()
159+
});
152160

153161
fn get_bit_or_doc() -> &'static Documentation {
154-
BIT_OR_DOC.get_or_init(|| {
155-
Documentation::builder(
156-
DOC_SECTION_GENERAL,
157-
"Computes the bitwise OR of all non-null input values.",
158-
"bit_or(expression)",
159-
)
160-
.with_standard_argument("expression", Some("Integer"))
161-
.build()
162-
})
162+
&BIT_OR_DOC
163163
}
164164

165-
static BIT_XOR_DOC: OnceLock<Documentation> = OnceLock::new();
165+
static BIT_XOR_DOC: LazyLock<Documentation> = LazyLock::new(|| {
166+
Documentation::builder(
167+
DOC_SECTION_GENERAL,
168+
"Computes the bitwise exclusive OR of all non-null input values.",
169+
"bit_xor(expression)",
170+
)
171+
.with_standard_argument("expression", Some("Integer"))
172+
.build()
173+
});
166174

167175
fn get_bit_xor_doc() -> &'static Documentation {
168-
BIT_XOR_DOC.get_or_init(|| {
169-
Documentation::builder(
170-
DOC_SECTION_GENERAL,
171-
"Computes the bitwise exclusive OR of all non-null input values.",
172-
"bit_xor(expression)",
173-
)
174-
.with_standard_argument("expression", Some("Integer"))
175-
.build()
176-
})
176+
&BIT_XOR_DOC
177177
}
178178

179179
make_bitwise_udaf_expr_and_func!(

0 commit comments

Comments
 (0)