@@ -260,8 +260,8 @@ impl<'a, 'b> ExecutionPlanVisitor for IndentVisitor<'a, 'b> {
260
260
}
261
261
}
262
262
}
263
- let stats = plan. statistics ( ) . map_err ( |_e| fmt:: Error ) ?;
264
263
if self . show_statistics {
264
+ let stats = plan. statistics ( ) . map_err ( |_e| fmt:: Error ) ?;
265
265
write ! ( self . f, ", statistics=[{}]" , stats) ?;
266
266
}
267
267
writeln ! ( self . f) ?;
@@ -341,8 +341,8 @@ impl ExecutionPlanVisitor for GraphvizVisitor<'_, '_> {
341
341
}
342
342
} ;
343
343
344
- let stats = plan. statistics ( ) . map_err ( |_e| fmt:: Error ) ?;
345
344
let statistics = if self . show_statistics {
345
+ let stats = plan. statistics ( ) . map_err ( |_e| fmt:: Error ) ?;
346
346
format ! ( "statistics=[{}]" , stats)
347
347
} else {
348
348
"" . to_string ( )
@@ -436,3 +436,126 @@ impl<'a> fmt::Display for OutputOrderingDisplay<'a> {
436
436
write ! ( f, "]" )
437
437
}
438
438
}
439
+
440
+ #[ cfg( test) ]
441
+ mod tests {
442
+ use std:: fmt:: Write ;
443
+ use std:: sync:: Arc ;
444
+
445
+ use datafusion_common:: DataFusionError ;
446
+
447
+ use crate :: { DisplayAs , ExecutionPlan } ;
448
+
449
+ use super :: DisplayableExecutionPlan ;
450
+
451
+ #[ derive( Debug , Clone , Copy ) ]
452
+ enum TestStatsExecPlan {
453
+ Panic ,
454
+ Error ,
455
+ Ok ,
456
+ }
457
+
458
+ impl DisplayAs for TestStatsExecPlan {
459
+ fn fmt_as (
460
+ & self ,
461
+ _t : crate :: DisplayFormatType ,
462
+ f : & mut std:: fmt:: Formatter ,
463
+ ) -> std:: fmt:: Result {
464
+ write ! ( f, "TestStatsExecPlan" )
465
+ }
466
+ }
467
+
468
+ impl ExecutionPlan for TestStatsExecPlan {
469
+ fn as_any ( & self ) -> & dyn std:: any:: Any {
470
+ self
471
+ }
472
+
473
+ fn schema ( & self ) -> arrow_schema:: SchemaRef {
474
+ Arc :: new ( arrow_schema:: Schema :: empty ( ) )
475
+ }
476
+
477
+ fn output_partitioning ( & self ) -> datafusion_physical_expr:: Partitioning {
478
+ datafusion_physical_expr:: Partitioning :: UnknownPartitioning ( 1 )
479
+ }
480
+
481
+ fn output_ordering (
482
+ & self ,
483
+ ) -> Option < & [ datafusion_physical_expr:: PhysicalSortExpr ] > {
484
+ None
485
+ }
486
+
487
+ fn children ( & self ) -> Vec < Arc < dyn ExecutionPlan > > {
488
+ vec ! [ ]
489
+ }
490
+
491
+ fn with_new_children (
492
+ self : Arc < Self > ,
493
+ _: Vec < Arc < dyn ExecutionPlan > > ,
494
+ ) -> datafusion_common:: Result < Arc < dyn ExecutionPlan > > {
495
+ unimplemented ! ( )
496
+ }
497
+
498
+ fn execute (
499
+ & self ,
500
+ _: usize ,
501
+ _: Arc < datafusion_execution:: TaskContext > ,
502
+ ) -> datafusion_common:: Result < datafusion_execution:: SendableRecordBatchStream >
503
+ {
504
+ todo ! ( )
505
+ }
506
+
507
+ fn statistics ( & self ) -> datafusion_common:: Result < datafusion_common:: Statistics > {
508
+ match self {
509
+ Self :: Panic => panic ! ( "expected panic" ) ,
510
+ Self :: Error => {
511
+ Err ( DataFusionError :: Internal ( "expected error" . to_string ( ) ) )
512
+ }
513
+ Self :: Ok => Ok ( datafusion_common:: Statistics :: new_unknown (
514
+ self . schema ( ) . as_ref ( ) ,
515
+ ) ) ,
516
+ }
517
+ }
518
+ }
519
+
520
+ fn test_stats_display ( exec : TestStatsExecPlan , show_stats : bool ) {
521
+ let display =
522
+ DisplayableExecutionPlan :: new ( & exec) . set_show_statistics ( show_stats) ;
523
+
524
+ let mut buf = String :: new ( ) ;
525
+ write ! ( & mut buf, "{}" , display. one_line( ) ) . unwrap ( ) ;
526
+ let buf = buf. trim ( ) ;
527
+ assert_eq ! ( buf, "TestStatsExecPlan" ) ;
528
+ }
529
+
530
+ #[ test]
531
+ fn test_display_when_stats_panic_with_no_show_stats ( ) {
532
+ test_stats_display ( TestStatsExecPlan :: Panic , false ) ;
533
+ }
534
+
535
+ #[ test]
536
+ fn test_display_when_stats_error_with_no_show_stats ( ) {
537
+ test_stats_display ( TestStatsExecPlan :: Error , false ) ;
538
+ }
539
+
540
+ #[ test]
541
+ fn test_display_when_stats_ok_with_no_show_stats ( ) {
542
+ test_stats_display ( TestStatsExecPlan :: Ok , false ) ;
543
+ }
544
+
545
+ #[ test]
546
+ #[ should_panic( expected = "expected panic" ) ]
547
+ fn test_display_when_stats_panic_with_show_stats ( ) {
548
+ test_stats_display ( TestStatsExecPlan :: Panic , true ) ;
549
+ }
550
+
551
+ #[ test]
552
+ #[ should_panic( expected = "Error" ) ] // fmt::Error
553
+ fn test_display_when_stats_error_with_show_stats ( ) {
554
+ test_stats_display ( TestStatsExecPlan :: Error , true ) ;
555
+ }
556
+
557
+ #[ test]
558
+ fn test_display_when_stats_ok_with_show_stats ( ) {
559
+ test_stats_display ( TestStatsExecPlan :: Ok , false ) ;
560
+ }
561
+ }
0 commit comments