@@ -28,7 +28,7 @@ use datafusion_functions_aggregate::sum::sum_udaf;
2828use datafusion_functions_nested:: expr_ext:: { IndexAccessor , SliceAccessor } ;
2929use sqlparser:: ast:: NullTreatment ;
3030/// Tests of using and evaluating `Expr`s outside the context of a LogicalPlan
31- use std:: sync:: { Arc , OnceLock } ;
31+ use std:: sync:: { Arc , LazyLock } ;
3232
3333mod parse_sql_expr;
3434mod simplification;
@@ -305,13 +305,11 @@ async fn test_aggregate_ext_null_treatment() {
305305/// Evaluates the specified expr as an aggregate and compares the result to the
306306/// expected result.
307307async fn evaluate_agg_test ( expr : Expr , expected_lines : Vec < & str > ) {
308- let batch = test_batch ( ) ;
309-
310308 let ctx = SessionContext :: new ( ) ;
311309 let group_expr = vec ! [ ] ;
312310 let agg_expr = vec ! [ expr] ;
313311 let result = ctx
314- . read_batch ( batch )
312+ . read_batch ( TEST_BATCH . clone ( ) )
315313 . unwrap ( )
316314 . aggregate ( group_expr, agg_expr)
317315 . unwrap ( )
@@ -332,13 +330,13 @@ async fn evaluate_agg_test(expr: Expr, expected_lines: Vec<&str>) {
332330/// Converts the `Expr` to a `PhysicalExpr`, evaluates it against the provided
333331/// `RecordBatch` and compares the result to the expected result.
334332fn evaluate_expr_test ( expr : Expr , expected_lines : Vec < & str > ) {
335- let batch = test_batch ( ) ;
333+ let batch = & TEST_BATCH ;
336334 let df_schema = DFSchema :: try_from ( batch. schema ( ) ) . unwrap ( ) ;
337335 let physical_expr = SessionContext :: new ( )
338336 . create_physical_expr ( expr, & df_schema)
339337 . unwrap ( ) ;
340338
341- let result = physical_expr. evaluate ( & batch) . unwrap ( ) ;
339+ let result = physical_expr. evaluate ( batch) . unwrap ( ) ;
342340 let array = result. into_array ( 1 ) . unwrap ( ) ;
343341 let result = pretty_format_columns ( "expr" , & [ array] ) . unwrap ( ) . to_string ( ) ;
344342 let actual_lines = result. lines ( ) . collect :: < Vec < _ > > ( ) ;
@@ -350,39 +348,33 @@ fn evaluate_expr_test(expr: Expr, expected_lines: Vec<&str>) {
350348 ) ;
351349}
352350
353- static TEST_BATCH : OnceLock < RecordBatch > = OnceLock :: new ( ) ;
354-
355- fn test_batch ( ) -> RecordBatch {
356- TEST_BATCH
357- . get_or_init ( || {
358- let string_array: ArrayRef = Arc :: new ( StringArray :: from ( vec ! [ "1" , "2" , "3" ] ) ) ;
359- let int_array: ArrayRef =
360- Arc :: new ( Int64Array :: from_iter ( vec ! [ Some ( 10 ) , None , Some ( 5 ) ] ) ) ;
361-
362- // { a: "2021-02-01" } { a: "2021-02-02" } { a: "2021-02-03" }
363- let struct_array: ArrayRef = Arc :: from ( StructArray :: from ( vec ! [ (
364- Arc :: new( Field :: new( "a" , DataType :: Utf8 , false ) ) ,
365- Arc :: new( StringArray :: from( vec![
366- "2021-02-01" ,
367- "2021-02-02" ,
368- "2021-02-03" ,
369- ] ) ) as _,
370- ) ] ) ) ;
371-
372- // ["one"] ["two", "three", "four"] ["five"]
373- let mut builder = ListBuilder :: new ( StringBuilder :: new ( ) ) ;
374- builder. append_value ( [ Some ( "one" ) ] ) ;
375- builder. append_value ( [ Some ( "two" ) , Some ( "three" ) , Some ( "four" ) ] ) ;
376- builder. append_value ( [ Some ( "five" ) ] ) ;
377- let list_array: ArrayRef = Arc :: new ( builder. finish ( ) ) ;
378-
379- RecordBatch :: try_from_iter ( vec ! [
380- ( "id" , string_array) ,
381- ( "i" , int_array) ,
382- ( "props" , struct_array) ,
383- ( "list" , list_array) ,
384- ] )
385- . unwrap ( )
386- } )
387- . clone ( )
388- }
351+ static TEST_BATCH : LazyLock < RecordBatch > = LazyLock :: new ( || {
352+ let string_array: ArrayRef = Arc :: new ( StringArray :: from ( vec ! [ "1" , "2" , "3" ] ) ) ;
353+ let int_array: ArrayRef =
354+ Arc :: new ( Int64Array :: from_iter ( vec ! [ Some ( 10 ) , None , Some ( 5 ) ] ) ) ;
355+
356+ // { a: "2021-02-01" } { a: "2021-02-02" } { a: "2021-02-03" }
357+ let struct_array: ArrayRef = Arc :: from ( StructArray :: from ( vec ! [ (
358+ Arc :: new( Field :: new( "a" , DataType :: Utf8 , false ) ) ,
359+ Arc :: new( StringArray :: from( vec![
360+ "2021-02-01" ,
361+ "2021-02-02" ,
362+ "2021-02-03" ,
363+ ] ) ) as _,
364+ ) ] ) ) ;
365+
366+ // ["one"] ["two", "three", "four"] ["five"]
367+ let mut builder = ListBuilder :: new ( StringBuilder :: new ( ) ) ;
368+ builder. append_value ( [ Some ( "one" ) ] ) ;
369+ builder. append_value ( [ Some ( "two" ) , Some ( "three" ) , Some ( "four" ) ] ) ;
370+ builder. append_value ( [ Some ( "five" ) ] ) ;
371+ let list_array: ArrayRef = Arc :: new ( builder. finish ( ) ) ;
372+
373+ RecordBatch :: try_from_iter ( vec ! [
374+ ( "id" , string_array) ,
375+ ( "i" , int_array) ,
376+ ( "props" , struct_array) ,
377+ ( "list" , list_array) ,
378+ ] )
379+ . unwrap ( )
380+ } ) ;
0 commit comments