Merge branch 'gerrit/stabilization-f69489' into 'gerrit/mad-hatter'

Change-Id: I17edb6b03563ac527bcad39f93193067ee42a33d
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.1.ddl.sqlpp
new file mode 100644
index 0000000..bafc59a
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.1.ddl.sqlpp
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*
+ * Description     : Test that temporary run files generated are deleted as soon as possible and query runs fine
+ * Expected Result : Success
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use test;
+
+create type openType as open {id: uuid};
+create dataset ds1(openType) primary key id autogenerated;
+create dataset ds2(openType) primary key id autogenerated;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.2.update.sqlpp
new file mode 100644
index 0000000..9a695ff
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.2.update.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+insert into ds1(from range(0,1000000) as data select value {"f1": 1});
+insert into ds2(from range(0,1000000) as data select value {"f1": 2});
+
+insert into ds1 {"f1": 3};
+insert into ds2 {"f1": 3};
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.3.query.sqlpp
new file mode 100644
index 0000000..f668266
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.3.query.sqlpp
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+set `compiler.joinmemory` "180KB";
+
+from ds1 join ds2 on ds1.f1 = ds2.f1
+select ds1.f1;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.4.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.4.ddl.sqlpp
new file mode 100644
index 0000000..548e632
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.4.ddl.sqlpp
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+drop dataverse test if exists;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.1.adm
new file mode 100644
index 0000000..fca24df
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/join-ASTERIXDB-2686/join-ASTERIXDB-2686.1.adm
@@ -0,0 +1 @@
+{ "f1": 3 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index 24988bb..cc5d18d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -5897,6 +5897,11 @@
       </compilation-unit>
     </test-case>
     <test-case FilePath="misc">
+      <compilation-unit name="join-ASTERIXDB-2686">
+        <output-dir compare="Text">join-ASTERIXDB-2686</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="misc">
       <compilation-unit name="poll-dynamic">
         <output-dir compare="Text">poll-dynamic</output-dir>
       </compilation-unit>
diff --git a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/io/IWorkspaceFileFactory.java b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/io/IWorkspaceFileFactory.java
index 9a35bb8..6859ec7 100644
--- a/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/io/IWorkspaceFileFactory.java
+++ b/hyracks-fullstack/hyracks/hyracks-api/src/main/java/org/apache/hyracks/api/io/IWorkspaceFileFactory.java
@@ -18,10 +18,26 @@
  */
 package org.apache.hyracks.api.io;
 
+import org.apache.hyracks.api.context.IHyracksJobletContext;
+import org.apache.hyracks.api.context.IHyracksTaskContext;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
 
 public interface IWorkspaceFileFactory {
+
+    /**
+     * Creates an unmanaged file. Unmanaged files are not automatically deleted. The caller has to delete them.
+     *
+     * @param prefix a meaningful string to start the file name with.
+     */
     public FileReference createUnmanagedWorkspaceFile(String prefix) throws HyracksDataException;
 
+    /**
+     * Creates a managed file. Managed files are automatically deleted at a certain point. This depends on which
+     * context the file is created in. Files created in the {@link IHyracksJobletContext job context} will be deleted
+     * as soon as the job is done. Files created in the {@link IHyracksTaskContext task context} will be deleted as
+     * soon as the task is done.
+     *
+     * @param prefix a meaningful string to start the file name with.
+     */
     public FileReference createManagedWorkspaceFile(String prefix) throws HyracksDataException;
 }
diff --git a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/group/external/ExternalGroupWriteOperatorNodePushable.java b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/group/external/ExternalGroupWriteOperatorNodePushable.java
index 999dbac..e8a1b76 100644
--- a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/group/external/ExternalGroupWriteOperatorNodePushable.java
+++ b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/group/external/ExternalGroupWriteOperatorNodePushable.java
@@ -171,8 +171,7 @@
 
     @Override
     public RunFileWriter getRunFileWriter() throws HyracksDataException {
-        FileReference newRun = ctx.getJobletContext()
-                .createManagedWorkspaceFile(ExternalGroupOperatorDescriptor.class.getSimpleName());
+        FileReference newRun = ctx.createManagedWorkspaceFile(ExternalGroupOperatorDescriptor.class.getSimpleName());
         return new RunFileWriter(newRun, ctx.getIoManager());
     }
 }
diff --git a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/OptimizedHybridHashJoinOperatorDescriptor.java b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/OptimizedHybridHashJoinOperatorDescriptor.java
index 6d2b485..45cccec 100644
--- a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/OptimizedHybridHashJoinOperatorDescriptor.java
+++ b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/join/OptimizedHybridHashJoinOperatorDescriptor.java
@@ -434,6 +434,12 @@
                                 if (isLeftOuter && pReader != null) {
                                     appendNullToProbeTuples(pReader);
                                 }
+                                if (bReader != null) {
+                                    bReader.close();
+                                }
+                                if (pReader != null) {
+                                    pReader.close();
+                                }
                                 continue;
                             }
                             int bSize = state.hybridHJ.getBuildPartitionSizeInTup(pid);
@@ -628,6 +634,12 @@
                                         // For the outer join, we don't reverse the role.
                                         appendNullToProbeTuples(rprfw);
                                     }
+                                    if (rbrfw != null) {
+                                        rbrfw.close();
+                                    }
+                                    if (rprfw != null) {
+                                        rprfw.close();
+                                    }
                                     continue;
                                 }
 
@@ -652,6 +664,12 @@
                                         // For the outer join, we don't reverse the role.
                                         appendNullToProbeTuples(rprfw);
                                     }
+                                    if (rbrfw != null) {
+                                        rbrfw.close();
+                                    }
+                                    if (rprfw != null) {
+                                        rprfw.close();
+                                    }
                                     continue;
                                 }
 
diff --git a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/sort/AbstractExternalSortRunMerger.java b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/sort/AbstractExternalSortRunMerger.java
index 08b2303..1beaab8 100644
--- a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/sort/AbstractExternalSortRunMerger.java
+++ b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/sort/AbstractExternalSortRunMerger.java
@@ -111,7 +111,7 @@
                         } finally {
                             mergeResultWriter.close();
                         }
-                        reader = mergeFileWriter.createReader();
+                        reader = mergeFileWriter.createDeleteOnCloseReader();
                     }
                     runs.add(reader);