From 7ba4272a429ab4b6afe49c2014f3c35e2809fb3a Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas.vondra@postgresql.org>
Date: Mon, 3 Oct 2022 13:39:05 +0200
Subject: [PATCH 4/7] review

---
 src/backend/executor/execScan.c      |  6 ++++++
 src/backend/executor/nodeMergejoin.c | 32 ++++++++++++++++++++++++++++
 src/backend/executor/nodeSeqscan.c   |  3 +++
 3 files changed, 41 insertions(+)

diff --git a/src/backend/executor/execScan.c b/src/backend/executor/execScan.c
index 4b97c394550..e87fb5c6eba 100644
--- a/src/backend/executor/execScan.c
+++ b/src/backend/executor/execScan.c
@@ -210,6 +210,12 @@ ExecScan(ScanState *node,
 		 */
 		if (TupIsNull(slot))
 		{
+			/*
+			 * XXX I'm confused. Are we building or applying the filter for this
+			 * node? Because applySemiJoinFilter kinda suggests we're checking
+			 * the filter (to filter rows), but SemiJoinFilterFinishScan sets
+			 * doneBuilding=true, which suggests we're building it.
+			 */
 			if (IsA(node, SeqScanState) &&
 				((SeqScanState *) node)->applySemiJoinFilter)
 			{
diff --git a/src/backend/executor/nodeMergejoin.c b/src/backend/executor/nodeMergejoin.c
index 81c253aad58..9a1c0894060 100644
--- a/src/backend/executor/nodeMergejoin.c
+++ b/src/backend/executor/nodeMergejoin.c
@@ -1608,6 +1608,9 @@ ExecInitMergeJoin(MergeJoin *node, EState *estate, int eflags)
 
 	/*
 	 * initialize SemiJoinFilter, if planner decided to do so
+	 *
+	 * XXX I suggest we move this into a separate function, called e.g.
+	 * ExecInitMergeBloomFilter() or something like that.
 	 */
 	if (((MergeJoin *) mergestate->js.ps.plan)->applySemiJoinFilter)
 	{
@@ -1739,6 +1742,9 @@ ExecEndMergeJoin(MergeJoinState *node)
 
 	/*
 	 * free SemiJoinFilter
+	 *
+	 * XXX I suggest we do the "if" in the function. And maybe call it
+	 * ExecFreeSemiJoinFilter() to keep the naming more consistent.
 	 */
 	if (node->sjf)
 	{
@@ -1790,6 +1796,13 @@ FreeSemiJoinFilter(SemiJoinFilter * sjf)
  * Determines the direction that a pushdown filter can be pushed. This is not very robust, but this
  * is because we've already done the careful calculations at the plan level. If we end up pushing where
  * we're not supposed to, it's fine because we've done the verifications in the planner.
+ *
+ * XXX Not sure I understand the last sentence. If we did the verification in the planner,
+ * we should not end pushing where we're not supposed to, no? Or what happens?
+ *
+ * XXX Also, either this is specific to merge join, and then maybe call it ExecMergePush... or
+ * it's generic function to be used from other places, and then maybe it should be in some
+ * other file?
  */
 int
 PushDownDirection(PlanState *node)
@@ -1880,6 +1893,12 @@ PushDownFilter(PlanState *node, Plan *plan, SemiJoinFilter * sjf, Oid *relId, in
  * the bloom filter and always returns true. If this table is the checking-side table for the SemiJoinFilter,
  * then checks the element against the bloom filter and returns true if the element is (probably) in the set,
  * and false if the element is not in the bloom filter.
+ *
+ * XXX So we can be building and examining the filter at the same time? That is, we can
+ * be examining the filter before it's fully built? Seems a bit strange, and it can
+ * make the benefits pretty arbitrary, depending on when we finish building. For example
+ * if we have non-overlapping data (a,b) and (c,d) where b<c, it'll be great, but if we
+ * have d<a then we'll never filter anything.
  */
 bool
 SemiJoinFilterExamineSlot(List *semiJoinFilters, TupleTableSlot *slot, Oid tableId)
@@ -1890,6 +1909,19 @@ SemiJoinFilterExamineSlot(List *semiJoinFilters, TupleTableSlot *slot, Oid table
 	{
 		SemiJoinFilter *sjf = ((SemiJoinFilter *) (lfirst(cell)));
 
+		/*
+		 * XXX Not sure this is correct expectation, but can we assume we
+		 * don't start probing until it's built? It might be true with sort
+		 * on top of seqscan, but what about other scans, that can produce
+		 * rows incrementally?
+		 */
+		Assert(!(!sjf->doneBuilding && (tableId == sjf->checkingId)));
+
+		/*
+		 * XXX comparing OIDs is wrong, and will fail when the same table
+		 * is self-joined. Needs to use rtindex or something like that.
+		 */
+
 		/* check if this table's relation ID matches the filter's relation ID */
 		if (!sjf->doneBuilding && tableId == sjf->buildingId)
 		{
diff --git a/src/backend/executor/nodeSeqscan.c b/src/backend/executor/nodeSeqscan.c
index e43ce3f8d0f..f53e215d9be 100644
--- a/src/backend/executor/nodeSeqscan.c
+++ b/src/backend/executor/nodeSeqscan.c
@@ -210,6 +210,9 @@ ExecEndSeqScan(SeqScanState *node)
 
 	/*
 	 * clear out semijoinfilter list, if non-null
+	 *
+	 * XXX This only frees the list, not the nodes. But does it matter? If not
+	 * why free the list at all?
 	 */
 	if (node->semiJoinFilters)
 		pfree(node->semiJoinFilters);
-- 
2.37.3

