[SPARK-28623][SQL] Support dow, isodow and doy by extract()

## What changes were proposed in this pull request?

In the PR, I propose to use existing expressions `DayOfYear`, `WeekDay` and `DayOfWeek`, and support additional parameters of `extract()` for feature parity with PostgreSQL (https://www.postgresql.org/docs/11/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT):

1. `dow` - the day of the week as Sunday (0) to Saturday (6)
2. `isodow` - the day of the week as Monday (1) to Sunday (7)
3. `doy` - the day of the year (1 - 365/366)

Here are examples:
```sql
spark-sql> SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
5
spark-sql> SELECT EXTRACT(ISODOW FROM TIMESTAMP '2001-02-18 20:38:40');
7
spark-sql> SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
47
```

## How was this patch tested?

Updated `extract.sql`.

Closes #25367 from MaxGekk/extract-ext.

Authored-by: Maxim Gekk <max.gekk@gmail.com>
Signed-off-by: Dongjoon Hyun <dhyun@apple.com>
This commit is contained in:
Maxim Gekk 2019-08-06 13:39:49 -07:00 committed by Dongjoon Hyun
parent c17fa1360d
commit 9e3aab8b95
3 changed files with 48 additions and 12 deletions

View file

@ -1408,6 +1408,12 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
DayOfMonth(expression(ctx.source))
case "DAYOFWEEK" =>
DayOfWeek(expression(ctx.source))
case "DOW" =>
Subtract(DayOfWeek(expression(ctx.source)), Literal(1))
case "ISODOW" =>
Add(WeekDay(expression(ctx.source)), Literal(1))
case "DOY" =>
DayOfYear(expression(ctx.source))
case "HOUR" =>
Hour(expression(ctx.source))
case "MINUTE" =>

View file

@ -12,6 +12,12 @@ select extract(day from c) from t;
select extract(dayofweek from c) from t;
select extract(dow from c) from t;
select extract(isodow from c) from t;
select extract(doy from c) from t;
select extract(hour from c) from t;
select extract(minute from c) from t;

View file

@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 11
-- Number of queries: 14
-- !query 0
@ -59,34 +59,58 @@ struct<dayofweek(CAST(c AS DATE)):int>
-- !query 7
select extract(hour from c) from t
select extract(dow from c) from t
-- !query 7 schema
struct<hour(CAST(c AS TIMESTAMP)):int>
struct<(dayofweek(CAST(c AS DATE)) - 1):int>
-- !query 7 output
7
5
-- !query 8
select extract(minute from c) from t
select extract(isodow from c) from t
-- !query 8 schema
struct<minute(CAST(c AS TIMESTAMP)):int>
struct<(weekday(CAST(c AS DATE)) + 1):int>
-- !query 8 output
8
5
-- !query 9
select extract(second from c) from t
select extract(doy from c) from t
-- !query 9 schema
struct<second(CAST(c AS TIMESTAMP)):int>
struct<dayofyear(CAST(c AS DATE)):int>
-- !query 9 output
9
126
-- !query 10
select extract(not_supported from c) from t
select extract(hour from c) from t
-- !query 10 schema
struct<>
struct<hour(CAST(c AS TIMESTAMP)):int>
-- !query 10 output
7
-- !query 11
select extract(minute from c) from t
-- !query 11 schema
struct<minute(CAST(c AS TIMESTAMP)):int>
-- !query 11 output
8
-- !query 12
select extract(second from c) from t
-- !query 12 schema
struct<second(CAST(c AS TIMESTAMP)):int>
-- !query 12 output
9
-- !query 13
select extract(not_supported from c) from t
-- !query 13 schema
struct<>
-- !query 13 output
org.apache.spark.sql.catalyst.parser.ParseException
Literals of type 'NOT_SUPPORTED' are currently not supported.(line 1, pos 7)