Skip to content

Commit cab527a

Browse files
author
Commitfest Bot
committed
[CF 6017] Add error_on_null() to produce an error if the input is null
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/6017 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/[email protected] Author(s): Joel Jacobson
2 parents 5487058 + 20512bd commit cab527a

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

doc/src/sgml/func/func-comparison.sgml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,26 @@ SELECT NOT(ROW(table.*) IS NOT NULL) FROM TABLE; -- detect at least one null in
631631
<returnvalue>1</returnvalue>
632632
</para></entry>
633633
</row>
634+
<row>
635+
<entry role="func_table_entry"><para role="func_signature">
636+
<indexterm>
637+
<primary>error_on_null</primary>
638+
</indexterm>
639+
<function>error_on_null</function> ( <type>anyelement</type> )
640+
<returnvalue>anyelement | error</returnvalue>
641+
</para>
642+
<para>
643+
Produces an error if the input is null, and returns the input otherwise.
644+
</para>
645+
<para>
646+
<literal>error_on_null(42)</literal>
647+
<returnvalue>42</returnvalue>
648+
</para>
649+
<para>
650+
<literal>error_on_null(null)</literal>
651+
<returnvalue>does not return</returnvalue>
652+
</para></entry>
653+
</row>
634654
</tbody>
635655
</tgroup>
636656
</table>

src/backend/utils/adt/misc.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,21 @@ pg_num_nonnulls(PG_FUNCTION_ARGS)
185185
PG_RETURN_INT32(nargs - nulls);
186186
}
187187

188+
/*
189+
* error_on_null()
190+
* Produces an error if the input is null, and returns the input otherwise
191+
*/
192+
Datum
193+
error_on_null(PG_FUNCTION_ARGS)
194+
{
195+
if (PG_ARGISNULL(0))
196+
ereport(ERROR,
197+
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
198+
errmsg("null value not allowed")));
199+
200+
PG_RETURN_DATUM(PG_GETARG_DATUM(0));
201+
}
202+
188203

189204
/*
190205
* current_database()

src/include/catalog/pg_proc.dat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12518,6 +12518,9 @@
1251812518
{ oid => '6292', descr => 'aggregate transition function',
1251912519
proname => 'any_value_transfn', prorettype => 'anyelement',
1252012520
proargtypes => 'anyelement anyelement', prosrc => 'any_value_transfn' },
12521+
{ oid => '8488', descr => 'error on null input; otherwise returns input unchanged',
12522+
proname => 'error_on_null', proisstrict => 'f', prorettype => 'anyelement',
12523+
proargtypes => 'anyelement', prosrc => 'error_on_null' },
1252112524

1252212525
{ oid => '6321', descr => 'list of available WAL summary files',
1252312526
proname => 'pg_available_wal_summaries', prorows => '100', proretset => 't',

src/test/regress/expected/misc_functions.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,23 @@ ERROR: function num_nulls() does not exist
177177
LINE 1: SELECT num_nulls();
178178
^
179179
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
180+
--
181+
-- error_on_null()
182+
--
183+
SELECT error_on_null(1);
184+
error_on_null
185+
---------------
186+
1
187+
(1 row)
188+
189+
SELECT error_on_null(NULL::int);
190+
ERROR: null value not allowed
191+
SELECT error_on_null(ROW(1,NULL::int));
192+
error_on_null
193+
---------------
194+
(1,)
195+
(1 row)
196+
180197
--
181198
-- canonicalize_path()
182199
--

src/test/regress/sql/misc_functions.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ SELECT num_nulls(VARIADIC '{}'::int[]);
7777
SELECT num_nonnulls();
7878
SELECT num_nulls();
7979

80+
--
81+
-- error_on_null()
82+
--
83+
84+
SELECT error_on_null(1);
85+
SELECT error_on_null(NULL::int);
86+
SELECT error_on_null(ROW(1,NULL::int));
87+
8088
--
8189
-- canonicalize_path()
8290
--

0 commit comments

Comments
 (0)