Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#36012
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
AilinKid committed Feb 19, 2024
1 parent 3b7f150 commit b672ffd
Show file tree
Hide file tree
Showing 3 changed files with 2,757 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,52 @@ func (b *PlanBuilder) coalesceCommonColumns(p *LogicalJoin, leftPlan, rightPlan
if err != nil {
return err
}
} else {
// Even with no using filter, we still should check the checkAmbiguous name before we try to find the common column from both side.
// (t3 cross join t4) natural join t1
// t1 natural join (t3 cross join t4)
// t3 and t4 may generate the same name column from cross join.
// for every common column of natural join, the name from right or left should be exactly one.
commonNames := make([]string, 0, len(lNames))
lNameMap := make(map[string]int, len(lNames))
rNameMap := make(map[string]int, len(rNames))
for _, name := range lNames {
// Natural join should ignore _tidb_rowid
if name.ColName.L == "_tidb_rowid" {
continue
}
// record left map
if cnt, ok := lNameMap[name.ColName.L]; ok {
lNameMap[name.ColName.L] = cnt + 1
} else {
lNameMap[name.ColName.L] = 1
}
}
for _, name := range rNames {
// Natural join should ignore _tidb_rowid
if name.ColName.L == "_tidb_rowid" {
continue
}
// record right map
if cnt, ok := rNameMap[name.ColName.L]; ok {
rNameMap[name.ColName.L] = cnt + 1
} else {
rNameMap[name.ColName.L] = 1
}
// check left map
if cnt, ok := lNameMap[name.ColName.L]; ok {
if cnt > 1 {
return plannererrors.ErrAmbiguous.GenWithStackByArgs(name.ColName.L, "from clause")
}
commonNames = append(commonNames, name.ColName.L)
}
}
// check right map
for _, commonName := range commonNames {
if rNameMap[commonName] > 1 {
return plannererrors.ErrAmbiguous.GenWithStackByArgs(commonName, "from clause")
}
}
}

// Find out all the common columns and put them ahead.
Expand Down
Loading

0 comments on commit b672ffd

Please sign in to comment.