You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by bu...@apache.org on 2016/04/08 02:35:12 UTC

incubator-asterixdb git commit: ASTERIXDB-1381: InlineColumnAliasVisitor improvements.

Repository: incubator-asterixdb
Updated Branches:
  refs/heads/master bce00de83 -> be7323100


ASTERIXDB-1381: InlineColumnAliasVisitor improvements.

-moved mapForRecordConstructor closer to usage location;
-documented the boolean argument to the visit method;
-documented what happens in mapForRecordConstructor if leftExpr is not a literal.

Change-Id: I82c5bc4568c11c474bec053e9f5162927c1c15d0
Reviewed-on: https://asterix-gerrit.ics.uci.edu/789
Tested-by: Jenkins <je...@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <ti...@apache.org>


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

Branch: refs/heads/master
Commit: be73231009a2e17994adee3b6accbb610c66fa0e
Parents: bce00de
Author: Yingyi Bu <yi...@couchbase.com>
Authored: Thu Apr 7 10:22:04 2016 -0700
Committer: Yingyi Bu <bu...@gmail.com>
Committed: Thu Apr 7 17:29:46 2016 -0700

----------------------------------------------------------------------
 .../group-by/sugar-02/sugar-02.4.query.sqlpp    |  31 +++
 .../results/group-by/core-02/core-02.2.adm      |   1 +
 .../visitor/InlineColumnAliasVisitor.java       | 234 ++++++++++---------
 3 files changed, 159 insertions(+), 107 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/be732310/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02/sugar-02.4.query.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02/sugar-02.4.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02/sugar-02.4.query.sqlpp
new file mode 100644
index 0000000..c86cd18
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02/sugar-02.4.query.sqlpp
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/* This query tests the column alias rewriting. */
+
+USE gby;
+
+FROM Employee e
+    JOIN Incentive i ON e.job_category = i.job_category
+    JOIN SuperStars s ON e.id = s.id
+GROUP BY deptId
+HAVING deptId = 'K55'
+SELECT e.department_id AS deptId, coll_sum(e.salary + i.bonus) AS star_cost
+ORDER BY deptId;
+

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/be732310/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-02/core-02.2.adm
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-02/core-02.2.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-02/core-02.2.adm
new file mode 100644
index 0000000..4281a7b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/core-02/core-02.2.adm
@@ -0,0 +1 @@
+{ "deptId": "K55", "star_cost": 3000 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/be732310/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/InlineColumnAliasVisitor.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/InlineColumnAliasVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/InlineColumnAliasVisitor.java
index bb8c149..f5f2fe5 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/InlineColumnAliasVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/InlineColumnAliasVisitor.java
@@ -82,22 +82,22 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
     }
 
     @Override
