diff --git a/sdk/python/feast/type_map.py b/sdk/python/feast/type_map.py index e86885f182f..16334e367c3 100644 --- a/sdk/python/feast/type_map.py +++ b/sdk/python/feast/type_map.py @@ -1595,8 +1595,22 @@ def _proto_value_to_value_type(proto_value: ProtoValue) -> ValueType: return PROTO_VALUE_TO_VALUE_TYPE_MAP[proto_str] +_FIXED_SIZE_LIST_PREFIX = "fixed_size_list ValueType: is_list = False + if pa_type_as_str.startswith(_FIXED_SIZE_LIST_PREFIX): + # A fixed-width vector is a list whose width happens to be fixed, and is + # how Arrow-native stores represent embeddings. Feast value types do not + # encode a width -- Field.vector_length carries it -- so this decodes to + # the same value type as the variable-width spelling, and is rewritten + # into that spelling so the list handling below stays single-sourced. + inner = pa_type_as_str[len(_FIXED_SIZE_LIST_PREFIX) :] + closing = inner.rfind(">") + if closing != -1: + pa_type_as_str = f"list" + if pa_type_as_str.startswith("list[8]") == ( + ValueType.FLOAT_LIST + ) + assert pa_to_feast_value_type("fixed_size_list[1536]") == ( + ValueType.DOUBLE_LIST + ) + assert pa_to_feast_value_type("fixed_size_list[4]") == ( + ValueType.INT64_LIST + ) + assert pa_to_feast_value_type("fixed_size_list[4]") == ( + ValueType.INT32_LIST + ) + assert pa_to_feast_value_type("fixed_size_list[2]") == ( + ValueType.STRING_LIST + ) + assert pa_to_feast_value_type("fixed_size_list[2]") == ( + ValueType.BOOL_LIST + ) + + +def test_pa_to_feast_value_type_fixed_size_list_matches_variable_width(): + for fixed, variable in [ + ("fixed_size_list[8]", "list"), + ("fixed_size_list[1536]", "list"), + ("fixed_size_list[4]", "list"), + ("fixed_size_list[2]", "list"), + ]: + assert pa_to_feast_value_type(fixed) == pa_to_feast_value_type(variable) + + +def test_pa_to_feast_value_type_nested_fixed_size_list(): + assert pa_to_feast_value_type("fixed_size_list>[8]") == ( + ValueType.VALUE_LIST + ) + + +def test_pa_to_feast_value_type_fixed_size_list_width_is_not_encoded(): + """The width is carried by Field.vector_length, not by the value type.""" + assert pa_to_feast_value_type("fixed_size_list[8]") == ( + pa_to_feast_value_type("fixed_size_list[1536]") + ) + + +def test_pa_to_feast_value_type_decodes_a_real_arrow_vector_type(): + """Guard the parser against the exact string PyArrow produces.""" + pa_type = pyarrow.list_(pyarrow.float32(), 8) + assert str(pa_type) == "fixed_size_list[8]" + assert pa_to_feast_value_type(str(pa_type)) == ValueType.FLOAT_LIST