@@ -32,6 +32,7 @@ use datafusion_expr::{
32
32
LogicalPlanBuilder , ScalarUDF , Volatility ,
33
33
} ;
34
34
use datafusion_functions:: math;
35
+ use datafusion_optimizer:: optimizer:: Optimizer ;
35
36
use datafusion_optimizer:: simplify_expressions:: { ExprSimplifier , SimplifyExpressions } ;
36
37
use datafusion_optimizer:: { OptimizerContext , OptimizerRule } ;
37
38
use std:: sync:: Arc ;
@@ -109,14 +110,14 @@ fn test_table_scan() -> LogicalPlan {
109
110
. expect ( "building plan" )
110
111
}
111
112
112
- fn get_optimized_plan_formatted ( plan : & LogicalPlan , date_time : & DateTime < Utc > ) -> String {
113
+ fn get_optimized_plan_formatted ( plan : LogicalPlan , date_time : & DateTime < Utc > ) -> String {
113
114
let config = OptimizerContext :: new ( ) . with_query_execution_start_time ( * date_time) ;
114
- let rule = SimplifyExpressions :: new ( ) ;
115
115
116
- let optimized_plan = rule
117
- . try_optimize ( plan, & config)
118
- . unwrap ( )
119
- . expect ( "failed to optimize plan" ) ;
116
+ // Use Optimizer to do plan traversal
117
+ fn observe ( _plan : & LogicalPlan , _rule : & dyn OptimizerRule ) { }
118
+ let optimizer = Optimizer :: with_rules ( vec ! [ Arc :: new( SimplifyExpressions :: new( ) ) ] ) ;
119
+ let optimized_plan = optimizer. optimize ( plan, & config, observe) . unwrap ( ) ;
120
+
120
121
format ! ( "{optimized_plan:?}" )
121
122
}
122
123
@@ -238,7 +239,7 @@ fn to_timestamp_expr_folded() -> Result<()> {
238
239
let expected = "Projection: TimestampNanosecond(1599566400000000000, None) AS to_timestamp(Utf8(\" 2020-09-08T12:00:00+00:00\" ))\
239
240
\n TableScan: test"
240
241
. to_string ( ) ;
241
- let actual = get_optimized_plan_formatted ( & plan, & Utc :: now ( ) ) ;
242
+ let actual = get_optimized_plan_formatted ( plan, & Utc :: now ( ) ) ;
242
243
assert_eq ! ( expected, actual) ;
243
244
Ok ( ( ) )
244
245
}
@@ -262,7 +263,7 @@ fn now_less_than_timestamp() -> Result<()> {
262
263
// expression down to a single constant (true)
263
264
let expected = "Filter: Boolean(true)\
264
265
\n TableScan: test";
265
- let actual = get_optimized_plan_formatted ( & plan, & time) ;
266
+ let actual = get_optimized_plan_formatted ( plan, & time) ;
266
267
267
268
assert_eq ! ( expected, actual) ;
268
269
Ok ( ( ) )
@@ -290,7 +291,7 @@ fn select_date_plus_interval() -> Result<()> {
290
291
// expression down to a single constant (true)
291
292
let expected = r#"Projection: Date32("18636") AS to_timestamp(Utf8("2020-09-08T12:05:00+00:00")) + IntervalDayTime("528280977408")
292
293
TableScan: test"# ;
293
- let actual = get_optimized_plan_formatted ( & plan, & time) ;
294
+ let actual = get_optimized_plan_formatted ( plan, & time) ;
294
295
295
296
assert_eq ! ( expected, actual) ;
296
297
Ok ( ( ) )
@@ -308,7 +309,7 @@ fn simplify_project_scalar_fn() -> Result<()> {
308
309
// after simplify: t.f as "power(t.f, 1.0)"
309
310
let expected = "Projection: test.f AS power(test.f,Float64(1))\
310
311
\n TableScan: test";
311
- let actual = get_optimized_plan_formatted ( & plan, & Utc :: now ( ) ) ;
312
+ let actual = get_optimized_plan_formatted ( plan, & Utc :: now ( ) ) ;
312
313
assert_eq ! ( expected, actual) ;
313
314
Ok ( ( ) )
314
315
}
@@ -330,7 +331,7 @@ fn simplify_scan_predicate() -> Result<()> {
330
331
// before simplify: t.g = power(t.f, 1.0)
331
332
// after simplify: (t.g = t.f) as "t.g = power(t.f, 1.0)"
332
333
let expected = "TableScan: test, full_filters=[g = f AS g = power(f,Float64(1))]" ;
333
- let actual = get_optimized_plan_formatted ( & plan, & Utc :: now ( ) ) ;
334
+ let actual = get_optimized_plan_formatted ( plan, & Utc :: now ( ) ) ;
334
335
assert_eq ! ( expected, actual) ;
335
336
Ok ( ( ) )
336
337
}
@@ -461,7 +462,7 @@ fn multiple_now() -> Result<()> {
461
462
. build ( ) ?;
462
463
463
464
// expect the same timestamp appears in both exprs
464
- let actual = get_optimized_plan_formatted ( & plan, & time) ;
465
+ let actual = get_optimized_plan_formatted ( plan, & time) ;
465
466
let expected = format ! (
466
467
"Projection: TimestampNanosecond({}, Some(\" +00:00\" )) AS now(), TimestampNanosecond({}, Some(\" +00:00\" )) AS t2\
467
468
\n TableScan: test",
0 commit comments