From 2525e52f4c245e67b41006cf921a473236b9ac1c Mon Sep 17 00:00:00 2001 From: HuaiyuXu <391585975@qq.com> Date: Thu, 21 Nov 2019 12:39:07 +0800 Subject: [PATCH] executor: refine the check of onlyFullGroupBy when groupByItem is parenthesesExpr or UnaryPlusExpr (#13655) --- executor/aggregate_test.go | 14 ++++++++++++++ planner/core/logical_plan_builder.go | 5 +++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index 94de5565715de..f6e495f15faa2 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -566,6 +566,20 @@ func (s *testSuiteAgg) TestOnlyFullGroupBy(c *C) { c.Assert(terror.ErrorEqual(err, plannercore.ErrAmbiguous), IsTrue, Commentf("err %v", err)) } +func (s *testSuiteAgg) TestIssue13652(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("set sql_mode = 'ONLY_FULL_GROUP_BY'") + tk.MustExec("drop table if exists t") + tk.MustExec("create table t(a real)") + tk.MustQuery("select a from t group by (a)") + tk.MustQuery("select a from t group by ((a))") + tk.MustQuery("select a from t group by +a") + tk.MustQuery("select a from t group by ((+a))") + _, err := tk.Exec("select a from t group by (-a)") + c.Assert(err.Error(), Equals, "[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") +} + func (s *testSuiteAgg) TestHaving(c *C) { tk := testkit.NewTestKitWithInit(c, s.store) diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 924363cf1e2d1..a971d3053e01f 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -1910,14 +1910,15 @@ func (b *PlanBuilder) checkOnlyFullGroupByWithGroupClause(p LogicalPlan, sel *as gbyColNames := make(map[*types.FieldName]struct{}, len(sel.Fields.Fields)) gbyExprs := make([]ast.ExprNode, 0, len(sel.Fields.Fields)) for _, byItem := range sel.GroupBy.Items { - if colExpr, ok := byItem.Expr.(*ast.ColumnNameExpr); ok { + expr := getInnerFromParenthesesAndUnaryPlus(byItem.Expr) + if colExpr, ok := expr.(*ast.ColumnNameExpr); ok { idx, err := expression.FindFieldName(p.OutputNames(), colExpr.Name) if err != nil || idx < 0 { continue } gbyColNames[p.OutputNames()[idx]] = struct{}{} } else { - gbyExprs = append(gbyExprs, byItem.Expr) + gbyExprs = append(gbyExprs, expr) } }