Skip to content

Commit e60b57e

Browse files
committed
feat: Add UUID and TIME_UUID as feature types (feast-dev#5885)
1 parent 4c66e76 commit e60b57e

8 files changed

Lines changed: 105 additions & 13 deletions

File tree

‎sdk/python/feast/feature_store.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,7 +2319,11 @@ def _doc_feature(x):
23192319
online_features_response=online_features_response,
23202320
data=requested_features_data,
23212321
)
2322-
return OnlineResponse(online_features_response)
2322+
feature_types = {
2323+
f.name: f.dtype.to_value_type()
2324+
for f in requested_feature_view.features
2325+
}
2326+
return OnlineResponse(online_features_response, feature_types=feature_types)
23232327

23242328
def retrieve_online_documents_v2(
23252329
self,
@@ -2603,7 +2607,11 @@ def _retrieve_from_online_store_v2(
26032607
online_features_response.metadata.feature_names.val.extend(
26042608
features_to_request
26052609
)
2606-
return OnlineResponse(online_features_response)
2610+
feature_types = {
2611+
f.name: f.dtype.to_value_type()
2612+
for f in table.features
2613+
}
2614+
return OnlineResponse(online_features_response, feature_types=feature_types)
26072615

26082616
table_entity_values, idxs, output_len = utils._get_unique_entities_from_values(
26092617
entity_key_dict,
@@ -2630,7 +2638,11 @@ def _retrieve_from_online_store_v2(
26302638
data=entity_key_dict,
26312639
)
26322640

2633-
return OnlineResponse(online_features_response)
2641+
feature_types = {
2642+
f.name: f.dtype.to_value_type()
2643+
for f in table.features
2644+
}
2645+
return OnlineResponse(online_features_response, feature_types=feature_types)
26342646

26352647
def serve(
26362648
self,

‎sdk/python/feast/infra/online_stores/online_store.py‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,21 @@ def get_online_features(
218218
output_len,
219219
)
220220

221+
feature_types = self._build_feature_types(grouped_refs)
222+
221223
if requested_on_demand_feature_views:
222224
utils._augment_response_with_on_demand_transforms(
223225
online_features_response,
224226
feature_refs,
225227
requested_on_demand_feature_views,
226228
full_feature_names,
229+
feature_types=feature_types,
227230
)
228231

229232
utils._drop_unneeded_columns(
230233
online_features_response, requested_result_row_names
231234
)
232-
return OnlineResponse(online_features_response)
235+
return OnlineResponse(online_features_response, feature_types=feature_types)
233236

234237
async def get_online_features_async(
235238
self,
@@ -318,18 +321,35 @@ async def query_table(table, requested_features):
318321
output_len,
319322
)
320323

324+
feature_types = self._build_feature_types(grouped_refs)
325+
321326
if requested_on_demand_feature_views:
322327
utils._augment_response_with_on_demand_transforms(
323328
online_features_response,
324329
feature_refs,
325330
requested_on_demand_feature_views,
326331
full_feature_names,
332+
feature_types=feature_types,
327333
)
328334

329335
utils._drop_unneeded_columns(
330336
online_features_response, requested_result_row_names
331337
)
332-
return OnlineResponse(online_features_response)
338+
return OnlineResponse(online_features_response, feature_types=feature_types)
339+
340+
@staticmethod
341+
def _build_feature_types(
342+
grouped_refs: List,
343+
) -> Dict[str, "ValueType"]:
344+
"""Build a mapping of feature names to ValueType from grouped feature view refs."""
345+
from feast.value_type import ValueType as VT
346+
347+
feature_types: Dict[str, VT] = {}
348+
for table, requested_features in grouped_refs:
349+
for field in table.features:
350+
if field.name in requested_features:
351+
feature_types[field.name] = field.dtype.to_value_type()
352+
return feature_types
333353

334354
@abstractmethod
335355
def update(

‎sdk/python/feast/on_demand_feature_view.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import functools
3+
import uuid
34
import warnings
45
from types import FunctionType
56
from typing import Any, List, Optional, Union, cast
@@ -1093,6 +1094,9 @@ def _get_sample_values_by_type(self) -> dict[ValueType, list[Any]]:
10931094
# Special binary types
10941095
ValueType.PDF_BYTES: [pdf_sample],
10951096
ValueType.IMAGE_BYTES: [image_sample],
1097+
# UUID types
1098+
ValueType.UUID: [str(uuid.uuid4())],
1099+
ValueType.TIME_UUID: [str(uuid.uuid1())],
10961100
# List types
10971101
ValueType.BYTES_LIST: [[b"hello world"]],
10981102
ValueType.STRING_LIST: [["hello world"]],

‎sdk/python/feast/online_response.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import TYPE_CHECKING, Any, Dict, List, TypeAlias, Union
15+
from typing import TYPE_CHECKING, Any, Dict, List, Optional, TypeAlias, Union
1616

1717
import pandas as pd
1818
import pyarrow as pa
@@ -21,6 +21,7 @@
2121
from feast.protos.feast.serving.ServingService_pb2 import GetOnlineFeaturesResponse
2222
from feast.torch_wrapper import get_torch
2323
from feast.type_map import feast_value_type_to_python_type
24+
from feast.value_type import ValueType
2425

2526
if TYPE_CHECKING:
2627
import torch
@@ -37,14 +38,20 @@ class OnlineResponse:
3738
Defines an online response in feast.
3839
"""
3940

40-
def __init__(self, online_response_proto: GetOnlineFeaturesResponse):
41+
def __init__(
42+
self,
43+
online_response_proto: GetOnlineFeaturesResponse,
44+
feature_types: Optional[Dict[str, ValueType]] = None,
45+
):
4146
"""
4247
Construct a native online response from its protobuf version.
4348
4449
Args:
4550
online_response_proto: GetOnlineResponse proto object to construct from.
51+
feature_types: Optional mapping of feature names to ValueType for type-aware deserialization.
4652
"""
4753
self.proto = online_response_proto
54+
self._feature_types = feature_types or {}
4855
# Delete DUMMY_ENTITY_ID from proto if it exists
4956
for idx, val in enumerate(self.proto.metadata.feature_names.val):
5057
if val == DUMMY_ENTITY_ID:
@@ -65,8 +72,10 @@ def to_dict(self, include_event_timestamps: bool = False) -> Dict[str, Any]:
6572
for feature_ref, feature_vector in zip(
6673
self.proto.metadata.feature_names.val, self.proto.results
6774
):
75+
feature_type = self._feature_types.get(feature_ref)
6876
response[feature_ref] = [
69-
feast_value_type_to_python_type(v) for v in feature_vector.values
77+
feast_value_type_to_python_type(v, feature_type)
78+
for v in feature_vector.values
7079
]
7180

7281
if include_event_timestamps:

‎sdk/python/feast/type_map.py‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import decimal
1616
import json
1717
import logging
18+
import uuid as uuid_module
1819
from collections import defaultdict
1920
from datetime import datetime, timezone
2021
from typing import (
@@ -60,7 +61,10 @@
6061
logger = logging.getLogger(__name__)
6162

6263

63-
def feast_value_type_to_python_type(field_value_proto: ProtoValue) -> Any:
64+
def feast_value_type_to_python_type(
65+
field_value_proto: ProtoValue,
66+
feature_type: Optional[ValueType] = None,
67+
) -> Any:
6468
"""
6569
Converts field value Proto to Dict and returns each field's Feast Value Type value
6670
in their respective Python value.
@@ -103,6 +107,9 @@ def feast_value_type_to_python_type(field_value_proto: ProtoValue) -> Any:
103107
else None
104108
)
105109

110+
if feature_type in (ValueType.UUID, ValueType.TIME_UUID) and isinstance(val, str):
111+
return uuid_module.UUID(val)
112+
106113
return val
107114

108115

@@ -139,6 +146,8 @@ def feast_value_type_to_pandas_type(value_type: ValueType) -> Any:
139146
ValueType.BYTES: "bytes",
140147
ValueType.BOOL: "bool",
141148
ValueType.UNIX_TIMESTAMP: "datetime64[ns]",
149+
ValueType.UUID: "str",
150+
ValueType.TIME_UUID: "str",
142151
}
143152
if value_type.name == "MAP" or value_type.name.endswith("_LIST"):
144153
return "object"
@@ -198,6 +207,7 @@ def python_type_to_feast_value_type(
198207
"datetime64[ns, utc]": ValueType.UNIX_TIMESTAMP,
199208
"date": ValueType.UNIX_TIMESTAMP,
200209
"category": ValueType.STRING,
210+
"uuid": ValueType.UUID,
201211
}
202212

203213
if type_name in type_map:
@@ -316,6 +326,10 @@ def _convert_value_type_str_to_value_type(type_str: str) -> ValueType:
316326
"FLOAT_LIST": ValueType.FLOAT_LIST,
317327
"BOOL_LIST": ValueType.BOOL_LIST,
318328
"UNIX_TIMESTAMP_LIST": ValueType.UNIX_TIMESTAMP_LIST,
329+
"UUID": ValueType.UUID,
330+
"TIME_UUID": ValueType.TIME_UUID,
331+
"UUID_LIST": ValueType.UUID_LIST,
332+
"TIME_UUID_LIST": ValueType.TIME_UUID_LIST,
319333
}
320334
return type_map.get(type_str, ValueType.STRING)
321335

@@ -347,6 +361,8 @@ def _type_err(item, dtype):
347361
ValueType.STRING_LIST: (StringList, "string_list_val", [np.str_, str]),
348362
ValueType.BOOL_LIST: (BoolList, "bool_list_val", [np.bool_, bool]),
349363
ValueType.BYTES_LIST: (BytesList, "bytes_list_val", [np.bytes_, bytes]),
364+
ValueType.UUID_LIST: (StringList, "string_list_val", [np.str_, str, uuid_module.UUID]),
365+
ValueType.TIME_UUID_LIST: (StringList, "string_list_val", [np.str_, str, uuid_module.UUID]),
350366
}
351367

352368
PYTHON_SCALAR_VALUE_TYPE_TO_PROTO_VALUE: Dict[
@@ -372,6 +388,8 @@ def _type_err(item, dtype):
372388
ValueType.BYTES: ("bytes_val", lambda x: x, {bytes}),
373389
ValueType.IMAGE_BYTES: ("bytes_val", lambda x: x, {bytes}),
374390
ValueType.BOOL: ("bool_val", lambda x: x, {bool, np.bool_, int, np.int_}),
391+
ValueType.UUID: ("string_val", lambda x: str(x), {str, uuid_module.UUID}),
392+
ValueType.TIME_UUID: ("string_val", lambda x: str(x), {str, uuid_module.UUID}),
375393
}
376394

377395

@@ -868,6 +886,8 @@ def _convert_value_name_to_snowflake_udf(value_name: str, project_name: str) ->
868886
"FLOAT_LIST": f"feast_{project_name}_snowflake_array_float_to_list_double_proto",
869887
"BOOL_LIST": f"feast_{project_name}_snowflake_array_boolean_to_list_bool_proto",
870888
"UNIX_TIMESTAMP_LIST": f"feast_{project_name}_snowflake_array_timestamp_to_list_unix_timestamp_proto",
889+
"UUID": f"feast_{project_name}_snowflake_varchar_to_string_proto",
890+
"TIME_UUID": f"feast_{project_name}_snowflake_varchar_to_string_proto",
871891
}
872892
return name_map[value_name].upper()
873893

