You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by HyukjinKwon <gi...@git.apache.org> on 2017/03/04 03:57:08 UTC

[GitHub] spark pull request #17160: [SPARK-19701][SQL][PYTHON] Throws a correct excep...

GitHub user HyukjinKwon opened a pull request:

    https://github.com/apache/spark/pull/17160

    [SPARK-19701][SQL][PYTHON] Throws a correct exception for 'in' operator against column

    ## What changes were proposed in this pull request?
    
    This PR proposes to remove incorrect implementation that has been not executed so far (at least from Spark 1.5.2) for `in` operator and throw a correct exception rather than saying it is a bool. I tested the codes above in 1.5.2, 1.6.3, 2.1.0 and in the master branch as below:
    
    **1.5.2**
    
    ```python
    >>> df = sqlContext.createDataFrame([[1]])
    >>> 1 in df._1
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File ".../spark-1.5.2-bin-hadoop2.6/python/pyspark/sql/column.py", line 418, in __nonzero__
        raise ValueError("Cannot convert column into bool: please use '&' for 'and', '|' for 'or', "
    ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
    ```
    
    **1.6.3**
    
    ```python
    >>> 1 in sqlContext.range(1).id
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File ".../spark-1.6.3-bin-hadoop2.6/python/pyspark/sql/column.py", line 447, in __nonzero__
        raise ValueError("Cannot convert column into bool: please use '&' for 'and', '|' for 'or', "
    ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
    ```
    
    **2.1.0**
    
    ```python
    >>> 1 in spark.range(1).id
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File ".../spark-2.1.0-bin-hadoop2.7/python/pyspark/sql/column.py", line 426, in __nonzero__
        raise ValueError("Cannot convert column into bool: please use '&' for 'and', '|' for 'or', "
    ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
    ```
    
    **Current Master**
    
    ```python
    >>> 1 in spark.range(1).id
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File ".../spark/python/pyspark/sql/column.py", line 452, in __nonzero__
        raise ValueError("Cannot convert column into bool: please use '&' for 'and', '|' for 'or', "
    ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
    ```
    
    **After**
    
    ```python
    >>> 1 in spark.range(1).id
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File ".../spark/python/pyspark/sql/column.py", line 184, in __contains__
        raise ValueError("Cannot apply 'in' operator against a column: please use 'contains' "
    ValueError: Cannot apply 'in' operator against a column: please use 'contains' in a string column or 'array_contains' function for an array column.
    ```
    
    In more details,
    
    It seems the implementation indented to support this
    
    ```python
    1 in df.column
    ```
    
    However, currently, it throws an exception as below:
    
    ```python
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File ".../spark/python/pyspark/sql/column.py", line 426, in __nonzero__
        raise ValueError("Cannot convert column into bool: please use '&' for 'and', '|' for 'or', "
    ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
    ```
    
    What happens here is as below:
    
    ```python
    class Column(object):
        def __contains__(self, item):
            print "I am contains"
            return Column()
        def __nonzero__(self):
            raise Exception("I am nonzero.")
    
    >>> 1 in Column()
    I am contains
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 6, in __nonzero__
    Exception: I am nonzero.
    ```
    
    It seems it calls `__contains__` first and then `__nonzero__` or `__bool__` is being called against `Column()` to make this a bool (or int to be specific).
    
    It seems `__nonzero__` (for Python 2), `__bool__` (for Python 3) and `__contains__` forcing the the return into a bool unlike other operators. There are few references about this as below:
    
    http://stackoverflow.com/questions/12244074/python-source-code-for-built-in-in-operator/12244378#12244378
    http://stackoverflow.com/questions/38542543/functionality-of-python-in-vs-contains/38542777
    
    It seems we can't overwrite `__nonzero__` or `__bool__` as a workaround to make this working because these force the return type as a bool as below:
    
    ```python
    class Column(object):
        def __contains__(self, item):
            print "I am contains"
            return Column()
        def __nonzero__(self):
            return "a"
    
    >>> 1 in Column()
    I am contains
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: __nonzero__ should return bool or int, returned str
    ```
    
    ## How was this patch tested?
    
    Added unit tests in `tests.py`.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/HyukjinKwon/spark SPARK-19701

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/17160.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #17160
    
----
commit 509747ca82aecd07541cc5b0c06fd5f4c147c3cf
Author: hyukjinkwon <gu...@gmail.com>
Date:   2017-03-04T03:37:48Z

    Throws a correct exception for in operator against column

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17160: [SPARK-19701][SQL][PYTHON] Throws a correct excep...

