Skip to content

Commit

Permalink
Reuse date_part() from extract
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGekk committed Aug 11, 2019
1 parent d23b8ba commit af51e52
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1395,40 +1395,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
* Create a Extract expression.
*/
override def visitExtract(ctx: ExtractContext): Expression = withOrigin(ctx) {
ctx.field.getText.toUpperCase(Locale.ROOT) match {
case "MILLENNIUM" =>
Millennium(expression(ctx.source))
case "CENTURY" =>
Century(expression(ctx.source))
case "DECADE" =>
Decade(expression(ctx.source))
case "YEAR" =>
Year(expression(ctx.source))
case "QUARTER" =>
Quarter(expression(ctx.source))
case "MONTH" =>
Month(expression(ctx.source))
case "WEEK" =>
WeekOfYear(expression(ctx.source))
case "DAY" =>
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" =>
Minute(expression(ctx.source))
case "SECOND" =>
Second(expression(ctx.source))
case other =>
throw new ParseException(s"Literals of type '$other' are currently not supported.", ctx)
}
new DatePart(Literal(ctx.field.getText), expression(ctx.source))
}

/**
Expand Down
28 changes: 13 additions & 15 deletions sql/core/src/test/resources/sql-tests/results/extract.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,95 +13,95 @@ struct<>
-- !query 1
select extract(year from c) from t
-- !query 1 schema
struct<year(CAST(c AS DATE)):int>
struct<date_part('year', `c`):int>
-- !query 1 output
2011


-- !query 2
select extract(quarter from c) from t
-- !query 2 schema
struct<quarter(CAST(c AS DATE)):int>
struct<date_part('quarter', `c`):int>
-- !query 2 output
2


-- !query 3
select extract(month from c) from t
-- !query 3 schema
struct<month(CAST(c AS DATE)):int>
struct<date_part('month', `c`):int>
-- !query 3 output
5


-- !query 4
select extract(week from c) from t
-- !query 4 schema
struct<weekofyear(CAST(c AS DATE)):int>
struct<date_part('week', `c`):int>
-- !query 4 output
18


-- !query 5
select extract(day from c) from t
-- !query 5 schema
struct<dayofmonth(CAST(c AS DATE)):int>
struct<date_part('day', `c`):int>
-- !query 5 output
6


-- !query 6
select extract(dayofweek from c) from t
-- !query 6 schema
struct<dayofweek(CAST(c AS DATE)):int>
struct<date_part('dayofweek', `c`):int>
-- !query 6 output
6


-- !query 7
select extract(dow from c) from t
-- !query 7 schema
struct<(dayofweek(CAST(c AS DATE)) - 1):int>
struct<date_part('dow', `c`):int>
-- !query 7 output
5


-- !query 8
select extract(isodow from c) from t
-- !query 8 schema
struct<(weekday(CAST(c AS DATE)) + 1):int>
struct<date_part('isodow', `c`):int>
-- !query 8 output
5


-- !query 9
select extract(doy from c) from t
-- !query 9 schema
struct<dayofyear(CAST(c AS DATE)):int>
struct<date_part('doy', `c`):int>
-- !query 9 output
126


-- !query 10
select extract(hour from c) from t
-- !query 10 schema
struct<hour(CAST(c AS TIMESTAMP)):int>
struct<date_part('hour', `c`):int>
-- !query 10 output
7


-- !query 11
select extract(minute from c) from t
-- !query 11 schema
struct<minute(CAST(c AS TIMESTAMP)):int>
struct<date_part('minute', `c`):int>
-- !query 11 output
8


-- !query 12
select extract(second from c) from t
-- !query 12 schema
struct<second(CAST(c AS TIMESTAMP)):int>
struct<date_part('second', `c`):int>
-- !query 12 output
9

Expand All @@ -113,8 +113,6 @@ struct<>
-- !query 13 output
org.apache.spark.sql.catalyst.parser.ParseException

Literals of type 'NOT_SUPPORTED' are currently not supported.(line 1, pos 7)

Literals of type 'NOT_SUPPORTED' are currently not supported.
== SQL ==
select extract(not_supported from c) from t
-------^^^
48 changes: 24 additions & 24 deletions sql/core/src/test/resources/sql-tests/results/pgSQL/date.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -504,71 +504,71 @@ struct<Days From 2K:int>
-- !query 47
SELECT EXTRACT(CENTURY FROM TO_DATE('0101-12-31 BC', 'yyyy-MM-dd G'))
-- !query 47 schema
struct<century(to_date('0101-12-31 BC', 'yyyy-MM-dd G')):int>
struct<date_part('CENTURY', TO_DATE('0101-12-31 BC', 'yyyy-MM-dd G')):int>
-- !query 47 output
-2


-- !query 48
SELECT EXTRACT(CENTURY FROM TO_DATE('0100-12-31 BC', 'yyyy-MM-dd G'))
-- !query 48 schema
struct<century(to_date('0100-12-31 BC', 'yyyy-MM-dd G')):int>
struct<date_part('CENTURY', TO_DATE('0100-12-31 BC', 'yyyy-MM-dd G')):int>
-- !query 48 output
-1


-- !query 49
SELECT EXTRACT(CENTURY FROM TO_DATE('0001-12-31 BC', 'yyyy-MM-dd G'))
-- !query 49 schema
struct<century(to_date('0001-12-31 BC', 'yyyy-MM-dd G')):int>
struct<date_part('CENTURY', TO_DATE('0001-12-31 BC', 'yyyy-MM-dd G')):int>
-- !query 49 output
-1


-- !query 50
SELECT EXTRACT(CENTURY FROM DATE '0001-01-01')
-- !query 50 schema
struct<century(DATE '0001-01-01'):int>
struct<date_part('CENTURY', DATE '0001-01-01'):int>
-- !query 50 output
1


-- !query 51
SELECT EXTRACT(CENTURY FROM DATE '0001-01-01 AD')
-- !query 51 schema
struct<century(DATE '0001-01-01'):int>
struct<date_part('CENTURY', DATE '0001-01-01'):int>
-- !query 51 output
1


-- !query 52
SELECT EXTRACT(CENTURY FROM DATE '1900-12-31')
-- !query 52 schema
struct<century(DATE '1900-12-31'):int>
struct<date_part('CENTURY', DATE '1900-12-31'):int>
-- !query 52 output
19


-- !query 53
SELECT EXTRACT(CENTURY FROM DATE '1901-01-01')
-- !query 53 schema
struct<century(DATE '1901-01-01'):int>
struct<date_part('CENTURY', DATE '1901-01-01'):int>
-- !query 53 output
20


-- !query 54
SELECT EXTRACT(CENTURY FROM DATE '2000-12-31')
-- !query 54 schema
struct<century(DATE '2000-12-31'):int>
struct<date_part('CENTURY', DATE '2000-12-31'):int>
-- !query 54 output
20


-- !query 55
SELECT EXTRACT(CENTURY FROM DATE '2001-01-01')
-- !query 55 schema
struct<century(DATE '2001-01-01'):int>
struct<date_part('CENTURY', DATE '2001-01-01'):int>
-- !query 55 output
21

Expand All @@ -584,111 +584,111 @@ true
-- !query 57
SELECT EXTRACT(MILLENNIUM FROM TO_DATE('0001-12-31 BC', 'yyyy-MM-dd G'))
-- !query 57 schema
struct<millennium(to_date('0001-12-31 BC', 'yyyy-MM-dd G')):int>
struct<date_part('MILLENNIUM', TO_DATE('0001-12-31 BC', 'yyyy-MM-dd G')):int>
-- !query 57 output
-1


-- !query 58
SELECT EXTRACT(MILLENNIUM FROM DATE '0001-01-01 AD')
-- !query 58 schema
struct<millennium(DATE '0001-01-01'):int>
struct<date_part('MILLENNIUM', DATE '0001-01-01'):int>
-- !query 58 output
1


-- !query 59
SELECT EXTRACT(MILLENNIUM FROM DATE '1000-12-31')
-- !query 59 schema
struct<millennium(DATE '1000-12-31'):int>
struct<date_part('MILLENNIUM', DATE '1000-12-31'):int>
-- !query 59 output
1


-- !query 60
SELECT EXTRACT(MILLENNIUM FROM DATE '1001-01-01')
-- !query 60 schema
struct<millennium(DATE '1001-01-01'):int>
struct<date_part('MILLENNIUM', DATE '1001-01-01'):int>
-- !query 60 output
2


-- !query 61
SELECT EXTRACT(MILLENNIUM FROM DATE '2000-12-31')
-- !query 61 schema
struct<millennium(DATE '2000-12-31'):int>
struct<date_part('MILLENNIUM', DATE '2000-12-31'):int>
-- !query 61 output
2


-- !query 62
SELECT EXTRACT(MILLENNIUM FROM DATE '2001-01-01')
-- !query 62 schema
struct<millennium(DATE '2001-01-01'):int>
struct<date_part('MILLENNIUM', DATE '2001-01-01'):int>
-- !query 62 output
3


-- !query 63
SELECT EXTRACT(MILLENNIUM FROM CURRENT_DATE)
-- !query 63 schema
struct<millennium(current_date()):int>
struct<date_part('MILLENNIUM', current_date()):int>
-- !query 63 output
3


-- !query 64
SELECT EXTRACT(DECADE FROM DATE '1994-12-25')
-- !query 64 schema
struct<decade(DATE '1994-12-25'):int>
struct<date_part('DECADE', DATE '1994-12-25'):int>
-- !query 64 output
199


-- !query 65
SELECT EXTRACT(DECADE FROM DATE '0010-01-01')
-- !query 65 schema
struct<decade(DATE '0010-01-01'):int>
struct<date_part('DECADE', DATE '0010-01-01'):int>
-- !query 65 output
1


-- !query 66
SELECT EXTRACT(DECADE FROM DATE '0009-12-31')
-- !query 66 schema
struct<decade(DATE '0009-12-31'):int>
struct<date_part('DECADE', DATE '0009-12-31'):int>
-- !query 66 output
0


-- !query 67
SELECT EXTRACT(DECADE FROM TO_DATE('0001-01-01 BC', 'yyyy-MM-dd G'))
-- !query 67 schema
struct<decade(to_date('0001-01-01 BC', 'yyyy-MM-dd G')):int>
struct<date_part('DECADE', TO_DATE('0001-01-01 BC', 'yyyy-MM-dd G')):int>
-- !query 67 output
0


-- !query 68
SELECT EXTRACT(DECADE FROM TO_DATE('0002-12-31 BC', 'yyyy-MM-dd G'))
-- !query 68 schema
struct<decade(to_date('0002-12-31 BC', 'yyyy-MM-dd G')):int>
struct<date_part('DECADE', TO_DATE('0002-12-31 BC', 'yyyy-MM-dd G')):int>
-- !query 68 output
-1


-- !query 69
SELECT EXTRACT(DECADE FROM TO_DATE('0011-01-01 BC', 'yyyy-MM-dd G'))
-- !query 69 schema
struct<decade(to_date('0011-01-01 BC', 'yyyy-MM-dd G')):int>
struct<date_part('DECADE', TO_DATE('0011-01-01 BC', 'yyyy-MM-dd G')):int>
-- !query 69 output
-1


-- !query 70
SELECT EXTRACT(DECADE FROM TO_DATE('0012-12-31 BC', 'yyyy-MM-dd G'))
-- !query 70 schema
struct<decade(to_date('0012-12-31 BC', 'yyyy-MM-dd G')):int>
struct<date_part('DECADE', TO_DATE('0012-12-31 BC', 'yyyy-MM-dd G')):int>
-- !query 70 output
-2

Expand All @@ -704,7 +704,7 @@ true
-- !query 72
SELECT EXTRACT(CENTURY FROM TIMESTAMP '1970-03-20 04:30:00.00000')
-- !query 72 schema
struct<century(CAST(TIMESTAMP('1970-03-20 04:30:00') AS DATE)):int>
struct<date_part('CENTURY', TIMESTAMP('1970-03-20 04:30:00')):int>
-- !query 72 output
20

Expand Down

0 comments on commit af51e52

Please sign in to comment.