Skip to content

Commit

Permalink
parser: fix aggregate functions. (pingcap#2453)
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored Jan 13, 2017
1 parent 554293a commit afeccd0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 28 deletions.
43 changes: 15 additions & 28 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -2507,7 +2507,10 @@ FunctionCallConflict:
}

DistinctOpt:
"ALL"
{
$$ = false
}
| "ALL"
{
$$ = false
}
Expand Down Expand Up @@ -3138,21 +3141,25 @@ TrimDirection:
}

FunctionCallAgg:
"AVG" '(' Expression ')'
"AVG" '(' DistinctOpt Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4.(ast.ExprNode)}, Distinct: $3.(bool)}
}
| "BIT_XOR" '(' Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$3.(ast.ExprNode)}}
}
| "AVG" '(' DistinctOpt ExpressionList ')'
| "COUNT" '(' "DISTINCT" ExpressionList ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: $4.([]ast.ExprNode), Distinct: $3.(bool)}
$$ = &ast.AggregateFuncExpr{F: $1, Args: $4.([]ast.ExprNode), Distinct: true}
}
| "COUNT" '(' Expression ')'
| "COUNT" '(' "ALL" Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$3.(ast.ExprNode)}}
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4.(ast.ExprNode)}}
}
| "COUNT" '(' DistinctOpt ExpressionList ')'
| "COUNT" '(' Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: $4.([]ast.ExprNode), Distinct: $3.(bool)}
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$3.(ast.ExprNode)}}
}
| "COUNT" '(' '*' ')'
{
Expand All @@ -3163,38 +3170,18 @@ FunctionCallAgg:
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: $4.([]ast.ExprNode), Distinct: $3.(bool)}
}
| "MAX" '(' Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$3.(ast.ExprNode)}}
}
| "MAX" '(' DistinctOpt Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4.(ast.ExprNode)}, Distinct: $3.(bool)}
}
| "MIN" '(' Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$3.(ast.ExprNode)}}
}
| "MIN" '(' DistinctOpt Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4.(ast.ExprNode)}, Distinct: $3.(bool)}
}
| "SUM" '(' Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$3.(ast.ExprNode)}}
}
| "SUM" '(' DistinctOpt Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4.(ast.ExprNode)}, Distinct: $3.(bool)}
}
| "BIT_XOR" '(' Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$3.(ast.ExprNode)}}
}
| "BIT_XOR" '(' DistinctOpt Expression ')'
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4.(ast.ExprNode)}, Distinct: $3.(bool)}
}

FuncDatetimePrec:
{
Expand Down
24 changes: 24 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,30 @@ func (s *testParserSuite) TestBuiltin(c *C) {
// For misc functions
{`SELECT GET_LOCK('lock1',10);`, true},
{`SELECT RELEASE_LOCK('lock1');`, true},

// For aggregate functions
{`select avg(c1,c2) from t;`, false},
{`select avg(distinct c1) from t;`, true},
{`select avg(c2) from t;`, true},
{`select bit_xor(c1) from t;`, true},
{`select bit_xor(distinct c1) from t;`, false},
{`select max(c1,c2) from t;`, false},
{`select max(distinct c1) from t;`, true},
{`select max(c2) from t;`, true},
{`select min(c1,c2) from t;`, false},
{`select min(distinct c1) from t;`, true},
{`select min(c2) from t;`, true},
{`select sum(c1,c2) from t;`, false},
{`select sum(distinct c1) from t;`, true},
{`select sum(c2) from t;`, true},
{`select count(c1) from t;`, true},
{`select count(distinct *) from t;`, false},
{`select count(*) from t;`, true},
{`select count(distinct c1, c2) from t;`, true},
{`select count(c1, c2) from t;`, false},
{`select count(all c1) from t;`, true},
{`select group_concat(c2,c1) from t group by c1;`, true},
{`select group_concat(distinct c2,c1) from t group by c1;`, true},
}
s.RunTest(c, table)
}
Expand Down

0 comments on commit afeccd0

Please sign in to comment.