You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by wa...@apache.org on 2016/02/06 02:25:23 UTC

[6/6] incubator-asterixdb git commit: Fixed ASTERIXDB-1249 and ASTERIXDB-1250: self index-nested-loop join correctly identifies the outer and the inner branch.

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 <bu...@gmail.com>
Tested-by: Jenkins <je...@fulliautomatix.ics.uci.edu>


Project: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/commit/d185e8ea
Tree: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/tree/d185e8ea
Diff: http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/diff/d185e8ea

Branch: refs/heads/master
Commit: d185e8ea5755e239cf7a7330934ea42ae82556d8
Parents: fa8a7ad
Author: Taewoo Kim <wa...@yahoo.com>
Authored: Tue Feb 2 21:17:00 2016 -0800
Committer: Taewoo Kim <wa...@gmail.com>
Committed: Fri Feb 5 16:26:58 2016 -0800

----------------------------------------------------------------------
 .../am/AbstractIntroduceAccessMethodRule.java   |  26 +-
 .../optimizer/rules/am/BTreeAccessMethod.java   |  14 +-
 .../rules/am/IntroduceJoinAccessMethodRule.java | 103 +++---
 .../rules/am/InvertedIndexAccessMethod.java     |  15 +-
 .../optimizer/rules/am/RTreeAccessMethod.java   |  15 +-
 .../primary-equi-join-multipred.aql             |  10 +-
 .../btree-index-join/primary-equi-join_01.aql   |   2 +-
 .../btree-index-join/primary-equi-join_02.aql   |   4 +-
 .../btree-index-join/primary-equi-join_03.aql   |   8 +-
 .../btree-index-join/primary-ge-join_01.aql     |   6 +-
 .../btree-index-join/primary-gt-join_01.aql     |   6 +-
 .../btree-index-join/primary-le-join_01.aql     |   6 +-
 .../btree-index-join/primary-lt-join_01.aql     |   6 +-
 .../secondary-equi-join-multipred.aql           |  10 +-
 .../btree-index-join/secondary-equi-join_01.aql |   8 +-
 .../secondary-self-equi-join.aql                |  65 ++++
 .../ngram-edit-distance.aql                     |   8 +-
 .../ngram-fuzzyeq-jaccard.aql                   |   6 +-
 .../ngram-jaccard.aql                           |   6 +-
 .../olist-edit-distance.aql                     |   8 +-
 .../olist-fuzzyeq-edit-distance.aql             |   8 +-
 .../olist-fuzzyeq-jaccard.aql                   |   8 +-
 .../olist-jaccard.aql                           |   8 +-
 .../ulist-fuzzyeq-jaccard.aql                   |   8 +-
 .../ulist-jaccard.aql                           |   8 +-
 .../word-fuzzyeq-jaccard.aql                    |   6 +-
 .../word-jaccard.aql                            |   6 +-
 .../ngram-edit-distance-check_01.aql            |   6 +-
 .../ngram-edit-distance-check_03.aql            |   2 +-
 .../ngram-edit-distance-contains.aql            |   2 +-
 .../ngram-edit-distance_01.aql                  |   6 +-
 .../ngram-fuzzyeq-edit-distance_02.aql          |   6 +-
 .../ngram-fuzzyeq-jaccard_01.aql                |   6 +-
 .../ngram-jaccard-check_01.aql                  |   6 +-
 .../inverted-index-join/ngram-jaccard_01.aql    |   6 +-
 .../olist-edit-distance-check_01.aql            |   8 +-
 .../olist-edit-distance_01.aql                  |   8 +-
 .../olist-fuzzyeq-edit-distance_01.aql          |   8 +-
 .../olist-fuzzyeq-jaccard_01.aql                |   8 +-
 .../olist-jaccard-check_01.aql                  |   8 +-
 .../inverted-index-join/olist-jaccard_01.aql    |   8 +-
 .../ulist-fuzzyeq-jaccard_01.aql                |   8 +-
 .../ulist-jaccard-check_01.aql                  |   8 +-
 .../inverted-index-join/ulist-jaccard_01.aql    |   8 +-
 .../word-fuzzyeq-jaccard_01.aql                 |   6 +-
 .../word-jaccard-check_01.aql                   |   6 +-
 .../inverted-index-join/word-jaccard_01.aql     |   6 +-
 .../primary-equi-join-multipred.aql             |   2 +-
 .../btree-index-join/primary-equi-join_01.aql   |   2 +-
 .../btree-index-join/primary-equi-join_03.aql   |   2 +-
 .../btree-index-join/primary-ge-join_01.aql     |   2 +-
 .../btree-index-join/primary-gt-join_01.aql     |   2 +-
 .../btree-index-join/primary-le-join_01.aql     |   2 +-
 .../btree-index-join/primary-lt-join_01.aql     |   2 +-
 .../secondary-equi-join-multipred.aql           |   2 +-
 .../btree-index-join/secondary-equi-join_01.aql |   2 +-
 .../ngram-edit-distance-check_01.aql            |   2 +-
 .../ngram-edit-distance-contains.aql            |   2 +-
 .../ngram-edit-distance_01.aql                  |   2 +-
 .../olist-edit-distance-check_01.aql            |   4 +-
 .../olist-edit-distance_01.aql                  |   4 +-
 .../olist-fuzzyeq-edit-distance_01.aql          |   4 +-
 .../olist-fuzzyeq-jaccard_01.aql                |   4 +-
 .../olist-jaccard-check_01.aql                  |   4 +-
 .../inverted-index-join/olist-jaccard_01.aql    |   4 +-
 .../ulist-fuzzyeq-jaccard_01.aql                |   4 +-
 .../ulist-jaccard-check_01.aql                  |   4 +-
 .../inverted-index-join/ulist-jaccard_01.aql    |   4 +-
 .../word-fuzzyeq-jaccard_01.aql                 |   2 +-
 .../word-jaccard-check_01.aql                   |   2 +-
 .../inverted-index-join/word-jaccard_01.aql     |   2 +-
 .../spatial-intersect-point_01.aql              |   2 +-
 .../secondary-equi-join-multipred.aql           |   2 +-
 .../btree-index-join/secondary-equi-join_01.aql |   2 +-
 .../inverted-index-join/ngram-contains_01.aql   |   2 +-
 .../inverted-index-join/ngram-contains_02.aql   |   2 +-
 .../ngram-edit-distance-check_01.aql            |   2 +-
 .../ngram-edit-distance-check_02.aql            |   2 +-
 .../ngram-edit-distance-contains.aql            |   2 +-
 .../ngram-edit-distance_01.aql                  |   2 +-
 .../ngram-edit-distance_02.aql                  |   2 +-
 .../ngram-fuzzyeq-edit-distance_01.aql          |   2 +-
 .../ngram-fuzzyeq-edit-distance_02.aql          |   2 +-
 .../ngram-fuzzyeq-jaccard_01.aql                |   2 +-
 .../ngram-fuzzyeq-jaccard_02.aql                |   2 +-
 .../ngram-jaccard-check_01.aql                  |   2 +-
 .../ngram-jaccard-check_02.aql                  |   2 +-
 .../inverted-index-join/ngram-jaccard_01.aql    |   2 +-
 .../inverted-index-join/ngram-jaccard_02.aql    |   2 +-
 .../word-fuzzyeq-jaccard_01.aql                 |   2 +-
 .../word-fuzzyeq-jaccard_02.aql                 |   2 +-
 .../word-jaccard-check_01.aql                   |   2 +-
 .../word-jaccard-check_02.aql                   |   2 +-
 .../inverted-index-join/word-jaccard_01.aql     |   2 +-
 .../inverted-index-join/word-jaccard_02.aql     |   2 +-
 .../spatial-intersect-point_01.aql              |   2 +-
 .../secondary-equi-join-multipred.aql           |   2 +-
 .../btree-index-join/secondary-equi-join_01.aql |   2 +-
 .../inverted-index-join/ngram-contains_01.aql   |   2 +-
 .../ngram-edit-distance-check_01.aql            |   2 +-
 .../ngram-edit-distance-contains.aql            |   2 +-
 .../ngram-edit-distance_01.aql                  |   2 +-
 .../ngram-fuzzyeq-edit-distance_02.aql          |   2 +-
 .../ngram-fuzzyeq-jaccard_01.aql                |   2 +-
 .../ngram-jaccard-check_01.aql                  |   2 +-
 .../inverted-index-join/ngram-jaccard_01.aql    |   2 +-
 .../word-fuzzyeq-jaccard_01.aql                 |   2 +-
 .../word-jaccard-check_01.aql                   |   2 +-
 .../inverted-index-join/word-jaccard_01.aql     |   2 +-
 .../spatial-intersect-point_01.aql              |   2 +-
 .../spatial-intersect-point_01.aql              |   4 +-
 .../spatial-self-intersect-point.aql            |  66 ++++
 .../primary-composite-key-prefix-join_03.plan   |   4 +-
 .../primary-composite-key-prefix-join_04.plan   |   2 +-
 .../primary-composite-key-prefix-join_05.plan   |   2 +-
 .../primary-composite-key-prefix-join_06.plan   |   2 +-
 .../secondary-self-equi-join.plan               |  26 ++
 .../ngram-fuzzyeq-jaccard.plan                  |   4 +-
 .../ngram-jaccard-inline.plan                   |   4 +-
 .../ngram-jaccard.plan                          |   4 +-
 .../word-fuzzyeq-jaccard.plan                   |   4 +-
 .../word-jaccard-inline.plan                    |   4 +-
 .../word-jaccard.plan                           |   4 +-
 .../ngram-edit-distance-check_01.plan           |   4 +-
 .../ngram-edit-distance-check_03.plan           |   4 +-
 .../ngram-edit-distance-check_04.plan           |   4 +-
 .../ngram-edit-distance-contains.plan           |   4 +-
 .../ngram-edit-distance_01.plan                 |   4 +-
 .../ngram-edit-distance_03.plan                 |   4 +-
 .../ngram-edit-distance_04.plan                 |   4 +-
 .../ngram-fuzzyeq-edit-distance_02.plan         |   4 +-
 .../ngram-fuzzyeq-edit-distance_03.plan         |   4 +-
 .../ngram-fuzzyeq-jaccard_01.plan               |   4 +-
 .../ngram-fuzzyeq-jaccard_03.plan               |   4 +-
 .../ngram-jaccard-check_01.plan                 |   4 +-
 .../ngram-jaccard-check_03.plan                 |   4 +-
 .../ngram-jaccard-check_04.plan                 |   4 +-
 .../inverted-index-join/ngram-jaccard_01.plan   |   4 +-
 .../inverted-index-join/ngram-jaccard_03.plan   |   4 +-
 .../inverted-index-join/ngram-jaccard_04.plan   |   4 +-
 .../olist-edit-distance-check_01.plan           |   4 +-
 .../olist-edit-distance-check_03.plan           |   4 +-
 .../olist-edit-distance-check_04.plan           |   4 +-
 .../olist-edit-distance_01.plan                 |   4 +-
 .../olist-edit-distance_03.plan                 |   4 +-
 .../olist-edit-distance_04.plan                 |   4 +-
 .../olist-fuzzyeq-edit-distance_01.plan         |   4 +-
 .../olist-fuzzyeq-edit-distance_03.plan         |   4 +-
 .../olist-fuzzyeq-jaccard_01.plan               |   4 +-
 .../olist-fuzzyeq-jaccard_03.plan               |   4 +-
 .../olist-jaccard-check_01.plan                 |   4 +-
 .../olist-jaccard-check_03.plan                 |   4 +-
 .../olist-jaccard-check_04.plan                 |   4 +-
 .../inverted-index-join/olist-jaccard_01.plan   |   4 +-
 .../inverted-index-join/olist-jaccard_03.plan   |   4 +-
 .../inverted-index-join/olist-jaccard_04.plan   |   4 +-
 .../ulist-fuzzyeq-jaccard_01.plan               |   4 +-
 .../ulist-fuzzyeq-jaccard_03.plan               |   4 +-
 .../ulist-jaccard-check_01.plan                 |   4 +-
 .../ulist-jaccard-check_03.plan                 |   4 +-
 .../ulist-jaccard-check_04.plan                 |   4 +-
 .../inverted-index-join/ulist-jaccard_01.plan   |   4 +-
 .../inverted-index-join/ulist-jaccard_03.plan   |   4 +-
 .../inverted-index-join/ulist-jaccard_04.plan   |   4 +-
 .../word-fuzzyeq-jaccard_01.plan                |   4 +-
 .../word-fuzzyeq-jaccard_03.plan                |   4 +-
 .../word-jaccard-check_01.plan                  |   4 +-
 .../word-jaccard-check_03.plan                  |   4 +-
 .../word-jaccard-check_04.plan                  |   4 +-
 .../inverted-index-join/word-jaccard_01.plan    |   4 +-
 .../inverted-index-join/word-jaccard_03.plan    |   4 +-
 .../inverted-index-join/word-jaccard_04.plan    |   4 +-
 .../primary-composite-key-join_03.plan          |   4 +-
 .../primary-composite-key-prefix-join_03.plan   |   4 +-
 .../primary-composite-key-prefix-join_04.plan   |   4 +-
 .../primary-composite-key-prefix-join_05.plan   |   4 +-
 .../primary-composite-key-prefix-join_06.plan   |   4 +-
 .../btree-index-join/primary-equi-join_05.plan  |   4 +-
 .../ngram-edit-distance-check_01.plan           |   4 +-
 .../ngram-edit-distance-contains.plan           |   4 +-
 .../ngram-edit-distance_01.plan                 |   4 +-
 .../ngram-jaccard-inline.plan                   |   4 +-
 .../olist-edit-distance-check_01.plan           |   4 +-
 .../olist-edit-distance_01.plan                 |   4 +-
 .../olist-fuzzyeq-edit-distance_01.plan         |   4 +-
 .../olist-fuzzyeq-jaccard_01.plan               |   4 +-
 .../olist-jaccard-check_01.plan                 |   4 +-
 .../inverted-index-join/olist-jaccard_01.plan   |   4 +-
 .../ulist-fuzzyeq-jaccard_01.plan               |   4 +-
 .../ulist-jaccard-check_01.plan                 |   4 +-
 .../inverted-index-join/ulist-jaccard_01.plan   |   4 +-
 .../word-fuzzyeq-jaccard_01.plan                |   4 +-
 .../word-jaccard-check_01.plan                  |   4 +-
 .../word-jaccard-inline.plan                    |   4 +-
 .../inverted-index-join/word-jaccard_01.plan    |   4 +-
 .../inverted-index-join/ngram-contains_01.plan  |   4 +-
 .../inverted-index-join/ngram-contains_02.plan  |   4 +-
 .../ngram-edit-distance-check_01.plan           |   4 +-
 .../ngram-edit-distance-check_02.plan           |   4 +-
 .../ngram-edit-distance-check_03.plan           |   4 +-
 .../ngram-edit-distance-contains.plan           |   4 +-
 .../ngram-edit-distance_01.plan                 |   4 +-
 .../ngram-edit-distance_02.plan                 |   4 +-
 .../ngram-edit-distance_03.plan                 |   4 +-
 .../ngram-fuzzyeq-edit-distance_01.plan         |   4 +-
 .../ngram-fuzzyeq-edit-distance_02.plan         |   4 +-
 .../ngram-fuzzyeq-edit-distance_03.plan         |   4 +-
 .../ngram-fuzzyeq-jaccard_01.plan               |   4 +-
 .../ngram-fuzzyeq-jaccard_02.plan               |   4 +-
 .../ngram-fuzzyeq-jaccard_03.plan               |   4 +-
 .../ngram-jaccard-check_01.plan                 |   4 +-
 .../ngram-jaccard-check_02.plan                 |   4 +-
 .../ngram-jaccard-check_03.plan                 |   4 +-
 .../ngram-jaccard-inline.plan                   |   4 +-
 .../inverted-index-join/ngram-jaccard_01.plan   |   4 +-
 .../inverted-index-join/ngram-jaccard_02.plan   |   4 +-
 .../inverted-index-join/ngram-jaccard_03.plan   |   4 +-
 .../word-fuzzyeq-jaccard_01.plan                |   4 +-
 .../word-fuzzyeq-jaccard_02.plan                |   4 +-
 .../word-fuzzyeq-jaccard_03.plan                |   4 +-
 .../word-jaccard-check_01.plan                  |   4 +-
 .../word-jaccard-check_02.plan                  |   4 +-
 .../word-jaccard-check_03.plan                  |   4 +-
 .../word-jaccard-inline.plan                    |   4 +-
 .../inverted-index-join/word-jaccard_01.plan    |   4 +-
 .../inverted-index-join/word-jaccard_02.plan    |   4 +-
 .../inverted-index-join/word-jaccard_03.plan    |   4 +-
 .../inverted-index-join/ngram-contains_01.plan  |   4 +-
 .../ngram-edit-distance-check_01.plan           |   4 +-
 .../ngram-edit-distance-check_03.plan           |   4 +-
 .../ngram-edit-distance-check_inline_03.plan    |   4 +-
 .../ngram-edit-distance-contains.plan           |   4 +-
 .../ngram-edit-distance_01.plan                 |   4 +-
 .../ngram-edit-distance_03.plan                 |   4 +-
 .../ngram-edit-distance_inline_03.plan          |   4 +-
 .../ngram-fuzzyeq-edit-distance_02.plan         |   4 +-
 .../ngram-fuzzyeq-edit-distance_03.plan         |   4 +-
 .../ngram-fuzzyeq-jaccard_01.plan               |   4 +-
 .../ngram-fuzzyeq-jaccard_03.plan               |   4 +-
 .../ngram-jaccard-check_01.plan                 |   4 +-
 .../ngram-jaccard-check_03.plan                 |   4 +-
 .../ngram-jaccard-check_inline_03.plan          |   4 +-
 .../inverted-index-join/ngram-jaccard_01.plan   |   4 +-
 .../inverted-index-join/ngram-jaccard_03.plan   |   4 +-
 .../ngram-jaccard_inline_03.plan                |   4 +-
 .../word-fuzzyeq-jaccard_01.plan                |   4 +-
 .../word-fuzzyeq-jaccard_03.plan                |   4 +-
 .../word-jaccard-check_01.plan                  |   4 +-
 .../word-jaccard-check_03.plan                  |   4 +-
 .../word-jaccard-check_inline_03.plan           |   4 +-
 .../inverted-index-join/word-jaccard_01.plan    |   4 +-
 .../inverted-index-join/word-jaccard_03.plan    |   4 +-
 .../word-jaccard_inline_03.plan                 |   4 +-
 .../spatial-self-intersect-point.plan           |  27 ++
 .../btree-secondary-self-equi-join_01.1.ddl.aql |  57 ++++
 ...ree-secondary-self-equi-join_01.2.update.aql |  31 ++
 ...tree-secondary-self-equi-join_01.3.query.aql |  34 ++
 ...rtree-spatial-self-intersect-point.1.ddl.aql |  58 ++++
 ...ee-spatial-self-intersect-point.2.update.aql |  31 ++
 ...ree-spatial-self-intersect-point.3.query.aql |  34 ++
 .../ngram-jaccard-inline.4.query.aql            |  12 +-
 .../ngram-jaccard/ngram-jaccard.4.query.aql     |  12 +-
 .../ngram-jaccard-inline.4.query.aql            |  12 +-
 .../ngram-jaccard/ngram-jaccard.4.query.aql     |  12 +-
 .../ngram-jaccard-inline.4.query.sqlpp          |   3 +-
 .../ngram-jaccard/ngram-jaccard.4.query.sqlpp   |   3 +-
 .../ngram-jaccard-inline.4.query.sqlpp          |   3 +-
 .../ngram-jaccard/ngram-jaccard.4.query.sqlpp   |   3 +-
 .../btree-secondary-self-equi-join_01.1.adm     | 228 +++++++++++++
 .../rtree-spatial-self-intersect-point.1.adm    | 316 +++++++++++++++++++
 .../ngram-jaccard-inline.4.ast                  |   8 +-
 .../ngram-jaccard/ngram-jaccard.4.ast           |   8 +-
 .../ngram-jaccard-inline.4.ast                  |   8 +-
 .../ngram-jaccard/ngram-jaccard.4.ast           |   8 +-
 .../src/test/resources/runtimets/testsuite.xml  |  10 +
 275 files changed, 1603 insertions(+), 623 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
