|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | +import pytest |
| 8 | +from pandas.testing import assert_frame_equal |
| 9 | + |
| 10 | +from feast import Entity, FeatureStore, FeatureView, Field, FileSource, RepoConfig |
| 11 | +from feast.types import Float64, Int64, ValueType |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize("full_feature_names", [False, True]) |
| 15 | +@pytest.mark.parametrize("filter_by_created_timestamp", [False, True]) |
| 16 | +@pytest.mark.parametrize( |
| 17 | + "created_column,include_request_created_column", |
| 18 | + [("created_ts", False), ("created_ts", True), ("event_timestamp", False)], |
| 19 | + ids=["no-collision", "request-data-collision", "request-timestamp-collision"], |
| 20 | +) |
| 21 | +def test_historical_retrieval_keeps_source_created_timestamp_separate( |
| 22 | + tmp_path: Path, |
| 23 | + created_column: str, |
| 24 | + include_request_created_column: bool, |
| 25 | + filter_by_created_timestamp: bool, |
| 26 | + full_feature_names: bool, |
| 27 | +) -> None: |
| 28 | + source_path = tmp_path / "features.parquet" |
| 29 | + pd.DataFrame( |
| 30 | + { |
| 31 | + "driver_id": [1, 1], |
| 32 | + "feature_timestamp": pd.to_datetime( |
| 33 | + ["2025-01-01T10:00:00Z", "2025-01-01T10:00:00Z"] |
| 34 | + ), |
| 35 | + created_column: pd.to_datetime( |
| 36 | + ["2025-01-01T12:00:00Z", "2025-01-03T00:00:00Z"] |
| 37 | + ), |
| 38 | + "score": [0.4, 0.9], |
| 39 | + } |
| 40 | + ).to_parquet(source_path, index=False) |
| 41 | + entity = Entity(name="driver", join_keys=["driver_id"], value_type=ValueType.INT64) |
| 42 | + feature_view = FeatureView( |
| 43 | + name="driver_stats", |
| 44 | + entities=[entity], |
| 45 | + schema=[ |
| 46 | + Field(name="driver_id", dtype=Int64), |
| 47 | + Field(name="score", dtype=Float64), |
| 48 | + ], |
| 49 | + source=FileSource( |
| 50 | + path=str(source_path), |
| 51 | + timestamp_field="feature_timestamp", |
| 52 | + created_timestamp_column=created_column, |
| 53 | + ), |
| 54 | + ttl=timedelta(days=7), |
| 55 | + ) |
| 56 | + store = FeatureStore( |
| 57 | + config=RepoConfig( |
| 58 | + project="created_collision", |
| 59 | + provider="local", |
| 60 | + registry=str(tmp_path / "registry.db"), |
| 61 | + online_store={"type": "sqlite", "path": str(tmp_path / "online.db")}, |
| 62 | + offline_store={"type": "file"}, |
| 63 | + repo_path=tmp_path, |
| 64 | + ) |
| 65 | + ) |
| 66 | + store.apply([entity, feature_view]) |
| 67 | + requests = pd.DataFrame( |
| 68 | + { |
| 69 | + "driver_id": [1], |
| 70 | + "event_timestamp": pd.to_datetime(["2025-01-02T00:00:00Z"]), |
| 71 | + # Existing request fields must not be reused as temporary source columns. |
| 72 | + f"__{created_column}": ["request label"], |
| 73 | + f"___{created_column}": ["another label"], |
| 74 | + } |
| 75 | + ) |
| 76 | + if include_request_created_column: |
| 77 | + requests[created_column] = pd.to_datetime(["2025-01-01T00:00:00Z"]) |
| 78 | + original = requests.copy(deep=True) |
| 79 | + |
| 80 | + result = store.get_historical_features( |
| 81 | + entity_df=requests, |
| 82 | + features=["driver_stats:score"], |
| 83 | + filter_by_created_timestamp=filter_by_created_timestamp, |
| 84 | + full_feature_names=full_feature_names, |
| 85 | + ).to_df() |
| 86 | + |
| 87 | + feature_column = "driver_stats__score" if full_feature_names else "score" |
| 88 | + expected_score = 0.4 if filter_by_created_timestamp else 0.9 |
| 89 | + assert result[feature_column].tolist() == [expected_score] |
| 90 | + assert set(result.columns) == set(requests.columns) | {feature_column} |
| 91 | + assert_frame_equal(result[requests.columns], original) |
| 92 | + assert_frame_equal(requests, original) |
| 93 | + |
| 94 | + |
| 95 | +@pytest.mark.parametrize("filter_by_created_timestamp", [False, True]) |
| 96 | +@pytest.mark.parametrize("include_second_view", [False, True]) |
| 97 | +def test_created_timestamp_alias_does_not_escape_feature_view( |
| 98 | + tmp_path: Path, |
| 99 | + filter_by_created_timestamp: bool, |
| 100 | + include_second_view: bool, |
| 101 | +) -> None: |
| 102 | + entity = Entity(name="driver", join_keys=["driver_id"], value_type=ValueType.INT64) |
| 103 | + first_path = tmp_path / "first.parquet" |
| 104 | + pd.DataFrame( |
| 105 | + { |
| 106 | + "driver_id": [1], |
| 107 | + "feature_timestamp": pd.to_datetime(["2025-01-01T10:00:00Z"]), |
| 108 | + "created_ts": pd.to_datetime(["2025-01-01T12:00:00Z"]), |
| 109 | + "__created_ts": [0.4], |
| 110 | + } |
| 111 | + ).to_parquet(first_path, index=False) |
| 112 | + first = FeatureView( |
| 113 | + name="first", |
| 114 | + entities=[entity], |
| 115 | + schema=[Field(name="__created_ts", dtype=Float64)], |
| 116 | + source=FileSource( |
| 117 | + path=str(first_path), |
| 118 | + timestamp_field="feature_timestamp", |
| 119 | + created_timestamp_column="created_ts", |
| 120 | + ), |
| 121 | + ttl=timedelta(days=7), |
| 122 | + ) |
| 123 | + store = FeatureStore( |
| 124 | + config=RepoConfig( |
| 125 | + project="alias_collision", |
| 126 | + provider="local", |
| 127 | + registry=str(tmp_path / "registry.db"), |
| 128 | + online_store={"type": "sqlite", "path": str(tmp_path / "online.db")}, |
| 129 | + offline_store={"type": "file"}, |
| 130 | + repo_path=tmp_path, |
| 131 | + ) |
| 132 | + ) |
| 133 | + store.apply([entity, first]) |
| 134 | + features = ["first:__created_ts"] |
| 135 | + expected_features = {"first____created_ts"} |
| 136 | + if include_second_view: |
| 137 | + second_path = tmp_path / "second.parquet" |
| 138 | + pd.DataFrame( |
| 139 | + { |
| 140 | + "driver_id": [1, 1], |
| 141 | + "__created_ts": pd.to_datetime( |
| 142 | + ["2025-01-01T10:00:00Z", "2025-01-03T10:00:00Z"] |
| 143 | + ), |
| 144 | + "score": [0.2, 0.8], |
| 145 | + } |
| 146 | + ).to_parquet(second_path, index=False) |
| 147 | + second = FeatureView( |
| 148 | + name="second", |
| 149 | + entities=[entity], |
| 150 | + schema=[Field(name="score", dtype=Float64)], |
| 151 | + source=FileSource(path=str(second_path), timestamp_field="__created_ts"), |
| 152 | + ttl=timedelta(days=7), |
| 153 | + ) |
| 154 | + store.apply([second]) |
| 155 | + features.append("second:score") |
| 156 | + expected_features.add("second__score") |
| 157 | + requests = pd.DataFrame( |
| 158 | + { |
| 159 | + "driver_id": [1], |
| 160 | + "event_timestamp": pd.to_datetime(["2025-01-02T00:00:00Z"]), |
| 161 | + "created_ts": pd.to_datetime(["2025-01-01T00:00:00Z"]), |
| 162 | + } |
| 163 | + ) |
| 164 | + original = requests.copy(deep=True) |
| 165 | + result = store.get_historical_features( |
| 166 | + entity_df=requests, |
| 167 | + features=features, |
| 168 | + full_feature_names=True, |
| 169 | + filter_by_created_timestamp=filter_by_created_timestamp, |
| 170 | + ).to_df() |
| 171 | + |
| 172 | + assert result["first____created_ts"].tolist() == [0.4] |
| 173 | + if include_second_view: |
| 174 | + assert result["second__score"].tolist() == [0.2] |
| 175 | + assert set(result.columns) == set(requests.columns) | expected_features |
| 176 | + assert_frame_equal(result[requests.columns], original) |
| 177 | + assert_frame_equal(requests, original) |
0 commit comments