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/core: give the full schema information when group by fields not exists (#11406) #11455

Merged
merged 7 commits into from
Aug 6, 2019
2 changes: 1 addition & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,7 @@ func (b *PlanBuilder) checkOnlyFullGroupByWithGroupClause(p LogicalPlan, sel *as
}
switch errExprLoc.Loc {
case ErrExprInSelect:
return ErrFieldNotInGroupBy.GenWithStackByArgs(errExprLoc.Offset+1, errExprLoc.Loc, sel.Fields.Fields[errExprLoc.Offset].Text())
return ErrFieldNotInGroupBy.GenWithStackByArgs(errExprLoc.Offset+1, errExprLoc.Loc, col.DBName.O+"."+col.TblName.O+"."+col.OrigColName.O)
case ErrExprInOrderBy:
return ErrFieldNotInGroupBy.GenWithStackByArgs(errExprLoc.Offset+1, errExprLoc.Loc, sel.OrderBy.Items[errExprLoc.Offset].Expr.Text())
}
Expand Down
65 changes: 65 additions & 0 deletions planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,71 @@ func (s *testPlanSuite) TestDeriveNotNullConds(c *C) {
}
}

func buildLogicPlan4GroupBy(s *testPlanSuite, c *C, sql string) (Plan, error) {
sqlMode := s.ctx.GetSessionVars().SQLMode
mockedTableInfo := MockTable()
// mock the table info here for later use
// enable only full group by
s.ctx.GetSessionVars().SQLMode = sqlMode | mysql.ModeOnlyFullGroupBy
defer func() { s.ctx.GetSessionVars().SQLMode = sqlMode }() // restore it
comment := Commentf("for %s", sql)
stmt, err := s.ParseOneStmt(sql, "", "")
c.Assert(err, IsNil, comment)

stmt.(*ast.SelectStmt).From.TableRefs.Left.(*ast.TableSource).Source.(*ast.TableName).TableInfo = mockedTableInfo

return BuildLogicalPlan(context.Background(), s.ctx, stmt, s.is)
}

func (s *testPlanSuite) TestGroupByWhenNotExistCols(c *C) {
sqlTests := []struct {
sql string
expectedErrMatch string
}{
{
sql: "select a from t group by b",
expectedErrMatch: ".*contains nonaggregated column 'test\\.t\\.a'.*",
},
{
// has an as column alias
sql: "select a as tempField from t group by b",
expectedErrMatch: ".*contains nonaggregated column 'test\\.t\\.a'.*",
},
{
// has as table alias
sql: "select tempTable.a from t as tempTable group by b",
expectedErrMatch: ".*contains nonaggregated column 'test\\.tempTable\\.a'.*",
},
{
// has a func call
sql: "select length(a) from t group by b",
expectedErrMatch: ".*contains nonaggregated column 'test\\.t\\.a'.*",
},
{
// has a func call with two cols
sql: "select length(b + a) from t group by b",
expectedErrMatch: ".*contains nonaggregated column 'test\\.t\\.a'.*",
},
{
// has a func call with two cols
sql: "select length(a + b) from t group by b",
expectedErrMatch: ".*contains nonaggregated column 'test\\.t\\.a'.*",
},
{
// has a func call with two cols
sql: "select length(a + b) as tempField from t group by b",
expectedErrMatch: ".*contains nonaggregated column 'test\\.t\\.a'.*",
},
}
for _, test := range sqlTests {
sql := test.sql
p, err := buildLogicPlan4GroupBy(s, c, sql)
c.Assert(err, NotNil)
c.Assert(p, IsNil)
c.Assert(err, ErrorMatches, test.expectedErrMatch)
}
}

func (s *testPlanSuite) TestDupRandJoinCondsPushDown(c *C) {
sql := "select * from t as t1 join t t2 on t1.a > rand() and t1.a > rand()"
comment := Commentf("for %s", sql)
Expand Down