Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: add order by clause in group_concat (#813) #849

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ast/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,8 @@ type AggregateFuncExpr struct {
// For example, column c1 values are "1", "2", "2", "sum(c1)" is "5",
// but "sum(distinct c1)" is "3".
Distinct bool
// Order is only used in GROUP_CONCAT
Order *OrderByClause
}

// Restore implements Node interface.
Expand All @@ -719,6 +721,12 @@ func (n *AggregateFuncExpr) Restore(ctx *format.RestoreCtx) error {
return errors.Annotatef(err, "An error occurred while restore AggregateFuncExpr.Args[%d]", i)
}
}
if n.Order != nil {
ctx.WritePlain(" ")
if err := n.Order.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occur while restore AggregateFuncExpr.Args Order")
}
}
ctx.WriteKeyWord(" SEPARATOR ")
if err := n.Args[len(n.Args)-1].Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while restore AggregateFuncExpr.Args SEPARATOR")
Expand Down Expand Up @@ -756,6 +764,13 @@ func (n *AggregateFuncExpr) Accept(v Visitor) (Node, bool) {
}
n.Args[i] = node.(ExprNode)
}
if n.Order != nil {
node, ok := n.Order.Accept(v)
if !ok {
return n, false
}
n.Order = node.(*OrderByClause)
}
return v.Leave(n)
}

Expand Down
4 changes: 4 additions & 0 deletions ast/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func (ts *testFunctionsSuite) TestAggregateFuncExprRestore(c *C) {
{"VAR_SAMP(test_score)", "VAR_SAMP(`test_score`)"},
{"VARIANCE(test_score)", "VAR_POP(`test_score`)"},
{"JSON_OBJECTAGG(test_score, results)", "JSON_OBJECTAGG(`test_score`, `results`)"},
{"GROUP_CONCAT(a)", "GROUP_CONCAT(`a` SEPARATOR ',')"},
{"GROUP_CONCAT(a separator '--')", "GROUP_CONCAT(`a` SEPARATOR '--')"},
{"GROUP_CONCAT(a order by b desc, c)", "GROUP_CONCAT(`a` ORDER BY `b` DESC,`c` SEPARATOR ',')"},
{"GROUP_CONCAT(a order by b desc, c separator '--')", "GROUP_CONCAT(`a` ORDER BY `b` DESC,`c` SEPARATOR '--')"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).Fields.Fields[0].Expr
Expand Down
6 changes: 5 additions & 1 deletion parser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -6320,7 +6320,11 @@ SumExpr:
if $8 != nil {
$$ = &ast.WindowFuncExpr{F: $1, Args: args, Distinct: $3.(bool), Spec: *($8.(*ast.WindowSpec))}
} else {
$$ = &ast.AggregateFuncExpr{F: $1, Args: args, Distinct: $3.(bool)}
agg := &ast.AggregateFuncExpr{F: $1, Args: args, Distinct: $3.(bool)}
if $5 != nil {
agg.Order = $5.(*ast.OrderByClause)
}
$$ = agg
}
}
| builtinMax '(' BuggyDefaultFalseDistinctOpt Expression ')' OptWindowingClause
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1802,7 +1802,7 @@ func (s *testParserSuite) TestBuiltin(c *C) {
{`select group_concat(c2,c1 SEPARATOR ';') from t group by c1;`, true, "SELECT GROUP_CONCAT(`c2`, `c1` SEPARATOR ';') FROM `t` GROUP BY `c1`"},
{`select group_concat(distinct c2,c1) from t group by c1;`, true, "SELECT GROUP_CONCAT(DISTINCT `c2`, `c1` SEPARATOR ',') FROM `t` GROUP BY `c1`"},
{`select group_concat(distinctrow c2,c1) from t group by c1;`, true, "SELECT GROUP_CONCAT(DISTINCT `c2`, `c1` SEPARATOR ',') FROM `t` GROUP BY `c1`"},
{`SELECT student_name, GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ') FROM student GROUP BY student_name;`, true, "SELECT `student_name`,GROUP_CONCAT(DISTINCT `test_score` SEPARATOR ' ') FROM `student` GROUP BY `student_name`"},
{`SELECT student_name, GROUP_CONCAT(DISTINCT test_score ORDER BY test_score DESC SEPARATOR ' ') FROM student GROUP BY student_name;`, true, "SELECT `student_name`,GROUP_CONCAT(DISTINCT `test_score` ORDER BY `test_score` DESC SEPARATOR ' ') FROM `student` GROUP BY `student_name`"},
{`select std(c1), std(all c1), std(distinct c1) from t`, true, "SELECT STD(`c1`),STD(`c1`),STD(DISTINCT `c1`) FROM `t`"},
{`select std(c1, c2) from t`, false, ""},
{`select stddev(c1), stddev(all c1), stddev(distinct c1) from t`, true, "SELECT STDDEV(`c1`),STDDEV(`c1`),STDDEV(DISTINCT `c1`) FROM `t`"},
Expand Down