Skip to content

Commit 411185f

Browse files
authored
Implement tree explain for AggregateExec (#15103)
* Implement tree explain for AggregateExec * Extract expr formatting logic for readability * fix empty group_by display
1 parent 4446d18 commit 411185f

File tree

2 files changed

+103
-51
lines changed

2 files changed

+103
-51
lines changed

datafusion/physical-plan/src/aggregates/mod.rs

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -742,21 +742,23 @@ impl DisplayAs for AggregateExec {
742742
t: DisplayFormatType,
743743
f: &mut std::fmt::Formatter,
744744
) -> std::fmt::Result {
745+
let format_expr_with_alias =
746+
|(e, alias): &(Arc<dyn PhysicalExpr>, String)| -> String {
747+
let e = e.to_string();
748+
if &e != alias {
749+
format!("{e} as {alias}")
750+
} else {
751+
e
752+
}
753+
};
745754
match t {
746755
DisplayFormatType::Default | DisplayFormatType::Verbose => {
747756
write!(f, "AggregateExec: mode={:?}", self.mode)?;
748757
let g: Vec<String> = if self.group_by.is_single() {
749758
self.group_by
750759
.expr
751760
.iter()
752-
.map(|(e, alias)| {
753-
let e = e.to_string();
754-
if &e != alias {
755-
format!("{e} as {alias}")
756-
} else {
757-
e
758-
}
759-
})
761+
.map(format_expr_with_alias)
760762
.collect()
761763
} else {
762764
self.group_by
@@ -768,21 +770,11 @@ impl DisplayAs for AggregateExec {
768770
.enumerate()
769771
.map(|(idx, is_null)| {
770772
if *is_null {
771-
let (e, alias) = &self.group_by.null_expr[idx];
772-
let e = e.to_string();
773-
if &e != alias {
774-
format!("{e} as {alias}")
775-
} else {
776-
e
777-
}
773+
format_expr_with_alias(
774+
&self.group_by.null_expr[idx],
775+
)
778776
} else {
779-
let (e, alias) = &self.group_by.expr[idx];
780-
let e = e.to_string();
781-
if &e != alias {
782-
format!("{e} as {alias}")
783-
} else {
784-
e
785-
}
777+
format_expr_with_alias(&self.group_by.expr[idx])
786778
}
787779
})
788780
.collect::<Vec<String>>()
@@ -809,8 +801,45 @@ impl DisplayAs for AggregateExec {
809801
}
810802
}
811803
DisplayFormatType::TreeRender => {
812-
// TODO: collect info
813-
write!(f, "")?;
804+
let g: Vec<String> = if self.group_by.is_single() {
805+
self.group_by
806+
.expr
807+
.iter()
808+
.map(format_expr_with_alias)
809+
.collect()
810+
} else {
811+
self.group_by
812+
.groups
813+
.iter()
814+
.map(|group| {
815+
let terms = group
816+
.iter()
817+
.enumerate()
818+
.map(|(idx, is_null)| {
819+
if *is_null {
820+
format_expr_with_alias(
821+
&self.group_by.null_expr[idx],
822+
)
823+
} else {
824+
format_expr_with_alias(&self.group_by.expr[idx])
825+
}
826+
})
827+
.collect::<Vec<String>>()
828+
.join(", ");
829+
format!("({terms})")
830+
})
831+
.collect()
832+
};
833+
let a: Vec<String> = self
834+
.aggr_expr
835+
.iter()
836+
.map(|agg| agg.name().to_string())
837+
.collect();
838+
writeln!(f, "mode={:?}", self.mode)?;
839+
if !g.is_empty() {
840+
writeln!(f, "group_by={}", g.join(", "))?;
841+
}
842+
writeln!(f, "aggr={}", a.join(", "))?;
814843
}
815844
}
816845
Ok(())

datafusion/sqllogictest/test_files/explain_tree.slt

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,42 @@ explain SELECT string_col, SUM(bigint_col) FROM table1 GROUP BY string_col;
166166
physical_plan
167167
01)┌───────────────────────────┐
168168
02)│ AggregateExec │
169-
03)└─────────────┬─────────────┘
170-
04)┌─────────────┴─────────────┐
171-
05)│ CoalesceBatchesExec
172-
06)└─────────────┬─────────────┘
173-
07)┌─────────────┴─────────────┐
174-
08)│ RepartitionExec
175-
09)└─────────────┬─────────────┘
176-
10)┌─────────────┴─────────────┐
177-
11)│ AggregateExec
169+
03)│ -------------------- │
170+
04)│ aggr: │
171+
05)│ sum(table1.bigint_col)
172+
06)│ │
173+
07)│ group_by: │
174+
08)│ string_col@0 as string_col
175+
09)│ │
176+
10)│ mode: │
177+
11)│ FinalPartitioned
178178
12)└─────────────┬─────────────┘
179179
13)┌─────────────┴─────────────┐
180-
14)│ RepartitionExec
180+
14)│ CoalesceBatchesExec
181181
15)└─────────────┬─────────────┘
182182
16)┌─────────────┴─────────────┐
183-
17)│ DataSourceExec │
184-
18)│ -------------------- │
185-
19)│ files: 1 │
186-
20)│ format: csv │
187-
21)└───────────────────────────┘
183+
17)│ RepartitionExec │
184+
18)└─────────────┬─────────────┘
185+
19)┌─────────────┴─────────────┐
186+
20)│ AggregateExec │
187+
21)│ -------------------- │
188+
22)│ aggr: │
189+
23)│ sum(table1.bigint_col) │
190+
24)│ │
191+
25)│ group_by: │
192+
26)│ string_col@0 as string_col│
193+
27)│ │
194+
28)│ mode: Partial │
195+
29)└─────────────┬─────────────┘
196+
30)┌─────────────┴─────────────┐
197+
31)│ RepartitionExec │
198+
32)└─────────────┬─────────────┘
199+
33)┌─────────────┴─────────────┐
200+
34)│ DataSourceExec │
201+
35)│ -------------------- │
202+
36)│ files: 1 │
203+
37)│ format: csv │
204+
38)└───────────────────────────┘
188205

