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 ab1a19cc4d0..a740f06156e 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 @@ -567,10 +567,12 @@ def online_write_batch( single_entity_record[field] = _default_for_milvus_type( field_type ) - # Store only the latest event timestamp per entity - if ( - entity_key_str not in unique_entities - or unique_entities[entity_key_str]["event_ts"] < timestamp_int + # Keep one row per entity: the latest event timestamp wins, then the + # latest created timestamp, then the last row of the batch. + previous = unique_entities.get(entity_key_str) + if previous is None or (previous["event_ts"], previous["created_ts"]) <= ( + timestamp_int, + created_ts_int, ): unique_entities[entity_key_str] = single_entity_record 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 74d7be745b5..aceb5873e85 100644 --- a/sdk/python/tests/integration/online_store/test_milvus_remote.py +++ b/sdk/python/tests/integration/online_store/test_milvus_remote.py @@ -166,6 +166,39 @@ def test_scalar_feature_view_round_trip( assert rows[0] is not None and rows[0]["city"].string_val == "Paris" +def test_write_batch_tie_break( + tmp_path: Path, project: str, store: MilvusOnlineStore +) -> None: + """Equal event timestamps: the row with the latest created timestamp wins.""" + config = _repo_config(tmp_path, project, consistency_level="Strong") + fv = _scalar_feature_view() + store.update(config, [], [fv], [], [], partial=False) + base = datetime(2024, 1, 1, tzinfo=timezone.utc) + + store.online_write_batch( + config, + fv, + [ + ( + _entity_key(1), + {"city": ValueProto(string_val="Rome")}, + base, + base + timedelta(seconds=2), + ), + ( + _entity_key(1), + {"city": ValueProto(string_val="Oslo")}, + base, + base + timedelta(seconds=1), + ), + ], + progress=None, + ) + + rows = _read(store, config, fv, [1], ["city"]) + assert rows[0] is not None and rows[0]["city"].string_val == "Rome" + + def _vector_feature_view(name: str = "driver_embeddings") -> FeatureView: return FeatureView( name=name, 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 14a47c043a8..0b04d823dce 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 @@ -160,6 +160,69 @@ def test_scalar_feature_view_round_trip_with_placeholder(tmp_path: Path) -> None assert placeholder["params"]["dim"] == PLACEHOLDER_VECTOR_DIM +@pytest.mark.parametrize( + "rows, expected_city", + [ + # Equal event timestamps: the latest created timestamp wins, in any order. + ([("Oslo", 0, 1), ("Rome", 0, 2)], "Rome"), + ([("Rome", 0, 2), ("Oslo", 0, 1)], "Rome"), + # A newer event timestamp wins over a newer created timestamp. + ([("Oslo", 1, 1), ("Rome", 0, 2)], "Oslo"), + # Equal timestamps: the last row of the batch wins. + ([("Oslo", 0, 1), ("Rome", 0, 1)], "Rome"), + ], +) +def test_write_batch_keeps_the_newest_row_per_entity( + tmp_path: Path, rows: List[Tuple[str, int, int]], expected_city: str +) -> None: + store = MilvusOnlineStore() + config = _lite_config(tmp_path) + fv = _scalar_feature_view() + base = datetime(2024, 1, 1, tzinfo=timezone.utc) + + store.online_write_batch( + config, + fv, + [ + ( + _entity_key(1), + {"city": ValueProto(string_val=city)}, + base + timedelta(seconds=event_ts), + base + timedelta(seconds=created_ts), + ) + for city, event_ts, created_ts in rows + ], + progress=None, + ) + + rows_read = _read(store, config, fv, [1], ["city"]) + assert rows_read[0] is not None + assert rows_read[0]["city"].string_val == expected_city + + +def test_write_batch_keeps_one_row_per_entity(tmp_path: Path) -> None: + store = MilvusOnlineStore() + config = _lite_config(tmp_path) + fv = _scalar_feature_view() + now = datetime(2024, 1, 1, tzinfo=timezone.utc) + + store.online_write_batch( + config, + fv, + [ + (_entity_key(1), {"city": ValueProto(string_val="Oslo")}, now, now), + (_entity_key(2), {"city": ValueProto(string_val="Rome")}, now, now), + ], + progress=None, + ) + + rows = _read(store, config, fv, [1, 2], ["city"]) + assert [row["city"].string_val for row in rows if row is not None] == [ + "Oslo", + "Rome", + ] + + @patch(f"{MILVUS_MODULE}.MilvusClient") def test_placeholder_vector_is_indexed_and_valid(mock_client_cls: MagicMock) -> None: mock_client = _mock_client(mock_client_cls, has_collection=False)