asterixdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjaco...@apache.org
Subject [3/3] asterixdb git commit: Make query plans more user-friendly.
Date Tue, 09 May 2017 21:16:04 GMT
Make query plans more user-friendly.

Change-Id: I91c15913009295352cf7cccf4a4028f1ff271e3f
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1721
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Steven Jacobs <sjaco002@ucr.edu>


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

Branch: refs/heads/master
Commit: 049d54148c8067a9f34e5b6249f61741de4b80f4
Parents: 81a346c
Author: Yingyi Bu <yingyi@couchbase.com>
Authored: Tue May 9 13:02:03 2017 -0700
Committer: Steven Jacobs <sjaco002@ucr.edu>
Committed: Tue May 9 14:15:38 2017 -0700

----------------------------------------------------------------------
 .../optimizer/rules/SimilarityCheckRule.java    |   9 +-
 .../AqlExpressionToPlanTranslator.java          |   4 +-
 .../LangExpressionToPlanTranslator.java         |  16 +-
 .../SqlppExpressionToPlanTranslator.java        |   8 +-
 .../asterix/translator/TranslationContext.java  |  44 ++++--
 .../optimizerts/results/count-tweets.plan       |   2 +-
 .../optimizerts/results/fj-phase1.plan          |  10 +-
 .../optimizerts/results/gby_inline.plan         |   8 +-
 .../results/hashjoin-with-unnest.plan           |   8 +-
 .../inlined_q18_large_volume_customer.plan      |   8 +-
 .../global-datetime-use-index-return-time.plan  |   6 +-
 .../nonpure/global-datetime-use-index.plan      |   6 +-
 .../results/orderby-desc-using-gby.plan         |   2 +-
 .../results/q01_pricing_summary_report_nt.plan  |   4 +-
 .../results/q03_shipping_priority.plan          |   4 +-
 .../results/query-ASTERIXDB-1005.plan           |   2 +-
 .../results/query-ASTERIXDB-1127.plan           |   4 +-
 .../results/query-ASTERIXDB-1168.plan           |   4 +-
 .../results/query-ASTERIXDB-1343-2.plan         |   2 +-
 .../results/query-ASTERIXDB-1343-3.plan         |   2 +-
 .../results/query-ASTERIXDB-1343-4.plan         |   2 +-
 .../results/query-ASTERIXDB-1343.plan           |   2 +-
 .../results/query-ASTERIXDB-1806.plan           |   2 +-
 .../optimizerts/results/query-issue562.plan     |   6 +-
 .../optimizerts/results/query-issue785.plan     |   6 +-
 .../optimizerts/results/query-issue810-2.plan   |   2 +-
 .../optimizerts/results/query-issue810.plan     |   2 +-
 .../split-materialization-above-join.plan       |  42 +++---
 .../optimizerts/results/subquery/exists.plan    |   2 +-
 .../results/subquery/not_exists.plan            |   2 +-
 .../results/subquery/query-ASTERIXDB-1572.plan  |  16 +-
 .../optimizerts/results/tpch/q12_shipping.plan  |   2 +-
 .../results/tpch/q12_shipping_broadcast.plan    |   2 +-
 .../results/udfs/query-ASTERIXDB-1298.plan      |   4 +-
 .../results/udfs/query-ASTERIXDB-1308-1.plan    |   4 +-
 .../results/udfs/query-ASTERIXDB-1308-2.plan    |   4 +-
 .../optimizerts/results/unnest-to-join_01.plan  |   2 +-
 .../optimizerts/results/unnest-to-join_02.plan  |   2 +-
 .../explain_field_access.1.ddl.sqlpp            |  29 ++++
 .../explain_field_access.2.query.sqlpp          |  25 ++++
 .../explain_field_access_closed.1.ddl.sqlpp     |  31 ++++
 .../explain_field_access_closed.2.query.sqlpp   |  25 ++++
 .../explain_object_constructor-01.1.query.sqlpp |  20 +++
 .../explain_object_constructor-02.1.query.sqlpp |  20 +++
 .../explain_object_constructor-03.1.query.sqlpp |  20 +++
 .../explain_field_access.1.adm                  |  46 ++++++
 .../explain_field_access_closed.1.adm           |  42 ++++++
 .../explain_object_constructor-01.1.adm         |   8 +
 .../explain_object_constructor-02.1.adm         |   8 +
 .../explain_object_constructor-03.1.adm         |   8 +
 .../explain/explain_simple/explain_simple.1.adm |   5 +-
 .../resources/runtimets/testsuite_sqlpp.xml     |  25 ++++
 .../asterix/common/exceptions/ErrorCode.java    |   3 -
 .../asterix/metadata/declared/DataSourceId.java |   4 +-
 .../functions/ExternalScalarFunctionInfo.java   |   4 -
 .../org/apache/asterix/om/base/ABinary.java     |   9 +-
 .../org/apache/asterix/om/base/ABitArray.java   | 148 -------------------
 .../org/apache/asterix/om/base/ABoolean.java    |   5 +-
 .../org/apache/asterix/om/base/ACircle.java     |   8 +-
 .../asterix/om/base/ACollectionCursor.java      |  11 --
 .../java/org/apache/asterix/om/base/ADate.java  |   8 +-
 .../org/apache/asterix/om/base/ADateTime.java   |   5 +-
 .../org/apache/asterix/om/base/ADouble.java     |   9 +-
 .../org/apache/asterix/om/base/ADuration.java   |   9 +-
 .../java/org/apache/asterix/om/base/AFloat.java |   9 +-
 .../java/org/apache/asterix/om/base/AInt16.java |   9 +-
 .../java/org/apache/asterix/om/base/AInt32.java |   9 +-
 .../java/org/apache/asterix/om/base/AInt64.java |   9 +-
 .../java/org/apache/asterix/om/base/AInt8.java  |   9 +-
 .../org/apache/asterix/om/base/AInterval.java   |  18 +--
 .../java/org/apache/asterix/om/base/ALine.java  |   8 +-
 .../apache/asterix/om/base/AOrderedList.java    |   5 +-
 .../java/org/apache/asterix/om/base/APoint.java |   8 +-
 .../org/apache/asterix/om/base/APoint3D.java    |   8 +-
 .../org/apache/asterix/om/base/APolygon.java    |   8 +-
 .../org/apache/asterix/om/base/ARecord.java     |   6 +-
 .../org/apache/asterix/om/base/ARectangle.java  |   8 +-
 .../org/apache/asterix/om/base/AString.java     |   4 +-
 .../java/org/apache/asterix/om/base/ATime.java  |   8 +-
 .../java/org/apache/asterix/om/base/AUUID.java  |  12 +-
 .../apache/asterix/om/base/AUnorderedList.java  |  17 +--
 .../asterix/om/base/AYearMonthDuration.java     |  29 ++--
 .../asterix/om/base/ShortWithoutTypeInfo.java   |  95 ------------
 .../om/functions/ExternalFunctionInfo.java      |  11 --
 .../om/functions/FunctionDisplayUtil.java       | 110 ++++++++++++++
 .../asterix/om/functions/FunctionInfo.java      |  43 +++---
 .../om/functions/IExternalFunctionInfo.java     |   3 +-
 .../core/algebra/base/IOptimizationContext.java |   2 -
 .../core/algebra/base/IVariableContext.java     |   7 +-
 .../core/algebra/base/LogicalVariable.java      |   9 +-
 .../AbstractFunctionCallExpression.java         |  17 +--
 .../AggregateFunctionCallExpression.java        |   4 -
 .../StatefulFunctionCallExpression.java         |   6 -
 .../VariableReferenceExpression.java            |  10 +-
 .../algebra/functions/AbstractFunctionInfo.java |  39 -----
 .../core/algebra/functions/IFunctionInfo.java   |  40 ++++-
 .../base/AlgebricksOptimizationContext.java     |  12 +-
 .../piglet/metadata/PigletFunction.java         |   7 +-
 98 files changed, 750 insertions(+), 641 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SimilarityCheckRule.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SimilarityCheckRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SimilarityCheckRule.java
