Skip to content

Commit 30e5f9e

Browse files
[SNOW-1879403] Fix replace when passing Column object to avoid incorrect usage of lit(...) from within. (#2858)
<!--- Please answer these questions before creating your pull request. Thanks! ---> 1. Which Jira issue is this PR addressing? Make sure that there is an accompanying issue to your PR. Fixes SNOW-1879403 2. Fill out the following pre-review checklist: - [x] I am adding a new automated test(s) to verify correctness of my new code - [ ] If this test skips Local Testing mode, I'm requesting review from @snowflakedb/local-testing - [ ] I am adding new logging messages - [ ] I am adding a new telemetry message - [ ] I am adding new credentials - [ ] I am adding a new dependency - [ ] If this is a new feature/behavior, I'm adding the Local Testing parity changes. - [x] I acknowledge that I have ensured my changes to be thread-safe. Follow the link for more information: [Thread-safe Developer Guidelines](https://github.com/snowflakedb/snowpark-python/blob/main/CONTRIBUTING.md#thread-safe-development) 3. Please describe how your code solves the related issue. `replace` supports both `Column` and literal objects. The code within the function directly called `lit(...)`, however `lit(...)` can't be called with a `Column` object. Adding if to fix this.
1 parent e40ac0b commit 30e5f9e

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
#### Bug Fixes
5252

5353
- Fixed a bug in local testing mode that caused a column to contain None when it should contain 0
54-
- Fixed a bug in `StructField.from_json` that prevented TimestampTypes with tzinfo from being parsed correctly.
54+
- Fixed a bug in `StructField.from_json` that prevented TimestampTypes with `tzinfo` from being parsed correctly.
5555
- Fixed a bug in function `date_format` that caused an error when the input column was date type or timestamp type.
56+
- Fixed a bug in `replace` when passing `Column` expression objects.
5657

5758
### Snowpark pandas API Updates
5859

src/snowflake/snowpark/functions.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3384,8 +3384,12 @@ def replace(
33843384
if _emit_ast
33853385
else None
33863386
)
3387-
pat = lit(pattern, _emit_ast=_emit_ast)
3388-
rep = lit(replacement, _emit_ast=_emit_ast)
3387+
pat = pattern if isinstance(pattern, Column) else lit(pattern, _emit_ast=_emit_ast)
3388+
rep = (
3389+
replacement
3390+
if isinstance(replacement, Column)
3391+
else lit(replacement, _emit_ast=_emit_ast)
3392+
)
33893393
return builtin(sql_func_name, _ast=ast, _emit_ast=False)(sub, pat, rep)
33903394

33913395

tests/integ/test_dataframe.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4459,3 +4459,23 @@ def test_with_column_keep_column_order(session):
44594459
assert df3.columns == ["A", "B", "C"]
44604460
df3 = df.with_columns(["C", "A"], [lit(0), lit(0)], keep_column_order=True)
44614461
assert df3.columns == ["A", "B", "C"]
4462+
4463+
4464+
@pytest.mark.skipif(
4465+
"config.getoption('local_testing_mode', default=False)",
4466+
reason="replace function is not supported in Local Testing",
4467+
)
4468+
def test_SNOW_1879403_replace_with_lit(session):
4469+
4470+
# TODO SNOW-1880749: support for local testing mode.
4471+
4472+
from snowflake.snowpark.functions import replace
4473+
4474+
df = session.create_dataframe(
4475+
[["apple"], ["apple pie"], ["apple juice"]], schema=["a"]
4476+
)
4477+
ans = df.select(
4478+
replace(col("a"), lit("apple"), lit("orange")).alias("result")
4479+
).collect()
4480+
4481+
Utils.check_answer(ans, [Row("orange"), Row("orange pie"), Row("orange juice")])

0 commit comments

Comments
 (0)