From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@postgresql.org
Subject: pgsql: Make operator precedence follow the SQL standard more closely.
Date: Wed, 11 Mar 2015 17:22:58 +0000
Message-ID: <E1YVkLS-0005vm-KJ@gemulon.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-committers>
Make operator precedence follow the SQL standard more closely.
While the SQL standard is pretty vague on the overall topic of operator
precedence (because it never presents a unified BNF for all expressions),
it does seem reasonable to conclude from the spec for <boolean value
expression> that OR has the lowest precedence, then AND, then NOT, then IS
tests, then the six standard comparison operators, then everything else
(since any non-boolean operator in a WHERE clause would need to be an
argument of one of these).
We were only sort of on board with that: most notably, while "<" ">" and
"=" had properly low precedence, "<=" ">=" and "<>" were treated as generic
operators and so had significantly higher precedence. And "IS" tests were
even higher precedence than those, which is very clearly wrong per spec.
Another problem was that "foo NOT SOMETHING bar" constructs, such as
"x NOT LIKE y", were treated inconsistently because of a bison
implementation artifact: they had the documented precedence with respect
to operators to their right, but behaved like NOT (i.e., very low priority)
with respect to operators to their left.
Fixing the precedence issues is just a small matter of rearranging the
precedence declarations in gram.y, except for the NOT problem, which
requires adding an additional lookahead case in base_yylex() so that we
can attach a different token precedence to NOT LIKE and allied two-word
operators.
The bulk of this patch is not the bug fix per se, but adding logic to
parse_expr.c to allow giving warnings if an expression has changed meaning
because of these precedence changes. These warnings are off by default
and are enabled by the new GUC operator_precedence_warning. It's believed
that very few applications will be affected by these changes, but it was
agreed that a warning mechanism is essential to help debug any that are.
Branch
------
master
Details
-------
http://git.postgresql.org/pg/commitdiff/c6b3c939b7e0f1d35f4ed4996e71420a993810d2
Modified Files
--------------
doc/src/sgml/config.sgml | 23 ++
doc/src/sgml/syntax.sgml | 84 +++--
src/backend/nodes/outfuncs.c | 3 +
src/backend/parser/gram.y | 97 +++--
src/backend/parser/parse_expr.c | 476 ++++++++++++++++++++++++-
src/backend/parser/parse_target.c | 7 +-
src/backend/parser/parser.c | 17 +
src/backend/parser/scan.l | 33 +-
src/backend/utils/misc/guc.c | 10 +
src/backend/utils/misc/postgresql.conf.sample | 1 +
src/bin/psql/psqlscan.l | 21 ++
src/include/nodes/parsenodes.h | 3 +-
src/include/parser/parse_expr.h | 1 +
src/include/parser/scanner.h | 1 +
src/interfaces/ecpg/preproc/parse.pl | 7 +-
src/interfaces/ecpg/preproc/parser.c | 17 +
src/interfaces/ecpg/preproc/pgc.l | 16 +-
src/pl/plpgsql/src/pl_gram.y | 1 +
18 files changed, 723 insertions(+), 95 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: Make operator precedence follow the SQL standard more closely.
In-Reply-To: <E1YVkLS-0005vm-KJ@gemulon.postgresql.org>
References: <E1YVkLS-0005vm-KJ@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