Skip to content

Commit d0bfddc

Browse files
committed
Implement tree explain for AggregateExec
1 parent 04d823b commit d0bfddc

File tree

2 files changed

+86
-17
lines changed

2 files changed

+86
-17
lines changed

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

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,8 +809,60 @@ impl DisplayAs for AggregateExec {
809809
}
810810
}
811811
DisplayFormatType::TreeRender => {
812-
// TODO: collect info
813-
write!(f, "")?;
812+
let g: Vec<String> = if self.group_by.is_single() {
813+
self.group_by
814+
.expr
815+
.iter()
816+
.map(|(e, alias)| {
817+
let e = e.to_string();
818+
if &e != alias {
819+
format!("{e} as {alias}")
820+
} else {
821+
e
822+
}
823+
})
824+
.collect()
825+
} else {
826+
self.group_by
827+
.groups
828+
.iter()
829+
.map(|group| {
830+
let terms = group
831+
.iter()
832+
.enumerate()
833+
.map(|(idx, is_null)| {
834+
if *is_null {
835+
let (e, alias) = &self.group_by.null_expr[idx];
836+
let e = e.to_string();
837+
if &e != alias {
838+
format!("{e} as {alias}")
839+
} else {
840+
e
841+
}
842+
} else {
843+
let (e, alias) = &self.group_by.expr[idx];
844+
let e = e.to_string();
845+
if &e != alias {
846+
format!("{e} as {alias}")
847+
} else {
848+
e
849+
}
850+
}
851+
})
852+
.collect::<Vec<String>>()
853+
.join(", ");
854+
format!("({terms})")
855+
})
856+
.collect()
857+
};
858+
let a: Vec<String> = self
859+
.aggr_expr
860+
.iter()
861+
.map(|agg| agg.name().to_string())
862+
.collect();
863+
writeln!(f, "mode={:?}", self.mode)?;
864+
writeln!(f, "group_by={}", g.join(", "))?;
865+
writeln!(f, "aggr={}", a.join(", "))?;
814866
}
815867
}
816868
Ok(())

datafusion/sqllogictest/test_files/explain_tree.slt

Lines changed: 32 additions & 15 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

0 commit comments

Comments
 (0)