Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
cc81650
Add the date_part() function
MaxGekk Aug 11, 2019
0e571be
Merge remote-tracking branch 'remotes/origin/master' into date_part
MaxGekk Aug 11, 2019
b856859
Support millennium, century and decade by date_part()
MaxGekk Aug 11, 2019
d23b8ba
Uncomment the first usage of date_part()
MaxGekk Aug 11, 2019
af51e52
Reuse date_part() from extract
MaxGekk Aug 11, 2019
e68611a
Add a description for DatePart
MaxGekk Aug 12, 2019
e1d5a75
Merge remote-tracking branch 'remotes/origin/master' into date_part
MaxGekk Aug 14, 2019
ae353b9
Regen extract.sql.out
MaxGekk Aug 14, 2019
f3b2772
Regen timestamp.sql.out
MaxGekk Aug 14, 2019
b795ebc
Add synonyms for field
MaxGekk Aug 14, 2019
efc3ee0
Regen date_part.sql.out
MaxGekk Aug 14, 2019
188d7de
Merge remote-tracking branch 'remotes/origin/master' into date_part
MaxGekk Aug 14, 2019
9935c01
Add isoyear, milliseconds, microseconds and epoch
MaxGekk Aug 14, 2019
c918eb0
Re-gen extract.sql.out
MaxGekk Aug 14, 2019
bcf73d2
Re-gen date.sql.out
MaxGekk Aug 14, 2019
7c549c7
Uncomment 2 queries in timestamp.sql
MaxGekk Aug 15, 2019
1b2c8d4
List all values of field
MaxGekk Aug 15, 2019
2fa25b1
Put synonyms to ()
MaxGekk Aug 16, 2019
494679c
Remove unneeded backquotes
MaxGekk Aug 16, 2019
5c8c34d
Remove backquotes around year, month, day, hour, minute and second
MaxGekk Aug 16, 2019
05b3746
Revert "Remove backquotes around year, month, day, hour, minute and s…
MaxGekk Aug 17, 2019
74a7fec
Turn off ansi mode for a query
MaxGekk Aug 23, 2019
bc707df
Fix
maropu Aug 16, 2019
d86c092
Re-gen date_part.sql.out
MaxGekk Aug 23, 2019
ade541d
Re-gen extract.sql.out
MaxGekk Aug 23, 2019
43968c2
Change type of errorHandleFunc to Nothing
MaxGekk Sep 4, 2019
b292931
Merge remote-tracking branch 'origin/master' into date_part
MaxGekk Sep 4, 2019
600eee6
Use backquotes around year, month, day, hour, minute and second
MaxGekk Sep 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add the date_part() function
  • Loading branch information
MaxGekk committed Aug 11, 2019
commit cc81650b09de527860d361d26510f443915daaae
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ object FunctionRegistry {
expression[TimeWindow]("window"),
expression[MakeDate]("make_date"),
expression[MakeTimestamp]("make_timestamp"),
expression[DatePart]("date_part"),

// collection functions
expression[CreateArray]("array"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1828,3 +1828,35 @@ case class MakeTimestamp(

override def prettyName: String = "make_timestamp"
}

case class DatePart(field: Expression, source: Expression, child: Expression)
extends RuntimeReplaceable {

def this(field: Expression, source: Expression) {
this(field, source, {
if (!field.foldable) {
throw new AnalysisException("The field parameter needs to be a foldable string value.")
}
val extractField = field.eval().asInstanceOf[UTF8String].toString.toUpperCase(Locale.ROOT)
extractField match {
case "YEAR" => Year(source)
case "QUARTER" => Quarter(source)
case "MONTH" => Month(source)
case "WEEK" => WeekOfYear(source)
case "DAY" => DayOfMonth(source)
case "DAYOFWEEK" => DayOfWeek(source)
case "DOW" => Subtract(DayOfWeek(source), Literal(1))
case "ISODOW" => Add(WeekDay(source), Literal(1))
case "DOY" => DayOfYear(source)
case "HOUR" => Hour(source)
case "MINUTE" => Minute(source)
case "SECOND" => Second(source)
case other =>
throw new AnalysisException(s"Literals of type '$other' are currently not supported.")
}})
}

override def flatArguments: Iterator[Any] = Iterator(field, source)
override def sql: String = s"$prettyName(${field.sql}, ${source.sql})"
override def prettyName: String = "date_part"
}
29 changes: 29 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/date_part.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
CREATE TEMPORARY VIEW t AS select '2011-05-06 07:08:09.1234567' as c;

select date_part('year', c) from t;

select date_part('quarter', c) from t;

select date_part('month', c) from t;

select date_part('week', c) from t;

select date_part('day', c) from t;

select date_part('dayofweek', c) from t;

select date_part('dow', c) from t;

select date_part('isodow', c) from t;

select date_part('doy', c) from t;

select date_part('hour', c) from t;

select date_part('minute', c) from t;

select date_part('second', c) from t;

select date_part('not_supported', c) from t;

select date_part(c, c) from t;
124 changes: 124 additions & 0 deletions sql/core/src/test/resources/sql-tests/results/date_part.sql.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 15


-- !query 0
CREATE TEMPORARY VIEW t AS select '2011-05-06 07:08:09.1234567' as c
-- !query 0 schema
struct<>
-- !query 0 output



-- !query 1
select date_part('year', c) from t
-- !query 1 schema
struct<date_part('year', t.`c`):int>
-- !query 1 output
2011


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


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


-- !query 4
select date_part('week', c) from t
-- !query 4 schema
struct<date_part('week', t.`c`):int>
-- !query 4 output
18


-- !query 5
select date_part('day', c) from t
-- !query 5 schema
struct<date_part('day', t.`c`):int>
-- !query 5 output
6


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


-- !query 7
select date_part('dow', c) from t
-- !query 7 schema
struct<date_part('dow', t.`c`):int>
-- !query 7 output
5


-- !query 8
select date_part('isodow', c) from t
-- !query 8 schema
struct<date_part('isodow', t.`c`):int>
-- !query 8 output
5


-- !query 9
select date_part('doy', c) from t
-- !query 9 schema
struct<date_part('doy', t.`c`):int>
-- !query 9 output
126


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


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


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


-- !query 13
select date_part('not_supported', c) from t
-- !query 13 schema
struct<>
-- !query 13 output
org.apache.spark.sql.AnalysisException
Literals of type 'NOT_SUPPORTED' are currently not supported.;; line 1 pos 7


-- !query 14
select date_part(c, c) from t
-- !query 14 schema
struct<>
-- !query 14 output
org.apache.spark.sql.AnalysisException
The field parameter needs to be a foldable string value.;; line 1 pos 7