Skip to content

Commit 171d601

Browse files
sohailshkntkathole
authored andcommitted
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 5eefef9 commit 171d601

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
@@ -2076,10 +2076,16 @@ def arrow_to_pg_type(t_str: str) -> str:
20762076
return "jsonb"
20772077
if t_str.startswith("struct<") or t_str.startswith("struct{"):
20782078
return "jsonb"
2079+
if t_str.startswith("decimal"):
2080+
return "numeric"
2081+
if t_str.startswith("date"):
2082+
return "date"
20792083
return {
2080-
"null": "null",
2084+
# Postgres has no "null" type; an all-null column still needs one.
2085+
"null": "text",
20812086
"bool": "boolean",
2082-
"int8": "tinyint",
2087+
# Postgres has no 1-byte integer type.
2088+
"int8": "smallint",
20832089
"int16": "smallint",
20842090
"int32": "int",
20852091
"int64": "bigint",
@@ -2094,7 +2100,8 @@ def arrow_to_pg_type(t_str: str) -> str:
20942100
"uint64": "bigint",
20952101
"float": "float",
20962102
"double": "double precision",
2097-
"binary": "binary",
2103+
"binary": "bytea",
2104+
"large_binary": "bytea",
20982105
"string": "text",
20992106
}[t_str]
21002107
except KeyError:

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

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

679+
@pytest.mark.parametrize(
680+
"arrow_type, expected",
681+
[
682+
(pyarrow.int8(), "smallint"),
683+
(pyarrow.binary(), "bytea"),
684+
(pyarrow.large_binary(), "bytea"),
685+
(pyarrow.null(), "text"),
686+
(pyarrow.date32(), "date"),
687+
(pyarrow.date64(), "date"),
688+
(pyarrow.decimal128(10, 2), "numeric"),
689+
],
690+
)
691+
def test_arrow_to_pg_type_uses_postgres_type_names(self, arrow_type, expected):
692+
"""Postgres has no tinyint, binary or null type; the entity dataframe
693+
upload builds CREATE TABLE from these names."""
694+
assert arrow_to_pg_type(str(arrow_type)) == expected
695+
679696
def test_arrow_to_pg_type_map(self):
680697
"""Test that Arrow map type converts to Postgres jsonb."""
681698
assert arrow_to_pg_type("map<string, string>") == "jsonb"

0 commit comments

Comments
 (0)