Skip to content

Commit b959f49

Browse files
committed
sql: add pg error codes to udf logic tests
Used postgres where possible to generate the error codes. A few questionable codes have been marked with TODOs to be addressed in future work. Epic: none Informs: cockroachdb#107369 Release note: none
1 parent f8ff565 commit b959f49

File tree

17 files changed

+149
-140
lines changed

17 files changed

+149
-140
lines changed

pkg/sql/logictest/testdata/logic_test/udf

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ CREATE TYPE notmyworkday AS ENUM ('Monday', 'Tuesday');
1818

1919
subtest usage
2020

21-
statement error pq: no language specified
21+
statement error pgcode 42P13 pq: no language specified
2222
CREATE FUNCTION f() RETURNS INT IMMUTABLE AS $$ SELECT 1 $$;
2323

24-
statement error pq: no function body specified
24+
statement error pgcode 42P13 pq: no function body specified
2525
CREATE FUNCTION f() RETURNS INT IMMUTABLE LANGUAGE SQL;
2626

2727

@@ -34,7 +34,7 @@ CREATE FUNCTION b(i INT) RETURNS INT LANGUAGE SQL AS 'SELECT a FROM ab WHERE a =
3434
statement ok
3535
CREATE FUNCTION c(i INT, j INT) RETURNS INT LANGUAGE SQL AS 'SELECT i - j'
3636

37-
statement error column \"j\" does not exist
37+
statement error pgcode 42703 column \"j\" does not exist
3838
CREATE FUNCTION err(i INT) RETURNS INT LANGUAGE SQL AS 'SELECT j'
3939

4040
statement error pgcode 42703 column \"j\" does not exist
@@ -46,13 +46,13 @@ CREATE FUNCTION err() RETURNS INT LANGUAGE SQL AS 'SELECT a FROM dne'
4646
statement ok
4747
CREATE FUNCTION d(i INT2) RETURNS INT4 LANGUAGE SQL AS 'SELECT i'
4848

49-
statement error return type mismatch in function declared to return bool\nDETAIL: Actual return type is int
49+
statement error pgcode 42P13 return type mismatch in function declared to return bool\nDETAIL: Actual return type is int
5050
CREATE FUNCTION err(i INT, j INT) RETURNS BOOL LANGUAGE SQL AS 'SELECT i - j'
5151

52-
statement error return type mismatch in function declared to return int\nDETAIL: Actual return type is bool
52+
statement error pgcode 42P13 return type mismatch in function declared to return int\nDETAIL: Actual return type is bool
5353
CREATE FUNCTION err(b BOOL) RETURNS INT LANGUAGE SQL AS 'SELECT b'
5454

55-
statement error return type mismatch in function declared to return bool\nDETAIL: Actual return type is int
55+
statement error pgcode 42P13 return type mismatch in function declared to return bool\nDETAIL: Actual return type is int
5656
CREATE FUNCTION err(i INT, j INT) RETURNS BOOL LANGUAGE SQL AS 'SELECT i - j'
5757

5858
# TODO(#100962): Add support for default parameters.
@@ -64,7 +64,7 @@ CREATE FUNCTION err(i INT, j INT DEFAULT 2) RETURNS INT LANGUAGE SQL AS 'SELECT
6464
statement ok
6565
CREATE TABLE t_implicit_type(a INT PRIMARY KEY, b STRING);
6666

67-
statement error pq: return type mismatch in function declared to return int\nDETAIL: Actual return type is record
67+
statement error pgcode 42P13 pq: return type mismatch in function declared to return int\nDETAIL: Actual return type is record
6868
CREATE FUNCTION f() RETURNS INT IMMUTABLE LANGUAGE SQL AS $$ SELECT a, b from t_implicit_type $$
6969

7070
# Create function with no references.
@@ -120,7 +120,7 @@ CREATE FUNCTION public.f(IN a test.public.notmyworkday)
120120
SELECT nextval('public.sq1'::REGCLASS);
121121
$$
122122

123-
statement error pq: unimplemented: alter function depends on extension not supported.*
123+
statement error pgcode 0A000 pq: unimplemented: alter function depends on extension not supported.*
124124
ALTER FUNCTION f() DEPENDS ON EXTENSION postgis
125125

126126
subtest end
@@ -277,7 +277,7 @@ CREATE FUNCTION public.proc_f(IN STRING, IN b INT8)
277277
SELECT 'hello';
278278
$$
279279

