Skip to content

Commit 67cbcd5

Browse files
chandlerokclaudentkathole
authored
fix: Refresh pre-computed vectors for pushes with source field mappings (#7076)
* feat: Refresh pre-computed vectors for pushed entities only precompute_feature_service accepts entity_keys and skips the keyspace scan when they are given. The push hook now passes the distinct entity keys from the pushed DataFrame instead of rebuilding every entity in the store. Signed-off-by: Chandler King <chandler.king@findheadway.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * fix: Match pre-computed vector names when full_feature_names is False Stored vectors carry "<fv>__<feature>" names while the default request expects bare feature names, so the fast path always reported a schema mismatch unless full_feature_names=True. Compare on the caller's naming and derive the per-feature view name from the stored names for TTL checks. Signed-off-by: Chandler King <chandler.king@findheadway.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * chore: Retrigger CI Signed-off-by: Chandler King <chandler.king@findheadway.com> * fix: Preserve vector refreshes for pushes with field mappings push() hands _precompute_for_push the frame as the caller sent it, but ingestion renames columns through batch_source.field_mapping before it builds entity keys. A pushed raw_driver_id column mapped to driver_id therefore found no join keys and skipped the refresh, leaving the pre-computed vector stale (or missing for a new entity). Map the columns before extracting the join keys, as ingestion does, and add a push-then-read regression that pushes one entity twice under its source column name and asserts the vector follows the second push. Signed-off-by: chandlerok <chandleroking@gmail.com> * chore: Retrigger CI Signed-off-by: chandlerok <chandleroking@gmail.com> * chore: Retrigger CI Signed-off-by: chandlerok <chandleroking@gmail.com> --------- Signed-off-by: Chandler King <chandler.king@findheadway.com> Signed-off-by: chandlerok <chandleroking@gmail.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: Nikhil Kathole <nikhilkathole2683@gmail.com>
1 parent 2a312cc commit 67cbcd5

2 files changed

Lines changed: 161 additions & 1 deletion

File tree

‎sdk/python/feast/feature_store.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5254,7 +5254,19 @@ def _entity_keys_from_dataframe(feature_view: Any, df: pd.DataFrame) -> List[Ent
52545254
from feast.type_map import python_values_to_proto_values
52555255

52565256
join_keys = list(getattr(feature_view, "join_keys", []) or [])
5257-
if not join_keys or any(k not in df.columns for k in join_keys):
5257+
if not join_keys:
5258+
return []
5259+
5260+
# Ingestion renames the pushed columns with batch_source.field_mapping
5261+
# before it builds entity keys, so a pushed frame may name a join key by
5262+
# its source column. Map the frame the same way before looking for them.
5263+
field_mapping = getattr(
5264+
getattr(feature_view, "batch_source", None), "field_mapping", None
5265+
)
5266+
if isinstance(field_mapping, dict) and field_mapping:
5267+
df = df.rename(columns=field_mapping)
5268+
5269+
if any(k not in df.columns for k in join_keys):
52585270
return []
52595271
value_types = {
52605272
field.name: field.dtype.to_value_type()

‎sdk/python/tests/unit/test_precomputed_feature_vectors.py‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import pytest
88
from google.protobuf.timestamp_pb2 import Timestamp
99

10+
from feast.data_source import PushSource
1011
from feast.feature_service import FeatureService
1112
from feast.feature_view import FeatureView
1213
from feast.field import Field
1314
from feast.infra.offline_stores.file_source import FileSource
15+
from feast.infra.online_stores.online_store import OnlineStore
1416
from feast.on_demand_feature_view import OnDemandFeatureView
1517
from feast.protos.feast.core.PrecomputedFeatureVector_pb2 import (
1618
FeatureViewTimestamp,
@@ -657,6 +659,128 @@ def read_precomputed_vectors(
657659
assert result["driver_stats__score"] == [30.0, 10.0, 30.0, 20.0]
658660

659661

662+
class _InMemoryOnlineStore(OnlineStore):
663+
"""Minimal key-value store so push -> precompute -> read runs end to end."""
664+
665+
def __init__(self):
666+
self._rows: dict = {}
667+
668+
@staticmethod
669+
def _key(table: FeatureView, entity_key) -> tuple:
670+
return table.name, entity_key.SerializeToString()
671+
672+
def online_write_batch(self, config, table, data, progress):
673+
for entity_key, values, _timestamp, _created_ts in data:
674+
self._rows.setdefault(self._key(table, entity_key), {}).update(values)
675+
676+
def online_read(self, config, table, entity_keys, requested_features=None):
677+
rows = []
678+
for entity_key in entity_keys:
679+
values = dict(self._rows.get(self._key(table, entity_key), {}))
680+
if requested_features is not None:
681+
values = {
682+
name: value
683+
for name, value in values.items()
684+
if name in requested_features
685+
}
686+
rows.append((None, values or None))
687+
return rows
688+
689+
def update(
690+
self,
691+
config,
692+
tables_to_delete,
693+
tables_to_keep,
694+
entities_to_delete,
695+
entities_to_keep,
696+
partial,
697+
):
698+
pass
699+
700+
def teardown(self, config, tables, entities):
701+
pass
702+
703+
704+
def _pushable_store(tmp_path):
705+
from feast.entity import Entity
706+
from feast.feature_store import FeatureStore
707+
from feast.infra.online_stores.sqlite import SqliteOnlineStoreConfig
708+
from feast.repo_config import RepoConfig
709+
from feast.value_type import ValueType
710+
711+
store = FeatureStore(
712+
config=RepoConfig(
713+
registry=str(tmp_path / "registry.db"),
714+
project="test",
715+
provider="local",
716+
online_store=SqliteOnlineStoreConfig(path=str(tmp_path / "online.db")),
717+
entity_key_serialization_version=3,
718+
)
719+
)
720+
721+
driver = Entity(name="driver", join_keys=["driver_id"], value_type=ValueType.INT64)
722+
push_source = PushSource(
723+
name="driver_stats_push",
724+
batch_source=FileSource(
725+
name="driver_stats_batch",
726+
path=str(tmp_path / "driver_stats.parquet"),
727+
timestamp_field="event_timestamp",
728+
# Columns arrive from the pushing job under the source names.
729+
field_mapping={"raw_driver_id": "driver_id"},
730+
),
731+
)
732+
feature_view = FeatureView(
733+
name="driver_stats",
734+
entities=[driver],
735+
schema=[
736+
Field(name="driver_id", dtype=Int64),
737+
Field(name="score", dtype=Float32),
738+
],
739+
source=push_source,
740+
)
741+
feature_service = FeatureService(
742+
name="driver_service",
743+
features=[feature_view],
744+
precompute_online=True,
745+
)
746+
store.apply([driver, feature_view, feature_service])
747+
store.provider._online_store = _InMemoryOnlineStore()
748+
return store, feature_service
749+
750+
751+
def _push_score(store, score, event_timestamp):
752+
import pandas as pd
753+
754+
store.push(
755+
"driver_stats_push",
756+
pd.DataFrame(
757+
{
758+
"raw_driver_id": [1001],
759+
"score": [score],
760+
"event_timestamp": [event_timestamp],
761+
}
762+
),
763+
)
764+
765+
766+
def test_push_with_field_mapping_refreshes_precomputed_vector(tmp_path):
767+
"""A push names its columns by source name; the vector must still refresh."""
768+
store, feature_service = _pushable_store(tmp_path)
769+
first_ts = datetime.now(tz=timezone.utc)
770+
771+
_push_score(store, 0.5, first_ts)
772+
response = store.get_online_features(
773+
features=feature_service, entity_rows=[{"driver_id": 1001}]
774+
)
775+
assert response.to_dict()["score"] == [pytest.approx(0.5)]
776+
777+
_push_score(store, 0.9, first_ts + timedelta(seconds=1))
778+
response = store.get_online_features(
779+
features=feature_service, entity_rows=[{"driver_id": 1001}]
780+
)
781+
assert response.to_dict()["score"] == [pytest.approx(0.9)]
782+
783+
660784
# ═══════════════════════════════════════════════════════════════════════════════
661785
# TTL enforcement in fast path
662786
# ═══════════════════════════════════════════════════════════════════════════════
@@ -1102,6 +1226,7 @@ def _make_pushed_fv(self):
11021226
fv = MagicMock()
11031227
fv.join_keys = ["driver_id"]
11041228
fv.entity_columns = [Field(name="driver_id", dtype=Int64)]
1229+
fv.batch_source = None
11051230
return fv
11061231

11071232
def test_triggers_for_matching_fv(self):
@@ -1128,6 +1253,29 @@ def test_triggers_for_matching_fv(self):
11281253
assert [ek.entity_values[0].int64_val for ek in entity_keys] == [1001, 1002]
11291254
assert all(list(ek.join_keys) == ["driver_id"] for ek in entity_keys)
11301255

1256+
def test_maps_source_columns_before_building_entity_keys(self):
1257+
import pandas as pd
1258+
1259+
proj = MagicMock()
1260+
proj.name = "pushed_fv"
1261+
svc = MagicMock()
1262+
svc.precompute_online = True
1263+
svc.feature_view_projections = [proj]
1264+
svc.name = "svc"
1265+
1266+
fv = self._make_pushed_fv()
1267+
fv.batch_source = MagicMock(field_mapping={"raw_driver_id": "driver_id"})
1268+
store = self._make_mock_store([svc])
1269+
store.registry.get_any_feature_view.return_value = fv
1270+
df = pd.DataFrame(
1271+
{"raw_driver_id": [1001, 1002, 1001], "conv_rate": [0.1, 0.2, 0.3]}
1272+
)
1273+
store._precompute_for_push("pushed_fv", df)
1274+
1275+
store.precompute_feature_service.assert_called_once()
1276+
entity_keys = store.precompute_feature_service.call_args.kwargs["entity_keys"]
1277+
assert [ek.entity_values[0].int64_val for ek in entity_keys] == [1001, 1002]
1278+
11311279
def test_skips_when_pushed_rows_have_no_entity_keys(self):
11321280
import pandas as pd
11331281

0 commit comments

Comments
 (0)