Skip to content

Commit a06f363

Browse files
fix: Recognize Parquet list<element: T> types in pa_to_feast_value_type
pa_to_feast_value_type only parsed the "list<item: T>" spelling. Parquet files written by pandas/pyarrow read back as "list<element: T>", so schema inference (feast apply) and write_to_offline_store raised KeyError: 'list<element: string>' for Array(String), Array(Bytes) and Array(UnixTimestamp) columns in a FileSource. The hard-coded "list<element: ...>" fallback keys only covered some element types, and two of them ("str", "bytes") are names pyarrow never prints. large_list<...>, "not null" elements and element-named nested lists failed the same way. Parse every list spelling with one pattern, recurse into the element type as before, and drop the fallback keys. _parse_pa_type_str, which rebuilds nested list types for write_to_offline_store, uses the same helper. Signed-off-by: Joaquin Hui Gomez <132194176+joaquinhuigomez@users.noreply.github.com>
1 parent 2000379 commit a06f363

3 files changed

Lines changed: 99 additions & 13 deletions

File tree

‎sdk/python/feast/infra/offline_stores/offline_utils.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from feast.infra.offline_stores.offline_store import OfflineStore
2121
from feast.infra.registry.base_registry import BaseRegistry
2222
from feast.repo_config import RepoConfig
23-
from feast.type_map import feast_value_type_to_pa
23+
from feast.type_map import _pa_list_inner_type_str, feast_value_type_to_pa
2424
from feast.utils import (
2525
_get_requested_feature_views_to_features_dict,
2626
_get_requested_on_demand_feature_views,
@@ -273,8 +273,8 @@ def get_offline_store_from_config(offline_store_config: Any) -> OfflineStore:
273273
def _parse_pa_type_str(pa_type_str: str) -> pa.DataType:
274274
"""Parse a PyArrow type string to preserve inner element types for nested lists."""
275275
pa_type_str = pa_type_str.strip()
276-
if pa_type_str.startswith("list<item: ") and pa_type_str.endswith(">"):
277-
inner = pa_type_str[len("list<item: ") : -1]
276+
inner = _pa_list_inner_type_str(pa_type_str)
277+
if inner is not None:
278278
return pa.list_(_parse_pa_type_str(inner))
279279
if pa_type_str in _PA_BASIC_TYPES:
280280
return _PA_BASIC_TYPES[pa_type_str]

‎sdk/python/feast/type_map.py‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,13 +1590,27 @@ def _proto_value_to_value_type(proto_value: ProtoValue) -> ValueType:
15901590
return PROTO_VALUE_TO_VALUE_TYPE_MAP[proto_str]
15911591

15921592

1593+
# PyArrow names the list child field "item" by default, but Parquet-compliant
1594+
# lists (e.g. files written by pandas/pyarrow) name it "element". Either may also
1595+
# be a large_list or mark its elements "not null".
1596+
_PA_LIST_TYPE_PATTERN = re.compile(
1597+
r"(?:large_)?list<(?:item|element): (.+?)(?: not null)?>"
1598+
)
1599+
1600+
1601+
def _pa_list_inner_type_str(pa_type_as_str: str) -> Optional[str]:
1602+
"""Return the element type string of a PyArrow list type string, else None."""
1603+
match = _PA_LIST_TYPE_PATTERN.fullmatch(pa_type_as_str)
1604+
return match.group(1) if match else None
1605+
1606+
15931607
def pa_to_feast_value_type(pa_type_as_str: str) -> ValueType:
15941608
is_list = False
1595-
if pa_type_as_str.startswith("list<item: "):
1609+
inner_str = _pa_list_inner_type_str(pa_type_as_str)
1610+
if inner_str is not None:
15961611
is_list = True
1597-
inner_str = pa_type_as_str[len("list<item: ") : -1]
15981612
# Check for nested list (list of lists) before stripping
1599-
if inner_str.startswith("list<item: "):
1613+
if _pa_list_inner_type_str(inner_str) is not None:
16001614
return ValueType.VALUE_LIST
16011615
pa_type_as_str = inner_str
16021616

@@ -1618,13 +1632,6 @@ def pa_to_feast_value_type(pa_type_as_str: str) -> ValueType:
16181632
"binary": ValueType.BYTES,
16191633
"bool": ValueType.BOOL,
16201634
"null": ValueType.NULL,
1621-
"list<element: double>": ValueType.DOUBLE_LIST,
1622-
"list<element: int64>": ValueType.INT64_LIST,
1623-
"list<element: int32>": ValueType.INT32_LIST,
1624-
"list<element: str>": ValueType.STRING_LIST,
1625-
"list<element: bool>": ValueType.BOOL_LIST,
1626-
"list<element: bytes>": ValueType.BYTES_LIST,
1627-
"list<element: float>": ValueType.FLOAT_LIST,
16281635
}
16291636
value_type = type_map[pa_type_as_str]
16301637

