Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
27393: sql: support PostgreSQL's abbreviated time units r=knz a=ivan

I spotted one of these in the wild in [postgrex's test suite](https://github.com/elixir-ecto/postgrex/blob/51bfc230f2e8e85e539121102cb262fdc39a52b5/test/query_test.exs#L94), and I added the rest because they are probably in use elsewhere.

Co-authored-by: Ivan Kozik <[email protected]>
  • Loading branch information
craig[bot] and ivan committed Jul 11, 2018
2 parents 246e394 + eb42b56 commit 8665be7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
19 changes: 11 additions & 8 deletions pkg/sql/sem/tree/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,18 +400,21 @@ var unitMap = func(
"month": {Months: 1},
"year": {Months: 12},
}, map[string][]string{
"nanosecond": {"ns"},
"nanosecond": {"ns", "nsec", "nsecs", "nsecond", "nseconds"},
// Include PostgreSQL's unit keywords for compatibility; see
// https://github.com/postgres/postgres/blob/a01d0fa1d889cc2003e1941e8b98707c4d701ba9/src/backend/utils/adt/datetime.c#L175-L240
//
// µ = U+00B5 = micro symbol
// μ = U+03BC = Greek letter mu
"microsecond": {"us", "µs", "μs"},
"millisecond": {"ms"},
"second": {"s"},
"minute": {"m"},
"hour": {"h"},
"microsecond": {"us", "µs", "μs", "usec", "usecs", "usecond", "useconds"},
"millisecond": {"ms", "msec", "msecs", "msecond", "mseconds"},
"second": {"s", "sec", "secs"},
"minute": {"m", "min", "mins"},
"hour": {"h", "hr", "hrs"},
"day": {"d"},
"week": {"w"},
"month": {"mon", "mons" /* pg compat */},
"year": {"y"},
"month": {"mon", "mons"},
"year": {"y", "yr", "yrs"},
})

// parseDuration parses a duration in the "traditional" Postgres
Expand Down
20 changes: 20 additions & 0 deletions pkg/sql/sem/tree/interval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ func TestPGIntervalSyntax(t *testing.T) {
{`1.2 ns`, `1ns`, ``},
{` 1.2 ns `, `1ns`, ``},
{`-1.2ns`, `-1ns`, ``},
{`-1.2nsec`, `-1ns`, ``},
{`-1.2nsecs`, `-1ns`, ``},
{`-1.2nsecond`, `-1ns`, ``},
{`-1.2nseconds`, `-1ns`, ``},
{`-9223372036854775808ns`, `-2562047h-47m-16s-854ms-775µs-808ns`, ``},
{`9223372036854775807ns`, `2562047h47m16s854ms775µs807ns`, ``},

Expand All @@ -196,6 +200,10 @@ func TestPGIntervalSyntax(t *testing.T) {
// μ = U+03BC = Greek letter mu
{`1.2µs`, `1µs200ns`, ``},
{`1.2μs`, `1µs200ns`, ``},
{`1.2usec`, `1µs200ns`, ``},
{`1.2usecs`, `1µs200ns`, ``},
{`1.2usecond`, `1µs200ns`, ``},
{`1.2useconds`, `1µs200ns`, ``},
{`0.23us`, `230ns`, ``},
{`-0.23us`, `-230ns`, ``},
{`0.2346us`, `234ns`, ``},
Expand All @@ -207,13 +215,19 @@ func TestPGIntervalSyntax(t *testing.T) {
{`1.2millisecond`, `1ms200µs`, ``},
{`1.2milliseconds`, `1ms200µs`, ``},
{`1.2ms`, `1ms200µs`, ``},
{`1.2msec`, `1ms200µs`, ``},
{`1.2msecs`, `1ms200µs`, ``},
{`1.2msecond`, `1ms200µs`, ``},
{`1.2mseconds`, `1ms200µs`, ``},
{`0.2304506ms`, `230µs450ns`, ``},
{`0.0002304506ms`, `230ns`, ``},
{`1 ms 1us 1ns`, `1ms1µs1ns`, ``},

{`1.2second`, `1s200ms`, ``},
{`1.2seconds`, `1s200ms`, ``},
{`1.2s`, `1s200ms`, ``},
{`1.2sec`, `1s200ms`, ``},
{`1.2secs`, `1s200ms`, ``},
{`0.2304506708s`, `230ms450µs670ns`, ``},
{`0.0002304506708s`, `230µs450ns`, ``},
{`0.0000002304506s`, `230ns`, ``},
Expand All @@ -225,6 +239,8 @@ func TestPGIntervalSyntax(t *testing.T) {
{`1.2minute`, `1m12s`, ``},
{`1.2minutes`, `1m12s`, ``},
{`1.2m`, `1m12s`, ``},
{`1.2min`, `1m12s`, ``},
{`1.2mins`, `1m12s`, ``},
{`1.2m 8s 20ns`, `1m20s20ns`, ``},
{`0.5m`, `30s`, ``},
{`120.5m`, `2h30s`, ``},
Expand All @@ -234,6 +250,8 @@ func TestPGIntervalSyntax(t *testing.T) {
{`1.2hour`, `1h12m`, ``},
{`1.2hours`, `1h12m`, ``},
{`1.2h`, `1h12m`, ``},
{`1.2hr`, `1h12m`, ``},
{`1.2hrs`, `1h12m`, ``},
{`1.2h 8m 20ns`, `1h20m20ns`, ``},
{`0.5h`, `30m`, ``},
{`25.5h`, `25h30m`, ``},
Expand Down Expand Up @@ -273,6 +291,8 @@ func TestPGIntervalSyntax(t *testing.T) {
{`1year`, `1y`, ``},
{`1years`, `1y`, ``},
{`1y`, `1y`, ``},
{`1yr`, `1y`, ``},
{`1yrs`, `1y`, ``},
{`1.5y`, `1y6mon`, ``},
{`1.1y`, `1y1mon6d`, ``},
{`1.11y`, `1y1mon9d14h24m`, ``},
Expand Down

0 comments on commit 8665be7

Please sign in to comment.