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

Fix Column.isin()with empty list #2001

Open
wants to merge 3 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
10 changes: 10 additions & 0 deletions src/snowflake/snowpark/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,16 @@ def validate_value(value_expr: Expression):
for ve in value_expressions:
validate_value(ve)

if len(value_expressions) == 0:
# If `InExpression()` gets passed an empty list as `value_expressions`, it
# produces an invalid (sub-)query with empty brackets, like:
#
# `SELECT ... FROM ... WHERE <column> IN ()`
#
# To avoid this we take the shortcut and return a no-op in case of an empty
# list.
return Column(Literal(None))

return Column(InExpression(self._expression, value_expressions))

def between(
Expand Down
12 changes: 12 additions & 0 deletions tests/integ/test_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,15 @@ def test_column_with_builtins_that_shadow_functions(session):
with pytest.raises(TypeError) as ex_info:
TestData.double1(session).select(sum(col("a"))).collect()
assert iter_error_msg_text in str(ex_info)


def test_return_empty_set_for_empty_filter_isin(session):
"""
Filtering a dataframe with `isin()` and an empty set should yield an empty
dataframe.

NOTE: In SQL an `IN` expression without at least one value is not valid at all.
"""
df1 = session.create_dataframe([1, 2]).to_df("a")
assert df1.filter(df1["a"].isin(None)).collect() == []
assert df1.filter(df1["a"].isin([])).collect() == []
9 changes: 9 additions & 0 deletions tests/mock/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ def test_null_nan_filter(session):
assert res[1] == Row(1.0, 1.0)
assert res[2] == Row(None, None)

# Check `in` expression with empty sets
origin_df: DataFrame = session.create_dataframe([[1], [2]], schema=["a"])

res = origin_df.filter(origin_df["a"].isin(None)).collect()
assert res == []

res = origin_df.filter(origin_df["a"].isin([])).collect()
assert res == []


def test_chain_filter(session):
origin_df: DataFrame = session.create_dataframe(
Expand Down
Loading