Skip to content

Commit cf013eb

Browse files
authored
fix: Keep features whose name ends in _y in ibis point-in-time joins (#7166)
* fix: Keep features whose name ends in _y in ibis point-in-time joins point_in_time_join, used by get_historical_features on the DuckDB offline store and the contrib Oracle and MSSQL stores, dropped every column whose name ends in "_y" after joining the feature data with the entity data. The drop was meant for the "_y" copies the join makes of entity columns that clash with feature-table columns, but it also removed a feature named like "pos_y" (or "y" with full_feature_names=True), and the select that followed failed with "Column 'pos_y' is not found in table". Timestamp and created columns ending in "_y" failed the same way, and a feature-table column named like one of the copies (e.g. "event_timestamp_y") failed with "Name collisions" even when it was not requested. Select entity_row_id and the timestamp, created and feature columns from their own tables instead, so that no column has to be dropped by name. Signed-off-by: Manoj Kumar Thapa <iammanoj807@gmail.com> * test: Cover names ending in _y in universal historical retrieval Request pos_x, pos_y and y through FeatureStore.get_historical_features from a source whose timestamp and created columns are ts_y and created_y, with and without full feature names, and check the latest eligible values. Signed-off-by: Manoj Kumar Thapa <iammanoj807@gmail.com> --------- Signed-off-by: Manoj Kumar Thapa <iammanoj807@gmail.com>
1 parent 4782ba3 commit cf013eb

3 files changed

Lines changed: 200 additions & 3 deletions

File tree

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,13 +517,21 @@ def point_in_time_join(
517517
>= entity_table[event_timestamp_col]
518518
)
519519

520+
# Select the columns used below from each side of the join, rather than
521+
# dropping the "_y" copies the join makes of clashing entity columns by
522+
# name: features, timestamps and created columns can end in "_y" too
523+
# (e.g. "pos_y").
524+
kept_columns = [timestamp_field]
525+
if created_timestamp_field:
526+
kept_columns.append(created_timestamp_field)
527+
kept_columns.extend(feature_refs)
520528
feature_table = feature_table.inner_join(
521529
entity_table, predicates, lname="", rname="{name}_y"
530+
).select(
531+
entity_table["entity_row_id"],
532+
*[feature_table[c] for c in dict.fromkeys(kept_columns)],
522533
)
523534

524-
cols_to_drop_y = [c for c in feature_table.columns if c.endswith("_y")]
525-
feature_table = feature_table.drop(*cols_to_drop_y)
526-
527535
feature_table = deduplicate(
528536
table=feature_table,
529537
group_by_cols=["entity_row_id"],

‎sdk/python/tests/integration/offline_store/test_universal_historical_retrieval.py‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from feast.types import Float32, Int32, String
1818
from feast.utils import _utc_now
1919
from tests.universal.feature_repos.repo_configuration import (
20+
Environment,
2021
construct_universal_feature_views,
2122
table_name_from_data_source,
2223
)
@@ -730,6 +731,109 @@ def test_historical_features_field_mapping(
730731
)
731732

732733

734+
@pytest.mark.integration
735+
@pytest.mark.universal_offline_stores
736+
@pytest.mark.parametrize("full_feature_names", [True, False], ids=lambda v: str(v))
737+
def test_historical_features_with_names_ending_in_y(
738+
environment: Environment, full_feature_names: bool
739+
) -> None:
740+
store = environment.feature_store
741+
742+
now = datetime.now().replace(microsecond=0, second=0, minute=0)
743+
tomorrow = now + timedelta(days=1)
744+
day_after_tomorrow = now + timedelta(days=2)
745+
746+
# Feature, timestamp and created column names ending in "_y", which the
747+
# point-in-time join also uses for its own suffixes.
748+
positions_df = pd.DataFrame(
749+
data=[
750+
{
751+
"driver_id": 1001,
752+
"pos_x": 1.5,
753+
"pos_y": 7.5,
754+
"y": 1,
755+
"ts_y": now,
756+
"created_y": now,
757+
},
758+
{
759+
"driver_id": 1001,
760+
"pos_x": 2.5,
761+
"pos_y": 8.5,
762+
"y": 2,
763+
"ts_y": tomorrow,
764+
"created_y": tomorrow,
765+
},
766+
{
767+
"driver_id": 1002,
768+
"pos_x": 3.5,
769+
"pos_y": 9.5,
770+
"y": 3,
771+
"ts_y": now,
772+
"created_y": now,
773+
},
774+
]
775+
)
776+
entity_df = pd.DataFrame(
777+
data=[
778+
{
779+
"driver_id": 1001,
780+
"event_timestamp": now + timedelta(hours=12),
781+
"request_id": 1,
782+
},
783+
{
784+
"driver_id": 1001,
785+
"event_timestamp": day_after_tomorrow,
786+
"request_id": 2,
787+
},
788+
{
789+
"driver_id": 1002,
790+
"event_timestamp": day_after_tomorrow,
791+
"request_id": 3,
792+
},
793+
]
794+
)
795+
796+
positions_source = environment.data_source_creator.create_data_source(
797+
df=positions_df,
798+
destination_name=f"test_driver_positions_{int(time.time_ns())}_{random.randint(1000, 9999)}",
799+
timestamp_field="ts_y",
800+
created_timestamp_column="created_y",
801+
)
802+
driver = Entity(name="driver", join_keys=["driver_id"])
803+
positions_fv = FeatureView(
804+
name="driver_positions",
805+
entities=[driver],
806+
schema=[
807+
Field(name="pos_x", dtype=Float32),
808+
Field(name="pos_y", dtype=Float32),
809+
Field(name="y", dtype=Int32),
810+
],
811+
source=positions_source,
812+
)
813+
store.apply([driver, positions_fv])
814+
815+
actual_df = store.get_historical_features(
816+
entity_df=entity_df,
817+
features=[
818+
"driver_positions:pos_x",
819+
"driver_positions:pos_y",
820+
"driver_positions:y",
821+
],
822+
full_feature_names=full_feature_names,
823+
).to_df()
824+
825+
prefix = "driver_positions__" if full_feature_names else ""
826+
expected_df = pd.DataFrame(
827+
data=[
828+
{"request_id": 1, "pos_x": 1.5, "pos_y": 7.5, "y": 1},
829+
{"request_id": 2, "pos_x": 2.5, "pos_y": 8.5, "y": 2},
830+
{"request_id": 3, "pos_x": 3.5, "pos_y": 9.5, "y": 3},
831+
]
832+
).rename(columns={name: f"{prefix}{name}" for name in ["pos_x", "pos_y", "y"]})
833+
834+
validate_dataframes(expected_df, actual_df, sort_by=["request_id"])
835+
836+
733837
@pytest.mark.integration
734838
@pytest.mark.universal_offline_stores(only=["file"])
735839
def test_historical_features_non_entity_retrieval(environment):

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,91 @@ def test_point_in_time_join_keeps_entity_rows_apart():
239239
assert rows == {"1": (1.0, 110.0), "11": (11.0, 10.0), "7": (7.0, None)}
240240

241241

242+
def test_point_in_time_join_keeps_features_whose_name_ends_in_y():
243+
"""A feature named like "pos_y" is returned, not dropped with the "_y"
244+
copies of the entity columns that the join renames."""
245+
entity_table = pa.table(
246+
{
247+
"driver_id": [1, 2],
248+
"event_timestamp": [pa_datetime(2024, 1, 3)] * 2,
249+
}
250+
)
251+
positions = pa.table(
252+
{
253+
"driver_id": [1, 1, 2],
254+
"event_timestamp": [
255+
pa_datetime(2024, 1, 1),
256+
pa_datetime(2024, 1, 2),
257+
pa_datetime(2024, 1, 1),
258+
],
259+
"created": [
260+
pa_datetime(2024, 1, 1),
261+
pa_datetime(2024, 1, 2),
262+
pa_datetime(2024, 1, 1),
263+
],
264+
"pos_x": [1.0, 2.0, 3.0],
265+
"pos_y": [10.0, 20.0, 30.0],
266+
}
267+
)
268+
269+
actual = point_in_time_join(
270+
ibis.memtable(entity_table),
271+
feature_tables=[
272+
(
273+
ibis.memtable(positions),
274+
"event_timestamp",
275+
"created",
276+
{"driver_id": "driver_id"},
277+
["pos_x", "pos_y"],
278+
timedelta(days=10),
279+
)
280+
],
281+
).to_pyarrow()
282+
283+
rows = {
284+
row["driver_id"]: (row["pos_x"], row["pos_y"]) for row in actual.to_pylist()
285+
}
286+
assert rows == {1: (2.0, 20.0), 2: (3.0, 30.0)}
287+
288+
289+
def test_point_in_time_join_allows_columns_named_like_the_y_copies():
290+
"""Feature-table columns named like the "_y" copies the join makes of
291+
clashing entity columns ("driver_id_y", "event_timestamp_y") do not clash
292+
with them, whether they are requested or not."""
293+
entity_table = pa.table(
294+
{
295+
"driver_id": [1, 2],
296+
"event_timestamp": [pa_datetime(2024, 1, 3)] * 2,
297+
}
298+
)
299+
features = pa.table(
300+
{
301+
"driver_id": [1, 2],
302+
"event_timestamp": [pa_datetime(2024, 1, 1)] * 2,
303+
"created": [pa_datetime(2024, 1, 1)] * 2,
304+
"driver_id_y": [10.0, 20.0],
305+
"event_timestamp_y": [pa_datetime(2023, 1, 1)] * 2,
306+
}
307+
)
308+
309+
actual = point_in_time_join(
310+
ibis.memtable(entity_table),
311+
feature_tables=[
312+
(
313+
ibis.memtable(features),
314+
"event_timestamp",
315+
"created",
316+
{"driver_id": "driver_id"},
317+
["driver_id_y"],
318+
timedelta(days=10),
319+
)
320+
],
321+
).to_pyarrow()
322+
323+
rows = {row["driver_id"]: row["driver_id_y"] for row in actual.to_pylist()}
324+
assert rows == {1: 10.0, 2: 20.0}
325+
326+
242327
@pytest.mark.parametrize(
243328
"dtype",
244329
["datetime64[ns, UTC]", "datetime64[us, UTC]", "datetime64[ns]", "datetime64[us]"],

0 commit comments

Comments
 (0)