Skip to content

Commit

Permalink
plan: fix concat in group statement (#7448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingyu Song authored Aug 21, 2018
1 parent 98d2a03 commit d455e6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions plan/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions plan/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d455e6a

Please sign in to comment.