From 1c83cbe7589031440bcf38dff8e81199a507566a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=80=E6=8C=83?= <yizhi.fzh@alibaba-inc.com>
Date: Tue, 16 Feb 2021 21:56:08 +0800
Subject: [PATCH v2 2/2] Reset notnullattrs for outer join

---
 src/backend/optimizer/util/relnode.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c
index 731ff708b9..c06d60a051 100644
--- a/src/backend/optimizer/util/relnode.c
+++ b/src/backend/optimizer/util/relnode.c
@@ -555,6 +555,19 @@ add_join_rel(PlannerInfo *root, RelOptInfo *joinrel)
 	}
 }
 
+/*
+ * unset_notnullattrs
+ */
+static void
+unset_notnullattrs(PlannerInfo *root, Relids relids)
+{
+	int i = -1;
+	while ((i = bms_next_member(relids, i)) >= 0)
+	{
+		root->simple_rel_array[i]->notnullattrs = NULL;
+	}
+}
+
 /*
  * build_join_rel
  *	  Returns relation entry corresponding to the union of two given rels,
@@ -671,6 +684,16 @@ build_join_rel(PlannerInfo *root,
 	joinrel->partexprs = NULL;
 	joinrel->nullable_partexprs = NULL;
 
+	if (sjinfo->jointype == JOIN_LEFT)
+	{
+		unset_notnullattrs(root, inner_rel->relids);
+	}
+	else if (sjinfo->jointype == JOIN_FULL)
+	{
+		unset_notnullattrs(root, inner_rel->relids);
+		unset_notnullattrs(root, outer_rel->relids);
+	}
+
 	/* Compute information relevant to the foreign relations. */
 	set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
 
-- 
2.21.0

