From: | "J(dot) Greg Davidson" <jgd(at)well(dot)com> |
---|---|
To: | PostgreSQL General <pgsql-general(at)postgresql(dot)org> |
Subject: | surprised by non-strict array_append |
Date: | 2010-02-03 07:08:28 |
Message-ID: | 2103698624.5709.1265180908227.JavaMail.root@zimbra.well.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
I was caught out today by the non-strict behavior of array_append
causing me to get an undesired result for a COALESCE. My subsequent
attempt to create a STRICT VARIADIC generalization of array_append
led to another surprise. The problem was easily solved, but might
be of interest to others. Perhaps someone will enlighten me as to
why the standard array functions are non-strict and why the STRICT
VARIADIC function fails to be strict. I've boiled it down to make
it clear:
psql (8.4.2)
SELECT COALESCE( ARRAY[1] || NULL::integer, ARRAY[42] );
coalesce
----------
{1,NULL}
(1 row)
SELECT COALESCE( array_append( ARRAY[1], NULL), ARRAY[42] );
coalesce
----------
{1,NULL}
(1 row)
CREATE OR REPLACE
FUNCTION array_add(ANYARRAY, VARIADIC ANYARRAY) RETURNS ANYARRAY AS $$
SELECT $1 || $2
$$ LANGUAGE sql STRICT;
COMMENT ON FUNCTION array_add(ANYARRAY, ANYARRAY)
IS 'a strict generalization version of array_append';
SELECT COALESCE( array_add( ARRAY[1], NULL, NULL), ARRAY[42] );
coalesce
---------------
{1,NULL,NULL}
(1 row)
-- hmm - not very strict!
CREATE OR REPLACE
FUNCTION array_add1(ANYARRAY, ANYELEMENT) RETURNS ANYARRAY AS $$
SELECT $1 || $2
$$ LANGUAGE sql STRICT;
COMMENT ON FUNCTION array_add1(ANYARRAY, ANYELEMENT)
IS 'a strict version of array_append';
SELECT COALESCE( array_add1( ARRAY[1], NULL), ARRAY[42] );
coalesce
----------
{42}
(1 row)
-- ah, finally!
_Greg
From | Date | Subject | |
---|---|---|---|
Next Message | Shoaib Mir | 2010-02-03 07:44:14 | Re: add column specify position |
Previous Message | AI Rumman | 2010-02-03 06:47:33 | stat collector is not using |