This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new e28fd0d0f1 Add `DatePart` enum 1-indexed variants (#9965)
e28fd0d0f1 is described below
commit e28fd0d0f1f45e5608928eb52af3b12ffe9fee2e
Author: Konstantin Tarasov <[email protected]>
AuthorDate: Mon May 25 07:19:27 2026 -0400
Add `DatePart` enum 1-indexed variants (#9965)
# 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.
-->
- Closes #9964.
# Rationale for this change
Remove extra computations done on the datafusion side for temporal
functions.
<!--
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?
- Added two new `DatePart` variant
- Closed some tests lists drift
<!--
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?
- 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)?
If this PR claims a performance improvement, please include evidence
such as benchmark results.
-->
# Are there any user-facing changes?
No
<!--
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 call them out.
-->
---------
Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
---
arrow-arith/src/temporal.rs | 88 ++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 80 insertions(+), 8 deletions(-)
diff --git a/arrow-arith/src/temporal.rs b/arrow-arith/src/temporal.rs
index 96a3b43a38..301ad172da 100644
--- a/arrow-arith/src/temporal.rs
+++ b/arrow-arith/src/temporal.rs
@@ -63,6 +63,10 @@ pub enum DatePart {
DayOfWeekSunday0,
/// Day of the week, in range `0..=6`, where Monday is `0`
DayOfWeekMonday0,
+ /// Day of the week, in range `1..=7`, where Sunday is `1`
+ DayOfWeekSunday1,
+ /// ISO day of the week, in range `1..=7`, where Monday is `1`
+ DayOfWeekMonday1,
/// Day of year, in range `1..=366`
DayOfYear,
/// Hour of the day, in range `0..=23`
@@ -100,11 +104,6 @@ impl std::fmt::Display for DatePart {
/// - `century`, `decade`, `millennium` — no matching [`DatePart`] variant.
/// - `timezone`, `timezone_hour`, `timezone_minute` — not modeled by
/// [`DatePart`].
-/// - `isodow` — PostgreSQL's `isodow` returns `1..=7` (Mon=1), but the
-/// closest variant ([`DatePart::DayOfWeekMonday0`]) returns `0..=6`.
-/// Accepting it here would silently shift the value range; callers that
-/// need the PostgreSQL semantic should map the alias themselves and
-/// add `1` to the extracted value.
impl FromStr for DatePart {
type Err = ArrowError;
@@ -118,6 +117,9 @@ impl FromStr for DatePart {
"isoweek" => Self::WeekISO,
"d" | "day" | "days" => Self::Day,
"dow" | "dayofweek" => Self::DayOfWeekSunday0,
+ "dow1" | "dayofweek1" => Self::DayOfWeekSunday1,
+ "isodow0" => Self::DayOfWeekMonday0,
+ "isodow" => Self::DayOfWeekMonday1,
"doy" | "dayofyear" => Self::DayOfYear,
"h" | "hr" | "hrs" | "hour" | "hours" => Self::Hour,
"m" | "min" | "mins" | "minute" | "minutes" => Self::Minute,
@@ -158,6 +160,8 @@ where
DatePart::Day => |d| d.day() as i32,
DatePart::DayOfWeekSunday0 => |d| d.num_days_from_sunday(),
DatePart::DayOfWeekMonday0 => |d| d.num_days_from_monday(),
+ DatePart::DayOfWeekSunday1 => |d| d.num_days_from_sunday() + 1,
+ DatePart::DayOfWeekMonday1 => |d| d.num_days_from_monday() + 1,
DatePart::DayOfYear => |d| d.ordinal() as i32,
DatePart::Hour => |d| d.hour() as i32,
DatePart::Minute => |d| d.minute() as i32,
@@ -500,6 +504,8 @@ impl ExtractDatePartExt for
PrimitiveArray<IntervalYearMonthType> {
| DatePart::Day
| DatePart::DayOfWeekSunday0
| DatePart::DayOfWeekMonday0
+ | DatePart::DayOfWeekSunday1
+ | DatePart::DayOfWeekMonday1
| DatePart::DayOfYear
| DatePart::Hour
| DatePart::Minute
@@ -536,6 +542,8 @@ impl ExtractDatePartExt for
PrimitiveArray<IntervalDayTimeType> {
| DatePart::Month
| DatePart::DayOfWeekSunday0
| DatePart::DayOfWeekMonday0
+ | DatePart::DayOfWeekSunday1
+ | DatePart::DayOfWeekMonday1
| DatePart::DayOfYear => {
return_compute_error_with!(format!("{part} does not support"),
self.data_type())
}
@@ -578,6 +586,8 @@ impl ExtractDatePartExt for
PrimitiveArray<IntervalMonthDayNanoType> {
| DatePart::YearISO
| DatePart::DayOfWeekSunday0
| DatePart::DayOfWeekMonday0
+ | DatePart::DayOfWeekSunday1
+ | DatePart::DayOfWeekMonday1
| DatePart::DayOfYear => {
return_compute_error_with!(format!("{part} does not support"),
self.data_type())
}
@@ -610,6 +620,8 @@ impl ExtractDatePartExt for
PrimitiveArray<DurationSecondType> {
| DatePart::Month
| DatePart::DayOfWeekSunday0
| DatePart::DayOfWeekMonday0
+ | DatePart::DayOfWeekSunday1
+ | DatePart::DayOfWeekMonday1
| DatePart::DayOfYear => {
return_compute_error_with!(format!("{part} does not support"),
self.data_type())
}
@@ -642,6 +654,8 @@ impl ExtractDatePartExt for
PrimitiveArray<DurationMillisecondType> {
| DatePart::Month
| DatePart::DayOfWeekSunday0
| DatePart::DayOfWeekMonday0
+ | DatePart::DayOfWeekSunday1
+ | DatePart::DayOfWeekMonday1
| DatePart::DayOfYear => {
return_compute_error_with!(format!("{part} does not support"),
self.data_type())
}
@@ -674,6 +688,8 @@ impl ExtractDatePartExt for
PrimitiveArray<DurationMicrosecondType> {
| DatePart::Month
| DatePart::DayOfWeekSunday0
| DatePart::DayOfWeekMonday0
+ | DatePart::DayOfWeekSunday1
+ | DatePart::DayOfWeekMonday1
| DatePart::DayOfYear => {
return_compute_error_with!(format!("{part} does not support"),
self.data_type())
}
@@ -706,6 +722,8 @@ impl ExtractDatePartExt for
PrimitiveArray<DurationNanosecondType> {
| DatePart::Month
| DatePart::DayOfWeekSunday0
| DatePart::DayOfWeekMonday0
+ | DatePart::DayOfWeekSunday1
+ | DatePart::DayOfWeekMonday1
| DatePart::DayOfYear => {
return_compute_error_with!(format!("{part} does not support"),
self.data_type())
}
@@ -985,6 +1003,46 @@ mod tests {
assert_eq!(2, b.value(2));
}
+ #[test]
+ fn test_temporal_array_date64_dayofweek1() {
+ //1514764800000 -> 2018-01-01 (Monday)
+ //1550636625000 -> 2019-02-20 (Wednesday)
+ //1483228800000 -> 2017-01-01 (Sunday)
+ let a: PrimitiveArray<Date64Type> = vec![
+ Some(1514764800000),
+ None,
+ Some(1550636625000),
+ Some(1483228800000),
+ ]
+ .into();
+
+ let b = date_part_primitive(&a, DatePart::DayOfWeekSunday1).unwrap();
+ assert_eq!(2, b.value(0));
+ assert!(!b.is_valid(1));
+ assert_eq!(4, b.value(2));
+ assert_eq!(1, b.value(3));
+ }
+
+ #[test]
+ fn test_temporal_array_date64_isodow() {
+ //1514764800000 -> 2018-01-01 (Monday)
+ //1550636625000 -> 2019-02-20 (Wednesday)
+ //1483228800000 -> 2017-01-01 (Sunday)
+ let a: PrimitiveArray<Date64Type> = vec![
+ Some(1514764800000),
+ None,
+ Some(1550636625000),
+ Some(1483228800000),
+ ]
+ .into();
+
+ let b = date_part_primitive(&a, DatePart::DayOfWeekMonday1).unwrap();
+ assert_eq!(1, b.value(0));
+ assert!(!b.is_valid(1));
+ assert_eq!(3, b.value(2));
+ assert_eq!(7, b.value(3));
+ }
+
#[test]
fn test_temporal_array_date64_weekday0() {
//1483228800000 -> 2017-01-01 (Sunday)
@@ -1579,11 +1637,15 @@ mod tests {
let invalid_parts = [
DatePart::Quarter,
DatePart::Year,
+ DatePart::YearISO,
DatePart::Month,
DatePart::Week,
+ DatePart::WeekISO,
DatePart::Day,
DatePart::DayOfWeekSunday0,
DatePart::DayOfWeekMonday0,
+ DatePart::DayOfWeekSunday1,
+ DatePart::DayOfWeekMonday1,
DatePart::DayOfYear,
];
@@ -1813,8 +1875,12 @@ mod tests {
fn ensure_returns_error(array: &dyn Array) {
let invalid_parts = [
DatePart::Quarter,
+ DatePart::YearISO,
+ DatePart::WeekISO,
DatePart::DayOfWeekSunday0,
DatePart::DayOfWeekMonday0,
+ DatePart::DayOfWeekSunday1,
+ DatePart::DayOfWeekMonday1,
DatePart::DayOfYear,
];
@@ -1956,10 +2022,14 @@ mod tests {
fn ensure_returns_error(array: &dyn Array) {
let invalid_parts = [
DatePart::Year,
+ DatePart::YearISO,
DatePart::Quarter,
DatePart::Month,
+ DatePart::WeekISO,
DatePart::DayOfWeekSunday0,
DatePart::DayOfWeekMonday0,
+ DatePart::DayOfWeekSunday1,
+ DatePart::DayOfWeekMonday1,
DatePart::DayOfYear,
];
@@ -2157,6 +2227,11 @@ mod tests {
("day", DatePart::Day),
("dow", DatePart::DayOfWeekSunday0),
("DayOfWeek", DatePart::DayOfWeekSunday0),
+ ("dow1", DatePart::DayOfWeekSunday1),
+ ("dayofweek1", DatePart::DayOfWeekSunday1),
+ ("DAYOFWEEK1", DatePart::DayOfWeekSunday1),
+ ("isodow", DatePart::DayOfWeekMonday1),
+ ("ISODOW", DatePart::DayOfWeekMonday1),
("doy", DatePart::DayOfYear),
("DayOfYear", DatePart::DayOfYear),
("h", DatePart::Hour),
@@ -2195,8 +2270,6 @@ mod tests {
#[test]
fn test_date_part_from_str_unknown() {
// Names intentionally rejected — see the FromStr doc comment for why.
- // `isodow` is here because mapping it to `DayOfWeekMonday0` would
- // silently shift the value range vs. PostgreSQL's `isodow` (1..=7).
let unknown = [
"epoch",
"century",
@@ -2205,7 +2278,6 @@ mod tests {
"timezone",
"timezone_hour",
"timezone_minute",
- "isodow",
// Whitespace is not trimmed — pin this so the behavior doesn't
// change silently. Callers should trim before parsing.
" year ",