-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-22901][PYTHON] Add deterministic flag to pyspark UDF #19929
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
Changes from all commits
6187d5a
187ff9a
cc309b0
462a92a
a40ba73
47801c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,7 @@ def __init__(self, func, | |
| func.__name__ if hasattr(func, '__name__') | ||
| else func.__class__.__name__) | ||
| self.evalType = evalType | ||
| self._deterministic = True | ||
|
|
||
| @property | ||
| def returnType(self): | ||
|
|
@@ -125,7 +126,7 @@ def _create_judf(self): | |
| wrapped_func = _wrap_function(sc, self.func, self.returnType) | ||
| jdt = spark._jsparkSession.parseDataType(self.returnType.json()) | ||
| judf = sc._jvm.org.apache.spark.sql.execution.python.UserDefinedPythonFunction( | ||
| self._name, wrapped_func, jdt, self.evalType) | ||
| self._name, wrapped_func, jdt, self.evalType, self._deterministic) | ||
| return judf | ||
|
|
||
| def __call__(self, *cols): | ||
|
|
@@ -157,5 +158,15 @@ def wrapper(*args): | |
| wrapper.func = self.func | ||
| wrapper.returnType = self.returnType | ||
| wrapper.evalType = self.evalType | ||
| wrapper.asNondeterministic = self.asNondeterministic | ||
|
|
||
| return wrapper | ||
|
|
||
| def asNondeterministic(self): | ||
| """ | ||
| Updates UserDefinedFunction to nondeterministic. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| .. versionadded:: 2.3 | ||
| """ | ||
| self._deterministic = False | ||
| return self | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ import scala.reflect.runtime.universe.TypeTag | |
| import scala.util.Try | ||
|
|
||
| import org.apache.spark.annotation.InterfaceStability | ||
| import org.apache.spark.api.python.PythonEvalType | ||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.sql.api.java._ | ||
| import org.apache.spark.sql.catalyst.{JavaTypeInference, ScalaReflection} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Looks obsolete. |
||
|
|
@@ -41,8 +42,6 @@ import org.apache.spark.util.Utils | |
| * spark.udf | ||
| * }}} | ||
| * | ||
| * @note The user-defined functions must be deterministic. | ||
| * | ||
| * @since 1.3.0 | ||
| */ | ||
| @InterfaceStability.Stable | ||
|
|
@@ -58,6 +57,8 @@ class UDFRegistration private[sql] (functionRegistry: FunctionRegistry) extends | |
| | pythonIncludes: ${udf.func.pythonIncludes} | ||
| | pythonExec: ${udf.func.pythonExec} | ||
| | dataType: ${udf.dataType} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you also print out |
||
| | pythonEvalType: ${PythonEvalType.toString(udf.pythonEvalType)} | ||
| | udfDeterministic: ${udf.udfDeterministic} | ||
| """.stripMargin) | ||
|
|
||
| functionRegistry.createOrReplaceTempFunction(name, udf.builder) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to just add a parameter for deterministic? Adding it to the end is OK to PySpark without breaking the existing app? cc @ueshin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed what was done for scala UDF, where this parameter is not added, but there is a method to add it. If we add a parameter here, I'd then suggest to add it also to the scala API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Scala and Python are different, because that is also for JAVA API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gatorsmile, however, wouldn't it be better to keep them consistent if possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am saying this because I had few talks about this before and I am pretty sure we usually keep them as same whenever possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
asNondeterministicis not straightforward to users. In Scala sides, we have no choice for avoiding breaking the API. Anyway, I am fine to keep it as now