Skip to content

Commit

Permalink
ast: fix the behavior of window function (pingcap#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros authored Mar 11, 2019
1 parent 4740ee2 commit 32e93bd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
7 changes: 7 additions & 0 deletions ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2009,12 +2009,19 @@ type WindowSpec struct {
PartitionBy *PartitionByClause
OrderBy *OrderByClause
Frame *FrameClause

// OnlyAlias will set to true of the first following case.
// To make compatiable with MySQL, we need to distinguish `select func over w` from `select func over (w)`.
OnlyAlias bool
}

// Restore implements Node interface.
func (n *WindowSpec) Restore(ctx *RestoreCtx) error {
if name := n.Name.String(); name != "" {
ctx.WriteName(name)
if n.OnlyAlias {
return nil
}
ctx.WriteKeyWord(" AS ")
}
ctx.WritePlain("(")
Expand Down
3 changes: 2 additions & 1 deletion ast/dml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,9 @@ func (ts *testDMLSuite) TestWindowSpecRestore(c *C) {
RunNodeRestoreTest(c, testCases, "select rank() over w from t window %s", extractNodeFunc)

testCases = []NodeRestoreTestCase{
{"w", "(`w`)"},
{"w", "`w`"},
{"()", "()"},
{"(w)", "(`w`)"},
{"(w PARTITION BY country)", "(`w` PARTITION BY `country`)"},
{"(PARTITION BY a ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)", "(PARTITION BY `a` ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)"},
}
Expand Down
10 changes: 5 additions & 5 deletions ast/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ func (ts *testFunctionsSuite) TestAggregateFuncExprRestore(c *C) {

func (ts *testDMLSuite) TestWindowFuncExprRestore(c *C) {
testCases := []NodeRestoreTestCase{
{"RANK() OVER w", "RANK() OVER (`w`)"},
{"RANK() OVER w", "RANK() OVER `w`"},
{"RANK() OVER (PARTITION BY a)", "RANK() OVER (PARTITION BY `a`)"},
{"MAX(DISTINCT a) OVER (PARTITION BY a)", "MAX(DISTINCT `a`) OVER (PARTITION BY `a`)"},
{"MAX(DISTINCTROW a) OVER (PARTITION BY a)", "MAX(DISTINCT `a`) OVER (PARTITION BY `a`)"},
{"MAX(DISTINCT ALL a) OVER (PARTITION BY a)", "MAX(DISTINCT `a`) OVER (PARTITION BY `a`)"},
{"MAX(ALL a) OVER (PARTITION BY a)", "MAX(`a`) OVER (PARTITION BY `a`)"},
{"FIRST_VALUE(val) IGNORE NULLS OVER w", "FIRST_VALUE(`val`) IGNORE NULLS OVER (`w`)"},
{"FIRST_VALUE(val) RESPECT NULLS OVER w", "FIRST_VALUE(`val`) OVER (`w`)"},
{"NTH_VALUE(val, 233) FROM LAST IGNORE NULLS OVER w", "NTH_VALUE(`val`, 233) FROM LAST IGNORE NULLS OVER (`w`)"},
{"NTH_VALUE(val, 233) FROM FIRST IGNORE NULLS OVER w", "NTH_VALUE(`val`, 233) IGNORE NULLS OVER (`w`)"},
{"FIRST_VALUE(val) IGNORE NULLS OVER (w)", "FIRST_VALUE(`val`) IGNORE NULLS OVER (`w`)"},
{"FIRST_VALUE(val) RESPECT NULLS OVER w", "FIRST_VALUE(`val`) OVER `w`"},
{"NTH_VALUE(val, 233) FROM LAST IGNORE NULLS OVER w", "NTH_VALUE(`val`, 233) FROM LAST IGNORE NULLS OVER `w`"},
{"NTH_VALUE(val, 233) FROM FIRST IGNORE NULLS OVER (w)", "NTH_VALUE(`val`, 233) IGNORE NULLS OVER (`w`)"},
}
extractNodeFunc := func(node Node) Node {
return node.(*SelectStmt).Fields.Fields[0].Expr
Expand Down
2 changes: 1 addition & 1 deletion parser.go

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

2 changes: 1 addition & 1 deletion parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -4836,7 +4836,7 @@ WindowingClause:
WindowNameOrSpec:
WindowName
{
$$ = ast.WindowSpec{Ref: $1.(model.CIStr)}
$$ = ast.WindowSpec{Name: $1.(model.CIStr), OnlyAlias: true,}
}
| WindowSpec
{
Expand Down

0 comments on commit 32e93bd

Please sign in to comment.