ganeshashree commented on code in PR #60214:
URL: https://github.com/apache/spark/pull/60214#discussion_r4539292605
##########
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:
Done. Temp table functions are now taken from what resolution records while
analyzing the body, so built-in/persistent-owned calls are accepted and
IDENTIFIER calls are rejected. The name-only branch is removed.
##########
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:
Done. Added a legacy-mode case with no `builtin`/`session` schema.
##########
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:
Done. Added the table-function variant.
##########
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:
Done.
--
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]