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: ONLY_FULL_GROUP_BY sql_mode was not working with VIEWs (#57473) #59322

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 15 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8806,6 +8806,21 @@ func TestIssue40285(t *testing.T) {
tk.MustQuery("(select last_value(col1) over () as r0 from t) union all (select col2 as r0 from t);")
}

func TestIssue53175(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec(`create table t(a int)`)
tk.MustExec(`set @@sql_mode = default`)
tk.MustQuery(`select @@sql_mode REGEXP 'ONLY_FULL_GROUP_BY'`).Check(testkit.Rows("1"))
tk.MustContainErrMsg(`select * from t group by null`, "[planner:1055]Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by")
tk.MustExec(`create view v as select * from t group by null`)
tk.MustContainErrMsg(`select * from v`, "[planner:1055]Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by")
tk.MustExec(`set @@sql_mode = ''`)
tk.MustQuery(`select * from t group by null`)
tk.MustQuery(`select * from v`)
}

func TestCastBitAsString(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
31 changes: 21 additions & 10 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3096,12 +3096,23 @@
return inNode, true
}

func tblInfoFromCol(from ast.ResultSetNode, name *types.FieldName) *model.TableInfo {
func (b *PlanBuilder) tblInfoFromCol(from ast.ResultSetNode, name *types.FieldName) *model.TableInfo {
var tableList []*ast.TableName
tableList = extractTableList(from, tableList, true)
for _, field := range tableList {
if field.Name.L == name.TblName.L {
return field.TableInfo
if field.TableInfo != nil {
return field.TableInfo
}
if b.isCreateView {
return nil
}
tbl, err := b.is.TableByName(name.DBName, name.TblName)
if err != nil {
return nil
}

Check warning on line 3113 in planner/core/logical_plan_builder.go

View check run for this annotation

Codecov / codecov/patch

planner/core/logical_plan_builder.go#L3112-L3113

Added lines #L3112 - L3113 were not covered by tests
tblInfo := tbl.Meta()
return tblInfo
}
}
return nil
Expand Down Expand Up @@ -3157,7 +3168,7 @@
return colDependMap, nil
}

func buildJoinFuncDepend(p LogicalPlan, from ast.ResultSetNode) (map[*types.FieldName]*types.FieldName, error) {
func (b *PlanBuilder) buildJoinFuncDepend(p LogicalPlan, from ast.ResultSetNode) (map[*types.FieldName]*types.FieldName, error) {
switch x := from.(type) {
case *ast.Join:
if x.On == nil {
Expand All @@ -3173,7 +3184,7 @@
if lCol == nil || rCol == nil {
continue
}
lTbl := tblInfoFromCol(x.Left, lCol)
lTbl := b.tblInfoFromCol(x.Left, lCol)
if lTbl == nil {
lCol, rCol = rCol, lCol
}
Expand Down Expand Up @@ -3379,7 +3390,7 @@
}
}

func (*PlanBuilder) checkOnlyFullGroupByWithGroupClause(p LogicalPlan, sel *ast.SelectStmt) error {
func (b *PlanBuilder) checkOnlyFullGroupByWithGroupClause(p LogicalPlan, sel *ast.SelectStmt) error {
gbyOrSingleValueColNames := make(map[*types.FieldName]struct{}, len(sel.Fields.Fields))
gbyExprs := make([]ast.ExprNode, 0, len(sel.Fields.Fields))
for _, byItem := range sel.GroupBy.Items {
Expand Down Expand Up @@ -3426,13 +3437,13 @@
if err != nil {
return err
}
joinDepends, err := buildJoinFuncDepend(p, sel.From.TableRefs)
joinDepends, err := b.buildJoinFuncDepend(p, sel.From.TableRefs)
if err != nil {
return err
}
tblMap := make(map[*model.TableInfo]struct{}, len(notInGbyOrSingleValueColNames))
for name, errExprLoc := range notInGbyOrSingleValueColNames {
tblInfo := tblInfoFromCol(sel.From.TableRefs, name)
tblInfo := b.tblInfoFromCol(sel.From.TableRefs, name)
if tblInfo == nil {
continue
}
Expand All @@ -3454,7 +3465,7 @@
return nil
}

func (*PlanBuilder) checkOnlyFullGroupByWithOutGroupClause(p LogicalPlan, sel *ast.SelectStmt) error {
func (b *PlanBuilder) checkOnlyFullGroupByWithOutGroupClause(p LogicalPlan, sel *ast.SelectStmt) error {
resolver := colResolverForOnlyFullGroupBy{
firstOrderByAggColIdx: -1,
}
Expand Down Expand Up @@ -3489,7 +3500,7 @@
return err
}

joinDepends, err := buildJoinFuncDepend(p, sel.From.TableRefs)
joinDepends, err := b.buildJoinFuncDepend(p, sel.From.TableRefs)
if err != nil {
return err
}
Expand All @@ -3503,7 +3514,7 @@
if _, ok := singleValueColNames[fieldName]; ok {
continue
}
tblInfo := tblInfoFromCol(sel.From.TableRefs, fieldName)
tblInfo := b.tblInfoFromCol(sel.From.TableRefs, fieldName)
if tblInfo == nil {
continue
}
Expand Down