Fixed ASTERIXDB-1249 and ASTERIXDB-1250: self index-nested-loop join correctly identifies the outer and the inner branch.

The first dataset becomes the outer branch.
The second dataset becomes the inner branch.
The optimizer for index-nested-loop join now only try to use an index from the inner branch.

Change-Id: I0d4291197c2bcfbcdcde998c5952af41960c4ad7
Reviewed-on: https://asterix-gerrit.ics.uci.edu/576
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join-multipred.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join-multipred.aql
index 9e8a987..8a78d40 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join-multipred.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join-multipred.aql
@@ -20,7 +20,7 @@
  * Description    : Equi joins two datasets, Customers and Orders, based on the customer id.
  *                  Given the 'indexnl' hint we expect the join to be transformed
  *                  into an indexed nested-loop join using Customers' primary index.
- *                  We expect the additional predicates to be put into a select above the 
+ *                  We expect the additional predicates to be put into a select above the
  *                  primary index search.
  * Success        : Yes
  */
@@ -30,13 +30,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -60,7 +60,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-equi-join-multipred.adm";
 
-for $c in dataset('Customers')
 for $o in dataset('Orders')
+for $c in dataset('Customers')
 where $c.cid /*+ indexnl */ = $o.cid and $c.name < $o.orderstatus and $c.age < $o.cid
-return {"customer":$c, "order": $o} 
+return {"customer":$c, "order": $o}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_01.aql
index 48e7965..aeb6cbb 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_01.aql
@@ -40,8 +40,8 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-equi-join_01.adm";
 
-for $x in dataset('test1.DsOne')
 for $y in dataset('test1.DsTwo')
+for $x in dataset('test1.DsOne')
 where $x.key1 /*+ indexnl */ = $y.key2
 return $x
 
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_02.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_02.aql
index a4b5264..2aa29f4 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_02.aql
@@ -17,8 +17,8 @@
  * under the License.
  */
 /*
- * Description  : Notice the query hint to use an indexed nested-loops join plan. 
- *              : We expect a plan that hash-exchanges internal dataset DsTwo, then probes internal dataset DsOne’s primary index. 
+ * Description  : Notice the query hint to use an indexed nested-loops join plan.
+ *              : We expect a plan that hash-exchanges internal dataset DsTwo, then probes internal dataset DsOne’s primary index.
  * Expected Res : Success
  * Date         : 29th November 2012
  */
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_03.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_03.aql
index 025b101..622e8e1 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_03.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_03.aql
@@ -28,13 +28,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -58,7 +58,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-equi-join_04.adm";
 
-for $c in dataset('Customers')
 for $o in dataset('Orders')
+for $c in dataset('Customers')
 where $c.cid /*+ indexnl */ = $o.cid
-return {"customer":$c, "order": $o} 
+return {"customer":$c, "order": $o}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-ge-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-ge-join_01.aql
index 5ae298c..72b8b87 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-ge-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-ge-join_01.aql
@@ -17,8 +17,8 @@
  * under the License.
  */
 /*
- * Description  : Notice the query hint to use an indexed nested-loops join plan. 
- *              : We expect a plan that broadcasts internal dataset DsTwo, then probes internal dataset DsOne’s primary index. 
+ * Description  : Notice the query hint to use an indexed nested-loops join plan.
+ *              : We expect a plan that broadcasts internal dataset DsTwo, then probes internal dataset DsOne’s primary index.
  * Expected Res : Success
  * Date         : 29th November 2012
  */
@@ -40,8 +40,8 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-ge-join_01.adm";
 
-for $x in dataset('test1.DsOne')
 for $y in dataset('test1.DsTwo')
+for $x in dataset('test1.DsOne')
 where $x.key1 /*+ indexnl */ >= $y.key2
 return $x
 
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-gt-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-gt-join_01.aql
index f0d1853..05d3f89 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-gt-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-gt-join_01.aql
@@ -17,8 +17,8 @@
  * under the License.
  */
 /*
- * Description  : Notice the query hint to use an indexed nested-loops join plan. 
- *              : We expect a plan that broadcasts internal dataset DsTwo, then probes internal dataset DsOne’s primary index. 
+ * Description  : Notice the query hint to use an indexed nested-loops join plan.
+ *              : We expect a plan that broadcasts internal dataset DsTwo, then probes internal dataset DsOne’s primary index.
  * Expected Res : Success
  * Date         : 29th November 2012
  */
@@ -40,8 +40,8 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-gt-join_01.adm";
 
-for $x in dataset('test1.DsOne')
 for $y in dataset('test1.DsTwo')
+for $x in dataset('test1.DsOne')
 where $x.key1 /*+ indexnl */ > $y.key2
 return $x
 
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-le-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-le-join_01.aql
index a880f16..6353d16 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-le-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-le-join_01.aql
@@ -17,8 +17,8 @@
  * under the License.
  */
 /*
- * Description  : Notice the query hint to use an indexed nested-loops join plan. 
- *              : We expect a plan that broadcasts internal dataset DsTwo, then probes internal dataset DsOne’s primary index. 
+ * Description  : Notice the query hint to use an indexed nested-loops join plan.
+ *              : We expect a plan that broadcasts internal dataset DsTwo, then probes internal dataset DsOne’s primary index.
  * Expected Res : Success
  * Date         : 29th November 2012
  */
@@ -40,8 +40,8 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-le-join_01.adm";
 
-for $x in dataset('test1.DsOne')
 for $y in dataset('test1.DsTwo')
+for $x in dataset('test1.DsOne')
 where $x.key1 /*+ indexnl */ <= $y.key2
 return $x
 
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-lt-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-lt-join_01.aql
index e87265c..a5a964d 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-lt-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-lt-join_01.aql
@@ -17,8 +17,8 @@
  * under the License.
  */
 /*
- * Description  : Notice the query hint to use an indexed nested-loops join plan. 
- *              : We expect a plan that broadcasts internal dataset DsTwo, then probes internal dataset DsOne’s primary index. 
+ * Description  : Notice the query hint to use an indexed nested-loops join plan.
+ *              : We expect a plan that broadcasts internal dataset DsTwo, then probes internal dataset DsOne’s primary index.
  * Expected Res : Success
  * Date         : 29th November 2012
  */
