Hashed LEFT JOIN would miss outer tuples with no inner match if the join
authorTom Lane <[email protected]>
Fri, 17 Sep 2004 18:29:54 +0000 (18:29 +0000)
committerTom Lane <[email protected]>
Fri, 17 Sep 2004 18:29:54 +0000 (18:29 +0000)
was large enough to be batched and the tuples fell into a batch where
there were no inner tuples at all.  Thanks to Xiaoyu Wang for finding a
test case that exposed this long-standing bug.

src/backend/executor/nodeHashjoin.c

index e61b88f0b76938e87051dfd1390bb829029ada8b..09a732cb090f4fb0355a432b1fc11e2eb4bae113 100644 (file)
@@ -575,12 +575,14 @@ ExecHashJoinNewBatch(HashJoinState *hjstate)
        }
 
        /*
-        * We can skip over any batches that are empty on either side. Release
-        * associated temp files right away.
+        * Normally we can skip over any batches that are empty on either side
+        * --- but for JOIN_LEFT, can only skip when left side is empty.
+        * Release associated temp files right away.
         */
        while (newbatch <= nbatch &&
-                  (innerBatchSize[newbatch - 1] == 0L ||
-                       outerBatchSize[newbatch - 1] == 0L))
+                  (outerBatchSize[newbatch - 1] == 0L ||
+                       (innerBatchSize[newbatch - 1] == 0L &&
+                        hjstate->js.jointype != JOIN_LEFT)))
        {
                BufFileClose(hashtable->innerBatchFile[newbatch - 1]);
                hashtable->innerBatchFile[newbatch - 1] = NULL;