You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "ueshin (via GitHub)" <gi...@apache.org> on 2023/08/22 21:44:50 UTC
[GitHub] [spark] ueshin opened a new pull request, #42617: [SPARK-44918][SQL][PYTHON] Support named arguments in scalar Python/Pandas UDFs
ueshin opened a new pull request, #42617:
URL: https://github.com/apache/spark/pull/42617
### What changes were proposed in this pull request?
Supports named arguments in scalar Python/Pandas UDF.
For example:
```py
>>> @udf("int")
... def test_udf(a, b):
... return a + 10 * b
...
>>> spark.udf.register("test_udf", test_udf)
>>> spark.range(2).select(test_udf(b=col("id") * 10, a=col("id"))).show()
+---------------------------------+
|test_udf(b => (id * 10), a => id)|
+---------------------------------+
| 0|
| 101|
+---------------------------------+
>>> spark.sql("SELECT test_udf(b => id * 10, a => id) FROM range(2)").show()
+---------------------------------+
|test_udf(b => (id * 10), a => id)|
+---------------------------------+
| 0|
| 101|
+---------------------------------+
```
or:
```py
>>> @pandas_udf("int")
... def test_udf(a, b):
... return a + 10 * b
...
>>> spark.udf.register("test_udf", test_udf)
>>> spark.range(2).select(test_udf(b=col("id") * 10, a=col("id"))).show()
+---------------------------------+
|test_udf(b => (id * 10), a => id)|
+---------------------------------+
| 0|
| 101|
+---------------------------------+
>>> spark.sql("SELECT test_udf(b => id * 10, a => id) FROM range(2)").show()
+---------------------------------+
|test_udf(b => (id * 10), a => id)|
+---------------------------------+
| 0|
| 101|
+---------------------------------+
```
### Why are the changes needed?
Now that named arguments support was added (https://github.com/apache/spark/pull/41796, https://github.com/apache/spark/pull/42020).
Scalar Python/Pandas UDFs can support it.
### Does this PR introduce _any_ user-facing change?
Yes, named arguments will be available for scalar Python/Pandas UDFs.
### How was this patch tested?
Added related tests.
### Was this patch authored or co-authored using generative AI tooling?
No.
--
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
[GitHub] [spark] ueshin closed pull request #42617: [SPARK-44918][SQL][PYTHON] Support named arguments in scalar Python/Pandas UDFs
Posted by "ueshin (via GitHub)" <gi...@apache.org>.
ueshin closed pull request #42617: [SPARK-44918][SQL][PYTHON] Support named arguments in scalar Python/Pandas UDFs
URL: https://github.com/apache/spark/pull/42617
--
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
[GitHub] [spark] dtenedor commented on a diff in pull request #42617: [SPARK-44918][SQL][PYTHON] Support named arguments in scalar Python/Pandas UDFs
Posted by "dtenedor (via GitHub)" <gi...@apache.org>.
dtenedor commented on code in PR #42617:
URL: https://github.com/apache/spark/pull/42617#discussion_r1302322467
##########
python/pyspark/sql/tests/pandas/test_pandas_udf_scalar.py:
##########
@@ -1467,6 +1467,96 @@ def udf(x):
finally:
shutil.rmtree(path)
+ def test_named_arguments(self):
+ @pandas_udf("int")
+ def test_udf(a, b):
+ return a + 10 * b
+
+ self.spark.udf.register("test_udf", test_udf)
+
+ for i, df in enumerate(
+ [
+ self.spark.range(2).select(test_udf(a=col("id"), b=col("id") * 10)),
+ self.spark.range(2).select(test_udf(b=col("id") * 10, a=col("id"))),
+ self.spark.sql("SELECT test_udf(a => id, b => id * 10) FROM range(2)"),
Review Comment:
can we also have a test case with a positional argument first and then a named argument after? We can have a positive version of this test case where the positional argument maps to the first argument of the UDF and the named argument refers to the second, and another negative version where the named argument uses the same name as the first (positional) argument? Same for `test_udf.py`?
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/UserDefinedPythonFunction.scala:
##########
@@ -50,6 +50,19 @@ case class UserDefinedPythonFunction(
udfDeterministic: Boolean) {
def builder(e: Seq[Expression]): Expression = {
+ if (pythonEvalType == PythonEvalType.SQL_BATCHED_UDF
+ || pythonEvalType ==PythonEvalType.SQL_ARROW_BATCHED_UDF
+ || pythonEvalType == PythonEvalType.SQL_SCALAR_PANDAS_UDF) {
+ /*
+ * Check if the named arguments:
+ * - don't have duplicated names
+ * - don't contain positional arguments
Review Comment:
```suggestion
* - don't contain positional arguments after named arguments
* - all map to valid argument names from the function declaration
```
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/PythonUDFRunner.scala:
##########
@@ -146,4 +157,30 @@ object PythonUDFRunner {
}
}
}
+
+ def writeUDFs(
+ dataOut: DataOutputStream,
Review Comment:
please indent each of these function arguments by 2 more spaces (for a total of +4 spaces each) per the style guide?
--
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
[GitHub] [spark] ueshin commented on a diff in pull request #42617: [SPARK-44918][SQL][PYTHON] Support named arguments in scalar Python/Pandas UDFs
Posted by "ueshin (via GitHub)" <gi...@apache.org>.
ueshin commented on code in PR #42617:
URL: https://github.com/apache/spark/pull/42617#discussion_r1302373707
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/UserDefinedPythonFunction.scala:
##########
@@ -50,6 +50,19 @@ case class UserDefinedPythonFunction(
udfDeterministic: Boolean) {
def builder(e: Seq[Expression]): Expression = {
+ if (pythonEvalType == PythonEvalType.SQL_BATCHED_UDF
+ || pythonEvalType ==PythonEvalType.SQL_ARROW_BATCHED_UDF
+ || pythonEvalType == PythonEvalType.SQL_SCALAR_PANDAS_UDF) {
+ /*
+ * Check if the named arguments:
+ * - don't have duplicated names
+ * - don't contain positional arguments
Review Comment:
The third item is not right for Python UDF/UDTF. Currently it relies on Python.
--
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
[GitHub] [spark] allisonwang-db commented on a diff in pull request #42617: [SPARK-44918][SQL][PYTHON] Support named arguments in scalar Python/Pandas UDFs
Posted by "allisonwang-db (via GitHub)" <gi...@apache.org>.
allisonwang-db commented on code in PR #42617:
URL: https://github.com/apache/spark/pull/42617#discussion_r1303674520
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/python/EvalPythonUDTFExec.scala:
##########
@@ -66,7 +56,6 @@ trait EvalPythonUDTFExec extends UnaryExecNode {
inputRDD.mapPartitions { iter =>
val context = TaskContext.get()
- val contextAwareIterator = new ContextAwareIterator(context, iter)
Review Comment:
Good catch!
--
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
[GitHub] [spark] ueshin commented on pull request #42617: [SPARK-44918][SQL][PYTHON] Support named arguments in scalar Python/Pandas UDFs
Posted by "ueshin (via GitHub)" <gi...@apache.org>.
ueshin commented on PR #42617:
URL: https://github.com/apache/spark/pull/42617#issuecomment-1689030291
cc @dtenedor @HyukjinKwon @xinrong-meng
--
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
[GitHub] [spark] ueshin commented on pull request #42617: [SPARK-44918][SQL][PYTHON] Support named arguments in scalar Python/Pandas UDFs
Posted by "ueshin (via GitHub)" <gi...@apache.org>.
ueshin commented on PR #42617:
URL: https://github.com/apache/spark/pull/42617#issuecomment-1692167559
Thanks! merging to master.
--
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