Skip to content

Commit

Permalink
Merge pull request #94959 from rafiss/fix-pg-func-is-visible
Browse files Browse the repository at this point in the history
  • Loading branch information
rafiss authored Jan 10, 2023
2 parents 05e4853 + d56a08a commit 7346058
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
41 changes: 36 additions & 5 deletions pkg/sql/logictest/testdata/logic_test/pg_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,26 @@ NULL
statement ok
CREATE TABLE is_visible(a int primary key);
CREATE TYPE visible_type AS ENUM('a');
CREATE FUNCTION visible_func() RETURNS INT LANGUAGE SQL AS $$ SELECT 1 $$;
CREATE SCHEMA other;
CREATE TABLE other.not_visible(a int primary key);
CREATE TYPE other.not_visible_type AS ENUM('b');
CREATE FUNCTION other.not_visible_func() RETURNS INT LANGUAGE SQL AS $$ SELECT 1 $$;
CREATE DATABASE db2;
SET DATABASE = db2;
CREATE TABLE table_in_db2(a int primary key);
CREATE TYPE type_in_db2 AS ENUM('c');
CREATE FUNCTION func_in_db2() RETURNS INT LANGUAGE SQL AS $$ SELECT 1 $$;

let $table_in_db2_id
SELECT c.oid FROM pg_class c WHERE c.relname = 'table_in_db2';

let $type_in_db2_id
SELECT t.oid FROM pg_type t WHERE t.typname = 'type_in_db2';

let $func_in_db2_id
SELECT p.oid FROM pg_proc p WHERE p.proname = 'func_in_db2';

statement ok
SET DATABASE = test;

Expand Down Expand Up @@ -148,6 +154,31 @@ SELECT pg_type_is_visible(NULL)
----
NULL

query TB rowsort
SELECT p.proname, pg_function_is_visible(p.oid)
FROM pg_proc p
WHERE p.proname IN ('array_length', 'visible_func', 'not_visible_func')
----
array_length true
visible_func true
not_visible_func false

# Looking up a function in a different database should return NULL.
query B
SELECT pg_function_is_visible($func_in_db2_id)
----
NULL

# Looking up a non-existent OID should return NULL.
query B
SELECT pg_function_is_visible(1010101010)
----
NULL

query B
SELECT pg_function_is_visible(NULL)
----
NULL

query TT
SELECT pg_get_partkeydef(1), pg_get_partkeydef(NULL)
Expand All @@ -172,11 +203,11 @@ WHERE c.relname IN ('is_updatable', 'is_updatable_view', 'pg_class')
ORDER BY c.oid, a.attnum
----
relname attname oid attnum pg_relation_is_updatable pg_column_is_updatable
is_updatable a 120 1 28 true
is_updatable b 120 2 28 true
is_updatable c 120 3 28 false
is_updatable_view a 121 1 0 false
is_updatable_view b 121 2 0 false
is_updatable a 123 1 28 true
is_updatable b 123 2 28 true
is_updatable c 123 3 28 false
is_updatable_view a 124 1 0 false
is_updatable_view b 124 2 0 false
pg_class oid 4294967123 1 0 false
pg_class relname 4294967123 2 0 false
pg_class relnamespace 4294967123 3 0 false
Expand Down
9 changes: 6 additions & 3 deletions pkg/sql/sem/builtins/pg_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1185,16 +1185,19 @@ SELECT description
Types: tree.ArgTypes{{"oid", types.Oid}},
ReturnType: tree.FixedReturnType(types.Bool),
Fn: func(ctx *eval.Context, args tree.Datums) (tree.Datum, error) {
oid := tree.MustBeDOid(args[0])
dOid := tree.MustBeDOid(args[0])
t, err := ctx.Planner.QueryRowEx(
ctx.Ctx(), "pg_function_is_visible",
sessiondata.NoSessionDataOverride,
"SELECT * from pg_proc WHERE oid=$1 LIMIT 1", oid.Oid)
`SELECT n.nspname = ANY current_schemas(true) FROM pg_proc p
INNER JOIN pg_namespace n ON p.pronamespace = n.oid WHERE p.oid=$1 LIMIT 1`,
dOid,
)
if err != nil {
return nil, err
}
if t != nil {
return tree.DBoolTrue, nil
return tree.MakeDBool(tree.MustBeDBool(t[0])), nil
}
return tree.DNull, nil
},
Expand Down

0 comments on commit 7346058

Please sign in to comment.