280-
statement error pq: unknown function: proc_f_2()
280+
statement error pgcode 42883 pq: unknown function: proc_f_2()
281281
SHOW CREATE FUNCTION proc_f_2;
282282

283283
query T
@@ -330,10 +330,10 @@ SELECT 'sc.proc_f_2'::REGPROC::INT;
330330
----
331331
100121
332332

333-
statement error pq: unknown function: no_such_func()
333+
statement error pgcode 42883 pq: unknown function: no_such_func()
334334
SELECT 'no_such_func'::REGPROC;
335335

336-
statement error pq: more than one function named 'proc_f'
336+
statement error pgcode 42P09 pq: more than one function named 'proc_f'
337337
SELECT 'proc_f'::REGPROC;
338338

339339
query T
@@ -406,17 +406,18 @@ CREATE FUNCTION sc1.f_test_drop(IN INT8)
406406
SELECT 1;
407407
$$
408408

409-
statement error pq: function name \"f_test_drop\" is not unique
409+
# TODO(107369): Postgres errors with code 42725 here.
410+
statement error pgcode XXUUU pq: function name \"f_test_drop\" is not unique
410411
DROP FUNCTION f_test_drop;
411412

412413
statement ok
413414
DROP FUNCTION IF EXISTS f_not_existing;
414415

415-
statement error pq: unknown function: f_not_existing\(\): function undefined
416+
statement error pgcode 42883 pq: unknown function: f_not_existing\(\): function undefined
416417
DROP FUNCTION f_not_existing;
417418

418419
# drop a function twice should fail.
419-
statement error pq: function f_test_drop\(\) does not exist: function undefined
420+
statement error pgcode 42883 pq: function f_test_drop\(\) does not exist: function undefined
420421
BEGIN;
421422
DROP FUNCTION f_test_drop();
422423
DROP FUNCTION f_test_drop();
@@ -459,7 +460,7 @@ $$
459460
statement ok
460461
DROP FUNCTION f_test_drop(INT), f_test_drop(INT);
461462

462-
statement error pq: unknown function: public.f_test_drop\(\): function undefined
463+
statement error pgcode 42883 pq: unknown function: public.f_test_drop\(\): function undefined
463464
SELECT create_statement FROM [SHOW CREATE FUNCTION public.f_test_drop];
464465

465466
query T
@@ -478,7 +479,7 @@ $$
478479
statement ok
479480
DROP FUNCTION f_test_drop(INT);
480481

481-
statement error pq: unknown function: sc1.f_test_drop\(\): function undefined
482+
statement error pgcode 42883 pq: unknown function: sc1.f_test_drop\(\): function undefined
482483
SELECT create_statement FROM [SHOW CREATE FUNCTION sc1.f_test_drop];
483484

484485
# If there are identical function signatures in different schemas, multiple drop
@@ -519,10 +520,10 @@ DROP FUNCTION f_test_drop();
519520
DROP FUNCTION f_test_drop();
520521
COMMIT;
521522

522-
statement error pq: unknown function: public.f_test_drop\(\): function undefined
523+
statement error pgcode 42883 pq: unknown function: public.f_test_drop\(\): function undefined
523524
SELECT create_statement FROM [SHOW CREATE FUNCTION public.f_test_drop];
524525

525-
statement error pq: unknown function: sc1.f_test_drop\(\): function undefined
526+
statement error pgcode 42883 pq: unknown function: sc1.f_test_drop\(\): function undefined
526527
SELECT create_statement FROM [SHOW CREATE FUNCTION sc1.f_test_drop];
527528

528529
statement ok
@@ -547,30 +548,30 @@ SELECT f_test_exec_dropped(123);
547548
statement ok
548549
DROP FUNCTION f_test_exec_dropped;
549550

550-
statement error pq: unknown function: f_test_exec_dropped\(\): function undefined
551+
statement error pgcode 42883 pq: unknown function: f_test_exec_dropped\(\): function undefined
551552
SELECT f_test_exec_dropped(321);
552553

553554
subtest end
554555

555556

556557
subtest create_or_replace_function
557558

558-
statement error pq: parameter name "a" used more than once
559+
statement error pgcode 42P13 pq: parameter name "a" used more than once
559560
CREATE FUNCTION f_test_cor(a INT, a INT) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$ SELECT 1 $$;
560561

561562
statement ok
562563
CREATE FUNCTION f_test_cor(a INT, b INT) RETURNS INT IMMUTABLE LEAKPROOF STRICT LANGUAGE SQL AS $$ SELECT 1 $$;
563564

