berkaysynnada commented on code in PR #5603:
URL: https://github.com/apache/arrow-datafusion/pull/5603#discussion_r1139219837
##########
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:
In the
[example](https://github.com/apache/arrow-rs/pull/3801/files#diff-5f92a7816bbae9b685c2f85ab84a268b85246bfaa14272c5afd339810ad471f3R22)
you mentioned, there is a function `string_to_datetime`, taking the timestamp
and timezone as a whole string. In our case, we have a timestamp as an integer
and a string for the timezone. If I try to implement the part where we parse
the timezone string to `Tz`, I need to use `FromStr::from_str` implemented for
Tz. However, there are 2 different implementations of that function inside
timezone.rs:
The first one is protected with chrono-tz feature. Even if I add that
feature, Tz struct and the functions are under `private` namespace which is not
public.
On the other hand, the second implementation cannot handle timezones such as
`"America/Los_Angeles"`, when I try to use it without any modification on arrow
and cargo.toml.
I suggest that you can merge this PR, and when the new release that enables
us to parse timezone string is announced, I will open a new PR supporting that
kind of timezones.
I don't think it will look good, but I can provide this support by
duplicating the parts that will work for me in the arrow code.
--
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]