Skip to content

Commit ef0bc41

Browse files
committed
sql: fix some error codes and messages for udfs
This commit fixes some incorrect error codes and error messages in UDF logic tests that were uncovered as part of auditing pg error codes against postgres. In some cases where there were discrepancies between postgres and CRDB that were due to implementation differences or improved behavior, we left a comment above the test detailing why there is a difference between postgres and CRDB behavior. Epic: None Informs: cockroachdb#107369 Release note: None
1 parent d6f6de2 commit ef0bc41

File tree

11 files changed

+20
-21
lines changed

11 files changed

+20
-21
lines changed

pkg/sql/logictest/testdata/logic_test/udf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,7 @@ CREATE FUNCTION sc1.f_test_drop(IN INT8)
406406
SELECT 1;
407407
$$
408408

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

413412
statement ok

pkg/sql/logictest/testdata/logic_test/udf_insert

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ SELECT f_void();
1818
----
1919
NULL
2020

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
21+
# Note: This does not error in postgres until the function is executed.
22+
statement error pgcode 23502 missing "a" primary key column
2423
CREATE FUNCTION f_err(b INT) RETURNS RECORD AS
2524
$$
2625
INSERT INTO t (b) VALUES (b);

pkg/sql/logictest/testdata/logic_test/udf_options

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ l1 l2 i1 i2 s1 s2 v1 v2
109109
statement ok
110110
CREATE SEQUENCE sq2;
111111

112-
# TODO(107369): This does not error in postgres.
112+
# Note: postgres allows non-volatile functions to call other volatile functions.
113113
statement error pgcode 22023 volatile statement not allowed in immutable function: SELECT nextval\('sq2'\)
114114
CREATE FUNCTION rand_i() RETURNS INT IMMUTABLE LANGUAGE SQL AS $$SELECT nextval('sq2')$$;
115115

116-
# TODO(107369): This does not error in postgres.
116+
# Note: postgres allows non-volatile functions to call other volatile functions.
117117
statement error pgcode 22023 volatile statement not allowed in stable function: SELECT nextval\('sq2'\)
118-
CREATE FUNCTION rand_s() RETURNS INT STABLE LANGUAGE SQL AS $$SELECT nextval('sq2')$$;
118+
CREATE FUNCTION rand_s() RETURNS INT STABLE LANGUAGE SQL AS $$SELECT nextval('sq2')$$;
119119

120120
statement ok
121121
CREATE FUNCTION rand_v() RETURNS INT VOLATILE LANGUAGE SQL AS $$SELECT nextval('sq2')$$;

pkg/sql/logictest/testdata/logic_test/udf_privileges

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ CREATE FUNCTION f_test_show_grants(INT, string, OID) RETURNS INT LANGUAGE SQL AS
533533
CREATE USER u_test_show_grants;
534534
GRANT EXECUTE ON FUNCTION f_test_show_grants(INT), f_test_show_grants(INT, string, OID) TO u_test_show_grants;
535535

536-
statement error pgcode XXUUU pq: function name "f_test_show_grants" is not unique
536+
statement error pgcode 42725 pq: function name "f_test_show_grants" is not unique
537537
SHOW GRANTS ON FUNCTION f_test_show_grants;
538538

539539
query TTTTTTB colnames

pkg/sql/logictest/testdata/logic_test/udf_record

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,20 +535,21 @@ SELECT imp_cast(), (1,2,'3') = imp_cast(), pg_typeof(imp_cast())
535535
----
536536
(1,2,3) true imp
537537

538-
# TODO(107369): This is code 42846 in postgres.
538+
# Note: postgres returns error code 42846 (cannot cast type) here instead due to
539+
# implementation differences.
539540
statement error pgcode 42P13 return type mismatch in function declared to return imp
540541
CREATE FUNCTION err() RETURNS imp LANGUAGE SQL AS $$
541542
SELECT (1, 2)
542543
$$
543544

