ganeshashree commented on code in PR #60214:
URL: https://github.com/apache/spark/pull/60214#discussion_r4563589273


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/CreateSQLFunctionCommand.scala:
##########
@@ -360,24 +362,43 @@ case class CreateSQLFunctionCommand(
       }.distinct
     }
 
+    def collectInExpression(expr: Expression): Seq[String] = expr.flatMap {
+      case e: SubqueryExpression => collectTempFunctions(e.plan)
+      case e: UnresolvedFunction if isTemporaryFunctionName(catalog, 
e.nameParts) =>
+        Seq(e.nameParts.last)
+      // An IDENTIFIER-clause call with a constant name: check the call it 
builds. The body
+      // has already been analyzed, so the name evaluates.
+      case e: ExpressionWithUnresolvedIdentifier
+          if !legacyTempFunctionCheck && e.identifierExpr.resolved =>

Review Comment:
   Done. Non-literal names are now resolved and evaluated like analysis does, 
in a fresh context. Added tests for `concat`/`lower` names.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/CreateSQLFunctionCommand.scala:
##########
@@ -387,13 +408,56 @@ case class CreateSQLFunctionCommand(
     (collectTempViews(child), tempFunctions)
   }
 
+  private def legacyTempFunctionCheck: Boolean =
+    conf.getConf(SQLConf.LEGACY_ALLOW_TEMP_FUNCTION_IN_PERSISTED_SQL_FUNCTION)
+
+  // Whether `nameParts` (unqualified, or `session.` / 
`system.session.`-qualified) names a temp
+  // function.
+  private def isTemporaryFunctionName(catalog: SessionCatalog, nameParts: 
Seq[String]): Boolean =
+    nameParts match {
+      case Seq(funcName) => 
catalog.isTemporaryFunction(FunctionIdentifier(funcName))
+      // Check the in-memory registry first: the lookup may probe the 
metastore.
+      case _ =>
+        
catalog.identifierFromSystemNameParts(nameParts).exists(catalog.isTemporaryFunction)
 &&

Review Comment:
   Done. Under the flag, a `session.f` call backed by a persistent `session.f` 
is accepted again, for scalar and table functions.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala:
##########
@@ -4600,6 +4600,18 @@ object SQLConf {
       .booleanConf
       .createWithDefault(false)
 
+  val LEGACY_ALLOW_TEMP_FUNCTION_IN_PERSISTED_SQL_FUNCTION =
+    buildConf("spark.sql.legacy.allowTempFunctionInPersistedSQLFunction")
+      .internal()
+      .doc("When true, CREATE FUNCTION for a persistent SQL function accepts a 
body that calls " +

Review Comment:
   Done.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/CreateSQLFunctionCommand.scala:
##########
@@ -387,13 +408,56 @@ case class CreateSQLFunctionCommand(
     (collectTempViews(child), tempFunctions)
   }
 
+  private def legacyTempFunctionCheck: Boolean =
+    conf.getConf(SQLConf.LEGACY_ALLOW_TEMP_FUNCTION_IN_PERSISTED_SQL_FUNCTION)
+
+  // Whether `nameParts` (unqualified, or `session.` / 
`system.session.`-qualified) names a temp
+  // function.
+  private def isTemporaryFunctionName(catalog: SessionCatalog, nameParts: 
Seq[String]): Boolean =
+    nameParts match {
+      case Seq(funcName) => 
catalog.isTemporaryFunction(FunctionIdentifier(funcName))
+      // Check the in-memory registry first: the lookup may probe the 
metastore.
+      case _ =>
+        
catalog.identifierFromSystemNameParts(nameParts).exists(catalog.isTemporaryFunction)
 &&
+          functionIdentifierForLookup(catalog, nameParts).exists(_._2)
+    }
+
+  // The identifier to look up `nameParts` by, and whether it is in a system 
namespace. A
+  // `builtin.` / `session.` name follows function resolution's candidate 
order. None for another
+  // catalog's function.
+  private def functionIdentifierForLookup(
+      catalog: SessionCatalog,
+      nameParts: Seq[String]): Option[(FunctionIdentifier, Boolean)] = {
+    lazy val persistentIdent = nameParts match {
+      case Seq(_) | Seq(_, _) => Some(nameParts.asFunctionIdentifier)
+      // The session catalog name is case-insensitive even with 
spark.sql.caseSensitive=true.
+      case Seq(c, db, f) if 
c.equalsIgnoreCase(CatalogManager.SESSION_CATALOG_NAME) =>
+        Some(FunctionIdentifier(f, Some(db), 
Some(CatalogManager.SESSION_CATALOG_NAME)))
+      case _ => None

Review Comment:
   Done. Added a test for another catalog's function.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to