@@ -1047,8 +1067,8 @@ def pg_type_to_feast_value_type(type_str: str) -> ValueType:
10471067
"timestamp with time zone[]": ValueType.UNIX_TIMESTAMP_LIST,
10481068
"numeric[]": ValueType.DOUBLE_LIST,
10491069
"numeric": ValueType.DOUBLE,
1050-
"uuid": ValueType.STRING,
1051-
"uuid[]": ValueType.STRING_LIST,
1070+
"uuid": ValueType.UUID,
1071+
"uuid[]": ValueType.UUID_LIST,
10521072
}
10531073
value = (
10541074
type_map[type_str.lower()]
@@ -1083,6 +1103,10 @@ def feast_value_type_to_pa(
10831103
ValueType.BOOL_LIST: pyarrow.list_(pyarrow.bool_()),
10841104
ValueType.UNIX_TIMESTAMP_LIST: pyarrow.list_(pyarrow.timestamp(timestamp_unit)),
10851105
ValueType.NULL: pyarrow.null(),
1106+
ValueType.UUID: pyarrow.string(),
1107+
ValueType.TIME_UUID: pyarrow.string(),
1108+
ValueType.UUID_LIST: pyarrow.list_(pyarrow.string()),
1109+
ValueType.TIME_UUID_LIST: pyarrow.list_(pyarrow.string()),
10861110
}
10871111
return type_map[feast_type]
10881112

@@ -1221,7 +1245,7 @@ def cb_columnar_type_to_feast_value_type(type_str: str) -> ValueType:
12211245
"object": ValueType.UNKNOWN,
12221246
"array": ValueType.UNKNOWN,
12231247
"multiset": ValueType.UNKNOWN,
1224-
"uuid": ValueType.STRING,
1248+
"uuid": ValueType.UUID,
12251249
}
12261250
value = (
12271251
type_map[type_str.lower()]
@@ -1247,6 +1271,8 @@ def convert_scalar_column(
12471271
return series.astype("boolean")
12481272
elif value_type == ValueType.STRING:
12491273
return series.astype("string")
1274+
elif value_type in [ValueType.UUID, ValueType.TIME_UUID]:
1275+
return series.astype("string")
12501276
elif value_type == ValueType.UNIX_TIMESTAMP:
12511277
return pd.to_datetime(series, unit="s", errors="coerce")
12521278
else:

‎sdk/python/feast/types.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
"BYTES": "BYTES",
2626
"PDF_BYTES": "PDF_BYTES",
2727
"IMAGE_BYTES": "IMAGE_BYTES",
28+
"UUID": "UUID",
29+
"TIME_UUID": "TIME_UUID",
2830
"STRING": "STRING",
2931
"INT32": "INT32",
3032
"INT64": "INT64",
@@ -85,6 +87,8 @@ class PrimitiveFeastType(Enum):
8587
PDF_BYTES = 9
8688
IMAGE_BYTES = 10
8789
MAP = 11
90+
UUID = 12
91+
TIME_UUID = 13
8892

8993
def to_value_type(self) -> ValueType:
9094
"""
@@ -118,6 +122,8 @@ def __hash__(self):
118122
Float64 = PrimitiveFeastType.FLOAT64
119123
UnixTimestamp = PrimitiveFeastType.UNIX_TIMESTAMP
120124
Map = PrimitiveFeastType.MAP
125+
Uuid = PrimitiveFeastType.UUID
126+
TimeUuid = PrimitiveFeastType.TIME_UUID
121127

122128
SUPPORTED_BASE_TYPES = [
123129
Invalid,
@@ -132,6 +138,8 @@ def __hash__(self):
132138
Float64,
133139
UnixTimestamp,
134140
Map,
141+
Uuid,
142+
TimeUuid,
135143
]
136144

137145
PRIMITIVE_FEAST_TYPES_TO_STRING = {
@@ -147,6 +155,8 @@ def __hash__(self):
147155
"FLOAT64": "Float64",
148156
"UNIX_TIMESTAMP": "UnixTimestamp",
149157
"MAP": "Map",
158+
"UUID": "Uuid",
159+
"TIME_UUID": "TimeUuid",
150160
}
151161

152162

@@ -202,6 +212,10 @@ def __str__(self):
202212
ValueType.UNIX_TIMESTAMP_LIST: Array(UnixTimestamp),
203213
ValueType.MAP: Map,
204214
ValueType.MAP_LIST: Array(Map),
215+
ValueType.UUID: Uuid,
216+
ValueType.TIME_UUID: TimeUuid,
217+
ValueType.UUID_LIST: Array(Uuid),
218+
ValueType.TIME_UUID_LIST: Array(TimeUuid),
205219
}
206220

207221
FEAST_TYPES_TO_PYARROW_TYPES = {
@@ -213,6 +227,8 @@ def __str__(self):
213227
Float64: pyarrow.float64(),
214228
# Note: datetime only supports microseconds https://github.com/python/cpython/blob/3.8/Lib/datetime.py#L1559
215229
UnixTimestamp: pyarrow.timestamp("us", tz=_utc_now().tzname()),
230+
Uuid: pyarrow.string(),
231+
TimeUuid: pyarrow.string(),
216232
}
217233

218234
FEAST_VECTOR_TYPES: List[Union[ValueType, PrimitiveFeastType, ComplexFeastType]] = [

‎sdk/python/feast/utils.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,7 @@ def _augment_response_with_on_demand_transforms(
649649
feature_refs: List[str],
650650
requested_on_demand_feature_views: List["OnDemandFeatureView"],
651651
full_feature_names: bool,
652+
feature_types: Optional[Dict[str, "ValueType"]] = None,
652653
):
653654
"""Computes on demand feature values and adds them to the result rows.
654655
@@ -679,7 +680,7 @@ def _augment_response_with_on_demand_transforms(
679680
else feature_name
680681
)
681682

682-
initial_response = OnlineResponse(online_features_response)
683+
initial_response = OnlineResponse(online_features_response, feature_types=feature_types)
683684
initial_response_arrow: Optional[pyarrow.Table] = None
684685
initial_response_dict: Optional[Dict[str, List[Any]]] = None
685686

‎sdk/python/feast/value_type.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ class ValueType(enum.Enum):
5252
MAP_LIST = 21
5353
PDF_BYTES = 22
5454
IMAGE_BYTES = 23
55+
UUID = 24
56+
TIME_UUID = 25
57+
UUID_LIST = 26
58+
TIME_UUID_LIST = 27
5559

5660

5761
ListType = Union[

0 commit comments

Comments
 (0)