diff --git a/planner/core/integration_test.go b/planner/core/integration_test.go index fcb116e2403ec..3e1b09e4f6155 100644 --- a/planner/core/integration_test.go +++ b/planner/core/integration_test.go @@ -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) diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 6b1acac4355a3..ffca81ff5eb52 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -3096,12 +3096,23 @@ func (g *gbyResolver) Leave(inNode ast.Node) (ast.Node, bool) { 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 + } + tblInfo := tbl.Meta() + return tblInfo } } return nil @@ -3157,7 +3168,7 @@ func buildWhereFuncDepend(p LogicalPlan, where ast.ExprNode) (map[*types.FieldNa 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 { @@ -3173,7 +3184,7 @@ func buildJoinFuncDepend(p LogicalPlan, from ast.ResultSetNode) (map[*types.Fiel if lCol == nil || rCol == nil { continue } - lTbl := tblInfoFromCol(x.Left, lCol) + lTbl := b.tblInfoFromCol(x.Left, lCol) if lTbl == nil { lCol, rCol = rCol, lCol } @@ -3379,7 +3390,7 @@ func extractSingeValueColNamesFromWhere(p LogicalPlan, where ast.ExprNode, gbyOr } } -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 { @@ -3426,13 +3437,13 @@ func (*PlanBuilder) checkOnlyFullGroupByWithGroupClause(p LogicalPlan, sel *ast. 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 } @@ -3454,7 +3465,7 @@ func (*PlanBuilder) checkOnlyFullGroupByWithGroupClause(p LogicalPlan, sel *ast. 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, } @@ -3489,7 +3500,7 @@ func (*PlanBuilder) checkOnlyFullGroupByWithOutGroupClause(p LogicalPlan, sel *a return err } - joinDepends, err := buildJoinFuncDepend(p, sel.From.TableRefs) + joinDepends, err := b.buildJoinFuncDepend(p, sel.From.TableRefs) if err != nil { return err } @@ -3503,7 +3514,7 @@ func (*PlanBuilder) checkOnlyFullGroupByWithOutGroupClause(p LogicalPlan, sel *a if _, ok := singleValueColNames[fieldName]; ok { continue } - tblInfo := tblInfoFromCol(sel.From.TableRefs, fieldName) + tblInfo := b.tblInfoFromCol(sel.From.TableRefs, fieldName) if tblInfo == nil { continue }