From e2004dfc69c602226288a99a4e769d6494a9afe7 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Mon, 30 Apr 2001 19:24:47 +0000
Subject: [PATCH] Suppress pull-up of subqueries that are in the nullable side
 of an outer join.  This is needed to avoid improper evaluation of expressions
 that should be nulled out, as in Victor Wagner's bug report of 4/27/01.
 Pretty ugly solution, but no time to do anything better for 7.1.1.

---
 src/backend/optimizer/plan/planner.c | 39 +++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)

diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 3026fdf0581..b2ab4600209 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.104 2001/04/18 20:42:55 tgl Exp $
+ *	  $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.105 2001/04/30 19:24:47 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -363,8 +363,41 @@ pull_up_subqueries(Query *parse, Node *jtnode)
 	{
 		JoinExpr   *j = (JoinExpr *) jtnode;
 
-		j->larg = pull_up_subqueries(parse, j->larg);
-		j->rarg = pull_up_subqueries(parse, j->rarg);
+		/*
+		 * At the moment, we can't pull up subqueries that are inside the
+		 * nullable side of an outer join, because substituting their target
+		 * list entries for upper Var references wouldn't do the right thing
+		 * (the entries wouldn't go to NULL when they're supposed to).
+		 * Suppressing the pullup is an ugly, performance-losing hack, but
+		 * I see no alternative for now.  Find a better way to handle this
+		 * when we redesign query trees --- tgl 4/30/01.
+		 */
+		switch (j->jointype)
+		{
+			case JOIN_INNER:
+				j->larg = pull_up_subqueries(parse, j->larg);
+				j->rarg = pull_up_subqueries(parse, j->rarg);
+				break;
+			case JOIN_LEFT:
+				j->larg = pull_up_subqueries(parse, j->larg);
+				break;
+			case JOIN_FULL:
+				break;
+			case JOIN_RIGHT:
+				j->rarg = pull_up_subqueries(parse, j->rarg);
+				break;
+			case JOIN_UNION:
+				/*
+				 * This is where we fail if upper levels of planner
+				 * haven't rewritten UNION JOIN as an Append ...
+				 */
+				elog(ERROR, "UNION JOIN is not implemented yet");
+				break;
+			default:
+				elog(ERROR, "pull_up_subqueries: unexpected join type %d",
+					 j->jointype);
+				break;
+		}
 	}
 	else
 		elog(ERROR, "pull_up_subqueries: unexpected node type %d",
-- 
GitLab