Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.apache.spark.sql.catalyst.analysis.{FunctionRegistry, NoSuchFunctionE
import org.apache.spark.sql.catalyst.catalog.{CatalogFunction, FunctionResource}
import org.apache.spark.sql.catalyst.expressions.Attribute
import org.apache.spark.sql.catalyst.util.StringUtils
import org.apache.spark.sql.errors.QueryCompilationErrors
import org.apache.spark.sql.types.{StringType, StructField, StructType}


Expand Down Expand Up @@ -77,6 +78,9 @@ case class CreateFunctionCommand(
val catalog = sparkSession.sessionState.catalog
val func = CatalogFunction(FunctionIdentifier(functionName, databaseName), className, resources)
if (isTemp) {
if (!replace && catalog.isRegisteredFunction(func.identifier)) {
throw QueryCompilationErrors.functionAlreadyExistsError(func.identifier)
}
// We first load resources and then put the builder in the function registry.
catalog.loadFunctionResources(resources)
catalog.registerFunction(func, overrideIfExists = replace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import org.apache.spark.sql.functions._
import org.apache.spark.sql.hive.HiveExternalCatalog
import org.apache.spark.sql.hive.HiveUtils.{CONVERT_METASTORE_ORC, CONVERT_METASTORE_PARQUET}
import org.apache.spark.sql.hive.orc.OrcFileOperator
import org.apache.spark.sql.hive.test.TestHiveSingleton
import org.apache.spark.sql.hive.test.{TestHiveSingleton, TestHiveSparkSession}
import org.apache.spark.sql.internal.{HiveSerDe, SQLConf}
import org.apache.spark.sql.internal.SQLConf.ORC_IMPLEMENTATION
import org.apache.spark.sql.internal.StaticSQLConf.CATALOG_IMPLEMENTATION
Expand Down Expand Up @@ -2905,4 +2905,26 @@ class HiveDDLSuite
}
}
}

test("SPARK-34261: Avoid side effect if create exists temporary function") {
withUserDefinedFunction("f1" -> true) {
sql("CREATE TEMPORARY FUNCTION f1 AS 'org.apache.hadoop.hive.ql.udf.UDFUUID'")

val jarName = "TestUDTF.jar"
val jar = spark.asInstanceOf[TestHiveSparkSession].getHiveFile(jarName).toURI.toString
spark.sparkContext.addedJars.keys.find(_.contains(jarName))
.foreach(spark.sparkContext.addedJars.remove)
assert(!spark.sparkContext.listJars().exists(_.contains(jarName)))
val msg = intercept[AnalysisException] {
sql("CREATE TEMPORARY FUNCTION f1 AS " +
s"'org.apache.hadoop.hive.ql.udf.UDFUUID' USING JAR '$jar'")
}.getMessage
assert(msg.contains("Function f1 already exists"))
assert(!spark.sparkContext.listJars().exists(_.contains(jarName)))

sql("CREATE OR REPLACE TEMPORARY FUNCTION f1 AS " +
s"'org.apache.hadoop.hive.ql.udf.UDFUUID' USING JAR '$jar'")
assert(spark.sparkContext.listJars().exists(_.contains(jarName)))
}
}
}