kosiew commented on code in PR #25163:
URL: https://github.com/apache/datafusion/pull/25163#discussion_r4071597656
##########
datafusion/functions/src/datetime/date_part.rs:
##########
@@ -317,6 +337,100 @@ fn is_nanosecond(part: &str) -> bool {
.unwrap_or(false)
}
+/// The `timezone`, `timezone_hour` and `timezone_minute` fields, which report
+/// the UTC offset that applies to a timezone-aware timestamp *at that
instant*.
+///
+/// Because the offset of a named timezone changes with daylight saving time,
+/// these are per-row values, not a property of the type alone.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+enum TimezonePart {
+ /// `timezone`: the whole UTC offset, in seconds
+ Offset,
+ /// `timezone_hour`: whole hours of the UTC offset
+ Hour,
+ /// `timezone_minute`: whole minutes of the UTC offset, excluding the
hours.
+ /// The sign follows the sign of the offset (PostgreSQL behaviour), so
+ /// `-03:30` yields `-3` hours and `-30` minutes.
+ Minute,
+}
+
+impl TimezonePart {
+ fn parse(part: &str) -> Option<Self> {
+ match part {
+ "timezone" => Some(Self::Offset),
+ "timezone_hour" => Some(Self::Hour),
+ "timezone_minute" => Some(Self::Minute),
+ _ => None,
+ }
+ }
+
+ fn name(&self) -> &'static str {
+ match self {
+ Self::Offset => "timezone",
+ Self::Hour => "timezone_hour",
+ Self::Minute => "timezone_minute",
+ }
+ }
+
+ /// Extract this field from a UTC offset expressed in seconds.
+ ///
+ /// Rust integer division truncates towards zero, which is what makes the
+ /// sign of `timezone_minute` follow the sign of the offset, matching
+ /// PostgreSQL.
+ fn project(&self, offset_seconds: i32) -> i32 {
+ match self {
+ Self::Offset => offset_seconds,
+ Self::Hour => offset_seconds / 3_600,
+ Self::Minute => (offset_seconds % 3_600) / 60,
+ }
+ }
+}
+
+/// Compute a [`TimezonePart`] for every element of a timezone-aware timestamp
+/// array.
+///
+/// Errors for any other input type, including timezone-naive timestamps, which
+/// carry no offset at all. This matches PostgreSQL, which rejects
+/// `date_part('timezone', <timestamp without time zone>)`.
+fn timezone_part(array: &dyn Array, part: TimezonePart) -> Result<ArrayRef> {
+ let Timestamp(unit, tz_opt) = array.data_type() else {
+ return exec_err!(
+ "Date part '{}' is only supported for timestamps with a timezone,
got {}",
+ part.name(),
+ array.data_type()
+ );
+ };
+ let Some(tz) = parse_tz(tz_opt.as_ref())? else {
Review Comment:
Could we add a small unit test with a timestamp array that has an invalid
Arrow timezone string? It would be useful to assert that the execution error
preserves the timezone value and context. This is the new caller of the
relocated `parse_tz` helper, and while the valid timezone cases cover the
success path well, a test here would also protect the user-visible error path
for externally constructed Arrow timestamp schemas.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]