Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-21394][SPARK-21432][PYTHON] Reviving callable object/partial function support in UDF in PySpark #18615

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion python/pyspark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2087,10 +2087,13 @@ def _wrapped(self):
"""
Wrap this udf with a function and attach docstring from func
"""
@functools.wraps(self.func)
assignments = tuple(a for a in functools.WRAPPER_ASSIGNMENTS if a != "__name__")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put a comment here saying what this is for just so that when we see this next year we don't forget why we stripped out name from the things we asked functools to assign.


@functools.wraps(self.func, assigned=assignments)
def wrapper(*args):
return self(*args)

wrapper.__name__ = self._name
wrapper.func = self.func
wrapper.returnType = self.returnType

Expand Down
13 changes: 13 additions & 0 deletions python/pyspark/sql/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,19 @@ def f(x):
self.assertEqual(f, f_.func)
self.assertEqual(return_type, f_.returnType)

class F(object):
"""Identity"""
def __call__(self, x):
return x

f = F()
return_type = IntegerType()
f_ = udf(f, return_type)

self.assertTrue(f.__doc__ in f_.__doc__)
self.assertEqual(f, f_.func)
self.assertEqual(return_type, f_.returnType)

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