aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/ruleutils.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2004-07-06 04:50:21 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2004-07-06 04:50:21 +0000
commit07b4c48b6a6f13ade6cc8f897a67616b47033ad0 (patch)
tree730e6556edacc30cd574862c1dda828c07905be4 /src/backend/utils/adt/ruleutils.c
parenta21bb2722aa00f6fac630709e845df37b9ff05b6 (diff)
downloadpostgresql-07b4c48b6a6f13ade6cc8f897a67616b47033ad0.tar.gz
postgresql-07b4c48b6a6f13ade6cc8f897a67616b47033ad0.zip
Fix broken logic for pretty-printing parenthesis-suppression in UNION
et al.
Diffstat (limited to 'src/backend/utils/adt/ruleutils.c')
-rw-r--r--src/backend/utils/adt/ruleutils.c56
1 files changed, 28 insertions, 28 deletions
diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 208cbf5d31b..17ff48400e7 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -3,7 +3,7 @@
* back to source text
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.174 2004/06/25 17:20:24 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.175 2004/07/06 04:50:21 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
@@ -2066,6 +2066,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
TupleDesc resultDesc)
{
StringInfo buf = context->buf;
+ bool need_paren;
if (IsA(setOp, RangeTblRef))
{
@@ -2074,24 +2075,37 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
Query *subquery = rte->subquery;
Assert(subquery != NULL);
+ Assert(subquery->setOperations == NULL);
+ /* Need parens if ORDER BY, FOR UPDATE, or LIMIT; see gram.y */
+ need_paren = (subquery->sortClause ||
+ subquery->rowMarks ||
+ subquery->limitOffset ||
+ subquery->limitCount);
+ if (need_paren)
+ appendStringInfoChar(buf, '(');
get_query_def(subquery, buf, context->namespaces, resultDesc,
context->prettyFlags, context->indentLevel);
+ if (need_paren)
+ appendStringInfoChar(buf, ')');
}
else if (IsA(setOp, SetOperationStmt))
{
SetOperationStmt *op = (SetOperationStmt *) setOp;
- bool need_paren;
-
- need_paren = (PRETTY_PAREN(context) ?
- !IsA(op->rarg, RangeTblRef) : true);
- if (!PRETTY_PAREN(context))
- appendStringInfoString(buf, "((");
+ /*
+ * We force parens whenever nesting two SetOperationStmts.
+ * There are some cases in which parens are needed around a leaf
+ * query too, but those are more easily handled at the next level
+ * down (see code above).
+ */
+ need_paren = !IsA(op->larg, RangeTblRef);
+ if (need_paren)
+ appendStringInfoChar(buf, '(');
get_setop_query(op->larg, query, context, resultDesc);
-
- if (!PRETTY_PAREN(context))
+ if (need_paren)
appendStringInfoChar(buf, ')');
+
if (!PRETTY_INDENT(context))
appendStringInfoChar(buf, ' ');
switch (op->op)
@@ -2118,27 +2132,13 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
if (PRETTY_INDENT(context))
appendStringInfoChar(buf, '\n');
- if (PRETTY_PAREN(context))
- {
- if (need_paren)
- {
- appendStringInfoChar(buf, '(');
- if (PRETTY_INDENT(context))
- appendStringInfoChar(buf, '\n');
- }
- }
- else
- appendStringInfoChar(buf, '(');
+ need_paren = !IsA(op->rarg, RangeTblRef);
+ if (need_paren)
+ appendStringInfoChar(buf, '(');
get_setop_query(op->rarg, query, context, resultDesc);
-
- if (PRETTY_PAREN(context))
- {
- if (need_paren)
- appendStringInfoChar(buf, ')');
- }
- else
- appendStringInfoString(buf, "))");
+ if (need_paren)
+ appendStringInfoChar(buf, ')');
}
else
{