Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions sdk/python/feast/on_demand_feature_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,13 +1092,16 @@ def _postprocess_arrow_table(
def transform_dict(
self,
feature_dict: dict[str, Any], # type: ignore
full_feature_names: bool = False,
) -> dict[str, Any]:
"""
Transform a dictionary of features using the configured transformation.
Handles both singleton and batch transformations.

Args:
feature_dict: Dictionary containing input features
full_feature_names: Name each output feature "<view>__<feature>",
as :meth:`transform_arrow` does, instead of by its bare name.

Returns:
Dictionary with transformed features
Expand All @@ -1122,6 +1125,16 @@ def transform_dict(
if feature_name in output_dict:
del output_dict[feature_name]

# The UDF names its outputs as it likes; rename them to match what the
# caller asked for, like _postprocess_arrow_table does.
for feature in self.features:
short_name = feature.name
long_name = self._get_projected_feature_name(short_name)
if full_feature_names and short_name in output_dict:
output_dict[long_name] = output_dict.pop(short_name)
elif not full_feature_names and long_name in output_dict:
output_dict[short_name] = output_dict.pop(long_name)

return output_dict

def _preprocess_feature_dict(
Expand Down
6 changes: 5 additions & 1 deletion sdk/python/feast/type_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,11 @@ def python_values_to_proto_values(
value_type = feature_type
sample = next(filter(_non_empty_value, values), None) # first not empty value
if sample is not None and feature_type == ValueType.UNKNOWN:
if isinstance(sample, (list, np.ndarray)):
if isinstance(sample, ProtoValue):
# Already converted, e.g. request data from a gRPC RepeatedValue:
# the field it sets names the type, and the values pass through.
value_type = _proto_value_to_value_type(sample)
Comment thread
patelchaitany marked this conversation as resolved.
Resolved
elif isinstance(sample, (list, np.ndarray)):
value_type = (
feature_type
if len(sample) == 0
Expand Down
11 changes: 9 additions & 2 deletions sdk/python/feast/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1012,7 +1012,7 @@ def _is_metrics_active():
if k not in undeclared_columns
}
transformed_features_dict: Dict[str, List[Any]] = odfv.transform_dict(
odfv_input_dict
odfv_input_dict, full_feature_names
Comment thread
patelchaitany marked this conversation as resolved.
Resolved
)
elif odfv.mode in {"pandas", "substrait"}:
if initial_response_arrow is None:
Expand Down Expand Up @@ -1054,7 +1054,14 @@ def _is_metrics_active():
selected_subset = [f for f in transformed_columns if f in _feature_refs]

proto_values = []
schema_dict = {k.name: k.dtype for k in odfv.schema}
schema_dict = {
(
odfv._get_projected_feature_name(k.name)
if full_feature_names
else k.name
): k.dtype
for k in odfv.schema
}
for selected_feature in selected_subset:
feature_vector = transformed_features[selected_feature]
selected_feature_type = schema_dict.get(selected_feature, None)
Expand Down
25 changes: 25 additions & 0 deletions sdk/python/tests/unit/test_on_demand_python_transformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from feast.infra.online_stores.sqlite import SqliteOnlineStoreConfig
from feast.nlp_test_data import create_document_chunks_df
from feast.on_demand_feature_view import on_demand_feature_view
from feast.protos.feast.types.Value_pb2 import Value as ProtoValue
from feast.types import (
Array,
Bool,
Expand Down Expand Up @@ -345,6 +346,30 @@ def test_python_pandas_parity(self):
+ online_python_response["acc_rate"][0]
)

def test_python_view_with_full_feature_names(self):
request_source = RequestSource(
name="match_request", schema=[Field(name="match_count", dtype=Int64)]
)

@on_demand_feature_view(
sources=[request_source],
schema=[Field(name="matches", dtype=Array(Float32))],
mode="python",
)
def match_view(inputs: dict[str, Any]) -> dict[str, Any]:
return {"matches": [[1.5] * n for n in inputs["match_count"]]}

self.store.apply([request_source, match_view])
response = self.store.get_online_features(
features=["match_view:matches"],
# The Python gRPC server passes request data as proto Values.
entity_rows=[{"match_count": ProtoValue(int64_val=0)}],
full_feature_names=True,
).to_dict()

# An empty list converts only with its declared type, found by the long name.
assert response["match_view__matches"] == [[]]

def test_python_docs_demo(self):
entity_rows = [
{
Expand Down
Loading