Repository: spark
Updated Branches:
  refs/heads/master d96d7b557 -> 7fb5ae502


[SPARK-8573] [SPARK-8568] [SQL] [PYSPARK] raise Exception if column is used in 
booelan expression

It's a common mistake that user will put Column in a boolean expression 
(together with `and` , `or`), which does not work as expected, we should raise 
a exception in that case, and suggest user to use `&`, `|` instead.

Author: Davies Liu <dav...@databricks.com>

Closes #6961 from davies/column_bool and squashes the following commits:

9f19beb [Davies Liu] update message
af74bd6 [Davies Liu] fix tests
07dff84 [Davies Liu] address comments, fix tests
f70c08e [Davies Liu] raise Exception if column is used in booelan expression


Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/7fb5ae50
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/7fb5ae50
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/7fb5ae50

Branch: refs/heads/master
Commit: 7fb5ae5024284593204779ff463bfbdb4d1c6da5
Parents: d96d7b5
Author: Davies Liu <dav...@databricks.com>
Authored: Tue Jun 23 15:51:16 2015 -0700
Committer: Davies Liu <dav...@databricks.com>
Committed: Tue Jun 23 15:51:16 2015 -0700

----------------------------------------------------------------------
 python/pyspark/sql/column.py |  5 +++++
 python/pyspark/sql/tests.py  | 10 +++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/spark/blob/7fb5ae50/python/pyspark/sql/column.py
----------------------------------------------------------------------
diff --git a/python/pyspark/sql/column.py b/python/pyspark/sql/column.py
index 1ecec5b..0a85da7 100644
--- a/python/pyspark/sql/column.py
+++ b/python/pyspark/sql/column.py
@@ -396,6 +396,11 @@ class Column(object):
         jc = self._jc.over(window._jspec)
         return Column(jc)
 
+    def __nonzero__(self):
+        raise ValueError("Cannot convert column into bool: please use '&' for 
'and', '|' for 'or', "
+                         "'~' for 'not' when building DataFrame boolean 
expressions.")
+    __bool__ = __nonzero__
+
     def __repr__(self):
         return 'Column<%s>' % self._jc.toString().encode('utf8')
 

http://git-wip-us.apache.org/repos/asf/spark/blob/7fb5ae50/python/pyspark/sql/tests.py
----------------------------------------------------------------------
diff --git a/python/pyspark/sql/tests.py b/python/pyspark/sql/tests.py
index 13f4556..e6a434e 100644
--- a/python/pyspark/sql/tests.py
+++ b/python/pyspark/sql/tests.py
@@ -164,6 +164,14 @@ class SQLTests(ReusedPySparkTestCase):
         self.assertEqual(result[0][0], "a")
         self.assertEqual(result[0][1], "b")
 
+    def test_and_in_expression(self):
+        self.assertEqual(4, self.df.filter((self.df.key <= 10) & 
(self.df.value <= "2")).count())
+        self.assertRaises(ValueError, lambda: (self.df.key <= 10) and 
(self.df.value <= "2"))
+        self.assertEqual(14, self.df.filter((self.df.key <= 3) | 
(self.df.value < "2")).count())
+        self.assertRaises(ValueError, lambda: self.df.key <= 3 or 
self.df.value < "2")
+        self.assertEqual(99, self.df.filter(~(self.df.key == 1)).count())
+        self.assertRaises(ValueError, lambda: not self.df.key == 1)
+
     def test_udf_with_callable(self):
         d = [Row(number=i, squared=i**2) for i in range(10)]
         rdd = self.sc.parallelize(d)
@@ -408,7 +416,7 @@ class SQLTests(ReusedPySparkTestCase):
         self.assertTrue(isinstance((- ci - 1 - 2) % 3 * 2.5 / 3.5, Column))
         rcc = (1 + ci), (1 - ci), (1 * ci), (1 / ci), (1 % ci)
         self.assertTrue(all(isinstance(c, Column) for c in rcc))
-        cb = [ci == 5, ci != 0, ci > 3, ci < 4, ci >= 0, ci <= 7, ci and cs, 
ci or cs]
+        cb = [ci == 5, ci != 0, ci > 3, ci < 4, ci >= 0, ci <= 7]
         self.assertTrue(all(isinstance(c, Column) for c in cb))
         cbool = (ci & ci), (ci | ci), (~ci)
         self.assertTrue(all(isinstance(c, Column) for c in cbool))


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to