This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-22116-36aad9430f7b7c26a8d78be578fd34aacfe4b1ce in repository https://gitbox.apache.org/repos/asf/datafusion.git
commit f6a19953ba61e7e27c5781e453f54d3fd47b9b27 Author: Konstantin Tarasov <[email protected]> AuthorDate: Tue May 12 16:12:12 2026 -0400 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. --> --- datafusion/functions/src/datetime/date_part.rs | 15 +++++++++++++-- datafusion/spark/src/function/datetime/date_part.rs | 7 +++++-- datafusion/sqllogictest/test_files/datetime/date_part.slt | 8 ++++---- docs/source/user-guide/sql/scalar_functions.md | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/datafusion/functions/src/datetime/date_part.rs b/datafusion/functions/src/datetime/date_part.rs index 10275f5aae..3c405d388b 100644 --- a/datafusion/functions/src/datetime/date_part.rs +++ b/datafusion/functions/src/datetime/date_part.rs @@ -80,7 +80,7 @@ use datafusion_macros::user_doc; - dow (day of the week where Sunday is 0) - doy (day of the year) - epoch (seconds since Unix epoch for timestamps/dates, total seconds for intervals) - - isodow (day of the week where Monday is 0) + - isodow (ISO 8601 day of the week where Monday is 1 and Sunday is 7) "# ), argument( @@ -239,7 +239,18 @@ impl ScalarUDFImpl for DatePartFunc { "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)?, - "isodow" => date_part(array.as_ref(), DatePart::DayOfWeekMonday0)?, + "isodow" => { + // Postgres `isodow` is 1..=7 with Mon=1. Arrow's + // `DayOfWeekMonday0` returns 0..=6 with Mon=0; shift by + // +1 to match Postgres. TODO: switch to a future + // `DatePart::DayOfWeekMonday1` upstream variant once it + // exists, so this kernel-then-add becomes a single call. + let zero_based = + date_part(array.as_ref(), DatePart::DayOfWeekMonday0)?; + let int_arr = as_int32_array(&zero_based)?; + let one_based: Int32Array = int_arr.unary(|v| v + 1); + Arc::new(one_based) as ArrayRef + } "epoch" => epoch(array.as_ref())?, _ => return exec_err!("Date part '{part}' not supported"), } diff --git a/datafusion/spark/src/function/datetime/date_part.rs b/datafusion/spark/src/function/datetime/date_part.rs index ced4865d16..91bdb9a553 100644 --- a/datafusion/spark/src/function/datetime/date_part.rs +++ b/datafusion/spark/src/function/datetime/date_part.rs @@ -125,8 +125,11 @@ impl ScalarUDFImpl for SparkDatePart { )); match part { - // Add 1 for day-of-week parts to convert 0-indexed to 1-indexed - "dow" | "isodow" => Ok(ExprSimplifyResult::Simplified( + // Spark's `dayofweek` is 1..=7 (Sun=1) but df's `dow` is 0..=6 + // (Sun=0); shift by +1. df's `isodow` already returns the + // PG-correct 1..=7 (Mon=1), which matches Spark's + // `dayofweek_iso`/`dow_iso`, so no shift is needed there. + "dow" => Ok(ExprSimplifyResult::Simplified( date_part_expr + Expr::Literal(ScalarValue::Int32(Some(1)), None), )), _ => Ok(ExprSimplifyResult::Simplified(date_part_expr)), diff --git a/datafusion/sqllogictest/test_files/datetime/date_part.slt b/datafusion/sqllogictest/test_files/datetime/date_part.slt index 07dc1302b9..891319f9e2 100644 --- a/datafusion/sqllogictest/test_files/datetime/date_part.slt +++ b/datafusion/sqllogictest/test_files/datetime/date_part.slt @@ -1240,22 +1240,22 @@ true query I SELECT date_part('ISODOW', CAST('2000-01-01' AS DATE)) ---- -5 +6 query I SELECT EXTRACT(isodow FROM to_timestamp('2020-09-08T12:00:00+00:00')) ---- -1 +2 query I SELECT EXTRACT("isodow" FROM to_timestamp('2020-09-08T12:00:00+00:00')) ---- -1 +2 query I SELECT EXTRACT('isodow' FROM to_timestamp('2020-09-08T12:00:00+00:00')) ---- -1 +2 ## Preimage tests diff --git a/docs/source/user-guide/sql/scalar_functions.md b/docs/source/user-guide/sql/scalar_functions.md index 260f69f737..8023e2103e 100644 --- a/docs/source/user-guide/sql/scalar_functions.md +++ b/docs/source/user-guide/sql/scalar_functions.md @@ -2573,7 +2573,7 @@ date_part(part, expression) - dow (day of the week where Sunday is 0) - doy (day of the year) - epoch (seconds since Unix epoch for timestamps/dates, total seconds for intervals) - - isodow (day of the week where Monday is 0) + - isodow (ISO 8601 day of the week where Monday is 1 and Sunday is 7) - **expression**: Time expression to operate on. Can be a constant, column, or function. --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
