ganeshashree commented on code in PR #60214:
URL: https://github.com/apache/spark/pull/60214#discussion_r4598595327
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/CreateSQLFunctionCommand.scala:
##########
@@ -374,24 +376,42 @@ case class CreateSQLFunctionCommand(
}.distinct
}
+ def collectInExpression(expr: Expression): Seq[String] = expr.flatMap {
+ case e: SubqueryExpression => collectTempFunctions(e.plan)
+ case e: UnresolvedFunction if isTemporaryScalarFunctionName(catalog,
e.nameParts) =>
+ Seq(e.nameParts.last)
+ // An IDENTIFIER-clause call: check the call it builds from its constant
name. An unqualified
+ // name counts only when the SQL PATH resolves it to the temp function.
+ case e: ExpressionWithUnresolvedIdentifier if !legacyTempFunctionCheck =>
+ identifierNameParts(analyzer, e.identifierExpr).map(e.exprBuilder(_,
e.otherExprs)) match {
+ case Some(f: UnresolvedFunction) if
isTemporaryScalarFunctionName(catalog, f.nameParts) &&
+ (f.nameParts.length > 1 ||
+ analyzer.unqualifiedFunctionResolvesToTemp(f.nameParts.head))
=>
+ Seq(f.nameParts.last)
+ case _ => Seq.empty
+ }
+ // A routed JSON constructor is a temporary-function reference only when
the
+ // temporary function owns it, not the built-in or a persistent function
before it.
+ case j: UnresolvedJsonConstructor
+ if j.routeThroughResolution &&
+ catalog.isTemporaryFunction(FunctionIdentifier(j.prettyName)) &&
+ analyzer.unqualifiedFunctionResolvesToTemp(j.prettyName) =>
+ Seq(j.prettyName)
+ case _ => Seq.empty
+ }
+
def collectTempFunctions(child: LogicalPlan): Seq[String] = {
child.flatMap {
case w: UnresolvedWith if !w.resolved =>
w.innerChildren.flatMap(collectTempFunctions)
- case plan if !plan.resolved =>
- plan.expressions.flatMap(_.flatMap {
- case e: SubqueryExpression => collectTempFunctions(e.plan)
- case e: UnresolvedFunction
- if catalog.isTemporaryFunction(e.nameParts.asFunctionIdentifier)
=>
- Seq(e.nameParts.asFunctionIdentifier.funcName)
- // A routed JSON constructor is a temporary-function reference
only when the
- // temporary function owns it, not the built-in or a persistent
function before it.
- case j: UnresolvedJsonConstructor
- if j.routeThroughResolution &&
- catalog.isTemporaryFunction(FunctionIdentifier(j.prettyName))
&&
- analyzer.unqualifiedFunctionResolvesToTemp(j.prettyName) =>
- Seq(j.prettyName)
- case _ => Seq.empty
- })
+ // A qualified temp table function call. Resolution's recorded names
cover the others.
+ case u: UnresolvedTableValuedFunction =>
+ val qualified = if (u.name.length > 1 &&
isTemporaryTableFunctionName(catalog, u.name)) {
+ Seq(u.name.last)
+ } else {
+ Nil
+ }
+ qualified ++ u.functionArgs.flatMap(collectInExpression)
+ case plan if !plan.resolved =>
plan.expressions.flatMap(collectInExpression)
Review Comment:
Fixed: the scan now checks the arguments of an IDENTIFIER-named table
function, following the SQL PATH for unqualified calls.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/CreateSQLFunctionCommand.scala:
##########
@@ -446,14 +561,28 @@ case class CreateSQLFunctionCommand(
identifier: FunctionIdentifier,
plan: LogicalPlan): Unit = {
+ // Returns `functionIdentifierForLookup`'s result when `nameParts` names a
SQL function, and
+ // None otherwise. A missing function outside the system namespace makes
the lookup throw.
+ def sqlFunctionIdentifier(
+ nameParts: Seq[String],
+ tableFunction: Boolean): Option[(FunctionIdentifier, Boolean)] =
+ functionIdentifierForLookup(catalog, nameParts).filter {
Review Comment:
Fixed: under the flag, the cycle check now also maps the `session.f` call to
the persistent function.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLFunctionSuite.scala:
##########
@@ -534,4 +534,353 @@ 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 builtin.-qualified name that exists nowhere is an unresolved
routine.
+ val e = intercept[AnalysisException] {
+ sql("CREATE OR REPLACE FUNCTION perm_q() RETURNS INT RETURN
builtin.nope()")
+ }
+ assert(e.getCondition == "UNRESOLVED_ROUTINE", e.getMessage)
+ // 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 temporary function may call another temporary function through
`system.session.`.
+ 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 INT RETURN IDENTIFIER('temp_fn')()",
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN
IDENTIFIER('session.temp_fn')() + 1",
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN
IDENTIFIER(concat('temp', '_fn'))()",
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN
IDENTIFIER(lower('TEMP_FN'))()",
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN (SELECT a FROM
temp_tbl())",
+ "CREATE FUNCTION perm_fn() RETURNS INT RETURN (SELECT a FROM
IDENTIFIER('temp_tbl')())",
+ "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}")
+ }
+ withUserDefinedFunction("temp_built" -> true, "perm_built" -> false) {
+ sql("CREATE TEMPORARY FUNCTION temp_built() RETURNS INT " +
+ "RETURN IDENTIFIER(concat('temp', '_fn'))()")
+ checkAnswer(sql("SELECT temp_built()"), Row(1))
+ sql("CREATE FUNCTION perm_built() RETURNS INT RETURN
IDENTIFIER(concat('ab', 's'))(-1)")
+ checkAnswer(sql("SELECT perm_built()"), Row(1))
+ }
+ // The legacy flag re-allows only the forms earlier versions accepted at
creation.
+ val legacyKey =
SQLConf.LEGACY_ALLOW_TEMP_FUNCTION_IN_PERSISTED_SQL_FUNCTION.key
+ withUserDefinedFunction("perm_legacy" -> false, "perm_legacy_tbl" ->
false,
+ "perm_outer" -> false) {
+ withSQLConf(legacyKey -> "true") {
+ Seq("temp_tbl()", "IDENTIFIER('temp_tbl')()").foreach { call =>
+ sql("CREATE OR REPLACE FUNCTION perm_legacy_tbl() RETURNS TABLE (a
INT) " +
+ s"RETURN SELECT * FROM $call")
+ checkAnswer(sql("SELECT * FROM perm_legacy_tbl()"), Row(1))
+ }
+ Seq("IDENTIFIER('temp_fn')()", "IDENTIFIER(concat('temp',
'_fn'))()").foreach { call =>
+ sql(s"CREATE OR REPLACE FUNCTION perm_legacy() RETURNS INT RETURN
$call")
+ checkAnswer(sql("SELECT perm_legacy()"), Row(1))
+ }
+ Seq(
+ "RETURNS INT RETURN temp_fn()",
+ "RETURNS INT RETURN session.temp_fn()",
+ "RETURNS INT RETURN system.session.temp_fn()",
+ "RETURNS TABLE (a INT) RETURN SELECT * FROM session.temp_tbl()",
+ "RETURNS TABLE (a INT) RETURN SELECT * FROM
system.session.temp_tbl()"
+ ).foreach { body =>
+ val e = intercept[AnalysisException] {
+ sql(s"CREATE OR REPLACE FUNCTION perm_legacy_rej() $body")
+ }
+ assert(e.getCondition == "INVALID_TEMP_OBJ_REFERENCE", s"$body:
${e.getMessage}")
+ }
+ }
+ // Without the flag, calling a function that depends on a temp TVF is
rejected too.
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]