diff --git a/sdk/python/feast/infra/offline_stores/ibis.py b/sdk/python/feast/infra/offline_stores/ibis.py index 05da1cb316f..654d2117b97 100644 --- a/sdk/python/feast/infra/offline_stores/ibis.py +++ b/sdk/python/feast/infra/offline_stores/ibis.py @@ -517,13 +517,21 @@ def point_in_time_join( >= entity_table[event_timestamp_col] ) + # Select the columns used below from each side of the join, rather than + # dropping the "_y" copies the join makes of clashing entity columns by + # name: features, timestamps and created columns can end in "_y" too + # (e.g. "pos_y"). + kept_columns = [timestamp_field] + if created_timestamp_field: + kept_columns.append(created_timestamp_field) + kept_columns.extend(feature_refs) feature_table = feature_table.inner_join( entity_table, predicates, lname="", rname="{name}_y" + ).select( + entity_table["entity_row_id"], + *[feature_table[c] for c in dict.fromkeys(kept_columns)], ) - cols_to_drop_y = [c for c in feature_table.columns if c.endswith("_y")] - feature_table = feature_table.drop(*cols_to_drop_y) - feature_table = deduplicate( table=feature_table, group_by_cols=["entity_row_id"], diff --git a/sdk/python/tests/integration/offline_store/test_universal_historical_retrieval.py b/sdk/python/tests/integration/offline_store/test_universal_historical_retrieval.py index 6a17fc47bbb..90e9e6d4ac8 100644 --- a/sdk/python/tests/integration/offline_store/test_universal_historical_retrieval.py +++ b/sdk/python/tests/integration/offline_store/test_universal_historical_retrieval.py @@ -17,6 +17,7 @@ from feast.types import Float32, Int32, String from feast.utils import _utc_now from tests.universal.feature_repos.repo_configuration import ( + Environment, construct_universal_feature_views, table_name_from_data_source, ) @@ -730,6 +731,109 @@ def test_historical_features_field_mapping( ) +@pytest.mark.integration +@pytest.mark.universal_offline_stores +@pytest.mark.parametrize("full_feature_names", [True, False], ids=lambda v: str(v)) +def test_historical_features_with_names_ending_in_y( + environment: Environment, full_feature_names: bool +) -> None: + store = environment.feature_store + + now = datetime.now().replace(microsecond=0, second=0, minute=0) + tomorrow = now + timedelta(days=1) + day_after_tomorrow = now + timedelta(days=2) + + # Feature, timestamp and created column names ending in "_y", which the + # point-in-time join also uses for its own suffixes. + positions_df = pd.DataFrame( + data=[ + { + "driver_id": 1001, + "pos_x": 1.5, + "pos_y": 7.5, + "y": 1, + "ts_y": now, + "created_y": now, + }, + { + "driver_id": 1001, + "pos_x": 2.5, + "pos_y": 8.5, + "y": 2, + "ts_y": tomorrow, + "created_y": tomorrow, + }, + { + "driver_id": 1002, + "pos_x": 3.5, + "pos_y": 9.5, + "y": 3, + "ts_y": now, + "created_y": now, + }, + ] + ) + entity_df = pd.DataFrame( + data=[ + { + "driver_id": 1001, + "event_timestamp": now + timedelta(hours=12), + "request_id": 1, + }, + { + "driver_id": 1001, + "event_timestamp": day_after_tomorrow, + "request_id": 2, + }, + { + "driver_id": 1002, + "event_timestamp": day_after_tomorrow, + "request_id": 3, + }, + ] + ) + + positions_source = environment.data_source_creator.create_data_source( + df=positions_df, + destination_name=f"test_driver_positions_{int(time.time_ns())}_{random.randint(1000, 9999)}", + timestamp_field="ts_y", + created_timestamp_column="created_y", + ) + driver = Entity(name="driver", join_keys=["driver_id"]) + positions_fv = FeatureView( + name="driver_positions", + entities=[driver], + schema=[ + Field(name="pos_x", dtype=Float32), + Field(name="pos_y", dtype=Float32), + Field(name="y", dtype=Int32), + ], + source=positions_source, + ) + store.apply([driver, positions_fv]) + + actual_df = store.get_historical_features( + entity_df=entity_df, + features=[ + "driver_positions:pos_x", + "driver_positions:pos_y", + "driver_positions:y", + ], + full_feature_names=full_feature_names, + ).to_df() + + prefix = "driver_positions__" if full_feature_names else "" + expected_df = pd.DataFrame( + data=[ + {"request_id": 1, "pos_x": 1.5, "pos_y": 7.5, "y": 1}, + {"request_id": 2, "pos_x": 2.5, "pos_y": 8.5, "y": 2}, + {"request_id": 3, "pos_x": 3.5, "pos_y": 9.5, "y": 3}, + ] + ).rename(columns={name: f"{prefix}{name}" for name in ["pos_x", "pos_y", "y"]}) + + validate_dataframes(expected_df, actual_df, sort_by=["request_id"]) + + @pytest.mark.integration @pytest.mark.universal_offline_stores(only=["file"]) def test_historical_features_non_entity_retrieval(environment): diff --git a/sdk/python/tests/unit/infra/offline_stores/test_ibis.py b/sdk/python/tests/unit/infra/offline_stores/test_ibis.py index ddfa73fed41..54d68eaea26 100644 --- a/sdk/python/tests/unit/infra/offline_stores/test_ibis.py +++ b/sdk/python/tests/unit/infra/offline_stores/test_ibis.py @@ -239,6 +239,91 @@ def test_point_in_time_join_keeps_entity_rows_apart(): assert rows == {"1": (1.0, 110.0), "11": (11.0, 10.0), "7": (7.0, None)} +def test_point_in_time_join_keeps_features_whose_name_ends_in_y(): + """A feature named like "pos_y" is returned, not dropped with the "_y" + copies of the entity columns that the join renames.""" + entity_table = pa.table( + { + "driver_id": [1, 2], + "event_timestamp": [pa_datetime(2024, 1, 3)] * 2, + } + ) + positions = pa.table( + { + "driver_id": [1, 1, 2], + "event_timestamp": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 2), + pa_datetime(2024, 1, 1), + ], + "created": [ + pa_datetime(2024, 1, 1), + pa_datetime(2024, 1, 2), + pa_datetime(2024, 1, 1), + ], + "pos_x": [1.0, 2.0, 3.0], + "pos_y": [10.0, 20.0, 30.0], + } + ) + + actual = point_in_time_join( + ibis.memtable(entity_table), + feature_tables=[ + ( + ibis.memtable(positions), + "event_timestamp", + "created", + {"driver_id": "driver_id"}, + ["pos_x", "pos_y"], + timedelta(days=10), + ) + ], + ).to_pyarrow() + + rows = { + row["driver_id"]: (row["pos_x"], row["pos_y"]) for row in actual.to_pylist() + } + assert rows == {1: (2.0, 20.0), 2: (3.0, 30.0)} + + +def test_point_in_time_join_allows_columns_named_like_the_y_copies(): + """Feature-table columns named like the "_y" copies the join makes of + clashing entity columns ("driver_id_y", "event_timestamp_y") do not clash + with them, whether they are requested or not.""" + entity_table = pa.table( + { + "driver_id": [1, 2], + "event_timestamp": [pa_datetime(2024, 1, 3)] * 2, + } + ) + features = pa.table( + { + "driver_id": [1, 2], + "event_timestamp": [pa_datetime(2024, 1, 1)] * 2, + "created": [pa_datetime(2024, 1, 1)] * 2, + "driver_id_y": [10.0, 20.0], + "event_timestamp_y": [pa_datetime(2023, 1, 1)] * 2, + } + ) + + actual = point_in_time_join( + ibis.memtable(entity_table), + feature_tables=[ + ( + ibis.memtable(features), + "event_timestamp", + "created", + {"driver_id": "driver_id"}, + ["driver_id_y"], + timedelta(days=10), + ) + ], + ).to_pyarrow() + + rows = {row["driver_id"]: row["driver_id_y"] for row in actual.to_pylist()} + assert rows == {1: 10.0, 2: 20.0} + + @pytest.mark.parametrize( "dtype", ["datetime64[ns, UTC]", "datetime64[us, UTC]", "datetime64[ns]", "datetime64[us]"],