@@ -30,6 +30,7 @@ use datafusion_expr::{
30
30
expr, table_scan, BuiltinScalarFunction , Cast , ColumnarValue , Expr , ExprSchemable ,
31
31
LogicalPlan , LogicalPlanBuilder , ScalarUDF , Volatility ,
32
32
} ;
33
+ use datafusion_optimizer:: optimizer:: Optimizer ;
33
34
use datafusion_optimizer:: simplify_expressions:: { ExprSimplifier , SimplifyExpressions } ;
34
35
use datafusion_optimizer:: { OptimizerContext , OptimizerRule } ;
35
36
use std:: sync:: Arc ;
@@ -107,14 +108,14 @@ fn test_table_scan() -> LogicalPlan {
107
108
. expect ( "building plan" )
108
109
}
109
110
110
- fn get_optimized_plan_formatted ( plan : & LogicalPlan , date_time : & DateTime < Utc > ) -> String {
111
+ fn get_optimized_plan_formatted ( plan : LogicalPlan , date_time : & DateTime < Utc > ) -> String {
111
112
let config = OptimizerContext :: new ( ) . with_query_execution_start_time ( * date_time) ;
112
- let rule = SimplifyExpressions :: new ( ) ;
113
113
114
- let optimized_plan = rule
115
- . try_optimize ( plan, & config)
116
- . unwrap ( )
117
- . expect ( "failed to optimize plan" ) ;
114
+ // Use Optimizer to do plan traversal
115
+ fn observe ( _plan : & LogicalPlan , _rule : & dyn OptimizerRule ) { }
116
+ let optimizer = Optimizer :: with_rules ( vec ! [ Arc :: new( SimplifyExpressions :: new( ) ) ] ) ;
117
+ let optimized_plan = optimizer. optimize ( plan, & config, observe) . unwrap ( ) ;
118
+
118
119
format ! ( "{optimized_plan:?}" )
119
120
}
120
121
@@ -236,7 +237,7 @@ fn to_timestamp_expr_folded() -> Result<()> {
236
237
let expected = "Projection: TimestampNanosecond(1599566400000000000, None) AS to_timestamp(Utf8(\" 2020-09-08T12:00:00+00:00\" ))\
237
238
\n TableScan: test"
238
239
. to_string ( ) ;
239
- let actual = get_optimized_plan_formatted ( & plan, & Utc :: now ( ) ) ;
240
+ let actual = get_optimized_plan_formatted ( plan, & Utc :: now ( ) ) ;
240
241
assert_eq ! ( expected, actual) ;
241
242
Ok ( ( ) )
242
243
}
@@ -260,7 +261,7 @@ fn now_less_than_timestamp() -> Result<()> {
260
261
// expression down to a single constant (true)
261
262
let expected = "Filter: Boolean(true)\
262
263
\n TableScan: test";
263
- let actual = get_optimized_plan_formatted ( & plan, & time) ;
264
+ let actual = get_optimized_plan_formatted ( plan, & time) ;
264
265
265
266
assert_eq ! ( expected, actual) ;
266
267
Ok ( ( ) )
@@ -288,7 +289,7 @@ fn select_date_plus_interval() -> Result<()> {
288
289
// expression down to a single constant (true)
289
290
let expected = r#"Projection: Date32("18636") AS to_timestamp(Utf8("2020-09-08T12:05:00+00:00")) + IntervalDayTime("528280977408")
290
291
TableScan: test"# ;
291
- let actual = get_optimized_plan_formatted ( & plan, & time) ;
292
+ let actual = get_optimized_plan_formatted ( plan, & time) ;
292
293
293
294
assert_eq ! ( expected, actual) ;
294
295
Ok ( ( ) )
@@ -420,7 +421,7 @@ fn multiple_now() -> Result<()> {
420
421
. build ( ) ?;
421
422
422
423
// expect the same timestamp appears in both exprs
423
- let actual = get_optimized_plan_formatted ( & plan, & time) ;
424
+ let actual = get_optimized_plan_formatted ( plan, & time) ;
424
425
let expected = format ! (
425
426
"Projection: TimestampNanosecond({}, Some(\" +00:00\" )) AS now(), TimestampNanosecond({}, Some(\" +00:00\" )) AS t2\
426
427
\n TableScan: test",
0 commit comments