-    public Void visit(WhereClause whereClause, Boolean arg) throws AsterixException {
-        whereClause.getWhereExpr().accept(this, arg);
+    public Void visit(WhereClause whereClause, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        whereClause.getWhereExpr().accept(this, overwriteWithGbyKeyVarRefs);
         return null;
     }
 
     @Override
-    public Void visit(FromClause fromClause, Boolean arg) throws AsterixException {
+    public Void visit(FromClause fromClause, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         for (FromTerm fromTerm : fromClause.getFromTerms()) {
-            fromTerm.accept(this, arg);
+            fromTerm.accept(this, overwriteWithGbyKeyVarRefs);
         }
         return null;
     }
 
     @Override
-    public Void visit(FromTerm fromTerm, Boolean arg) throws AsterixException {
-        fromTerm.getLeftExpression().accept(this, arg);
+    public Void visit(FromTerm fromTerm, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        fromTerm.getLeftExpression().accept(this, overwriteWithGbyKeyVarRefs);
         // A from binding variable will override the alias to substitute.
         scopeChecker.getCurrentScope().removeSymbolExpressionMapping(fromTerm.getLeftVariable());
         if (fromTerm.hasPositionalVariable()) {
@@ -105,67 +105,68 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
         }
 
         for (AbstractBinaryCorrelateClause correlate : fromTerm.getCorrelateClauses()) {
-            correlate.accept(this, arg);
+            correlate.accept(this, overwriteWithGbyKeyVarRefs);
         }
         return null;
     }
 
     @Override
-    public Void visit(JoinClause joinClause, Boolean arg) throws AsterixException {
-        joinClause.getRightExpression().accept(this, arg);
+    public Void visit(JoinClause joinClause, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        joinClause.getRightExpression().accept(this, overwriteWithGbyKeyVarRefs);
         removeSubsutitions(joinClause);
-        joinClause.getConditionExpression().accept(this, arg);
+        joinClause.getConditionExpression().accept(this, overwriteWithGbyKeyVarRefs);
         return null;
     }
 
     @Override
-    public Void visit(NestClause nestClause, Boolean arg) throws AsterixException {
-        nestClause.getRightExpression().accept(this, arg);
-        nestClause.getConditionExpression().accept(this, arg);
+    public Void visit(NestClause nestClause, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        nestClause.getRightExpression().accept(this, overwriteWithGbyKeyVarRefs);
+        nestClause.getConditionExpression().accept(this, overwriteWithGbyKeyVarRefs);
         removeSubsutitions(nestClause);
         return null;
     }
 
     @Override
-    public Void visit(UnnestClause unnestClause, Boolean arg) throws AsterixException {
-        unnestClause.getRightExpression().accept(this, arg);
+    public Void visit(UnnestClause unnestClause, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        unnestClause.getRightExpression().accept(this, overwriteWithGbyKeyVarRefs);
         removeSubsutitions(unnestClause);
         return null;
     }
 
     @Override
-    public Void visit(Projection projection, Boolean arg) throws AsterixException {
-        projection.getExpression().accept(this, arg);
+    public Void visit(Projection projection, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        projection.getExpression().accept(this, overwriteWithGbyKeyVarRefs);
         VariableExpr columnAlias = new VariableExpr(
                 SqlppVariableUtil.toInternalVariableIdentifier(projection.getName()));
         VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope().getVarSubstitutionEnvironment();
         Expression gbyKey = (Expression) SqlppRewriteUtil.deepCopy(env.findSubstituion(columnAlias));
-        if (arg) {
-            scopeChecker.getCurrentScope().addSymbolExpressionMappingToScope(columnAlias, projection.getExpression());
-        } else {
+        if (overwriteWithGbyKeyVarRefs) {
             if (gbyKey != null) {
                 projection.setExpression(gbyKey);
             }
+        } else {
+            scopeChecker.getCurrentScope().addSymbolExpressionMappingToScope(columnAlias, projection.getExpression());
         }
         return null;
     }
 
     @Override
-    public Void visit(SelectBlock selectBlock, Boolean arg) throws AsterixException {
+    public Void visit(SelectBlock selectBlock, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         // Traverses the select block in the order of "select", "group-by",
         // "group-by" lets and "having".
-        selectBlock.getSelectClause().accept(this, true);
+        // The first pass over the select clause will not overwrite projection expressions.
+        selectBlock.getSelectClause().accept(this, false);
 
         if (selectBlock.hasFromClause()) {
-            selectBlock.getFromClause().accept(this, arg);
+            selectBlock.getFromClause().accept(this, overwriteWithGbyKeyVarRefs);
         }
         if (selectBlock.hasLetClauses()) {
             for (LetClause letClause : selectBlock.getLetList()) {
-                letClause.accept(this, arg);
+                letClause.accept(this, overwriteWithGbyKeyVarRefs);
             }
         }
         if (selectBlock.hasGroupbyClause()) {
-            selectBlock.getGroupbyClause().accept(this, arg);
+            selectBlock.getGroupbyClause().accept(this, overwriteWithGbyKeyVarRefs);
         }
         if (selectBlock.hasLetClausesAfterGroupby()) {
             for (LetClause letClauseAfterGby : selectBlock.getLetListAfterGroupby()) {
@@ -173,75 +174,119 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
             }
         }
         if (selectBlock.hasHavingClause()) {
-            selectBlock.getHavingClause().accept(this, arg);
+            selectBlock.getHavingClause().accept(this, overwriteWithGbyKeyVarRefs);
         }
 
-        // Visit select clause again to overwrite projection expressions if the group-by clause is rewritten.
-        selectBlock.getSelectClause().accept(this, false);
+        // Visit select clause again to overwrite projection expressions to group-by
+        // key variable references if any group-by key is the original projection
+        // column alias.
+        selectBlock.getSelectClause().accept(this, true);
         return null;
     }
 
     @Override
-    public Void visit(SelectClause selectClause, Boolean arg) throws AsterixException {
+    public Void visit(SelectClause selectClause, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         if (selectClause.selectElement()) {
-            selectClause.getSelectElement().accept(this, arg);
+            selectClause.getSelectElement().accept(this, overwriteWithGbyKeyVarRefs);
         }
         if (selectClause.selectRegular()) {
-            selectClause.getSelectRegular().accept(this, arg);
+            selectClause.getSelectRegular().accept(this, overwriteWithGbyKeyVarRefs);
         }
         return null;
     }
 
     @Override
-    public Void visit(SelectElement selectElement, Boolean arg) throws AsterixException {
+    public Void visit(SelectElement selectElement, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         Expression expr = selectElement.getExpression();
-        expr.accept(this, arg);
+        expr.accept(this, overwriteWithGbyKeyVarRefs);
         if (expr.getKind() == Kind.RECORD_CONSTRUCTOR_EXPRESSION) {
-            // To be consistent with SelectRegular.
-            mapForRecordConstructor(arg, (RecordConstructor) expr);
+            // Rewrite top-level field names (aliases), in order to be consistent with SelectRegular.
+            mapForRecordConstructor(overwriteWithGbyKeyVarRefs, (RecordConstructor) expr);
         }
         return null;
     }
 
+    /**
+     * Map aliases for a record constructor in SELECT ELEMENT.
+     *
+     * @param overwriteWithGbyKeyVarRefs,
+     *            whether we rewrite the record constructor with mapped group-by key variables.
+     * @param rc,
+     *            the RecordConstructor expression.
+     * @throws AsterixException
+     */
+    private void mapForRecordConstructor(Boolean overwriteWithGbyKeyVarRefs, RecordConstructor rc)
+            throws AsterixException {
+        for (FieldBinding binding : rc.getFbList()) {
+            Expression leftExpr = binding.getLeftExpr();
+            // We only need to deal with the case that the left expression (for a field name) is
+            // a string literal. Otherwise, it is different from a column alias in a projection
+            // (e.g., foo.name AS name) in regular SQL SELECT.
+            if (leftExpr.getKind() == Kind.LITERAL_EXPRESSION) {
+                LiteralExpr literalExpr = (LiteralExpr) leftExpr;
+                if (literalExpr.getValue().getLiteralType() == Literal.Type.STRING) {
+                    String fieldName = literalExpr.getValue().getStringValue();
+                    VariableExpr columnAlias = new VariableExpr(
+                            SqlppVariableUtil.toInternalVariableIdentifier(fieldName));
+                    VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope()
+                            .getVarSubstitutionEnvironment();
+                    if (overwriteWithGbyKeyVarRefs) {
+                        // Rewrites the field value expression by the mapped grouping key
+                        // (for the column alias) if there exists such a mapping.
+                        Expression gbyKey = (Expression) SqlppRewriteUtil.deepCopy(env.findSubstituion(columnAlias));
+                        if (gbyKey != null) {
+                            binding.setRightExpr(gbyKey);
+                        }
+                    } else {
+                        // If this is the first pass, map a field name (i.e., column alias) to the field expression.
+                        scopeChecker.getCurrentScope().addSymbolExpressionMappingToScope(columnAlias,
+                                binding.getRightExpr());
+                    }
+                }
+            }
+        }
+    }
+
     @Override
-    public Void visit(SelectRegular selectRegular, Boolean arg) throws AsterixException {
+    public Void visit(SelectRegular selectRegular, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         for (Projection projection : selectRegular.getProjections()) {
-            projection.accept(this, arg);
+            projection.accept(this, overwriteWithGbyKeyVarRefs);
         }
         return null;
     }
 
     @Override
-    public Void visit(SelectSetOperation selectSetOperation, Boolean arg) throws AsterixException {
-        selectSetOperation.getLeftInput().accept(this, arg);
+    public Void visit(SelectSetOperation selectSetOperation, Boolean overwriteWithGbyKeyVarRefs)
+            throws AsterixException {
+        selectSetOperation.getLeftInput().accept(this, overwriteWithGbyKeyVarRefs);
         for (SetOperationRight right : selectSetOperation.getRightInputs()) {
-            right.getSetOperationRightInput().accept(this, arg);
+            right.getSetOperationRightInput().accept(this, overwriteWithGbyKeyVarRefs);
         }
         return null;
     }
 
     @Override
-    public Void visit(SelectExpression selectExpression, Boolean arg) throws AsterixException {
+    public Void visit(SelectExpression selectExpression, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         scopeChecker.createNewScope();
 
         // Visits let bindings.
         if (selectExpression.hasLetClauses()) {
             for (LetClause lc : selectExpression.getLetList()) {
-                lc.accept(this, arg);
+                lc.accept(this, overwriteWithGbyKeyVarRefs);
             }
         }
 
         // Visits selectSetOperation.
-        selectExpression.getSelectSetOperation().accept(this, arg);
+        selectExpression.getSelectSetOperation().accept(this, overwriteWithGbyKeyVarRefs);
 
         // Visits order by.
         if (selectExpression.hasOrderby()) {
-            selectExpression.getOrderbyClause().accept(this, arg);
+            selectExpression.getOrderbyClause().accept(this, overwriteWithGbyKeyVarRefs);
         }
 
         // Visits limit.
         if (selectExpression.hasLimit()) {
-            selectExpression.getLimitClause().accept(this, arg);
+            selectExpression.getLimitClause().accept(this, overwriteWithGbyKeyVarRefs);
         }
 
         // Exits the scope that were entered within this select expression
@@ -250,9 +295,9 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
     }
 
     @Override
-    public Void visit(LetClause letClause, Boolean rewrite) throws AsterixException {
+    public Void visit(LetClause letClause, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope().getVarSubstitutionEnvironment();
-        if (rewrite) {
+        if (overwriteWithGbyKeyVarRefs) {
             Expression newBindExpr = (Expression) SqlppVariableSubstitutionUtil
                     .substituteVariableWithoutContext(letClause.getBindingExpr(), env);
             letClause.setBindingExpr(newBindExpr);
@@ -264,26 +309,26 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
     }
 
     @Override
-    public Void visit(OrderbyClause oc, Boolean arg) throws AsterixException {
+    public Void visit(OrderbyClause oc, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope().getVarSubstitutionEnvironment();
         List<Expression> orderExprs = new ArrayList<Expression>();
         for (Expression orderExpr : oc.getOrderbyList()) {
             orderExprs.add((Expression) SqlppVariableSubstitutionUtil.substituteVariableWithoutContext(orderExpr, env));
-            orderExpr.accept(this, arg);
+            orderExpr.accept(this, overwriteWithGbyKeyVarRefs);
         }
         oc.setOrderbyList(orderExprs);
         return null;
     }
 
     @Override
-    public Void visit(GroupbyClause gc, Boolean arg) throws AsterixException {
+    public Void visit(GroupbyClause gc, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope().getVarSubstitutionEnvironment();
         Map<VariableExpr, VariableExpr> oldGbyExprsToNewGbyVarMap = new HashMap<>();
         for (GbyVariableExpressionPair gbyVarExpr : gc.getGbyPairList()) {
             Expression oldGbyExpr = gbyVarExpr.getExpr();
             Expression newExpr = (Expression) SqlppVariableSubstitutionUtil.substituteVariableWithoutContext(oldGbyExpr,
                     env);
-            newExpr.accept(this, arg);
+            newExpr.accept(this, overwriteWithGbyKeyVarRefs);
             gbyVarExpr.setExpr(newExpr);
             if (gbyVarExpr.getVar() == null) {
                 gbyVarExpr.setVar(new VariableExpr(context.newVariable()));
@@ -305,50 +350,50 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
     }
 
     @Override
-    public Void visit(LimitClause limitClause, Boolean arg) throws AsterixException {
-        limitClause.getLimitExpr().accept(this, arg);
+    public Void visit(LimitClause limitClause, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        limitClause.getLimitExpr().accept(this, overwriteWithGbyKeyVarRefs);
         return null;
     }
 
     @Override
-    public Void visit(HavingClause havingClause, Boolean arg) throws AsterixException {
+    public Void visit(HavingClause havingClause, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope().getVarSubstitutionEnvironment();
         Expression newFilterExpr = (Expression) SqlppVariableSubstitutionUtil
                 .substituteVariableWithoutContext(havingClause.getFilterExpression(), env);
-        newFilterExpr.accept(this, arg);
+        newFilterExpr.accept(this, overwriteWithGbyKeyVarRefs);
         havingClause.setFilterExpression(newFilterExpr);
         return null;
     }
 
     @Override
-    public Void visit(Query q, Boolean arg) throws AsterixException {
-        q.getBody().accept(this, arg);
+    public Void visit(Query q, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        q.getBody().accept(this, overwriteWithGbyKeyVarRefs);
         return null;
     }
 
     @Override
-    public Void visit(FunctionDecl fd, Boolean arg) throws AsterixException {
+    public Void visit(FunctionDecl fd, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         scopeChecker.createNewScope();
-        fd.getFuncBody().accept(this, arg);
+        fd.getFuncBody().accept(this, overwriteWithGbyKeyVarRefs);
         scopeChecker.removeCurrentScope();
         return null;
     }
 
     @Override
-    public Void visit(LiteralExpr l, Boolean arg) throws AsterixException {
+    public Void visit(LiteralExpr l, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         return null;
     }
 
     @Override
-    public Void visit(ListConstructor lc, Boolean arg) throws AsterixException {
+    public Void visit(ListConstructor lc, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         for (Expression expr : lc.getExprList()) {
-            expr.accept(this, arg);
+            expr.accept(this, overwriteWithGbyKeyVarRefs);
         }
         return null;
     }
 
     @Override
-    public Void visit(RecordConstructor rc, Boolean rewrite) throws AsterixException {
+    public Void visit(RecordConstructor rc, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         for (FieldBinding binding : rc.getFbList()) {
             binding.getLeftExpr().accept(this, false);
             binding.getRightExpr().accept(this, false);
@@ -356,87 +401,62 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
         return null;
     }
 
-    private void mapForRecordConstructor(Boolean initPhase, RecordConstructor rc) throws AsterixException {
-        for (FieldBinding binding : rc.getFbList()) {
-            Expression leftExpr = binding.getLeftExpr();
-            if (leftExpr.getKind() == Kind.LITERAL_EXPRESSION) {
-                LiteralExpr literalExpr = (LiteralExpr) leftExpr;
-                if (literalExpr.getValue().getLiteralType() == Literal.Type.STRING) {
-                    String fieldName = literalExpr.getValue().getStringValue();
-                    VariableExpr columnAlias = new VariableExpr(
-                            SqlppVariableUtil.toInternalVariableIdentifier(fieldName));
-                    VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope()
-                            .getVarSubstitutionEnvironment();
-                    if (initPhase) {
-                        scopeChecker.getCurrentScope().addSymbolExpressionMappingToScope(columnAlias,
-                                binding.getRightExpr());
-                    } else {
-                        Expression gbyKey = (Expression) SqlppRewriteUtil.deepCopy(env.findSubstituion(columnAlias));
-                        if (gbyKey != null) {
-                            binding.setRightExpr(gbyKey);
-                        }
-                    }
-                }
-            }
-        }
-    }
-
     @Override
-    public Void visit(OperatorExpr operatorExpr, Boolean arg) throws AsterixException {
+    public Void visit(OperatorExpr operatorExpr, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         for (Expression expr : operatorExpr.getExprList()) {
-            expr.accept(this, arg);
+            expr.accept(this, overwriteWithGbyKeyVarRefs);
         }
         return null;
     }
 
     @Override
-    public Void visit(IfExpr ifExpr, Boolean arg) throws AsterixException {
-        ifExpr.getCondExpr().accept(this, arg);
-        ifExpr.getThenExpr().accept(this, arg);
-        ifExpr.getElseExpr().accept(this, arg);
+    public Void visit(IfExpr ifExpr, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        ifExpr.getCondExpr().accept(this, overwriteWithGbyKeyVarRefs);
+        ifExpr.getThenExpr().accept(this, overwriteWithGbyKeyVarRefs);
+        ifExpr.getElseExpr().accept(this, overwriteWithGbyKeyVarRefs);
         return null;
     }
 
     @Override
-    public Void visit(QuantifiedExpression qe, Boolean arg) throws AsterixException {
+    public Void visit(QuantifiedExpression qe, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         for (QuantifiedPair pair : qe.getQuantifiedList()) {
-            pair.getExpr().accept(this, arg);
+            pair.getExpr().accept(this, overwriteWithGbyKeyVarRefs);
         }
-        qe.getSatisfiesExpr().accept(this, arg);
+        qe.getSatisfiesExpr().accept(this, overwriteWithGbyKeyVarRefs);
         return null;
     }
 
     @Override
-    public Void visit(CallExpr callExpr, Boolean arg) throws AsterixException {
+    public Void visit(CallExpr callExpr, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         for (Expression expr : callExpr.getExprList()) {
-            expr.accept(this, arg);
+            expr.accept(this, overwriteWithGbyKeyVarRefs);
         }
         return null;
     }
 
     @Override
-    public Void visit(VariableExpr varExpr, Boolean arg) throws AsterixException {
+    public Void visit(VariableExpr varExpr, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
         return null;
     }
 
     @Override
-    public Void visit(UnaryExpr u, Boolean arg) throws AsterixException {
-        u.getExpr().accept(this, arg);
+    public Void visit(UnaryExpr u, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        u.getExpr().accept(this, overwriteWithGbyKeyVarRefs);
         return null;
     }
 
     @Override
-    public Void visit(FieldAccessor fa, Boolean arg) throws AsterixException {
-        fa.getExpr().accept(this, arg);
+    public Void visit(FieldAccessor fa, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        fa.getExpr().accept(this, overwriteWithGbyKeyVarRefs);
         return null;
     }
 
     @Override
-    public Void visit(IndexAccessor ia, Boolean arg) throws AsterixException {
-        ia.getExpr().accept(this, arg);
+    public Void visit(IndexAccessor ia, Boolean overwriteWithGbyKeyVarRefs) throws AsterixException {
+        ia.getExpr().accept(this, overwriteWithGbyKeyVarRefs);
         Expression indexExpr = ia.getExpr();
         if (indexExpr != null) {
-            indexExpr.accept(this, arg);
+            indexExpr.accept(this, overwriteWithGbyKeyVarRefs);
         }
         return null;
     }