----------------------------------------------------------------------
diff --git a/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java b/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
index 0c2463c..7a36db8 100644
--- a/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
+++ b/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
@@ -25,7 +25,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.asterix.common.config.MetadataConstants;
 import org.apache.asterix.common.config.DatasetConfig.IndexType;
 import org.apache.asterix.dataflow.data.common.AqlExpressionTypeComputer;
 import org.apache.asterix.metadata.api.IMetadataEntity;
@@ -63,6 +62,8 @@ import org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperat
 import org.apache.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator;
 import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
 
+import com.google.common.collect.ImmutableSet;
+
 /**
  * Class that embodies the commonalities between rewrite rules for access
  * methods.
@@ -71,6 +72,17 @@ public abstract class AbstractIntroduceAccessMethodRule implements IAlgebraicRew
 
     private AqlMetadataProvider metadataProvider;
 
+    // Function Identifier sets that retain the original field variable through each function's arguments
+    private final ImmutableSet<FunctionIdentifier> funcIDSetThatRetainFieldName = ImmutableSet.of(
+            AsterixBuiltinFunctions.WORD_TOKENS, AsterixBuiltinFunctions.GRAM_TOKENS, AsterixBuiltinFunctions.SUBSTRING,
+            AsterixBuiltinFunctions.SUBSTRING_BEFORE, AsterixBuiltinFunctions.SUBSTRING_AFTER,
+            AsterixBuiltinFunctions.CREATE_POLYGON, AsterixBuiltinFunctions.CREATE_MBR,
+            AsterixBuiltinFunctions.CREATE_RECTANGLE, AsterixBuiltinFunctions.CREATE_CIRCLE,
+            AsterixBuiltinFunctions.CREATE_LINE, AsterixBuiltinFunctions.CREATE_POINT,
+            AsterixBuiltinFunctions.NUMERIC_ADD, AsterixBuiltinFunctions.NUMERIC_SUBTRACT,
+            AsterixBuiltinFunctions.NUMERIC_MULTIPLY, AsterixBuiltinFunctions.NUMERIC_DIVIDE,
+            AsterixBuiltinFunctions.NUMERIC_MOD);
+
     public abstract Map<FunctionIdentifier, List<IAccessMethod>> getAccessMethods();
 
     protected static void registerAccessMethod(IAccessMethod accessMethod,
@@ -187,6 +199,7 @@ public abstract class AbstractIntroduceAccessMethodRule implements IAlgebraicRew
         while (indexExprAndVarIt.hasNext()) {
             Map.Entry<Index, List<Pair<Integer, Integer>>> indexExprAndVarEntry = indexExprAndVarIt.next();
             Index index = indexExprAndVarEntry.getKey();
+
             boolean allUsed = true;
             int lastFieldMatched = -1;
             boolean foundKeyField = false;
@@ -739,16 +752,7 @@ public abstract class AbstractIntroduceAccessMethodRule implements IAlgebraicRew
 
         }
 
-        if (funcIdent != AsterixBuiltinFunctions.WORD_TOKENS && funcIdent != AsterixBuiltinFunctions.GRAM_TOKENS
-                && funcIdent != AsterixBuiltinFunctions.SUBSTRING
-                && funcIdent != AsterixBuiltinFunctions.SUBSTRING_BEFORE
-                && funcIdent != AsterixBuiltinFunctions.SUBSTRING_AFTER
-                && funcIdent != AsterixBuiltinFunctions.CREATE_POLYGON
-                && funcIdent != AsterixBuiltinFunctions.CREATE_MBR
-                && funcIdent != AsterixBuiltinFunctions.CREATE_RECTANGLE
-                && funcIdent != AsterixBuiltinFunctions.CREATE_CIRCLE
-                && funcIdent != AsterixBuiltinFunctions.CREATE_LINE
-                && funcIdent != AsterixBuiltinFunctions.CREATE_POINT) {
+        if (!funcIDSetThatRetainFieldName.contains(funcIdent)) {
             return null;
         }
         // We use a part of the field in edit distance computation

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/BTreeAccessMethod.java
----------------------------------------------------------------------
diff --git a/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/BTreeAccessMethod.java b/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/BTreeAccessMethod.java
index 9f95c85..dd6784e 100644
--- a/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/BTreeAccessMethod.java
+++ b/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/BTreeAccessMethod.java
@@ -164,20 +164,16 @@ public class BTreeAccessMethod implements IAccessMethod {
         Mutable<ILogicalExpression> conditionRef = joinOp.getCondition();
         // Determine if the index is applicable on the left or right side (if both, we arbitrarily prefer the left side).
         Dataset dataset = analysisCtx.indexDatasetMap.get(chosenIndex);
-        // Determine probe and index subtrees based on chosen index.
         OptimizableOperatorSubTree indexSubTree = null;
         OptimizableOperatorSubTree probeSubTree = null;
-        if (!isLeftOuterJoin && leftSubTree.hasDataSourceScan()
-                && dataset.getDatasetName().equals(leftSubTree.dataset.getDatasetName())) {
-            indexSubTree = leftSubTree;
-            probeSubTree = rightSubTree;
-        } else if (rightSubTree.hasDataSourceScan()
+        // We assume that the left subtree is the outer branch and the right subtree is the inner branch.
+        // This assumption holds true since we only use an index from the right subtree.
+        // The following is just a sanity check.
+        if (rightSubTree.hasDataSourceScan()
                 && dataset.getDatasetName().equals(rightSubTree.dataset.getDatasetName())) {
             indexSubTree = rightSubTree;
             probeSubTree = leftSubTree;
-        }
-        if (indexSubTree == null) {
-            //This may happen for left outer join case
+        } else {
             return false;
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceJoinAccessMethodRule.java
----------------------------------------------------------------------
diff --git a/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceJoinAccessMethodRule.java b/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceJoinAccessMethodRule.java
index 44314ff..13f7119 100644
--- a/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceJoinAccessMethodRule.java
+++ b/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceJoinAccessMethodRule.java
@@ -49,9 +49,9 @@ import org.apache.hyracks.algebricks.core.algebra.util.OperatorPropertiesUtil;
  * Matches the following operator pattern:
  * (join) <-- (select)? <-- (assign | unnest)+ <-- (datasource scan)
  * <-- (select)? <-- (assign | unnest)+ <-- (datasource scan | unnest-map)
- * The order of the join inputs matters (left-outer relation, right-inner relation).
- * This rule tries to utilize an index on the inner relation first.
- * If that's not possible, it tries to use an index on the outer relation.
+ * The order of the join inputs matters (left becomes the outer relation and right becomes the inner relation).
+ * This rule tries to utilize an index on the inner relation.
+ * If that's not possible, it stops transforming the given join into an index-nested-loop join.
  * Replaces the above pattern with the following simplified plan:
  * (select) <-- (assign) <-- (btree search) <-- (sort) <-- (unnest(index search)) <-- (assign) <-- (datasource scan | unnest-map)
  * The sort is optional, and some access methods may choose not to sort.
@@ -140,52 +140,11 @@ public class IntroduceJoinAccessMethodRule extends AbstractIntroduceAccessMethod
         }
         pruneIndexCandidates(analyzedAMs, context, typeEnvironment);
 
-        // Prioritize the order of index that will be applied. If the right subtree (inner branch) has indexes,
-        // those indexes will be used.
-        String innerDataset = null;
-        if (rightSubTree.dataset != null) {
-            innerDataset = rightSubTree.dataset.getDatasetName();
-        }
-
-        Iterator<Map.Entry<IAccessMethod, AccessMethodAnalysisContext>> amIt = analyzedAMs.entrySet().iterator();
-        while (amIt.hasNext()) {
-            Map.Entry<IAccessMethod, AccessMethodAnalysisContext> entry = amIt.next();
-            AccessMethodAnalysisContext amCtx = entry.getValue();
-            Iterator<Map.Entry<Index, List<Pair<Integer, Integer>>>> indexIt = amCtx.indexExprsAndVars.entrySet()
-                    .iterator();
-
-            // Check whether we can choose the indexes from the inner relations (removing indexes from the outer relations)
-            int totalIndexCount = 0;
-            int indexCountFromTheOuterBranch = 0;
+        // We only consider indexes from the inner branch (right subTree).
+        // If no index is available, then we stop this optimization.
+        removeIndexCandidatesFromOuterBranch(analyzedAMs);
 
-            while (indexIt.hasNext()) {
-                Map.Entry<Index, List<Pair<Integer, Integer>>> indexEntry = indexIt.next();
-
-                Index chosenIndex = indexEntry.getKey();
-                //Count possible indexes that can be removed from left Tree (outer branch)
-                if (!chosenIndex.getDatasetName().equals(innerDataset)) {
-                    indexCountFromTheOuterBranch++;
-                }
-                totalIndexCount++;
-            }
-
-            if (indexCountFromTheOuterBranch < totalIndexCount) {
-                indexIt = amCtx.indexExprsAndVars.entrySet().iterator();
-                while (indexIt.hasNext()) {
-                    Map.Entry<Index, List<Pair<Integer, Integer>>> indexEntry = indexIt.next();
-
-                    Index chosenIndex = indexEntry.getKey();
-                    //Remove possibly chosen indexes from left Tree (outer branch)
-                    if (!chosenIndex.getDatasetName().equals(innerDataset)) {
-                        indexIt.remove();
-                    }
-                }
-            }
-        }
-
-        // For the case of left-outer-join, we have to use indexes from the inner branch.
-        // For the inner-join, we try to use the indexes from the inner branch first.
-        // If no index is available, then we use the indexes from the outer branch.
+        // Choose an index from the inner branch that will be used.
         Pair<IAccessMethod, Index> chosenIndex = chooseIndex(analyzedAMs);
         if (chosenIndex == null) {
             context.addToDontApplySet(this, join);
@@ -202,6 +161,9 @@ public class IntroduceJoinAccessMethodRule extends AbstractIntroduceAccessMethod
                     .findLOJIsNullFuncInGroupBy((GroupByOperator) opRef.getValue());
             analysisCtx.setLOJIsNullFuncInGroupBy(isNullFuncExpr);
         }
+
+        // At this point, we are sure that only an index from the inner branch is going to be used.
+        // So, the left subtree is the outer branch and the right subtree is the inner branch.
         boolean res = chosenIndex.first.applyJoinPlanTransformation(joinRef, leftSubTree, rightSubTree,
                 chosenIndex.second, analysisCtx, context, isLeftOuterJoin, hasGroupBy);
         if (res) {
@@ -211,6 +173,51 @@ public class IntroduceJoinAccessMethodRule extends AbstractIntroduceAccessMethod
         return res;
     }
 
+    /**
+     * Removes indexes from the optimizer's consideration for this rule.
+     */
+    protected void removeIndexCandidatesFromOuterBranch(Map<IAccessMethod, AccessMethodAnalysisContext> analyzedAMs) {
+        String innerDataset = null;
+        if (rightSubTree.dataset != null) {
+            innerDataset = rightSubTree.dataset.getDatasetName();
+        }
+
+        Iterator<Map.Entry<IAccessMethod, AccessMethodAnalysisContext>> amIt = analyzedAMs.entrySet().iterator();
+        while (amIt.hasNext()) {
+            Map.Entry<IAccessMethod, AccessMethodAnalysisContext> entry = amIt.next();
+            AccessMethodAnalysisContext amCtx = entry.getValue();
+
+            // Fetch index, expression, and variables.
+            Iterator<Map.Entry<Index, List<Pair<Integer, Integer>>>> indexIt = amCtx.indexExprsAndVars.entrySet()
+                    .iterator();
+
+            while (indexIt.hasNext()) {
+                Map.Entry<Index, List<Pair<Integer, Integer>>> indexExprAndVarEntry = indexIt.next();
+                Iterator<Pair<Integer, Integer>> exprsAndVarIter = indexExprAndVarEntry.getValue().iterator();
+                boolean indexFromInnerBranch = false;
+
+                while (exprsAndVarIter.hasNext()) {
+                    Pair<Integer, Integer> exprAndVarIdx = exprsAndVarIter.next();
+                    IOptimizableFuncExpr optFuncExpr = amCtx.matchedFuncExprs.get(exprAndVarIdx.first);
+
+                    // Does this index come from the inner branch?
+                    // We check the dataset name and the subtree to make sure the index is applicable.
+                    if (indexExprAndVarEntry.getKey().getDatasetName().equals(innerDataset)) {
+                        if (optFuncExpr.getOperatorSubTree(exprAndVarIdx.second).equals(rightSubTree)) {
+                            indexFromInnerBranch = true;
+                        }
+                    }
+                }
+
+                // If the index does not come from the inner branch,
+                // We do not consider this index.
+                if (!indexFromInnerBranch) {
+                    indexIt.remove();
+                }
+            }
+        }
+    }
+
     protected boolean matchesOperatorPattern(Mutable<ILogicalOperator> opRef, IOptimizationContext context) {
         // First check that the operator is a join and its condition is a function call.
         AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/InvertedIndexAccessMethod.java
----------------------------------------------------------------------
diff --git a/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/InvertedIndexAccessMethod.java b/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/InvertedIndexAccessMethod.java
index 35b4075..851264c 100644
--- a/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/InvertedIndexAccessMethod.java
+++ b/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/InvertedIndexAccessMethod.java
@@ -443,20 +443,17 @@ public class InvertedIndexAccessMethod implements IAccessMethod {
             boolean hasGroupBy) throws AlgebricksException {
         // Figure out if the index is applicable on the left or right side (if both, we arbitrarily prefer the left side).
         Dataset dataset = analysisCtx.indexDatasetMap.get(chosenIndex);
-        // Determine probe and index subtrees based on chosen index.
         OptimizableOperatorSubTree indexSubTree = null;
         OptimizableOperatorSubTree probeSubTree = null;
-        if (!isLeftOuterJoin && leftSubTree.hasDataSourceScan()
-                && dataset.getDatasetName().equals(leftSubTree.dataset.getDatasetName())) {
-            indexSubTree = leftSubTree;
-            probeSubTree = rightSubTree;
-        } else if (rightSubTree.hasDataSourceScan()
+
+        // We assume that the left subtree is the outer branch and the right subtree is the inner branch.
+        // This assumption holds true since we only use an index from the right subtree.
+        // The following is just a sanity check.
+        if (rightSubTree.hasDataSourceScan()
                 && dataset.getDatasetName().equals(rightSubTree.dataset.getDatasetName())) {
             indexSubTree = rightSubTree;
             probeSubTree = leftSubTree;
-        }
-        if (indexSubTree == null) {
-            //This may happen for left outer join case
+        } else {
             return false;
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/RTreeAccessMethod.java
----------------------------------------------------------------------
diff --git a/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/RTreeAccessMethod.java b/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/RTreeAccessMethod.java
index 4aebbda..461fb2a 100644
--- a/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/RTreeAccessMethod.java
+++ b/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/RTreeAccessMethod.java
@@ -118,20 +118,17 @@ public class RTreeAccessMethod implements IAccessMethod {
             boolean hasGroupBy) throws AlgebricksException {
         // Determine if the index is applicable on the left or right side (if both, we arbitrarily prefer the left side).
         Dataset dataset = analysisCtx.indexDatasetMap.get(chosenIndex);
-        // Determine probe and index subtrees based on chosen index.
         OptimizableOperatorSubTree indexSubTree = null;
         OptimizableOperatorSubTree probeSubTree = null;
-        if (!isLeftOuterJoin && leftSubTree.hasDataSourceScan()
-                && dataset.getDatasetName().equals(leftSubTree.dataset.getDatasetName())) {
-            indexSubTree = leftSubTree;
-            probeSubTree = rightSubTree;
-        } else if (rightSubTree.hasDataSourceScan()
+
+        // We assume that the left subtree is the outer branch and the right subtree is the inner branch.
+        // This assumption holds true since we only use an index from the right subtree.
+        // The following is just a sanity check.
+        if (rightSubTree.hasDataSourceScan()
                 && dataset.getDatasetName().equals(rightSubTree.dataset.getDatasetName())) {
             indexSubTree = rightSubTree;
             probeSubTree = leftSubTree;
-        }
-        if (indexSubTree == null) {
-            //This may happen for left outer join case
+        } else {
             return false;
         }
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join-multipred.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 @@ create dataset Orders(OrderType) primary key oid;
 
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_01.aql
----------------------------------------------------------------------
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 @@ create dataset test1.DsTwo(TestType) primary key key1;
 
 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
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_02.aql
----------------------------------------------------------------------
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
  */

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-equi-join_03.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 @@ create dataset Orders(OrderType) primary key oid;
 
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-ge-join_01.aql
----------------------------------------------------------------------
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 @@ create dataset test1.DsTwo(TestType) primary key key1;
 
 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
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-gt-join_01.aql
----------------------------------------------------------------------
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 @@ create dataset test1.DsTwo(TestType) primary key key1;
 
 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
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-le-join_01.aql
----------------------------------------------------------------------
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 @@ create dataset test1.DsTwo(TestType) primary key key1;
 
 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
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/primary-lt-join_01.aql
----------------------------------------------------------------------
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 @@ create dataset test1.DsTwo(TestType) primary key key1;
 
 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
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-equi-join-multipred.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -38,7 +38,7 @@ create type DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -53,7 +53,7 @@ create index title_index on DBLP(title);
 
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-equi-join_01.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@ create type DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -51,7 +51,7 @@ create index title_index on DBLP(title);
 
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/btree-index-join/secondary-self-equi-join.aql
----------------------------------------------------------------------
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};

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-edit-distance.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@ create type DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -51,7 +51,7 @@ create index ngram_index on DBLP(authors) type ngram(3);
 
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-fuzzyeq-jaccard.aql
----------------------------------------------------------------------
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 @@ use dataverse test;
 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 DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -55,7 +55,7 @@ write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_ngram-fuzzyeq-j
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ngram-jaccard.aql
----------------------------------------------------------------------
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 @@ use dataverse test;
 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 DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -52,8 +52,8 @@ create index ngram_index on DBLP(title) type ngram(3);
 
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-edit-distance.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 type CustomerType as closed {
 }
 
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-fuzzyeq-edit-distance.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 type CustomerType as closed {
 }
 
 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 @@ write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_olist-fuzzyeq-e
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-fuzzyeq-jaccard.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 type CustomerType as closed {
 }
 
 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 @@ write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_olist-fuzzyeq-j
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/olist-jaccard.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 type CustomerType as closed {
 }
 
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ulist-fuzzyeq-jaccard.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 type CustomerType as closed {
 }
 
 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 @@ write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_ulist-fuzzyeq-j
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/ulist-jaccard.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 type CustomerType as closed {
 }
 
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/word-fuzzyeq-jaccard.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@ create type DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -54,7 +54,7 @@ write output to asterix_nc1:"rttest/inverted-index-join-noeqjoin_word-fuzzyeq-ja
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join-noeqjoin/word-jaccard.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -36,7 +36,7 @@ create type DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -51,8 +51,8 @@ create index keyword_index on DBLP(title) type keyword;
 
 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}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-check_01.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -35,7 +35,7 @@ create type DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -50,7 +50,7 @@ create index ngram_index on DBLP(authors) type ngram(3);
 
 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 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-check_03.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance-contains.aql
----------------------------------------------------------------------
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 @@ create index ngram_index on DBLP(authors) type ngram(3);
 
 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 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-edit-distance_01.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -35,7 +35,7 @@ create type DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -50,7 +50,7 @@ create index ngram_index on DBLP(authors) type ngram(3);
 
 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 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-fuzzyeq-edit-distance_02.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 use dataverse test;
 
 create type DBLPType as closed {
-  id: int32, 
+  id: int32,
   dblpid: string,
   title: string,
   authors: string,
@@ -35,7 +35,7 @@ create type DBLPType as closed {
 }
 
 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/inverted-index-join_ngram-fuzzyeq-edit-dista
 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 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-fuzzyeq-jaccard_01.aql
----------------------------------------------------------------------
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 @@ use dataverse test;
 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 DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -54,7 +54,7 @@ write output to asterix_nc1:"rttest/inverted-index-join_ngram-fuzzyeq-jaccard_01
 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 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-jaccard-check_01.aql
----------------------------------------------------------------------
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 @@ use dataverse test;
 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 DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -51,8 +51,8 @@ create index ngram_index on DBLP(title) type ngram(3);
 
 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 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/ngram-jaccard_01.aql
----------------------------------------------------------------------
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 @@ use dataverse test;
 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 DBLPType as closed {
 }
 
 create type CSXType as closed {
-  id: int32, 
+  id: int32,
   csxid: string,
   title: string,
   authors: string,
@@ -51,8 +51,8 @@ create index ngram_index on DBLP(title) type ngram(3);
 
 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 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-edit-distance-check_01.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 type CustomerType as closed {
 }
 
 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 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-edit-distance_01.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 type CustomerType as closed {
 }
 
 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 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-fuzzyeq-edit-distance_01.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 type CustomerType as closed {
 }
 
 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 @@ write output to asterix_nc1:"rttest/inverted-index-join_olist-fuzzyeq-jaccard_01
 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 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/d185e8ea/asterix-app/src/test/resources/optimizerts/queries/inverted-index-join/olist-fuzzyeq-jaccard_01.aql
----------------------------------------------------------------------
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 @@ create dataverse test;
 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 type CustomerType as closed {
 }
 
 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 @@ write output to asterix_nc1:"rttest/inverted-index-join_olist-fuzzyeq-jaccard_01
 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 }