Skip to content

Commit 500ab40

Browse files
authored
fix: Pull stats in IdentVisitor/GraphvizVisitor only when requested (#8514)
Signed-off-by: Vaibhav <vrongmeal@gmail.com>
1 parent 861cc36 commit 500ab40

File tree

1 file changed

+125
-2
lines changed

1 file changed

+125
-2
lines changed

datafusion/physical-plan/src/display.rs

Lines changed: 125 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ impl<'a, 'b> ExecutionPlanVisitor for IndentVisitor<'a, 'b> {
260260
}
261261
}
262262
}
263-
let stats = plan.statistics().map_err(|_e| fmt::Error)?;
264263
if self.show_statistics {
264+
let stats = plan.statistics().map_err(|_e| fmt::Error)?;
265265
write!(self.f, ", statistics=[{}]", stats)?;
266266
}
267267
writeln!(self.f)?;
@@ -341,8 +341,8 @@ impl ExecutionPlanVisitor for GraphvizVisitor<'_, '_> {
341341
}
342342
};
343343

344-
let stats = plan.statistics().map_err(|_e| fmt::Error)?;
345344
let statistics = if self.show_statistics {
345+
let stats = plan.statistics().map_err(|_e| fmt::Error)?;
346346
format!("statistics=[{}]", stats)
347347
} else {
348348
"".to_string()
@@ -436,3 +436,126 @@ impl<'a> fmt::Display for OutputOrderingDisplay<'a> {
436436
write!(f, "]")
437437
}
438438
}
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

Comments
 (0)