diff --git a/sdk/python/feast/on_demand_feature_view.py b/sdk/python/feast/on_demand_feature_view.py index 3fff495c204..5903a492052 100644 --- a/sdk/python/feast/on_demand_feature_view.py +++ b/sdk/python/feast/on_demand_feature_view.py @@ -1092,6 +1092,7 @@ 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. @@ -1099,6 +1100,8 @@ def transform_dict( Args: feature_dict: Dictionary containing input features + full_feature_names: Name each output feature "__", + as :meth:`transform_arrow` does, instead of by its bare name. Returns: Dictionary with transformed features @@ -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( diff --git a/sdk/python/feast/type_map.py b/sdk/python/feast/type_map.py index 53680908eca..b0be39e06e4 100644 --- a/sdk/python/feast/type_map.py +++ b/sdk/python/feast/type_map.py @@ -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) + elif isinstance(sample, (list, np.ndarray)): value_type = ( feature_type if len(sample) == 0 diff --git a/sdk/python/feast/utils.py b/sdk/python/feast/utils.py index e09577de110..60d3ac7efc9 100644 --- a/sdk/python/feast/utils.py +++ b/sdk/python/feast/utils.py @@ -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 ) elif odfv.mode in {"pandas", "substrait"}: if initial_response_arrow is None: @@ -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) diff --git a/sdk/python/tests/unit/test_on_demand_python_transformation.py b/sdk/python/tests/unit/test_on_demand_python_transformation.py index 522c0c53a39..2175a0efa3d 100644 --- a/sdk/python/tests/unit/test_on_demand_python_transformation.py +++ b/sdk/python/tests/unit/test_on_demand_python_transformation.py @@ -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, @@ -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 = [ {