21
21
use std:: fmt;
22
22
use std:: fmt:: Formatter ;
23
23
24
- use super :: { accept, ExecutionPlan , ExecutionPlanVisitor } ;
25
-
26
24
use arrow_schema:: SchemaRef ;
25
+
27
26
use datafusion_common:: display:: { GraphvizBuilder , PlanType , StringifiedPlan } ;
27
+ use datafusion_expr:: display_schema;
28
28
use datafusion_physical_expr:: { LexOrdering , PhysicalSortExpr } ;
29
29
30
+ use super :: { accept, ExecutionPlan , ExecutionPlanVisitor } ;
31
+
30
32
/// Options for controlling how each [`ExecutionPlan`] should format itself
31
33
#[ derive( Debug , Clone , Copy ) ]
32
34
pub enum DisplayFormatType {
@@ -37,12 +39,15 @@ pub enum DisplayFormatType {
37
39
}
38
40
39
41
/// Wraps an `ExecutionPlan` with various ways to display this plan
42
+ #[ derive( Debug , Clone ) ]
40
43
pub struct DisplayableExecutionPlan < ' a > {
41
44
inner : & ' a dyn ExecutionPlan ,
42
45
/// How to show metrics
43
46
show_metrics : ShowMetrics ,
44
47
/// If statistics should be displayed
45
48
show_statistics : bool ,
49
+ /// If schema should be displayed. See [`Self::set_show_schema`]
50
+ show_schema : bool ,
46
51
}
47
52
48
53
impl < ' a > DisplayableExecutionPlan < ' a > {
@@ -53,6 +58,7 @@ impl<'a> DisplayableExecutionPlan<'a> {
53
58
inner,
54
59
show_metrics : ShowMetrics :: None ,
55
60
show_statistics : false ,
61
+ show_schema : false ,
56
62
}
57
63
}
58
64
@@ -64,6 +70,7 @@ impl<'a> DisplayableExecutionPlan<'a> {
64
70
inner,
65
71
show_metrics : ShowMetrics :: Aggregated ,
66
72
show_statistics : false ,
73
+ show_schema : false ,
67
74
}
68
75
}
69
76
@@ -75,9 +82,19 @@ impl<'a> DisplayableExecutionPlan<'a> {
75
82
inner,
76
83
show_metrics : ShowMetrics :: Full ,
77
84
show_statistics : false ,
85
+ show_schema : false ,
78
86
}
79
87
}
80
88
89
+ /// Enable display of schema
90
+ ///
91
+ /// If true, plans will be displayed with schema information at the end
92
+ /// of each line. The format is `schema=[[a:Int32;N, b:Int32;N, c:Int32;N]]`
93
+ pub fn set_show_schema ( mut self , show_schema : bool ) -> Self {
94
+ self . show_schema = show_schema;
95
+ self
96
+ }
97
+
81
98
/// Enable display of statistics
82
99
pub fn set_show_statistics ( mut self , show_statistics : bool ) -> Self {
83
100
self . show_statistics = show_statistics;
@@ -105,6 +122,7 @@ impl<'a> DisplayableExecutionPlan<'a> {
105
122
plan : & ' a dyn ExecutionPlan ,
106
123
show_metrics : ShowMetrics ,
107
124
show_statistics : bool ,
125
+ show_schema : bool ,
108
126
}
109
127
impl < ' a > fmt:: Display for Wrapper < ' a > {
110
128
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -114,6 +132,7 @@ impl<'a> DisplayableExecutionPlan<'a> {
114
132
indent : 0 ,
115
133
show_metrics : self . show_metrics ,
116
134
show_statistics : self . show_statistics ,
135
+ show_schema : self . show_schema ,
117
136
} ;
118
137
accept ( self . plan , & mut visitor)
119
138
}
@@ -123,6 +142,7 @@ impl<'a> DisplayableExecutionPlan<'a> {
123
142
plan : self . inner ,
124
143
show_metrics : self . show_metrics ,
125
144
show_statistics : self . show_statistics ,
145
+ show_schema : self . show_schema ,
126
146
}
127
147
}
128
148
@@ -179,6 +199,7 @@ impl<'a> DisplayableExecutionPlan<'a> {
179
199
plan : & ' a dyn ExecutionPlan ,
180
200
show_metrics : ShowMetrics ,
181
201
show_statistics : bool ,
202
+ show_schema : bool ,
182
203
}
183
204
184
205
impl < ' a > fmt:: Display for Wrapper < ' a > {
@@ -189,6 +210,7 @@ impl<'a> DisplayableExecutionPlan<'a> {
189
210
indent : 0 ,
190
211
show_metrics : self . show_metrics ,
191
212
show_statistics : self . show_statistics ,
213
+ show_schema : self . show_schema ,
192
214
} ;
193
215
visitor. pre_visit ( self . plan ) ?;
194
216
Ok ( ( ) )
@@ -199,6 +221,7 @@ impl<'a> DisplayableExecutionPlan<'a> {
199
221
plan : self . inner ,
200
222
show_metrics : self . show_metrics ,
201
223
show_statistics : self . show_statistics ,
224
+ show_schema : self . show_schema ,
202
225
}
203
226
}
204
227
@@ -221,6 +244,14 @@ enum ShowMetrics {
221
244
}
222
245
223
246
/// Formats plans with a single line per node.
247
+ ///
248
+ /// # Example
249
+ ///
250
+ /// ```text
251
+ /// ProjectionExec: expr=[column1@0 + 2 as column1 + Int64(2)]
252
+ /// FilterExec: column1@0 = 5
253
+ /// ValuesExec
254
+ /// ```
224
255
struct IndentVisitor < ' a , ' b > {
225
256
/// How to format each node
226
257
t : DisplayFormatType ,
@@ -232,6 +263,8 @@ struct IndentVisitor<'a, 'b> {
232
263
show_metrics : ShowMetrics ,
233
264
/// If statistics should be displayed
234
265
show_statistics : bool ,
266
+ /// If schema should be displayed
267
+ show_schema : bool ,
235
268
}
236
269
237
270
impl < ' a , ' b > ExecutionPlanVisitor for IndentVisitor < ' a , ' b > {
@@ -265,6 +298,13 @@ impl<'a, 'b> ExecutionPlanVisitor for IndentVisitor<'a, 'b> {
265
298
let stats = plan. statistics ( ) . map_err ( |_e| fmt:: Error ) ?;
266
299
write ! ( self . f, ", statistics=[{}]" , stats) ?;
267
300
}
301
+ if self . show_schema {
302
+ write ! (
303
+ self . f,
304
+ ", schema={}" ,
305
+ display_schema( plan. schema( ) . as_ref( ) )
306
+ ) ?;
307
+ }
268
308
writeln ! ( self . f) ?;
269
309
self . indent += 1 ;
270
310
Ok ( true )
@@ -465,12 +505,13 @@ mod tests {
465
505
use std:: fmt:: Write ;
466
506
use std:: sync:: Arc ;
467
507
468
- use super :: DisplayableExecutionPlan ;
469
- use crate :: { DisplayAs , ExecutionPlan , PlanProperties } ;
470
-
471
508
use datafusion_common:: { DataFusionError , Result , Statistics } ;
472
509
use datafusion_execution:: { SendableRecordBatchStream , TaskContext } ;
473
510
511
+ use crate :: { DisplayAs , ExecutionPlan , PlanProperties } ;
512
+
513
+ use super :: DisplayableExecutionPlan ;
514
+
474
515
#[ derive( Debug , Clone , Copy ) ]
475
516
enum TestStatsExecPlan {
476
517
Panic ,
0 commit comments