Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spanner/spansql): fix (date|timestamp) comparison parse. #4471

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions spanner/spansql/sql.go
Expand Up @@ -25,6 +25,7 @@ package spansql

import (
"fmt"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -373,6 +374,7 @@ func (sft SelectFromTable) SQL() string {
kvs[i] = fmt.Sprintf("%s=%s", k, v)
i++
}
sort.Strings(kvs)
str += strings.Join(kvs, ",")
str += "}"
}
Expand Down