189206
# Limit
190207
query TT
@@ -1076,22 +1093,28 @@ physical_plan
10761093
11)└───────────────────────────┘└─────────────┬─────────────┘
10771094
12)-----------------------------┌─────────────┴─────────────┐
10781095
13)-----------------------------│ AggregateExec │
1079-
14)-----------------------------└─────────────┬─────────────┘
1080-
15)-----------------------------┌─────────────┴─────────────┐
1081-
16)-----------------------------│ CoalescePartitionsExec
1096+
14)-----------------------------│ -------------------- │
1097+
15)-----------------------------│ aggr: count(Int64(1)) │
1098+
16)-----------------------------│ mode: Final
10821099
17)-----------------------------└─────────────┬─────────────┘
10831100
18)-----------------------------┌─────────────┴─────────────┐
1084-
19)-----------------------------│ AggregateExec
1101+
19)-----------------------------│ CoalescePartitionsExec
10851102
20)-----------------------------└─────────────┬─────────────┘
10861103
21)-----------------------------┌─────────────┴─────────────┐
1087-
22)-----------------------------│ RepartitionExec │
1088-
23)-----------------------------└─────────────┬─────────────┘
1089-
24)-----------------------------┌─────────────┴─────────────┐
1090-
25)-----------------------------│ DataSourceExec │
1091-
26)-----------------------------│ -------------------- │
1092-
27)-----------------------------│ files: 1 │
1093-
28)-----------------------------│ format: parquet │
1094-
29)-----------------------------└───────────────────────────┘
1104+
22)-----------------------------│ AggregateExec │
1105+
23)-----------------------------│ -------------------- │
1106+
24)-----------------------------│ aggr: count(Int64(1)) │
1107+
25)-----------------------------│ mode: Partial │
1108+
26)-----------------------------└─────────────┬─────────────┘
1109+
27)-----------------------------┌─────────────┴─────────────┐
1110+
28)-----------------------------│ RepartitionExec │
1111+
29)-----------------------------└─────────────┬─────────────┘
1112+
30)-----------------------------┌─────────────┴─────────────┐
1113+
31)-----------------------------│ DataSourceExec │
1114+
32)-----------------------------│ -------------------- │
1115+
33)-----------------------------│ files: 1 │
1116+
34)-----------------------------│ format: parquet │
1117+
35)-----------------------------└───────────────────────────┘
10951118

10961119
# Query with cross join.
10971120
query TT

0 commit comments

Comments
 (0)