@@ -140,12 +140,25 @@ func (p *PhysicalHashJoin) ResolveIndicesItself() (err error) {
140140 shallowColSlice := make ([]* expression.Column , p .schema .Len ())
141141 copy (shallowColSlice , p .schema .Columns )
142142 p .schema = expression .NewSchema (shallowColSlice ... )
143- for i := 0 ; i < colsNeedResolving ; i ++ {
144- newCol , err := p .schema .Columns [i ].ResolveIndices (mergedSchema )
145- if err != nil {
146- return err
143+ foundCnt := 0
144+ // The two column sets are all ordered. And the colsNeedResolving is the subset of the mergedSchema.
145+ // So we can just move forward j if there's no matching is found.
146+ // We don't use the normal ResolvIndices here since there might be duplicate columns in the schema.
147+ // e.g. The schema of child_0 is [col0, col0, col1]
148+ // ResolveIndices will only resolve all col0 reference of the current plan to the first col0.
149+ for i , j := 0 , 0 ; i < colsNeedResolving && j < len (mergedSchema .Columns ); {
150+ if ! p .schema .Columns [i ].Equal (nil , mergedSchema .Columns [j ]) {
151+ j ++
152+ continue
147153 }
148- p .schema .Columns [i ] = newCol .(* expression.Column )
154+ p .schema .Columns [i ] = p .schema .Columns [i ].Clone ().(* expression.Column )
155+ p .schema .Columns [i ].Index = j
156+ i ++
157+ j ++
158+ foundCnt ++
159+ }
160+ if foundCnt < colsNeedResolving {
161+ return errors .Errorf ("Some columns of %v cannot find the reference from its child(ren)" , p .ExplainID ().String ())
149162 }
150163
151164 return
@@ -213,12 +226,25 @@ func (p *PhysicalMergeJoin) ResolveIndices() (err error) {
213226 shallowColSlice := make ([]* expression.Column , p .schema .Len ())
214227 copy (shallowColSlice , p .schema .Columns )
215228 p .schema = expression .NewSchema (shallowColSlice ... )
216- for i := 0 ; i < colsNeedResolving ; i ++ {
217- newCol , err := p .schema .Columns [i ].ResolveIndices (mergedSchema )
218- if err != nil {
219- return err
229+ foundCnt := 0
230+ // The two column sets are all ordered. And the colsNeedResolving is the subset of the mergedSchema.
231+ // So we can just move forward j if there's no matching is found.
232+ // We don't use the normal ResolvIndices here since there might be duplicate columns in the schema.
233+ // e.g. The schema of child_0 is [col0, col0, col1]
234+ // ResolveIndices will only resolve all col0 reference of the current plan to the first col0.
235+ for i , j := 0 , 0 ; i < colsNeedResolving && j < len (mergedSchema .Columns ); {
236+ if ! p .schema .Columns [i ].Equal (nil , mergedSchema .Columns [j ]) {
237+ j ++
238+ continue
220239 }
221- p .schema .Columns [i ] = newCol .(* expression.Column )
240+ p .schema .Columns [i ] = p .schema .Columns [i ].Clone ().(* expression.Column )
241+ p .schema .Columns [i ].Index = j
242+ i ++
243+ j ++
244+ foundCnt ++
245+ }
246+ if foundCnt < colsNeedResolving {
247+ return errors .Errorf ("Some columns of %v cannot find the reference from its child(ren)" , p .ExplainID ().String ())
222248 }
223249 return
224250}
@@ -296,12 +322,25 @@ func (p *PhysicalIndexJoin) ResolveIndices() (err error) {
296322 shallowColSlice := make ([]* expression.Column , p .schema .Len ())
297323 copy (shallowColSlice , p .schema .Columns )
298324 p .schema = expression .NewSchema (shallowColSlice ... )
299- for i := 0 ; i < colsNeedResolving ; i ++ {
300- newCol , err := p .schema .Columns [i ].ResolveIndices (mergedSchema )
301- if err != nil {
302- return err
325+ foundCnt := 0
326+ // The two column sets are all ordered. And the colsNeedResolving is the subset of the mergedSchema.
327+ // So we can just move forward j if there's no matching is found.
328+ // We don't use the normal ResolvIndices here since there might be duplicate columns in the schema.
329+ // e.g. The schema of child_0 is [col0, col0, col1]
330+ // ResolveIndices will only resolve all col0 reference of the current plan to the first col0.
331+ for i , j := 0 , 0 ; i < colsNeedResolving && j < len (mergedSchema .Columns ); {
332+ if ! p .schema .Columns [i ].Equal (nil , mergedSchema .Columns [j ]) {
333+ j ++
334+ continue
303335 }
304- p .schema .Columns [i ] = newCol .(* expression.Column )
336+ p .schema .Columns [i ] = p .schema .Columns [i ].Clone ().(* expression.Column )
337+ p .schema .Columns [i ].Index = j
338+ i ++
339+ j ++
340+ foundCnt ++
341+ }
342+ if foundCnt < colsNeedResolving {
343+ return errors .Errorf ("Some columns of %v cannot find the reference from its child(ren)" , p .ExplainID ().String ())
305344 }
306345
307346 return
@@ -687,12 +726,25 @@ func (p *PhysicalLimit) ResolveIndices() (err error) {
687726 shallowColSlice := make ([]* expression.Column , p .schema .Len ())
688727 copy (shallowColSlice , p .schema .Columns )
689728 p .schema = expression .NewSchema (shallowColSlice ... )
690- for i , col := range p .schema .Columns {
691- newCol , err := col .ResolveIndices (p .children [0 ].Schema ())
692- if err != nil {
693- return err
729+ foundCnt := 0
730+ // The two column sets are all ordered. And the colsNeedResolving is the subset of the mergedSchema.
731+ // So we can just move forward j if there's no matching is found.
732+ // We don't use the normal ResolvIndices here since there might be duplicate columns in the schema.
733+ // e.g. The schema of child_0 is [col0, col0, col1]
734+ // ResolveIndices will only resolve all col0 reference of the current plan to the first col0.
735+ for i , j := 0 , 0 ; i < p .schema .Len () && j < p .children [0 ].Schema ().Len (); {
736+ if ! p .schema .Columns [i ].Equal (nil , p .children [0 ].Schema ().Columns [j ]) {
737+ j ++
738+ continue
694739 }
695- p .schema .Columns [i ] = newCol .(* expression.Column )
740+ p .schema .Columns [i ] = p .schema .Columns [i ].Clone ().(* expression.Column )
741+ p .schema .Columns [i ].Index = j
742+ i ++
743+ j ++
744+ foundCnt ++
745+ }
746+ if foundCnt < p .schema .Len () {
747+ return errors .Errorf ("Some columns of %v cannot find the reference from its child(ren)" , p .ExplainID ().String ())
696748 }
697749 return
698750}
0 commit comments