diff --git a/docs/reference/online-stores/milvus.md b/docs/reference/online-stores/milvus.md index aa7f19a8e34..72fd434390b 100644 --- a/docs/reference/online-stores/milvus.md +++ b/docs/reference/online-stores/milvus.md @@ -225,6 +225,37 @@ with an empty group is rejected, so a mistake cannot delete more rows than it na 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. +## Field types + +Each feature is a column of the collection. A value comes back as it was written, whatever its +type. + +| Feast type | Default | With `enable_openai_compatible_store: true` | +|:-----------|:--------|:--------------------------------------------| +| `String` | `VARCHAR` | `VARCHAR` | +| `Bool`, `Int32`, `Int64`, `Float32`, `Float64` | `VARCHAR` holding the value as text | `BOOL`, `INT32`, `INT64`, `FLOAT`, `DOUBLE` | +| `UnixTimestamp` | `VARCHAR` holding the seconds as text | `INT64` | +| `Bytes` | `VARCHAR` holding base64 | `VARCHAR` holding base64 | +| `Array` with `vector_index=True` | `FLOAT_VECTOR` with the field's `vector_length`, or `embedding_dim` when it is unset | the same | +| Every other type, such as an array without `vector_index`, `Json`, `Map` or `Struct` | `VARCHAR` holding the serialized value | the same | + +Filters apply to `VARCHAR` and native columns. A column that holds a serialized value can be read +but not filtered on. + +`Array(Bool)` cannot be a vector field: Milvus binary vectors are not supported. + +A missing or null value is written as the column's default: an empty string, `0` or `false`. Feast +reads an empty string in a numeric or timestamp text column as null, but a missing `String` or +`Bytes` value comes back empty and a `0` in a native numeric column comes back as `0`. Tag a field +with `milvus.nullable: "true"` to store null as `NULL` instead: + +```python +Field(name="rating", dtype=Float32, tags={"milvus.nullable": "true"}) +``` + +The tag applies when Feast creates the collection, and it needs Milvus 2.5 or later. Entity, +partition key and vector fields cannot be nullable. + ## 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 a740f06156e..5d63f9029ed 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 @@ -16,6 +16,7 @@ from feast import Entity from feast.feature_view import FeatureView +from feast.field import Field from feast.filter_models import ( ComparisonFilter, CompoundFilter, @@ -38,7 +39,6 @@ from feast.type_map import ( PROTO_VALUE_TO_VALUE_TYPE_MAP, VALUE_TYPE_TO_PROTO_VALUE_MAP, - feast_value_type_to_python_type, ) from feast.types import ( VALUE_TYPES_TO_FEAST_TYPES, @@ -64,6 +64,7 @@ PROTO_VALUE_TO_VALUE_TYPE_MAP["double_val"]: DataType.DOUBLE, PROTO_VALUE_TO_VALUE_TYPE_MAP["int32_val"]: DataType.INT32, PROTO_VALUE_TO_VALUE_TYPE_MAP["int64_val"]: DataType.INT64, + PROTO_VALUE_TO_VALUE_TYPE_MAP["unix_timestamp_val"]: DataType.INT64, PROTO_VALUE_TO_VALUE_TYPE_MAP["float_list_val"]: DataType.FLOAT_VECTOR, PROTO_VALUE_TO_VALUE_TYPE_MAP["int32_list_val"]: DataType.FLOAT_VECTOR, PROTO_VALUE_TO_VALUE_TYPE_MAP["int64_list_val"]: DataType.FLOAT_VECTOR, @@ -114,6 +115,7 @@ DataType.DOUBLE, DataType.BOOL, } +MILVUS_VECTOR_TYPES = {DataType.FLOAT_VECTOR, DataType.BINARY_VECTOR} # Milvus requires every collection to have a vector field, so feature views # without one get a small placeholder vector. Milvus servers reject vectors @@ -354,49 +356,61 @@ def _get_or_create_collection( ] fields_to_add = [f for f in table.schema if f.name not in fields_to_exclude] use_typed = config.online_store.enable_openai_compatible_store + partition_key = _partition_key_name(config.online_store, table) + entity_columns = {column.name for column in table.entity_columns} for field in fields_to_add: - dtype = FEAST_PRIMITIVE_TO_MILVUS_TYPE_MAPPING.get(field.dtype) - if dtype is None and isinstance(field.dtype, ComplexFeastType): - dtype = DataType.VARCHAR - if dtype: - if dtype == DataType.FLOAT_VECTOR: - fields.append( - FieldSchema( - name=field.name, - dtype=dtype, - dim=config.online_store.embedding_dim, - ) + dtype = _milvus_field_type(field) + if dtype == DataType.BINARY_VECTOR: + raise ValueError( + f"Field '{field.name}': Array(Bool) cannot be a vector field," + " Milvus binary vectors are not supported." + ) + nullable = _nullable_tag(field) + if nullable and ( + field.name in entity_columns + or field.name == partition_key + or dtype in MILVUS_VECTOR_TYPES + ): + raise ValueError( + f"Field '{field.name}' cannot be nullable: entity, partition" + " key and vector fields must always have a value." + ) + options: Dict[str, Any] = {"nullable": True} if nullable else {} + if dtype in MILVUS_VECTOR_TYPES: + fields.append( + FieldSchema( + name=field.name, + dtype=dtype, + dim=field.vector_length + or config.online_store.embedding_dim, ) - elif use_typed and dtype in MILVUS_NATIVE_NUMERIC_TYPES: - fields.append( - FieldSchema( - name=field.name, - dtype=dtype, + ) + elif use_typed and dtype in MILVUS_NATIVE_NUMERIC_TYPES: + fields.append(FieldSchema(name=field.name, dtype=dtype, **options)) + else: + if "max_length" in field.tags: + try: + field_max_length = int(field.tags["max_length"]) + except (ValueError, TypeError): + raise ValueError( + f"Field '{field.name}' has invalid max_length tag" + f" '{field.tags['max_length']}': must be an integer." ) - ) - else: - if "max_length" in field.tags: - try: - field_max_length = int(field.tags["max_length"]) - except (ValueError, TypeError): - raise ValueError( - f"Field '{field.name}' has invalid max_length tag" - f" '{field.tags['max_length']}': must be an integer." - ) - if not (1 <= field_max_length <= 65535): - raise ValueError( - f"Field '{field.name}' max_length tag must be" - f" between 1 and 65535, got {field_max_length}" - ) - else: - field_max_length = varchar_max_length - fields.append( - FieldSchema( - name=field.name, - dtype=DataType.VARCHAR, - max_length=field_max_length, + if not (1 <= field_max_length <= 65535): + raise ValueError( + f"Field '{field.name}' max_length tag must be" + f" between 1 and 65535, got {field_max_length}" ) + else: + field_max_length = varchar_max_length + fields.append( + FieldSchema( + name=field.name, + dtype=DataType.VARCHAR, + max_length=field_max_length, + **options, ) + ) has_vector_field = any( f.dtype in (DataType.FLOAT_VECTOR, DataType.BINARY_VECTOR) for f in fields @@ -409,7 +423,6 @@ def _get_or_create_collection( dim=PLACEHOLDER_VECTOR_DIM, ) ) - partition_key = _partition_key_name(config.online_store, table) if partition_key: _mark_partition_key(fields, partition_key, table.name) schema = CollectionSchema( @@ -512,6 +525,9 @@ def online_write_batch( ), PLACEHOLDER_VECTOR_DIM, ) + nullable_fields = { + field["name"] for field in collection["fields"] if field.get("nullable") + } schema_internal_fields = {"event_ts", "created_ts"} collection_has_native_numerics = any( collection_field_types.get(name) in MILVUS_NATIVE_NUMERIC_TYPES @@ -528,17 +544,10 @@ def online_write_batch( created_ts_int = ( int(to_naive_utc(created_ts).timestamp() * 1e6) if created_ts else 0 ) - entity_dict = { - join_key: feast_value_type_to_python_type(value) - for join_key, value in zip( - entity_key.join_keys, entity_key.entity_values - ) - } - values_dict.update(entity_dict) + values_dict.update(zip(entity_key.join_keys, entity_key.entity_values)) values_dict = _extract_proto_values_to_dict( values_dict, vector_cols=vector_cols, - serialize_to_string=True, use_native_numeric_types=collection_has_native_numerics, ) @@ -563,6 +572,8 @@ def online_write_batch( field_type = collection_field_types.get(field, DataType.VARCHAR) if field == PLACEHOLDER_VECTOR_FIELD: single_entity_record[field] = [0.0] * placeholder_dim + elif field in nullable_fields: + single_entity_record[field] = None else: single_entity_record[field] = _default_for_milvus_type( field_type @@ -638,12 +649,8 @@ def online_read( # Map the features to their Feast types. feature_name_feast_primitive_type_map = { - f.name: f.dtype for f in table.features + f.name: f.dtype for f in list(table.schema) + list(table.features) } - if getattr(table, "write_to_online_store", False): - feature_name_feast_primitive_type_map.update( - {f.name: f.dtype for f in table.schema} - ) # Build a dictionary mapping composite key -> (res_ts, res) results_dict: Dict[ str, Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]] @@ -657,7 +664,6 @@ def online_read( res = {} res_ts = None for field in output_fields: - val = ValueProto() field_value = hit.get(field, None) if field_value is None and ":" in field: _, field_short = field.split(":", 1) @@ -669,57 +675,13 @@ def online_read( # We do not return the composite key value pass else: - feature_feast_primitive_type = ( + key_to_use = field.split(":", 1)[-1] if ":" in field else field + res[key_to_use] = _stored_value_to_proto( + field_value, feature_name_feast_primitive_type_map.get( field, PrimitiveFeastType.INVALID - ) + ), ) - feature_fv_dtype = from_feast_type(feature_feast_primitive_type) - proto_attr = VALUE_TYPE_TO_PROTO_VALUE_MAP.get(feature_fv_dtype) - if proto_attr: - if proto_attr == "bytes_val": - setattr(val, proto_attr, field_value.encode()) - elif proto_attr in [ - "int32_val", - "int64_val", - "float_val", - "double_val", - "string_val", - ]: - setattr( - val, - proto_attr, - type(getattr(val, proto_attr))(field_value), - ) - elif proto_attr in [ - "int32_list_val", - "int64_list_val", - "float_list_val", - "double_list_val", - ]: - getattr(val, proto_attr).val.extend(field_value) - elif proto_attr in [ - "map_val", - "map_list_val", - "struct_val", - "struct_list_val", - "json_list_val", - ]: - if isinstance(field_value, str) and field_value: - try: - proto_bytes = base64.b64decode(field_value) - val.ParseFromString(proto_bytes) - except Exception: - setattr(val, "string_val", field_value) - else: - setattr(val, proto_attr, field_value) - else: - raise ValueError( - f"Unsupported ValueType: {feature_feast_primitive_type} with feature view value {field_value} for feature {field} with value type {proto_attr}" - ) - # res[field] = val - key_to_use = field.split(":", 1)[-1] if ":" in field else field - res[key_to_use] = val results_dict[key] = (res_ts, res if res else None) # Map the results back into a list matching the original order of composite_keys. @@ -843,9 +805,7 @@ def retrieve_online_documents_v2( List of tuples containing the event timestamp, entity key, and feature values """ entity_name_feast_primitive_type_map = { - k.name: k.dtype - for k in list(table.entity_columns) + list(table.features) - if isinstance(k.dtype, PrimitiveFeastType) + k.name: k.dtype for k in list(table.schema) + list(table.features) } self.client = self._connect(config) collection_name = _table_id( @@ -1032,7 +992,6 @@ def _combine_exprs(*parts: Optional[str]) -> Optional[str]: else None ) for field in output_fields: - val = ValueProto() field_value = hit.get("entity", {}).get(field, None) # entity_key_proto = None if field in ["created_ts", "event_ts"]: @@ -1042,57 +1001,15 @@ def _combine_exprs(*parts: Optional[str]) -> Optional[str]: embedding ) res[ann_search_field] = serialized_embedding - elif ( - entity_name_feast_primitive_type_map.get( - field, PrimitiveFeastType.INVALID - ) - == PrimitiveFeastType.STRING - ): - res[field] = ValueProto(string_val=str(field_value)) - elif ( - entity_name_feast_primitive_type_map.get( - field, PrimitiveFeastType.INVALID - ) - == PrimitiveFeastType.BYTES - ): - try: - decoded_bytes = base64.b64decode(field_value) - res[field] = ValueProto(bytes_val=decoded_bytes) - except Exception: - res[field] = ValueProto(string_val=str(field_value)) - elif entity_name_feast_primitive_type_map.get( - field, PrimitiveFeastType.INVALID - ) in [ - PrimitiveFeastType.INT64, - PrimitiveFeastType.INT32, - ]: - res[field] = ValueProto(int64_val=int(field_value)) - elif entity_name_feast_primitive_type_map.get( - field, PrimitiveFeastType.INVALID - ) in [ - PrimitiveFeastType.FLOAT64, - PrimitiveFeastType.FLOAT32, - ]: - res[field] = ValueProto(double_val=float(field_value)) - elif ( - entity_name_feast_primitive_type_map.get( - field, PrimitiveFeastType.INVALID - ) - == PrimitiveFeastType.BOOL - ): - res[field] = ValueProto(bool_val=bool(field_value)) elif field == composite_key_name: pass - elif isinstance(field_value, bytes): - val.ParseFromString(field_value) - res[field] = val - elif isinstance(field_value, int): - res[field] = ValueProto(int64_val=field_value) - elif isinstance(field_value, float): - res[field] = ValueProto(double_val=field_value) else: - val.string_val = str(field_value) - res[field] = val + res[field] = _stored_value_to_proto( + field_value, + entity_name_feast_primitive_type_map.get( + field, PrimitiveFeastType.INVALID + ), + ) raw_distance = hit.get("distance", None) res["distance"] = ( ValueProto(float_val=raw_distance) @@ -1263,70 +1180,111 @@ def _default_for_milvus_type(dtype: DataType) -> Any: return _MILVUS_TYPE_DEFAULTS.get(dtype, "") +_NUMERIC_SCALAR_PROTO_TYPES = ( + "double_val", + "float_val", + "int32_val", + "int64_val", + "bool_val", + "unix_timestamp_val", +) + + def _extract_proto_values_to_dict( - input_dict: Dict[str, Any], + input_dict: Dict[str, ValueProto], vector_cols: List[str], - serialize_to_string: bool = False, use_native_numeric_types: bool = False, ) -> Dict[str, Any]: - numeric_vector_list_types = [ - k - for k in PROTO_VALUE_TO_VALUE_TYPE_MAP.keys() - if k is not None and ("list" in k or "set" in k) and "string" not in k - ] - numeric_types = [ - "double_val", - "float_val", - "int32_val", - "int64_val", - "bool_val", - ] - output_dict = {} - for feature_name, feature_values in input_dict.items(): - for proto_val_type in PROTO_VALUE_TO_VALUE_TYPE_MAP: - if not isinstance(feature_values, (int, float, str)): - if feature_values.HasField(proto_val_type): - if proto_val_type in numeric_vector_list_types: - if serialize_to_string and feature_name not in vector_cols: - vector_values = getattr( - feature_values, proto_val_type - ).SerializeToString() - else: - vector_values = getattr(feature_values, proto_val_type).val - else: - if ( - serialize_to_string - and proto_val_type - not in ["string_val", "bytes_val", "unix_timestamp_val"] - + numeric_types - ): - vector_values = base64.b64encode( - feature_values.SerializeToString() - ).decode("utf-8") - elif proto_val_type == "bytes_val": - byte_data = getattr(feature_values, proto_val_type) - vector_values = base64.b64encode(byte_data).decode("utf-8") - elif ( - use_native_numeric_types and proto_val_type in numeric_types - ): - vector_values = getattr(feature_values, proto_val_type) - else: - if not isinstance(feature_values, str): - vector_values = str( - getattr(feature_values, proto_val_type) - ) - else: - vector_values = getattr(feature_values, proto_val_type) - output_dict[feature_name] = vector_values - else: - if serialize_to_string: - if use_native_numeric_types and isinstance( - feature_values, (int, float) - ): - output_dict[feature_name] = feature_values - else: - if not isinstance(feature_values, str): - feature_values = str(feature_values) - output_dict[feature_name] = feature_values + """Convert the values of one row to what the collection stores. + A vector column gets its list of numbers. A numeric is stored natively or as + text, a string as is, bytes as base64 text. Every other value, such as a + list, a map or JSON, is stored as the base64 of its serialized ValueProto so + it comes back as written. A null value is left out: the caller writes None + or the column's default for it. + """ + output_dict: Dict[str, Any] = {} + for feature_name, value in input_dict.items(): + proto_val_type = value.WhichOneof("val") + if proto_val_type is None: + continue + stored = getattr(value, proto_val_type) + if feature_name in vector_cols: + output_dict[feature_name] = list(stored.val) + elif proto_val_type == "string_val": + output_dict[feature_name] = stored + elif proto_val_type == "bytes_val": + output_dict[feature_name] = base64.b64encode(stored).decode("utf-8") + elif proto_val_type in _NUMERIC_SCALAR_PROTO_TYPES: + output_dict[feature_name] = ( + stored if use_native_numeric_types else str(stored) + ) + else: + output_dict[feature_name] = base64.b64encode( + value.SerializeToString() + ).decode("utf-8") return output_dict + + +def _stored_value_to_proto(stored: Any, feast_type: Any) -> ValueProto: + """Rebuild the ValueProto of a feature from what the collection stores. + + The inverse of ``_extract_proto_values_to_dict``. None, and the empty text + that a collection without nullable columns stores for a missing value, are + null. + """ + val = ValueProto() + if stored is None: + return val + proto_attr = VALUE_TYPE_TO_PROTO_VALUE_MAP.get(from_feast_type(feast_type)) + if proto_attr is None: + # A column the feature view does not declare, for example an entity key. + if isinstance(stored, bool): + val.bool_val = stored + elif isinstance(stored, int): + val.int64_val = stored + elif isinstance(stored, float): + val.double_val = stored + else: + val.string_val = str(stored) + return val + if isinstance(stored, list): + getattr(val, proto_attr).val.extend(stored) + return val + if proto_attr == "string_val": + val.string_val = str(stored) + return val + if proto_attr == "bytes_val": + val.bytes_val = base64.b64decode(stored) + return val + if stored == "": + return val + if proto_attr == "bool_val": + val.bool_val = ( + stored if isinstance(stored, bool) else str(stored).lower() == "true" + ) + elif proto_attr in ("int32_val", "int64_val", "unix_timestamp_val"): + setattr(val, proto_attr, int(stored)) + elif proto_attr in ("float_val", "double_val"): + setattr(val, proto_attr, float(stored)) + else: + val.ParseFromString(base64.b64decode(stored)) + return val + + +def _milvus_field_type(field: Field) -> DataType: + """The Milvus type of a feature: a vector only for ``vector_index`` fields, + a native scalar where the collection stores them, VARCHAR for the rest.""" + if isinstance(field.dtype, Array) and not field.vector_index: + return DataType.VARCHAR + return FEAST_PRIMITIVE_TO_MILVUS_TYPE_MAPPING.get(field.dtype, DataType.VARCHAR) + + +def _nullable_tag(field: Field) -> bool: + value = field.tags.get("milvus.nullable", "false").lower() + if value not in ("true", "false"): + raise ValueError( + f"Field '{field.name}' has invalid milvus.nullable tag" + f" '{field.tags['milvus.nullable']}': must be 'true' or 'false'." + ) + return value == "true" 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 aceb5873e85..7831fc24bfb 100644 --- a/sdk/python/tests/integration/online_store/test_milvus_remote.py +++ b/sdk/python/tests/integration/online_store/test_milvus_remote.py @@ -25,9 +25,20 @@ from feast.filter_models import ComparisonFilter from feast.infra.online_stores.milvus_online_store.milvus import MilvusOnlineStore from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto +from feast.protos.feast.types.Value_pb2 import StringList from feast.protos.feast.types.Value_pb2 import Value as ValueProto from feast.repo_config import RepoConfig -from feast.types import Array, Float32, Int64, String +from feast.types import ( + Array, + Bool, + Bytes, + Float32, + Float64, + Int64, + Json, + String, + UnixTimestamp, +) from feast.value_type import ValueType T = TypeVar("T") @@ -520,3 +531,49 @@ def test_online_delete(tmp_path: Path, project: str, store: MilvusOnlineStore) - _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" + + +@pytest.mark.parametrize("native", [False, True], ids=["varchar", "native_numerics"]) +def test_every_type_round_trips_and_nullable_columns_store_null( + tmp_path: Path, project: str, store: MilvusOnlineStore, native: bool +) -> None: + config = _repo_config( + tmp_path, + project, + enable_openai_compatible_store=native, + consistency_level="Strong", + ) + fv = FeatureView( + name="typed", + entities=[ + Entity( + name="driver_id", join_keys=["driver_id"], value_type=ValueType.INT64 + ) + ], + ttl=timedelta(days=1), + schema=[ + Field(name="driver_id", dtype=Int64), + Field(name="flag", dtype=Bool), + Field(name="blob", dtype=Bytes), + Field(name="seen_at", dtype=UnixTimestamp), + Field(name="doc", dtype=Json), + Field(name="tags", dtype=Array(String)), + Field(name="score", dtype=Float64, tags={"milvus.nullable": "true"}), + ], + ) + values = { + "flag": ValueProto(bool_val=False), + "blob": ValueProto(bytes_val=b"\x00\xff"), + "seen_at": ValueProto(unix_timestamp_val=1_700_000_000), + "doc": ValueProto(json_val='{"a": [1, 2]}'), + "tags": ValueProto(string_list_val=StringList(val=["a", "b"])), + "score": ValueProto(double_val=2.5), + } + store.update(config, [], [fv], [], [], partial=False) + + _write_rows(store, config, fv, {1: values, 2: {**values, "score": ValueProto()}}) + rows = _read(store, config, fv, [1, 2], list(values)) + + assert rows[0] == values + assert rows[1] is not None and rows[1]["score"].WhichOneof("val") is None + assert rows[1]["tags"] == values["tags"] 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 0b04d823dce..f43776b1db2 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 @@ -20,9 +20,27 @@ MilvusOnlineStoreConfig, ) from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto +from feast.protos.feast.types.Value_pb2 import ( + FloatList, + Int64List, + StringList, +) from feast.protos.feast.types.Value_pb2 import Value as ValueProto from feast.repo_config import RepoConfig -from feast.types import Array, Float32, Int64, String +from feast.type_map import python_values_to_proto_values +from feast.types import ( + Array, + Bool, + Bytes, + Float32, + Float64, + Int32, + Int64, + Json, + Map, + String, + UnixTimestamp, +) from feast.value_type import ValueType MILVUS_MODULE = "feast.infra.online_stores.milvus_online_store.milvus" @@ -946,3 +964,274 @@ def test_online_delete_with_no_entity_keys_sends_nothing( ) mock_client_cls.assert_not_called() + + +def _typed_feature_view(name: str, dtype: Any, **field_kwargs: Any) -> FeatureView: + return FeatureView( + name=name, + entities=[_driver_entity()], + ttl=timedelta(days=1), + schema=[ + Field(name="driver_id", dtype=Int64), + Field(name="value", dtype=dtype, **field_kwargs), + ], + ) + + +_TYPED_VALUES: List[Tuple[Any, ValueProto]] = [ + (Bool, ValueProto(bool_val=False)), + (Int32, ValueProto(int32_val=-7)), + (Int64, ValueProto(int64_val=1 << 40)), + (Float32, ValueProto(float_val=0.1)), + (Float64, ValueProto(double_val=2.5)), + (String, ValueProto(string_val="True")), + (Bytes, ValueProto(bytes_val=b"\x00\xff")), + (Bytes, ValueProto(bytes_val=b"")), + (UnixTimestamp, ValueProto(unix_timestamp_val=1_700_000_000)), + (Json, ValueProto(json_val='{"a": [1, 2]}')), + (Array(String), ValueProto(string_list_val=StringList(val=["a", "b"]))), + (Array(Int64), ValueProto(int64_list_val=Int64List(val=[1, 2, 3]))), + (Map, python_values_to_proto_values([{"a": "x", "n": 1}], ValueType.MAP)[0]), +] + + +@pytest.mark.parametrize( + "online_store", + [{}, {"enable_openai_compatible_store": True}], + ids=["varchar", "native_numerics"], +) +def test_every_feast_type_round_trips( + tmp_path: Path, online_store: Dict[str, Any] +) -> None: + config = _lite_config(tmp_path, **online_store) + store = MilvusOnlineStore() + + for index, (dtype, value) in enumerate(_TYPED_VALUES): + fv = _typed_feature_view(f"typed_{index}", dtype) + store.update(config, [], [fv], [], [], partial=False) + _write_rows(store, config, fv, {1: {"value": value}}) + rows = _read(store, config, fv, [1], ["value"]) + assert rows[0] is not None and rows[0]["value"] == value, dtype + + +@patch(f"{MILVUS_MODULE}.MilvusClient") +def test_column_types_follow_vector_index_and_vector_length( + mock_client_cls: MagicMock, +) -> None: + mock_client = _mock_client(mock_client_cls, has_collection=False) + fv = FeatureView( + name="catalog", + entities=[_driver_entity()], + ttl=timedelta(days=1), + schema=[ + Field(name="driver_id", dtype=Int64), + Field( + name="embedding", + dtype=Array(Float32), + vector_index=True, + vector_length=3, + ), + Field(name="history", dtype=Array(Float32)), + Field(name="seen_at", dtype=UnixTimestamp), + ], + ) + + for native, seen_at_type in [(False, DataType.VARCHAR), (True, DataType.INT64)]: + MilvusOnlineStore()._get_or_create_collection( + _mock_config(embedding_dim=2, enable_openai_compatible_store=native), fv + ) + schema = mock_client.create_collection.call_args.kwargs["schema"] + fields = {field.name: field for field in schema.fields} + assert fields["embedding"].dtype == DataType.FLOAT_VECTOR + assert fields["embedding"].params["dim"] == 3 + assert fields["history"].dtype == DataType.VARCHAR + assert fields["seen_at"].dtype == seen_at_type + assert PLACEHOLDER_VECTOR_FIELD not in fields + + +def test_missing_value_in_a_text_column_reads_as_null(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, + {1: {"trips_today": ValueProto(), "city": ValueProto(string_val="Paris")}}, + ) + rows = _read(store, config, fv, [1], ["trips_today", "city"]) + + assert rows[0] is not None + assert rows[0]["trips_today"].WhichOneof("val") is None + assert rows[0]["city"].string_val == "Paris" + + +def test_nullable_tag_stores_null_instead_of_the_column_default( + tmp_path: Path, +) -> None: + config = _lite_config(tmp_path, enable_openai_compatible_store=True) + fv = FeatureView( + name="driver_stats", + entities=[_driver_entity()], + ttl=timedelta(days=1), + schema=[ + Field(name="driver_id", dtype=Int64), + Field(name="trips_today", dtype=Float32, tags={"milvus.nullable": "true"}), + Field(name="rating", dtype=Float32), + ], + ) + store = MilvusOnlineStore() + store.update(config, [], [fv], [], [], partial=False) + assert store.client is not None + description = store.client.describe_collection("test_milvus_driver_stats") + nullable = {f["name"]: f.get("nullable", False) for f in description["fields"]} + assert nullable["trips_today"] and not nullable["rating"] + + _write_rows( + store, config, fv, {1: {"trips_today": ValueProto(), "rating": ValueProto()}} + ) + rows = _read(store, config, fv, [1], ["trips_today", "rating"]) + + assert rows[0] is not None + assert rows[0]["trips_today"].WhichOneof("val") is None + # A column that is not nullable keeps the type's default. + assert rows[0]["rating"].float_val == 0.0 + + +@pytest.mark.parametrize( + "field, match", + [ + ( + Field(name="driver_id", dtype=Int64, tags={"milvus.nullable": "true"}), + "cannot be nullable", + ), + ( + Field( + name="embedding", + dtype=Array(Float32), + vector_index=True, + tags={"milvus.nullable": "true"}, + ), + "cannot be nullable", + ), + ( + Field(name="city", dtype=String, tags={"milvus.nullable": "maybe"}), + "invalid milvus.nullable tag", + ), + ], + ids=["entity", "vector", "invalid"], +) +@patch(f"{MILVUS_MODULE}.MilvusClient") +def test_nullable_tag_is_rejected_where_a_value_is_required( + mock_client_cls: MagicMock, field: Field, match: str +) -> None: + mock_client = _mock_client(mock_client_cls, has_collection=False) + schema = [Field(name="driver_id", dtype=Int64)] if field.name != "driver_id" else [] + fv = FeatureView( + name="driver_stats", + entities=[_driver_entity()], + ttl=timedelta(days=1), + schema=schema + [field], + ) + + with pytest.raises(ValueError, match=match): + MilvusOnlineStore()._get_or_create_collection(_mock_config(), fv) + + mock_client.create_collection.assert_not_called() + + +def test_search_hits_decode_values_like_reads(tmp_path: Path) -> None: + config = _lite_config(tmp_path) + fv = FeatureView( + name="documents", + entities=[_driver_entity()], + ttl=timedelta(days=1), + schema=[ + Field(name="driver_id", dtype=Int64), + Field( + name="embedding", + dtype=Array(Float32), + vector_index=True, + vector_search_metric="COSINE", + ), + Field(name="flag", dtype=Bool), + Field(name="blob", dtype=Bytes), + Field(name="doc", dtype=Json), + Field(name="tags", dtype=Array(String)), + Field(name="labels", dtype=Map), + ], + ) + values = { + "embedding": ValueProto(float_list_val=FloatList(val=[1.0, 0.0])), + "flag": ValueProto(bool_val=False), + "blob": ValueProto(bytes_val=b"\x00\xff"), + "doc": ValueProto(json_val='{"a": 1}'), + "tags": ValueProto(string_list_val=StringList(val=["a", "b"])), + "labels": python_values_to_proto_values([{"a": "x"}], ValueType.MAP)[0], + } + features = ["flag", "blob", "doc", "tags", "labels"] + store = MilvusOnlineStore() + store.update(config, [], [fv], [], [], partial=False) + _write_rows(store, config, fv, {1: values}) + + hits = store.retrieve_online_documents_v2(config, fv, features, [1.0, 0.0], top_k=1) + + assert len(hits) == 1 + _, _, hit = hits[0] + assert hit is not None + expected = {name: values[name] for name in features} + assert {name: hit[name] for name in features} == expected + assert _read(store, config, fv, [1], features) == [expected] + + +def test_entity_columns_use_the_same_encoding_as_features(tmp_path: Path) -> None: + config = _lite_config(tmp_path) + fv = FeatureView( + name="devices", + entities=[ + Entity(name="device", join_keys=["device"], value_type=ValueType.BYTES), + Entity( + name="seen", join_keys=["seen"], value_type=ValueType.UNIX_TIMESTAMP + ), + ], + ttl=timedelta(days=1), + schema=[ + Field(name="device", dtype=Bytes), + Field(name="seen", dtype=UnixTimestamp), + Field(name="city", dtype=String), + ], + ) + key = EntityKeyProto( + join_keys=["device", "seen"], + entity_values=[ + ValueProto(bytes_val=b"\x00\xff"), + ValueProto(unix_timestamp_val=1_700_000_000), + ], + ) + store = MilvusOnlineStore() + store.update(config, [], [fv], [], [], partial=False) + now = datetime.now(timezone.utc) + store.online_write_batch( + config, fv, [(key, {"city": ValueProto(string_val="Oslo")}, now, now)], None + ) + + _, values = store.online_read(config, fv, [key], ["device", "seen", "city"])[0] + + assert values == { + "device": ValueProto(bytes_val=b"\x00\xff"), + "seen": ValueProto(unix_timestamp_val=1_700_000_000), + "city": ValueProto(string_val="Oslo"), + } + + +@patch(f"{MILVUS_MODULE}.MilvusClient") +def test_bool_array_cannot_be_a_vector_field(mock_client_cls: MagicMock) -> None: + mock_client = _mock_client(mock_client_cls, has_collection=False) + fv = _typed_feature_view("flags", Array(Bool), vector_index=True) + + with pytest.raises(ValueError, match="binary vectors are not supported"): + MilvusOnlineStore()._get_or_create_collection(_mock_config(), fv) + + mock_client.create_collection.assert_not_called()