Github user zero323 commented on a diff in the pull request:
https://github.com/apache/spark/pull/16533#discussion_r97957210
--- Diff: python/pyspark/sql/functions.py ---
@@ -1862,9 +1862,33 @@ def udf(f, returnType=StringType()):
>>> slen = udf(lambda s: len(s), IntegerType())
>>> df.select(slen(df.name).alias('slen')).collect()
[Row(slen=5), Row(slen=3)]
+ >>> @udf
+ ... def to_upper(s):
+ ... if s is not None:
+ ... return s.upper()
+ ...
+ >>> @udf(returnType=IntegerType())
+ ... def add_one(x):
+ ... if x is not None:
+ ... return x + 1
+ ...
"""
return UserDefinedFunction(f, returnType)
+
[email protected](_udf)
+def udf(f=None, returnType=StringType()):
+ """A decorator version of pyspark.sql.functions.udf
+ """
+ if f is None or isinstance(f, DataType):
--- End diff --
Do you mean something like this?
```python
@functools.wraps(_udf)
def udf(f=None, returnType=StringType()):
"""A decorator version of pyspark.sql.functions.udf
"""
if (
# Decorator as @udf() or @udf
f is None
# Decorator with positional DataType @udf(IntegerType())
or isinstance(f, DataType)
):
...
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]