18
18
//! [`EliminateNestedUnion`]: flattens nested `Union` to a single `Union`
19
19
use crate :: optimizer:: ApplyOrder ;
20
20
use crate :: { OptimizerConfig , OptimizerRule } ;
21
- use datafusion_common:: Result ;
21
+ use datafusion_common:: tree_node:: Transformed ;
22
+ use datafusion_common:: { internal_err, Result } ;
22
23
use datafusion_expr:: expr_rewriter:: coerce_plan_expr_for_schema;
23
24
use datafusion_expr:: { Distinct , LogicalPlan , Union } ;
24
25
use std:: sync:: Arc ;
@@ -37,49 +38,63 @@ impl EliminateNestedUnion {
37
38
impl OptimizerRule for EliminateNestedUnion {
38
39
fn try_optimize (
39
40
& self ,
40
- plan : & LogicalPlan ,
41
+ _plan : & LogicalPlan ,
41
42
_config : & dyn OptimizerConfig ,
42
43
) -> Result < Option < LogicalPlan > > {
44
+ internal_err ! ( "Should have called EliminateNestedUnion::rewrite" )
45
+ }
46
+
47
+ fn name ( & self ) -> & str {
48
+ "eliminate_nested_union"
49
+ }
50
+
51
+ fn apply_order ( & self ) -> Option < ApplyOrder > {
52
+ Some ( ApplyOrder :: BottomUp )
53
+ }
54
+
55
+ fn supports_rewrite ( & self ) -> bool {
56
+ true
57
+ }
58
+
59
+ fn rewrite (
60
+ & self ,
61
+ plan : LogicalPlan ,
62
+ _config : & dyn OptimizerConfig ,
63
+ ) -> Result < Transformed < LogicalPlan > > {
43
64
match plan {
44
65
LogicalPlan :: Union ( Union { inputs, schema } ) => {
45
66
let inputs = inputs
46
67
. iter ( )
47
68
. flat_map ( extract_plans_from_union)
48
69
. collect :: < Vec < _ > > ( ) ;
49
70
50
- Ok ( Some ( LogicalPlan :: Union ( Union {
71
+ Ok ( Transformed :: yes ( LogicalPlan :: Union ( Union {
51
72
inputs,
52
- schema : schema . clone ( ) ,
73
+ schema,
53
74
} ) ) )
54
75
}
55
- LogicalPlan :: Distinct ( Distinct :: All ( plan) ) => match plan. as_ref ( ) {
56
- LogicalPlan :: Union ( Union { inputs, schema } ) => {
57
- let inputs = inputs
58
- . iter ( )
59
- . map ( extract_plan_from_distinct)
60
- . flat_map ( extract_plans_from_union)
61
- . collect :: < Vec < _ > > ( ) ;
62
-
63
- Ok ( Some ( LogicalPlan :: Distinct ( Distinct :: All ( Arc :: new (
64
- LogicalPlan :: Union ( Union {
65
- inputs,
66
- schema : schema. clone ( ) ,
67
- } ) ,
68
- ) ) ) ) )
76
+ LogicalPlan :: Distinct ( Distinct :: All ( ref nested_plan) ) => {
77
+ match nested_plan. as_ref ( ) {
78
+ LogicalPlan :: Union ( Union { inputs, schema } ) => {
79
+ let inputs = inputs
80
+ . iter ( )
81
+ . map ( extract_plan_from_distinct)
82
+ . flat_map ( extract_plans_from_union)
83
+ . collect :: < Vec < _ > > ( ) ;
84
+
85
+ Ok ( Transformed :: yes ( LogicalPlan :: Distinct ( Distinct :: All (
86
+ Arc :: new ( LogicalPlan :: Union ( Union {
87
+ inputs,
88
+ schema : schema. clone ( ) ,
89
+ } ) ) ,
90
+ ) ) ) )
91
+ }
92
+ _ => Ok ( Transformed :: no ( plan) ) ,
69
93
}
70
- _ => Ok ( None ) ,
71
- } ,
72
- _ => Ok ( None ) ,
94
+ }
95
+ _ => Ok ( Transformed :: no ( plan) ) ,
73
96
}
74
97
}
75
-
76
- fn name ( & self ) -> & str {
77
- "eliminate_nested_union"
78
- }
79
-
80
- fn apply_order ( & self ) -> Option < ApplyOrder > {
81
- Some ( ApplyOrder :: BottomUp )
82
- }
83
98
}
84
99
85
100
fn extract_plans_from_union ( plan : & Arc < LogicalPlan > ) -> Vec < Arc < LogicalPlan > > {
0 commit comments