diff --git a/docs/reference/online-stores/milvus.md b/docs/reference/online-stores/milvus.md index 31169d2a8c9..d681a229450 100644 --- a/docs/reference/online-stores/milvus.md +++ b/docs/reference/online-stores/milvus.md @@ -197,6 +197,26 @@ When Feast finds an existing collection it checks its load state and loads it on Reads and searches never load collections, so a collection released outside Feast is only reloaded the next time a Feast process first accesses it. +## Row deletion + +`MilvusOnlineStore.online_delete` removes rows of a feature view, by entity key or by filter: + +```python +from feast.filter_models import ComparisonFilter + +online_store.online_delete(config, feature_view, entity_keys=[entity_key]) +online_store.online_delete( + config, + feature_view, + filters=ComparisonFilter(type="eq", key="brand_id", value="acme"), +) +``` + +Give exactly one of `entity_keys` and `filters`. An empty key list deletes nothing, and a filter +with an empty group is rejected, so a mistake cannot delete more rows than it names. A deleted row +leaves no tombstone: a later write creates it again, and Feast does not order a delete against +writes from other processes. Deletion works on every collection that Feast created. + ## Feature views without vectors Milvus requires every collection to have a vector field. For feature views that have no vector diff --git a/sdk/python/feast/infra/online_stores/milvus_online_store/milvus.py b/sdk/python/feast/infra/online_stores/milvus_online_store/milvus.py index 722d8a6ec7b..5a5da70a1ce 100644 --- a/sdk/python/feast/infra/online_stores/milvus_online_store/milvus.py +++ b/sdk/python/feast/infra/online_stores/milvus_online_store/milvus.py @@ -202,6 +202,13 @@ def translate_compound(self, f: CompoundFilter) -> str: return operator.join(clauses) +def _has_empty_group(filters: FilterType) -> bool: + """True when a compound filter has no conditions, at any level.""" + return isinstance(filters, CompoundFilter) and ( + not filters.filters or any(_has_empty_group(f) for f in filters.filters) + ) + + class MilvusOnlineStoreConfig(FeastConfigBaseModel, VectorStoreConfig): """ Configuration for the Milvus online store. @@ -706,6 +713,52 @@ def online_read( return result_list + def online_delete( + self, + config: RepoConfig, + table: FeatureView, + entity_keys: Optional[List[EntityKeyProto]] = None, + *, + filters: Optional[Union[ComparisonFilter, CompoundFilter]] = None, + ) -> None: + """Delete rows of a feature view by entity key or by filter. + + Give exactly one of ``entity_keys`` and ``filters``. An empty key list + deletes nothing. A filter with an empty group is rejected, so a mistake + cannot delete more rows than it names. No tombstone is kept: a later + write creates the row again. + """ + if (entity_keys is None) == (filters is None): + raise ValueError("Give exactly one of entity_keys or filters") + selector: Dict[str, Any] + if entity_keys is not None: + if not entity_keys: + return + selector = { + "ids": [ + serialize_entity_key( + entity_key, + entity_key_serialization_version=config.entity_key_serialization_version, + ).hex() + for entity_key in entity_keys + ] + } + else: + if _has_empty_group(filters): + raise ValueError( + "Each group of the deletion filter must have a condition" + ) + selector = {"filter": MilvusFilterTranslator().translate(filters)} + self.client = self._connect(config) + collection_name = _table_id( + config.project, table, config.registry.enable_online_feature_view_versioning + ) + if collection_name not in self._collections: + if not self.client.has_collection(collection_name): + return + self._ensure_loaded(collection_name) + self.client.delete(collection_name=collection_name, **selector) + def update( self, config: RepoConfig, diff --git a/sdk/python/tests/integration/online_store/test_milvus_remote.py b/sdk/python/tests/integration/online_store/test_milvus_remote.py index d49abe76a95..8f37bdd3fdb 100644 --- a/sdk/python/tests/integration/online_store/test_milvus_remote.py +++ b/sdk/python/tests/integration/online_store/test_milvus_remote.py @@ -407,3 +407,26 @@ def test_partition_key_filtering( filters=ComparisonFilter(type="eq", key="brand_id", value="globex"), ) assert [hit["city"].string_val for hit in hits] == ["Rome"] + + +def test_online_delete(tmp_path: Path, project: str, store: MilvusOnlineStore) -> None: + config = _repo_config(tmp_path, project, consistency_level="Strong") + fv = _vector_feature_view() + store.update(config, [], [fv], [], [], partial=False) + _write_rows(store, config, fv, _vector_rows()) + + store.online_delete(config, fv, [_entity_key(1)]) + assert [row is None for row in _read(store, config, fv, [1, 2], ["city"])] == [ + True, + False, + ] + + store.online_delete( + config, fv, filters=ComparisonFilter(type="eq", key="city", value="Rome") + ) + assert _read(store, config, fv, [1, 2], ["city"]) == [None, None] + + # No tombstone is kept, so the row can be written again. + _write_rows(store, config, fv, {1: _vector_rows()[1]}) + rows = _read(store, config, fv, [1], ["city"]) + assert rows[0] is not None and rows[0]["city"].string_val == "Paris" diff --git a/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py b/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py index d5e227c8b68..f01de6e693b 100644 --- a/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py +++ b/sdk/python/tests/unit/infra/online_store/test_milvus_online_store.py @@ -12,7 +12,7 @@ from feast import Entity, FeatureView from feast.field import Field -from feast.filter_models import ComparisonFilter +from feast.filter_models import ComparisonFilter, CompoundFilter from feast.infra.online_stores.milvus_online_store.milvus import ( PLACEHOLDER_VECTOR_DIM, PLACEHOLDER_VECTOR_FIELD, @@ -746,3 +746,103 @@ def test_warns_when_existing_collection_lacks_partition_key( assert "created without partition key 'brand_id'" in caplog.text mock_client.create_collection.assert_not_called() + + +def test_online_delete_by_entity_key_and_by_filter(tmp_path: Path) -> None: + config = _lite_config(tmp_path) + fv = _scalar_feature_view() + store = MilvusOnlineStore() + store.update(config, [], [fv], [], [], partial=False) + _write_rows( + store, + config, + fv, + { + driver_id: { + "trips_today": ValueProto(float_val=float(driver_id)), + "city": ValueProto(string_val=city), + } + for driver_id, city in [(1, "Paris"), (2, "Rome"), (3, "Rome")] + }, + ) + + store.online_delete(config, fv, [_entity_key(1)]) + rows = _read(store, config, fv, [1, 2, 3], ["city"]) + assert [row is None for row in rows] == [True, False, False] + + store.online_delete( + config, fv, filters=ComparisonFilter(type="eq", key="city", value="Rome") + ) + assert _read(store, config, fv, [1, 2, 3], ["city"]) == [None, None, None] + + # No tombstone is kept, so the row can be written again. + _write_rows( + store, + config, + fv, + { + 1: { + "trips_today": ValueProto(float_val=4.0), + "city": ValueProto(string_val="Oslo"), + } + }, + ) + rows = _read(store, config, fv, [1], ["city"]) + assert rows[0] is not None and rows[0]["city"].string_val == "Oslo" + + +@pytest.mark.parametrize( + "kwargs", + [ + {}, + { + "entity_keys": [_entity_key(1)], + "filters": ComparisonFilter(type="eq", key="city", value="Rome"), + }, + {"filters": CompoundFilter(type="and", filters=[])}, + { + "filters": CompoundFilter( + type="and", + filters=[ + ComparisonFilter(type="eq", key="brand_id", value="acme"), + CompoundFilter(type="or", filters=[]), + ], + ) + }, + ], +) +def test_online_delete_rejects_ambiguous_and_unbounded_calls( + kwargs: Dict[str, Any], +) -> None: + store = MilvusOnlineStore() + store.client = MagicMock() + + with pytest.raises(ValueError): + store.online_delete(_mock_config(), _scalar_feature_view(), **kwargs) + + store.client.delete.assert_not_called() + + +@patch(f"{MILVUS_MODULE}.MilvusClient") +def test_online_delete_does_not_create_a_missing_collection( + mock_client_cls: MagicMock, +) -> None: + mock_client = _mock_client(mock_client_cls, has_collection=False) + store = MilvusOnlineStore() + + store.online_delete(_mock_config(), _scalar_feature_view(), [_entity_key(1)]) + store.online_delete(_mock_config(), _scalar_feature_view(), []) + + mock_client.create_collection.assert_not_called() + mock_client.delete.assert_not_called() + + +@patch(f"{MILVUS_MODULE}.MilvusClient") +def test_online_delete_with_no_entity_keys_sends_nothing( + mock_client_cls: MagicMock, +) -> None: + MilvusOnlineStore().online_delete( + _mock_config(), _scalar_feature_view(), entity_keys=[] + ) + + mock_client_cls.assert_not_called()