Skip to content

Commit c518a89

Browse files
authored
planner: refactor Join and Limit's ResolveIndices (#45831) (#46091)
close #45758, close #45805
1 parent 1f6c883 commit c518a89

File tree

2 files changed

+82
-20
lines changed

2 files changed

+82
-20
lines changed

planner/core/issuetest/planner_issue_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ func TestIssue42732(t *testing.T) {
4545
tk.MustExec("INSERT INTO t2 VALUES (1, 1)")
4646
tk.MustQuery("SELECT one.a, one.b as b2 FROM t1 one ORDER BY (SELECT two.b FROM t2 two WHERE two.a = one.b)").Check(testkit.Rows("1 1"))
4747
}
48+
49+
func TestIssue45758(t *testing.T) {
50+
store := testkit.CreateMockStore(t)
51+
tk := testkit.NewTestKit(t, store)
52+
tk.MustExec("use test")
53+
tk.MustExec("CREATE TABLE tb1 (cid INT, code INT, class VARCHAR(10))")
54+
tk.MustExec("CREATE TABLE tb2 (cid INT, code INT, class VARCHAR(10))")
55+
// result ok
56+
tk.MustExec("UPDATE tb1, (SELECT code AS cid, code, MAX(class) AS class FROM tb2 GROUP BY code) tb3 SET tb1.cid = tb3.cid, tb1.code = tb3.code, tb1.class = tb3.class")
57+
}

planner/core/resolve_indices.go

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -631,12 +670,25 @@ func (p *PhysicalLimit) ResolveIndices() (err error) {
631670
shallowColSlice := make([]*expression.Column, p.schema.Len())
632671
copy(shallowColSlice, p.schema.Columns)
633672
p.schema = expression.NewSchema(shallowColSlice...)
634-
for i, col := range p.schema.Columns {
635-
newCol, err := col.ResolveIndices(p.children[0].Schema())
636-
if err != nil {
637-
return err
673+
foundCnt := 0
674+
// The two column sets are all ordered. And the colsNeedResolving is the subset of the mergedSchema.
675+
// So we can just move forward j if there's no matching is found.
676+
// We don't use the normal ResolvIndices here since there might be duplicate columns in the schema.
677+
// e.g. The schema of child_0 is [col0, col0, col1]
678+
// ResolveIndices will only resolve all col0 reference of the current plan to the first col0.
679+
for i, j := 0, 0; i < p.schema.Len() && j < p.children[0].Schema().Len(); {
680+
if !p.schema.Columns[i].Equal(nil, p.children[0].Schema().Columns[j]) {
681+
j++
682+
continue
638683
}
639-
p.schema.Columns[i] = newCol.(*expression.Column)
684+
p.schema.Columns[i] = p.schema.Columns[i].Clone().(*expression.Column)
685+
p.schema.Columns[i].Index = j
686+
i++
687+
j++
688+
foundCnt++
689+
}
690+
if foundCnt < p.schema.Len() {
691+
return errors.Errorf("Some columns of %v cannot find the reference from its child(ren)", p.ExplainID().String())
640692
}
641693
return
642694
}

0 commit comments

Comments
 (0)