Skip to content

Commit

Permalink
fix: support postgresql array types (#2985)
Browse files Browse the repository at this point in the history
  • Loading branch information
kukushking authored Oct 2, 2024
1 parent 5c6d3be commit ed1abde
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions awswrangler/_data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ def pyarrow2postgresql( # noqa: PLR0911
return pyarrow2postgresql(dtype=dtype.value_type, string_type=string_type)
if pa.types.is_binary(dtype):
return "BYTEA"
if pa.types.is_list(dtype):
return pyarrow2postgresql(dtype=dtype.value_type, string_type=string_type) + "[]"
raise exceptions.UnsupportedType(f"Unsupported PostgreSQL type: {dtype}")


Expand Down
2 changes: 2 additions & 0 deletions awswrangler/_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ def generate_placeholder_parameter_pairs(
"""Extract Placeholder and Parameter pairs."""

def convert_value_to_native_python_type(value: Any) -> Any:
if isinstance(value, list):
return value
if pd.isna(value):
return None
if hasattr(value, "to_pydatetime"):
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def test_unknown_overwrite_method_error(postgresql_table, postgresql_con):
def test_sql_types(postgresql_table, postgresql_con):
table = postgresql_table
df = get_df()
df["arrint"] = pd.Series([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
df["arrstr"] = pd.Series([["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]])
df.drop(["binary"], axis=1, inplace=True)
wr.postgresql.to_sql(
df=df,
Expand All @@ -108,7 +110,7 @@ def test_sql_types(postgresql_table, postgresql_con):
schema="public",
mode="overwrite",
index=True,
dtype={"iint32": "INTEGER"},
dtype={"iint32": "INTEGER", "arrint": "INTEGER[]", "arrstr": "VARCHAR[]"},
)
df = wr.postgresql.read_sql_query(f"SELECT * FROM public.{table}", postgresql_con)
ensure_data_types(df, has_list=False)
Expand All @@ -130,6 +132,8 @@ def test_sql_types(postgresql_table, postgresql_con):
"timestamp": pa.timestamp(unit="ns"),
"binary": pa.binary(),
"category": pa.float64(),
"arrint": pa.list_(pa.int64()),
"arrstr": pa.list_(pa.string()),
},
)
for df in dfs:
Expand Down

0 comments on commit ed1abde

Please sign in to comment.