Skip to content

Commit

Permalink
sql: delete function name key from schema if no overload left after drop
Browse files Browse the repository at this point in the history
Backport fixes: cockroachdb#89046
Previously we just remove an overload from the slice when dropping a
function. This is problematic if there's zero overloads left after the
drop because it pretends that there is some function with the name but
actually nothing. So we need to delete the key if there is not overload
for the name.

Release note: None
Release justification: GA blocker bug fix.
  • Loading branch information
chengxiong-ruan committed Oct 3, 2022
1 parent 240675d commit 7a9e8b6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/catalog/schemadesc/schema_desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ func (desc *Mutable) RemoveFunction(name string, id descpb.ID) {
updated = append(updated, ol)
}
}
if len(updated) == 0 {
delete(desc.Functions, name)
return
}
desc.Functions[name] = descpb.SchemaDescriptor_Function{
Name: name,
Overloads: updated,
Expand Down
28 changes: 22 additions & 6 deletions pkg/sql/logictest/testdata/logic_test/udf
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ $$
statement ok
DROP FUNCTION f_test_drop(INT), f_test_drop(INT);

statement error pq: function public.f_test_drop does not exist
statement error pq: unknown function: public.f_test_drop\(\): function undefined
SELECT @2 FROM [SHOW CREATE FUNCTION public.f_test_drop];

query T
Expand All @@ -473,7 +473,7 @@ $$
statement ok
DROP FUNCTION f_test_drop(INT);

statement error pq: function sc1.f_test_drop does not exist
statement error pq: unknown function: sc1.f_test_drop\(\): function undefined
SELECT @2 FROM [SHOW CREATE FUNCTION sc1.f_test_drop];

# If there are identical function signatures in different schemas, multiple drop
Expand Down Expand Up @@ -514,10 +514,10 @@ DROP FUNCTION f_test_drop();
DROP FUNCTION f_test_drop();
COMMIT;

statement error pq: function public.f_test_drop does not exist
statement error pq: unknown function: public.f_test_drop\(\): function undefined
SELECT @2 FROM [SHOW CREATE FUNCTION public.f_test_drop];

statement error pq: function sc1.f_test_drop does not exist
statement error pq: unknown function: sc1.f_test_drop\(\): function undefined
SELECT @2 FROM [SHOW CREATE FUNCTION sc1.f_test_drop];

statement ok
Expand Down Expand Up @@ -1044,7 +1044,7 @@ ALTER FUNCTION f_test_alter_name RENAME TO f_test_alter_name_same_in
statement ok
ALTER FUNCTION f_test_alter_name RENAME TO f_test_alter_name_new

statement error pq: function f_test_alter_name does not exist
statement error pq: unknown function: f_test_alter_name\(\): function undefined
SELECT @2 FROM [SHOW CREATE FUNCTION f_test_alter_name];

query T
Expand All @@ -1063,7 +1063,7 @@ $$
statement ok
ALTER FUNCTION f_test_alter_name_new RENAME to f_test_alter_name_diff_in

statement error pq: function f_test_alter_name_new does not exist
statement error pq: unknown function: f_test_alter_name_new\(\): function undefined
SELECT @2 FROM [SHOW CREATE FUNCTION f_test_alter_name_new];

query T
Expand Down Expand Up @@ -2501,3 +2501,19 @@ subtest variadic
# Variadic UDFS are not currently supported.
statement error pgcode 0A000 unimplemented: this syntax\nHINT.*\n.*88947
CREATE FUNCTION rec(VARIADIC arr INT[]) RETURNS INT LANGUAGE SQL AS '1'

subtest execute_dropped_function

statement ok
CREATE FUNCTION f_test_exec_dropped(a int) RETURNS INT LANGUAGE SQL AS $$ SELECT a $$;

query I
SELECT f_test_exec_dropped(123);
----
123

statement ok
DROP FUNCTION f_test_exec_dropped;

statement error pq: unknown function: f_test_exec_dropped\(\): function undefined
SELECT f_test_exec_dropped(321);
7 changes: 6 additions & 1 deletion pkg/sql/schema_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,12 @@ func (sr *schemaResolver) ResolveFunction(
// name which is not lowercase. So here we try to lowercase the given
// function name and find a suggested function name if possible.
extraMsg := ""
lowerName := tree.MakeUnresolvedName(strings.ToLower(name.Parts[0]))
var lowerName tree.UnresolvedName
if fn.ExplicitSchema {
tree.MakeUnresolvedName(strings.ToLower(name.Parts[0]), strings.ToLower(name.Parts[1]))
} else {
tree.MakeUnresolvedName(strings.ToLower(name.Parts[0]))
}
if lowerName != *name {
alternative, err := sr.ResolveFunction(ctx, &lowerName, path)
if err == nil && alternative != nil {
Expand Down

0 comments on commit 7a9e8b6

Please sign in to comment.