Skip to content

Commit

Permalink
[SPARK-16086] [SQL] fix Python UDF without arguments (for 1.6)
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

Fix the bug for Python UDF that does not have any arguments.

## How was this patch tested?

Added regression tests.

Author: Davies Liu <davies.liu@gmail.com>

Closes apache#13793 from davies/fix_no_arguments.

(cherry picked from commit abe36c5)
  • Loading branch information
davies authored and zzcclp committed Jun 21, 2016
1 parent 1d6377d commit 5cfab77
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
5 changes: 5 additions & 0 deletions python/pyspark/sql/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,11 @@ def test_udf2(self):
[res] = self.sqlCtx.sql("SELECT strlen(a) FROM test WHERE strlen(a) > 1").collect()
self.assertEqual(4, res[0])

def test_udf_without_arguments(self):
self.sqlCtx.registerFunction("foo", lambda: "bar")
[row] = self.sqlCtx.sql("SELECT foo()").collect()
self.assertEqual(row[0], "bar")

def test_udf_with_array_type(self):
d = [Row(l=list(range(3)), d={"key": list(range(5))})]
rdd = self.sc.parallelize(d)
Expand Down
9 changes: 3 additions & 6 deletions python/pyspark/sql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,19 +1195,16 @@ def __new__(self, *args, **kwargs):
if args and kwargs:
raise ValueError("Can not use both args "
"and kwargs to create Row")
if args:
# create row class or objects
return tuple.__new__(self, args)

elif kwargs:
if kwargs:
# create row objects
names = sorted(kwargs.keys())
row = tuple.__new__(self, [kwargs[n] for n in names])
row.__fields__ = names
return row

else:
raise ValueError("No args or kwargs")
# create row class or objects
return tuple.__new__(self, args)

def asDict(self, recursive=False):
"""
Expand Down

0 comments on commit 5cfab77

Please sign in to comment.