Skip to content

Commit ed60413

Browse files
committed
fix: Use valid Postgres type names for Arrow types in entity dataframe uploads
arrow_to_pg_type, which builds the CREATE TABLE for a pandas entity dataframe, returned type names Postgres does not have: int8 -> tinyint, binary -> binary, null -> null. It also raised for date32 and decimal columns. Map them to smallint, bytea, text, date and numeric. Signed-off-by: sohailshk <122166523+sohailshk@users.noreply.github.com>
1 parent e81e69d commit ed60413

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎sdk/python/feast/type_map.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,10 +2065,16 @@ def arrow_to_pg_type(t_str: str) -> str:
20652065
return "jsonb"
20662066
if t_str.startswith("struct<") or t_str.startswith("struct{"):
20672067
return "jsonb"
2068+
if t_str.startswith("decimal"):
2069+
return "numeric"
2070+
if t_str.startswith("date"):
2071+
return "date"
20682072
return {
2069-
"null": "null",
2073+
# Postgres has no "null" type; an all-null column still needs one.
2074+
"null": "text",
20702075
"bool": "boolean",
2071-
"int8": "tinyint",
2076+
# Postgres has no 1-byte integer type.
2077+
"int8": "smallint",
20722078
"int16": "smallint",
20732079
"int32": "int",
20742080
"int64": "bigint",
@@ -2083,7 +2089,8 @@ def arrow_to_pg_type(t_str: str) -> str:
20832089
"uint64": "bigint",
20842090
"float": "float",
20852091
"double": "double precision",
2086-
"binary": "binary",
2092+
"binary": "bytea",
2093+
"large_binary": "bytea",
20872094
"string": "text",
20882095
}[t_str]
20892096
except KeyError:

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,23 @@ def test_convert_value_type_str_round_trips_every_name(self, value_type):
639639
"""Every ValueType name converts back to that ValueType."""
640640
assert _convert_value_type_str_to_value_type(value_type.name) == value_type
641641

642+
@pytest.mark.parametrize(
643+
"arrow_type, expected",
644+
[
645+
(pyarrow.int8(), "smallint"),
646+
(pyarrow.binary(), "bytea"),
647+
(pyarrow.large_binary(), "bytea"),
648+
(pyarrow.null(), "text"),
649+
(pyarrow.date32(), "date"),
650+
(pyarrow.date64(), "date"),
651+
(pyarrow.decimal128(10, 2), "numeric"),
652+
],
653+
)
654+
def test_arrow_to_pg_type_uses_postgres_type_names(self, arrow_type, expected):
655+
"""Postgres has no tinyint, binary or null type; the entity dataframe
656+
upload builds CREATE TABLE from these names."""
657+
assert arrow_to_pg_type(str(arrow_type)) == expected
658+
642659
def test_arrow_to_pg_type_map(self):
643660
"""Test that Arrow map type converts to Postgres jsonb."""
644661
assert arrow_to_pg_type("map<string, string>") == "jsonb"

0 commit comments

Comments
 (0)