Skip to content

Commit

Permalink
UserDefinedFunction should validate that func is callable
Browse files Browse the repository at this point in the history
  • Loading branch information
zero323 committed Feb 13, 2017
1 parent ab88b24 commit 7d350bf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions python/pyspark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,11 @@ class UserDefinedFunction(object):
.. versionadded:: 1.3
"""
def __init__(self, func, returnType, name=None):
if not callable(func):
raise TypeError(
"Not a function or callable (__call__ is not defined): "
"{0}".format(type(func)))

self.func = func
self.returnType = (
returnType if isinstance(returnType, DataType)
Expand Down
7 changes: 7 additions & 0 deletions python/pyspark/sql/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ def test_udf_with_string_return_type(self):

self.assertTupleEqual(expected, actual)

def test_udf_shouldnt_accept_noncallable_object(self):
from pyspark.sql.functions import UserDefinedFunction
from pyspark.sql.types import StringType

non_callable = None
self.assertRaises(TypeError, UserDefinedFunction, non_callable, StringType())

def test_basic_functions(self):
rdd = self.sc.parallelize(['{"foo":"bar"}', '{"foo":"baz"}'])
df = self.spark.read.json(rdd)
Expand Down

0 comments on commit 7d350bf

Please sign in to comment.