564-
statement error pq: function "f_test_cor" already exists with same argument types
565+
statement error pgcode 42723 pq: function "f_test_cor" already exists with same argument types
565566
CREATE FUNCTION f_test_cor(a INT, b INT) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$ SELECT 1 $$;
566567

567568
statement ok
568569
CREATE OR REPLACE FUNCTION f_test_cor_not_exist(a INT, b INT) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$ SELECT 1 $$;
569570

570-
statement error pq: cannot change name of input parameter "b"
571+
statement error pgcode 42P13 pq: cannot change name of input parameter "b"
571572
CREATE OR REPLACE FUNCTION f_test_cor(a INT, c INT) RETURNS INT IMMUTABLE LANGUAGE SQL AS $$ SELECT 1 $$;
572573

573-
statement error pq: cannot change return type of existing function
574+
statement error pgcode 42P13 pq: cannot change return type of existing function
574575
CREATE OR REPLACE FUNCTION f_test_cor(a INT, b INT) RETURNS STRING IMMUTABLE LANGUAGE SQL AS $$ SELECT 'hello' $$;
575576

576577
statement error pgcode 42P13 leak proof function must be immutable, but got volatility: VOLATILE
@@ -632,7 +633,7 @@ statement ok
632633
CREATE SCHEMA sc_seq_qualified_name;
633634
CREATE SEQUENCE sc_seq_qualified_name.sq;
634635

635-
statement error pq: relation "sc_seq_qualified_name.sq" does not exist
636+
statement error pgcode 42P01 pq: relation "sc_seq_qualified_name.sq" does not exist
636637
CREATE FUNCTION f_seq_qualified_name() RETURNS INT LANGUAGE SQL AS $$ SELECT * FROM nextval('"sc_seq_qualified_name.sq"') $$;
637638

638639
statement ok
@@ -835,18 +836,18 @@ subtest end
835836

836837
subtest args
837838

838-
statement error pq: SQL functions cannot have arguments of type record
839+
statement error pgcode 42P13 pq: SQL functions cannot have arguments of type record
839840
CREATE FUNCTION f(r RECORD) RETURNS INT LANGUAGE SQL AS 'SELECT i'
840841

841842
# TODO(mgartner): Technically $3 is a parameter, and the error message should be
842843
# more similar to Postgres's "there is no parameter $3".
843-
statement error no value provided for placeholder: \$3
844+
statement error pgcode 42P02 no value provided for placeholder: \$3
844845
CREATE FUNCTION err(x INT, y INT) RETURNS INT LANGUAGE SQL AS 'SELECT x + y + $1 + $2 + $3'
845846

846-
statement error no value provided for placeholder: \$3
847+
statement error pgcode 42P02 no value provided for placeholder: \$3
847848
CREATE FUNCTION err(INT, INT) RETURNS INT LANGUAGE SQL AS 'SELECT $1 + $2 + $3'
848849

849-
statement error placeholder index must be between 1 and 65536
850+
statement error pgcode 42601 placeholder index must be between 1 and 65536
850851
CREATE FUNCTION err(INT) RETURNS INT LANGUAGE SQL AS 'SELECT 1 + $0'
851852

852853
statement ok
@@ -1014,7 +1015,7 @@ SELECT single_quote('hello')
10141015
----
10151016
'hello'
10161017

1017-
statement error unterminated string
1018+
statement error pgcode 42601 unterminated string
10181019
CREATE FUNCTION err() RETURNS STRING LANGUAGE SQL AS $outer$
10191020
SELECT $inner$hello$outer$
10201021
$inner$

pkg/sql/logictest/testdata/logic_test/udf_delete

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ INSERT INTO kv VALUES (1, 2), (3, 4), (5, 6), (7, 8)
1212
statement ok
1313
CREATE VIEW kview AS SELECT k,v FROM kv
1414

15-
statement error pq: "test.public.kview" is not a table
15+
statement error pgcode 42809 pq: "test.public.kview" is not a table
1616
CREATE FUNCTION f_view() RETURNS RECORD AS
1717
$$
1818
DELETE FROM kview;

pkg/sql/logictest/testdata/logic_test/udf_in_column_defaults

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ CREATE TABLE t2(
355355
FAMILY fam_0 (a, b)
356356
);
357357

358-
statement error cannot drop function "f1" because other objects \(\[test.public.t1, test.public.t2\]\) still depend on it
358+
statement error pgcode 2BP01 cannot drop function "f1" because other objects \(\[test.public.t1, test.public.t2\]\) still depend on it
359359
DROP FUNCTION f1;
360360

