Skip to content

Commit

Permalink
plan: fix bugs when do natural join or join with using. (pingcap#4382)
Browse files Browse the repository at this point in the history
* plan: fix bug when do natural join or join with using.
  • Loading branch information
winoros authored and zimulala committed Aug 31, 2017
1 parent d739d7f commit 87baaaf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
13 changes: 13 additions & 0 deletions executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,19 @@ func (s *testSuite) TestUsing(c *C) {
tk.MustQuery("select * from t1 join t2 using (b, a)").Check(testkit.Rows("2 1 4 5"))

tk.MustExec("select * from (t1 join t2 using (a)) join (t3 join t4 using (a)) on (t2.a = t4.a and t1.a = t3.a)")

tk.MustExec("drop table if exists t, tt")
tk.MustExec("create table t(a int, b int)")
tk.MustExec("create table tt(b int, a int)")
tk.MustExec("insert into t (a, b) values(1, 1)")
tk.MustExec("insert into tt (a, b) values(1, 2)")
tk.MustQuery("select * from t join tt using(a)").Check(testkit.Rows("1 1 2"))

tk.MustExec("drop table if exists t, tt")
tk.MustExec("create table t(a float, b int)")
tk.MustExec("create table tt(b bigint, a int)")
// Check whether this sql can execute successfully.
tk.MustExec("select * from t join tt using(a)")
}

func (s *testSuite) TestNaturalJoin(c *C) {
Expand Down
18 changes: 9 additions & 9 deletions plan/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,14 @@ func (b *planBuilder) coalesceCommonColumns(p *LogicalJoin, leftPlan, rightPlan
filter[lCol.ColName.L] = false
}

col := rColumns[i]
copy(rColumns[commonLen+1:i+1], rColumns[commonLen:i])
rColumns[commonLen] = col

col = lColumns[j]
copy(lColumns[commonLen+1:j+1], lColumns[commonLen:j])
col := lColumns[i]
copy(lColumns[commonLen+1:i+1], lColumns[commonLen:i])
lColumns[commonLen] = col

col = rColumns[j]
copy(rColumns[commonLen+1:j+1], rColumns[commonLen:j])
rColumns[commonLen] = col

commonLen++
break
}
Expand All @@ -369,19 +369,19 @@ func (b *planBuilder) coalesceCommonColumns(p *LogicalJoin, leftPlan, rightPlan
copy(schemaCols[:len(lColumns)], lColumns)
copy(schemaCols[len(lColumns):], rColumns[commonLen:])

conds := make([]*expression.ScalarFunction, 0, commonLen)
conds := make([]expression.Expression, 0, commonLen)
for i := 0; i < commonLen; i++ {
lc, rc := lsc.Columns[i], rsc.Columns[i]
cond, err := expression.NewFunction(b.ctx, ast.EQ, types.NewFieldType(mysql.TypeTiny), lc, rc)
if err != nil {
return errors.Trace(err)
}
conds = append(conds, cond.(*expression.ScalarFunction))
conds = append(conds, cond)
}

p.SetSchema(expression.NewSchema(schemaCols...))
p.redundantSchema = expression.MergeSchema(p.redundantSchema, expression.NewSchema(rColumns[:commonLen]...))
p.EqualConditions = append(conds, p.EqualConditions...)
p.OtherConditions = append(conds, p.OtherConditions...)

return nil
}
Expand Down

0 comments on commit 87baaaf

Please sign in to comment.