Skip to content

Commit f6a1995

Browse files
authored
fix date_part('isodow') (#22116)
## 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 #22115. ## Rationale for this change Bug in `date_part` - `isodow` argument logic <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? - fix the logic - fix the docs <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? - Yes, unit tests <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? - Yes, this PR fixes the logic bug. Docs are updated. <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 36aad94 commit f6a1995

4 files changed

Lines changed: 23 additions & 9 deletions

File tree

datafusion/functions/src/datetime/date_part.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ use datafusion_macros::user_doc;
8080
- dow (day of the week where Sunday is 0)
8181
- doy (day of the year)
8282
- epoch (seconds since Unix epoch for timestamps/dates, total seconds for intervals)
83-
- isodow (day of the week where Monday is 0)
83+
- isodow (ISO 8601 day of the week where Monday is 1 and Sunday is 7)
8484
"#
8585
),
8686
argument(
@@ -239,7 +239,18 @@ impl ScalarUDFImpl for DatePartFunc {
239239
"qtr" | "quarter" => date_part(array.as_ref(), DatePart::Quarter)?,
240240
"doy" => date_part(array.as_ref(), DatePart::DayOfYear)?,
241241
"dow" => date_part(array.as_ref(), DatePart::DayOfWeekSunday0)?,
242-
"isodow" => date_part(array.as_ref(), DatePart::DayOfWeekMonday0)?,
242+
"isodow" => {
243+
// Postgres `isodow` is 1..=7 with Mon=1. Arrow's
244+
// `DayOfWeekMonday0` returns 0..=6 with Mon=0; shift by
245+
// +1 to match Postgres. TODO: switch to a future
246+
// `DatePart::DayOfWeekMonday1` upstream variant once it
247+
// exists, so this kernel-then-add becomes a single call.
248+
let zero_based =
249+
date_part(array.as_ref(), DatePart::DayOfWeekMonday0)?;
250+
let int_arr = as_int32_array(&zero_based)?;
251+
let one_based: Int32Array = int_arr.unary(|v| v + 1);
252+
Arc::new(one_based) as ArrayRef
253+
}
243254
"epoch" => epoch(array.as_ref())?,
244255
_ => return exec_err!("Date part '{part}' not supported"),
245256
}

datafusion/spark/src/function/datetime/date_part.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,11 @@ impl ScalarUDFImpl for SparkDatePart {
125125
));
126126

127127
match part {
128-
// Add 1 for day-of-week parts to convert 0-indexed to 1-indexed
129-
"dow" | "isodow" => Ok(ExprSimplifyResult::Simplified(
128+
// Spark's `dayofweek` is 1..=7 (Sun=1) but df's `dow` is 0..=6
129+
// (Sun=0); shift by +1. df's `isodow` already returns the
130+
// PG-correct 1..=7 (Mon=1), which matches Spark's
131+
// `dayofweek_iso`/`dow_iso`, so no shift is needed there.
132+
"dow" => Ok(ExprSimplifyResult::Simplified(
130133
date_part_expr + Expr::Literal(ScalarValue::Int32(Some(1)), None),
131134
)),
132135
_ => Ok(ExprSimplifyResult::Simplified(date_part_expr)),

datafusion/sqllogictest/test_files/datetime/date_part.slt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,22 +1240,22 @@ true
12401240
query I
12411241
SELECT date_part('ISODOW', CAST('2000-01-01' AS DATE))
12421242
----
1243-
5
1243+
6
12441244

12451245
query I
12461246
SELECT EXTRACT(isodow FROM to_timestamp('2020-09-08T12:00:00+00:00'))
12471247
----
1248-
1
1248+
2
12491249

12501250
query I
12511251
SELECT EXTRACT("isodow" FROM to_timestamp('2020-09-08T12:00:00+00:00'))
12521252
----
1253-
1
1253+
2
12541254

12551255
query I
12561256
SELECT EXTRACT('isodow' FROM to_timestamp('2020-09-08T12:00:00+00:00'))
12571257
----
1258-
1
1258+
2
12591259

12601260
## Preimage tests
12611261

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2573,7 +2573,7 @@ date_part(part, expression)
25732573
- dow (day of the week where Sunday is 0)
25742574
- doy (day of the year)
25752575
- epoch (seconds since Unix epoch for timestamps/dates, total seconds for intervals)
2576-
- isodow (day of the week where Monday is 0)
2576+
- isodow (ISO 8601 day of the week where Monday is 1 and Sunday is 7)
25772577

25782578
- **expression**: Time expression to operate on. Can be a constant, column, or function.
25792579

0 commit comments

Comments
 (0)