You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "tustvold (via GitHub)" <gi...@apache.org> on 2023/03/15 11:43:57 UTC

[GitHub] [arrow-datafusion] tustvold commented on a diff in pull request #5603: Timestamp subtraction and interval operations for `ScalarValue`

tustvold commented on code in PR #5603:
URL: https://github.com/apache/arrow-datafusion/pull/5603#discussion_r1136922654


##########
datafusion/common/src/scalar.rs:
##########
@@ -563,46 +843,154 @@ macro_rules! get_sign {
     };
 }
 
+#[derive(Clone, Copy)]
+enum IntervalMode {
+    Milli,
+    Nano,
+}
+
+/// This function computes subtracts `rhs_ts` from `lhs_ts`, taking timezones
+/// into account when given. Units of the resulting interval is specified by
+/// the argument `mode`.
+/// The default behavior of Datafusion is the following:
+/// - When subtracting timestamps at seconds/milliseconds precision, the output
+///   interval will have the type [`IntervalDayTimeType`].
+/// - When subtracting timestamps at microseconds/nanoseconds precision, the
+///   output interval will have the type [`IntervalMonthDayNanoType`].
+fn ts_sub_to_interval(
+    lhs_ts: i64,
+    rhs_ts: i64,
+    lhs_tz: &Option<String>,
+    rhs_tz: &Option<String>,
+    mode: IntervalMode,
+) -> Result<ScalarValue, DataFusionError> {
+    let lhs_dt = with_timezone_to_naive_datetime(lhs_ts, lhs_tz, mode)?;
+    let rhs_dt = with_timezone_to_naive_datetime(rhs_ts, rhs_tz, mode)?;
+    let delta_secs = lhs_dt.signed_duration_since(rhs_dt);
+
+    match mode {
+        IntervalMode::Milli => {
+            let as_millisecs = delta_secs.num_milliseconds();
+            Ok(ScalarValue::IntervalDayTime(Some(
+                IntervalDayTimeType::make_value(
+                    (as_millisecs / MILLISECS_IN_ONE_DAY) as i32,
+                    (as_millisecs % MILLISECS_IN_ONE_DAY) as i32,
+                ),
+            )))
+        }
+        IntervalMode::Nano => {
+            let as_nanosecs = delta_secs.num_nanoseconds().ok_or_else(|| {
+                DataFusionError::Execution(String::from(
+                    "Can not compute timestamp differences with nanosecond precision",
+                ))
+            })?;
+            Ok(ScalarValue::IntervalMonthDayNano(Some(
+                IntervalMonthDayNanoType::make_value(
+                    0,
+                    (as_nanosecs / NANOSECS_IN_ONE_DAY) as i32,
+                    as_nanosecs % NANOSECS_IN_ONE_DAY,
+                ),
+            )))
+        }
+    }
+}
+
+/// This function creates the [`NaiveDateTime`] object corresponding to the
+/// given timestamp using the units (tick size) implied by argument `mode`.
+#[inline]
+fn with_timezone_to_naive_datetime(

Review Comment:
   This should probably make use of https://docs.rs/arrow-array/latest/arrow_array/timezone/struct.Tz.html to parse to a `DateTime<Tz>`
   
   Crucially this handles things like daylight savings time



##########
datafusion/common/src/scalar.rs:
##########
@@ -563,46 +843,154 @@ macro_rules! get_sign {
     };
 }
 
+#[derive(Clone, Copy)]
+enum IntervalMode {
+    Milli,
+    Nano,
+}
+
+/// This function computes subtracts `rhs_ts` from `lhs_ts`, taking timezones
+/// into account when given. Units of the resulting interval is specified by
+/// the argument `mode`.
+/// The default behavior of Datafusion is the following:
+/// - When subtracting timestamps at seconds/milliseconds precision, the output
+///   interval will have the type [`IntervalDayTimeType`].
+/// - When subtracting timestamps at microseconds/nanoseconds precision, the
+///   output interval will have the type [`IntervalMonthDayNanoType`].
+fn ts_sub_to_interval(
+    lhs_ts: i64,
+    rhs_ts: i64,
+    lhs_tz: &Option<String>,
+    rhs_tz: &Option<String>,
+    mode: IntervalMode,
+) -> Result<ScalarValue, DataFusionError> {
+    let lhs_dt = with_timezone_to_naive_datetime(lhs_ts, lhs_tz, mode)?;
+    let rhs_dt = with_timezone_to_naive_datetime(rhs_ts, rhs_tz, mode)?;
+    let delta_secs = lhs_dt.signed_duration_since(rhs_dt);
+
+    match mode {
+        IntervalMode::Milli => {
+            let as_millisecs = delta_secs.num_milliseconds();
+            Ok(ScalarValue::IntervalDayTime(Some(
+                IntervalDayTimeType::make_value(
+                    (as_millisecs / MILLISECS_IN_ONE_DAY) as i32,
+                    (as_millisecs % MILLISECS_IN_ONE_DAY) as i32,
+                ),
+            )))
+        }
+        IntervalMode::Nano => {
+            let as_nanosecs = delta_secs.num_nanoseconds().ok_or_else(|| {
+                DataFusionError::Execution(String::from(
+                    "Can not compute timestamp differences with nanosecond precision",
+                ))
+            })?;
+            Ok(ScalarValue::IntervalMonthDayNano(Some(
+                IntervalMonthDayNanoType::make_value(
+                    0,
+                    (as_nanosecs / NANOSECS_IN_ONE_DAY) as i32,
+                    as_nanosecs % NANOSECS_IN_ONE_DAY,
+                ),
+            )))
+        }
+    }
+}
+
+/// This function creates the [`NaiveDateTime`] object corresponding to the
+/// given timestamp using the units (tick size) implied by argument `mode`.
+#[inline]
+fn with_timezone_to_naive_datetime(

Review Comment:
   This should probably make use of https://docs.rs/arrow-array/latest/arrow_array/timezone/struct.Tz.html to parse to a `DateTime<Tz>`
   
   Crucially this handles things like daylight savings time, where the timezone offset depends on the time in question



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org