From: Tom Lane <tgl@sss.pgh.pa.us>
To: david.g.johnston@gmail.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #8228: Unexpected "set-valued function" with varchar(n) but not varchar
Date: Thu, 13 Jun 2013 16:02:22 -0400
Message-ID: <16577.1371153742@sss.pgh.pa.us> (raw)
In-Reply-To: <E1UnCv4-0007oF-Bo@wrigleys.postgresql.org>
References: <E1UnCv4-0007oF-Bo@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
david.g.johnston@gmail.com writes:
> The following query results in "SQL Error: ERROR: set-valued function called
> in context that cannot accept a set"
> SELECT *, CASE WHEN id = 2 THEN
> (regexp_matches(input_string,'^0*([1-9]\d+)$'))[1] ELSE input_string
> END::varchar(30) AS o_l2_a
> FROM (
> VALUES (1,''), (2,'0000000049404'),(3,'FROM 10000000876')
> ) l0_src (id, input_string)
Hm, interesting example. What seems to be happening is that during
evaluation of the SELECT list for the first VALUES row, the CASE
expression doesn't call regexp_matches() but just returns the ELSE
expression. The ExecMakeFunctionResult() call for the cast function
then decides that the function's argument expression doesn't return a
set, so it changes the node execution pointer so that subsequent
executions go through the much simpler ExecMakeFunctionResultNoSets()
execution function. And then that spits up when on the next row, the
argument expression *does* return a set :-(
You could work around that using the trick documented in the
regexp_matches documentation to force it to return exactly one row,
ie interpose a sub-SELECT:
regression=# SELECT *, CASE WHEN id = 2 THEN
(select (regexp_matches(input_string,'^0*([1-9]\d+)$'))[1]) ELSE input_string
END::varchar(30) AS o_l2_a
FROM (
VALUES (1,''), (2,'0000000049404'),(3,'FROM 10000000876')
) l0_src (id, input_string);
id | input_string | o_l2_a
----+------------------+------------------
1 | |
2 | 0000000049404 | 49404
3 | FROM 10000000876 | FROM 10000000876
(3 rows)
Not sure about non-hack fixes. I guess we need to analyze
can-it-return-a-set statically instead of believing the first execution
result, but that might add an unpleasant amount of startup overhead.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, david.g.johnston@gmail.com
Subject: Re: BUG #8228: Unexpected "set-valued function" with varchar(n) but not varchar
In-Reply-To: <16577.1371153742@sss.pgh.pa.us>
References: <E1UnCv4-0007oF-Bo@wrigleys.postgresql.org> <16577.1371153742@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox