Skip to content

Commit 9cadb7c

Browse files
craig[bot]DrewKimball
andcommitted
108216: plpgsql: handle NULL values correctly for RAISE statements r=DrewKimball a=DrewKimball This patch fixes two behaviors related to RAISE statements handling NULL values: 1. When a NULL value is passed as a formatting argument, it is printed as `<NULL>` in the result string. 2. When a NULL value is passed as a RAISE option (message, detail, etc.) a runtime `RAISE statement option cannot be null` error results. Informs cockroachdb#105254 Release note: None Co-authored-by: Drew Kimball <[email protected]>
2 parents 3a393c1 + d1e527e commit 9cadb7c

File tree

4 files changed

+113
-32
lines changed

4 files changed

+113
-32
lines changed

pkg/sql/logictest/testdata/logic_test/udf_plpgsql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,42 @@ CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
700700
END
701701
$$ LANGUAGE PLpgSQL;
702702

703+
# NULL formatting arguments are printed as "<NULL>".
704+
statement ok
705+
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
706+
BEGIN
707+
RAISE 'foo % bar %', NULL::TEXT, NULL::INT;
708+
return 0;
709+
END
710+
$$ LANGUAGE PLpgSQL;
711+
712+
statement error pgcode P0001 pq: foo <NULL> bar <NULL>
713+
SELECT f();
714+
715+
# NULL values cannot be supplied as RAISE options.
716+
statement ok
717+
CREATE OR REPLACE FUNCTION f(n INT) RETURNS INT AS $$
718+
BEGIN
719+
IF n = 0 THEN
720+
RAISE division_by_zero USING message = NULL::TEXT;
721+
END IF;
722+
IF n = 1 THEN
723+
RAISE division_by_zero USING detail = NULL::TEXT;
724+
END IF;
725+
RAISE division_by_zero USING hint = (SELECT 'foo' FROM xy WHERE False);
726+
return 0;
727+
END
728+
$$ LANGUAGE PLpgSQL;
729+
730+
statement error pgcode 22004 pq: RAISE statement option cannot be null
731+
SELECT f(0);
732+
733+
statement error pgcode 22004 pq: RAISE statement option cannot be null
734+
SELECT f(1);
735+
736+
statement error pgcode 22004 pq: RAISE statement option cannot be null
737+
SELECT f(2);
738+
703739
statement error pgcode 42601 pq: \"i\" is not a known variable
704740
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
705741
BEGIN

pkg/sql/opt/optbuilder/plpgsql.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,10 @@ func (b *plpgsqlBuilder) makeRaiseFormatMessage(
554554
if argIdx >= len(args) {
555555
panic(pgerror.Newf(pgcode.Syntax, "too few parameters specified for RAISE"))
556556
}
557-
addToResult(b.buildPLpgSQLExpr(args[argIdx], types.String, s))
557+
// If the argument is NULL, postgres prints "<NULL>".
558+
arg := b.buildPLpgSQLExpr(args[argIdx], types.String, s)
559+
arg = b.ob.factory.ConstructCoalesce(memo.ScalarListExpr{arg, makeConstStr("<NULL>")})
560+
addToResult(arg)
558561
argIdx++
559562
}
560563
addToResult(makeConstStr(paramSubstr))

0 commit comments

Comments
 (0)