Skip to content

Commit 33940bb

Browse files
committed
feat: Add nullifzero function support for local testing
1 parent 7d70182 commit 33940bb

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/snowflake/snowpark/mock/_functions.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,18 @@ def mock_abs(expr):
707707
return abs(expr)
708708

709709

710+
@patch("nullifzero")
711+
def mock_nullifzero(expr: ColumnEmulator) -> ColumnEmulator:
712+
def convert_zero_to_null(value):
713+
if value == 0 or value == 0.0:
714+
return None
715+
return value
716+
717+
result = expr.apply(convert_zero_to_null)
718+
result.sf_type = ColumnType(expr.sf_type.datatype, nullable=True)
719+
return result
720+
721+
710722
@patch("to_decimal")
711723
def mock_to_decimal(
712724
e: ColumnEmulator,

tests/mock/test_functions.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,31 @@ def test_abs(session):
161161
assert origin_df.select(abs(col("m"))).collect() == [Row(1), Row(1), Row(2)]
162162

163163

164+
def test_nullifzero(session):
165+
"""Test nullifzero function converts 0 to NULL while preserving other values."""
166+
from snowflake.snowpark.functions import nullifzero
167+
168+
df = session.create_dataframe(
169+
[
170+
[0],
171+
[1],
172+
[0.0],
173+
[100],
174+
[-5],
175+
],
176+
schema=["value"],
177+
)
178+
result = df.select(nullifzero(col("value")).alias("result")).collect()
179+
180+
assert result == [
181+
Row(RESULT=None),
182+
Row(RESULT=1),
183+
Row(RESULT=None),
184+
Row(RESULT=100),
185+
Row(RESULT=-5),
186+
]
187+
188+
164189
def test_asc_and_desc(session):
165190
origin_df: DataFrame = session.create_dataframe(
166191
[

0 commit comments

Comments
 (0)