From 506eb3b0c468d50e0c886135d4b8d22ccc9831b3 Mon Sep 17 00:00:00 2001 From: xuhuaiyu <391585975@qq.com> Date: Thu, 21 Nov 2019 11:26:25 +0800 Subject: [PATCH 1/3] executor: refine the check of onlyFullGroupBy when contains parentheses --- executor/aggregate_test.go | 10 ++++++++++ planner/core/logical_plan_builder.go | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index 94de5565715de..ee4bc7a2be67b 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -566,6 +566,16 @@ 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))") +} + 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 980365d8029ad..43529d9922759 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -1910,7 +1910,11 @@ 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 := byItem.Expr + if pe, ok := expr.(*ast.ParenthesesExpr); ok { + expr = getInnerFromParenthesesAndUnaryPlus(pe) + } + if colExpr, ok := expr.(*ast.ColumnNameExpr); ok { idx, err := expression.FindFieldName(p.OutputNames(), colExpr.Name) if err != nil || idx < 0 { continue From 9350e79b19d85228416b844aab5e9c76cae07b84 Mon Sep 17 00:00:00 2001 From: xuhuaiyu <391585975@qq.com> Date: Thu, 21 Nov 2019 11:27:52 +0800 Subject: [PATCH 2/3] refine code --- planner/core/logical_plan_builder.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 43529d9922759..36bcbed2d080f 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -1921,7 +1921,7 @@ func (b *PlanBuilder) checkOnlyFullGroupByWithGroupClause(p LogicalPlan, sel *as } gbyColNames[p.OutputNames()[idx]] = struct{}{} } else { - gbyExprs = append(gbyExprs, byItem.Expr) + gbyExprs = append(gbyExprs, expr) } } From 6f5d433fd4e91f78e82b2918ceb4ab83fa7a598c Mon Sep 17 00:00:00 2001 From: xuhuaiyu <391585975@qq.com> Date: Thu, 21 Nov 2019 12:22:14 +0800 Subject: [PATCH 3/3] address comment --- executor/aggregate_test.go | 4 ++++ planner/core/logical_plan_builder.go | 5 +---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/executor/aggregate_test.go b/executor/aggregate_test.go index ee4bc7a2be67b..f6e495f15faa2 100644 --- a/executor/aggregate_test.go +++ b/executor/aggregate_test.go @@ -574,6 +574,10 @@ func (s *testSuiteAgg) TestIssue13652(c *C) { 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) { diff --git a/planner/core/logical_plan_builder.go b/planner/core/logical_plan_builder.go index 36bcbed2d080f..4f15fd2692cb0 100644 --- a/planner/core/logical_plan_builder.go +++ b/planner/core/logical_plan_builder.go @@ -1910,10 +1910,7 @@ 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 { - expr := byItem.Expr - if pe, ok := expr.(*ast.ParenthesesExpr); ok { - expr = getInnerFromParenthesesAndUnaryPlus(pe) - } + expr := getInnerFromParenthesesAndUnaryPlus(byItem.Expr) if colExpr, ok := expr.(*ast.ColumnNameExpr); ok { idx, err := expression.FindFieldName(p.OutputNames(), colExpr.Name) if err != nil || idx < 0 {