Skip to content

Commit

Permalink
Merge #38623
Browse files Browse the repository at this point in the history
38623: sqlfmt: retain ORDER BY in aggregates r=justinj a=justinj

Release note (bug fix): sqlfmt no longer strips ORDER BY from
aggregates.

Co-authored-by: Justin Jaffray <justin@cockroachlabs.com>
  • Loading branch information
craig[bot] and Justin Jaffray committed Jul 3, 2019
2 parents 69fc2b9 + a5f6f04 commit 393a6f1
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,8 @@ func TestParse(t *testing.T) {

{`SELECT 1 FROM t GROUP BY a`},
{`SELECT 1 FROM t GROUP BY a, b`},
{`SELECT sum(x ORDER BY y) FROM t`},
{`SELECT sum(x ORDER BY y, z) FROM t`},

{`SELECT a FROM t HAVING a = b`},

Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/sem/tree/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,10 @@ func (node *FuncExpr) Format(ctx *FmtCtx) {
ctx.WriteByte('(')
ctx.WriteString(typ)
ctx.FormatNode(&node.Exprs)
if len(node.OrderBy) > 0 {
ctx.WriteByte(' ')
ctx.FormatNode(&node.OrderBy)
}
ctx.WriteByte(')')
if ctx.HasFlags(FmtParsable) && node.typ != nil {
if node.fnProps.AmbiguousReturnType {
Expand Down
4 changes: 4 additions & 0 deletions pkg/sql/sem/tree/pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,10 @@ func (node *FuncExpr) doc(p *PrettyCfg) pretty.Doc {
args,
)
}

if len(node.OrderBy) > 0 {
args = pretty.ConcatSpace(args, node.OrderBy.doc(p))
}
d = pretty.Concat(d, p.bracket("(", args, ")"))
} else {
d = pretty.Concat(d, pretty.Text("()"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
1:
-
SELECT min(a, b),
min(a ORDER BY b),
min(a ORDER BY b, c),
min(a ORDER BY b, c, d),
min(DISTINCT a, b),
min(),
min() OVER (),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
1:
-
SELECT min(a, b),
min(a ORDER BY b),
min(a ORDER BY b, c),
min(a ORDER BY b, c, d),
min(DISTINCT a, b),
min(),
min() OVER (),
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/sem/tree/testdata/pretty/functions.ref.golden.short
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
-
SELECT
min(a, b),
min(a ORDER BY b),
min(a ORDER BY b, c),
min(a ORDER BY b, c, d),
min(DISTINCT a, b),
min(),
min() OVER (),
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/sem/tree/testdata/pretty/functions.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
select
min(a,b),
min(a order by b),
min(a order by b, c),
min(a order by b, c, d),
min(distinct a,b),
min(),
min() over (),
Expand Down

0 comments on commit 393a6f1

Please sign in to comment.