zero323 commented on a change in pull request #27406: 
[SPARK-30681][PYSPARK][SQL] Add higher order functions API to PySpark
URL: https://github.com/apache/spark/pull/27406#discussion_r373156864
 
 

 ##########
 File path: python/pyspark/sql/functions.py
 ##########
 @@ -2840,6 +2840,359 @@ def from_csv(col, schema, options={}):
     return Column(jc)
 
 
+def _invoke_higher_order_function(name):
+    """
+    Given name of the expression corresponding to a higher order SQL function,
+    return a function that takes
+
+    - a list of columns
+    - a list of functions (*Column) -> Column
+
+    invokes expression, and wraps result in a Column
+    (first Scala one, then Python).
+    """
+
+    def _(cols, funs):
+        sc = SparkContext._active_spark_context
+        expressions = sc._jvm.org.apache.spark.sql.catalyst.expressions
+        expr = getattr(expressions, name)
+
+        jcols = [_to_java_column(col).expr() for col in cols]
+        jfuns = [_create_lambda(f) for f in funs]
+
+        return Column(sc._jvm.Column(expr(*jcols + jfuns)))
+
+    return _
+
+
+@since(3.0)
+def transform(col, f):
+    """
+    Returns an array of elements after applying a transformation to each 
element in the input array.
+
+    :param col: name of column or expression
+    :param f: a function that is applied to each element of the input array.
+        Can take one of the following forms:
+
+        - Unary ``(x: Column) -> Column: ...``
+        - Binary ``(x: Column, i: Column) -> Column...``, where the second 
argument is
+            a 0-based index of the element.
+
+        and an use methods of :class:`pyspark.sql.Column`, functions defined in
+        :py:mod:`pyspark.sql.functions` and Scala ``UserDefinedFunctions``.
+        Python ``UserDefinedFunctions`` are not supported
+        (`SPARK-27052 <https://issues.apache.org/jira/browse/SPARK-27052>`__).
 
 Review comment:
   If SPARK-27052 is won't fix, we could be more proactive and inspect the 
closure here:
   
   ```python
   import inspect
   from itertools import chain
   from pyspark.sql.udf import UserDefinedFunction
   
   
   def _is_udf(f):
       return isinstance(f, UserDefinedFunction) or (
           hasattr(f, "returnType")
           and hasattr(f, "evalType")
           and f.__closure__
           and isinstance(f.__closure__[0].cell_contents, UserDefinedFunction)
       )
   
   
   def _check_conatins_udf(f):
       closurevars = inspect.getclosurevars(f)
       for name, f in chain.from_iterable(
           [closurevars.nonlocals.items(), closurevars.globals.items()]
       ):
           if _is_udf(f):
   
               raise ValueError(
                   "Higher order functions cannot use UserDefinedFunctions ",
                   "Detected following udf {}: {}".format(name, f),
               )
   ```
   
   or throw an exception at the analyzer level

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to