Skip to content

Commit 509747c

Browse files
committed
Throws a correct exception for in operator against column
1 parent db0ddce commit 509747c

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

python/pyspark/sql/column.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ def __init__(self, jc):
180180
__ror__ = _bin_op("or")
181181

182182
# container operators
183-
__contains__ = _bin_op("contains")
183+
def __contains__(self, item):
184+
raise ValueError("Cannot apply 'in' operator against a column: please use 'contains' "
185+
"in a string column or 'array_contains' function for an array column.")
184186

185187
# bitwise operators
186188
bitwiseOR = _bin_op("bitwiseOR")

python/pyspark/sql/tests.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,9 @@ def test_column_operators(self):
967967
cs.startswith('a'), cs.endswith('a')
968968
self.assertTrue(all(isinstance(c, Column) for c in css))
969969
self.assertTrue(isinstance(ci.cast(LongType()), Column))
970+
self.assertRaisesRegexp(ValueError,
971+
"Cannot apply 'in' operator against a column",
972+
lambda: 1 in cs)
970973

971974
def test_column_getitem(self):
972975
from pyspark.sql.functions import col

0 commit comments

Comments
 (0)