Skip to content

Commit f70c08e

Browse files
author
Davies Liu
committed
raise Exception if column is used in booelan expression
1 parent a803118 commit f70c08e

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

python/pyspark/sql/column.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ def over(self, window):
396396
jc = self._jc.over(window._jspec)
397397
return Column(jc)
398398

399+
def __nonzero__(self):
400+
raise ValueError("Can't convert column into bool: please use '&' for 'and', '|' for 'or', "
401+
"when using Column in a boolean expression.")
402+
__bool__ = __nonzero__
403+
399404
def __repr__(self):
400405
return 'Column<%s>' % self._jc.toString().encode('utf8')
401406

python/pyspark/sql/tests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,13 @@ def test_explode(self):
164164
self.assertEqual(result[0][0], "a")
165165
self.assertEqual(result[0][1], "b")
166166

167+
def test_and_in_expression(self):
168+
self.assertEqual(4, self.df.filter(self.df.key <= 10 & self.df.value <= "2").count())
169+
self.assertRaises(ValueError, lambda: self.df.key <= 10 & self.df.value <= "2")
170+
self.assertEqual(2, self.df.filter(self.df.key <= 3 | self.df.value < "2").count())
171+
self.assertRaises(ValueError,
172+
lambda: self.df.filter(self.df.key <= 3 | self.df.value < "2").count())
173+
167174
def test_udf_with_callable(self):
168175
d = [Row(number=i, squared=i**2) for i in range(10)]
169176
rdd = self.sc.parallelize(d)

0 commit comments

Comments
 (0)