Skip to content

Commit

Permalink
planner/core: give the full schema information when group by fields n…
Browse files Browse the repository at this point in the history
…ot exists (#11406) (#11455)
  • Loading branch information
sre-bot authored Aug 6, 2019
1 parent 4fb7f3f commit 8642e9b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
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

0 comments on commit 8642e9b

Please sign in to comment.