361361
statement ok
@@ -508,5 +508,5 @@ statement ok
508508
CREATE TABLE t_circle(a INT PRIMARY KEY, b INT);
509509
CREATE FUNCTION f_circle() RETURNS INT LANGUAGE SQL AS $$ SELECT a FROM t_circle $$;
510510

511-
statement error pq: cannot add dependency from descriptor \d+ to function f_circle \(\d+\) because there will be a dependency cycle
511+
statement error pgcode XXUUU pq: cannot add dependency from descriptor \d+ to function f_circle \(\d+\) because there will be a dependency cycle
512512
ALTER TABLE t_circle ALTER COLUMN b SET DEFAULT f_circle();

pkg/sql/logictest/testdata/logic_test/udf_in_constraints

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ CREATE TABLE t2(
229229
FAMILY fam_0 (a, b)
230230
);
231231

232-
statement error cannot drop function "f1" because other objects \(\[test.public.t1, test.public.t2\]\) still depend on it
232+
statement error pgcode 2BP01 cannot drop function "f1" because other objects \(\[test.public.t1, test.public.t2\]\) still depend on it
233233
DROP FUNCTION f1;
234234

235235
statement ok
@@ -368,26 +368,26 @@ CREATE TABLE t1 (
368368
FAMILY fam_0 (a, b)
369369
);
370370

371-
statement error failed to satisfy CHECK constraint \(public\.f1\(b\) > 1:::INT8\)
371+
statement error pgcode 23514 failed to satisfy CHECK constraint \(public\.f1\(b\) > 1:::INT8\)
372372
INSERT INTO t1 VALUES (1,0);
373373

374374
statement ok
375375
INSERT INTO t1 VALUES (1,1);
376376

377-
statement error validation of CHECK "public\.f1\(a\) > 10:::INT8" failed on row: a=1, b=1
377+
statement error pgcode 23514 validation of CHECK "public\.f1\(a\) > 10:::INT8" failed on row: a=1, b=1
378378
ALTER TABLE t1 ADD CONSTRAINT cka CHECK (f1(a) > 10);
379379

380380
# Make sure that constraint still works after a function is renamed.
381381
statement ok
382382
ALTER TABLE t1 ADD CONSTRAINT cka CHECK (f1(a) > 1);
383383

384-
statement error pq: failed to satisfy CHECK constraint \(public\.f1\(b\) > 1:::INT8\)
384+
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(public\.f1\(b\) > 1:::INT8\)
385385
INSERT INTO t1 VALUES (2, -1);
386386

387387
statement ok
388388
ALTER FUNCTION f1 RENAME to f2;
389389

390-
statement error pq: failed to satisfy CHECK constraint \(public\.f2\(b\) > 1:::INT8\)
390+
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(public\.f2\(b\) > 1:::INT8\)
391391
INSERT INTO t1 VALUES (2, -1);
392392

393393
query T
@@ -433,5 +433,6 @@ statement ok
433433
CREATE TABLE t_circle(a INT PRIMARY KEY, b INT);
434434
CREATE FUNCTION f_circle() RETURNS INT LANGUAGE SQL AS $$ SELECT a FROM t_circle $$;
435435

436+
# TODO(107369): This does not appear to error in postgres.
436437
statement error .*cannot add dependency from descriptor \d+ to function f_circle \(\d+\) because there will be a dependency cycle
437438
ALTER TABLE t_circle ADD CONSTRAINT ckb CHECK (b + f_circle() > 1);

pkg/sql/logictest/testdata/logic_test/udf_insert

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
statement ok
22
CREATE TABLE t (a INT PRIMARY KEY, b INT DEFAULT 0);
33

4-
statement error pq: return type mismatch in function declared to return record\nDETAIL: Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.
4+
statement error pgcode 42P13 pq: return type mismatch in function declared to return record\nDETAIL: Function's final statement must be SELECT or INSERT/UPDATE/DELETE RETURNING.
55
CREATE FUNCTION f_err() RETURNS RECORD AS
66
$$
77
INSERT INTO t VALUES (1,2);
@@ -18,20 +18,22 @@ SELECT f_void();
1818
----
1919
NULL
2020

21-
statement error missing "a" primary key column
21+
# TODO(107369): This doesn't error in postgres, and this is the error code for
22+
# invalid foreign key, which may not make sense.
23+
statement error pgcode 42830 missing "a" primary key column
2224
CREATE FUNCTION f_err(b INT) RETURNS RECORD AS
2325
$$
2426
INSERT INTO t (b) VALUES (b);
2527
SELECT * FROM t WHERE t.a=a AND t.b=b;
2628
$$ LANGUAGE SQL;
2729

