Skip to content

Commit

Permalink
parser: Unify time unit string literal (pingcap#2660)
Browse files Browse the repository at this point in the history
Use upper case for time unit string literal in parser.
  • Loading branch information
shenli authored Feb 17, 2017
1 parent 7f8f16f commit b001b8c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
60 changes: 60 additions & 0 deletions parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -3430,25 +3430,85 @@ FuncDatetimePrec:

TimeUnit:
"MICROSECOND"
{
$$ = strings.ToUpper($1)
}
| "SECOND"
{
$$ = strings.ToUpper($1)
}
| "MINUTE"
{
$$ = strings.ToUpper($1)
}
| "HOUR"
{
$$ = strings.ToUpper($1)
}
| "DAY"
{
$$ = strings.ToUpper($1)
}
| "WEEK"
{
$$ = strings.ToUpper($1)
}
| "MONTH"
{
$$ = strings.ToUpper($1)
}
| "QUARTER"
{
$$ = strings.ToUpper($1)
}
| "YEAR"
{
$$ = strings.ToUpper($1)
}
| "SECOND_MICROSECOND"
{
$$ = strings.ToUpper($1)
}
| "MINUTE_MICROSECOND"
{
$$ = strings.ToUpper($1)
}
| "MINUTE_SECOND"
{
$$ = strings.ToUpper($1)
}
| "HOUR_MICROSECOND"
{
$$ = strings.ToUpper($1)
}
| "HOUR_SECOND"
{
$$ = strings.ToUpper($1)
}
| "HOUR_MINUTE"
{
$$ = strings.ToUpper($1)
}
| "DAY_MICROSECOND"
{
$$ = strings.ToUpper($1)
}
| "DAY_SECOND"
{
$$ = strings.ToUpper($1)
}
| "DAY_MINUTE"
{
$$ = strings.ToUpper($1)
}
| "DAY_HOUR"
{
$$ = strings.ToUpper($1)
}
| "YEAR_MONTH"
{
$$ = strings.ToUpper($1)
}

ExpressionOpt:
{
Expand Down
20 changes: 20 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1313,3 +1313,23 @@ func (s *testParserSuite) TestExplain(c *C) {
}
s.RunTest(c, table)
}

func (s *testParserSuite) TestTimestampDiffUnit(c *C) {
// Test case for timestampdiff unit.
// TimeUnit should be unified to upper case.
parser := New()
stmt, err := parser.Parse("SELECT TIMESTAMPDIFF(MONTH,'2003-02-01','2003-05-01'), TIMESTAMPDIFF(month,'2003-02-01','2003-05-01');", "", "")
c.Assert(err, IsNil)
ss := stmt[0].(*ast.SelectStmt)
fields := ss.Fields.Fields
c.Assert(len(fields), Equals, 2)
expr := fields[0].Expr
f, ok := expr.(*ast.FuncCallExpr)
c.Assert(ok, IsTrue)
c.Assert(f.Args[0].GetDatum().GetString(), Equals, "MONTH")

expr = fields[1].Expr
f, ok = expr.(*ast.FuncCallExpr)
c.Assert(ok, IsTrue)
c.Assert(f.Args[0].GetDatum().GetString(), Equals, "MONTH")
}

0 comments on commit b001b8c

Please sign in to comment.