Skip to content

Commit 35a8423

Browse files
authored
fix: Added unix_timestamp_val in _serialize_val (#5659)
1 parent e7fd506 commit 35a8423

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

sdk/python/feast/infra/key_encoding_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def _serialize_val(
2222
if 0 <= entity_key_serialization_version <= 1:
2323
return struct.pack("<l", v.int64_val), ValueType.INT64
2424
return struct.pack("<q", v.int64_val), ValueType.INT64
25+
elif value_type == "unix_timestamp_val":
26+
return struct.pack("<q", v.unix_timestamp_val), ValueType.UNIX_TIMESTAMP
2527
else:
2628
raise ValueError(f"Value type not supported for feast feature store: {v}")
2729

@@ -38,6 +40,9 @@ def _deserialize_value(value_type, value_bytes) -> ValueProto:
3840
return ValueProto(string_val=value)
3941
elif value_type == ValueType.BYTES:
4042
return ValueProto(bytes_val=value_bytes)
43+
elif value_type == ValueType.UNIX_TIMESTAMP:
44+
value = struct.unpack("<q", value_bytes)[0]
45+
return ValueProto(unix_timestamp_val=value)
4146
else:
4247
raise ValueError(f"Unsupported value type: {value_type}")
4348

sdk/python/tests/unit/infra/test_key_encoding_utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ def test_serialize_value():
7979
assert t == ValueType.INT64
8080
assert v == b"\x01\x00\x00\x00\x00\x00\x00\x00"
8181

82+
# Test unix_timestamp_val serialization
83+
v, t = _serialize_val(
84+
"unix_timestamp_val", ValueProto(unix_timestamp_val=1758823656)
85+
)
86+
assert t == ValueType.UNIX_TIMESTAMP
87+
# Verify roundtrip: deserialize the serialized value
88+
deserialized = _deserialize_value(ValueType.UNIX_TIMESTAMP, v)
89+
assert deserialized.unix_timestamp_val == 1758823656
90+
8291

8392
def test_deserialize_value():
8493
v = _deserialize_value(ValueType.STRING, b"test")
@@ -93,6 +102,33 @@ def test_deserialize_value():
93102
v = _deserialize_value(ValueType.INT64, b"\x01\x00\x00\x00\x00\x00\x00\x00")
94103
assert v.int64_val == 1
95104

105+
timestamp_val = 1758823656
106+
serialized_bytes, _ = _serialize_val(
107+
"unix_timestamp_val", ValueProto(unix_timestamp_val=timestamp_val)
108+
)
109+
v = _deserialize_value(ValueType.UNIX_TIMESTAMP, serialized_bytes)
110+
assert v.unix_timestamp_val == timestamp_val
111+
112+
113+
def test_serialize_deserialize_unix_timestamp_entity():
114+
entity_key_proto = EntityKeyProto(
115+
join_keys=["e2"],
116+
entity_values=[ValueProto(unix_timestamp_val=1758823656)],
117+
)
118+
119+
serialized_key = serialize_entity_key(
120+
entity_key_proto,
121+
entity_key_serialization_version=3,
122+
)
123+
124+
deserialized_key = deserialize_entity_key(
125+
serialized_key,
126+
entity_key_serialization_version=3,
127+
)
128+
129+
assert deserialized_key == entity_key_proto
130+
assert deserialized_key.entity_values[0].unix_timestamp_val == 1758823656
131+
96132

97133
def test_reserialize_entity_v2_key_to_v3():
98134
entity_key_proto_v2 = EntityKeyProto(

0 commit comments

Comments
 (0)