Skip to content

Commit 5eefef9

Browse files
Sonica-Bntkathole
authored andcommitted
fix: Floor pre-1970 fractional-second timestamps when encoding to Unix seconds
Datetime and pd.Timestamp values were converted with int(dt.timestamp()), which truncates toward zero. A pre-1970 instant with a fractional second, such as 1969-12-31 23:59:59.5 UTC, was stored as 0 (1970-01-01 00:00:00) instead of -1, while the same instant given as np.datetime64 floored to -1. Compute whole seconds with exact integer arithmetic in one helper and use it for UNIX_TIMESTAMP, UNIX_TIMESTAMP_LIST/SET, ZONED_TIMESTAMP and SCALAR_MAP datetime keys. Naive datetimes are still read as UTC. Signed-off-by: Sonica-B <shreyaa.boyane@gmail.com>
1 parent 029dc95 commit 5eefef9

2 files changed

Lines changed: 56 additions & 8 deletions

File tree

‎sdk/python/feast/type_map.py‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,22 @@ def _type_err(item, dtype):
723723
}
724724

725725

726+
_UNIX_EPOCH = datetime(1970, 1, 1, tzinfo=timezone.utc)
727+
728+
729+
def _datetime_to_unix_seconds(value: datetime) -> int:
730+
"""Whole seconds since the epoch, floored like numpy's datetime64[s] cast.
731+
732+
A naive datetime is UTC, as everywhere else in Feast; datetime.timestamp()
733+
would read it in the machine's local timezone. int(value.timestamp()) would
734+
also truncate toward zero, moving a pre-1970 datetime with a fractional
735+
second one second later (23:59:59.5 becomes 00:00:00).
736+
"""
737+
if value.utcoffset() is None:
738+
value = value.replace(tzinfo=timezone.utc)
739+
return (value - _UNIX_EPOCH) // timedelta(seconds=1)
740+
741+
726742
def _python_datetime_to_int_timestamp(
727743
values: Sequence[Any],
728744
) -> Sequence[Union[int, np.int_]]:
@@ -735,11 +751,7 @@ def _python_datetime_to_int_timestamp(
735751
int_timestamps = []
736752
for value in values:
737753
if isinstance(value, datetime):
738-
# A naive datetime is UTC, as everywhere else in Feast. Without this,
739-
# datetime.timestamp() would read it in the machine's local timezone.
740-
if value.utcoffset() is None:
741-
value = value.replace(tzinfo=timezone.utc)
742-
int_timestamps.append(int(value.timestamp()))
754+
int_timestamps.append(_datetime_to_unix_seconds(value))
743755
elif isinstance(value, Timestamp):
744756
int_timestamps.append(int(value.ToSeconds()))
745757
elif isinstance(value, np.datetime64):
@@ -1156,7 +1168,7 @@ def _convert_scalar_values_to_proto(
11561168
out.append(
11571169
ProtoValue(
11581170
zoned_timestamp_val=ZonedTimestamp(
1159-
unix_timestamp=int(dt.timestamp()),
1171+
unix_timestamp=_datetime_to_unix_seconds(dt),
11601172
zone=_zone_name(value.tzinfo),
11611173
)
11621174
) # type: ignore
@@ -1498,8 +1510,7 @@ def _python_value_to_map_key_proto(key: Any) -> MapKey:
14981510
if isinstance(key, bytes):
14991511
return MapKey(bytes_key=key)
15001512
if isinstance(key, (datetime, pd.Timestamp)):
1501-
ts = int(pd.Timestamp(key).timestamp())
1502-
return MapKey(unix_timestamp_key=ts)
1513+
return MapKey(unix_timestamp_key=_datetime_to_unix_seconds(key))
15031514
raise TypeError(
15041515
f"Unsupported key type for SCALAR_MAP: {type(key).__name__}. "
15051516
"Supported non-string key types: int, float, bool, uuid.UUID, "

‎sdk/python/tests/unit/test_type_map.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
_convert_value_type_str_to_value_type,
1616
_python_dict_to_map_proto,
1717
_python_list_to_map_list_proto,
18+
_python_value_to_map_key_proto,
1819
arrow_to_pg_type,
1920
feast_value_type_to_pa,
2021
feast_value_type_to_python_type,
@@ -109,6 +110,42 @@ def test_naive_datetime_unix_timestamp_is_utc(
109110
assert list(converted) == [expected]
110111

111112

113+
_PRE_EPOCH_DATETIME = datetime(1969, 12, 31, 23, 59, 59, 500000, tzinfo=timezone.utc)
114+
_PRE_EPOCH_DATETIME64 = np.datetime64("1969-12-31T23:59:59.5")
115+
116+
117+
@pytest.mark.parametrize(
118+
"value_type, value",
119+
[
120+
(ValueType.UNIX_TIMESTAMP, _PRE_EPOCH_DATETIME),
121+
(ValueType.UNIX_TIMESTAMP, pd.Timestamp(_PRE_EPOCH_DATETIME)),
122+
(ValueType.UNIX_TIMESTAMP, _PRE_EPOCH_DATETIME64),
123+
(ValueType.UNIX_TIMESTAMP_LIST, [_PRE_EPOCH_DATETIME]),
124+
(ValueType.UNIX_TIMESTAMP_LIST, [_PRE_EPOCH_DATETIME64]),
125+
(ValueType.UNIX_TIMESTAMP_SET, [_PRE_EPOCH_DATETIME]),
126+
(ValueType.ZONED_TIMESTAMP, _PRE_EPOCH_DATETIME),
127+
(ValueType.ZONED_TIMESTAMP, pd.Timestamp(_PRE_EPOCH_DATETIME)),
128+
],
129+
)
130+
def test_pre_epoch_fractional_second_floors(value_type, value):
131+
"""A pre-1970 instant with a fractional second floors to the earlier second,
132+
as numpy's datetime64[s] cast does, instead of rounding up to the epoch."""
133+
proto = python_values_to_proto_values([value], value_type)[0]
134+
converted = feast_value_type_to_python_type(proto)
135+
136+
expected = datetime(1969, 12, 31, 23, 59, 59, tzinfo=timezone.utc)
137+
if isinstance(value, list):
138+
assert list(converted) == [expected]
139+
else:
140+
assert converted == expected
141+
142+
143+
def test_pre_epoch_fractional_second_map_key_floors():
144+
"""A pre-1970 SCALAR_MAP datetime key floors like a UNIX_TIMESTAMP value."""
145+
key = _python_value_to_map_key_proto(_PRE_EPOCH_DATETIME)
146+
assert key.unix_timestamp_key == -1
147+
148+
112149
@pytest.mark.parametrize(
113150
"values",
114151
(

0 commit comments

Comments
 (0)