Skip to content

Apache DataFusion SQL Query Engine

Collapse statistics in normal explain plan (#4157)

Andrew Lamb75ef1945f9aa
diff --git a/datafusion/core/src/physical_plan/display.rs b/datafusion/core/src/physical_plan/display.rs
index f89512ab44..a8093d8a7f 100644
--- a/datafusion/core/src/physical_plan/display.rs
+++ b/datafusion/core/src/physical_plan/display.rs
@@ -167,7 +167,7 @@ impl<'a, 'b> ExecutionPlanVisitor for IndentVisitor<'a, 'b> {
ShowMetrics::Aggregated => {
if let Some(metrics) = plan.metrics() {
let metrics = metrics
- .aggregate_by_partition()
+ .aggregate_by_name()
.sorted_for_display()
.timestamps_removed();
diff --git a/datafusion/core/src/physical_plan/metrics/mod.rs b/datafusion/core/src/physical_plan/metrics/mod.rs
index 0617c4828a..db8723217e 100644
--- a/datafusion/core/src/physical_plan/metrics/mod.rs
+++ b/datafusion/core/src/physical_plan/metrics/mod.rs
@@ -140,7 +140,7 @@ impl Metric {
}
/// Add a new label to this metric
- pub fn with(mut self, label: Label) -> Self {
+ pub fn with_label(mut self, label: Label) -> Self {
self.labels.push(label);
self
}
@@ -259,15 +259,15 @@ impl MetricsSet {
}
/// Returns returns a new derived `MetricsSet` where all metrics
- /// that had the same name and partition=`Some(..)` have been
+ /// that had the same name have been
/// aggregated together. The resulting `MetricsSet` has all
/// metrics with `Partition=None`
- pub fn aggregate_by_partition(&self) -> Self {
+ pub fn aggregate_by_name(&self) -> Self {
let mut map = HashMap::new();
// There are all sorts of ways to make this more efficient
for metric in &self.metrics {
- let key = (metric.value.name(), metric.labels.clone());
+ let key = metric.value.name();
map.entry(key)
.and_modify(|accum: &mut Metric| {
accum.value_mut().aggregate(metric.value());
@@ -275,11 +275,7 @@ impl MetricsSet {
.or_insert_with(|| {
// accumulate with no partition
let partition = None;
- let mut accum = Metric::new_with_labels(
- metric.value().new_empty(),
- partition,
- metric.labels().to_vec(),
- );
+ let mut accum = Metric::new(metric.value().new_empty(), partition);
accum.value_mut().aggregate(metric.value());
accum
});
@@ -537,10 +533,10 @@ mod tests {
}
#[test]
- fn test_aggregate_partition() {
+ fn test_aggregate_by_name() {
let metrics = ExecutionPlanMetricsSet::new();
- // Note cpu_time1 has labels so it is not aggregated with 2 and 3
+ // Note cpu_time1 has labels but it is still aggregated with metrics 2 and 3
let elapsed_compute1 = MetricBuilder::new(&metrics)
.with_new_label("foo", "bar")
.elapsed_compute(1);
@@ -555,18 +551,15 @@ mod tests {
let output_rows = MetricBuilder::new(&metrics).output_rows(1); // output rows
output_rows.add(56);
- let aggregated = metrics.clone_inner().aggregate_by_partition();
+ let aggregated = metrics.clone_inner().aggregate_by_name();
// cpu time should be aggregated:
let elapsed_computes = aggregated
.iter()
- .filter(|metric| {
- matches!(metric.value(), MetricValue::ElapsedCompute(_))
- && metric.labels().is_empty()
- })
+ .filter(|metric| matches!(metric.value(), MetricValue::ElapsedCompute(_)))
.collect::<Vec<_>>();
assert_eq!(elapsed_computes.len(), 1);
- assert_eq!(elapsed_computes[0].value().as_usize(), 34 + 56);
+ assert_eq!(elapsed_computes[0].value().as_usize(), 12 + 34 + 56);
assert!(elapsed_computes[0].partition().is_none());
// output rows should
@@ -591,7 +584,7 @@ mod tests {
time.add_duration(Duration::from_nanos(10));
// can't aggregate time and count -- expect a panic
- metrics.clone_inner().aggregate_by_partition();
+ metrics.clone_inner().aggregate_by_name();
}
#[test]
@@ -617,7 +610,7 @@ mod tests {
end_timestamp1.set(t4);
// aggregate
- let aggregated = metrics.clone_inner().aggregate_by_partition();
+ let aggregated = metrics.clone_inner().aggregate_by_name();
let mut ts = aggregated
.iter()
diff --git a/datafusion/core/tests/sql/explain_analyze.rs b/datafusion/core/tests/sql/explain_analyze.rs
index 693a99984c..da290daa9f 100644
--- a/datafusion/core/tests/sql/explain_analyze.rs
+++ b/datafusion/core/tests/sql/explain_analyze.rs
@@ -138,7 +138,7 @@ async fn explain_analyze_baseline_metrics() {
if !expected_to_have_metrics(plan) {
return Ok(true);
}
- let metrics = plan.metrics().unwrap().aggregate_by_partition();
+ let metrics = plan.metrics().unwrap().aggregate_by_name();
assert!(
metrics.output_rows().unwrap() > 0,
@@ -822,6 +822,39 @@ async fn csv_explain_analyze() {
assert_not_contains!(formatted, verbose_needle);
}
+#[tokio::test]
+#[cfg_attr(tarpaulin, ignore)]
+async fn parquet_explain_analyze() {
+ let ctx = SessionContext::new();
+ register_alltypes_parquet(&ctx).await;
+
+ let sql = "EXPLAIN ANALYZE select id, float_col, timestamp_col from alltypes_plain where timestamp_col > to_timestamp('2009-02-01T00:00:00'); ";
+ let actual = execute_to_batches(&ctx, sql).await;
+ let formatted = arrow::util::pretty::pretty_format_batches(&actual)
+ .unwrap()
+ .to_string();
+
+ // should contain aggregated stats
+ assert_contains!(&formatted, "output_rows=8");
+ assert_contains!(&formatted, "row_groups_pruned=0");
+}
+
+#[tokio::test]
+#[cfg_attr(tarpaulin, ignore)]
+async fn parquet_explain_analyze_verbose() {
+ let ctx = SessionContext::new();
+ register_alltypes_parquet(&ctx).await;
+
+ let sql = "EXPLAIN ANALYZE VERBOSE select id, float_col, timestamp_col from alltypes_plain where timestamp_col > to_timestamp('2009-02-01T00:00:00'); ";
+ let actual = execute_to_batches(&ctx, sql).await;
+ let formatted = arrow::util::pretty::pretty_format_batches(&actual)
+ .unwrap()
+ .to_string();
+
+ // should contain the raw per file stats (with the label)
+ assert_contains!(&formatted, "row_groups_pruned{partition=0");
+}
+
#[tokio::test]
#[cfg_attr(tarpaulin, ignore)]
async fn csv_explain_analyze_verbose() {
@@ -849,7 +882,7 @@ async fn explain_logical_plan_only() {
let expected = vec![
vec![
- "logical_plan",
+ "logical_plan",
"Projection: COUNT(UInt8(1))\
\n Aggregate: groupBy=[[]], aggr=[[COUNT(UInt8(1))]]\
\n Values: (Utf8(\"a\"), Int64(1), Int64(100)), (Utf8(\"a\"), Int64(2), Int64(150))",