Skip to content

Commit a968da4

Browse files
committed
Address review: floor time ± interval like timestamp, simplify coercion
Apply the interval at nanosecond precision and floor to the time's unit via div_euclid (with the sign applied before the floor), so `time(s) - interval '1 nanosecond'` rolls back a full second exactly as `timestamp(s) - interval '1 nanosecond'` does, instead of truncating toward zero. Adds an slt case for it. Also take the suggestion to bind the time type and the result type in a single match arm in the coercion.
1 parent c7493f6 commit a968da4

3 files changed

Lines changed: 32 additions & 26 deletions

File tree

‎datafusion/expr-common/src/type_coercion/binary.rs‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -276,20 +276,13 @@ impl<'a> BinaryTypeCoercer<'a> {
276276
// interval at that resolution. So, like `timestamp(s) + interval
277277
// '1 nanosecond'`, `time(s) + interval '1 nanosecond'` is a no-op rather
278278
// than widening the type.
279-
let time = if let Interval(_) = lhs {
280-
rhs.clone()
281-
} else {
282-
lhs.clone()
283-
};
284-
let (lhs, rhs) = match (lhs, rhs) {
285-
(Interval(_), _) => (Interval(MonthDayNano), time.clone()),
286-
(_, _) => (time.clone(), Interval(MonthDayNano)),
279+
let (lhs, rhs, ret) = match (lhs, rhs) {
280+
(Interval(_), time) => {
281+
(Interval(MonthDayNano), time.clone(), time.clone())
282+
}
283+
(time, _) => (time.clone(), Interval(MonthDayNano), time.clone()),
287284
};
288-
return Ok(Signature {
289-
lhs,
290-
rhs,
291-
ret: time,
292-
});
285+
return Ok(Signature { lhs, rhs, ret });
293286
}
294287
Plus | Minus | Multiply | Divide | Modulo => {
295288
if let Ok(ret) = self.get_result(lhs, rhs) {

‎datafusion/physical-expr/src/expressions/binary.rs‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,12 @@ fn is_time_minus_interval(lhs: &DataType, rhs: &DataType) -> bool {
302302
/// not implement time-of-day arithmetic, so it is handled here.
303303
///
304304
/// The result keeps the input time's unit; the interval (normalized to `MonthDayNano`
305-
/// by the coercion layer) is applied at that resolution, mirroring `timestamp + interval`.
306-
/// Only the sub-day portion of the interval affects a time-of-day -- whole months and
307-
/// days are ignored, matching PostgreSQL -- and any interval precision finer than the
308-
/// time's unit is truncated (so `time(s) + interval '1 nanosecond'` is a no-op).
305+
/// by the coercion layer) is applied at nanosecond precision and floored to that unit,
306+
/// mirroring `timestamp(unit) + interval`. Only the sub-day portion of the interval
307+
/// affects a time-of-day -- whole months and days are ignored, matching PostgreSQL. The
308+
/// floor is applied after the sign, so `time(s) + interval '1 nanosecond'` is a no-op
309+
/// while `time(s) - interval '1 nanosecond'` rolls back a second, exactly as the
310+
/// timestamp case does.
309311
fn apply_time_interval(
310312
lhs: &ColumnarValue,
311313
rhs: &ColumnarValue,
@@ -359,13 +361,17 @@ where
359361
// Units in a 24-hour day, at `T`'s resolution.
360362
let day_units = DAY_NANOS / ns_per_unit;
361363

362-
// Wraps `time ± interval` into `[0, day_units)`. The interval's nanoseconds are
363-
// reduced modulo a day (so the sum stays within `i64`) and converted to `T`'s unit,
364-
// truncating any finer precision; `rem_euclid` keeps the result non-negative when
365-
// subtracting. The wrapped value is in `[0, day_units)`, which always fits `T::Native`.
364+
// Wraps `time ± interval` into `[0, day_units)`. The interval is reduced modulo a day
365+
// (so the sum stays within `i64`), applied at nanosecond precision, then floored to
366+
// `T`'s unit -- matching `timestamp(unit) ± interval`. Because the floor is applied
367+
// after the sign, `time(s) - interval '1 nanosecond'` rolls back a full second, just
368+
// as the timestamp case does, while `time(s) + interval '1 nanosecond'` is a no-op.
369+
// `div_euclid`/`rem_euclid` floor toward negative infinity, so the wrapped value stays
370+
// in `[0, day_units)`, which always fits `T::Native`.
366371
let wrap = |time_unit: i64, iv: IntervalMonthDayNano| -> T::Native {
367-
let delta = (iv.nanoseconds % DAY_NANOS) / ns_per_unit;
368-
let delta = if subtract { -delta } else { delta };
372+
let iv_ns = iv.nanoseconds % DAY_NANOS;
373+
let signed_ns = if subtract { -iv_ns } else { iv_ns };
374+
let delta = signed_ns.div_euclid(ns_per_unit);
369375
let wrapped = (time_unit + delta).rem_euclid(day_units);
370376
T::Native::try_from(wrapped).unwrap_or_default()
371377
};

‎datafusion/sqllogictest/test_files/datetime/arith_time_interval.slt‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,21 @@ SELECT arrow_typeof(arrow_cast('22:00', 'Time64(Nanosecond)') + interval '3 hour
6868
----
6969
Time64(ns)
7070

71-
# Interval precision finer than the time's unit is truncated, exactly as for
72-
# `timestamp(unit) + interval`: adding one nanosecond to a second-resolution time is a
73-
# no-op, while a finer-resolution time keeps it.
71+
# The interval is applied at nanosecond precision and floored to the time's unit, exactly
72+
# as for `timestamp(unit) ± interval`. Adding one nanosecond to a second-resolution time
73+
# floors back to a no-op...
7474
query D
7575
SELECT arrow_cast('22:00', 'Time32(Second)') + interval '1 nanosecond'
7676
----
7777
22:00:00
7878

79+
# ...but subtracting one nanosecond floors down a full second, matching
80+
# `timestamp(s) - interval '1 nanosecond'` (= 09:59:59) rather than staying put.
81+
query D
82+
SELECT arrow_cast('10:00:00', 'Time32(Second)') - interval '1 nanosecond'
83+
----
84+
09:59:59
85+
7986
query D
8087
SELECT arrow_cast('12:00:00', 'Time32(Millisecond)') + interval '1 microsecond'
8188
----

0 commit comments

Comments
 (0)