maropu commented on a change in pull request #28840:
URL: https://github.com/apache/spark/pull/28840#discussion_r444259130
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/command/functions.scala
##########
@@ -236,6 +236,58 @@ case class ShowFunctionsCommand(
}
}
+
+/**
+ * A command for users to refresh the persistent function.
+ * The syntax of using this command in SQL is:
+ * {{{
+ * REFRESH FUNCTION functionName
+ * }}}
+ */
+case class RefreshFunctionCommand(
+ databaseName: Option[String],
+ functionName: String)
+ extends RunnableCommand {
+
+ override def run(sparkSession: SparkSession): Seq[Row] = {
+ val catalog = sparkSession.sessionState.catalog
+ if
(FunctionRegistry.builtin.functionExists(FunctionIdentifier(functionName))) {
+ throw new AnalysisException(s"Cannot refresh native function
$functionName")
+ } else if (catalog.isTemporaryFunction(FunctionIdentifier(functionName,
databaseName))) {
+ throw new AnalysisException(s"Cannot refresh temp function
$functionName")
+ } else {
+ // we only refresh the permanent function.
+ // there are 4 cases:
+ // 1. registry exists externalCatalog exists
+ // 2. registry exists externalCatalog not exists
+ // 3. registry not exists externalCatalog exists
+ // 4. registry not exists externalCatalog not exists
+ val identifier = FunctionIdentifier(
+ functionName, Some(databaseName.getOrElse(catalog.getCurrentDatabase)))
+ val isRegisteredFunction = catalog.isRegisteredFunction(identifier)
+ val isPersistentFunction = catalog.isPersistentFunction(identifier)
+ if (isRegisteredFunction && isPersistentFunction) {
+ // re-register function
+ catalog.unregisterFunction(identifier)
Review comment:
Ah, I see. I checked the code again and how about refactoring it like
this (just a suggestion)?;
```
val identifier = FunctionIdentifier(
functionName, Some(databaseName.getOrElse(catalog.getCurrentDatabase)))
if (catalog.isRegisteredFunction(identifier)) {
// (...Write comments about why do we need to unregister a function...)
// catalog.unregisterFunction(identifier)
}
if (catalog.isPersistentFunction(identifier)) {
val func = catalog.getFunctionMetadata(identifier)
catalog.registerFunction(func, true)
} else {
throw new NoSuchFunctionException(identifier.database.get,
functionName)
}
```
That's because many branches made me understood a bit hardly.
----------------------------------------------------------------
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]