99 *
1010 *
1111 * IDENTIFICATION
12- * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.268 2008/01/01 19:45:52 momjian Exp $
12+ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.269 2008/01/06 01:03:16 tgl Exp $
1313 *
1414 *-------------------------------------------------------------------------
1515 */
@@ -175,7 +175,7 @@ static void get_coercion_expr(Node *arg, deparse_context *context,
175175Oid resulttype ,int32 resulttypmod ,
176176Node * parentNode );
177177static void get_const_expr (Const * constval ,deparse_context * context ,
178- bool showtype );
178+ int showtype );
179179static void get_sublink_expr (SubLink * sublink ,deparse_context * context );
180180static void get_from_clause (Query * query ,const char * prefix ,
181181deparse_context * context );
@@ -2258,15 +2258,20 @@ get_rule_sortgroupclause(SortClause *srt, List *tlist, bool force_colno,
22582258expr = (Node * )tle -> expr ;
22592259
22602260/*
2261- * Use column-number form if requested by caller or if expression is a
2262- * constant --- a constant is ambiguous (and will be misinterpreted by
2263- * findTargetlistEntry()) if we dump it explicitly.
2261+ * Use column-number form if requested by caller. Otherwise, if
2262+ * expression is a constant, force it to be dumped with an explicit
2263+ * cast as decoration --- this is because a simple integer constant
2264+ * is ambiguous (and will be misinterpreted by findTargetlistEntry())
2265+ * if we dump it without any decoration. Otherwise, just dump the
2266+ * expression normally.
22642267 */
2265- if (force_colno || ( expr && IsA ( expr , Const )) )
2268+ if (force_colno )
22662269{
22672270Assert (!tle -> resjunk );
22682271appendStringInfo (buf ,"%d" ,tle -> resno );
22692272}
2273+ else if (expr && IsA (expr ,Const ))
2274+ get_const_expr ((Const * )expr ,context ,1 );
22702275else
22712276get_rule_expr (expr ,context , true);
22722277
@@ -3409,7 +3414,7 @@ get_rule_expr(Node *node, deparse_context *context,
34093414break ;
34103415
34113416case T_Const :
3412- get_const_expr ((Const * )node ,context ,true );
3417+ get_const_expr ((Const * )node ,context ,0 );
34133418break ;
34143419
34153420case T_Param :
@@ -4388,7 +4393,7 @@ get_coercion_expr(Node *arg, deparse_context *context,
43884393((Const * )arg )-> consttypmod == -1 )
43894394{
43904395/* Show the constant without normal ::typename decoration */
4391- get_const_expr ((Const * )arg ,context ,false );
4396+ get_const_expr ((Const * )arg ,context ,-1 );
43924397}
43934398else
43944399{
@@ -4407,13 +4412,13 @@ get_coercion_expr(Node *arg, deparse_context *context,
44074412 *
44084413 *Make a string representation of a Const
44094414 *
4410- *Note: if showtypeis false, the Const is the direct argument of a coercion
4411- *operation with the same target type, and so we should suppress "::typename"
4412- *to avoid redundant output .
4415+ * showtypecan be -1 to never show "::typename" decoration, or +1 to always
4416+ *show it, or 0 to show it only if the constant wouldn't be assumed to be
4417+ *the right type by default .
44134418 * ----------
44144419 */
44154420static void
4416- get_const_expr (Const * constval ,deparse_context * context ,bool showtype )
4421+ get_const_expr (Const * constval ,deparse_context * context ,int showtype )
44174422{
44184423StringInfo buf = context -> buf ;
44194424Oid typoutput ;
@@ -4430,7 +4435,7 @@ get_const_expr(Const *constval, deparse_context *context, bool showtype)
44304435 * about type when reparsing.
44314436 */
44324437appendStringInfo (buf ,"NULL" );
4433- if (showtype )
4438+ if (showtype >= 0 )
44344439appendStringInfo (buf ,"::%s" ,
44354440format_type_with_typemod (constval -> consttype ,
44364441constval -> consttypmod ));
@@ -4506,13 +4511,15 @@ get_const_expr(Const *constval, deparse_context *context, bool showtype)
45064511
45074512pfree (extval );
45084513
4509- if (! showtype )
4514+ if (showtype < 0 )
45104515return ;
45114516
45124517/*
4513- * Append ::typename unless the constant will be implicitly typed as the
4514- * right type when it is read in. XXX this code has to be kept in sync
4515- * with the behavior of the parser, especially make_const.
4518+ * For showtype == 0, append ::typename unless the constant will be
4519+ * implicitly typed as the right type when it is read in.
4520+ *
4521+ * XXX this code has to be kept in sync with the behavior of the parser,
4522+ * especially make_const.
45164523 */
45174524switch (constval -> consttype )
45184525{
@@ -4534,7 +4541,7 @@ get_const_expr(Const *constval, deparse_context *context, bool showtype)
45344541needlabel = true;
45354542break ;
45364543}
4537- if (needlabel )
4544+ if (needlabel || showtype > 0 )
45384545appendStringInfo (buf ,"::%s" ,
45394546format_type_with_typemod (constval -> consttype ,
45404547constval -> consttypmod ));