Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-977836 Update dataframe.py #1149

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/snowflake/snowpark/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3554,7 +3554,7 @@ def with_column_renamed(self, existing: ColumnOrName, new: str) -> "DataFrame":
else:
raise TypeError(f"{str(existing)} must be a column name or Column object.")

to_be_renamed = [x for x in self._output if x.name.upper() == old_name.upper()]
to_be_renamed = [x for x in self._output if x.name == old_name]
if not to_be_renamed:
raise ValueError(
f'Unable to rename column "{existing}" because it doesn\'t exist.'
Expand Down
14 changes: 13 additions & 1 deletion tests/unit/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from snowflake.snowpark.dataframe import _get_unaliased
from snowflake.snowpark.exceptions import SnowparkCreateDynamicTableException
from snowflake.snowpark.session import Session
from snowflake.snowpark.types import IntegerType, StringType
from snowflake.snowpark.types import IntegerType, StringType, StructField, StructType


def test_get_unaliased():
Expand Down Expand Up @@ -162,6 +162,18 @@ def test_with_column_renamed_bad_input():
with pytest.raises(TypeError) as exc_info:
df1.with_column_renamed(123, "int4")
assert "must be a column name or Column object." in str(exc_info)


def test_with_column_renamed_case_sensitivity():
mock_connection = mock.create_autospec(ServerConnection)
mock_connection._conn = mock.MagicMock()
session = snowflake.snowpark.session.Session(mock_connection)
schema = StructType([StructField("id", IntegerType()), StructField("Snow Flake", StringType()), StructField("SNOW FLAKE",StringType())])
expected_schema = StructType([StructField("id", IntegerType()), StructField("Snow Flake Renamed", StringType()), StructField("SNOW FLAKE",StringType())])
df = session.create_dataframe([[1, "snow", "flake"], [3, "snow", "flake"]], schema)
df_renamed = df.with_column_renamed('"Snow Flake"','"Snow Flake Renamed"')
expected_df = session.create_dataframe([[1, "snow", "flake"], [3, "snow", "flake"]], expected_schema)
assert df_renamed.columns == expected_df.columns


def test_with_column_rename_function_bad_input():
Expand Down