aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/ruleutils.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2008-06-06 18:00:09 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2008-06-06 18:00:09 +0000
commitc760d70af47d648833c03d764255c31b8d554ee3 (patch)
treeca256aeea6f9ce1e8c63e417a818609c8d6feb27 /src/backend/utils/adt/ruleutils.c
parentbef81252ca29ed71299d5fe5c3ade06c65e62802 (diff)
downloadpostgresql-c760d70af47d648833c03d764255c31b8d554ee3.tar.gz
postgresql-c760d70af47d648833c03d764255c31b8d554ee3.zip
Fix pg_get_ruledef() so that negative numeric constants are parenthesized.
This is needed because :: casting binds more tightly than minus, so for example -1::integer is not the same as (-1)::integer, and there are cases where the difference is important. In particular this caused a failure in SELECT DISTINCT ... ORDER BY ... where expressions that should have matched were seen as different by the parser; but I suspect that there could be other cases where failure to parenthesize leads to subtler semantic differences in reloaded rules. Per report from Alexandr Popov.
Diffstat (limited to 'src/backend/utils/adt/ruleutils.c')
-rw-r--r--src/backend/utils/adt/ruleutils.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 0790ea76ca6..b5f9fcf5b0a 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -3,7 +3,7 @@
* back to source text
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.157.2.6 2008/01/06 01:03:46 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.157.2.7 2008/06/06 18:00:09 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
@@ -3518,10 +3518,19 @@ get_const_expr(Const *constval, deparse_context *context, int showtype)
* In reality we only need to defend against infinity and
* NaN, so we need not get too crazy about pattern
* matching here.
+ *
+ * There is a special-case gotcha: if the constant is signed,
+ * we need to parenthesize it, else the parser might see a
+ * leading plus/minus as binding less tightly than adjacent
+ * operators --- particularly, the cast that we might attach
+ * below.
*/
if (strspn(extval, "0123456789+-eE.") == strlen(extval))
{
- appendStringInfoString(buf, extval);
+ if (extval[0] == '+' || extval[0] == '-')
+ appendStringInfo(buf, "(%s)", extval);
+ else
+ appendStringInfoString(buf, extval);
if (strcspn(extval, "eE.") != strlen(extval))
isfloat = true; /* it looks like a float */
}