28-
statement error pq: volatile statement not allowed in stable function
30+
statement error pgcode 22023 pq: volatile statement not allowed in stable function
2931
CREATE FUNCTION f_err(i INT, j INT) RETURNS RECORD STABLE AS
3032
$$
3133
INSERT INTO t VALUES (i,j) RETURNING *;
3234
$$ LANGUAGE SQL;
3335

34-
statement error pq: volatile statement not allowed in immutable function
36+
statement error pgcode 22023 pq: volatile statement not allowed in immutable function
3537
CREATE FUNCTION f_err(i INT, j INT) RETURNS RECORD IMMUTABLE AS
3638
$$
3739
INSERT INTO t VALUES (i,j) RETURNING *;
@@ -54,7 +56,7 @@ SELECT f_insert(3,4);
5456
----
5557
(3,4)
5658

57-
statement error duplicate key
59+
statement error pgcode 23505 duplicate key
5860
SELECT f_insert(3,4);
5961

6062
query II rowsort
@@ -138,7 +140,7 @@ SELECT f_2values(7,8,9,10);
138140
(7,8)
139141
(9,10)
140142

141-
statement error pq: duplicate key value violates unique constraint "t_multi_pkey"
143+
statement error pgcode 23505 pq: duplicate key value violates unique constraint "t_multi_pkey"
142144
SELECT f_2values(42,42,42,42);
143145

144146
statement ok
@@ -155,7 +157,7 @@ SELECT f_2inserts(11,12,13,14);
155157
(11,12)
156158
(13,14)
157159

158-
statement error pq: duplicate key value violates unique constraint "t_multi_pkey"
160+
statement error pgcode 23505 pq: duplicate key value violates unique constraint "t_multi_pkey"
159161
SELECT f_2inserts(42,42,42,42);
160162

161163
# None of the duplicate key inserts should have succeeded.
@@ -260,7 +262,7 @@ $$
260262
INSERT INTO t_checkb VALUES (1, 0) ON CONFLICT(a) DO UPDATE SET b=0 RETURNING *;
261263
$$ LANGUAGE SQL;
262264

263-
statement error pq: failed to satisfy CHECK constraint \(b > 1:::INT8\)
265+
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(b > 1:::INT8\)
264266
SELECT f_checkb();
265267

266268
subtest end

pkg/sql/logictest/testdata/logic_test/udf_oid_ref

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ SELECT * FROM t1@idx WHERE [FUNCTION 1074](b, ',') = ARRAY['hello','new','world'
3232
statement ok
3333
ALTER TABLE t1 ADD CONSTRAINT c_len CHECK ([FUNCTION 814](b) > 2)
3434

35-
statement error pq: failed to satisfy CHECK constraint \(length\(b\) > 2:::INT8\)
35+
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(length\(b\) > 2:::INT8\)
3636
INSERT INTO t1 VALUES (3, 'a')
3737

3838
statement ok
@@ -59,7 +59,7 @@ hello world
5959

6060
# Make sure that argument types are still checked even we know which function
6161
# overload to use.
62-
statement error pq: unknown signature: public.f2\(int\)
62+
statement error pgcode 42883 pq: unknown signature: public.f2\(int\)
6363
SELECT [FUNCTION $fn_oid](123)
6464

6565
# Make sure that renaming does not break the reference.
@@ -86,7 +86,7 @@ hello world
8686
statement ok
8787
DROP FUNCTION sc1.f2_new(STRING);
8888

89-
statement error function undefined
89+
statement error pgcode 42883 function undefined
9090
SELECT [FUNCTION $fn_oid]('maybe')
9191

9292
# Referencing UDF OID within a UDF
@@ -98,7 +98,7 @@ let $fn_oid
9898
SELECT oid FROM pg_catalog.pg_proc WHERE proname = 'f_in_udf'
9999

100100
# TODO(chengxiong,mgartner): Fix this test when we enable support of calling UDFs from UDFs.
101-
statement error pq: function \d+ not found: function undefined
101+
statement error pgcode 42883 pq: function \d+ not found: function undefined
102102
CREATE FUNCTION f_using_udf() RETURNS INT LANGUAGE SQL AS $$ SELECT [FUNCTION $fn_oid]() $$;
103103

104104
# 814 is the OID of builtin function "length" with signature, and it's ok to

0 commit comments

Comments
 (0)