Skip to content

Commit

Permalink
Merge #97755
Browse files Browse the repository at this point in the history
97755: sql: set name prefix before resolving a function r=chengxiong-ruan a=chengxiong-ruan

Fixes: #97400

Previously, in desclarative schema changer, we would run into a builtin function when we try to create a UDF with a same builtin function signature. This is because we didn't set the name prefixes properly. This commit fixes this by setting the name prefixes using the prefix re-resolution result.

Release note: None

Co-authored-by: Chengxiong Ruan <[email protected]>
  • Loading branch information
craig[bot] and chengxiong-ruan committed Mar 2, 2023
2 parents bc46049 + 38173eb commit 2cc95c9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
12 changes: 5 additions & 7 deletions pkg/bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1443,8 +1443,7 @@ func BenchmarkFuncExprTypeCheck(b *testing.B) {
sqlDB.ExecMultiple(b,
`CREATE SCHEMA sc1`,
`CREATE SCHEMA sc2`,
// TODO(chengxiong): uncomment this when #97400 is resolved.
//`CREATE FUNCTION abs(val INT) RETURNS INT CALLED ON NULL INPUT LANGUAGE SQL AS $$ SELECT val $$`,
`CREATE FUNCTION abs(val INT) RETURNS INT CALLED ON NULL INPUT LANGUAGE SQL AS $$ SELECT val $$`,
`CREATE FUNCTION sc1.udf(val INT) RETURNS INT CALLED ON NULL INPUT LANGUAGE SQL AS $$ SELECT val $$`,
`CREATE FUNCTION sc1.udf(val STRING) RETURNS STRING LANGUAGE SQL AS $$ SELECT val $$`,
`CREATE FUNCTION sc1.udf(val FLOAT) RETURNS FLOAT LANGUAGE SQL AS $$ SELECT val $$`,
Expand Down Expand Up @@ -1488,11 +1487,10 @@ func BenchmarkFuncExprTypeCheck(b *testing.B) {
name: "builtin aggregate not called on null",
exprStr: "concat_agg(NULL)",
},
// TODO(chengxiong): uncomment this when #97400 is resolved.
//{
// name: "udf same name as builtin",
// exprStr: "abs(123)",
//},
{
name: "udf same name as builtin",
exprStr: "abs(123)",
},
{
name: "udf across different schemas",
exprStr: "udf(123)",
Expand Down
19 changes: 19 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/udf
Original file line number Diff line number Diff line change
Expand Up @@ -3133,3 +3133,22 @@ SELECT f_97130();

statement ok
SET search_path = $pre_search_path

subtest regression_97400

# Make sure that creating a UDF with builtin function name is ok.
statement ok
CREATE FUNCTION abs(val INT) RETURNS INT
CALLED ON NULL INPUT
LANGUAGE SQL
AS $$ SELECT val+100 $$;

query I
SELECT abs(-1)
----
1

query I
SELECT public.abs(-1)
----
99
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ func CreateFunction(b BuildCtx, n *tree.CreateFunction) {
dbElts, scElts := b.ResolvePrefix(n.FuncName.ObjectNamePrefix, privilege.CREATE)
_, _, sc := scpb.FindSchema(scElts)
_, _, db := scpb.FindDatabase(dbElts)
_, _, scName := scpb.FindNamespace(scElts)
_, _, dbname := scpb.FindNamespace(dbElts)

validateParameters(b, n, db.DatabaseID)
n.FuncName.SchemaName = tree.Name(scName.Name)
n.FuncName.CatalogName = tree.Name(dbname.Name)

validateParameters(n)

existingFn := b.ResolveUDF(
&tree.FuncObj{
Expand Down Expand Up @@ -142,7 +147,7 @@ func CreateFunction(b BuildCtx, n *tree.CreateFunction) {
b.Add(b.WrapFunctionBody(fnID, fnBodyStr, lang, refProvider))
}

func validateParameters(b BuildCtx, n *tree.CreateFunction, parentDBID descpb.ID) {
func validateParameters(n *tree.CreateFunction) {
seen := make(map[tree.Name]struct{})
for _, param := range n.Params {
if param.Name != "" {
Expand Down

0 comments on commit 2cc95c9

Please sign in to comment.