Skip to content

Commit 785b815

Browse files
fix: preserve column names with spaces in wr.redshift.copy() (#3298)
* fix: preserve column names with spaces in wr.redshift.copy() Passes flavor=None to internal s3.to_parquet call to prevent pyarrow spark flavor from sanitizing column names (spaces → underscores). Fixes #3293 * feat: add sanitize_column_names parameter to wr.redshift.copy() Problem: wr.redshift.copy() internally calls s3.to_parquet() which defaults to pyarrow flavor='spark'. This causes column names with spaces to be silently renamed (e.g. "my col" → "my_col"), leading to a mismatch between the DataFrame schema and the Redshift table schema. Solution: Add an optional sanitize_column_names parameter (default=True) to wr.redshift.copy() that controls whether pyarrow sanitizes column names. - sanitize_column_names=True (default): preserves existing behavior, column names are sanitized for backward compatibility. - sanitize_column_names=False: passes flavor=None to the internal s3.to_parquet() call, preserving original column names including spaces. This is a non-breaking change — existing users are unaffected since the default value maintains the current behavior. Changes: - Added sanitize_column_names: bool = True parameter to copy() - Updated pyarrow_additional_kwargs in s3.to_parquet() call accordingly - Added docstring for the new parameter - Added test case for sanitize_column_names=False behavior Fixes #3293 * test: add test for sanitize_column_names=False in wr.redshift.copy() test: add test for sanitize_column_names=False in wr.redshift.copy() * style: fix ruff formatting - remove trailing whitespace style: fix ruff formatting - remove trailing whitespace * style: fix ruff formatting - remove trailing whitespace in _write.py style: fix ruff formatting - remove trailing whitespace in _write.py * style: fix ruff formatting in test_redshift.py style: fix ruff formatting in test_redshift.py * fix: add required blank line in docstring for ruff D410/D411 fix: add required blank line in docstring for ruff D410/D411 * fix: remove trailing whitespace in sanitize_column_names docstring fix: remove trailing whitespace in sanitize_column_names docstring * fix: replace sanitize_column_names with pyarrow_additional_kwargs * fix: update test to use pyarrow_additional_kwargs instead of sanitize_column_names ---------
1 parent 9f70cfa commit 785b815

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

awswrangler/redshift/_write.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ def copy( # noqa: PLR0913
574574
precombine_key: str | None = None,
575575
use_column_names: bool = False,
576576
add_new_columns: bool = False,
577+
pyarrow_additional_kwargs: dict[str, str] | None = None,
577578
) -> None:
578579
"""Load Pandas DataFrame as a Table on Amazon Redshift using parquet files on S3 as stage.
579580
@@ -687,6 +688,9 @@ def copy( # noqa: PLR0913
687688
inserted into the database columns `col1` and `col3`.
688689
add_new_columns
689690
If True, it automatically adds the new DataFrame columns into the target table.
691+
pyarrow_additional_kwargs
692+
Forwarded to pyarrow.
693+
e.g. pyarrow_additional_kwargs={'coerce_timestamps': 'us', 'allow_truncated_timestamps': False}
690694
691695
Examples
692696
--------
@@ -715,6 +719,7 @@ def copy( # noqa: PLR0913
715719
s3.to_parquet(
716720
df=df,
717721
path=path,
722+
pyarrow_additional_kwargs=pyarrow_additional_kwargs or {},
718723
index=index,
719724
dataset=True,
720725
mode="append",

tests/unit/test_redshift.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,3 +1657,26 @@ def test_copy_serialize_to_json_super(
16571657
df_res["text"] = df_res["text"].apply(json.loads)
16581658

16591659
assert_pandas_equals(df, df_res)
1660+
1661+
1662+
def test_copy_preserve_column_names_with_spaces(
1663+
path: str,
1664+
redshift_table: str,
1665+
redshift_con: "redshift_connector.Connection",
1666+
databases_parameters: dict[str, Any],
1667+
) -> None:
1668+
"""Test that pyarrow_additional_kwargs={'flavor': None} preserves column names with spaces."""
1669+
df = pd.DataFrame({"my col": [1, 2, 3], "another col": ["a", "b", "c"]})
1670+
wr.redshift.copy(
1671+
df=df,
1672+
path=path,
1673+
con=redshift_con,
1674+
table=redshift_table,
1675+
schema="public",
1676+
mode="overwrite",
1677+
iam_role=databases_parameters["redshift"]["role"],
1678+
pyarrow_additional_kwargs={"flavor": None},
1679+
)
1680+
result = wr.redshift.read_sql_table(table=redshift_table, schema="public", con=redshift_con)
1681+
assert list(result.columns) == ["my col", "another col"]
1682+
assert len(result) == 3

0 commit comments

Comments
 (0)