Skip to content

[SPARK-19161][PYTHON][SQL] Improving UDF Docstrings #16534

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 10 additions & 1 deletion python/pyspark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,16 @@ def udf(f=None, returnType=StringType()):
+----------+--------------+------------+
"""
def _udf(f, returnType=StringType()):
return UserDefinedFunction(f, returnType)
udf_obj = UserDefinedFunction(f, returnType)

@functools.wraps(f)
def wrapper(*args):
return udf_obj(*args)

wrapper.func = udf_obj.func
wrapper.returnType = udf_obj.returnType

return wrapper

# decorator @udf, @udf() or @udf(dataType())
if f is None or isinstance(f, (str, DataType)):
Expand Down
25 changes: 15 additions & 10 deletions python/pyspark/sql/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,6 @@ def test_explode(self):
self.assertEqual(result[0][0], "a")
self.assertEqual(result[0][1], "b")

with self.assertRaises(ValueError):
data.select(explode(data.mapfield).alias("a", "b", metadata={'max': 99})).count()

def test_and_in_expression(self):
self.assertEqual(4, self.df.filter((self.df.key <= 10) & (self.df.value <= "2")).count())
self.assertRaises(ValueError, lambda: (self.df.key <= 10) and (self.df.value <= "2"))
Expand Down Expand Up @@ -571,6 +568,21 @@ def as_double(x):
[2, 3.0, "FOO", "foo", "foo", 3, 1.0]
)

def test_udf_wrapper(self):
from pyspark.sql.functions import udf
from pyspark.sql.types import IntegerType

def f(x):
"""Identity"""
return x

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 Expand Up @@ -955,13 +967,6 @@ def test_column_select(self):
self.assertEqual(self.testData, df.select(df.key, df.value).collect())
self.assertEqual([Row(value='1')], df.where(df.key == 1).select(df.value).collect())

def test_column_alias_metadata(self):
df = self.df
df_with_meta = df.select(df.key.alias('pk', metadata={'label': 'Primary Key'}))
self.assertEqual(df_with_meta.schema['pk'].metadata['label'], 'Primary Key')
with self.assertRaises(AssertionError):
df.select(df.key.alias('pk', metdata={'label': 'Primary Key'}))

def test_freqItems(self):
vals = [Row(a=1, b=-2.0) if i % 2 == 0 else Row(a=i, b=i * 1.0) for i in range(100)]
df = self.sc.parallelize(vals).toDF()
Expand Down