Skip to content

Commit

Permalink
[parser] ast: Fix FrameBound.Accept (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
spongedu authored and ti-chi-bot committed Oct 9, 2021
1 parent b850cf8 commit 326ba01
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion parser/ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ func (n *FrameBound) Accept(v Visitor) (Node, bool) {
n.Expr = node.(ExprNode)
}
if n.Unit != nil {
node, ok := n.Expr.Accept(v)
node, ok := n.Unit.Accept(v)
if !ok {
return n, false
}
Expand Down
54 changes: 54 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2573,6 +2573,60 @@ func (s *testParserSuite) TestWindowFunctions(c *C) {
s.RunTest(c, table)
}

type windowFrameBoundChecker struct {
fb *ast.FrameBound
exprRc int
timeUnitRc int
}

// Enter implements ast.Visitor interface.
func (wfc *windowFrameBoundChecker) Enter(inNode ast.Node) (outNode ast.Node, skipChildren bool) {
if _, ok := inNode.(*ast.FrameBound); ok {
wfc.fb = inNode.(*ast.FrameBound)
}
return inNode, false
}

// Leave implements ast.Visitor interface.
func (wfc *windowFrameBoundChecker) Leave(inNode ast.Node) (node ast.Node, ok bool) {
if _, ok := inNode.(*ast.FrameBound); ok {
wfc.fb = nil
}
if wfc.fb != nil {
if inNode == wfc.fb.Expr {
wfc.exprRc += 1
} else if inNode == wfc.fb.Unit {
wfc.timeUnitRc += 1
}
}
return inNode, true
}

// For issue #51
// See https://github.com/pingcap/parser/pull/51 for details
func (s *testParserSuite) TestVisitFrameBound(c *C) {
parser := New()
parser.EnableWindowFunc()
table := []struct {
s string
exprRc int
timeUnitRc int
}{
{`SELECT AVG(val) OVER (RANGE INTERVAL '2:30' MINUTE_SECOND PRECEDING) FROM t;`, 1, 1},
{`SELECT AVG(val) OVER (RANGE 5 PRECEDING) FROM t;`, 1, 0},
{`SELECT AVG(val) OVER () FROM t;`, 0, 0},
}
for _, t := range table {
stmt, err := parser.ParseOneStmt(t.s, "", "")
c.Assert(err, IsNil)
checker := windowFrameBoundChecker{}
stmt.Accept(&checker)
c.Assert(checker.exprRc, Equals, t.exprRc)
c.Assert(checker.timeUnitRc, Equals, t.timeUnitRc)
}

}

func (s *testParserSuite) TestFieldText(c *C) {
parser := New()
stmts, err := parser.Parse("select a from t", "", "")
Expand Down

0 comments on commit 326ba01

Please sign in to comment.