{"action":"created","issue":{"url":"https://api.github.com/repos/apache/spark/issues/19027","repository_url":"https://api.github.com/repos/apache/spark","labels_url":"https://api.github.com/repos/apache/spark/issues/19027/labels{/name}","comments_url":"https://api.github.com/repos/apache/spark/issues/19027/comments","events_url":"https://api.github.com/repos/apache/spark/issues/19027/events","html_url":"https://github.com/apache/spark/pull/19027","id":252215547,"number":19027,"title":"[SPARK-19165][PYTHON][SQL] PySpark APIs using columns as arguments should validate input types for column","user":{"login":"HyukjinKwon","id":6477701,"avatar_url":"https://avatars0.githubusercontent.com/u/6477701?v=4","gravatar_id":"","url":"https://api.github.com/users/HyukjinKwon","html_url":"https://github.com/HyukjinKwon","followers_url":"https://api.github.com/users/HyukjinKwon/followers","following_url":"https://api.github.com/users/HyukjinKwon/following{/other_user}","gists_url":"https://api.github.com/users/HyukjinKwon/gists{/gist_id}","starred_url":"https://api.github.com/users/HyukjinKwon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HyukjinKwon/subscriptions","organizations_url":"https://api.github.com/users/HyukjinKwon/orgs","repos_url":"https://api.github.com/users/HyukjinKwon/repos","events_url":"https://api.github.com/users/HyukjinKwon/events{/privacy}","received_events_url":"https://api.github.com/users/HyukjinKwon/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2017-08-23T09:21:15Z","updated_at":"2017-08-23T23:44:04Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/19027","html_url":"https://github.com/apache/spark/pull/19027","diff_url":"https://github.com/apache/spark/pull/19027.diff","patch_url":"https://github.com/apache/spark/pull/19027.patch"},"body":"## What changes were proposed in this pull request?\r\n\r\nWhile preparing to take over https://github.com/apache/spark/pull/16537, I realised a (I think) better approach to make the exception handling in one point.\r\n\r\nThis PR proposes to fix `_to_java_column` in `pyspark.sql.column`, which most of functions in `functions.py` and some other APIs use. This `_to_java_column` basically looks not working with other types than `pyspark.sql.column.Column` or string (`str` and `unicode`). \r\n\r\nIf this is not `Column`, then it calls `_create_column_from_name` which calls `functions.col` within JVM:\r\n\r\nhttps://github.com/apache/spark/blob/42b9eda80e975d970c3e8da4047b318b83dd269f/sql/core/src/main/scala/org/apache/spark/sql/functions.scala#L76\r\n\r\nAnd it looks we only have `String` one with `col`.\r\n\r\nSo, these should work:\r\n\r\n```python\r\n>>> from pyspark.sql.column import _to_java_column, Column\r\n>>> _to_java_column(\"a\")\r\nJavaObject id=o28\r\n>>> _to_java_column(u\"a\")\r\nJavaObject id=o29\r\n>>> _to_java_column(spark.range(1).id)\r\nJavaObject id=o33\r\n```\r\n\r\nwhereas these do not:\r\n\r\n```python\r\n>>> _to_java_column(1)\r\n```\r\n```\r\n...\r\npy4j.protocol.Py4JError: An error occurred while calling z:org.apache.spark.sql.functions.col. Trace:\r\npy4j.Py4JException: Method col([class java.lang.Integer]) does not exist\r\n    ...\r\n```\r\n\r\n```python\r\n>>> _to_java_column([])\r\n```\r\n```\r\n...\r\npy4j.protocol.Py4JError: An error occurred while calling z:org.apache.spark.sql.functions.col. Trace:\r\npy4j.Py4JException: Method col([class java.util.ArrayList]) does not exist\r\n    ...\r\n```\r\n\r\n```python\r\n>>> class A(): pass\r\n>>> _to_java_column(A())\r\n```\r\n```\r\n...\r\nAttributeError: 'A' object has no attribute '_get_object_id'\r\n```\r\n\r\nMeaning most of functions using `_to_java_column` such as `udf` or `to_json` or some other APIs throw an exception as below:\r\n\r\n```python\r\n>>> from pyspark.sql.functions import udf\r\n>>> udf(lambda x: x)(None)\r\n```\r\n\r\n```\r\n...\r\npy4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.sql.functions.col.\r\n: java.lang.NullPointerException\r\n    ...\r\n```\r\n\r\n```python\r\n>>> from pyspark.sql.functions import to_json\r\n>>> to_json(None)\r\n```\r\n\r\n```\r\n...\r\npy4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.sql.functions.col.\r\n: java.lang.NullPointerException\r\n    ...\r\n```\r\n\r\n**After this PR**:\r\n\r\n```python\r\n>>> from pyspark.sql.functions import udf\r\n>>> udf(lambda x: x)(None)\r\n...\r\n```\r\n\r\n```\r\nTypeError: Invalid argument, not a string or column: None of type <type 'NoneType'>. For column literals, use 'lit', 'array', 'struct' or 'create_map' functions.\r\n```\r\n\r\n```python\r\n>>> from pyspark.sql.functions import to_json\r\n>>> to_json(None)\r\n```\r\n\r\n```\r\n...\r\nTypeError: Invalid argument, not a string or column: None of type <type 'NoneType'>. For column literals, use 'lit', 'array', 'struct' or 'create_map' functions.\r\n```\r\n\r\n## How was this patch tested?\r\n\r\nUnit tests added in `python/pyspark/sql/tests.py` and manual tests."},"comment":{"url":"https://api.github.com/repos/apache/spark/issues/comments/324492769","html_url":"https://github.com/apache/spark/pull/19027#issuecomment-324492769","issue_url":"https://api.github.com/repos/apache/spark/issues/19027","id":324492769,"user":{"login":"AmplabJenkins","id":4031291,"avatar_url":"https://avatars3.githubusercontent.com/u/4031291?v=4","gravatar_id":"","url":"https://api.github.com/users/AmplabJenkins","html_url":"https://github.com/AmplabJenkins","followers_url":"https://api.github.com/users/AmplabJenkins/followers","following_url":"https://api.github.com/users/AmplabJenkins/following{/other_user}","gists_url":"https://api.github.com/users/AmplabJenkins/gists{/gist_id}","starred_url":"https://api.github.com/users/AmplabJenkins/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AmplabJenkins/subscriptions","organizations_url":"https://api.github.com/users/AmplabJenkins/orgs","repos_url":"https://api.github.com/users/AmplabJenkins/repos","events_url":"https://api.github.com/users/AmplabJenkins/events{/privacy}","received_events_url":"https://api.github.com/users/AmplabJenkins/received_events","type":"User","site_admin":false},"created_at":"2017-08-23T23:44:04Z","updated_at":"2017-08-23T23:44:04Z","body":"Merged build finished. Test FAILed."}}
{"actor":{"display_login":"AmplabJenkins"}}