@@ -57,19 +57,19 @@ pub trait TreeNode: Sized {
57
57
F : FnMut ( & Self ) -> Result < TreeNodeRecursion > ,
58
58
{
59
59
// Apply `f` on self.
60
- f ( self )
60
+ f ( self ) ?
61
61
// If it returns continue (not prune or stop or stop all) then continue
62
62
// traversal on inner children and children.
63
63
. and_then_on_continue ( || {
64
64
// Run the recursive `apply` on each inner children, but as they are
65
65
// unrelated root nodes of inner trees if any returns stop then continue
66
66
// with the next one.
67
- self . apply_inner_children ( & mut |c| c. visit_down ( f) . continue_on_stop ( ) )
67
+ self . apply_inner_children ( & mut |c| c. visit_down ( f) ? . continue_on_stop ( ) ) ?
68
68
// Run the recursive `apply` on each children.
69
69
. and_then_on_continue ( || {
70
70
self . apply_children ( & mut |c| c. visit_down ( f) )
71
71
} )
72
- } )
72
+ } ) ?
73
73
// Applying `f` on self might have returned prune, but we need to propagate
74
74
// continue.
75
75
. continue_on_prune ( )
@@ -107,21 +107,21 @@ pub trait TreeNode: Sized {
107
107
) -> Result < TreeNodeRecursion > {
108
108
// Apply `pre_visit` on self.
109
109
visitor
110
- . pre_visit ( self )
110
+ . pre_visit ( self ) ?
111
111
// If it returns continue (not prune or stop or stop all) then continue
112
112
// traversal on inner children and children.
113
113
. and_then_on_continue ( || {
114
114
// Run the recursive `visit` on each inner children, but as they are
115
115
// unrelated subquery plans if any returns stop then continue with the
116
116
// next one.
117
- self . apply_inner_children ( & mut |c| c. visit ( visitor) . continue_on_stop ( ) )
117
+ self . apply_inner_children ( & mut |c| c. visit ( visitor) ? . continue_on_stop ( ) ) ?
118
118
// Run the recursive `visit` on each children.
119
119
. and_then_on_continue ( || {
120
120
self . apply_children ( & mut |c| c. visit ( visitor) )
121
- } )
121
+ } ) ?
122
122
// Apply `post_visit` on self.
123
123
. and_then_on_continue ( || visitor. post_visit ( self ) )
124
- } )
124
+ } ) ?
125
125
// Applying `pre_visit` or `post_visit` on self might have returned prune,
126
126
// but we need to propagate continue.
127
127
. continue_on_prune ( )
@@ -133,31 +133,144 @@ pub trait TreeNode: Sized {
133
133
) -> Result < TreeNodeRecursion > {
134
134
// Apply `pre_transform` on self.
135
135
transformer
136
- . pre_transform ( self )
136
+ . pre_transform ( self ) ?
137
137
// If it returns continue (not prune or stop or stop all) then continue
138
138
// traversal on inner children and children.
139
139
. and_then_on_continue ( ||
140
140
// Run the recursive `transform` on each children.
141
141
self
142
- . transform_children ( & mut |c| c. transform ( transformer) )
142
+ . transform_children ( & mut |c| c. transform ( transformer) ) ?
143
143
// Apply `post_transform` on new self.
144
- . and_then_on_continue ( || {
145
- transformer. post_transform ( self )
146
- } ) )
144
+ . and_then_on_continue ( || transformer. post_transform ( self ) ) ) ?
147
145
// Applying `pre_transform` or `post_transform` on self might have returned
148
146
// prune, but we need to propagate continue.
149
147
. continue_on_prune ( )
150
148
}
151
149
150
+ fn transform_with_payload < FD , PD , FU , PU > (
151
+ & mut self ,
152
+ f_down : & mut FD ,
153
+ payload_down : Option < PD > ,
154
+ f_up : & mut FU ,
155
+ ) -> Result < ( TreeNodeRecursion , Option < PU > ) >
156
+ where
157
+ FD : FnMut ( & mut Self , Option < PD > ) -> Result < ( TreeNodeRecursion , Vec < PD > ) > ,
158
+ FU : FnMut ( & mut Self , Vec < PU > ) -> Result < ( TreeNodeRecursion , PU ) > ,
159
+ {
160
+ // Apply `f_down` on self.
161
+ let ( tnr, new_payload_down) = f_down ( self , payload_down) ?;
162
+ let mut new_payload_down_iter = new_payload_down. into_iter ( ) ;
163
+ // If it returns continue (not prune or stop or stop all) then continue traversal
164
+ // on inner children and children.
165
+ let mut new_payload_up = None ;
166
+ tnr. and_then_on_continue ( || {
167
+ // Run the recursive `transform` on each children.
168
+ let mut payload_up = vec ! [ ] ;
169
+ let tnr = self . transform_children ( & mut |c| {
170
+ let ( tnr, p) =
171
+ c. transform_with_payload ( f_down, new_payload_down_iter. next ( ) , f_up) ?;
172
+ p. into_iter ( ) . for_each ( |p| payload_up. push ( p) ) ;
173
+ Ok ( tnr)
174
+ } ) ?;
175
+ // Apply `f_up` on self.
176
+ tnr. and_then_on_continue ( || {
177
+ let ( tnr, np) = f_up ( self , payload_up) ?;
178
+ new_payload_up = Some ( np) ;
179
+ Ok ( tnr)
180
+ } )
181
+ } ) ?
182
+ // Applying `f_down` or `f_up` on self might have returned prune, but we need to propagate
183
+ // continue.
184
+ . continue_on_prune ( )
185
+ . map ( |tnr| ( tnr, new_payload_up) )
186
+ }
187
+
188
+ fn transform_down < F > ( & mut self , f : & mut F ) -> Result < TreeNodeRecursion >
189
+ where
190
+ F : FnMut ( & mut Self ) -> Result < TreeNodeRecursion > ,
191
+ {
192
+ // Apply `f` on self.
193
+ f ( self ) ?
194
+ // If it returns continue (not prune or stop or stop all) then continue
195
+ // traversal on inner children and children.
196
+ . and_then_on_continue ( ||
197
+ // Run the recursive `transform` on each children.
198
+ self . transform_children ( & mut |c| c. transform_down ( f) ) ) ?
199
+ // Applying `f` on self might have returned prune, but we need to propagate
200
+ // continue.
201
+ . continue_on_prune ( )
202
+ }
203
+
204
+ fn transform_down_with_payload < F , P > (
205
+ & mut self ,
206
+ f : & mut F ,
207
+ payload : P ,
208
+ ) -> Result < TreeNodeRecursion >
209
+ where
210
+ F : FnMut ( & mut Self , P ) -> Result < ( TreeNodeRecursion , Vec < P > ) > ,
211
+ {
212
+ // Apply `f` on self.
213
+ let ( tnr, new_payload) = f ( self , payload) ?;
214
+ let mut new_payload_iter = new_payload. into_iter ( ) ;
215
+ // If it returns continue (not prune or stop or stop all) then continue
216
+ // traversal on inner children and children.
217
+ tnr. and_then_on_continue ( ||
218
+ // Run the recursive `transform` on each children.
219
+ self . transform_children ( & mut |c| c. transform_down_with_payload ( f, new_payload_iter. next ( ) . unwrap ( ) ) ) ) ?
220
+ // Applying `f` on self might have returned prune, but we need to propagate
221
+ // continue.
222
+ . continue_on_prune ( )
223
+ }
224
+
225
+ fn transform_up < F > ( & mut self , f : & mut F ) -> Result < TreeNodeRecursion >
226
+ where
227
+ F : FnMut ( & mut Self ) -> Result < TreeNodeRecursion > ,
228
+ {
229
+ // Run the recursive `transform` on each children.
230
+ self . transform_children ( & mut |c| c. transform_up ( f) ) ?
231
+ // Apply `f` on self.
232
+ . and_then_on_continue ( || f ( self ) ) ?
233
+ // Applying `f` on self might have returned prune, but we need to propagate
234
+ // continue.
235
+ . continue_on_prune ( )
236
+ }
237
+
238
+ fn transform_up_with_payload < F , P > (
239
+ & mut self ,
240
+ f : & mut F ,
241
+ ) -> Result < ( TreeNodeRecursion , Option < P > ) >
242
+ where
243
+ F : FnMut ( & mut Self , Vec < P > ) -> Result < ( TreeNodeRecursion , P ) > ,
244
+ {
245
+ // Run the recursive `transform` on each children.
246
+ let mut payload = vec ! [ ] ;
247
+ let tnr = self . transform_children ( & mut |c| {
248
+ let ( tnr, p) = c. transform_up_with_payload ( f) ?;
249
+ p. into_iter ( ) . for_each ( |p| payload. push ( p) ) ;
250
+ Ok ( tnr)
251
+ } ) ?;
252
+ let mut new_payload = None ;
253
+ // Apply `f` on self.
254
+ tnr. and_then_on_continue ( || {
255
+ let ( tnr, np) = f ( self , payload) ?;
256
+ new_payload = Some ( np) ;
257
+ Ok ( tnr)
258
+ } ) ?
259
+ // Applying `f` on self might have returned prune, but we need to propagate
260
+ // continue.
261
+ . continue_on_prune ( )
262
+ . map ( |tnr| ( tnr, new_payload) )
263
+ }
264
+
152
265
/// Convenience utils for writing optimizers rule: recursively apply the given 'op' to the node and all of its
153
266
/// children(Preorder Traversal).
154
267
/// When the `op` does not apply to a given node, it is left unchanged.
155
- fn transform_down < F > ( self , op : & F ) -> Result < Self >
268
+ fn transform_down_old < F > ( self , op : & F ) -> Result < Self >
156
269
where
157
270
F : Fn ( Self ) -> Result < Transformed < Self > > ,
158
271
{
159
272
let after_op = op ( self ) ?. into ( ) ;
160
- after_op. map_children ( |node| node. transform_down ( op) )
273
+ after_op. map_children ( |node| node. transform_down_old ( op) )
161
274
}
162
275
163
276
/// Convenience utils for writing optimizers rule: recursively apply the given 'op' to the node and all of its
@@ -174,11 +287,11 @@ pub trait TreeNode: Sized {
174
287
/// Convenience utils for writing optimizers rule: recursively apply the given 'op' first to all of its
175
288
/// children and then itself(Postorder Traversal).
176
289
/// When the `op` does not apply to a given node, it is left unchanged.
177
- fn transform_up < F > ( self , op : & F ) -> Result < Self >
290
+ fn transform_up_old < F > ( self , op : & F ) -> Result < Self >
178
291
where
179
292
F : Fn ( Self ) -> Result < Transformed < Self > > ,
180
293
{
181
- let after_op_children = self . map_children ( |node| node. transform_up ( op) ) ?;
294
+ let after_op_children = self . map_children ( |node| node. transform_up_old ( op) ) ?;
182
295
183
296
let new_node = op ( after_op_children) ?. into ( ) ;
184
297
Ok ( new_node)
@@ -402,63 +515,35 @@ pub enum TreeNodeRecursion {
402
515
}
403
516
404
517
impl TreeNodeRecursion {
405
- fn continue_on_prune ( self ) -> TreeNodeRecursion {
406
- match self {
407
- TreeNodeRecursion :: Prune => TreeNodeRecursion :: Continue ,
408
- o => o,
409
- }
410
- }
411
-
412
- fn fail_on_prune ( self ) -> TreeNodeRecursion {
413
- match self {
414
- TreeNodeRecursion :: Prune => panic ! ( "Recursion can't prune." ) ,
415
- o => o,
416
- }
417
- }
418
-
419
- fn continue_on_stop ( self ) -> TreeNodeRecursion {
420
- match self {
421
- TreeNodeRecursion :: Stop => TreeNodeRecursion :: Continue ,
422
- o => o,
423
- }
424
- }
425
- }
426
-
427
- /// This helper trait provide functions to control recursion on
428
- /// [`Result<TreeNodeRecursion>`].
429
- pub trait TreeNodeRecursionResult : Sized {
430
- fn and_then_on_continue < F > ( self , f : F ) -> Result < TreeNodeRecursion >
431
- where
432
- F : FnOnce ( ) -> Result < TreeNodeRecursion > ;
433
-
434
- fn continue_on_prune ( self ) -> Result < TreeNodeRecursion > ;
435
-
436
- fn fail_on_prune ( self ) -> Result < TreeNodeRecursion > ;
437
-
438
- fn continue_on_stop ( self ) -> Result < TreeNodeRecursion > ;
439
- }
440
-
441
- impl TreeNodeRecursionResult for Result < TreeNodeRecursion > {
442
- fn and_then_on_continue < F > ( self , f : F ) -> Result < TreeNodeRecursion >
518
+ pub fn and_then_on_continue < F > ( self , f : F ) -> Result < TreeNodeRecursion >
443
519
where
444
520
F : FnOnce ( ) -> Result < TreeNodeRecursion > ,
445
521
{
446
- match self ? {
522
+ match self {
447
523
TreeNodeRecursion :: Continue => f ( ) ,
448
524
o => Ok ( o) ,
449
525
}
450
526
}
451
527
452
- fn continue_on_prune ( self ) -> Result < TreeNodeRecursion > {
453
- self . map ( |tnr| tnr. continue_on_prune ( ) )
528
+ pub fn continue_on_prune ( self ) -> Result < TreeNodeRecursion > {
529
+ Ok ( match self {
530
+ TreeNodeRecursion :: Prune => TreeNodeRecursion :: Continue ,
531
+ o => o,
532
+ } )
454
533
}
455
534
456
- fn fail_on_prune ( self ) -> Result < TreeNodeRecursion > {
457
- self . map ( |tnr| tnr. fail_on_prune ( ) )
535
+ pub fn fail_on_prune ( self ) -> Result < TreeNodeRecursion > {
536
+ Ok ( match self {
537
+ TreeNodeRecursion :: Prune => panic ! ( "Recursion can't prune." ) ,
538
+ o => o,
539
+ } )
458
540
}
459
541
460
- fn continue_on_stop ( self ) -> Result < TreeNodeRecursion > {
461
- self . map ( |tnr| tnr. continue_on_stop ( ) )
542
+ pub fn continue_on_stop ( self ) -> Result < TreeNodeRecursion > {
543
+ Ok ( match self {
544
+ TreeNodeRecursion :: Stop => TreeNodeRecursion :: Continue ,
545
+ o => o,
546
+ } )
462
547
}
463
548
}
464
549
0 commit comments