@@ -40,8 +40,8 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-lt-join_01.adm";
 
-for $x in dataset('test1.DsOne')
 for $y in dataset('test1.DsTwo')
+for $x in dataset('test1.DsOne')
 where $x.key1 /*+ indexnl */ < $y.key2
 return $x
 
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-equi-join-multipred.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-equi-join-multipred.aql
index dd4ca77..bffaf9c 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-equi-join-multipred.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-equi-join-multipred.aql
@@ -18,9 +18,9 @@
  */
 /*
  * Description    : Equi joins two datasets, DBLP and CSX, based on their title.
- *                  DBLP has a secondary btree index on title, and given the 'indexnl' hint 
+ *                  DBLP has a secondary btree index on title, and given the 'indexnl' hint
  *                  we expect the join to be transformed into an indexed nested-loop join.
- *                  We expect the additional predicates to be put into a select above the 
+ *                  We expect the additional predicates to be put into a select above the
  *                  primary index search.
  * Success        : Yes
  */
@@ -30,7 +30,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -38,7 +38,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -53,7 +53,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_title-secondary-equi-join-multipred.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.title /*+ indexnl */ = $b.title and $a.authors < $b.authors and $a.misc > $b.misc
 return {"arec": $a, "brec": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-equi-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-equi-join_01.aql
index 4de7a0b..eb2d407 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-equi-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-equi-join_01.aql
@@ -18,7 +18,7 @@
  */
 /*
  * Description    : Equi joins two datasets, DBLP and CSX, based on their title.
- *                  DBLP has a secondary btree index on title, and given the 'indexnl' hint 
+ *                  DBLP has a secondary btree index on title, and given the 'indexnl' hint
  *                  we expect the join to be transformed into an indexed nested-loop join.
  * Success        : Yes
  */
@@ -28,7 +28,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -51,7 +51,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_title-secondary-equi-join_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.title /*+ indexnl */ = $b.title
 return {"arec": $a, "brec": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-self-equi-join.aql b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-self-equi-join.aql
new file mode 100644
index 0000000..abfd197
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-self-equi-join.aql
@@ -0,0 +1,65 @@
+/*
+ * 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    : Self-equi joins on a dataset using two fields - countA and countB.
+ *                  TweetMessages has a secondary btree index on countB, and given the 'indexnl' hint
+ *                  we expect the join to be transformed into an indexed nested-loop join.
+ * Success        : Yes
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type TwitterUserType as closed {
+    screen-name: string,
+    lang: string,
+    friends-count: int64,
+    statuses-count: int64,
+    name: string,
+    followers-count: int64
+}
+
+create type TweetMessageType as closed {
+    tweetid: int64,
+        user: TwitterUserType,
+        sender-location: point,
+    send-time: datetime,
+        referred-topics: {{ string }},
+    message-text: string,
+    countA: int64,
+    countB: int64
+}
+
+create dataset TweetMessages(TweetMessageType)
+primary key tweetid;
+
+create index twmSndLocIx on TweetMessages(sender-location) type rtree;
+create index msgCountAIx on TweetMessages(countA) type btree;
+create index msgCountBIx on TweetMessages(countB) type btree;
+create index msgTextIx on TweetMessages(message-text) type keyword;
+
+write output to asterix_nc1:"rttest/btree-index-join_self-secondary-equi-join.adm";
+
+for $t1 in dataset('TweetMessages')
+for $t2 in dataset('TweetMessages')
+let $c := $t1.countA + 20
+where $c /* +indexnl */= $t2.countB
+order by $t2.tweetid
+return {"tweetid2": $t2.tweetid, "count2":$t2.countB};
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-edit-distance.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-edit-distance.aql
index 991dd0b..6640508 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-edit-distance.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-edit-distance.aql
@@ -19,7 +19,7 @@
 /*
  * Description    : Fuzzy joins two datasets, DBLP and CSX, based on the edit-distance function of their authors.
  *                  DBLP has a 3-gram index on authors, and we expect the join to be transformed into an indexed nested-loop join.
- *                  We expect the top-level equi join introduced because of surrogate optimization to be removed, since it is not necessary. 
+ *                  We expect the top-level equi join introduced because of surrogate optimization to be removed, since it is not necessary.
  * Success        : Yes
  */
 
@@ -28,7 +28,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -51,7 +51,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_ngram-edit-distance.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance($a.authors, $b.authors) < 3 and $a.id < $b.id
 return {"aauthors": $a.authors, "bauthors": $b.authors}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-fuzzyeq-jaccard.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-fuzzyeq-jaccard.aql
index 635b331..72e4b69 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-fuzzyeq-jaccard.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-fuzzyeq-jaccard.aql
@@ -29,7 +29,7 @@
 set import-private-functions 'true';
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -37,7 +37,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -55,7 +55,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where gram-tokens($a.title, 3, false) ~= gram-tokens($b.title, 3, false) and $a.id < $b.id
 return {"atitle": $a.title, "btitle": $b.title}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-jaccard.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-jaccard.aql
index 5449ea4..5f0c612 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-jaccard.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-jaccard.aql
@@ -29,7 +29,7 @@
 set import-private-functions 'true';
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -37,7 +37,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -52,8 +52,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_ngram-jaccard.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard(gram-tokens($a.title, 3, false), gram-tokens($b.title, 3, false)) >= 0.5f
       and $a.id < $b.id
 return {"atitle": $a.title, "btitle": $b.title}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-edit-distance.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-edit-distance.aql
index 8d7bb42..0e7ee97 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-edit-distance.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-edit-distance.aql
@@ -28,13 +28,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -43,14 +43,14 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_olist-edit-distance.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where edit-distance($a.interests, $b.interests) <= 2 and $a.cid < $b.cid
 return {"ainterests": $a.interests, "binterests": $b.interests}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-fuzzyeq-edit-distance.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-fuzzyeq-edit-distance.aql
index 416e89f..0021360 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-fuzzyeq-edit-distance.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-fuzzyeq-edit-distance.aql
@@ -28,13 +28,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -43,7 +43,7 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
@@ -53,7 +53,7 @@
 set simfunction 'edit-distance';
 set simthreshold '3';
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where $a.interests ~= $b.interests and $a.cid < $b.cid
 return {"ainterests": $a.interests, "binterests": $b.interests}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-fuzzyeq-jaccard.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-fuzzyeq-jaccard.aql
index 48f30a5..ba31c49 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-fuzzyeq-jaccard.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-fuzzyeq-jaccard.aql
@@ -28,13 +28,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -43,7 +43,7 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
@@ -53,7 +53,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.7f';
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where $a.interests /*+ indexnl */ ~= $b.interests and $a.cid < $b.cid
 return {"ainterests": $a.interests, "binterests": $b.interests}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-jaccard.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-jaccard.aql
index a6767bd..eec411d 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-jaccard.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-jaccard.aql
@@ -28,13 +28,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -43,14 +43,14 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_olist-jaccard.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where /*+ indexnl */ similarity-jaccard($a.interests, $b.interests) >= 0.7f and $a.cid < $b.cid
 return {"ainterests": $a.interests, "binterests": $b.interests}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ulist-fuzzyeq-jaccard.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ulist-fuzzyeq-jaccard.aql
index 907afce..f2983ce 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ulist-fuzzyeq-jaccard.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ulist-fuzzyeq-jaccard.aql
@@ -28,13 +28,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -43,7 +43,7 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
@@ -53,7 +53,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.7f';
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where $a.interests /*+ indexnl */ ~= $b.interests and $a.cid < $b.cid
 return {"ainterests": $a.interests, "binterests": $b.interests}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ulist-jaccard.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ulist-jaccard.aql
index 2bc78e0..557990c 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ulist-jaccard.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ulist-jaccard.aql
@@ -28,13 +28,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -43,14 +43,14 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_ulist-jaccard.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where /*+ indexnl */ similarity-jaccard($a.interests, $b.interests) >= 0.7f and $a.cid < $b.cid
 return {"ainterests": $a.interests, "binterests": $b.interests}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/word-fuzzyeq-jaccard.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/word-fuzzyeq-jaccard.aql
index 0640ee0..1b90252 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/word-fuzzyeq-jaccard.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/word-fuzzyeq-jaccard.aql
@@ -28,7 +28,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -54,7 +54,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where word-tokens($a.title) ~= word-tokens($b.title) and $a.id < $b.id
 return {"atitle": $a.title, "btitle": $b.title}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/word-jaccard.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/word-jaccard.aql
index 0902832..4558d49 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/word-jaccard.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/word-jaccard.aql
@@ -28,7 +28,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -51,8 +51,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_word-jaccard.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard(word-tokens($a.title), word-tokens($b.title)) >= 0.5f
       and $a.id < $b.id
 return {"atitle": $a.title, "btitle": $b.title}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-check_01.aql
index e3d2c61..ed212cc 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-check_01.aql
@@ -27,7 +27,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -35,7 +35,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -50,7 +50,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance-check($a.authors, $b.authors, 3)[0] and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-check_03.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-check_03.aql
index 62964d9..95b2912 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-check_03.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-check_03.aql
@@ -27,7 +27,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-contains.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-contains.aql
index 4178083..7d9c5d2 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-contains.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-contains.aql
@@ -50,7 +50,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance-contains.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance-contains($a.authors, $b.authors, 3)[0] and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance_01.aql
index 4a1ace5..391f165 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance_01.aql
@@ -27,7 +27,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -35,7 +35,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -50,7 +50,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance($a.authors, $b.authors) < 3 and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql
index cfed751..50ed8d5 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql
@@ -27,7 +27,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -35,7 +35,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -53,7 +53,7 @@
 set simfunction 'edit-distance';
 set simthreshold '3';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.authors ~= $b.authors and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql
index 93e3a5f..a9aa579 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql
@@ -28,7 +28,7 @@
 set import-private-functions 'true';
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -54,7 +54,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where gram-tokens($a.title, 3, false) ~= gram-tokens($b.title, 3, false) and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-jaccard-check_01.aql
index 629b72a..07e6c18 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-jaccard-check_01.aql
@@ -28,7 +28,7 @@
 set import-private-functions 'true';
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -51,8 +51,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-jaccard-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard-check(gram-tokens($a.title, 3, false), gram-tokens($b.title, 3, false), 0.5f)[0]
       and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-jaccard_01.aql
index 36ba05c..a68d600 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-jaccard_01.aql
@@ -28,7 +28,7 @@
 set import-private-functions 'true';
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -51,8 +51,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-jaccard_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard(gram-tokens($a.title, 3, false), gram-tokens($b.title, 3, false)) >= 0.5f
       and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-edit-distance-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-edit-distance-check_01.aql
index 10a7c91..d02cee7 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-edit-distance-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-edit-distance-check_01.aql
@@ -27,13 +27,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -42,14 +42,14 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_olist-edit-distance-check_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where edit-distance-check($a.interests, $b.interests, 3)[0] and $a.cid < $b.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-edit-distance_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-edit-distance_01.aql
index 8628ed4..b46fbc1 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-edit-distance_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-edit-distance_01.aql
@@ -27,13 +27,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -42,14 +42,14 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_olist-edit-distance_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where edit-distance($a.interests, $b.interests) <= 2 and $a.cid < $b.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-fuzzyeq-edit-distance_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-fuzzyeq-edit-distance_01.aql
index 05d275b..840e9c7 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-fuzzyeq-edit-distance_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-fuzzyeq-edit-distance_01.aql
@@ -27,13 +27,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -42,7 +42,7 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
@@ -52,7 +52,7 @@
 set simfunction 'edit-distance';
 set simthreshold '3';
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where $a.interests ~= $b.interests and $a.cid < $b.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-fuzzyeq-jaccard_01.aql
index 09cc6e4..fb39ddf 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-fuzzyeq-jaccard_01.aql
@@ -27,13 +27,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -42,7 +42,7 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
@@ -52,7 +52,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.7f';
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where $a.interests /*+ indexnl */ ~= $b.interests and $a.cid < $b.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-jaccard-check_01.aql
index 0459a4b..14e3dc5 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-jaccard-check_01.aql
@@ -27,13 +27,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -42,14 +42,14 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_olist-jaccard-check_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where /*+ indexnl */ similarity-jaccard-check($a.interests, $b.interests, 0.7f)[0] and $a.cid < $b.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-jaccard_01.aql
index c76880d..4c865f6 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-jaccard_01.aql
@@ -27,13 +27,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -42,14 +42,14 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_olist-jaccard_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where /*+ indexnl */ similarity-jaccard($a.interests, $b.interests) >= 0.7f and $a.cid < $b.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-fuzzyeq-jaccard_01.aql
index aacd110..edd1e94 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-fuzzyeq-jaccard_01.aql
@@ -27,13 +27,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -42,7 +42,7 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
@@ -52,7 +52,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.7f';
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where $a.interests /*+ indexnl */ ~= $b.interests and $a.cid < $b.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-jaccard-check_01.aql
index 9560995..53ff785 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-jaccard-check_01.aql
@@ -27,13 +27,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -42,14 +42,14 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_ulist-jaccard-check_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where /*+ indexnl */ similarity-jaccard-check($a.interests, $b.interests, 0.7f)[0] and $a.cid < $b.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-jaccard_01.aql
index 887ac20..e23334f 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ulist-jaccard_01.aql
@@ -27,13 +27,13 @@
 use dataverse test;
 
 create type AddressType as closed {
-  number: int32, 
+  number: int32,
   street: string,
   city: string
 }
 
 create type CustomerType as closed {
-  cid: int32, 
+  cid: int32,
   name: string,
   age: int32?,
   address: AddressType?,
@@ -42,14 +42,14 @@
 }
 
 create dataset Customers(CustomerType) primary key cid;
-  
+
 create dataset Customers2(CustomerType) primary key cid;
 
 create index interests_index on Customers(interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_ulist-jaccard_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where /*+ indexnl */ similarity-jaccard($a.interests, $b.interests) >= 0.7f and $a.cid < $b.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-fuzzyeq-jaccard_01.aql
index d9a5afa..29bd3d0 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-fuzzyeq-jaccard_01.aql
@@ -27,7 +27,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -35,7 +35,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -53,7 +53,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where word-tokens($a.title) ~= word-tokens($b.title) and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-jaccard-check_01.aql
index 78be4fe..989f250 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-jaccard-check_01.aql
@@ -27,7 +27,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -35,7 +35,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -50,8 +50,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_word-jaccard-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard-check(word-tokens($a.title), word-tokens($b.title), 0.5f)[0]
       and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-jaccard_01.aql
index 21d5c3e..e2c373a 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/word-jaccard_01.aql
@@ -27,7 +27,7 @@
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -35,7 +35,7 @@
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -50,8 +50,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_word-jaccard_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard(word-tokens($a.title), word-tokens($b.title)) >= 0.5f
       and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join-multipred.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join-multipred.aql
index 3662074..c681194 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join-multipred.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join-multipred.aql
@@ -68,7 +68,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-equi-join-multipred.adm";
 
-for $c in dataset('Customers')
 for $o in dataset('Orders')
+for $c in dataset('Customers')
 where $c.nested.cid /*+ indexnl */ = $o.nested.cid and $c.nested.name < $o.nested.orderstatus and $c.nested.age < $o.nested.cid
 return {"customer":$c.nested, "order": $o.nested}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join_01.aql
index 1f6ff8f..2794abc 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join_01.aql
@@ -44,8 +44,8 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-equi-join_01.adm";
 
-for $x in dataset('test1.DsOne')
 for $y in dataset('test1.DsTwo')
+for $x in dataset('test1.DsOne')
 where $x.nested.key1 /*+ indexnl */ = $y.nested.key2
 return $x
 
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join_03.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join_03.aql
index 0b9df82..08042d6 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join_03.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-equi-join_03.aql
@@ -66,7 +66,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-equi-join_04.adm";
 
-for $c in dataset('Customers')
 for $o in dataset('Orders')
+for $c in dataset('Customers')
 where $c.nested.cid /*+ indexnl */ = $o.nested.cid
 return {"customer":$c.nested, "order": $o.nested}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-ge-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-ge-join_01.aql
index c230ee7..86887b9 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-ge-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-ge-join_01.aql
@@ -44,8 +44,8 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-ge-join_01.adm";
 
-for $x in dataset('test1.DsOne')
 for $y in dataset('test1.DsTwo')
+for $x in dataset('test1.DsOne')
 where $x.nested.key1 /*+ indexnl */ >= $y.nested.key2
 return $x
 
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-gt-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-gt-join_01.aql
index 1d50be7..d72c87c 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-gt-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-gt-join_01.aql
@@ -44,8 +44,8 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-gt-join_01.adm";
 
-for $x in dataset('test1.DsOne')
 for $y in dataset('test1.DsTwo')
+for $x in dataset('test1.DsOne')
 where $x.nested.key1 /*+ indexnl */ > $y.nested.key2
 return $x
 
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-le-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-le-join_01.aql
index e66f0b8..f5fa9ed 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-le-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-le-join_01.aql
@@ -44,8 +44,8 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-le-join_01.adm";
 
-for $x in dataset('test1.DsOne')
 for $y in dataset('test1.DsTwo')
+for $x in dataset('test1.DsOne')
 where $x.nested.key1 /*+ indexnl */ <= $y.nested.key2
 return $x
 
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-lt-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-lt-join_01.aql
index a0d32a0..b623032 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-lt-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/primary-lt-join_01.aql
@@ -44,8 +44,8 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_primary-lt-join_01.adm";
 
-for $x in dataset('test1.DsOne')
 for $y in dataset('test1.DsTwo')
+for $x in dataset('test1.DsOne')
 where $x.nested.key1 /*+ indexnl */ < $y.nested.key2
 return $x
 
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/secondary-equi-join-multipred.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/secondary-equi-join-multipred.aql
index d3dacd3..ecad56d 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/secondary-equi-join-multipred.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/secondary-equi-join-multipred.aql
@@ -61,7 +61,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_title-secondary-equi-join-multipred.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.nested.title /*+ indexnl */ = $b.nested.title and $a.nested.authors < $b.nested.authors and $a.nested.misc > $b.nested.misc
 return {"arec": $a, "brec": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/secondary-equi-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/secondary-equi-join_01.aql
index 5e6e852..d0d2d57 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/secondary-equi-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/btree-index-join/secondary-equi-join_01.aql
@@ -59,7 +59,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_title-secondary-equi-join_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.nested.title /*+ indexnl */ = $b.nested.title
 return {"arec": $a, "brec": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance-check_01.aql
index d17f352..defd28d 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance-check_01.aql
@@ -58,7 +58,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance-check($a.nested.authors, $b.nested.authors, 3)[0] and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance-contains.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance-contains.aql
index a18fd38..3190e66 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance-contains.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance-contains.aql
@@ -58,7 +58,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance-contains.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance-contains($a.nested.authors, $b.nested.authors, 3)[0] and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance_01.aql
index 3a1bcbc..e7e6dc7 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ngram-edit-distance_01.aql
@@ -58,7 +58,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance($a.nested.authors, $b.nested.authors) < 3 and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-edit-distance-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-edit-distance-check_01.aql
index 3a8a273..354b943 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-edit-distance-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-edit-distance-check_01.aql
@@ -48,14 +48,14 @@
 }
 
 create dataset Customers(CustomerType) primary key nested.cid;
- 
+
 create dataset Customers2(CustomerType) primary key nested.cid;
 
 create index interests_index on Customers(nested.interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_olist-edit-distance-check_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where edit-distance-check($a.nested.interests, $b.nested.interests, 3)[0] and $a.nested.cid < $b.nested.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-edit-distance_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-edit-distance_01.aql
index 28214c9..2b375fd 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-edit-distance_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-edit-distance_01.aql
@@ -47,14 +47,14 @@
 }
 
 create dataset Customers(CustomerType) primary key nested.cid;
- 
+
 create dataset Customers2(CustomerType) primary key nested.cid;
 
 create index interests_index on Customers(nested.interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_olist-edit-distance_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where edit-distance($a.nested.interests, $b.nested.interests) <= 2 and $a.nested.cid < $b.nested.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-fuzzyeq-edit-distance_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-fuzzyeq-edit-distance_01.aql
index f2d633e..a235f67 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-fuzzyeq-edit-distance_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-fuzzyeq-edit-distance_01.aql
@@ -46,7 +46,7 @@
 }
 
 create dataset Customers(CustomerType) primary key nested.cid;
- 
+
 create dataset Customers2(CustomerType) primary key nested.cid;
 
 create index interests_index on Customers(nested.interests) type keyword;
@@ -56,7 +56,7 @@
 set simfunction 'edit-distance';
 set simthreshold '3';
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where $a.nested.interests ~= $b.nested.interests and $a.nested.cid < $b.nested.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-fuzzyeq-jaccard_01.aql
index d44dbbe..06ade10 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-fuzzyeq-jaccard_01.aql
@@ -46,7 +46,7 @@
 }
 
 create dataset Customers(CustomerType) primary key nested.cid;
- 
+
 create dataset Customers2(CustomerType) primary key nested.cid;
 
 create index interests_index on Customers(nested.interests) type keyword;
@@ -56,7 +56,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.7f';
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where $a.nested.interests /*+ indexnl */ ~= $b.nested.interests and $a.nested.cid < $b.nested.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-jaccard-check_01.aql
index 2adaea5..153ea02 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-jaccard-check_01.aql
@@ -46,14 +46,14 @@
 }
 
 create dataset Customers(CustomerType) primary key nested.cid;
- 
+
 create dataset Customers2(CustomerType) primary key nested.cid;
 
 create index interests_index on Customers(nested.interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_olist-jaccard-check_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where /*+ indexnl */ similarity-jaccard-check($a.nested.interests, $b.nested.interests, 0.7f)[0] and $a.nested.cid < $b.nested.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-jaccard_01.aql
index 825e3fa..fb51eff 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/olist-jaccard_01.aql
@@ -46,14 +46,14 @@
 }
 
 create dataset Customers(CustomerType) primary key nested.cid;
- 
+
 create dataset Customers2(CustomerType) primary key nested.cid;
 
 create index interests_index on Customers(nested.interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_olist-jaccard_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where /*+ indexnl */ similarity-jaccard($a.nested.interests, $b.nested.interests) >= 0.7f and $a.nested.cid < $b.nested.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-fuzzyeq-jaccard_01.aql
index 234fb6f..998e29c 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-fuzzyeq-jaccard_01.aql
@@ -46,7 +46,7 @@
 }
 
 create dataset Customers(CustomerType) primary key nested.cid;
- 
+
 create dataset Customers2(CustomerType) primary key nested.cid;
 
 create index interests_index on Customers(nested.interests) type keyword;
@@ -56,7 +56,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.7f';
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where $a.nested.interests /*+ indexnl */ ~= $b.nested.interests and $a.nested.cid < $b.nested.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-jaccard-check_01.aql
index 1d483a1..4493a40 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-jaccard-check_01.aql
@@ -46,14 +46,14 @@
 }
 
 create dataset Customers(CustomerType) primary key nested.cid;
- 
+
 create dataset Customers2(CustomerType) primary key nested.cid;
 
 create index interests_index on Customers(nested.interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_ulist-jaccard-check_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where /*+ indexnl */ similarity-jaccard-check($a.nested.interests, $b.nested.interests, 0.7f)[0] and $a.nested.cid < $b.nested.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-jaccard_01.aql
index bb95661..b39409e 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/ulist-jaccard_01.aql
@@ -46,14 +46,14 @@
 }
 
 create dataset Customers(CustomerType) primary key nested.cid;
- 
+
 create dataset Customers2(CustomerType) primary key nested.cid;
 
 create index interests_index on Customers(nested.interests) type keyword;
 
 write output to asterix_nc1:"rttest/inverted-index-join_ulist-jaccard_01.adm";
 
-for $a in dataset('Customers')
 for $b in dataset('Customers2')
+for $a in dataset('Customers')
 where /*+ indexnl */ similarity-jaccard($a.nested.interests, $b.nested.interests) >= 0.7f and $a.nested.cid < $b.nested.cid
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-fuzzyeq-jaccard_01.aql
index a66b7b3..e45b02d 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-fuzzyeq-jaccard_01.aql
@@ -61,7 +61,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where word-tokens($a.nested.title) ~= word-tokens($b.nested.title) and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-jaccard-check_01.aql
index 376ecec..55c2ff4 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-jaccard-check_01.aql
@@ -58,8 +58,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_word-jaccard-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard-check(word-tokens($a.nested.title), word-tokens($b.nested.title), 0.5f)[0]
       and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-jaccard_01.aql
index e374a44..acbe3db 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/inverted-index-join/word-jaccard_01.aql
@@ -58,8 +58,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_word-jaccard_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard(word-tokens($a.nested.title), word-tokens($b.nested.title)) >= 0.5f
       and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-index/rtree-index-join/spatial-intersect-point_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-index/rtree-index-join/spatial-intersect-point_01.aql
index c2a8711..b5f36e7 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-index/rtree-index-join/spatial-intersect-point_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-index/rtree-index-join/spatial-intersect-point_01.aql
@@ -49,7 +49,7 @@
 
 write output to asterix_nc1:"rttest/index-join_rtree-spatial-intersect-point.adm";
 
-for $a in dataset('MyData1')
 for $b in dataset('MyData2')
+for $a in dataset('MyData1')
 where spatial-intersect($a.nested.point, $b.point)
 return {"a": $a, "b": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/btree-index-join/secondary-equi-join-multipred.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/btree-index-join/secondary-equi-join-multipred.aql
index 378f6bb..b4e7e78 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/btree-index-join/secondary-equi-join-multipred.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/btree-index-join/secondary-equi-join-multipred.aql
@@ -60,7 +60,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_title-secondary-equi-join-multipred.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.nested.title /*+ indexnl */ = $b.nested.title and $a.nested.authors < $b.nested.authors and $a.nested.misc > $b.nested.misc
 return {"arec": $a, "brec": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/btree-index-join/secondary-equi-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/btree-index-join/secondary-equi-join_01.aql
index 518ea36..fca3294 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/btree-index-join/secondary-equi-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/btree-index-join/secondary-equi-join_01.aql
@@ -58,7 +58,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_title-secondary-equi-join_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.nested.title /*+ indexnl */ = $b.nested.title
 return {"arec": $a, "brec": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-contains_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-contains_01.aql
index 7d63fd1..30b7078 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-contains_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-contains_01.aql
@@ -48,8 +48,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-contains-01.adm";
 
-for $o1 in dataset('DBLP')
 for $o2 in dataset('CSX')
+for $o1 in dataset('DBLP')
 where contains($o1.title, $o2.title) and $o1.id < $o2.id
 order by $o1.id, $o2.id
 return {"title1":$o1.title, "title2":$o2.title}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-contains_02.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-contains_02.aql
index fc3e555..7f48b7a 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-contains_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-contains_02.aql
@@ -48,8 +48,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-contains-02.adm";
 
-for $o1 in dataset('CSX')
 for $o2 in dataset('DBLP')
+for $o1 in dataset('CSX')
 where contains($o1.title, $o2.title) and $o1.id < $o2.id
 order by $o1.id, $o2.id
 return {"title1":$o1.title, "title2":$o2.title}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-check_01.aql
index 06ab807..ae3f431 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-check_01.aql
@@ -57,7 +57,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance-check($a.nested.authors, $b.nested.authors, 3)[0] and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-check_02.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-check_02.aql
index 8147e81..b916550 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-check_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-check_02.aql
@@ -57,7 +57,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance-check_01.adm";
 
-for $a in dataset('CSX')
 for $b in dataset('DBLP')
+for $a in dataset('CSX')
 where edit-distance-check($a.nested.authors, $b.nested.authors, 3)[0] and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-contains.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-contains.aql
index 6319464..62a20f5 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-contains.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance-contains.aql
@@ -57,7 +57,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance-contains.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance-contains($a.nested.authors, $b.nested.authors, 3)[0] and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance_01.aql
index 7d53c14..c7e0b7e 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance_01.aql
@@ -57,7 +57,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance($a.nested.authors, $b.nested.authors) < 3 and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance_02.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance_02.aql
index 9d5317a..cd8dc85 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-edit-distance_02.aql
@@ -57,7 +57,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance_01.adm";
 
-for $a in dataset('CSX')
 for $b in dataset('DBLP')
+for $a in dataset('CSX')
 where edit-distance($a.nested.authors, $b.nested.authors) < 3 and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-edit-distance_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-edit-distance_01.aql
index 30231fb..fba60ad 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-edit-distance_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-edit-distance_01.aql
@@ -60,7 +60,7 @@
 set simfunction 'edit-distance';
 set simthreshold '3';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.nested.authors ~= $b.nested.authors and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql
index 38302e9..e89dd53 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql
@@ -60,7 +60,7 @@
 set simfunction 'edit-distance';
 set simthreshold '3';
 
-for $a in dataset('CSX')
 for $b in dataset('DBLP')
+for $a in dataset('CSX')
 where $a.nested.authors ~= $b.nested.authors and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql
index 2756836..8cf3373 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql
@@ -61,7 +61,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where gram-tokens($a.nested.title, 3, false) ~= gram-tokens($b.nested.title, 3, false) and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-jaccard_02.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-jaccard_02.aql
index a930fad..2de6fb3 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-jaccard_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-fuzzyeq-jaccard_02.aql
@@ -61,7 +61,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('CSX')
 for $b in dataset('DBLP')
+for $a in dataset('CSX')
 where gram-tokens($a.nested.title, 3, false) ~= gram-tokens($b.nested.title, 3, false) and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard-check_01.aql
index 263c5fb..f26b0e9 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard-check_01.aql
@@ -58,8 +58,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-jaccard-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard-check(gram-tokens($a.nested.title, 3, false), gram-tokens($b.nested.title, 3, false), 0.5f)[0]
       and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard-check_02.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard-check_02.aql
index 4fa503d..2a834ce 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard-check_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard-check_02.aql
@@ -58,8 +58,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-jaccard-check_01.adm";
 
-for $a in dataset('CSX')
 for $b in dataset('DBLP')
+for $a in dataset('CSX')
 where similarity-jaccard-check(gram-tokens($a.nested.title, 3, false), gram-tokens($b.nested.title, 3, false), 0.5f)[0]
       and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard_01.aql
index dd83e2e..14cb1ff 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard_01.aql
@@ -58,8 +58,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-jaccard_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard(gram-tokens($a.nested.title, 3, false), gram-tokens($b.nested.title, 3, false)) >= 0.5f
       and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard_02.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard_02.aql
index 1d77aa7..e85c997 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/ngram-jaccard_02.aql
@@ -58,8 +58,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-jaccard_01.adm";
 
-for $a in dataset('CSX')
 for $b in dataset('DBLP')
+for $a in dataset('CSX')
 where similarity-jaccard(gram-tokens($a.nested.title, 3, false), gram-tokens($b.nested.title, 3, false)) >= 0.5f
       and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-fuzzyeq-jaccard_01.aql
index a52deb4..37ee1b4 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-fuzzyeq-jaccard_01.aql
@@ -60,7 +60,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where word-tokens($a.nested.title) ~= word-tokens($b.nested.title) and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-fuzzyeq-jaccard_02.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-fuzzyeq-jaccard_02.aql
index 7d07d45..7939ca2 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-fuzzyeq-jaccard_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-fuzzyeq-jaccard_02.aql
@@ -60,7 +60,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('CSX')
 for $b in dataset('DBLP')
+for $a in dataset('CSX')
 where word-tokens($a.nested.title) ~= word-tokens($b.nested.title) and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard-check_01.aql
index ab039ed..8bca7b3 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard-check_01.aql
@@ -57,8 +57,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_word-jaccard-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard-check(word-tokens($a.nested.title), word-tokens($b.nested.title), 0.5f)[0]
       and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard-check_02.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard-check_02.aql
index 401716e..803d4db 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard-check_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard-check_02.aql
@@ -57,8 +57,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_word-jaccard-check_01.adm";
 
-for $a in dataset('CSX')
 for $b in dataset('DBLP')
+for $a in dataset('CSX')
 where similarity-jaccard-check(word-tokens($a.nested.title), word-tokens($b.nested.title), 0.5f)[0]
       and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard_01.aql
index 063520a..aeef474 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard_01.aql
@@ -57,8 +57,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_word-jaccard_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard(word-tokens($a.nested.title), word-tokens($b.nested.title)) >= 0.5f
       and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard_02.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard_02.aql
index 4230533..15a88ca 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/inverted-index-join/word-jaccard_02.aql
@@ -57,8 +57,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_word-jaccard_01.adm";
 
-for $a in dataset('CSX')
 for $b in dataset('DBLP')
+for $a in dataset('CSX')
 where similarity-jaccard(word-tokens($a.nested.title), word-tokens($b.nested.title)) >= 0.5f
       and $a.nested.id < $b.nested.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/rtree-index-join/spatial-intersect-point_01.aql b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/rtree-index-join/spatial-intersect-point_01.aql
index c2a8711..b5f36e7 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/rtree-index-join/spatial-intersect-point_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/nested-open-index/rtree-index-join/spatial-intersect-point_01.aql
@@ -49,7 +49,7 @@
 
 write output to asterix_nc1:"rttest/index-join_rtree-spatial-intersect-point.adm";
 
-for $a in dataset('MyData1')
 for $b in dataset('MyData2')
+for $a in dataset('MyData1')
 where spatial-intersect($a.nested.point, $b.point)
 return {"a": $a, "b": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/btree-index-join/secondary-equi-join-multipred.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/btree-index-join/secondary-equi-join-multipred.aql
index e16bf87..4cad8e3 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/btree-index-join/secondary-equi-join-multipred.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/btree-index-join/secondary-equi-join-multipred.aql
@@ -52,7 +52,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_title-secondary-equi-join-multipred.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.title /*+ indexnl */ = $b.title and $a.authors < $b.authors and $a.misc > $b.misc
 return {"arec": $a, "brec": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/btree-index-join/secondary-equi-join_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/btree-index-join/secondary-equi-join_01.aql
index cd801cf..b9a9f59 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/btree-index-join/secondary-equi-join_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/btree-index-join/secondary-equi-join_01.aql
@@ -50,7 +50,7 @@
 
 write output to asterix_nc1:"rttest/btree-index-join_title-secondary-equi-join_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.title /*+ indexnl */ = $b.title
 return {"arec": $a, "brec": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-contains_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-contains_01.aql
index 7d63fd1..30b7078 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-contains_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-contains_01.aql
@@ -48,8 +48,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-contains-01.adm";
 
-for $o1 in dataset('DBLP')
 for $o2 in dataset('CSX')
+for $o1 in dataset('DBLP')
 where contains($o1.title, $o2.title) and $o1.id < $o2.id
 order by $o1.id, $o2.id
 return {"title1":$o1.title, "title2":$o2.title}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance-check_01.aql
index 533e697..a2a4ba3 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance-check_01.aql
@@ -49,7 +49,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance-check($a.authors, $b.authors, 3)[0] and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance-contains.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance-contains.aql
index 05f90a2..466fb71 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance-contains.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance-contains.aql
@@ -49,7 +49,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance-contains.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance-contains($a.authors, $b.authors, 3)[0] and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance_01.aql
index 104c7a3..d3ad9e2 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-edit-distance_01.aql
@@ -49,7 +49,7 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-edit-distance_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where edit-distance($a.authors, $b.authors) < 3 and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql
index b29f8c9..afa9a18 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql
@@ -52,7 +52,7 @@
 set simfunction 'edit-distance';
 set simthreshold '3';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where $a.authors ~= $b.authors and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql
index fe68a2a..c7e57af 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql
@@ -53,7 +53,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where gram-tokens($a.title, 3, false) ~= gram-tokens($b.title, 3, false) and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-jaccard-check_01.aql
index d5e033a..06d1a7b 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-jaccard-check_01.aql
@@ -50,8 +50,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-jaccard-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard-check(gram-tokens($a.title, 3, false), gram-tokens($b.title, 3, false), 0.5f)[0]
       and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-jaccard_01.aql
index 5877397..07773fa 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/ngram-jaccard_01.aql
@@ -50,8 +50,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_ngram-jaccard_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard(gram-tokens($a.title, 3, false), gram-tokens($b.title, 3, false)) >= 0.5f
       and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-fuzzyeq-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-fuzzyeq-jaccard_01.aql
index 2f46d9c..dd19d4e 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-fuzzyeq-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-fuzzyeq-jaccard_01.aql
@@ -52,7 +52,7 @@
 set simfunction 'jaccard';
 set simthreshold '0.5f';
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where word-tokens($a.title) ~= word-tokens($b.title) and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-jaccard-check_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-jaccard-check_01.aql
index 8857de5..c2f5903 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-jaccard-check_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-jaccard-check_01.aql
@@ -49,8 +49,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_word-jaccard-check_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard-check(word-tokens($a.title), word-tokens($b.title), 0.5f)[0]
       and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-jaccard_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-jaccard_01.aql
index e7fb8d5..6b449b1 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-jaccard_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/inverted-index-join/word-jaccard_01.aql
@@ -49,8 +49,8 @@
 
 write output to asterix_nc1:"rttest/inverted-index-join_word-jaccard_01.adm";
 
-for $a in dataset('DBLP')
 for $b in dataset('CSX')
+for $a in dataset('DBLP')
 where similarity-jaccard(word-tokens($a.title), word-tokens($b.title)) >= 0.5f
       and $a.id < $b.id
 return {"arec": $a, "brec": $b }
diff --git a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/rtree-index-join/spatial-intersect-point_01.aql b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/rtree-index-join/spatial-intersect-point_01.aql
index 1b3bb3e..fd2fce1 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/rtree-index-join/spatial-intersect-point_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/open-index-enforced/rtree-index-join/spatial-intersect-point_01.aql
@@ -55,7 +55,7 @@
 
 write output to asterix_nc1:"rttest/index-join_rtree-spatial-intersect-point.adm";
 
-for $a in dataset('MyData1')
 for $b in dataset('MyData2')
+for $a in dataset('MyData1')
 where spatial-intersect($a.point, $b.point)
 return {"a": $a, "b": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/rtree-index-join/spatial-intersect-point_01.aql b/asterix-app/src/test/resources/optimizerts/queries/rtree-index-join/spatial-intersect-point_01.aql
index 58c1acc..4a82561 100644
--- a/asterix-app/src/test/resources/optimizerts/queries/rtree-index-join/spatial-intersect-point_01.aql
+++ b/asterix-app/src/test/resources/optimizerts/queries/rtree-index-join/spatial-intersect-point_01.aql
@@ -18,7 +18,7 @@
  */
 /*
  * Description    : Joins two datasets on the intersection of their point attributes.
- *                  The dataset 'MyData1' has an RTree index, and we expect the 
+ *                  The dataset 'MyData1' has an RTree index, and we expect the
  *                  join to be transformed into an indexed nested-loop join.
  * Success        : Yes
  */
@@ -45,7 +45,7 @@
 
 write output to asterix_nc1:"rttest/index-join_rtree-spatial-intersect-point.adm";
 
-for $a in dataset('MyData1')
 for $b in dataset('MyData2')
+for $a in dataset('MyData1')
 where spatial-intersect($a.point, $b.point)
 return {"a": $a, "b": $b}
diff --git a/asterix-app/src/test/resources/optimizerts/queries/rtree-index-join/spatial-self-intersect-point.aql b/asterix-app/src/test/resources/optimizerts/queries/rtree-index-join/spatial-self-intersect-point.aql
new file mode 100644
index 0000000..816bfe6
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/rtree-index-join/spatial-self-intersect-point.aql
@@ -0,0 +1,66 @@
+/*
+ * 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    : Self-joins on a dataset on the intersection of a point attribute.
+ *                  The dataset 'TweetMessages' has an RTree index, and we expect the
+ *                  join to be transformed into an indexed nested-loop join.
+ * Success        : Yes
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type TwitterUserType as closed {
+    screen-name: string,
+    lang: string,
+    friends-count: int64,
+    statuses-count: int64,
+    name: string,
+    followers-count: int64
+}
+
+create type TweetMessageType as closed {
+    tweetid: int64,
+        user: TwitterUserType,
+        sender-location: point,
+    send-time: datetime,
+        referred-topics: {{ string }},
+    message-text: string,
+    countA: int64,
+    countB: int64
+}
+
+create dataset TweetMessages(TweetMessageType)
+primary key tweetid;
+
+create index twmSndLocIx on TweetMessages(sender-location) type rtree;
+create index msgCountAIx on TweetMessages(countA) type btree;
+create index msgCountBIx on TweetMessages(countB) type btree;
+create index msgTextIx on TweetMessages(message-text) type keyword;
+
+write output to asterix_nc1:"rttest/index-join_rtree-spatial-self-intersect-point.adm";
+
+for $t1 in dataset('TweetMessages')
+for $t2 in dataset('TweetMessages')
+let $n :=  create-circle($t1.sender-location, 0.5)
+where spatial-intersect($t2.sender-location, $n)
+order by $t2.tweetid
+return {"tweetid2":$t2.tweetid, "loc2":$t2.sender-location};
+