Fix the ASTERIXDB-1336 and ASTERIXDB-1413

Change-Id: Iffc60490aea144cec9abd38349853764024b0ce0
Reviewed-on: https://asterix-gerrit.ics.uci.edu/830
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
diff --git a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/OptimizedHybridHashJoin.java b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/OptimizedHybridHashJoin.java
index ebcb462..810039b 100644
--- a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/OptimizedHybridHashJoin.java
+++ b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/OptimizedHybridHashJoin.java
@@ -20,7 +20,9 @@
 
 import java.nio.ByteBuffer;
 import java.util.BitSet;
+import java.util.logging.Logger;
 
+import org.apache.commons.io.FileUtils;
 import org.apache.hyracks.api.comm.IFrame;
 import org.apache.hyracks.api.comm.IFrameWriter;
 import org.apache.hyracks.api.comm.VSizeFrame;
@@ -273,7 +275,7 @@
     }
 
     private boolean loadPartitionInMem(int pid, RunFileWriter wr) throws HyracksDataException {
-        RunFileReader r = wr.createDeleteOnCloseReader();
+        RunFileReader r = wr.createReader();
         r.open();
         if (reloadBuffer == null) {
             reloadBuffer = new VSizeFrame(ctx);
@@ -290,6 +292,7 @@
             }
         }
 
+        FileUtils.deleteQuietly(wr.getFileReference().getFile()); // delete the runfile if it already loaded into memory.
         r.close();
         spilledStatus.set(pid, false);
         buildRFWriters[pid] = null;