Skip to content
This repository was archived by the owner on Nov 10, 2022. It is now read-only.

Commit 4c32d75

Browse files
authored
Refactor OnlineResponse.to_dict() (feast-dev#2196)
Signed-off-by: Judah Rand <[email protected]>
1 parent 14048fd commit 4c32d75

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

sdk/python/feast/online_response.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from feast.type_map import (
2727
_proto_value_to_value_type,
2828
_python_value_to_proto_value,
29-
feast_value_type_to_python_type,
3029
python_values_to_feast_value_type,
3130
)
3231
from feast.value_type import ValueType
@@ -63,14 +62,34 @@ def to_dict(self) -> Dict[str, Any]:
6362
"""
6463
Converts GetOnlineFeaturesResponse features into a dictionary form.
6564
"""
65+
# Status for every Feature should be present in every record.
6666
features_dict: Dict[str, List[Any]] = {
67-
k: list() for row in self.field_values for k, _ in row.statuses.items()
67+
k: list() for k in self.field_values[0].statuses.keys()
6868
}
69-
70-
for row in self.field_values:
71-
for feature in features_dict.keys():
72-
native_type_value = feast_value_type_to_python_type(row.fields[feature])
73-
features_dict[feature].append(native_type_value)
69+
rows = [record.fields for record in self.field_values]
70+
71+
# Find the first non-null instance of each Feature to determine
72+
# which ValueType.
73+
val_types = {k: None for k in features_dict.keys()}
74+
for feature in features_dict.keys():
75+
for row in rows:
76+
try:
77+
val_types[feature] = row[feature].WhichOneof("val")
78+
except KeyError:
79+
continue
80+
if val_types[feature] is not None:
81+
break
82+
83+
# Now we know what attribute to fetch.
84+
for feature, val_type in val_types.items():
85+
if val_type is None:
86+
features_dict[feature] = [None] * len(rows)
87+
else:
88+
for row in rows:
89+
val = getattr(row[feature], val_type)
90+
if "_list_" in val_type:
91+
val = list(val.val)
92+
features_dict[feature].append(val)
7493

7594
return features_dict
7695

0 commit comments

Comments
 (0)