index 5835a21..46e0f7e 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SimilarityCheckRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/SimilarityCheckRule.java
@@ -190,9 +190,9 @@ public class SimilarityCheckRule implements IAlgebraicRewriteRule {
                     .add(new MutableObject<ILogicalOperator>(matchingAssign.getInputs().get(0).getValue()));
             matchingAssign.getInputs().get(0).setValue(newAssign);
 
-            // Replace select condition with a get-item on newVar.
+            // Replace select condition with a get-item on newVarFromExpression.
             List<Mutable<ILogicalExpression>> selectGetItemArgs = new ArrayList<Mutable<ILogicalExpression>>();
-            // First arg is a variable reference expr on newVar.
+            // First arg is a variable reference expr on newVarFromExpression.
             selectGetItemArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(newVar)));
             // Second arg is the item index to be accessed, here 0.
             selectGetItemArgs.add(new MutableObject<ILogicalExpression>(
@@ -202,9 +202,10 @@ public class SimilarityCheckRule implements IAlgebraicRewriteRule {
             // Replace the old similarity function call with the new getItemExpr.
             expRef.setValue(selectGetItemExpr);
 
-            // Replace expr corresponding to original variable in the original assign with a get-item on newVar.
+            // Replace expr corresponding to original variable in the original assign with a get-item on
+            // newVarFromExpression.
             List<Mutable<ILogicalExpression>> assignGetItemArgs = new ArrayList<Mutable<ILogicalExpression>>();
-            // First arg is a variable reference expr on newVar.
+            // First arg is a variable reference expr on newVarFromExpression.
             assignGetItemArgs.add(new MutableObject<ILogicalExpression>(new VariableReferenceExpression(newVar)));
             // Second arg is the item index to be accessed, here 1.
             assignGetItemArgs.add(new MutableObject<ILogicalExpression>(

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/AqlExpressionToPlanTranslator.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/AqlExpressionToPlanTranslator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/AqlExpressionToPlanTranslator.java
index 62b71df..81c5f4c 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/AqlExpressionToPlanTranslator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/AqlExpressionToPlanTranslator.java
@@ -79,7 +79,7 @@ class AqlExpressionToPlanTranslator extends LangExpressionToPlanTranslator imple
     @Override
     public Pair<ILogicalOperator, LogicalVariable> visit(ForClause fc, Mutable<ILogicalOperator> tupSource)
             throws CompilationException {
-        LogicalVariable v = context.newVar(fc.getVarExpr());
+        LogicalVariable v = context.newVarFromExpression(fc.getVarExpr());
         Expression inExpr = fc.getInExpr();
         Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(inExpr, tupSource);
         ILogicalOperator returnedOp;
@@ -87,7 +87,7 @@ class AqlExpressionToPlanTranslator extends LangExpressionToPlanTranslator imple
         if (fc.getPosVarExpr() == null) {
             returnedOp = new UnnestOperator(v, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)));
         } else {
-            LogicalVariable pVar = context.newVar(fc.getPosVarExpr());
+            LogicalVariable pVar = context.newVarFromExpression(fc.getPosVarExpr());
             // We set the positional variable type as INT64 type.
             returnedOp = new UnnestOperator(v, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)),
                     pVar, BuiltinType.AINT64, new PositionWriter());

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
index 4b4ef8e..2662be8 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
@@ -691,7 +691,7 @@ class LangExpressionToPlanTranslator
         ILogicalOperator rootOperator = inputOperator;
 
         //Makes the id of the insert var point to the record variable.
-        context.newVar(compiledInsert.getVar());
+        context.newVarFromExpression(compiledInsert.getVar());
         context.setVar(compiledInsert.getVar(),
                 ((VariableReferenceExpression) insertOp.getPayloadExpression().getValue()).getVariableReference());
         Pair<ILogicalExpression, Mutable<ILogicalOperator>> p =
@@ -744,12 +744,12 @@ class LangExpressionToPlanTranslator
         LogicalVariable v;
         ILogicalOperator returnedOp;
         if (lc.getBindingExpr().getKind() == Kind.VARIABLE_EXPRESSION) {
-            v = context.newVar(lc.getVarExpr());
+            v = context.newVarFromExpression(lc.getVarExpr());
             LogicalVariable prev = context.getVar(((VariableExpr) lc.getBindingExpr()).getVar().getId());
             returnedOp = new AssignOperator(v, new MutableObject<>(new VariableReferenceExpression(prev)));
             returnedOp.getInputs().add(tupSource);
         } else {
-            v = context.newVar(lc.getVarExpr());
+            v = context.newVarFromExpression(lc.getVarExpr());
             Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo =
                     langExprToAlgExpression(lc.getBindingExpr(), tupSource);
             returnedOp = new AssignOperator(v, new MutableObject<>(eo.first));
@@ -762,7 +762,7 @@ class LangExpressionToPlanTranslator
     public Pair<ILogicalOperator, LogicalVariable> visit(FieldAccessor fa, Mutable<ILogicalOperator> tupSource)
             throws CompilationException {
         Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = langExprToAlgExpression(fa.getExpr(), tupSource);
-        LogicalVariable v = context.newVar();
+        LogicalVariable v = context.newVarFromExpression(fa);
         AbstractFunctionCallExpression fldAccess =
                 new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.FIELD_ACCESS_BY_NAME));
         fldAccess.getArguments().add(new MutableObject<>(p.first));
@@ -922,7 +922,7 @@ class LangExpressionToPlanTranslator
                 ILogicalExpression groupFieldExpr = langExprToAlgExpression(groupField.first, topOp).first;
                 groupRecordConstructorArgList.add(new MutableObject<>(groupFieldExpr));
             }
-            LogicalVariable groupVar = context.newVar(gc.getGroupVar());
+            LogicalVariable groupVar = context.newVarFromExpression(gc.getGroupVar());
             AssignOperator groupVarAssignOp = new AssignOperator(groupVar,
                     new MutableObject<>(new ScalarFunctionCallExpression(
                             FunctionUtil.getFunctionInfo(BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR),
@@ -934,14 +934,14 @@ class LangExpressionToPlanTranslator
         GroupByOperator gOp = new GroupByOperator();
         for (GbyVariableExpressionPair ve : gc.getGbyPairList()) {
             VariableExpr vexpr = ve.getVar();
-            LogicalVariable v = vexpr == null ? context.newVar() : context.newVar(vexpr);
+            LogicalVariable v = vexpr == null ? context.newVar() : context.newVarFromExpression(vexpr);
             Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(ve.getExpr(), topOp);
             gOp.addGbyExpression(v, eo.first);
             topOp = eo.second;
         }
         for (GbyVariableExpressionPair ve : gc.getDecorPairList()) {
             VariableExpr vexpr = ve.getVar();
-            LogicalVariable v = vexpr == null ? context.newVar() : context.newVar(vexpr);
+            LogicalVariable v = vexpr == null ? context.newVar() : context.newVarFromExpression(vexpr);
             Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(ve.getExpr(), topOp);
             gOp.addDecorExpression(v, eo.first);
             topOp = eo.second;
@@ -1159,7 +1159,7 @@ class LangExpressionToPlanTranslator
         for (QuantifiedPair qt : qe.getQuantifiedList()) {
             Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo1 = langExprToAlgExpression(qt.getExpr(), topOp);
             topOp = eo1.second;
-            LogicalVariable uVar = context.newVar(qt.getVarExpr());
+            LogicalVariable uVar = context.newVarFromExpression(qt.getVarExpr());
             ILogicalOperator u = new UnnestOperator(uVar, new MutableObject<>(makeUnnestExpression(eo1.first)));
 
             if (firstOp == null) {

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
index f46613b..477d596 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
@@ -248,12 +248,12 @@ class SqlppExpressionToPlanTranslator extends LangExpressionToPlanTranslator imp
     @Override
     public Pair<ILogicalOperator, LogicalVariable> visit(FromTerm fromTerm, Mutable<ILogicalOperator> tupSource)
             throws CompilationException {
-        LogicalVariable fromVar = context.newVar(fromTerm.getLeftVariable());
+        LogicalVariable fromVar = context.newVarFromExpression(fromTerm.getLeftVariable());
         Expression fromExpr = fromTerm.getLeftExpression();
         Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(fromExpr, tupSource);
         ILogicalOperator unnestOp;
         if (fromTerm.hasPositionalVariable()) {
-            LogicalVariable pVar = context.newVar(fromTerm.getPositionalVariable());
+            LogicalVariable pVar = context.newVarFromExpression(fromTerm.getPositionalVariable());
             // We set the positional variable type as INT64 type.
             unnestOp =
                     new UnnestOperator(fromVar, new MutableObject<ILogicalExpression>(makeUnnestExpression(eo.first)),
@@ -448,12 +448,12 @@ class SqlppExpressionToPlanTranslator extends LangExpressionToPlanTranslator imp
     private Pair<ILogicalOperator, LogicalVariable> generateUnnestForBinaryCorrelateRightBranch(
             AbstractBinaryCorrelateClause binaryCorrelate, Mutable<ILogicalOperator> inputOpRef, boolean innerUnnest)
             throws CompilationException {
-        LogicalVariable rightVar = context.newVar(binaryCorrelate.getRightVariable());
+        LogicalVariable rightVar = context.newVarFromExpression(binaryCorrelate.getRightVariable());
         Expression rightExpr = binaryCorrelate.getRightExpression();
         Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(rightExpr, inputOpRef);
         ILogicalOperator unnestOp;
         if (binaryCorrelate.hasPositionalVariable()) {
-            LogicalVariable pVar = context.newVar(binaryCorrelate.getPositionalVariable());
+            LogicalVariable pVar = context.newVarFromExpression(binaryCorrelate.getPositionalVariable());
             // We set the positional variable type as INT64 type.
             unnestOp = innerUnnest
                     ? new UnnestOperator(rightVar, new MutableObject<>(makeUnnestExpression(eo.first)), pVar,

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/TranslationContext.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/TranslationContext.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/TranslationContext.java
index 71de5ac..1344e0c 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/TranslationContext.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/TranslationContext.java
@@ -18,10 +18,12 @@
  */
 package org.apache.asterix.translator;
 
+import java.util.ArrayDeque;
+import java.util.Deque;
 import java.util.HashMap;
 import java.util.Map;
-import java.util.Stack;
 
+import org.apache.asterix.lang.common.base.Expression;
 import org.apache.asterix.lang.common.expression.VariableExpr;
 import org.apache.hyracks.algebricks.core.algebra.base.Counter;
 import org.apache.hyracks.algebricks.core.algebra.base.IVariableContext;
@@ -32,8 +34,8 @@ public final class TranslationContext implements IVariableContext {
     private Counter varCounter;
 
     /** The stack is the used to manage the scope of variables for group-by rebindings. */
-    private Stack<Map<Integer, LogicalVariable>> stack = new Stack<Map<Integer, LogicalVariable>>();
-    private Map<Integer, LogicalVariable> currentVarMap = new HashMap<Integer, LogicalVariable>();
+    private Deque<Map<Integer, LogicalVariable>> stack = new ArrayDeque<>();
+    private Map<Integer, LogicalVariable> currentVarMap = new HashMap<>();
     private boolean topFlwor = true;
 
     public TranslationContext(Counter varCounter) {
@@ -47,9 +49,15 @@ public final class TranslationContext implements IVariableContext {
 
     @Override
     public LogicalVariable newVar() {
+        return newVarFromExpression(null);
+    }
+
+    @Override
+    public LogicalVariable newVar(String displayName) {
         varCounter.inc();
-        LogicalVariable var = new LogicalVariable(varCounter.get());
-        currentVarMap.put(varCounter.get(), var);
+        int varId = varCounter.get();
+        LogicalVariable var = new LogicalVariable(varId, displayName);
+        currentVarMap.put(varId, var);
         return var;
     }
 
@@ -70,20 +78,26 @@ public final class TranslationContext implements IVariableContext {
         return currentVarMap.get(varId);
     }
 
-    public LogicalVariable getVar(VariableExpr v) {
-        return currentVarMap.get(v.getVar().getId());
-    }
-
-    public LogicalVariable newVar(VariableExpr v) {
-        Integer i = v.getVar().getId();
-        if (i > varCounter.get()) {
-            varCounter.set(i);
+    public LogicalVariable newVarFromExpression(Expression expr) {
+        int varId;
+        if (expr != null && expr.getKind() == Expression.Kind.VARIABLE_EXPRESSION) {
+            VariableExpr v = (VariableExpr) expr;
+            varId = v.getVar().getId();
+            if (varId > varCounter.get()) {
+                varCounter.set(varId);
+            }
+        } else {
+            varCounter.inc();
+            varId = varCounter.get();
         }
-        LogicalVariable var = new LogicalVariable(i);
-        currentVarMap.put(i, var);
+        LogicalVariable var = expr != null && (expr.getKind() == Expression.Kind.VARIABLE_EXPRESSION
+                || expr.getKind() == Expression.Kind.FIELD_ACCESSOR_EXPRESSION)
+                        ? new LogicalVariable(varId, expr.toString()) : new LogicalVariable(varId);
+        currentVarMap.put(varId, var);
         return var;
     }
 
+
     public void setVar(VariableExpr v, LogicalVariable var) {
         currentVarMap.put(v.getVar().getId(), var);
     }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/count-tweets.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/count-tweets.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/count-tweets.plan
index a17020e..4f8d74b 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/count-tweets.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/count-tweets.plan
@@ -9,7 +9,7 @@
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
             -- HASH_PARTITION_EXCHANGE [$$14]  |PARTITIONED|
-              -- SORT_GROUP_BY[$$4]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$token]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
                           -- NESTED_TUPLE_SOURCE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/fj-phase1.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/fj-phase1.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/fj-phase1.plan
index 83af511..c00096d 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/fj-phase1.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/fj-phase1.plan
@@ -10,12 +10,12 @@
                         -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-              -- STABLE_SORT [$$21(ASC), $$4(ASC)]  |PARTITIONED|
+              -- STABLE_SORT [$$21(ASC), $$item(ASC)]  |PARTITIONED|
                 -- HASH_PARTITION_EXCHANGE [$$21]  |PARTITIONED|
                   -- STREAM_PROJECT  |PARTITIONED|
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      -- HYBRID_HASH_JOIN [$$1][$$4]  |PARTITIONED|
-                        -- HASH_PARTITION_EXCHANGE [$$1]  |PARTITIONED|
+                      -- HYBRID_HASH_JOIN [$$lottery_number][$$item]  |PARTITIONED|
+                        -- HASH_PARTITION_EXCHANGE [$$lottery_number]  |PARTITIONED|
                           -- STREAM_PROJECT  |PARTITIONED|
                             -- UNNEST  |PARTITIONED|
                               -- STREAM_PROJECT  |PARTITIONED|
@@ -24,7 +24,7 @@
                                     -- DATASOURCE_SCAN  |PARTITIONED|
                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                         -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                        -- HASH_PARTITION_EXCHANGE [$$4]  |PARTITIONED|
+                        -- HASH_PARTITION_EXCHANGE [$$item]  |PARTITIONED|
                           -- ASSIGN  |PARTITIONED|
                             -- RUNNING_AGGREGATE  |PARTITIONED|
                               -- STREAM_PROJECT  |PARTITIONED|
@@ -37,7 +37,7 @@
                                                   -- NESTED_TUPLE_SOURCE  |LOCAL|
                                               }
                                         -- HASH_PARTITION_EXCHANGE [$$30]  |PARTITIONED|
-                                          -- SORT_GROUP_BY[$$3]  |PARTITIONED|
+                                          -- SORT_GROUP_BY[$$lottery_number]  |PARTITIONED|
                                                   {
                                                     -- AGGREGATE  |LOCAL|
                                                       -- NESTED_TUPLE_SOURCE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/gby_inline.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/gby_inline.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/gby_inline.plan
index b3f4ac2..4043a9c 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/gby_inline.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/gby_inline.plan
@@ -4,20 +4,20 @@
       -- ASSIGN  |PARTITIONED|
         -- STREAM_PROJECT  |PARTITIONED|
           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-            -- PRE_CLUSTERED_GROUP_BY[$$1]  |PARTITIONED|
+            -- PRE_CLUSTERED_GROUP_BY[$$ccustkey]  |PARTITIONED|
                     {
                       -- AGGREGATE  |LOCAL|
                         -- STREAM_SELECT  |LOCAL|
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
                     }
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                -- STABLE_SORT [$$1(ASC)]  |PARTITIONED|
+                -- STABLE_SORT [$$ccustkey(ASC)]  |PARTITIONED|
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                     -- STREAM_PROJECT  |PARTITIONED|
                       -- STREAM_PROJECT  |PARTITIONED|
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                          -- HYBRID_HASH_JOIN [$$1, $$2][$$24, $$26]  |PARTITIONED|
-                            -- HASH_PARTITION_EXCHANGE [$$1]  |PARTITIONED|
+                          -- HYBRID_HASH_JOIN [$$ccustkey, $$cnationkey][$$24, $$26]  |PARTITIONED|
+                            -- HASH_PARTITION_EXCHANGE [$$ccustkey]  |PARTITIONED|
                               -- PRE_CLUSTERED_GROUP_BY[$$20]  |PARTITIONED|
                                       {
                                         -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/hashjoin-with-unnest.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/hashjoin-with-unnest.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/hashjoin-with-unnest.plan
index b36dbe8..302d7e4 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/hashjoin-with-unnest.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/hashjoin-with-unnest.plan
@@ -4,8 +4,8 @@
       -- ASSIGN  |PARTITIONED|
         -- STREAM_PROJECT  |PARTITIONED|
           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-            -- HYBRID_HASH_JOIN [$$2][$$3]  |PARTITIONED|
-              -- HASH_PARTITION_EXCHANGE [$$2]  |PARTITIONED|
+            -- HYBRID_HASH_JOIN [$$a][$$b]  |PARTITIONED|
+              -- HASH_PARTITION_EXCHANGE [$$a]  |PARTITIONED|
                 -- STREAM_PROJECT  |PARTITIONED|
                   -- UNNEST  |PARTITIONED|
                     -- ASSIGN  |PARTITIONED|
@@ -14,7 +14,7 @@
                           -- DATASOURCE_SCAN  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                               -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-              -- HASH_PARTITION_EXCHANGE [$$3]  |PARTITIONED|
+              -- HASH_PARTITION_EXCHANGE [$$b]  |PARTITIONED|
                 -- STREAM_PROJECT  |PARTITIONED|
                   -- UNNEST  |PARTITIONED|
                     -- ASSIGN  |PARTITIONED|
@@ -22,4 +22,4 @@
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                           -- DATASOURCE_SCAN  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                              -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
\ No newline at end of file
+                              -- EMPTY_TUPLE_SOURCE  |PARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/inlined_q18_large_volume_customer.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/inlined_q18_large_volume_customer.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/inlined_q18_large_volume_customer.plan
index b2d5267..d9ce6c8 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/inlined_q18_large_volume_customer.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/inlined_q18_large_volume_customer.plan
@@ -3,10 +3,10 @@
     -- STREAM_PROJECT  |UNPARTITIONED|
       -- ASSIGN  |UNPARTITIONED|
         -- STREAM_LIMIT  |UNPARTITIONED|
-          -- SORT_MERGE_EXCHANGE [$$12(DESC), $$11(ASC) ]  |PARTITIONED|
+          -- SORT_MERGE_EXCHANGE [$$o_totalprice(DESC), $$o_orderdate(ASC) ]  |PARTITIONED|
             -- STREAM_LIMIT  |PARTITIONED|
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                -- STABLE_SORT [topK: 100] [$$12(DESC), $$11(ASC)]  |PARTITIONED|
+                -- STABLE_SORT [topK: 100] [$$o_totalprice(DESC), $$o_orderdate(ASC)]  |PARTITIONED|
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                     -- SORT_GROUP_BY[$$72, $$73]  |PARTITIONED|
                             {
@@ -26,7 +26,7 @@
                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                     -- STREAM_PROJECT  |PARTITIONED|
                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                        -- HYBRID_HASH_JOIN [$$57][$$3]  |PARTITIONED|
+                                        -- HYBRID_HASH_JOIN [$$57][$$l_orderkey]  |PARTITIONED|
                                           -- HASH_PARTITION_EXCHANGE [$$57]  |PARTITIONED|
                                             -- STREAM_PROJECT  |PARTITIONED|
                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -45,7 +45,7 @@
                                                           -- DATASOURCE_SCAN  |PARTITIONED|
                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                               -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                          -- HASH_PARTITION_EXCHANGE [$$3]  |PARTITIONED|
+                                          -- HASH_PARTITION_EXCHANGE [$$l_orderkey]  |PARTITIONED|
                                             -- STREAM_PROJECT  |PARTITIONED|
                                               -- STREAM_SELECT  |PARTITIONED|
                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/nonpure/global-datetime-use-index-return-time.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/nonpure/global-datetime-use-index-return-time.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/nonpure/global-datetime-use-index-return-time.plan
index c6fd83e..a7a7662 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/nonpure/global-datetime-use-index-return-time.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/nonpure/global-datetime-use-index-return-time.plan
@@ -6,7 +6,7 @@
           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
             -- BTREE_SEARCH  |PARTITIONED|
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                -- STABLE_SORT [$$0(ASC)]  |PARTITIONED|
-                  -- HASH_PARTITION_EXCHANGE [$$0]  |PARTITIONED|
+                -- STABLE_SORT [$$time(ASC)]  |PARTITIONED|
+                  -- HASH_PARTITION_EXCHANGE [$$time]  |PARTITIONED|
                     -- ASSIGN  |UNPARTITIONED|
-                      -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
\ No newline at end of file
+                      -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/nonpure/global-datetime-use-index.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/nonpure/global-datetime-use-index.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/nonpure/global-datetime-use-index.plan
index 972e56a..228a6ec 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/nonpure/global-datetime-use-index.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/nonpure/global-datetime-use-index.plan
@@ -4,7 +4,7 @@
       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
         -- BTREE_SEARCH  |PARTITIONED|
           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-            -- STABLE_SORT [$$0(ASC)]  |PARTITIONED|
-              -- HASH_PARTITION_EXCHANGE [$$0]  |PARTITIONED|
+            -- STABLE_SORT [$$time(ASC)]  |PARTITIONED|
+              -- HASH_PARTITION_EXCHANGE [$$time]  |PARTITIONED|
                 -- ASSIGN  |UNPARTITIONED|
-                  -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
\ No newline at end of file
+                  -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/orderby-desc-using-gby.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orderby-desc-using-gby.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orderby-desc-using-gby.plan
index 8c86c6b..8e7f8ca 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orderby-desc-using-gby.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orderby-desc-using-gby.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$1(DESC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$name(DESC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$10]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/q01_pricing_summary_report_nt.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/q01_pricing_summary_report_nt.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/q01_pricing_summary_report_nt.plan
index d0b7ef3..de74804 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/q01_pricing_summary_report_nt.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/q01_pricing_summary_report_nt.plan
@@ -15,8 +15,8 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$1(ASC), $$2(ASC) ]  |PARTITIONED|
-          -- STABLE_SORT [$$1(ASC), $$2(ASC)]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$l_returnflag(ASC), $$l_linestatus(ASC) ]  |PARTITIONED|
+          -- STABLE_SORT [$$l_returnflag(ASC), $$l_linestatus(ASC)]  |PARTITIONED|
             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
               -- EXTERNAL_GROUP_BY[$$74, $$75]  |PARTITIONED|
                       {

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/q03_shipping_priority.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/q03_shipping_priority.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/q03_shipping_priority.plan
index c4c0c05..e5a6867 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/q03_shipping_priority.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/q03_shipping_priority.plan
@@ -3,10 +3,10 @@
     -- STREAM_PROJECT  |UNPARTITIONED|
       -- ASSIGN  |UNPARTITIONED|
         -- STREAM_LIMIT  |UNPARTITIONED|
-          -- SORT_MERGE_EXCHANGE [$$49(DESC), $$4(ASC) ]  |PARTITIONED|
+          -- SORT_MERGE_EXCHANGE [$$49(DESC), $$o_orderdate(ASC) ]  |PARTITIONED|
             -- STREAM_LIMIT  |PARTITIONED|
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                -- STABLE_SORT [topK: 10] [$$49(DESC), $$4(ASC)]  |PARTITIONED|
+                -- STABLE_SORT [topK: 10] [$$49(DESC), $$o_orderdate(ASC)]  |PARTITIONED|
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                     -- EXTERNAL_GROUP_BY[$$55, $$56, $$57]  |PARTITIONED|
                             {

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1005.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1005.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1005.plan
index 5675545..92a1472 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1005.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1005.plan
@@ -20,7 +20,7 @@
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                               -- STREAM_PROJECT  |PARTITIONED|
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                  -- HYBRID_HASH_JOIN [$$1][$$13]  |PARTITIONED|
+                                  -- HYBRID_HASH_JOIN [$$c][$$13]  |PARTITIONED|
                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                       -- UNNEST  |PARTITIONED|
                                         -- STREAM_PROJECT  |PARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1127.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1127.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1127.plan
index d7ca1e9..40e653c 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1127.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1127.plan
@@ -3,10 +3,10 @@
     -- STREAM_PROJECT  |UNPARTITIONED|
       -- ASSIGN  |UNPARTITIONED|
         -- STREAM_LIMIT  |UNPARTITIONED|
-          -- SORT_MERGE_EXCHANGE [$$49(DESC), $$4(ASC) ]  |PARTITIONED|
+          -- SORT_MERGE_EXCHANGE [$$49(DESC), $$o_orderdate(ASC) ]  |PARTITIONED|
             -- STREAM_LIMIT  |PARTITIONED|
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                -- STABLE_SORT [topK: 10] [$$49(DESC), $$4(ASC)]  |PARTITIONED|
+                -- STABLE_SORT [topK: 10] [$$49(DESC), $$o_orderdate(ASC)]  |PARTITIONED|
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                     -- EXTERNAL_GROUP_BY[$$55, $$56, $$57]  |PARTITIONED|
                             {

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1168.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1168.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1168.plan
index b4d6934..660ad87 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1168.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1168.plan
@@ -15,8 +15,8 @@
                   -- HASH_PARTITION_EXCHANGE [$$12]  |PARTITIONED|
                     -- STREAM_PROJECT  |PARTITIONED|
                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                        -- HYBRID_HASH_JOIN [$$1][$$11]  |PARTITIONED|
-                          -- HASH_PARTITION_EXCHANGE [$$1]  |PARTITIONED|
+                        -- HYBRID_HASH_JOIN [$$p][$$11]  |PARTITIONED|
+                          -- HASH_PARTITION_EXCHANGE [$$p]  |PARTITIONED|
                             -- ASSIGN  |UNPARTITIONED|
                               -- STREAM_PROJECT  |UNPARTITIONED|
                                 -- UNNEST  |UNPARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-2.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-2.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-2.plan
index cb833a5..caa317d 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-2.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-2.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$3(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$o_orderpriority(ASC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$39]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-3.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-3.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-3.plan
index 336d7d4..5141e09 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-3.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-3.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$3(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$o_orderpriority(ASC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$39]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-4.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-4.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-4.plan
index 5e3d14a..2de88ae 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-4.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343-4.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$3(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$o_orderpriority(ASC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$39]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343.plan
index cb833a5..caa317d 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1343.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$3(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$o_orderpriority(ASC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$39]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1806.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1806.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1806.plan
index 2439f0d..7ea82a1 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1806.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1806.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$2(ASC), $$3(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$l_returnflag(ASC), $$l_linestatus(ASC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$98, $$99]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue562.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue562.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue562.plan
index b7e11d3..9974287 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue562.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue562.plan
@@ -2,14 +2,14 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$7(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$cntrycode(ASC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$82]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
             -- HASH_PARTITION_EXCHANGE [$$82]  |PARTITIONED|
-              -- SORT_GROUP_BY[$$11]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$phone_substr]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
@@ -41,7 +41,7 @@
                                               -- ASSIGN  |PARTITIONED|
                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                    -- HYBRID_HASH_JOIN [$$11][$$65]  |PARTITIONED|
+                                                    -- HYBRID_HASH_JOIN [$$phone_substr][$$65]  |PARTITIONED|
                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                         -- STREAM_PROJECT  |PARTITIONED|
                                                           -- ASSIGN  |PARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue785.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue785.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue785.plan
index 4027e6a..fe1f67a 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue785.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue785.plan
@@ -3,7 +3,7 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-          -- PRE_CLUSTERED_GROUP_BY[$$7]  |PARTITIONED|
+          -- PRE_CLUSTERED_GROUP_BY[$$nation_key]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- STREAM_LIMIT  |LOCAL|
@@ -11,8 +11,8 @@
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-              -- STABLE_SORT [$$7(ASC)]  |PARTITIONED|
-                -- HASH_PARTITION_EXCHANGE [$$7]  |PARTITIONED|
+              -- STABLE_SORT [$$nation_key(ASC)]  |PARTITIONED|
+                -- HASH_PARTITION_EXCHANGE [$$nation_key]  |PARTITIONED|
                   -- SORT_GROUP_BY[$$69, $$70]  |PARTITIONED|
                           {
                             -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue810-2.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue810-2.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue810-2.plan
index 7b94c2a..e229e75 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue810-2.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue810-2.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$1(ASC), $$2(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$l_returnflag(ASC), $$l_linestatus(ASC) ]  |PARTITIONED|
           -- PRE_CLUSTERED_GROUP_BY[$$75, $$76]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue810.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue810.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue810.plan
index 3e7d5d9..ca941bf 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue810.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-issue810.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$1(ASC), $$2(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$l_returnflag(ASC), $$l_linestatus(ASC) ]  |PARTITIONED|
           -- PRE_CLUSTERED_GROUP_BY[$$34, $$35]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/split-materialization-above-join.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/split-materialization-above-join.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/split-materialization-above-join.plan
index d698437..a81a142 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/split-materialization-above-join.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/split-materialization-above-join.plan
@@ -8,16 +8,16 @@
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                 -- STREAM_PROJECT  |PARTITIONED|
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                    -- HYBRID_HASH_JOIN [$$26][$$94]  |PARTITIONED|
-                      -- HASH_PARTITION_EXCHANGE [$$26]  |PARTITIONED|
+                    -- HYBRID_HASH_JOIN [$$idRight][$$94]  |PARTITIONED|
+                      -- HASH_PARTITION_EXCHANGE [$$idRight]  |PARTITIONED|
                         -- STREAM_PROJECT  |PARTITIONED|
                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                            -- HYBRID_HASH_JOIN [$$93][$$25]  |PARTITIONED|
+                            -- HYBRID_HASH_JOIN [$$93][$$idLeft]  |PARTITIONED|
                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                 -- DATASOURCE_SCAN  |PARTITIONED|
                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                     -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                              -- HASH_PARTITION_EXCHANGE [$$25]  |PARTITIONED|
+                              -- HASH_PARTITION_EXCHANGE [$$idLeft]  |PARTITIONED|
                                 -- PRE_CLUSTERED_GROUP_BY[$$122, $$124]  |PARTITIONED|
                                         {
                                           -- AGGREGATE  |LOCAL|
@@ -31,13 +31,13 @@
                                             -- STREAM_SELECT  |PARTITIONED|
                                               -- STREAM_PROJECT  |PARTITIONED|
                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                  -- HYBRID_HASH_JOIN [$$12][$$23]  |PARTITIONED|
+                                                  -- HYBRID_HASH_JOIN [$$prefixTokenLeft][$$prefixTokenRight]  |PARTITIONED|
                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                       -- STREAM_PROJECT  |PARTITIONED|
                                                         -- ASSIGN  |PARTITIONED|
                                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                             -- REPLICATE  |PARTITIONED|
-                                                              -- HASH_PARTITION_EXCHANGE [$$23]  |PARTITIONED|
+                                                              -- HASH_PARTITION_EXCHANGE [$$prefixTokenRight]  |PARTITIONED|
                                                                 -- UNNEST  |PARTITIONED|
                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                     -- PRE_CLUSTERED_GROUP_BY[$$97]  |PARTITIONED|
@@ -47,12 +47,12 @@
                                                                                   -- NESTED_TUPLE_SOURCE  |LOCAL|
                                                                             }
                                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                        -- STABLE_SORT [$$97(ASC), $$21(ASC)]  |PARTITIONED|
+                                                                        -- STABLE_SORT [$$97(ASC), $$i(ASC)]  |PARTITIONED|
                                                                           -- HASH_PARTITION_EXCHANGE [$$97]  |PARTITIONED|
                                                                             -- STREAM_PROJECT  |PARTITIONED|
                                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                                -- HYBRID_HASH_JOIN [$$15][$$18]  |PARTITIONED|
-                                                                                  -- HASH_PARTITION_EXCHANGE [$$15]  |PARTITIONED|
+                                                                                -- HYBRID_HASH_JOIN [$$tokenUnranked][$$tokenGroupped]  |PARTITIONED|
+                                                                                  -- HASH_PARTITION_EXCHANGE [$$tokenUnranked]  |PARTITIONED|
                                                                                     -- STREAM_PROJECT  |PARTITIONED|
                                                                                       -- UNNEST  |PARTITIONED|
                                                                                         -- ASSIGN  |PARTITIONED|
@@ -62,12 +62,12 @@
                                                                                                 -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                     -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                                                  -- HASH_PARTITION_EXCHANGE [$$18]  |PARTITIONED|
+                                                                                  -- HASH_PARTITION_EXCHANGE [$$tokenGroupped]  |PARTITIONED|
                                                                                     -- ASSIGN  |PARTITIONED|
                                                                                       -- RUNNING_AGGREGATE  |PARTITIONED|
                                                                                         -- STREAM_PROJECT  |PARTITIONED|
-                                                                                          -- SORT_MERGE_EXCHANGE [$$102(ASC), $$18(ASC) ]  |PARTITIONED|
-                                                                                            -- STABLE_SORT [$$102(ASC), $$18(ASC)]  |PARTITIONED|
+                                                                                          -- SORT_MERGE_EXCHANGE [$$102(ASC), $$tokenGroupped(ASC) ]  |PARTITIONED|
+                                                                                            -- STABLE_SORT [$$102(ASC), $$tokenGroupped(ASC)]  |PARTITIONED|
                                                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                 -- SORT_GROUP_BY[$$130]  |PARTITIONED|
                                                                                                         {
@@ -75,7 +75,7 @@
                                                                                                             -- NESTED_TUPLE_SOURCE  |LOCAL|
                                                                                                         }
                                                                                                   -- HASH_PARTITION_EXCHANGE [$$130]  |PARTITIONED|
-                                                                                                    -- SORT_GROUP_BY[$$17]  |PARTITIONED|
+                                                                                                    -- SORT_GROUP_BY[$$token]  |PARTITIONED|
                                                                                                             {
                                                                                                               -- AGGREGATE  |LOCAL|
                                                                                                                 -- NESTED_TUPLE_SOURCE  |LOCAL|
@@ -94,7 +94,7 @@
                                                                                                                             -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                       -- REPLICATE  |PARTITIONED|
-                                                        -- HASH_PARTITION_EXCHANGE [$$23]  |PARTITIONED|
+                                                        -- HASH_PARTITION_EXCHANGE [$$prefixTokenRight]  |PARTITIONED|
                                                           -- UNNEST  |PARTITIONED|
                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                               -- PRE_CLUSTERED_GROUP_BY[$$97]  |PARTITIONED|
@@ -104,12 +104,12 @@
                                                                             -- NESTED_TUPLE_SOURCE  |LOCAL|
                                                                       }
                                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                  -- STABLE_SORT [$$97(ASC), $$21(ASC)]  |PARTITIONED|
+                                                                  -- STABLE_SORT [$$97(ASC), $$i(ASC)]  |PARTITIONED|
                                                                     -- HASH_PARTITION_EXCHANGE [$$97]  |PARTITIONED|
                                                                       -- STREAM_PROJECT  |PARTITIONED|
                                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                          -- HYBRID_HASH_JOIN [$$15][$$18]  |PARTITIONED|
-                                                                            -- HASH_PARTITION_EXCHANGE [$$15]  |PARTITIONED|
+                                                                          -- HYBRID_HASH_JOIN [$$tokenUnranked][$$tokenGroupped]  |PARTITIONED|
+                                                                            -- HASH_PARTITION_EXCHANGE [$$tokenUnranked]  |PARTITIONED|
                                                                               -- STREAM_PROJECT  |PARTITIONED|
                                                                                 -- UNNEST  |PARTITIONED|
                                                                                   -- ASSIGN  |PARTITIONED|
@@ -119,12 +119,12 @@
                                                                                           -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                               -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                                            -- HASH_PARTITION_EXCHANGE [$$18]  |PARTITIONED|
+                                                                            -- HASH_PARTITION_EXCHANGE [$$tokenGroupped]  |PARTITIONED|
                                                                               -- ASSIGN  |PARTITIONED|
                                                                                 -- RUNNING_AGGREGATE  |PARTITIONED|
                                                                                   -- STREAM_PROJECT  |PARTITIONED|
-                                                                                    -- SORT_MERGE_EXCHANGE [$$102(ASC), $$18(ASC) ]  |PARTITIONED|
-                                                                                      -- STABLE_SORT [$$102(ASC), $$18(ASC)]  |PARTITIONED|
+                                                                                    -- SORT_MERGE_EXCHANGE [$$102(ASC), $$tokenGroupped(ASC) ]  |PARTITIONED|
+                                                                                      -- STABLE_SORT [$$102(ASC), $$tokenGroupped(ASC)]  |PARTITIONED|
                                                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                           -- SORT_GROUP_BY[$$130]  |PARTITIONED|
                                                                                                   {
@@ -132,7 +132,7 @@
                                                                                                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                                                                                                   }
                                                                                             -- HASH_PARTITION_EXCHANGE [$$130]  |PARTITIONED|
-                                                                                              -- SORT_GROUP_BY[$$17]  |PARTITIONED|
+                                                                                              -- SORT_GROUP_BY[$$token]  |PARTITIONED|
                                                                                                       {
                                                                                                         -- AGGREGATE  |LOCAL|
                                                                                                           -- NESTED_TUPLE_SOURCE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/exists.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/exists.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/exists.plan
index 1c404ab..3b92cdd 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/exists.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/exists.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$9(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$cntrycode(ASC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$82]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/not_exists.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/not_exists.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/not_exists.plan
index b134499..06257a1 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/not_exists.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/not_exists.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$9(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$cntrycode(ASC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$83]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/query-ASTERIXDB-1572.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/query-ASTERIXDB-1572.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/query-ASTERIXDB-1572.plan
index a8f916c..6c9c457 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/query-ASTERIXDB-1572.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/query-ASTERIXDB-1572.plan
@@ -16,7 +16,7 @@
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                       -- STREAM_PROJECT  |PARTITIONED|
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                          -- HYBRID_HASH_JOIN [$$44][$$5]  |PARTITIONED|
+                          -- HYBRID_HASH_JOIN [$$44][$#4]  |PARTITIONED|
                             -- HASH_PARTITION_EXCHANGE [$$44]  |PARTITIONED|
                               -- PRE_CLUSTERED_GROUP_BY[$$42]  |PARTITIONED|
                                       {
@@ -29,7 +29,7 @@
                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                       -- STREAM_PROJECT  |PARTITIONED|
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                          -- HYBRID_HASH_JOIN [$$42][$$4]  |PARTITIONED|
+                                          -- HYBRID_HASH_JOIN [$$42][$#3]  |PARTITIONED|
                                             -- HASH_PARTITION_EXCHANGE [$$42]  |PARTITIONED|
                                               -- PRE_CLUSTERED_GROUP_BY[$$40]  |PARTITIONED|
                                                       {
@@ -42,7 +42,7 @@
                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                       -- STREAM_PROJECT  |PARTITIONED|
                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                          -- HYBRID_HASH_JOIN [$$40][$$3]  |PARTITIONED|
+                                                          -- HYBRID_HASH_JOIN [$$40][$#2]  |PARTITIONED|
                                                             -- HASH_PARTITION_EXCHANGE [$$40]  |PARTITIONED|
                                                               -- PRE_CLUSTERED_GROUP_BY[$$33]  |PARTITIONED|
                                                                       {
@@ -53,26 +53,26 @@
                                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                   -- STREAM_PROJECT  |PARTITIONED|
                                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                      -- HYBRID_HASH_JOIN [$$33][$$2]  |PARTITIONED|
+                                                                      -- HYBRID_HASH_JOIN [$$33][$#1]  |PARTITIONED|
                                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                           -- STREAM_PROJECT  |PARTITIONED|
                                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                               -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                   -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                                        -- HASH_PARTITION_EXCHANGE [$$2]  |PARTITIONED|
+                                                                        -- HASH_PARTITION_EXCHANGE [$#1]  |PARTITIONED|
                                                                           -- ASSIGN  |UNPARTITIONED|
                                                                             -- UNNEST  |UNPARTITIONED|
                                                                               -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
-                                                            -- HASH_PARTITION_EXCHANGE [$$3]  |PARTITIONED|
+                                                            -- HASH_PARTITION_EXCHANGE [$#2]  |PARTITIONED|
                                                               -- ASSIGN  |UNPARTITIONED|
                                                                 -- UNNEST  |UNPARTITIONED|
                                                                   -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
-                                            -- HASH_PARTITION_EXCHANGE [$$4]  |PARTITIONED|
+                                            -- HASH_PARTITION_EXCHANGE [$#3]  |PARTITIONED|
                                               -- ASSIGN  |UNPARTITIONED|
                                                 -- UNNEST  |UNPARTITIONED|
                                                   -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
-                            -- HASH_PARTITION_EXCHANGE [$$5]  |PARTITIONED|
+                            -- HASH_PARTITION_EXCHANGE [$#4]  |PARTITIONED|
                               -- ASSIGN  |UNPARTITIONED|
                                 -- UNNEST  |UNPARTITIONED|
                                   -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping.plan
index cbcce5d..57ca6ce 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$3(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$l_shipmode(ASC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$95]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping_broadcast.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping_broadcast.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping_broadcast.plan
index cd7e6da..40c1c55 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping_broadcast.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping_broadcast.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$3(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$l_shipmode(ASC) ]  |PARTITIONED|
           -- SORT_GROUP_BY[$$95]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1298.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1298.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1298.plan
index c62d34e..ffd8105 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1298.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1298.plan
@@ -2,8 +2,8 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-        -- HYBRID_HASH_JOIN [$$5][$$17]  |PARTITIONED|
-          -- HASH_PARTITION_EXCHANGE [$$5]  |PARTITIONED|
+        -- HYBRID_HASH_JOIN [$#1][$$17]  |PARTITIONED|
+          -- HASH_PARTITION_EXCHANGE [$#1]  |PARTITIONED|
             -- STREAM_PROJECT  |PARTITIONED|
               -- ASSIGN  |PARTITIONED|
                 -- STREAM_PROJECT  |PARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
index f6ee3fa..1e60aed 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
@@ -10,7 +10,7 @@
                   -- SUBPLAN  |UNPARTITIONED|
                           {
                             -- AGGREGATE  |UNPARTITIONED|
-                              -- IN_MEMORY_STABLE_SORT [$$28(ASC)]  |UNPARTITIONED|
+                              -- IN_MEMORY_STABLE_SORT [$$j(ASC)]  |UNPARTITIONED|
                                 -- UNNEST  |UNPARTITIONED|
                                   -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
                           }
@@ -31,7 +31,7 @@
                                                       -- SUBPLAN  |UNPARTITIONED|
                                                               {
                                                                 -- AGGREGATE  |UNPARTITIONED|
-                                                                  -- IN_MEMORY_STABLE_SORT [$$21(ASC)]  |UNPARTITIONED|
+                                                                  -- IN_MEMORY_STABLE_SORT [$$j(ASC)]  |UNPARTITIONED|
                                                                     -- UNNEST  |UNPARTITIONED|
                                                                       -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
                                                               }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
index aa22db0..a5124c3 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
@@ -12,7 +12,7 @@
                             -- SUBPLAN  |LOCAL|
                                     {
                                       -- AGGREGATE  |LOCAL|
-                                        -- IN_MEMORY_STABLE_SORT [$$38(ASC)]  |LOCAL|
+                                        -- IN_MEMORY_STABLE_SORT [$$j(ASC)]  |LOCAL|
                                           -- UNNEST  |LOCAL|
                                             -- NESTED_TUPLE_SOURCE  |LOCAL|
                                     }
@@ -36,7 +36,7 @@
                                   -- SUBPLAN  |LOCAL|
                                           {
                                             -- AGGREGATE  |LOCAL|
-                                              -- IN_MEMORY_STABLE_SORT [$$31(ASC)]  |LOCAL|
+                                              -- IN_MEMORY_STABLE_SORT [$$j(ASC)]  |LOCAL|
                                                 -- UNNEST  |LOCAL|
                                                   -- NESTED_TUPLE_SOURCE  |LOCAL|
                                           }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/unnest-to-join_01.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/unnest-to-join_01.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/unnest-to-join_01.plan
index beacfb0..50dce7a 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/unnest-to-join_01.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/unnest-to-join_01.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
     -- STREAM_PROJECT  |UNPARTITIONED|
       -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
-        -- HYBRID_HASH_JOIN [$$0][$$1]  |UNPARTITIONED|
+        -- HYBRID_HASH_JOIN [$$x][$$y]  |UNPARTITIONED|
           -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
             -- UNNEST  |UNPARTITIONED|
               -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/optimizerts/results/unnest-to-join_02.plan
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/unnest-to-join_02.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/unnest-to-join_02.plan
index 8c65c4b..b8b37a2 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/unnest-to-join_02.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/unnest-to-join_02.plan
@@ -2,7 +2,7 @@
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
     -- AGGREGATE  |UNPARTITIONED|
       -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
-        -- HYBRID_HASH_JOIN [$$0][$$1]  |UNPARTITIONED|
+        -- HYBRID_HASH_JOIN [$$x][$$y]  |UNPARTITIONED|
           -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
             -- UNNEST  |UNPARTITIONED|
               -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/explain/explain_field_access/explain_field_access.1.ddl.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/explain/explain_field_access/explain_field_access.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/explain/explain_field_access/explain_field_access.1.ddl.sqlpp
new file mode 100644
index 0000000..866804c
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/explain/explain_field_access/explain_field_access.1.ddl.sqlpp
@@ -0,0 +1,29 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+DROP DATAVERSE gby IF EXISTS;
+CREATE DATAVERSE gby;
+
+USE gby;
+
+CREATE TYPE EmployeeType AS {
+  id : string
+}
+
+CREATE DATASET Employee(EmployeeType) PRIMARY KEY id;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/049d5414/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/explain/explain_field_access/explain_field_access.2.query.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/explain/explain_field_access/explain_field_access.2.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/explain/explain_field_access/explain_field_access.2.query.sqlpp
new file mode 100644
index 0000000..a4166e5
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/explain/explain_field_access/explain_field_access.2.query.sqlpp
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+USE gby;
+
+EXPLAIN
+FROM Employee e
+GROUP BY substr(e.dept.department_id, 1)
+SELECT substr(e.dept.department_id, 1) as deptId, SUM(e.salary) AS star_cost;


Mime
View raw message