diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index c1bbb4f6d6f..0790ea76ca6 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.5 2006/05/21 19:57:07 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.157.2.6 2008/01/06 01:03:46 tgl Exp $ * * This software is copyrighted by Jan Wieck - Hamburg. * @@ -194,7 +194,8 @@ static void get_func_expr(FuncExpr *expr, deparse_context *context, bool showimplicit); static void get_agg_expr(Aggref *aggref, deparse_context *context); static Node *strip_type_coercion(Node *expr, Oid resultType); -static void get_const_expr(Const *constval, deparse_context *context); +static void get_const_expr(Const *constval, deparse_context *context, + int showtype); static void get_sublink_expr(SubLink *sublink, deparse_context *context); static void get_from_clause(Query *query, deparse_context *context); static void get_from_clause_item(Node *jtnode, Query *query, @@ -2088,15 +2089,20 @@ get_rule_sortgroupclause(SortClause *srt, List *tlist, bool force_colno, expr = (Node *) tle->expr; /* - * Use column-number form if requested by caller or if expression is a - * constant --- a constant is ambiguous (and will be misinterpreted by - * findTargetlistEntry()) if we dump it explicitly. + * Use column-number form if requested by caller. Otherwise, if + * expression is a constant, force it to be dumped with an explicit + * cast as decoration --- this is because a simple integer constant + * is ambiguous (and will be misinterpreted by findTargetlistEntry()) + * if we dump it without any decoration. Otherwise, just dump the + * expression normally. */ - if (force_colno || (expr && IsA(expr, Const))) + if (force_colno) { Assert(!tle->resdom->resjunk); appendStringInfo(buf, "%d", tle->resdom->resno); } + else if (expr && IsA(expr, Const)) + get_const_expr((Const *) expr, context, 1); else get_rule_expr(expr, context, true); @@ -2781,7 +2787,7 @@ get_rule_expr(Node *node, deparse_context *context, break; case T_Const: - get_const_expr((Const *) node, context); + get_const_expr((Const *) node, context, 0); break; case T_Param: @@ -3449,10 +3455,14 @@ strip_type_coercion(Node *expr, Oid resultType) * get_const_expr * * Make a string representation of a Const + * + * showtype can be -1 to never show "::typename" decoration, or +1 to always + * show it, or 0 to show it only if the constant wouldn't be assumed to be + * the right type by default. * ---------- */ static void -get_const_expr(Const *constval, deparse_context *context) +get_const_expr(Const *constval, deparse_context *context, int showtype) { StringInfo buf = context->buf; HeapTuple typetup; @@ -3468,8 +3478,11 @@ get_const_expr(Const *constval, deparse_context *context) * Always label the type of a NULL constant to prevent * misdecisions about type when reparsing. */ - appendStringInfo(buf, "NULL::%s", - format_type_with_typemod(constval->consttype, -1)); + appendStringInfo(buf, "NULL"); + if (showtype >= 0) + appendStringInfo(buf, "::%s", + format_type_with_typemod(constval->consttype, + -1)); return; } @@ -3556,10 +3569,15 @@ get_const_expr(Const *constval, deparse_context *context) pfree(extval); + if (showtype < 0) + return; + /* - * Append ::typename unless the constant will be implicitly typed as - * the right type when it is read in. XXX this code has to be kept in - * sync with the behavior of the parser, especially make_const. + * For showtype == 0, append ::typename unless the constant will be + * implicitly typed as the right type when it is read in. + * + * XXX this code has to be kept in sync with the behavior of the parser, + * especially make_const. */ switch (constval->consttype) { @@ -3577,7 +3595,7 @@ get_const_expr(Const *constval, deparse_context *context) needlabel = true; break; } - if (needlabel) + if (needlabel || showtype > 0) appendStringInfo(buf, "::%s", format_type_with_typemod(constval->consttype, -1));