Skip to content

Commit 0007a6c

Browse files
authored
planner: fix bug in PhysicalExchangeSender::ResolveIndicesItself (#60520) (#60550)
close #60517
1 parent 9fef7c5 commit 0007a6c

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

pkg/planner/core/physical_plan_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,36 @@ func TestPhysicalTableScanExtractCorrelatedCols(t *testing.T) {
517517
require.Equal(t, 1, len(correlated))
518518
require.Equal(t, "test.t2.company_no", correlated[0].StringWithCtx(errors.RedactLogDisable))
519519
}
520+
521+
func TestExchangeSenderResolveIndices(t *testing.T) {
522+
schemaCols1 := make([]*expression.Column, 0, 4)
523+
schemaCols1 = append(schemaCols1, &expression.Column{UniqueID: 1})
524+
schemaCols1 = append(schemaCols1, &expression.Column{UniqueID: 2})
525+
schemaCols1 = append(schemaCols1, &expression.Column{UniqueID: 3})
526+
schemaCols1 = append(schemaCols1, &expression.Column{UniqueID: 4})
527+
schema1 := expression.NewSchema(schemaCols1...)
528+
529+
schemaCols2 := make([]*expression.Column, 0, 2)
530+
schemaCols2 = append(schemaCols2, &expression.Column{UniqueID: 3})
531+
schemaCols2 = append(schemaCols2, &expression.Column{UniqueID: 4})
532+
schema2 := expression.NewSchema(schemaCols2...)
533+
534+
partitionCol1 := &property.MPPPartitionColumn{Col: &expression.Column{UniqueID: 4}}
535+
536+
// two exchange sender share the same MPPPartitionColumn
537+
exchangeSender1 := &core.PhysicalExchangeSender{
538+
HashCols: []*property.MPPPartitionColumn{partitionCol1},
539+
}
540+
exchangeSender2 := &core.PhysicalExchangeSender{
541+
HashCols: []*property.MPPPartitionColumn{partitionCol1},
542+
}
543+
544+
err := exchangeSender1.ResolveIndicesItselfWithSchema(schema1)
545+
require.NoError(t, err)
546+
547+
err = exchangeSender2.ResolveIndicesItselfWithSchema(schema2)
548+
require.NoError(t, err)
549+
550+
// after resolving, the partition col in two different exchange sender should have different index
551+
require.NotEqual(t, exchangeSender1.HashCols[0].Col.Index, exchangeSender2.HashCols[0].Col.Index)
552+
}

pkg/planner/core/resolve_indices.go

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

506506
// ResolveIndicesItself resolve indices for PhysicalPlan itself
507507
func (p *PhysicalExchangeSender) ResolveIndicesItself() (err error) {
508-
for i, col := range p.HashCols {
509-
colExpr, err1 := col.Col.ResolveIndices(p.children[0].Schema())
510-
if err1 != nil {
511-
return err1
508+
return p.ResolveIndicesItselfWithSchema(p.children[0].Schema())
509+
}
510+
511+
// ResolveIndicesItselfWithSchema is added for test usage
512+
func (p *PhysicalExchangeSender) ResolveIndicesItselfWithSchema(inputSchema *expression.Schema) (err error) {
513+
for i, hashCol := range p.HashCols {
514+
newHashCol, err := hashCol.ResolveIndices(inputSchema)
515+
if err != nil {
516+
return err
512517
}
513-
p.HashCols[i].Col, _ = colExpr.(*expression.Column)
518+
p.HashCols[i] = newHashCol
514519
}
515520
return
516521
}

pkg/planner/property/physical_property.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ type MPPPartitionColumn struct {
9393
CollateID int32
9494
}
9595

96+
// ResolveIndices resolve index for MPPPartitionColumn
97+
func (partitionCol *MPPPartitionColumn) ResolveIndices(schema *expression.Schema) (*MPPPartitionColumn, error) {
98+
newColExpr, err := partitionCol.Col.ResolveIndices(schema)
99+
if err != nil {
100+
return nil, err
101+
}
102+
newCol, _ := newColExpr.(*expression.Column)
103+
return &MPPPartitionColumn{
104+
Col: newCol,
105+
CollateID: partitionCol.CollateID,
106+
}, nil
107+
}
108+
96109
func (partitionCol *MPPPartitionColumn) hashCode() []byte {
97110
hashcode := partitionCol.Col.HashCode()
98111
if partitionCol.CollateID < 0 {

0 commit comments

Comments
 (0)