cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
30.1k stars 3.81k forks source link

sql: support more time spans for EXTRACT #41548

Closed yuzefovich closed 4 years ago

yuzefovich commented 5 years ago

Currently we support these "fields": year, quarter, month, week, dayofweek, dayofyear, hour, minute, second, millisecond, microsecond, epoch.

Postgres supports more. Some of the missing ones are century, decade, isodow, isoyear, millenium. The relevant code lives in pkg/sql/sem/builtins/builtins.go.

As a bonus, Postgres returns "double precision" type for EXTRACT, but Cockroach returns "int". It could be fixed as well (cc @jordanlewis in case I'm missing why we do it that way).

rogaps commented 5 years ago

I would like to work on it.

yuzefovich commented 5 years ago

@rogaps great! I'll assign the issue to you.

rogaps commented 5 years ago

I am also going to make EXTRACT returns "float". While I am on it, may I add fractional parts to "second", "millisecond", and "epoch"?

On PostgreSQL:

rogaps=# select extract(second from '2016-02-10 19:46:33.306157519'::timestamp);
 date_part
-----------
 33.306158
(1 row)

rogaps=# select extract(millisecond from '2016-02-10 19:46:33.306157519'::timestamp);
 date_part
-----------
 33306.158
(1 row)

rogaps=# select extract(epoch from '2016-02-10 19:46:33.306157519'::timestamp);
    date_part
------------------
 1455133593.30616
(1 row)

On CockroachDB

root=# select extract(second from '2016-02-10 19:46:33.306157519'::timestamp);
 extract
---------
      33
(1 row)

root=# select extract(millisecond from '2016-02-10 19:46:33.306157519'::timestamp);
 extract
---------
   33306
(1 row)

root=# select extract(epoch from '2016-02-10 19:46:33.306157519'::timestamp);
  extract
------------
 1455133593
(1 row)

I am also going to add "julian" field for EXTRACT to support.

yuzefovich commented 5 years ago

@rogaps thanks for working on this! Now that #41784 has been merged, do you think that this issue can be closed or is there something additional work remaining?

rogaps commented 5 years ago

You are welcome. I am happy to help. I have prepared the bonus (EXTRACT returns float), but I am waiting your approval to add fractional parts to it. If you think it is unnecessary you can close this issue.

yuzefovich commented 5 years ago

I believe it would nice to fix this difference Postgres returns "double precision" type for EXTRACT, but Cockroach returns "int" since it would improve our compatibility. @jordanlewis @mjibson do you guys know why we wouldn't want that?

@rogaps please go ahead with addressing the bonus item.

maddyblue commented 5 years ago

We can change extract to return the same type as postgres, that's fine.