Skip to content

Commit f3f6dec

Browse files
authored
feat: Add support for 'isoyear' in date_part function (#19821)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #19820. ## Rationale for this change `isoyear` part is available in both PG and Spark EXTRACT functions. https://www.postgresql.org/docs/current/functions-datetime.html#:~:text=the%20week%20numbering.-,isoyear,-The%20ISO%208601 https://github.com/apache/spark/blob/a03bedb6c1281c5263a42bfd20608d2ee005ab05/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/datetimeExpressions.scala#L3360 ## What changes are included in this PR? Support for part `isoyear` in date_part function. ## Are these changes tested? yes in SLT ## Are there any user-facing changes? yes <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 5e893ab commit f3f6dec

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

datafusion/functions/src/datetime/date_part.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use datafusion_macros::user_doc;
6161
description = r#"Part of the date to return. The following date parts are supported:
6262
6363
- year
64+
- isoyear (ISO 8601 week-numbering year)
6465
- quarter (emits value in inclusive range [1, 4] based on which quartile of the year the date is in)
6566
- month
6667
- week (week of the year)
@@ -218,6 +219,7 @@ impl ScalarUDFImpl for DatePartFunc {
218219
} else {
219220
// special cases that can be extracted (in postgres) but are not interval units
220221
match part_trim.to_lowercase().as_str() {
222+
"isoyear" => date_part(array.as_ref(), DatePart::YearISO)?,
221223
"qtr" | "quarter" => date_part(array.as_ref(), DatePart::Quarter)?,
222224
"doy" => date_part(array.as_ref(), DatePart::DayOfYear)?,
223225
"dow" => date_part(array.as_ref(), DatePart::DayOfWeekSunday0)?,

datafusion/sqllogictest/test_files/datetime/date_part.slt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ SELECT date_part('year', ts_nano_no_tz), date_part('year', ts_nano_utc), date_pa
8181
2020 2020 2019 2020 2020 2019
8282
2020 2020 2019 2020 2020 2019
8383

84+
# date_part (isoyear) with columns and explicit timestamp
85+
query IIIIII
86+
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;
87+
----
88+
2020 2020 2020 2020 2020 2020
89+
2020 2020 2020 2020 2020 2020
90+
2020 2020 2020 2020 2020 2020
91+
2020 2020 2020 2020 2020 2020
92+
2020 2020 2020 2020 2020 2020
93+
2020 2020 2020 2020 2020 2020
94+
2020 2020 2020 2020 2020 2020
95+
2020 2020 2020 2020 2020 2020
96+
2020 2020 2020 2020 2020 2020
97+
2020 2020 2020 2020 2020 2020
98+
2020 2020 2020 2020 2020 2020
99+
100+
84101
# date_part (month)
85102
query IIIIII
86103
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;
@@ -228,6 +245,26 @@ SELECT EXTRACT('year' FROM timestamp '2020-09-08T12:00:00+00:00')
228245
----
229246
2020
230247

248+
query I
249+
SELECT date_part('ISOYEAR', CAST('2000-01-01' AS DATE))
250+
----
251+
1999
252+
253+
query I
254+
SELECT EXTRACT(isoyear FROM timestamp '2020-09-08T12:00:00+00:00')
255+
----
256+
2020
257+
258+
query I
259+
SELECT EXTRACT("isoyear" FROM timestamp '2020-09-08T12:00:00+00:00')
260+
----
261+
2020
262+
263+
query I
264+
SELECT EXTRACT('isoyear' FROM timestamp '2020-09-08T12:00:00+00:00')
265+
----
266+
2020
267+
231268
query I
232269
SELECT date_part('QUARTER', CAST('2000-01-01' AS DATE))
233270
----
@@ -865,9 +902,15 @@ SELECT extract(month from arrow_cast('20 months', 'Interval(YearMonth)'))
865902
----
866903
8
867904

905+
query error DataFusion error: Arrow error: Compute error: YearISO does not support: Interval\(YearMonth\)
906+
SELECT extract(isoyear from arrow_cast('10 years', 'Interval(YearMonth)'))
907+
868908
query error DataFusion error: Arrow error: Compute error: Year does not support: Interval\(DayTime\)
869909
SELECT extract(year from arrow_cast('10 days', 'Interval(DayTime)'))
870910

911+
query error DataFusion error: Arrow error: Compute error: YearISO does not support: Interval\(DayTime\)
912+
SELECT extract(isoyear from arrow_cast('10 days', 'Interval(DayTime)'))
913+
871914
query error DataFusion error: Arrow error: Compute error: Month does not support: Interval\(DayTime\)
872915
SELECT extract(month from arrow_cast('10 days', 'Interval(DayTime)'))
873916

@@ -1062,6 +1105,9 @@ SELECT extract(month from arrow_cast(864000, 'Duration(Second)'))
10621105
query error DataFusion error: Arrow error: Compute error: Year does not support: Duration\(s\)
10631106
SELECT extract(year from arrow_cast(864000, 'Duration(Second)'))
10641107

1108+
query error DataFusion error: Arrow error: Compute error: YearISO does not support: Duration\(s\)
1109+
SELECT extract(isoyear from arrow_cast(864000, 'Duration(Second)'))
1110+
10651111
query I
10661112
SELECT extract(day from arrow_cast(NULL, 'Duration(Second)'))
10671113
----
@@ -1074,6 +1120,11 @@ SELECT (date_part('year', now()) = EXTRACT(year FROM now()))
10741120
----
10751121
true
10761122

1123+
query B
1124+
SELECT (date_part('isoyear', now()) = EXTRACT(isoyear FROM now()))
1125+
----
1126+
true
1127+
10771128
query B
10781129
SELECT (date_part('quarter', now()) = EXTRACT(quarter FROM now()))
10791130
----

docs/source/user-guide/sql/scalar_functions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,7 @@ date_part(part, expression)
25192519
- **part**: Part of the date to return. The following date parts are supported:
25202520

25212521
- year
2522+
- isoyear (ISO 8601 week-numbering year)
25222523
- quarter (emits value in inclusive range [1, 4] based on which quartile of the year the date is in)
25232524
- month
25242525
- week (week of the year)

0 commit comments

Comments
 (0)