@@ -27,7 +27,6 @@ use crate::executor::explain::PlanStatsInfo;
27
27
use crate :: executor:: PhysicalPlan ;
28
28
use crate :: executor:: PhysicalPlanBuilder ;
29
29
use crate :: optimizer:: SExpr ;
30
- use crate :: optimizer:: StatInfo ;
31
30
use crate :: plans:: BoundColumnRef ;
32
31
use crate :: plans:: Operator ;
33
32
use crate :: plans:: RelOp ;
@@ -75,12 +74,10 @@ impl PhysicalPlanBuilder {
75
74
& mut output_cols,
76
75
)
77
76
. await ?;
78
-
79
- debug_assert ! ( union_children. len( ) >= 2 ) ;
80
- let left_schema = union_children[ 0 ] . output_schema ( ) ?;
81
- let right_schema = union_children[ 1 ] . output_schema ( ) ?;
77
+ let right_schema = union_children[ 0 ] . output_schema ( ) ?;
78
+ let left_schema = union_children[ 1 ] . output_schema ( ) ?;
82
79
let mut common_types = vec ! [ ] ;
83
- for ( l , r ) in output_cols[ 0 ] . iter ( ) . zip ( output_cols[ 1 ] . iter ( ) ) {
80
+ for ( r , l ) in output_cols[ 0 ] . iter ( ) . zip ( output_cols[ 1 ] . iter ( ) ) {
84
81
let left_field = left_schema. field_with_name ( & l. to_string ( ) ) ?;
85
82
let right_field = right_schema. field_with_name ( & r. to_string ( ) ) ?;
86
83
@@ -100,10 +97,8 @@ impl PhysicalPlanBuilder {
100
97
} ) ?)
101
98
}
102
99
103
- for ( right_plan, right_output_cols) in union_children. iter ( ) . zip ( output_cols. iter ( ) ) . skip ( 2 )
104
- {
105
- common_types =
106
- common_super_types_for_union ( common_types, right_plan, right_output_cols) ?;
100
+ for ( left_plan, left_output_cols) in union_children. iter ( ) . zip ( output_cols. iter ( ) ) . skip ( 2 ) {
101
+ common_types = common_super_types_for_union ( common_types, left_plan, left_output_cols) ?;
107
102
}
108
103
109
104
async fn cast_plan (
@@ -168,7 +163,10 @@ impl PhysicalPlanBuilder {
168
163
. await ?;
169
164
}
170
165
171
- let fields = output_cols[ 0 ]
166
+ // Last is the most left union child
167
+ let fields = output_cols
168
+ . last ( )
169
+ . unwrap ( )
172
170
. iter ( )
173
171
. zip ( & common_types)
174
172
. map ( |( index, ty) | DataField :: new ( & index. to_string ( ) , ty. clone ( ) ) )
@@ -192,27 +190,30 @@ impl PhysicalPlanBuilder {
192
190
union_children : & mut Vec < Box < PhysicalPlan > > ,
193
191
output_cols : & mut Vec < Vec < IndexType > > ,
194
192
) -> Result < ( ) > {
195
- let left_required = union. left_cols . iter ( ) . fold ( required. clone ( ) , |mut acc, v| {
196
- acc. insert ( * v) ;
197
- acc
198
- } ) ;
199
- let left_plan = self . build ( s_expr. child ( 0 ) ?, left_required) . await ?;
200
- union_children. push ( Box :: new ( left_plan) ) ;
201
- output_cols. push ( union. left_cols . clone ( ) ) ;
202
- let right_s_expr = s_expr. child ( 1 ) ?;
203
- if right_s_expr. plan . rel_op ( ) != RelOp :: UnionAll {
204
- let right_required = union. right_cols . iter ( ) . fold ( required, |mut acc, v| {
193
+ let right_required = union
194
+ . right_cols
195
+ . iter ( )
196
+ . fold ( required. clone ( ) , |mut acc, v| {
197
+ acc. insert ( * v) ;
198
+ acc
199
+ } ) ;
200
+ let right_plan = self . build ( s_expr. child ( 1 ) ?, right_required) . await ?;
201
+ union_children. push ( Box :: new ( right_plan) ) ;
202
+ output_cols. push ( union. right_cols . clone ( ) ) ;
203
+ let left_s_expr = s_expr. child ( 0 ) ?;
204
+ if left_s_expr. plan . rel_op ( ) != RelOp :: UnionAll {
205
+ let left_required = union. left_cols . iter ( ) . fold ( required, |mut acc, v| {
205
206
acc. insert ( * v) ;
206
207
acc
207
208
} ) ;
208
- let plan = self . build ( right_s_expr , right_required ) . await ?;
209
+ let plan = self . build ( left_s_expr , left_required ) . await ?;
209
210
union_children. push ( Box :: new ( plan) ) ;
210
- output_cols. push ( union. right_cols . clone ( ) ) ;
211
+ output_cols. push ( union. left_cols . clone ( ) ) ;
211
212
return Ok ( ( ) ) ;
212
213
}
213
214
self . flatten_union (
214
- right_s_expr ,
215
- & right_s_expr . plan ( ) . clone ( ) . try_into ( ) ?,
215
+ left_s_expr ,
216
+ & left_s_expr . plan ( ) . clone ( ) . try_into ( ) ?,
216
217
required,
217
218
union_children,
218
219
output_cols,
@@ -223,23 +224,23 @@ impl PhysicalPlanBuilder {
223
224
224
225
fn common_super_types_for_union (
225
226
common_types : Vec < DataType > ,
226
- right_plan : & PhysicalPlan ,
227
- right_output_cols : & [ IndexType ] ,
227
+ left_plan : & PhysicalPlan ,
228
+ left_output_cols : & [ IndexType ] ,
228
229
) -> Result < Vec < DataType > > {
229
230
let mut new_common_types = Vec :: with_capacity ( common_types. len ( ) ) ;
230
- let right_schema = right_plan . output_schema ( ) ?;
231
- for ( left_type , idx) in common_types. iter ( ) . zip ( right_output_cols . iter ( ) ) {
232
- let right_field = right_schema . field_with_name ( & idx. to_string ( ) ) ?;
231
+ let left_schema = left_plan . output_schema ( ) ?;
232
+ for ( right_type , idx) in common_types. iter ( ) . zip ( left_output_cols . iter ( ) ) {
233
+ let left_field = left_schema . field_with_name ( & idx. to_string ( ) ) ?;
233
234
let common_type = common_super_type (
234
- left_type . clone ( ) ,
235
- right_field . data_type ( ) . clone ( ) ,
235
+ right_type . clone ( ) ,
236
+ left_field . data_type ( ) . clone ( ) ,
236
237
& BUILTIN_FUNCTIONS . default_cast_rules ,
237
238
) ;
238
239
new_common_types. push ( common_type. ok_or_else ( || {
239
240
ErrorCode :: SemanticError ( format ! (
240
241
"SetOperation's types cannot be matched, left type: {:?}, right type: {:?}" ,
241
- left_type ,
242
- right_field . data_type ( )
242
+ left_field . data_type ( ) ,
243
+ right_type ,
243
244
) )
244
245
} ) ?)
245
246
}
0 commit comments