Skip to content

Commit 621c1b1

Browse files
fix: Merge shared ODFV source projections in feature resolution
When several on demand feature views share a source feature view but select different input columns, _get_feature_views_to_use appended the shared source view once per ODFV, each copy carrying only that ODFV's projection. The membership check 'if source_fv not in fvs_to_use' never matched because BaseFeatureView.__eq__ compares projections and the freshly fetched full-projection view never equals a stored narrowed copy. Downstream, _group_feature_refs keys its view index by projection.name_to_use(), so the last duplicate won and the other ODFVs' source projections were silently dropped, producing missing inputs (NaN in pandas mode) during retrieval. Index accumulated source views by projection.name_to_use() and merge projections (union of features, deduped by name) instead of appending a duplicate, so a shared source view resolves to a single entry carrying the union of the required input columns. Fixes feast-dev#6621 Signed-off-by: Sanskar Singh <[email protected]>
1 parent 294efad commit 621c1b1

2 files changed

Lines changed: 151 additions & 3 deletions

File tree

‎sdk/python/feast/utils.py‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from feast.base_feature_view import BaseFeatureView
5656
from feast.feature_service import FeatureService
5757
from feast.feature_view import FeatureView
58+
from feast.feature_view_projection import FeatureViewProjection
5859
from feast.infra.registry.base_registry import BaseRegistry
5960
from feast.on_demand_feature_view import OnDemandFeatureView
6061

