Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions sdk/python/feast/type_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<item: "


def pa_to_feast_value_type(pa_type_as_str: str) -> 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<item: {inner[:closing]}>"

if pa_type_as_str.startswith("list<item: "):
is_list = True
inner_str = pa_type_as_str[len("list<item: ") : -1]
Expand Down
57 changes: 57 additions & 0 deletions sdk/python/tests/unit/test_type_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -2385,3 +2385,60 @@ def test_null_timestamp_int_value():
from feast.type_map import NULL_TIMESTAMP_INT_VALUE

assert NULL_TIMESTAMP_INT_VALUE == np.iinfo(np.int64).min


def test_pa_to_feast_value_type_fixed_size_list():
"""A fixed-width vector decodes to the same list type as a variable-width one.

``fixed_size_list`` is how Arrow-native stores spell an embedding column, and
is the type ``Field.vector_length`` validation reads a width from. Feast value
types carry no width, so the two spellings must agree.
"""
assert pa_to_feast_value_type("fixed_size_list<item: float>[8]") == (
ValueType.FLOAT_LIST
)
assert pa_to_feast_value_type("fixed_size_list<item: double>[1536]") == (
ValueType.DOUBLE_LIST
)
assert pa_to_feast_value_type("fixed_size_list<item: int64>[4]") == (
ValueType.INT64_LIST
)
assert pa_to_feast_value_type("fixed_size_list<item: int32>[4]") == (
ValueType.INT32_LIST
)
assert pa_to_feast_value_type("fixed_size_list<item: string>[2]") == (
ValueType.STRING_LIST
)
assert pa_to_feast_value_type("fixed_size_list<item: bool>[2]") == (
ValueType.BOOL_LIST
)


def test_pa_to_feast_value_type_fixed_size_list_matches_variable_width():
for fixed, variable in [
("fixed_size_list<item: float>[8]", "list<item: float>"),
("fixed_size_list<item: double>[1536]", "list<item: double>"),
("fixed_size_list<item: int64>[4]", "list<item: int64>"),
("fixed_size_list<item: bool>[2]", "list<item: bool>"),
]:
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<item: list<item: float>>[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<item: float>[8]") == (
pa_to_feast_value_type("fixed_size_list<item: float>[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<item: float>[8]"
assert pa_to_feast_value_type(str(pa_type)) == ValueType.FLOAT_LIST
Loading