Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: fix unknown columns in join using below agg (#21922) #21957

Merged
merged 4 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion executor/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,10 +631,19 @@ func (s *testSuiteJoin1) TestUsing(c *C) {
tk.MustQuery("select t1.*, t2.* from t1 join t1 t2 using(a)").Check(testkit.Rows("1 1"))
tk.MustQuery("select * from t1 join t1 t2 using(a)").Check(testkit.Rows("1"))

// For issue18992
// For issue 18992
tk.MustExec("drop table t")
tk.MustExec("CREATE TABLE t ( a varchar(55) NOT NULL, b varchar(55) NOT NULL, c int(11) DEFAULT NULL, d int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;")
tk.MustExec("update t t1 join t t2 using(a,b) set t1.c=t2.d;")

// For issue 6712
tk.MustExec("drop table if exists t1,t2")
tk.MustExec("create table t1 (t1 int , t0 int)")
tk.MustExec("create table t2 (t2 int, t0 int)")
tk.MustExec("insert into t1 select 11, 1")
tk.MustExec("insert into t2 select 22, 1")
tk.MustQuery("select t1.t0, t2.t0 from t1 join t2 using(t0) group by t1.t0").Check(testkit.Rows("1 1"))
tk.MustQuery("select t1.t0, t2.t0 from t1 join t2 using(t0) having t1.t0 > 0").Check(testkit.Rows("1 1"))
}

func (s *testSuiteJoin1) TestNaturalJoin(c *C) {
Expand Down
16 changes: 16 additions & 0 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,22 @@ func (b *PlanBuilder) buildAggregation(ctx context.Context, p LogicalPlan, aggFu
schema4Agg.Append(newCol)
names = append(names, p.OutputNames()[i])
}
if join, isJoin := p.(*LogicalJoin); isJoin && join.redundantSchema != nil {
for i, col := range join.redundantSchema.Columns {
if p.Schema().Contains(col) {
continue
}
newFunc, err := aggregation.NewAggFuncDesc(b.ctx, ast.AggFuncFirstRow, []expression.Expression{col}, false)
if err != nil {
return nil, nil, err
}
plan4Agg.AggFuncs = append(plan4Agg.AggFuncs, newFunc)
newCol, _ := col.Clone().(*expression.Column)
newCol.RetType = newFunc.RetTp
schema4Agg.Append(newCol)
names = append(names, join.redundantNames[i])
}
}
hasGroupBy := len(gbyItems) > 0
for i, aggFunc := range plan4Agg.AggFuncs {
err := aggFunc.UpdateNotNullFlag4RetType(hasGroupBy, allAggsFirstRow)
Expand Down