Skip to content

Commit 5840bb8

Browse files
authored
planner: fix bug in PhysicalExchangeSender::ResolveIndicesItself (#60520) (#60551)
close #60517
1 parent 1540a2d commit 5840bb8

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

planner/core/physical_plan_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/pingcap/kvproto/pkg/metapb"
2626
"github.com/pingcap/tidb/domain"
2727
"github.com/pingcap/tidb/executor"
28+
"github.com/pingcap/tidb/expression"
2829
"github.com/pingcap/tidb/infoschema"
2930
"github.com/pingcap/tidb/kv"
3031
"github.com/pingcap/tidb/parser"
@@ -2615,3 +2616,36 @@ func TestAlwaysTruePredicateWithSubquery(t *testing.T) {
26152616
tk.MustQuery(ts).Check(testkit.Rows(output[i].Plan...))
26162617
}
26172618
}
2619+
2620+
func TestExchangeSenderResolveIndices(t *testing.T) {
2621+
schemaCols1 := make([]*expression.Column, 0, 4)
2622+
schemaCols1 = append(schemaCols1, &expression.Column{UniqueID: 1})
2623+
schemaCols1 = append(schemaCols1, &expression.Column{UniqueID: 2})
2624+
schemaCols1 = append(schemaCols1, &expression.Column{UniqueID: 3})
2625+
schemaCols1 = append(schemaCols1, &expression.Column{UniqueID: 4})
2626+
schema1 := expression.NewSchema(schemaCols1...)
2627+
2628+
schemaCols2 := make([]*expression.Column, 0, 2)
2629+
schemaCols2 = append(schemaCols2, &expression.Column{UniqueID: 3})
2630+
schemaCols2 = append(schemaCols2, &expression.Column{UniqueID: 4})
2631+
schema2 := expression.NewSchema(schemaCols2...)
2632+
2633+
partitionCol1 := &property.MPPPartitionColumn{Col: &expression.Column{UniqueID: 4}}
2634+
2635+
// two exchange sender share the same MPPPartitionColumn
2636+
exchangeSender1 := &core.PhysicalExchangeSender{
2637+
HashCols: []*property.MPPPartitionColumn{partitionCol1},
2638+
}
2639+
exchangeSender2 := &core.PhysicalExchangeSender{
2640+
HashCols: []*property.MPPPartitionColumn{partitionCol1},
2641+
}
2642+
2643+
err := exchangeSender1.ResolveIndicesItselfWithSchema(schema1)
2644+
require.NoError(t, err)
2645+
2646+
err = exchangeSender2.ResolveIndicesItselfWithSchema(schema2)
2647+
require.NoError(t, err)
2648+
2649+
// after resolving, the partition col in two different exchange sender should have different index
2650+
require.NotEqual(t, exchangeSender1.HashCols[0].Col.Index, exchangeSender2.HashCols[0].Col.Index)
2651+
}

planner/core/resolve_indices.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -490,12 +490,17 @@ func (p *PhysicalSelection) ResolveIndices() (err error) {
490490

491491
// ResolveIndicesItself resolve indices for PhyicalPlan itself
492492
func (p *PhysicalExchangeSender) ResolveIndicesItself() (err error) {
493-
for i, col := range p.HashCols {
494-
colExpr, err1 := col.Col.ResolveIndices(p.children[0].Schema())
495-
if err1 != nil {
496-
return err1
493+
return p.ResolveIndicesItselfWithSchema(p.children[0].Schema())
494+
}
495+
496+
// ResolveIndicesItselfWithSchema is added for test usage
497+
func (p *PhysicalExchangeSender) ResolveIndicesItselfWithSchema(inputSchema *expression.Schema) (err error) {
498+
for i, hashCol := range p.HashCols {
499+
newHashCol, err := hashCol.ResolveIndices(inputSchema)
500+
if err != nil {
501+
return err
497502
}
498-
p.HashCols[i].Col, _ = colExpr.(*expression.Column)
503+
p.HashCols[i] = newHashCol
499504
}
500505
return
501506
}

planner/property/physical_property.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ func (partitionCol *MPPPartitionColumn) hashCode(ctx *stmtctx.StatementContext)
105105
return hashcode
106106
}
107107

108+
// ResolveIndices resolve index for MPPPartitionColumn
109+
func (partitionCol *MPPPartitionColumn) ResolveIndices(schema *expression.Schema) (*MPPPartitionColumn, error) {
110+
newColExpr, err := partitionCol.Col.ResolveIndices(schema)
111+
if err != nil {
112+
return nil, err
113+
}
114+
newCol, _ := newColExpr.(*expression.Column)
115+
return &MPPPartitionColumn{
116+
Col: newCol,
117+
CollateID: partitionCol.CollateID,
118+
}, nil
119+
}
120+
108121
// Equal returns true if partitionCol == other
109122
func (partitionCol *MPPPartitionColumn) Equal(other *MPPPartitionColumn) bool {
110123
if partitionCol.CollateID < 0 {

0 commit comments

Comments
 (0)