You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/12/02 07:30:16 UTC

[GitHub] [spark] grundprinzip commented on a diff in pull request #38872: [SPARK-41357][CONNECT][PYTHON] Implement math functions

grundprinzip commented on code in PR #38872:
URL: https://github.com/apache/spark/pull/38872#discussion_r1037855980


##########
python/pyspark/sql/connect/functions.py:
##########
@@ -14,29 +14,81 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-from pyspark.sql.connect.column import Column, LiteralExpression, ColumnReference
+from pyspark.sql.connect.column import (
+    Column,
+    Expression,
+    LiteralExpression,
+    ColumnReference,
+    UnresolvedFunction,
+)
 
-from typing import Any, TYPE_CHECKING
+from typing import Any, TYPE_CHECKING, Union, List
 
 if TYPE_CHECKING:
     from pyspark.sql.connect._typing import ColumnOrName
 
+
 # TODO(SPARK-40538) Add support for the missing PySpark functions.
 
 
 def _to_col(col: "ColumnOrName") -> Column:
     return col if isinstance(col, Column) else column(col)
 
 
-def col(x: str) -> Column:
-    return Column(ColumnReference(x))
+def _invoke_function(name: str, *args: Union[Column, Expression]) -> Column:
+    """
+    Simple wrapper function that converts the arguments into the appropriate types.
+    Parameters
+    ----------
+    name Name of the function to be called.
+    args The list of arguments.
+
+    Returns
+    -------
+    :class:`UnresolvedFunction`
+    """
+    expressions: List[Expression] = []
+    for arg in args:
+        assert isinstance(arg, (Column, Expression))
+        if isinstance(arg, Column):
+            expressions.append(arg._expr)
+        else:
+            expressions.append(arg)
+    return Column(UnresolvedFunction(name, expressions))
+
+
+def _invoke_function_over_columns(name: str, *cols: "ColumnOrName") -> Column:
+    """
+    Invokes n-ary JVM function identified by name
+    and wraps the result with :class:`~pyspark.sql.Column`.
+    """
+    _cols = [_to_col(c) for c in cols]
+    return _invoke_function(name, *_cols)
+
+
+def _invoke_binary_math_function(name: str, col1: Any, col2: Any) -> Column:
+    """
+    Invokes binary JVM math function identified by name

Review Comment:
   Maybe we can slightly adjust the comment here :)



##########
python/pyspark/sql/connect/functions.py:
##########
@@ -14,29 +14,81 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-from pyspark.sql.connect.column import Column, LiteralExpression, ColumnReference
+from pyspark.sql.connect.column import (
+    Column,
+    Expression,
+    LiteralExpression,
+    ColumnReference,
+    UnresolvedFunction,
+)
 
-from typing import Any, TYPE_CHECKING
+from typing import Any, TYPE_CHECKING, Union, List
 
 if TYPE_CHECKING:
     from pyspark.sql.connect._typing import ColumnOrName
 
+
 # TODO(SPARK-40538) Add support for the missing PySpark functions.
 
 
 def _to_col(col: "ColumnOrName") -> Column:
     return col if isinstance(col, Column) else column(col)
 
 
-def col(x: str) -> Column:
-    return Column(ColumnReference(x))
+def _invoke_function(name: str, *args: Union[Column, Expression]) -> Column:
+    """
+    Simple wrapper function that converts the arguments into the appropriate types.
+    Parameters
+    ----------
+    name Name of the function to be called.
+    args The list of arguments.
+
+    Returns
+    -------
+    :class:`UnresolvedFunction`
+    """
+    expressions: List[Expression] = []
+    for arg in args:
+        assert isinstance(arg, (Column, Expression))
+        if isinstance(arg, Column):
+            expressions.append(arg._expr)
+        else:
+            expressions.append(arg)
+    return Column(UnresolvedFunction(name, expressions))
+
+
+def _invoke_function_over_columns(name: str, *cols: "ColumnOrName") -> Column:
+    """
+    Invokes n-ary JVM function identified by name

Review Comment:
   ditto



-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org