Skip to content

Commit

Permalink
feat(spanner/spansql): fix (date|timestamp) comparison parse.
Browse files Browse the repository at this point in the history
This fixes case when query has `date = @date` or `timestamp =
@timestamp` case.
Since date and timestamp are not reserved keyword, they can be used as
ID
  • Loading branch information
takeshi.nakata committed Jul 21, 2021
1 parent 8ac1318 commit 3d305fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions spanner/spansql/parser.go
Expand Up @@ -2714,11 +2714,15 @@ func (p *parser) parseLit() (Expr, *parseError) {
p.back()
return p.parseArrayLit()
case tok.caseEqual("DATE"):
p.back()
return p.parseDateLit()
if !p.sniff("=") {
p.back()
return p.parseDateLit()
}
case tok.caseEqual("TIMESTAMP"):
p.back()
return p.parseTimestampLit()
if !p.sniff("=") {
p.back()
return p.parseTimestampLit()
}
}

// TODO: struct literals
Expand Down
11 changes: 11 additions & 0 deletions spanner/spansql/parser_test.go
Expand Up @@ -250,6 +250,13 @@ func TestParseQuery(t *testing.T) {
}

func TestParseExpr(t *testing.T) {
timef := func(format, s string) time.Time {
ti, err := time.ParseInLocation(format, string(s), defaultLocation)
if err != nil {
t.Errorf("parsing %s [%s] time.ParseInLocation failed.", s, format)
}
return ti
}
tests := []struct {
in string
want Expr
Expand Down Expand Up @@ -336,7 +343,11 @@ func TestParseExpr(t *testing.T) {

// Date and timestamp literals:
{`DATE '2014-09-27'`, DateLiteral(civil.Date{Year: 2014, Month: time.September, Day: 27})},
{`TIMESTAMP '2014-09-27 12:30:00'`, TimestampLiteral(timef("2006-01-02 15:04:05", "2014-09-27 12:30:00"))},

// date and timestamp
{`DATE = '2014-09-27'`, ComparisonOp{LHS: ID("DATE"), Op: Eq, RHS: StringLiteral("2014-09-27")}},
{`TIMESTAMP = '2014-09-27 12:30:00'`, ComparisonOp{LHS: ID("TIMESTAMP"), Op: Eq, RHS: StringLiteral("2014-09-27 12:30:00")}},
// Array literals:
// https://cloud.google.com/spanner/docs/lexical#array_literals
{`[1, 2, 3]`, Array{IntegerLiteral(1), IntegerLiteral(2), IntegerLiteral(3)}},
Expand Down

0 comments on commit 3d305fa

Please sign in to comment.