Skip to content

Commit 5b01404

Browse files
committed
fix: Do not write to the stores from local historical retrieval
The local engine ended every plan with a node that writes its rows to the online and offline stores of the feature view. For historical retrieval those rows are now as of the entity timestamps, so reading an older value would replace the newer one the online store serves. Write only for materialization, as the Flink engine does. Signed-off-by: Edson <zhuoxi2000@gmail.com>
1 parent 32e27d0 commit 5b01404

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎sdk/python/feast/infra/compute_engines/local/feature_builder.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ def build_validation_node(self, view, input_node):
171171
return node
172172

173173
def build_output_nodes(self, view, input_node):
174-
node = LocalOutputNode("output", self.dag_root.view, inputs=[input_node])
174+
node = LocalOutputNode(
175+
"output",
176+
self.dag_root.view,
177+
inputs=[input_node],
178+
write_to_stores=isinstance(self.task, MaterializationTask),
179+
)
175180
self.nodes.append(node)
176181
return node

‎sdk/python/feast/infra/compute_engines/local/nodes.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,13 @@ def __init__(
395395
name: str,
396396
feature_view: Union[BatchFeatureView, StreamFeatureView],
397397
inputs=None,
398+
write_to_stores: bool = True,
398399
):
399400
super().__init__(name, inputs=inputs)
400401
self.feature_view = feature_view
402+
# False for historical retrieval, which only reads: its rows are as of
403+
# the entity timestamps and must not replace what the stores serve.
404+
self.write_to_stores = write_to_stores
401405

402406
def execute(self, context: ExecutionContext) -> ArrowTableValue:
403407
input_table = self.get_single_table(context).data
@@ -407,7 +411,7 @@ def execute(self, context: ExecutionContext) -> ArrowTableValue:
407411
output = ArrowTableValue(data=input_table)
408412
context.node_outputs[self.name] = output
409413

410-
if input_table.num_rows == 0:
414+
if input_table.num_rows == 0 or not self.write_to_stores:
411415
return output
412416

413417
if self.feature_view.online:

‎sdk/python/tests/unit/infra/compute_engines/test_local_compute_engine.py‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
ValueType,
1515
)
1616
from feast.feature_view import DUMMY_ENTITY
17+
from feast.infra.common.materialization_job import MaterializationTask
1718
from feast.infra.common.retrieval_task import HistoricalRetrievalTask
1819
from feast.infra.compute_engines.base import ComputeEngine
1920
from feast.infra.compute_engines.local.compute import (
@@ -114,9 +115,7 @@ def _source_view(self, tmp_path):
114115
offline=False,
115116
)
116117

117-
def _get_historical_features(
118-
self, tmp_path, feature_view, entity_df=None, entity=None, backend="pandas"
119-
):
118+
def _engine(self, tmp_path, backend="pandas"):
120119
if backend == "polars":
121120
pytest.importorskip("polars")
122121
repo_config = RepoConfig(
@@ -127,13 +126,18 @@ def _get_historical_features(
127126
offline_store=DaskOfflineStoreConfig(),
128127
entity_key_serialization_version=3,
129128
)
130-
engine = LocalComputeEngine(
129+
return LocalComputeEngine(
131130
repo_config=repo_config,
132131
offline_store=DaskOfflineStore(),
133132
online_store=MagicMock(),
134133
backend=backend,
135134
)
136135

136+
def _get_historical_features(
137+
self, tmp_path, feature_view, entity_df=None, entity=None, backend="pandas"
138+
):
139+
engine = self.engine = self._engine(tmp_path, backend)
140+
137141
# Driver 1 is requested twice: before and after its second feature
138142
# row. Driver 2 is not requested at all.
139143
if entity_df is None:
@@ -290,3 +294,35 @@ def test_get_historical_features_for_view_without_entities(self, tmp_path, backe
290294
)
291295

292296
assert sorted(round(v, 6) for v in df["conv_rate"].to_list()) == [0.1, 0.9]
297+
298+
def test_get_historical_features_does_not_write_to_the_stores(self, tmp_path):
299+
"""The view is online and offline, and the retrieval reads an older value."""
300+
view = self._source_view(tmp_path)
301+
view.online = True
302+
view.offline = True
303+
entity_df = pd.DataFrame(
304+
{"driver_id": [1], "event_timestamp": [self.t0 + timedelta(hours=1)]}
305+
)
306+
307+
df = self._get_historical_features(tmp_path, view, entity_df)
308+
309+
assert [round(v, 6) for v in df["conv_rate"].to_list()] == [0.1]
310+
self.engine.online_store.online_write_batch.assert_not_called()
311+
312+
def test_materialization_still_writes_to_the_online_store(self, tmp_path):
313+
view = self._source_view(tmp_path)
314+
view.online = True
315+
engine = self._engine(tmp_path)
316+
registry = MagicMock()
317+
registry.get_entity.return_value = self.driver
318+
task = MaterializationTask(
319+
project="test_local_engine",
320+
feature_view=view,
321+
start_time=self.t0 - timedelta(hours=1),
322+
end_time=self.t0 + timedelta(hours=3),
323+
)
324+
325+
job = engine._materialize_one(registry, task)
326+
327+
assert job.error() is None
328+
engine.online_store.online_write_batch.assert_called_once()

0 commit comments

Comments
 (0)