16
16
//! available for the default [`Explain`] implementation for [`MirRelationExpr`]
17
17
//! in [`mz_expr`].
18
18
19
- use std:: collections:: BTreeMap ;
20
-
21
19
use mz_compute_client:: types:: dataflows:: DataflowDescription ;
22
- use mz_expr:: {
23
- explain_new:: { enforce_linear_chains, ExplainContext , ExplainMultiPlan , ExplainSinglePlan } ,
24
- visit:: Visit ,
25
- MirRelationExpr , OptimizedMirRelationExpr ,
26
- } ;
27
- use mz_ore:: { stack:: RecursionLimitError , str:: bracketed, str:: separated} ;
28
- use mz_repr:: explain_new:: {
29
- AnnotatedPlan , Attributes , Explain , ExplainConfig , ExplainError , UnsupportedFormat ,
30
- } ;
31
- use mz_transform:: attribute:: {
32
- Arity , DerivedAttributes , NonNegative , RelationType , SubtreeSize , UniqueKeys ,
20
+ use mz_expr:: explain:: {
21
+ enforce_linear_chains, ExplainContext , ExplainMultiPlan , ExplainSinglePlan ,
33
22
} ;
23
+ use mz_expr:: { MirRelationExpr , OptimizedMirRelationExpr } ;
24
+ use mz_repr:: explain:: { Explain , ExplainConfig , ExplainError , UnsupportedFormat } ;
25
+ use mz_transform:: attribute:: annotate_plan;
26
+ use mz_transform:: normalize_lets:: normalize_lets;
34
27
35
28
use super :: Explainable ;
36
29
@@ -75,12 +68,13 @@ impl<'a> Explainable<'a, MirRelationExpr> {
75
68
// normalize the representation of nested Let bindings
76
69
// and enforce sequential Let binding IDs
77
70
if !config. raw_plans {
78
- mz_transform:: normalize_lets:: normalize_lets ( self . 0 )
79
- . map_err ( |e| ExplainError :: UnknownError ( e. to_string ( ) ) ) ?;
71
+ normalize_lets ( self . 0 ) . map_err ( |e| ExplainError :: UnknownError ( e. to_string ( ) ) ) ?;
80
72
}
81
73
82
- let plan = try_from ( context, self . 0 ) ?;
83
- Ok ( ExplainSinglePlan { context, plan } )
74
+ Ok ( ExplainSinglePlan {
75
+ context,
76
+ plan : annotate_plan ( self . 0 , context) ?,
77
+ } )
84
78
}
85
79
}
86
80
@@ -122,25 +116,26 @@ impl<'a> Explainable<'a, DataflowDescription<OptimizedMirRelationExpr>> {
122
116
. iter_mut ( )
123
117
. rev ( )
124
118
. map ( |build_desc| {
119
+ let plan = build_desc. plan . as_inner_mut ( ) ;
120
+
125
121
// normalize the representation as linear chains
126
122
// (this implies !config.raw_plans by construction)
127
123
if config. linear_chains {
128
- enforce_linear_chains ( build_desc . plan . as_inner_mut ( ) ) ?;
124
+ enforce_linear_chains ( plan) ?;
129
125
} ;
130
126
// unless raw plans are explicitly requested
131
127
// normalize the representation of nested Let bindings
132
128
// and enforce sequential Let binding IDs
133
129
if !config. raw_plans {
134
- mz_transform:: normalize_lets:: normalize_lets ( build_desc. plan . as_inner_mut ( ) )
135
- . map_err ( |e| ExplainError :: UnknownError ( e. to_string ( ) ) ) ?;
130
+ normalize_lets ( plan) . map_err ( |e| ExplainError :: UnknownError ( e. to_string ( ) ) ) ?;
136
131
}
137
132
138
133
let id = context
139
134
. humanizer
140
135
. humanize_id ( build_desc. id )
141
136
. unwrap_or_else ( || build_desc. id . to_string ( ) ) ;
142
- let plan = try_from ( context , build_desc . plan . as_inner ( ) ) ? ;
143
- Ok ( ( id, plan) )
137
+
138
+ Ok ( ( id, annotate_plan ( plan, context ) ? ) )
144
139
} )
145
140
. collect :: < Result < Vec < _ > , ExplainError > > ( ) ?;
146
141
@@ -166,79 +161,3 @@ impl<'a> Explainable<'a, DataflowDescription<OptimizedMirRelationExpr>> {
166
161
} )
167
162
}
168
163
}
169
-
170
- fn try_from < ' a > (
171
- context : & ' a ExplainContext ,
172
- plan : & ' a MirRelationExpr ,
173
- ) -> Result < AnnotatedPlan < ' a , MirRelationExpr > , RecursionLimitError > {
174
- let mut annotations = BTreeMap :: < & MirRelationExpr , Attributes > :: default ( ) ;
175
- let config = context. config ;
176
-
177
- if config. requires_attributes ( ) {
178
- // get the annotation keys
179
- let subtree_refs = plan. post_order_vec ( ) ;
180
- // get the annotation values
181
- let mut explain_attrs = DerivedAttributes :: from ( config) ;
182
- plan. visit ( & mut explain_attrs) ?;
183
-
184
- if config. subtree_size {
185
- for ( expr, attr) in std:: iter:: zip (
186
- subtree_refs. iter ( ) ,
187
- explain_attrs. remove_results :: < SubtreeSize > ( ) . into_iter ( ) ,
188
- ) {
189
- let attrs = annotations. entry ( expr) . or_default ( ) ;
190
- attrs. subtree_size = Some ( attr) ;
191
- }
192
- }
193
- if config. non_negative {
194
- for ( expr, attr) in std:: iter:: zip (
195
- subtree_refs. iter ( ) ,
196
- explain_attrs. remove_results :: < NonNegative > ( ) . into_iter ( ) ,
197
- ) {
198
- let attrs = annotations. entry ( expr) . or_default ( ) ;
199
- attrs. non_negative = Some ( attr) ;
200
- }
201
- }
202
-
203
- if config. arity {
204
- for ( expr, attr) in std:: iter:: zip (
205
- subtree_refs. iter ( ) ,
206
- explain_attrs. remove_results :: < Arity > ( ) . into_iter ( ) ,
207
- ) {
208
- let attrs = annotations. entry ( expr) . or_default ( ) ;
209
- attrs. arity = Some ( attr) ;
210
- }
211
- }
212
-
213
- if config. types {
214
- for ( expr, types) in std:: iter:: zip (
215
- subtree_refs. iter ( ) ,
216
- explain_attrs. remove_results :: < RelationType > ( ) . into_iter ( ) ,
217
- ) {
218
- let humanized_columns = types
219
- . into_iter ( )
220
- . map ( |c| context. humanizer . humanize_column_type ( & c) )
221
- . collect :: < Vec < _ > > ( ) ;
222
- let attr = bracketed ( "(" , ")" , separated ( ", " , humanized_columns) ) . to_string ( ) ;
223
- let attrs = annotations. entry ( expr) . or_default ( ) ;
224
- attrs. types = Some ( attr) ;
225
- }
226
- }
227
-
228
- if config. keys {
229
- for ( expr, keys) in std:: iter:: zip (
230
- subtree_refs. iter ( ) ,
231
- explain_attrs. remove_results :: < UniqueKeys > ( ) . into_iter ( ) ,
232
- ) {
233
- let formatted_keys = keys
234
- . into_iter ( )
235
- . map ( |key_set| bracketed ( "[" , "]" , separated ( ", " , key_set) ) . to_string ( ) ) ;
236
- let attr = bracketed ( "(" , ")" , separated ( ", " , formatted_keys) ) . to_string ( ) ;
237
- let attrs = annotations. entry ( expr) . or_default ( ) ;
238
- attrs. keys = Some ( attr) ;
239
- }
240
- }
241
- }
242
-
243
- Ok ( AnnotatedPlan { plan, annotations } )
244
- }
0 commit comments