From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@postgresql.org
Subject: pgsql: Disallow set-returning functions inside CASE or COALESCE.
Date: Wed, 14 Jun 2017 03:46:49 +0000
Message-ID: <E1dKzGb-0006YN-6w@gemulon.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-committers>
Disallow set-returning functions inside CASE or COALESCE.
When we reimplemented SRFs in commit 69f4b9c85, our initial choice was
to allow the behavior to vary from historical practice in cases where a
SRF call appeared within a conditional-execution construct (currently,
only CASE or COALESCE). But that was controversial to begin with, and
subsequent discussion has resulted in a consensus that it's better to
throw an error instead of executing the query differently from before,
so long as we can provide a reasonably clear error message and a way to
rewrite the query.
Hence, add a parser mechanism to allow detection of such cases during
parse analysis. The mechanism just requires storing, in the ParseState,
a pointer to the set-returning FuncExpr or OpExpr most recently emitted
by parse analysis. Then the parsing functions for CASE and COALESCE can
detect the presence of a SRF in their arguments by noting whether this
pointer changes while analyzing their arguments. Furthermore, if it does,
it provides a suitable error cursor location for the complaint. (This
means that if there's more than one SRF in the arguments, the error will
point at the last one to be analyzed not the first. While connoisseurs of
parsing behavior might find that odd, it's unlikely the average user would
ever notice.)
While at it, we can also provide more specific error messages than before
about some pre-existing restrictions, such as no-SRFs-within-aggregates.
Also, reject at parse time cases where a NULLIF or IS DISTINCT FROM
construct would need to return a set. We've never supported that, but the
restriction is depended on in more subtle ways now, so it seems wise to
detect it at the start.
Also, provide some documentation about how to rewrite a SRF-within-CASE
query using a custom wrapper SRF.
It turns out that the information_schema.user_mapping_options view
contained an instance of exactly the behavior we're now forbidding; but
rewriting it makes it more clear and safer too.
initdb forced because of user_mapping_options change.
Patch by me, with error message suggestions from Alvaro Herrera and
Andres Freund, pursuant to a complaint from Regina Obe.
Discussion: https://postgr.es/m/000001d2d5de$d8d66170$8a832450$@pcorp.us
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/0436f6bde8848b7135f19dd7f8548b8c2ae89a34
Modified Files
--------------
doc/src/sgml/xfunc.sgml | 83 +++++++++++++++++++++---------
src/backend/catalog/information_schema.sql | 8 +--
src/backend/executor/functions.c | 1 +
src/backend/parser/parse_agg.c | 8 +++
src/backend/parser/parse_clause.c | 38 +++++++++++---
src/backend/parser/parse_expr.c | 79 +++++++++++++++++++++-------
src/backend/parser/parse_func.c | 41 +++++++++++++--
src/backend/parser/parse_oper.c | 14 +++--
src/include/catalog/catversion.h | 2 +-
src/include/parser/parse_func.h | 5 +-
src/include/parser/parse_node.h | 5 ++
src/include/parser/parse_oper.h | 2 +-
src/test/regress/expected/create_table.out | 2 +-
src/test/regress/expected/rangefuncs.out | 12 -----
src/test/regress/expected/tsrf.out | 24 +++++++--
src/test/regress/sql/rangefuncs.sql | 9 ----
src/test/regress/sql/tsrf.sql | 7 +++
17 files changed, 249 insertions(+), 91 deletions(-)
--
Sent via pgsql-committers mailing list (pgsql-committers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-committers
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-committers@postgresql.org
Cc: tgl@sss.pgh.pa.us
Subject: Re: pgsql: Disallow set-returning functions inside CASE or COALESCE.
In-Reply-To: <E1dKzGb-0006YN-6w@gemulon.postgresql.org>
References: <E1dKzGb-0006YN-6w@gemulon.postgresql.org>
* 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