From d455e6ac5a9a1c5c9dbe5fd145d7586f2f06708b Mon Sep 17 00:00:00 2001 From: Lingyu Song Date: Tue, 21 Aug 2018 19:21:13 +0800 Subject: [PATCH] plan: fix concat in group statement (#7448) --- plan/logical_plan_builder.go | 7 +++++++ plan/logical_plan_test.go | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/plan/logical_plan_builder.go b/plan/logical_plan_builder.go index 5f18b30bff543..db807b5315e11 100644 --- a/plan/logical_plan_builder.go +++ b/plan/logical_plan_builder.go @@ -854,6 +854,13 @@ func matchField(f *ast.SelectField, col *ast.ColumnNameExpr, ignoreAsName bool) if f.AsName.L == "" || ignoreAsName { if curCol, isCol := f.Expr.(*ast.ColumnNameExpr); isCol { return curCol.Name.Name.L == col.Name.Name.L + } else if _, isFunc := f.Expr.(*ast.FuncCallExpr); isFunc { + // Fix issue 7331 + // If there are some function calls in SelectField, we check if + // ColumnNameExpr in GroupByClause matches one of these function calls. + // Example: select concat(k1,k2) from t group by `concat(k1,k2)`, + // `concat(k1,k2)` matches with function call concat(k1, k2). + return strings.ToLower(f.Text()) == col.Name.Name.L } // a expression without as name can't be matched. return false diff --git a/plan/logical_plan_test.go b/plan/logical_plan_test.go index c0615ffe6ed2e..d831699064374 100644 --- a/plan/logical_plan_test.go +++ b/plan/logical_plan_test.go @@ -1171,6 +1171,14 @@ func (s *testPlanSuite) TestValidate(c *C) { sql: "select a from t having sum(avg(a))", err: ErrInvalidGroupFuncUse, }, + { + sql: "select concat(c_str, d_str) from t group by `concat(c_str, d_str)`", + err: nil, + }, + { + sql: "select concat(c_str, d_str) from t group by `concat(c_str,d_str)`", + err: ErrUnknownColumn, + }, } for _, tt := range tests { sql := tt.sql