‎sdk/python/tests/unit/test_type_map.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,85 @@ def test_pa_to_feast_value_type_nested(self):
18761876
)
18771877

18781878

1879+
class TestPyArrowListTypeStrings:
1880+
"""PyArrow spells list types several ways; Parquet round-trips (pyarrow >= 13)
1881+
name the child field ``element`` instead of ``item``. Schema inference and
1882+
``write_to_offline_store`` used to raise ``KeyError: 'list<element: string>'``.
1883+
"""
1884+
1885+
@pytest.mark.parametrize(
1886+
"pa_type_str, expected",
1887+
[
1888+
("list<element: string>", ValueType.STRING_LIST),
1889+
("list<element: binary>", ValueType.BYTES_LIST),
1890+
("list<element: int64>", ValueType.INT64_LIST),
1891+
("list<element: double>", ValueType.DOUBLE_LIST),
1892+
("list<element: timestamp[us, tz=UTC]>", ValueType.UNIX_TIMESTAMP_LIST),
1893+
("list<element: struct<a: int64 not null>>", ValueType.STRUCT_LIST),
1894+
("large_list<item: string>", ValueType.STRING_LIST),
1895+
("large_list<element: binary>", ValueType.BYTES_LIST),
1896+
("list<item: int64 not null>", ValueType.INT64_LIST),
1897+
("list<item: string>", ValueType.STRING_LIST),
1898+
("list<item: large_string>", ValueType.JSON_LIST),
1899+
("list<element: list<element: int64>>", ValueType.VALUE_LIST),
1900+
("list<item: list<item: int64> not null>", ValueType.VALUE_LIST),
1901+
("large_list<item: list<element: string>>", ValueType.VALUE_LIST),
1902+
],
1903+
)
1904+
def test_pa_to_feast_value_type_list_spellings(self, pa_type_str, expected):
1905+
assert pa_to_feast_value_type(pa_type_str) == expected
1906+
1907+
@pytest.mark.parametrize(
1908+
"pa_type_str, expected",
1909+
[
1910+
(
1911+
"list<element: list<element: int64>>",
1912+
pyarrow.list_(pyarrow.list_(pyarrow.int64())),
1913+
),
1914+
(
1915+
"list<item: list<item: string not null> not null>",
1916+
pyarrow.list_(pyarrow.list_(pyarrow.string())),
1917+
),
1918+
(
1919+
"large_list<item: list<element: double>>",
1920+
pyarrow.list_(pyarrow.list_(pyarrow.float64())),
1921+
),
1922+
],
1923+
)
1924+
def test_parse_pa_type_str_list_spellings(self, pa_type_str, expected):
1925+
from feast.infra.offline_stores.offline_utils import _parse_pa_type_str
1926+
1927+
assert _parse_pa_type_str(pa_type_str) == expected
1928+
1929+
def test_file_source_parquet_list_columns(self, tmp_path):
1930+
from feast.infra.offline_stores.file_source import FileSource
1931+
from feast.infra.offline_stores.offline_utils import (
1932+
get_pyarrow_schema_from_batch_source,
1933+
)
1934+
from feast.repo_config import RepoConfig
1935+
1936+
path = tmp_path / "features.parquet"
1937+
pd.DataFrame(
1938+
{"tags": [["a", "b"], ["c"]], "nested": [[[1, 2]], [[3]]]}
1939+
).to_parquet(path)
1940+
source = FileSource(path=str(path), timestamp_field="event_timestamp")
1941+
config = RepoConfig(
1942+
project="test", registry=str(tmp_path / "registry.db"), provider="local"
1943+
)
1944+
1945+
column_types = dict(source.get_table_column_names_and_types(config))
1946+
assert column_types["tags"] == "list<element: string>"
1947+
to_value_type = source.source_datatype_to_feast_value_type()
1948+
assert to_value_type(column_types["tags"]) == ValueType.STRING_LIST
1949+
assert to_value_type(column_types["nested"]) == ValueType.VALUE_LIST
1950+
1951+
pa_schema, _ = get_pyarrow_schema_from_batch_source(config, source)
1952+
assert pa_schema.field("tags").type == pyarrow.list_(pyarrow.string())
1953+
assert pa_schema.field("nested").type == pyarrow.list_(
1954+
pyarrow.list_(pyarrow.int64())
1955+
)
1956+
1957+
18791958
class TestEmptyArrayAsNull:
18801959
"""Regression tests for https://github.com/feast-dev/feast/issues/6255
18811960
Ensure that an empty numpy array in a scalar feature column is treated as

0 commit comments

Comments
 (0)