Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion datafusion/functions/src/datetime/date_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ use datafusion_macros::user_doc;
argument(
name = "part",
description = r#"Part of the date to return. The following date parts are supported:

- year
- isoyear (ISO 8601 week-numbering year)
- quarter (emits value in inclusive range [1, 4] based on which quartile of the year the date is in)
- month
- week (week of the year)
Expand Down Expand Up @@ -215,6 +216,7 @@ impl ScalarUDFImpl for DatePartFunc {
} else {
// special cases that can be extracted (in postgres) but are not interval units
match part_trim.to_lowercase().as_str() {
"isoyear" => date_part(array.as_ref(), DatePart::YearISO)?,
"qtr" | "quarter" => date_part(array.as_ref(), DatePart::Quarter)?,
"doy" => date_part(array.as_ref(), DatePart::DayOfYear)?,
"dow" => date_part(array.as_ref(), DatePart::DayOfWeekSunday0)?,
Expand Down
51 changes: 51 additions & 0 deletions datafusion/sqllogictest/test_files/datetime/date_part.slt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ SELECT date_part('year', ts_nano_no_tz), date_part('year', ts_nano_utc), date_pa
2020 2020 2019 2020 2020 2019
2020 2020 2019 2020 2020 2019

# date_part (isoyear) with columns and explicit timestamp
query IIIIII
SELECT date_part('isoyear', ts_nano_no_tz), date_part('isoyear', ts_nano_utc), date_part('isoyear', ts_nano_eastern), date_part('isoyear', ts_milli_no_tz), date_part('isoyear', ts_milli_utc), date_part('isoyear', ts_milli_eastern) FROM source_ts;
----
2020 2020 2020 2020 2020 2020
2020 2020 2020 2020 2020 2020
2020 2020 2020 2020 2020 2020
2020 2020 2020 2020 2020 2020
2020 2020 2020 2020 2020 2020
2020 2020 2020 2020 2020 2020
2020 2020 2020 2020 2020 2020
2020 2020 2020 2020 2020 2020
2020 2020 2020 2020 2020 2020
2020 2020 2020 2020 2020 2020
2020 2020 2020 2020 2020 2020


# date_part (month)
query IIIIII
SELECT date_part('month', ts_nano_no_tz), date_part('month', ts_nano_utc), date_part('month', ts_nano_eastern), date_part('month', ts_milli_no_tz), date_part('month', ts_milli_utc), date_part('month', ts_milli_eastern) FROM source_ts;
Expand Down Expand Up @@ -228,6 +245,26 @@ SELECT EXTRACT('year' FROM timestamp '2020-09-08T12:00:00+00:00')
----
2020

query I
SELECT date_part('ISOYEAR', CAST('2000-01-01' AS DATE))
----
1999

query I
SELECT EXTRACT(isoyear FROM timestamp '2020-09-08T12:00:00+00:00')
----
2020

query I
SELECT EXTRACT("isoyear" FROM timestamp '2020-09-08T12:00:00+00:00')
----
2020

query I
SELECT EXTRACT('isoyear' FROM timestamp '2020-09-08T12:00:00+00:00')
----
2020

query I
SELECT date_part('QUARTER', CAST('2000-01-01' AS DATE))
----
Expand Down Expand Up @@ -865,9 +902,15 @@ SELECT extract(month from arrow_cast('20 months', 'Interval(YearMonth)'))
----
8

query error DataFusion error: Arrow error: Compute error: YearISO does not support: Interval\(YearMonth\)
SELECT extract(isoyear from arrow_cast('10 years', 'Interval(YearMonth)'))

query error DataFusion error: Arrow error: Compute error: Year does not support: Interval\(DayTime\)
SELECT extract(year from arrow_cast('10 days', 'Interval(DayTime)'))

query error DataFusion error: Arrow error: Compute error: YearISO does not support: Interval\(DayTime\)
SELECT extract(isoyear from arrow_cast('10 days', 'Interval(DayTime)'))

query error DataFusion error: Arrow error: Compute error: Month does not support: Interval\(DayTime\)
SELECT extract(month from arrow_cast('10 days', 'Interval(DayTime)'))

Expand Down Expand Up @@ -1011,6 +1054,9 @@ SELECT extract(month from arrow_cast(864000, 'Duration(Second)'))
query error DataFusion error: Arrow error: Compute error: Year does not support: Duration\(s\)
SELECT extract(year from arrow_cast(864000, 'Duration(Second)'))

query error DataFusion error: Arrow error: Compute error: YearISO does not support: Duration\(s\)
SELECT extract(isoyear from arrow_cast(864000, 'Duration(Second)'))

query I
SELECT extract(day from arrow_cast(NULL, 'Duration(Second)'))
----
Expand All @@ -1023,6 +1069,11 @@ SELECT (date_part('year', now()) = EXTRACT(year FROM now()))
----
true

query B
SELECT (date_part('isoyear', now()) = EXTRACT(isoyear FROM now()))
----
true

query B
SELECT (date_part('quarter', now()) = EXTRACT(quarter FROM now()))
----
Expand Down
1 change: 1 addition & 0 deletions docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2519,6 +2519,7 @@ date_part(part, expression)
- **part**: Part of the date to return. The following date parts are supported:

- year
- isoyear (ISO 8601 week-numbering year)
- quarter (emits value in inclusive range [1, 4] based on which quartile of the year the date is in)
- month
- week (week of the year)
Expand Down