544-
# TODO(mgartner): The error message should say "imp" instead of "record".
545-
statement error pgcode 42P13 return type mismatch in function declared to return record
545+
statement error pgcode 42P13 return type mismatch in function declared to return imp
546546
CREATE FUNCTION err() RETURNS imp LANGUAGE SQL AS $$
547547
SELECT k, a FROM imp
548548
$$
549549

550-
# TODO(mgartner): The error message should say "imp" instead of "record".
551-
statement error pgcode 42P13 return type mismatch in function declared to return record
550+
# Note: This function can be successfully created in postgres, but will fail
551+
# when called.
552+
statement error pgcode 42P13 return type mismatch in function declared to return imp
552553
CREATE FUNCTION err() RETURNS imp LANGUAGE SQL AS $$
553554
SELECT k, a, b::INT FROM imp
554555
$$

pkg/sql/logictest/testdata/logic_test/udf_schema_change

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ ALTER TABLE t_alter ALTER b TYPE TEXT
633633
statement ok
634634
ALTER TABLE t_alter ADD COLUMN d INT;
635635

636-
statement error pgcode 42P13 pq: return type mismatch in function declared to return record
636+
statement error pgcode 42P13 pq: return type mismatch in function declared to return t_alter
637637
CREATE OR REPLACE FUNCTION f_rtbl() RETURNS t_alter LANGUAGE SQL AS $$
638638
SELECT 1, 'foobar', 2
639639
$$

pkg/sql/logictest/testdata/logic_test/udf_update

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ CREATE TABLE generated_as_id_t (
167167
statement ok
168168
INSERT INTO generated_as_id_t (a) VALUES (7), (8), (9);
169169

170-
# TODO(107369): This error code does not have a name associated with it.
171170
statement error pgcode 428C9 column "b" can only be updated to DEFAULT
172171
CREATE FUNCTION f_err(i INT, j INT) RETURNS RECORD AS
173172
$$

pkg/sql/logictest/testdata/logic_test/udf_volatility_check

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ statement ok
55
CREATE TABLE t1(a INT PRIMARY KEY, b INT);
66
CREATE TABLE t2(a INT PRIMARY KEY, b INT);
77

8-
# TODO(107369): These do not appear to error in postgres.
8+
# Note: postgres does not error in the following cases. CRDB provides stronger
9+
# protections against adding volatility to non-volatile functions.
910
statement error pgcode 22023 pq: referencing relations is not allowed in immutable function
1011
CREATE FUNCTION f() RETURNS FLOAT LANGUAGE SQL IMMUTABLE AS $$ SELECT a FROM t1 $$;
1112

pkg/sql/opt/optbuilder/create_function.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func validateReturnType(expected *types.T, cols []scopeColumn) error {
358358
if !typ.Equivalent(cols[i].typ) {
359359
return pgerror.WithCandidateCode(
360360
errors.WithDetailf(
361-
errors.Newf("return type mismatch in function declared to return record"),
361+
errors.Newf("return type mismatch in function declared to return %s", expected.Name()),
362362
"Final statement returns %s instead of %s at column %d",
363363
cols[i].typ.Name(), typ.Name(), i+1,
364364
),
@@ -372,7 +372,7 @@ func validateReturnType(expected *types.T, cols []scopeColumn) error {
372372
// Ran out of columns from last statement.
373373
return pgerror.WithCandidateCode(
374374
errors.WithDetailf(
375-
errors.New("return type mismatch in function declared to return record"),
375+
errors.Newf("return type mismatch in function declared to return %s", expected.Name()),
376376
"Final statement returns too few columns",
377377
),
378378
pgcode.InvalidFunctionDefinition,

pkg/sql/opt/optbuilder/insert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ func (mb *mutationBuilder) checkPrimaryKeyForInsert() {
454454
continue
455455
}
456456

457-
panic(pgerror.Newf(pgcode.InvalidForeignKey,
457+
panic(pgerror.Newf(pgcode.NotNullViolation,
458458
"missing %q primary key column", col.ColName()))
459459
}
460460
}

0 commit comments

Comments
 (0)