Skip to content

Commit 7162e5c

Browse files
iammanoj807ntkathole
authored andcommitted
fix: Keep unmapped join keys in ibis point-in-time joins
get_historical_features_ibis, used by the DuckDB offline store and the contrib Oracle and MSSQL stores, took a projection's join_key_map as the full list of join keys. When with_join_key_map renamed only some keys of a feature view with several entities, the other keys were left out of the join and of entity_row_id. Each entity row then got the latest value for the renamed key alone, taken from any value of the other keys. Build the map from the feature view's entity columns instead and keep the name of every key the projection does not rename, as the Dask and SQL offline stores and the online read path already do. Signed-off-by: Manoj Kumar Thapa <iammanoj807@gmail.com>
1 parent 326b149 commit 7162e5c

2 files changed

Lines changed: 65 additions & 6 deletions

File tree

‎sdk/python/feast/infra/offline_stores/ibis.py‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,24 @@ def _entity_row_id(entity_table: Table, columns: Iterable[str]):
181181
return r
182182

183183

184+
def _join_key_map(feature_view: FeatureView) -> Dict[str, str]:
185+
"""Map each join key of the feature data to its column in the entity data.
186+
187+
A projection's join_key_map may rename only some of the join keys; the
188+
others keep their own name.
189+
"""
190+
return {
191+
column.name: feature_view.projection.join_key_map.get(column.name, column.name)
192+
for column in feature_view.entity_columns
193+
}
194+
195+
184196
def _generate_row_id(
185197
entity_table: Table, feature_views: List[FeatureView], event_timestamp_col
186198
) -> Table:
187199
all_entities = [event_timestamp_col]
188200
for fv in feature_views:
189-
if fv.projection.join_key_map:
190-
all_entities.extend(fv.projection.join_key_map.values())
191-
else:
192-
all_entities.extend([e.name for e in fv.entity_columns])
201+
all_entities.extend(_join_key_map(fv).values())
193202

194203
entity_table = entity_table.mutate(
195204
entity_row_id=_entity_row_id(entity_table, set(all_entities))
@@ -285,8 +294,7 @@ def read_fv(
285294
fv_table,
286295
timestamp_field,
287296
created_timestamp_column,
288-
feature_view.projection.join_key_map
289-
or {e.name: e.name for e in feature_view.entity_columns},
297+
_join_key_map(feature_view),
290298
feature_refs,
291299
feature_view.ttl,
292300
)

‎sdk/python/tests/unit/infra/offline_stores/test_ibis.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
import pyarrow.compute as pc
99
import pytest
1010

11+
from feast import Entity, FeatureView, Field, FileSource
1112
from feast.infra.offline_stores.ibis import (
13+
get_historical_features_ibis,
1214
point_in_time_join,
1315
pull_all_from_table_or_query_ibis,
1416
pull_latest_from_table_or_query_ibis,
1517
)
18+
from feast.types import Int64
19+
from feast.value_type import ValueType
1620

1721

1822
def pa_datetime(year, month, day):
@@ -278,3 +282,50 @@ def test_pull_filters_parquet_timestamps_of_any_precision(tmp_path, dtype, pull)
278282
).to_arrow()
279283

280284
assert sorted(table["driver_id"].to_pylist()) == [2, 3]
285+
286+
287+
def test_get_historical_features_keeps_join_keys_left_out_of_join_key_map():
288+
"""A join_key_map that renames only some join keys still joins on the others."""
289+
user = Entity(name="user", join_keys=["user_id"], value_type=ValueType.INT64)
290+
merchant = Entity(
291+
name="merchant", join_keys=["merchant_id"], value_type=ValueType.INT64
292+
)
293+
user_merchant = FeatureView(
294+
name="user_merchant",
295+
entities=[user, merchant],
296+
schema=[
297+
Field(name="user_id", dtype=Int64),
298+
Field(name="merchant_id", dtype=Int64),
299+
Field(name="spend", dtype=Int64),
300+
],
301+
source=FileSource(path="unused.parquet", timestamp_field="event_timestamp"),
302+
).with_join_key_map({"user_id": "sender_id"})
303+
features = pa.table(
304+
{
305+
"user_id": [1, 1],
306+
"merchant_id": [100, 200],
307+
"event_timestamp": [pa_datetime(2024, 1, 1), pa_datetime(2024, 1, 2)],
308+
"spend": [10, 99],
309+
}
310+
)
311+
entity_df = pd.DataFrame(
312+
{
313+
"sender_id": [1, 1],
314+
"merchant_id": [100, 200],
315+
"event_timestamp": [datetime(2024, 1, 3, tzinfo=timezone.utc)] * 2,
316+
}
317+
)
318+
319+
actual = get_historical_features_ibis(
320+
config=MagicMock(),
321+
feature_views=[user_merchant],
322+
feature_refs=["user_merchant:spend"],
323+
entity_df=entity_df,
324+
registry=MagicMock(),
325+
project="test",
326+
data_source_reader=lambda *_: ibis.memtable(features),
327+
data_source_writer=MagicMock(),
328+
).to_df()
329+
330+
spend = dict(zip(actual["merchant_id"], actual["spend"]))
331+
assert spend == {100: 10, 200: 99}

0 commit comments

Comments
 (0)