Posted by binhnguyen1512 <gi...@git.apache.org>.
Github user binhnguyen1512 commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17160#discussion_r104281624
  
    --- Diff: python/pyspark/sql/column.py ---
    @@ -180,7 +180,9 @@ def __init__(self, jc):
         __ror__ = _bin_op("or")
     
         # container operators
    -    __contains__ = _bin_op("contains")
    +    def __contains__(self, item):
    +        raise ValueError("Cannot apply 'in' operator against a column: please use 'contains' "
    +                         "in a string column or 'array_contains' function for an array column.")
    --- End diff --
    
    Binhnguyen1512


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by HyukjinKwon <gi...@git.apache.org>.
Github user HyukjinKwon commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    **without `__contains__`, `__nonzero__` and `__bool__`**
    
    ```python
    class Column(object): pass
    
    >>> 1 in Column()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: argument of type 'Column' is not iterable
    >>> bool(Column())
    True
    ```
    
    **without `__nonzero__` and `__bool__`**
    
    ```python
    class Column(object):
        def __contains__(self, item):
            print "I am contains"
            return Column()
    
    >>> 1 in Column()
    I am contains
    True
    >>> bool(Column())
    True
    ```
    
    
    **without `__contains__`**
    
    ```python
    class Column(object):
        def __nonzero__(self):
            print "I am nonzero"
            return Column()
    
    >>> 1 in Column()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: argument of type 'Column' is not iterable
    >>> bool(Column())
    I am nonzero
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: __nonzero__ should return bool or int, returned Column
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by HyukjinKwon <gi...@git.apache.org>.
Github user HyukjinKwon commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    It does not need for `__contains__` but need for `bool` because I guess we would not want to return bool as other operators return `Column`.
    
    For example,
    
    ```python
    >>> not spark.range(1).id
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File ".../spark/python/pyspark/sql/column.py", line 452, in __nonzero__
        raise ValueError("Cannot convert column into bool: please use '&' for 'and', '|' for 'or', "
    ValueError: Cannot convert column into bool: please use '&' for 'and', '|' for 'or', '~' for 'not' when building DataFrame boolean expressions.
    ```
    
    If we remove this, then
    
    ```
    >>> not spark.range(1).id
    False
    ```
    
    I think we want `Column` as below:
    
    ```python
    >>> 1 < spark.range(1).id
    Column<(id > 1)>
    ```
    
    but for `bool`, it seems not.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    so do we still need `__non_zero__`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    **[Test build #73891 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/73891/testReport)** for PR 17160 at commit [`509747c`](https://github.com/apache/spark/commit/509747ca82aecd07541cc5b0c06fd5f4c147c3cf).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17160: [SPARK-19701][SQL][PYTHON] Throws a correct excep...

Posted by binhnguyen1512 <gi...@git.apache.org>.
Github user binhnguyen1512 commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17160#discussion_r104281627
  
    --- Diff: python/pyspark/sql/column.py ---
    @@ -180,7 +180,9 @@ def __init__(self, jc):
         __ror__ = _bin_op("or")
     
         # container operators
    -    __contains__ = _bin_op("contains")
    +    def __contains__(self, item):
    +        raise ValueError("Cannot apply 'in' operator against a column: please use 'contains' "
    +                         "in a string column or 'array_contains' function for an array column.")
    --- End diff --
    
    Binhnguyen1512


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17160: [SPARK-19701][SQL][PYTHON] Throws a correct excep...

Posted by HyukjinKwon <gi...@git.apache.org>.
Github user HyukjinKwon commented on a diff in the pull request:

    https://github.com/apache/spark/pull/17160#discussion_r104278452
  
    --- Diff: python/pyspark/sql/column.py ---
    @@ -180,7 +180,9 @@ def __init__(self, jc):
         __ror__ = _bin_op("or")
     
         # container operators
    -    __contains__ = _bin_op("contains")
    +    def __contains__(self, item):
    +        raise ValueError("Cannot apply 'in' operator against a column: please use 'contains' "
    +                         "in a string column or 'array_contains' function for an array column.")
    --- End diff --
    
    What I meant here is use
    
    ```python
    >>> df = spark.range(1)
    >>> df.select(df.id.contains(0)).show()
    ```
    ```
    +---------------+
    |contains(id, 0)|
    +---------------+
    |           true|
    +---------------+
    ```
    
    or
    
    ```python
    >>> from pyspark.sql.functions import array_contains
    >>> df = spark.createDataFrame([[[0]]], ["id"])
    >>> df.select(array_contains(df.id, 0)).show()
    ```
    ```
    +---------------------+
    |array_contains(id, 0)|
    +---------------------+
    |                 true|
    +---------------------+
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    **[Test build #73891 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/73891/testReport)** for PR 17160 at commit [`509747c`](https://github.com/apache/spark/commit/509747ca82aecd07541cc5b0c06fd5f4c147c3cf).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    what if we just remove `__contains__`, `__nonzero__ ` and `__bool__`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #17160: [SPARK-19701][SQL][PYTHON] Throws a correct excep...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/spark/pull/17160


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/73891/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by HyukjinKwon <gi...@git.apache.org>.
Github user HyukjinKwon commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    cc @cloud-fan, @davies and @holdenk.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by HyukjinKwon <gi...@git.apache.org>.
Github user HyukjinKwon commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    @cloud-fan Thank you sincerely so much for looking into this deeper and asking the details.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    thanks, merging to master!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #17160: [SPARK-19701][SQL][PYTHON] Throws a correct exception fo...

Posted by HyukjinKwon <gi...@git.apache.org>.
Github user HyukjinKwon commented on the issue:

    https://github.com/apache/spark/pull/17160
  
    So.. it seems 
    
    ```python
    TypeError: argument of type 'int' is not iterable
    ```
    
    vs
    
    ```python
    ValueError: Cannot apply 'in' operator against a column: please use 'contains' in a string column or 'array_contains' function for an array column.
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org