Skip to content

Commit

Permalink
sql: fix record-returning udfs with no result
Browse files Browse the repository at this point in the history
Previously, record-returning UDFs whose last SQL statement returned no
result (i.e., not a SELECT or RETURNING statement) would succeed. They
now return a return type mismatch error, as expected.

Epic: CRDB-19255
Informs: #87289

Release note: None
  • Loading branch information
rharding6373 committed May 10, 2023
1 parent 4dbaf55 commit 3c16454
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/sql/logictest/testdata/logic_test/udf_insert
@@ -1,8 +1,8 @@
statement ok
CREATE TABLE t (a INT PRIMARY KEY, b INT DEFAULT 0);

statement ok
CREATE FUNCTION f_should_err() RETURNS RECORD AS
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.
CREATE FUNCTION f_err() RETURNS RECORD AS
$$
INSERT INTO t VALUES (1,2);
$$ LANGUAGE SQL;
Expand Down
9 changes: 5 additions & 4 deletions pkg/sql/opt/optbuilder/create_function.go
Expand Up @@ -239,10 +239,6 @@ func validateReturnType(expected *types.T, cols []scopeColumn) error {
if expected.Equivalent(types.Void) {
return nil
}
// If return type is RECORD, any column types are valid.
if types.IsRecordType(expected) {
return nil
}

if len(cols) == 0 {
return pgerror.WithCandidateCode(
Expand All @@ -254,6 +250,11 @@ func validateReturnType(expected *types.T, cols []scopeColumn) error {
)
}

// If return type is RECORD, any column types are valid.
if types.IsRecordType(expected) {
return nil
}

if len(cols) == 1 {
if !expected.Equivalent(cols[0].typ) &&
!cast.ValidCast(cols[0].typ, expected, cast.ContextAssignment) {
Expand Down

0 comments on commit 3c16454

Please sign in to comment.