cloud-fan commented on code in PR #60214:
URL: https://github.com/apache/spark/pull/60214#discussion_r4537918915
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLFunctionSuite.scala:
##########
@@ -534,4 +534,153 @@ class SQLFunctionSuite extends SharedSparkSession {
checkAnswer(sql("SELECT * FROM tvf(null_udf(1))"), Row(null))
}
}
+
+ test("SPARK-60805: a SQL function body can call system-qualified functions")
{
+ withUserDefinedFunction("perm_q" -> false, "perm_q_tbl" -> false) {
+ Seq("builtin.abs(-1)", "system.builtin.abs(-1)",
"SYSTEM.Builtin.abs(-1)").foreach { call =>
+ sql(s"CREATE OR REPLACE FUNCTION perm_q() RETURNS INT RETURN $call")
+ checkAnswer(sql("SELECT perm_q()"), Row(1))
+ sql(s"CREATE OR REPLACE FUNCTION perm_q_tbl() RETURNS TABLE (a INT)
RETURN SELECT $call")
+ checkAnswer(sql("SELECT * FROM perm_q_tbl()"), Row(1))
+ }
+ // A system-qualified built-in table function, too.
+ Seq("builtin.range(3)", "system.builtin.range(3)").foreach { call =>
+ sql(s"CREATE OR REPLACE FUNCTION perm_q_tbl() RETURNS TABLE (id
BIGINT) " +
+ s"RETURN SELECT * FROM $call")
+ checkAnswer(sql("SELECT * FROM perm_q_tbl()"), Seq(Row(0), Row(1),
Row(2)))
+ }
+ }
+ // A catalog-qualified persistent call works too, and is checked for
cyclic references.
+ withUserDefinedFunction("perm_in" -> false, "perm_out" -> false) {
+ sql("CREATE FUNCTION perm_in() RETURNS INT RETURN 5")
+ sql("CREATE FUNCTION perm_out() RETURNS INT RETURN
spark_catalog.default.perm_in()")
+ checkAnswer(sql("SELECT perm_out()"), Row(5))
+ val e = intercept[AnalysisException] {
+ sql("CREATE OR REPLACE FUNCTION perm_in() RETURNS INT " +
+ "RETURN spark_catalog.default.perm_out()")
+ }
+ assert(e.getCondition == "CYCLIC_FUNCTION_REFERENCE", e.getMessage)
+ }
+ withUserDefinedFunction("tbl_in" -> false, "tbl_out" -> false) {
+ sql("CREATE FUNCTION tbl_in() RETURNS TABLE (a INT) RETURN SELECT 1")
+ sql("CREATE FUNCTION tbl_out() RETURNS TABLE (a INT) " +
+ "RETURN SELECT * FROM SPARK_CATALOG.default.tbl_in()")
+ checkAnswer(sql("SELECT * FROM tbl_out()"), Row(1))
+ val e = intercept[AnalysisException] {
+ sql("CREATE OR REPLACE FUNCTION tbl_in() RETURNS TABLE (a INT) " +
+ "RETURN SELECT * FROM spark_catalog.default.tbl_out()")
+ }
+ assert(e.getCondition == "CYCLIC_FUNCTION_REFERENCE", e.getMessage)
+ }
+ // A session-qualified call is checked for cyclic references like an
unqualified one.
+ withUserDefinedFunction("temp_c" -> true, "temp_c_tbl" -> true) {
+ sql("CREATE TEMPORARY FUNCTION temp_c() RETURNS INT RETURN 1")
+ sql("CREATE TEMPORARY FUNCTION temp_c_tbl() RETURNS TABLE (a INT) RETURN
SELECT 1")
+ Seq(
+ "CREATE OR REPLACE TEMPORARY FUNCTION temp_c() RETURNS INT RETURN
session.temp_c()",
+ "CREATE OR REPLACE TEMPORARY FUNCTION temp_c() RETURNS INT " +
+ "RETURN system.session.temp_c()",
+ "CREATE OR REPLACE TEMPORARY FUNCTION temp_c_tbl() RETURNS TABLE (a
INT) " +
+ "RETURN SELECT * FROM session.temp_c_tbl()"
+ ).foreach { ddl =>
+ val e = intercept[AnalysisException](sql(ddl))
+ assert(e.getCondition == "CYCLIC_FUNCTION_REFERENCE", s"$ddl:
${e.getMessage}")
+ }
+ }
+ }
+
+ test("SPARK-60805: a permanent function cannot reference a temp function or
temp TVF") {
+ withUserDefinedFunction("temp_fn" -> true, "temp_caller" -> true,
"temp_tbl" -> true,
+ "temp_tbl_caller" -> true) {
+ sql("CREATE TEMPORARY FUNCTION temp_fn() RETURNS INT RETURN 1")
+ sql("CREATE TEMPORARY FUNCTION temp_tbl() RETURNS TABLE (a INT) RETURN
SELECT 1")
+ // A temp function may.
+ sql("CREATE TEMPORARY FUNCTION temp_caller() RETURNS INT RETURN
system.session.temp_fn()")
+ checkAnswer(sql("SELECT temp_caller()"), Row(1))
+ sql("CREATE TEMPORARY FUNCTION temp_tbl_caller() RETURNS TABLE (a INT) "
+
+ "RETURN SELECT * FROM system.session.temp_tbl()")
+ checkAnswer(sql("SELECT * FROM temp_tbl_caller()"), Row(1))
+ Seq(
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN session.temp_fn()",
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN
system.session.temp_fn()",
+ "CREATE FUNCTION perm_fn() RETURNS TABLE (a INT) RETURN SELECT * FROM
temp_tbl()",
+ "CREATE FUNCTION perm_fn() RETURNS TABLE (a INT) RETURN SELECT * FROM
session.temp_tbl()",
+ "CREATE FUNCTION perm_fn() RETURNS TABLE (a INT) " +
+ "RETURN SELECT * FROM system.session.temp_tbl()",
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN (SELECT a FROM
session.temp_tbl())"
+ ).foreach { ddl =>
+ val e = intercept[AnalysisException](sql(ddl))
+ assert(e.getCondition == "INVALID_TEMP_OBJ_REFERENCE", s"$ddl:
${e.getMessage}")
+ }
+ }
+ }
+
+ test("SPARK-60805: builtin./session. calls fall back to a same-named
persistent schema") {
+ Seq("builtin", "session").foreach { schema =>
+ withDatabase(schema) {
+ sql(s"CREATE DATABASE $schema")
+ sql(s"CREATE FUNCTION $schema.pf() RETURNS INT RETURN 1")
+ sql(s"CREATE FUNCTION $schema.pg() RETURNS INT RETURN $schema.pf()")
+ sql(s"CREATE FUNCTION $schema.pt() RETURNS TABLE (a INT) RETURN SELECT
1")
+ checkAnswer(sql(s"SELECT $schema.pg()"), Row(1))
+ Seq(
+ s"CREATE OR REPLACE FUNCTION $schema.pf() RETURNS INT RETURN
$schema.pf()",
+ s"CREATE OR REPLACE FUNCTION $schema.pf() RETURNS INT RETURN
$schema.pg()",
+ s"CREATE OR REPLACE FUNCTION $schema.pt() RETURNS TABLE (a INT) " +
+ s"RETURN SELECT * FROM $schema.pt()"
+ ).foreach { ddl =>
+ val e = intercept[AnalysisException](sql(ddl))
+ assert(e.getCondition == "CYCLIC_FUNCTION_REFERENCE", s"$ddl:
${e.getMessage}")
+ }
+ }
+ }
+ }
+
+ test("SPARK-60805: session. calls follow
spark.sql.legacy.persistentCatalogFirst") {
+ withDatabase("session") {
+ sql("CREATE DATABASE session")
+ sql("CREATE FUNCTION session.sf() RETURNS INT RETURN 3")
+ withUserDefinedFunction("sf" -> true, "perm_sf" -> false) {
+ sql("CREATE TEMPORARY FUNCTION sf() RETURNS INT RETURN 7")
+ val createPerm = "CREATE OR REPLACE FUNCTION perm_sf() RETURNS INT
RETURN session.sf()"
+ // By default the temp function owns `session.sf`.
+ val e = intercept[AnalysisException](sql(createPerm))
+ assert(e.getCondition == "INVALID_TEMP_OBJ_REFERENCE", e.getMessage)
+ withSQLConf(SQLConf.PERSISTENT_CATALOG_FIRST.key -> "true") {
Review Comment:
**Non-blocking (P2):** This is the only legacy-mode block, and it always has
a persistent `session.sf`, so under `persistentCatalogFirst=true` only the
persistent side of `functionIdentifierForLookup` is exercised. When no
same-named persistent function exists, only the third disjunct
(`!catalog.isPersistentFunction(persistentIdent.get)`) sends a `builtin.` or
`session.` call to the system namespace. Without it, `CREATE FUNCTION p()
RETURNS INT RETURN builtin.abs(-1)` fails again with `SCHEMA_NOT_FOUND`, and
`session.temp_fn()` is no longer flagged in a persistent body under this mode,
yet no test here or in the other suites that set this conf would fail. A
legacy-mode case with no schema named `builtin` or `session` would pin it:
`builtin.abs(-1)` in a body succeeds, and `session.temp_fn()` in a persistent
body fails with `INVALID_TEMP_OBJ_REFERENCE`.
See **Shared repair plan 2** in the review body.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/CreateSQLFunctionCommand.scala:
##########
@@ -364,11 +363,15 @@ case class CreateSQLFunctionCommand(
child.flatMap {
case w: UnresolvedWith if !w.resolved =>
w.innerChildren.flatMap(collectTempFunctions)
case plan if !plan.resolved =>
- plan.expressions.flatMap(_.flatMap {
+ val tempTableFunction = plan match {
+ case u: UnresolvedTableValuedFunction if
isTemporaryFunctionName(catalog, u.name) =>
Review Comment:
**Blocking (P1):** This predicate treats an unqualified table-function call
as a temporary reference whenever a temporary function of that name exists in
either registry, but resolution may bind a different routine. Reading the code
under the default `spark.sql.functionResolution.sessionOrder=second`:
```sql
CREATE TEMPORARY FUNCTION range() RETURNS TABLE (a INT) RETURN SELECT 1; --
allowed: builtin comes first
CREATE FUNCTION p() RETURNS TABLE (id BIGINT) RETURN SELECT * FROM range(3);
```
The cyclic check skips the call because `lookupFunctionInfo(range)` returns
the built-in, and analysis binds the built-in through the fast path in
`FunctionResolution.resolveTableFunction`, so `p` never depends on the
temporary function. This branch still sees `isTemporaryFunction(range) ==
true`, and CREATE FUNCTION fails with `INVALID_TEMP_OBJ_REFERENCE`. On the base
branch `p` is created and returns 0, 1, 2 in every session. A temporary scalar
`explode` with a body `SELECT * FROM explode(array(1, 2))` fails the same way,
and so, under `sessionOrder=last` or a `SET PATH` that lists a schema before
`system.session`, does an unqualified call that resolves to a persistent table
function. The routed JSON-constructor arm a few lines below adds an ownership
check for exactly this reason, and CREATE VIEW rejects only the temporary
functions that resolution recorded.
See **Shared repair plan 1** in the review body.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/CreateSQLFunctionCommand.scala:
##########
@@ -432,14 +470,23 @@ case class CreateSQLFunctionCommand(
identifier: FunctionIdentifier,
plan: LogicalPlan): Unit = {
+ // `functionIdentifierForLookup`, if `nameParts` names a SQL function.
+ def sqlFunctionIdentifier(
+ nameParts: Seq[String],
+ tableFunction: Boolean): Option[(FunctionIdentifier, Boolean)] =
+ functionIdentifierForLookup(catalog, nameParts).filter {
+ case (ident, true) => catalog.lookupFunctionInfoByIdentifier(ident,
tableFunction)
+ .exists(info => isSQLFunction(info.getClassName))
+ case (ident, false) =>
isSQLFunction(catalog.lookupFunctionInfo(ident).getClassName)
+ }
+
def checkPlan(plan: LogicalPlan, path: Seq[FunctionIdentifier]): Unit = {
plan.foreach {
case u @ UnresolvedTableValuedFunction(nameParts, arguments, _) =>
- val funcId = nameParts.asFunctionIdentifier
- val info = catalog.lookupFunctionInfo(funcId)
- if (isSQLFunction(info.getClassName)) {
+ sqlFunctionIdentifier(nameParts, tableFunction = true).foreach {
case (ident, isSystem) =>
val f = withPosition(u) {
- catalog.lookupTableFunction(funcId,
arguments).asInstanceOf[SQLTableFunction]
+ (if (isSystem) catalog.resolveTableFunctionByIdentifier(ident,
arguments).get
Review Comment:
**Nit (P3):** `sqlFunctionIdentifier` checked the function with
`lookupFunctionInfoByIdentifier`, but this resolve is a second registry access,
and `resolveTableFunctionByIdentifier` returns `None` if the temporary function
was dropped in between, for example by a concurrent Spark Connect operation on
the same session. `.get` then throws `java.util.NoSuchElementException:
None.get` as an internal error, where the unqualified path's
`lookupTableFunction` raises a clear not-found error. The scalar call at line
518 has the same shape. `.getOrElse(catalog.failFunctionLookup(ident))` keeps
the error clear.
**Verification:**
- **Inspection:** Neither system-function resolve call in the cyclic check
is followed by an unchecked Option.get, and each missing result maps to the
catalog's not-found error for that function.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLFunctionSuite.scala:
##########
@@ -534,4 +534,153 @@ class SQLFunctionSuite extends SharedSparkSession {
checkAnswer(sql("SELECT * FROM tvf(null_udf(1))"), Row(null))
}
}
+
+ test("SPARK-60805: a SQL function body can call system-qualified functions")
{
+ withUserDefinedFunction("perm_q" -> false, "perm_q_tbl" -> false) {
+ Seq("builtin.abs(-1)", "system.builtin.abs(-1)",
"SYSTEM.Builtin.abs(-1)").foreach { call =>
+ sql(s"CREATE OR REPLACE FUNCTION perm_q() RETURNS INT RETURN $call")
+ checkAnswer(sql("SELECT perm_q()"), Row(1))
+ sql(s"CREATE OR REPLACE FUNCTION perm_q_tbl() RETURNS TABLE (a INT)
RETURN SELECT $call")
+ checkAnswer(sql("SELECT * FROM perm_q_tbl()"), Row(1))
+ }
+ // A system-qualified built-in table function, too.
+ Seq("builtin.range(3)", "system.builtin.range(3)").foreach { call =>
+ sql(s"CREATE OR REPLACE FUNCTION perm_q_tbl() RETURNS TABLE (id
BIGINT) " +
+ s"RETURN SELECT * FROM $call")
+ checkAnswer(sql("SELECT * FROM perm_q_tbl()"), Seq(Row(0), Row(1),
Row(2)))
+ }
+ }
+ // A catalog-qualified persistent call works too, and is checked for
cyclic references.
+ withUserDefinedFunction("perm_in" -> false, "perm_out" -> false) {
+ sql("CREATE FUNCTION perm_in() RETURNS INT RETURN 5")
+ sql("CREATE FUNCTION perm_out() RETURNS INT RETURN
spark_catalog.default.perm_in()")
+ checkAnswer(sql("SELECT perm_out()"), Row(5))
+ val e = intercept[AnalysisException] {
+ sql("CREATE OR REPLACE FUNCTION perm_in() RETURNS INT " +
+ "RETURN spark_catalog.default.perm_out()")
+ }
+ assert(e.getCondition == "CYCLIC_FUNCTION_REFERENCE", e.getMessage)
+ }
+ withUserDefinedFunction("tbl_in" -> false, "tbl_out" -> false) {
+ sql("CREATE FUNCTION tbl_in() RETURNS TABLE (a INT) RETURN SELECT 1")
+ sql("CREATE FUNCTION tbl_out() RETURNS TABLE (a INT) " +
+ "RETURN SELECT * FROM SPARK_CATALOG.default.tbl_in()")
+ checkAnswer(sql("SELECT * FROM tbl_out()"), Row(1))
+ val e = intercept[AnalysisException] {
+ sql("CREATE OR REPLACE FUNCTION tbl_in() RETURNS TABLE (a INT) " +
+ "RETURN SELECT * FROM spark_catalog.default.tbl_out()")
+ }
+ assert(e.getCondition == "CYCLIC_FUNCTION_REFERENCE", e.getMessage)
+ }
+ // A session-qualified call is checked for cyclic references like an
unqualified one.
+ withUserDefinedFunction("temp_c" -> true, "temp_c_tbl" -> true) {
+ sql("CREATE TEMPORARY FUNCTION temp_c() RETURNS INT RETURN 1")
+ sql("CREATE TEMPORARY FUNCTION temp_c_tbl() RETURNS TABLE (a INT) RETURN
SELECT 1")
+ Seq(
+ "CREATE OR REPLACE TEMPORARY FUNCTION temp_c() RETURNS INT RETURN
session.temp_c()",
+ "CREATE OR REPLACE TEMPORARY FUNCTION temp_c() RETURNS INT " +
+ "RETURN system.session.temp_c()",
+ "CREATE OR REPLACE TEMPORARY FUNCTION temp_c_tbl() RETURNS TABLE (a
INT) " +
+ "RETURN SELECT * FROM session.temp_c_tbl()"
+ ).foreach { ddl =>
+ val e = intercept[AnalysisException](sql(ddl))
+ assert(e.getCondition == "CYCLIC_FUNCTION_REFERENCE", s"$ddl:
${e.getMessage}")
+ }
+ }
+ }
+
+ test("SPARK-60805: a permanent function cannot reference a temp function or
temp TVF") {
+ withUserDefinedFunction("temp_fn" -> true, "temp_caller" -> true,
"temp_tbl" -> true,
+ "temp_tbl_caller" -> true) {
+ sql("CREATE TEMPORARY FUNCTION temp_fn() RETURNS INT RETURN 1")
+ sql("CREATE TEMPORARY FUNCTION temp_tbl() RETURNS TABLE (a INT) RETURN
SELECT 1")
+ // A temp function may.
+ sql("CREATE TEMPORARY FUNCTION temp_caller() RETURNS INT RETURN
system.session.temp_fn()")
+ checkAnswer(sql("SELECT temp_caller()"), Row(1))
+ sql("CREATE TEMPORARY FUNCTION temp_tbl_caller() RETURNS TABLE (a INT) "
+
+ "RETURN SELECT * FROM system.session.temp_tbl()")
+ checkAnswer(sql("SELECT * FROM temp_tbl_caller()"), Row(1))
+ Seq(
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN session.temp_fn()",
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN
system.session.temp_fn()",
+ "CREATE FUNCTION perm_fn() RETURNS TABLE (a INT) RETURN SELECT * FROM
temp_tbl()",
+ "CREATE FUNCTION perm_fn() RETURNS TABLE (a INT) RETURN SELECT * FROM
session.temp_tbl()",
+ "CREATE FUNCTION perm_fn() RETURNS TABLE (a INT) " +
+ "RETURN SELECT * FROM system.session.temp_tbl()",
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN (SELECT a FROM
session.temp_tbl())"
+ ).foreach { ddl =>
+ val e = intercept[AnalysisException](sql(ddl))
+ assert(e.getCondition == "INVALID_TEMP_OBJ_REFERENCE", s"$ddl:
${e.getMessage}")
+ }
+ }
+ }
+
+ test("SPARK-60805: builtin./session. calls fall back to a same-named
persistent schema") {
+ Seq("builtin", "session").foreach { schema =>
+ withDatabase(schema) {
+ sql(s"CREATE DATABASE $schema")
+ sql(s"CREATE FUNCTION $schema.pf() RETURNS INT RETURN 1")
+ sql(s"CREATE FUNCTION $schema.pg() RETURNS INT RETURN $schema.pf()")
+ sql(s"CREATE FUNCTION $schema.pt() RETURNS TABLE (a INT) RETURN SELECT
1")
+ checkAnswer(sql(s"SELECT $schema.pg()"), Row(1))
+ Seq(
+ s"CREATE OR REPLACE FUNCTION $schema.pf() RETURNS INT RETURN
$schema.pf()",
+ s"CREATE OR REPLACE FUNCTION $schema.pf() RETURNS INT RETURN
$schema.pg()",
+ s"CREATE OR REPLACE FUNCTION $schema.pt() RETURNS TABLE (a INT) " +
+ s"RETURN SELECT * FROM $schema.pt()"
+ ).foreach { ddl =>
+ val e = intercept[AnalysisException](sql(ddl))
+ assert(e.getCondition == "CYCLIC_FUNCTION_REFERENCE", s"$ddl:
${e.getMessage}")
+ }
+ }
+ }
+ }
+
+ test("SPARK-60805: session. calls follow
spark.sql.legacy.persistentCatalogFirst") {
+ withDatabase("session") {
+ sql("CREATE DATABASE session")
+ sql("CREATE FUNCTION session.sf() RETURNS INT RETURN 3")
+ withUserDefinedFunction("sf" -> true, "perm_sf" -> false) {
+ sql("CREATE TEMPORARY FUNCTION sf() RETURNS INT RETURN 7")
+ val createPerm = "CREATE OR REPLACE FUNCTION perm_sf() RETURNS INT
RETURN session.sf()"
Review Comment:
**Nit (P3):** This test pins temp-versus-persistent precedence only for the
scalar `session.sf`, which the scalar half of `systemFunctionExists` decides.
The table-function half (`lookupFunctionInfoByIdentifier(ident, tableFunction =
true)` at line 418 of `CreateSQLFunctionCommand.scala`) has no failure signal:
`session.pt` in the fallback test has no temporary counterpart, and the
`session.temp_tbl()` cases have no schema named `session`. Without that half,
`CREATE FUNCTION perm() RETURNS TABLE (a INT) RETURN SELECT * FROM
session.pt()`, with both a temporary `pt` and a persistent `session.pt`, maps
to the persistent side and is accepted, although analysis binds
`system.session.pt` first. A table-function variant of this case would cover it.
See **Shared repair plan 2** in the review body.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/CreateSQLFunctionCommand.scala:
##########
@@ -387,6 +390,41 @@ case class CreateSQLFunctionCommand(
(collectTempViews(child), tempFunctions)
}
+ // 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))
+ case _ => functionIdentifierForLookup(catalog, nameParts).exists {
+ case (ident, isSystem) => isSystem &&
catalog.isTemporaryFunction(ident)
+ }
+ }
+
+ // 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)
+ // Catalog names are case-insensitive regardless of
spark.sql.caseSensitive.
+ case Seq(c, db, f) if
c.equalsIgnoreCase(CatalogManager.SESSION_CATALOG_NAME) =>
+ Some(FunctionIdentifier(f, Some(db),
Some(CatalogManager.SESSION_CATALOG_NAME)))
+ case _ => None
+ }
+ def systemFunctionExists(ident: FunctionIdentifier): Boolean =
+ catalog.lookupFunctionInfoByIdentifier(ident, tableFunction =
false).isDefined ||
+ catalog.lookupFunctionInfoByIdentifier(ident, tableFunction =
true).isDefined
+ catalog.identifierFromSystemNameParts(nameParts) match {
+ case Some(ident) if nameParts.length > 2 ||
+ (conf.prioritizeSystemCatalog && systemFunctionExists(ident)) ||
+ !catalog.isPersistentFunction(persistentIdent.get) =>
Review Comment:
**Nit (P3):** `isPersistentFunction` is uncached (`databaseExists` plus
`externalCatalog.functionExists`), and this helper runs at every call site in
both pre-analysis checks. A body such as `builtin.pf() + builtin.pf() +
builtin.pf()` over a persistent schema named `builtin` therefore pays about
four metastore round trips per site, where the base branch's
`lookupFunctionInfo` went through the function registry cache after the first
load; with a Hive metastore each one is an RPC, though only at DDL time. In
`isTemporaryFunctionName` the probe cannot change the result, because a true
answer also needs `isTemporaryFunction` on the system identifier. Checking that
in-memory condition first, and answering the persistent-exists question in the
cyclic check with a cached, non-throwing lookup whose result the filter reuses,
keeps the same results.
**Verification:**
- **Inspection:** For a qualified name with no temporary function, the
temporary-function check consults only the in-memory temporary registries, and
the cyclic check performs at most one persistent-metadata lookup per call site,
answered from the function registry once the function has been loaded.
- **Compatibility:** Fallback to a same-named persistent schema, the legacy
persistentCatalogFirst order, and system-namespace resolution keep their
current results and errors. This includes a new case where a builtin.-qualified
name that exists nowhere reports the analyzer's unresolved-routine error rather
than a schema error.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/CreateSQLFunctionCommand.scala:
##########
@@ -387,6 +390,41 @@ case class CreateSQLFunctionCommand(
(collectTempViews(child), tempFunctions)
}
+ // 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))
+ case _ => functionIdentifierForLookup(catalog, nameParts).exists {
+ case (ident, isSystem) => isSystem &&
catalog.isTemporaryFunction(ident)
+ }
+ }
+
+ // 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)
+ // Catalog names are case-insensitive regardless of
spark.sql.caseSensitive.
Review Comment:
**Nit (P3):** Only the session catalog name is matched case-insensitively:
`CatalogManager.catalog` uses `equalsIgnoreCase` for `spark_catalog` and loads
every other catalog by its exact name, and the match below relies only on the
former.
```suggestion
// The session catalog name is case-insensitive even with
spark.sql.caseSensitive=true.
```
**Verification:**
- **Inspection:** The comment above the spark_catalog match no longer claims
that all catalog names are case-insensitive, and it agrees with
CatalogManager.catalog, which matches only the session catalog name
case-insensitively.
--
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]