Skip to content

[SPARK-52148][SQL] Fix CREATE OR REPLACE function for SQL user-defined TVFs #51191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
@@ -1481,6 +1481,8 @@ class SessionCatalog(
// For a permanent function, because we loaded it to the FunctionRegistry
// when it's first used, we also need to drop it from the FunctionRegistry.
functionRegistry.dropFunction(qualifiedIdent)
} else if (tableFunctionRegistry.functionExists(qualifiedIdent)) {
tableFunctionRegistry.dropFunction(qualifiedIdent)
}
externalCatalog.dropFunction(db, funcName)
} else if (!ignoreIfNotExists) {
Original file line number Diff line number Diff line change
@@ -2977,6 +2977,12 @@ SET spark.sql.ansi.enabled=true
SetCommand (spark.sql.ansi.enabled,Some(true))


-- !query
DROP FUNCTION IF EXISTS foo3_3at
-- !query analysis
DropFunctionCommand spark_catalog.default.foo3_3at, true, false


-- !query
CREATE FUNCTION foo3_3a(x INT) RETURNS DOUBLE RETURN 1 / x
-- !query analysis
@@ -3046,16 +3052,7 @@ CreateSQLFunctionCommand spark_catalog.default.foo3_3a, x INT, DOUBLE, 1 / x, fa
-- !query
CREATE OR REPLACE FUNCTION foo3_3at(x INT) RETURNS TABLE (a DOUBLE) RETURN SELECT 1 / x
-- !query analysis
org.apache.spark.sql.catalyst.analysis.FunctionAlreadyExistsException
{
"errorClass" : "ROUTINE_ALREADY_EXISTS",
"sqlState" : "42723",
"messageParameters" : {
"existingRoutineType" : "routine",
"newRoutineType" : "routine",
"routineName" : "`default`.`foo3_3at`"
}
}
CreateSQLFunctionCommand spark_catalog.default.foo3_3at, x INT, a DOUBLE, SELECT 1 / x, true, false, false, true


-- !query
1 change: 1 addition & 0 deletions sql/core/src/test/resources/sql-tests/inputs/sql-udf.sql
Original file line number Diff line number Diff line change
@@ -512,6 +512,7 @@ SELECT foo3_2e1(

-- 3.3 Create and invoke function with different SQL configurations
SET spark.sql.ansi.enabled=true;
DROP FUNCTION IF EXISTS foo3_3at;
CREATE FUNCTION foo3_3a(x INT) RETURNS DOUBLE RETURN 1 / x;
CREATE FUNCTION foo3_3at(x INT) RETURNS TABLE (a DOUBLE) RETURN SELECT 1 / x;
CREATE TEMPORARY FUNCTION foo3_3b(x INT) RETURNS DOUBLE RETURN 1 / x;
37 changes: 11 additions & 26 deletions sql/core/src/test/resources/sql-tests/results/sql-udf.sql.out
Original file line number Diff line number Diff line change
@@ -2460,6 +2460,14 @@ struct<key:string,value:string>
spark.sql.ansi.enabled true


-- !query
DROP FUNCTION IF EXISTS foo3_3at
-- !query schema
struct<>
-- !query output



-- !query
CREATE FUNCTION foo3_3a(x INT) RETURNS DOUBLE RETURN 1 / x
-- !query schema
@@ -2571,16 +2579,7 @@ CREATE OR REPLACE FUNCTION foo3_3at(x INT) RETURNS TABLE (a DOUBLE) RETURN SELEC
-- !query schema
struct<>
-- !query output
org.apache.spark.sql.catalyst.analysis.FunctionAlreadyExistsException
{
"errorClass" : "ROUTINE_ALREADY_EXISTS",
"sqlState" : "42723",
"messageParameters" : {
"existingRoutineType" : "routine",
"newRoutineType" : "routine",
"routineName" : "`default`.`foo3_3at`"
}
}



-- !query
@@ -2610,23 +2609,9 @@ NULL
-- !query
SELECT * FROM foo3_3at(0)
-- !query schema
struct<>
struct<a:double>
-- !query output
org.apache.spark.SparkArithmeticException
{
"errorClass" : "DIVIDE_BY_ZERO",
"sqlState" : "22012",
"messageParameters" : {
"config" : "\"spark.sql.ansi.enabled\""
},
"queryContext" : [ {
"objectType" : "",
"objectName" : "",
"startIndex" : 8,
"stopIndex" : 12,
"fragment" : "1 / x"
} ]
}
NULL


-- !query