22from unittest .mock import MagicMock
33
44import pandas as pd
5+ import pytest
56
67from feast import (
78 Aggregation ,
1213 RepoConfig ,
1314 ValueType ,
1415)
16+ from feast .feature_view import DUMMY_ENTITY
1517from feast .infra .common .retrieval_task import HistoricalRetrievalTask
1618from feast .infra .compute_engines .base import ComputeEngine
1719from feast .infra .compute_engines .local .compute import (
1820 LocalComputeEngine ,
1921 LocalComputeEngineConfig ,
2022)
23+ from feast .infra .compute_engines .utils import ENTITY_ROW_ID
2124from feast .infra .offline_stores .dask import DaskOfflineStore , DaskOfflineStoreConfig
2225from feast .infra .online_stores .sqlite import SqliteOnlineStoreConfig
2326from feast .types import Float32 , Int64
@@ -111,7 +114,11 @@ def _source_view(self, tmp_path):
111114 offline = False ,
112115 )
113116
114- def _get_historical_features (self , tmp_path , feature_view ):
117+ def _get_historical_features (
118+ self , tmp_path , feature_view , entity_df = None , entity = None , backend = "pandas"
119+ ):
120+ if backend == "polars" :
121+ pytest .importorskip ("polars" )
115122 repo_config = RepoConfig (
116123 project = "test_local_engine" ,
117124 registry = str (tmp_path / "registry.db" ),
@@ -124,21 +131,23 @@ def _get_historical_features(self, tmp_path, feature_view):
124131 repo_config = repo_config ,
125132 offline_store = DaskOfflineStore (),
126133 online_store = MagicMock (),
134+ backend = backend ,
127135 )
128136
129137 # Driver 1 is requested twice: before and after its second feature
130138 # row. Driver 2 is not requested at all.
131- entity_df = pd .DataFrame (
132- {
133- "driver_id" : [1 , 1 ],
134- "event_timestamp" : [
135- self .t0 + timedelta (hours = 1 ),
136- self .t0 + timedelta (hours = 3 ),
137- ],
138- }
139- )
139+ if entity_df is None :
140+ entity_df = pd .DataFrame (
141+ {
142+ "driver_id" : [1 , 1 ],
143+ "event_timestamp" : [
144+ self .t0 + timedelta (hours = 1 ),
145+ self .t0 + timedelta (hours = 3 ),
146+ ],
147+ }
148+ )
140149 registry = MagicMock ()
141- registry .get_entity .return_value = self .driver
150+ registry .get_entity .return_value = entity or self .driver
142151 task = HistoricalRetrievalTask (
143152 project = "test_local_engine" ,
144153 entity_df = entity_df ,
@@ -153,6 +162,7 @@ def test_get_historical_features_is_point_in_time_per_entity_row(self, tmp_path)
153162
154163 assert sorted (df ["driver_id" ].to_list ()) == [1 , 1 ]
155164 assert sorted (round (v , 6 ) for v in df ["conv_rate" ].to_list ()) == [0.1 , 0.9 ]
165+ assert ENTITY_ROW_ID not in df .columns
156166
157167 def test_get_historical_features_for_derived_view (self , tmp_path ):
158168 derived = BatchFeatureView (
@@ -196,3 +206,87 @@ def test_get_historical_features_aggregates_per_entity_row(self, tmp_path):
196206
197207 assert sorted (df ["driver_id" ].to_list ()) == [1 , 1 ]
198208 assert sorted (round (v , 6 ) for v in df ["sum_conv_rate" ].to_list ()) == [0.1 , 1.0 ]
209+
210+ def _same_request_twice (self ):
211+ """Two entity rows with the same driver and timestamp, told apart by a label."""
212+ return pd .DataFrame (
213+ {
214+ "driver_id" : [1 , 1 ],
215+ "event_timestamp" : [self .t0 + timedelta (hours = 1 )] * 2 ,
216+ "label" : ["a" , "b" ],
217+ }
218+ )
219+
220+ @pytest .mark .parametrize ("backend" , ["pandas" , "polars" ])
221+ def test_get_historical_features_keeps_entity_rows_with_the_same_key_and_time (
222+ self , tmp_path , backend
223+ ):
224+ df = self ._get_historical_features (
225+ tmp_path ,
226+ self ._source_view (tmp_path ),
227+ self ._same_request_twice (),
228+ backend = backend ,
229+ )
230+
231+ assert sorted (df ["label" ].to_list ()) == ["a" , "b" ]
232+ assert [round (v , 6 ) for v in df ["conv_rate" ].to_list ()] == [0.1 , 0.1 ]
233+
234+ @pytest .mark .parametrize ("backend" , ["pandas" , "polars" ])
235+ def test_get_historical_features_aggregates_entity_rows_with_the_same_key_and_time (
236+ self , tmp_path , backend
237+ ):
238+ driver_id = Entity (
239+ name = "driver_id" , join_keys = ["driver_id" ], value_type = ValueType .INT64
240+ )
241+ aggregated = BatchFeatureView (
242+ name = "driver_stats_sum" ,
243+ entities = [driver_id ],
244+ mode = "pandas" ,
245+ schema = self .schema ,
246+ source = self ._source_view (tmp_path ).batch_source ,
247+ aggregations = [Aggregation (column = "conv_rate" , function = "sum" )],
248+ ttl = timedelta (days = 1 ),
249+ online = False ,
250+ offline = False ,
251+ )
252+
253+ df = self ._get_historical_features (
254+ tmp_path , aggregated , self ._same_request_twice (), backend = backend
255+ )
256+
257+ assert [round (v , 6 ) for v in df ["sum_conv_rate" ].to_list ()] == [0.1 , 0.1 ]
258+ assert ENTITY_ROW_ID not in df .columns
259+
260+ @pytest .mark .parametrize ("backend" , ["pandas" , "polars" ])
261+ def test_get_historical_features_for_view_without_entities (self , tmp_path , backend ):
262+ source_path = str (tmp_path / "global_stats.parquet" )
263+ pd .DataFrame (
264+ {
265+ "conv_rate" : [0.1 , 0.9 ],
266+ "event_timestamp" : [self .t0 , self .t0 + timedelta (hours = 2 )],
267+ }
268+ ).to_parquet (source_path )
269+ global_view = BatchFeatureView (
270+ name = "global_stats" ,
271+ entities = [],
272+ mode = "pandas" ,
273+ schema = [Field (name = "conv_rate" , dtype = Float32 )],
274+ source = FileSource (path = source_path , timestamp_field = "event_timestamp" ),
275+ ttl = timedelta (days = 1 ),
276+ online = False ,
277+ offline = False ,
278+ )
279+ entity_df = pd .DataFrame (
280+ {
281+ "event_timestamp" : [
282+ self .t0 + timedelta (hours = 1 ),
283+ self .t0 + timedelta (hours = 3 ),
284+ ]
285+ }
286+ )
287+
288+ df = self ._get_historical_features (
289+ tmp_path , global_view , entity_df , entity = DUMMY_ENTITY , backend = backend
290+ )
291+
292+ assert sorted (round (v , 6 ) for v in df ["conv_rate" ].to_list ()) == [0.1 , 0.9 ]
0 commit comments