Skip to content

Commit

Permalink
sql/logictest: add more tests for UDFs calling UDFs
Browse files Browse the repository at this point in the history
This commit rearranges some existing tests and adds a few new tests for
UDFs that invoke other UDFs.

Release note: None
  • Loading branch information
mgartner committed Mar 20, 2024
1 parent ecedafa commit a2baac9
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 25 deletions.
17 changes: 0 additions & 17 deletions pkg/sql/logictest/testdata/logic_test/procedure
Original file line number Diff line number Diff line change
Expand Up @@ -412,20 +412,3 @@ statement error pgcode 42P13 null input attribute not allowed in procedure defin
CREATE PROCEDURE pv() AS 'SELECT 1' STRICT LANGUAGE SQL;

subtest end


subtest udf_calling_udfs

# Validate we can have UDF's both in the select and from clauses.
statement ok
CREATE FUNCTION udfCall(i int) RETURNS INT LANGUAGE SQL AS 'SELECT 100+i';
CREATE FUNCTION udfCallNest(i int, j int) RETURNS INT LANGUAGE SQL AS 'SELECT udfCall(i) + j';
CREATE FUNCTION udfCallNest_2(i int, j int) RETURNS INT LANGUAGE SQL AS 'SELECT udfCall(i) + udfCall(j) + udfCallNest(i, j)';
CREATE FUNCTION udfCallNest_3(i int, j int) RETURNS INT LANGUAGE SQL AS 'SELECT udfCall(j) + udfCallNest(i, j) + udfCallNest_2(i, j) + 1 FROM udfCallNest_2(i, j)';

query I
SELECT * FROM udfCallNest_3(1, 2)
----
512

subtest end
92 changes: 92 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/udf_calling_udf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# LogicTest: !local-mixed-23.1

statement ok
CREATE FUNCTION lower_hello() RETURNS STRING LANGUAGE SQL AS $$ SELECT lower('hello') $$;

Expand All @@ -15,10 +17,100 @@ SELECT upper_hello(), nested_udf_for_from(), lower_hello(), concat_hello()
----
HELLO HELLO hello HELLOHELLOhello

# Validate we can have UDF's both in the select and from clauses.
statement ok
CREATE FUNCTION udfCall(i int) RETURNS INT LANGUAGE SQL AS 'SELECT 100+i';
CREATE FUNCTION udfCallNest(i int, j int) RETURNS INT LANGUAGE SQL AS 'SELECT udfCall(i) + j';
CREATE FUNCTION udfCallNest_2(i int, j int) RETURNS INT LANGUAGE SQL AS 'SELECT udfCall(i) + udfCall(j) + udfCallNest(i, j)';
CREATE FUNCTION udfCallNest_3(i int, j int) RETURNS INT LANGUAGE SQL AS 'SELECT udfCall(j) + udfCallNest(i, j) + udfCallNest_2(i, j) + 1 FROM udfCallNest_2(i, j)';

query I
SELECT * FROM udfCallNest_3(1, 2)
----
512

# Validate recursion doesn't work today.
statement error pgcode 42883 unknown function: recursion_check\(\)
CREATE FUNCTION recursion_check() RETURNS STRING LANGUAGE SQL AS $$ SELECT recursion_check() $$;

# Validate that function renaming is blocked.
statement error pgcode 0A000 cannot rename function \"lower_hello\" because other functions \(\[test.public.upper_hello, test.public.concat_hello\]\) still depend on it
ALTER FUNCTION lower_hello rename to lower_hello_new

statement ok
CREATE FUNCTION f() RETURNS INT LANGUAGE SQL AS $$
SELECT 1;
$$

statement ok
CREATE FUNCTION g() RETURNS INT LANGUAGE SQL AS $$
SELECT f();
$$

# Mutual recursion is not currently allowed.
statement error pgcode 42P13 cannot add dependency from descriptor \d+ to function g \(\d+\) because there will be a dependency cycle
CREATE OR REPLACE FUNCTION f() RETURNS INT LANGUAGE SQL AS $$
SELECT g();
$$

statement ok
DROP FUNCTION g();
DROP FUNCTION f();

statement ok
CREATE TABLE ab (
a INT PRIMARY KEY,
b INT
)

statement ok
CREATE FUNCTION ins_ab(new_a INT, new_b INT) RETURNS INT LANGUAGE SQL AS $$
INSERT INTO ab VALUES (new_a, new_b) RETURNING a;
$$

statement ok
CREATE FUNCTION ins(new_a INT) RETURNS INT LANGUAGE SQL AS $$
SELECT ins_ab(new_a, new_a * 10);
SELECT b FROM ab WHERE a = new_a;
$$

query I rowsort
SELECT ins(i) FROM generate_series(1, 3) g(i)
----
10
20
30

query II
SELECT ins(5), ins(6) FROM (VALUES (1)) v(i) WHERE i < ins(4)
----
50 60

query II rowsort
SELECT * FROM ab
----
1 10
2 20
3 30
4 40
5 50
6 60

statement error pgcode 23505 duplicate key value violates unique constraint \"ab_pkey\"
SELECT ins(4)

skipif config local-legacy-schema-changer
statement error pgcode 2BP01 cannot drop table ab because other objects depend on it
DROP TABLE ab

onlyif config local-legacy-schema-changer
statement error pgcode 2BP01 pq: cannot drop relation \"ab\" because function \"ins\" depends on it\nHINT: consider dropping \"ins\" first.
DROP TABLE ab

statement error pgcode 2BP01 cannot drop function \"ins_ab\" because other objects \(\[test.public.ins\]\) still depend on it
DROP FUNCTION ins_ab

statement ok
DROP FUNCTION ins;
DROP FUNCTION ins_ab;
DROP TABLE ab;
7 changes: 0 additions & 7 deletions pkg/sql/logictest/tests/local-mixed-23.1/generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/sql/opt/optbuilder/routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func (b *Builder) buildRoutine(
}
// Add a VALUES (NULL) statement if the return type of the function is
// VOID. We cannot simply project NULL from the last statement because
// all column would be pruned and the contents of last statement would
// all columns would be pruned and the contents of last statement would
// not be executed.
// TODO(mgartner): This will add some planning overhead for every
// invocation of the function. Is there a more efficient way to do this?
Expand Down

0 comments on commit a2baac9

Please sign in to comment.