@@ -1193,6 +1194,19 @@ def _list_feature_views(
11931194
return feature_views
11941195

11951196

1197+
def _merge_projection_features(
1198+
target: "FeatureViewProjection",
1199+
other: "FeatureViewProjection",
1200+
) -> None:
1201+
existing_names = {feature.name for feature in target.features}
1202+
merged = list(target.features)
1203+
for feature in other.features:
1204+
if feature.name not in existing_names:
1205+
existing_names.add(feature.name)
1206+
merged.append(feature)
1207+
target.features = merged
1208+
1209+
11961210
def _get_feature_views_to_use(
11971211
registry: "BaseRegistry",
11981212
project,
@@ -1223,6 +1237,7 @@ def _get_feature_views_to_use(
12231237
feature_views = parsed # type: ignore[assignment]
12241238

12251239
fvs_to_use, od_fvs_to_use = [], []
1240+
fvs_by_projection_key: Dict[str, "FeatureView"] = {}
12261241
for name, version_num, projection in feature_views:
12271242
if version_num is not None:
12281243
if not getattr(registry, "enable_online_versioning", False):
@@ -1286,9 +1301,17 @@ def _get_feature_views_to_use(
12861301
source_fv.entities = [] # type: ignore[attr-defined]
12871302
source_fv.entity_columns = [] # type: ignore[attr-defined]
12881303

1289-
if source_fv not in fvs_to_use:
1290-
fvs_to_use.append(
1291-
source_fv.with_projection(copy.copy(source_projection))
1304+
projection_key = source_projection.name_to_use()
1305+
existing_source_fv = fvs_by_projection_key.get(projection_key)
1306+
if existing_source_fv is None:
1307+
new_source_fv = source_fv.with_projection(
1308+
copy.copy(source_projection)
1309+
)
1310+
fvs_by_projection_key[projection_key] = new_source_fv
1311+
fvs_to_use.append(new_source_fv)
1312+
else:
1313+
_merge_projection_features(
1314+
existing_source_fv.projection, source_projection
12921315
)
12931316
else:
12941317
if (

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

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,128 @@ def test_large_scale_correctness(self):
266266
assert ts.seconds == expected_ts
267267
for status in fv.statuses:
268268
assert status == FieldStatus.PRESENT
269+
270+
271+
class TestGetFeatureViewsToUseSharedSource:
272+
def _build_store(self, data_dir):
273+
import os
274+
from datetime import timedelta
275+
276+
import pandas as pd
277+
278+
from feast import Entity, FeatureStore, FeatureView, Field, FileSource
279+
from feast.infra.online_stores.sqlite import SqliteOnlineStoreConfig
280+
from feast.on_demand_feature_view import on_demand_feature_view
281+
from feast.repo_config import RepoConfig
282+
from feast.types import Float64
283+
284+
driver = Entity(name="driver", join_keys=["driver_id"])
285+
286+
df = pd.DataFrame(
287+
{
288+
"driver_id": [1001, 1002],
289+
"event_timestamp": [
290+
datetime(2024, 1, 1, 10, 0, 0, tzinfo=timezone.utc),
291+
datetime(2024, 1, 1, 11, 0, 0, tzinfo=timezone.utc),
292+
],
293+
"created": [
294+
datetime(2024, 1, 1, 10, 0, 0, tzinfo=timezone.utc),
295+
datetime(2024, 1, 1, 11, 0, 0, tzinfo=timezone.utc),
296+
],
297+
"a": [1.0, 2.0],
298+
"b": [10.0, 20.0],
299+
}
300+
)
301+
src_path = os.path.join(data_dir, "src.parquet")
302+
df.to_parquet(path=src_path, allow_truncated_timestamps=True)
303+
304+
src = FileSource(
305+
name="src_source",
306+
path=src_path,
307+
timestamp_field="event_timestamp",
308+
created_timestamp_column="created",
309+
)
310+
311+
src_fv = FeatureView(
312+
name="src_fv",
313+
entities=[driver],
314+
ttl=timedelta(days=0),
315+
schema=[
316+
Field(name="a", dtype=Float64),
317+
Field(name="b", dtype=Float64),
318+
],
319+
online=True,
320+
source=src,
321+
)
322+
323+
@on_demand_feature_view(
324+
sources=[src_fv[["a"]]],
325+
schema=[Field(name="a_out", dtype=Float64)],
326+
mode="pandas",
327+
)
328+
def odfv_a(inputs):
329+
return pd.DataFrame({"a_out": inputs["a"] + 1})
330+
331+
@on_demand_feature_view(
332+
sources=[src_fv[["b"]]],
333+
schema=[Field(name="b_out", dtype=Float64)],
334+
mode="pandas",
335+
)
336+
def odfv_b(inputs):
337+
return pd.DataFrame({"b_out": inputs["b"] + 100})
338+
339+
store = FeatureStore(
340+
config=RepoConfig(
341+
project="test_shared_source",
342+
registry=os.path.join(data_dir, "registry.db"),
343+
provider="local",
344+
entity_key_serialization_version=3,
345+
online_store=SqliteOnlineStoreConfig(
346+
path=os.path.join(data_dir, "online.db")
347+
),
348+
)
349+
)
350+
store.apply([driver, src, src_fv, odfv_a, odfv_b])
351+
return store
352+
353+
def test_shared_source_projections_are_merged(self):
354+
import tempfile
355+
356+
from feast.utils import _get_feature_views_to_use
357+
358+
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as data_dir:
359+
store = self._build_store(data_dir)
360+
361+
fvs, odfvs = _get_feature_views_to_use(
362+
store.registry,
363+
store.project,
364+
["odfv_a:a_out", "odfv_b:b_out"],
365+
)
366+
367+
src_entries = [fv for fv in fvs if fv.projection.name_to_use() == "src_fv"]
368+
assert len(src_entries) == 1
369+
projected = sorted(
370+
feature.name for feature in src_entries[0].projection.features
371+
)
372+
assert projected == ["a", "b"]
373+
assert {odfv.name for odfv in odfvs} == {"odfv_a", "odfv_b"}
374+
375+
def test_shared_source_ref_order_independent(self):
376+
import tempfile
377+
378+
from feast.utils import _get_feature_views_to_use
379+
380+
with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as data_dir:
381+
store = self._build_store(data_dir)
382+
383+
fvs, _ = _get_feature_views_to_use(
384+
store.registry,
385+
store.project,
386+
["odfv_b:b_out", "odfv_a:a_out"],
387+
)
388+
src_entries = [fv for fv in fvs if fv.projection.name_to_use() == "src_fv"]
389+
assert len(src_entries) == 1
390+
projected = sorted(
391+
feature.name for feature in src_entries[0].projection.features
392+
)
393+
assert projected == ["a", "b"]

0 commit comments

Comments
 (0)