Skip to content

Commit cb23648

Browse files
author
Andrew Pope
authored
fix: Conversion of null timestamp from proto to python (#2814)
Signed-off-by: Andrew Pope <apope@nursefly.com>
1 parent 6b4e8d0 commit cb23648

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

sdk/python/feast/type_map.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@
4646
from feast.protos.feast.types.Value_pb2 import Value as ProtoValue
4747
from feast.value_type import ListType, ValueType
4848

49+
# null timestamps get converted to -9223372036854775808
50+
NULL_TIMESTAMP_INT_VALUE = np.datetime64("NaT").astype(int)
51+
4952

5053
def feast_value_type_to_python_type(field_value_proto: ProtoValue) -> Any:
5154
"""
@@ -69,9 +72,18 @@ def feast_value_type_to_python_type(field_value_proto: ProtoValue) -> Any:
6972

7073
# Convert UNIX_TIMESTAMP values to `datetime`
7174
if val_attr == "unix_timestamp_list_val":
72-
val = [datetime.fromtimestamp(v, tz=timezone.utc) for v in val]
75+
val = [
76+
datetime.fromtimestamp(v, tz=timezone.utc)
77+
if v != NULL_TIMESTAMP_INT_VALUE
78+
else None
79+
for v in val
80+
]
7381
elif val_attr == "unix_timestamp_val":
74-
val = datetime.fromtimestamp(val, tz=timezone.utc)
82+
val = (
83+
datetime.fromtimestamp(val, tz=timezone.utc)
84+
if val != NULL_TIMESTAMP_INT_VALUE
85+
else None
86+
)
7587

7688
return val
7789

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
3+
from feast.type_map import (
4+
feast_value_type_to_python_type,
5+
python_values_to_proto_values,
6+
)
7+
from feast.value_type import ValueType
8+
9+
10+
def test_null_unix_timestamp():
11+
"""Test that null UnixTimestamps get converted from proto correctly."""
12+
13+
data = np.array(["NaT"], dtype="datetime64")
14+
protos = python_values_to_proto_values(data, ValueType.UNIX_TIMESTAMP)
15+
converted = feast_value_type_to_python_type(protos[0])
16+
17+
assert converted is None
18+
19+
20+
def test_null_unix_timestamp_list():
21+
"""Test that UnixTimestamp lists with a null get converted from proto
22+
correctly."""
23+
24+
data = np.array([["NaT"]], dtype="datetime64")
25+
protos = python_values_to_proto_values(data, ValueType.UNIX_TIMESTAMP_LIST)
26+
converted = feast_value_type_to_python_type(protos[0])
27+
28+
assert converted[0] is None

0 commit comments

Comments
 (0)