asterixdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From buyin...@apache.org
Subject [1/4] asterixdb git commit: Support SQL-compliant group-by syntax.
Date Thu, 14 Jul 2016 02:30:13 GMT
Repository: asterixdb
Updated Branches:
  refs/heads/master 6d999ed1a -> 5b2d4c89c


http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/DeepCopyVisitor.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/DeepCopyVisitor.java
b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/DeepCopyVisitor.java
index 559183e..6c48c64 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/DeepCopyVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/DeepCopyVisitor.java
@@ -139,7 +139,7 @@ public class DeepCopyVisitor extends AbstractSqlppQueryExpressionVisitor<ILangEx
         GroupbyClause gbyClause = null;
         List<LetClause> gbyLetClauses = new ArrayList<>();
         HavingClause havingClause = null;
-        SelectClause selectCluase = null;
+        SelectClause selectCluase;
         // Traverses the select block in the order of "from", "let"s, "where",
         // "group by", "let"s, "having" and "select".
         if (selectBlock.hasFromClause()) {
@@ -201,7 +201,7 @@ public class DeepCopyVisitor extends AbstractSqlppQueryExpressionVisitor<ILangEx
     @Override
     public SelectSetOperation visit(SelectSetOperation selectSetOperation, Void arg) throws
AsterixException {
         SetOperationInput leftInput = selectSetOperation.getLeftInput();
-        SetOperationInput newLeftInput = null;
+        SetOperationInput newLeftInput;
         if (leftInput.selectBlock()) {
             newLeftInput = new SetOperationInput((SelectBlock) leftInput.accept(this, arg),
null);
         } else {
@@ -209,12 +209,12 @@ public class DeepCopyVisitor extends AbstractSqlppQueryExpressionVisitor<ILangEx
         }
         List<SetOperationRight> rightInputs = new ArrayList<>();
         for (SetOperationRight right : selectSetOperation.getRightInputs()) {
-            SetOperationInput newRightInput = null;
+            SetOperationInput newRightInput;
             SetOperationInput setOpRightInput = right.getSetOperationRightInput();
             if (setOpRightInput.selectBlock()) {
-                newRightInput = new SetOperationInput((SelectBlock) leftInput.accept(this,
arg), null);
+                newRightInput = new SetOperationInput((SelectBlock) setOpRightInput.accept(this,
arg), null);
             } else {
-                newRightInput = new SetOperationInput(null, (SelectExpression) leftInput.accept(this,
arg));
+                newRightInput = new SetOperationInput(null, (SelectExpression) setOpRightInput.accept(this,
arg));
             }
             rightInputs.add(new SetOperationRight(right.getSetOpType(), right.isSetSemantics(),
newRightInput));
         }
@@ -244,7 +244,7 @@ public class DeepCopyVisitor extends AbstractSqlppQueryExpressionVisitor<ILangEx
 
     @Override
     public OrderbyClause visit(OrderbyClause oc, Void arg) throws AsterixException {
-        List<Expression> newOrderbyList = new ArrayList<Expression>();
+        List<Expression> newOrderbyList = new ArrayList<>();
         for (Expression orderExpr : oc.getOrderbyList()) {
             newOrderbyList.add((Expression) orderExpr.accept(this, arg));
         }
@@ -259,11 +259,13 @@ public class DeepCopyVisitor extends AbstractSqlppQueryExpressionVisitor<ILangEx
         VariableExpr groupVarExpr = null;
         List<Pair<Expression, Identifier>> groupFieldList = new ArrayList<>();
         for (GbyVariableExpressionPair gbyVarExpr : gc.getGbyPairList()) {
-            gbyPairList.add(new GbyVariableExpressionPair((VariableExpr) gbyVarExpr.getVar().accept(this,
arg),
+            VariableExpr var = gbyVarExpr.getVar();
+            gbyPairList.add(new GbyVariableExpressionPair(var == null ? null : (VariableExpr)
var.accept(this, arg),
                     (Expression) gbyVarExpr.getExpr().accept(this, arg)));
         }
         for (GbyVariableExpressionPair gbyVarExpr : gc.getDecorPairList()) {
-            decorPairList.add(new GbyVariableExpressionPair((VariableExpr) gbyVarExpr.getVar().accept(this,
arg),
+            VariableExpr var = gbyVarExpr.getVar();
+            decorPairList.add(new GbyVariableExpressionPair(var == null ? null : (VariableExpr)
var.accept(this, arg),
                     (Expression) gbyVarExpr.getExpr().accept(this, arg)));
         }
         for (VariableExpr withVar : gc.getWithVarList()) {
@@ -295,7 +297,7 @@ public class DeepCopyVisitor extends AbstractSqlppQueryExpressionVisitor<ILangEx
     @Override
     public SelectExpression visit(SelectExpression selectExpression, Void arg) throws AsterixException
{
         List<LetClause> lets = new ArrayList<>();
-        SelectSetOperation select = null;
+        SelectSetOperation select;
         OrderbyClause orderby = null;
         LimitClause limit = null;
 
@@ -332,7 +334,7 @@ public class DeepCopyVisitor extends AbstractSqlppQueryExpressionVisitor<ILangEx
 
     @Override
     public ListConstructor visit(ListConstructor lc, Void arg) throws AsterixException {
-        List<Expression> newExprList = new ArrayList<Expression>();
+        List<Expression> newExprList = new ArrayList<>();
         for (Expression expr : lc.getExprList()) {
             newExprList.add((Expression) expr.accept(this, arg));
         }
@@ -352,7 +354,7 @@ public class DeepCopyVisitor extends AbstractSqlppQueryExpressionVisitor<ILangEx
 
     @Override
     public OperatorExpr visit(OperatorExpr operatorExpr, Void arg) throws AsterixException
{
-        List<Expression> newExprList = new ArrayList<Expression>();
+        List<Expression> newExprList = new ArrayList<>();
         for (Expression expr : operatorExpr.getExprList()) {
             newExprList.add((Expression) expr.accept(this, arg));
         }
@@ -382,7 +384,7 @@ public class DeepCopyVisitor extends AbstractSqlppQueryExpressionVisitor<ILangEx
 
     @Override
     public CallExpr visit(CallExpr callExpr, Void arg) throws AsterixException {
-        List<Expression> newExprList = new ArrayList<Expression>();
+        List<Expression> newExprList = new ArrayList<>();
         for (Expression expr : callExpr.getExprList()) {
             newExprList.add((Expression) expr.accept(this, arg));
         }
@@ -391,8 +393,8 @@ public class DeepCopyVisitor extends AbstractSqlppQueryExpressionVisitor<ILangEx
 
     @Override
     public VariableExpr visit(VariableExpr varExpr, Void arg) throws AsterixException {
-        VariableExpr clonedVar = new VariableExpr(
-                new VarIdentifier(varExpr.getVar().getValue(), varExpr.getVar().getId()));
+        VariableExpr clonedVar =
+                new VariableExpr(new VarIdentifier(varExpr.getVar().getValue(), varExpr.getVar().getId()));
         clonedVar.setIsNewVar(varExpr.getIsNewVar());
         clonedVar.setNamedValueAccess(varExpr.namedValueAccess());
         return clonedVar;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppCloneAndSubstituteVariablesVisitor.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppCloneAndSubstituteVariablesVisitor.java
b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppCloneAndSubstituteVariablesVisitor.java
index 8ec2b04..445c835 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppCloneAndSubstituteVariablesVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppCloneAndSubstituteVariablesVisitor.java
@@ -68,7 +68,7 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
     public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FromClause
fromClause,
             VariableSubstitutionEnvironment env) throws AsterixException {
         VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
-        List<FromTerm> newFromTerms = new ArrayList<FromTerm>();
+        List<FromTerm> newFromTerms = new ArrayList<>();
         for (FromTerm fromTerm : fromClause.getFromTerms()) {
             Pair<ILangExpression, VariableSubstitutionEnvironment> p = fromTerm.accept(this,
currentEnv);
             newFromTerms.add((FromTerm) p.first);
@@ -76,7 +76,7 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
             // therefore we propagate the substitution environment.
             currentEnv = p.second;
         }
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(new FromClause(newFromTerms),
currentEnv);
+        return new Pair<>(new FromClause(newFromTerms), currentEnv);
     }
 
     @Override
@@ -87,7 +87,7 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
         VariableExpr newLeftPosVar = fromTerm.hasPositionalVariable()
                 ? generateNewVariable(context, fromTerm.getPositionalVariable()) : null;
         Expression newLeftExpr = (Expression) visitUnnesBindingExpression(fromTerm.getLeftExpression(),
env).first;
-        List<AbstractBinaryCorrelateClause> newCorrelateClauses = new ArrayList<AbstractBinaryCorrelateClause>();
+        List<AbstractBinaryCorrelateClause> newCorrelateClauses = new ArrayList<>();
 
         VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
         currentEnv.removeSubstitution(newLeftVar);
@@ -108,8 +108,7 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
                 newCorrelateClauses.add((AbstractBinaryCorrelateClause) correlateClause.accept(this,
env).first);
             }
         }
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(
-                new FromTerm(newLeftExpr, newLeftVar, newLeftPosVar, newCorrelateClauses),
currentEnv);
+        return new Pair<>(new FromTerm(newLeftExpr, newLeftVar, newLeftPosVar, newCorrelateClauses),
currentEnv);
     }
 
     @Override
@@ -132,9 +131,9 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
         // The condition can refer to the newRightVar and newRightPosVar.
         Expression conditionExpr = (Expression) joinClause.getConditionExpression().accept(this,
currentEnv).first;
 
-        JoinClause newJoinClause = new JoinClause(joinClause.getJoinType(), newRightExpr,
newRightVar, newRightPosVar,
-                conditionExpr);
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newJoinClause,
currentEnv);
+        JoinClause newJoinClause =
+                new JoinClause(joinClause.getJoinType(), newRightExpr, newRightVar, newRightPosVar,
conditionExpr);
+        return new Pair<>(newJoinClause, currentEnv);
     }
 
     @Override
@@ -157,9 +156,9 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
         // The condition can refer to the newRightVar and newRightPosVar.
         Expression conditionExpr = (Expression) nestClause.getConditionExpression().accept(this,
currentEnv).first;
 
-        NestClause newJoinClause = new NestClause(nestClause.getJoinType(), rightExpr, newRightVar,
newRightPosVar,
-                conditionExpr);
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newJoinClause,
currentEnv);
+        NestClause newJoinClause =
+                new NestClause(nestClause.getJoinType(), rightExpr, newRightVar, newRightPosVar,
conditionExpr);
+        return new Pair<>(newJoinClause, currentEnv);
     }
 
     @Override
@@ -180,9 +179,9 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
             currentEnv.removeSubstitution(newRightPosVar);
         }
         // The condition can refer to the newRightVar and newRightPosVar.
-        UnnestClause newJoinClause = new UnnestClause(unnestClause.getJoinType(), rightExpr,
newRightVar,
-                newRightPosVar);
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newJoinClause,
currentEnv);
+        UnnestClause newJoinClause =
+                new UnnestClause(unnestClause.getJoinType(), rightExpr, newRightVar, newRightPosVar);
+        return new Pair<>(newJoinClause, currentEnv);
     }
 
     @Override
@@ -190,20 +189,20 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
             VariableSubstitutionEnvironment env) throws AsterixException {
         Projection newProjection = new Projection((Expression) projection.getExpression().accept(this,
env).first,
                 projection.getName(), projection.star(), projection.exprStar());
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newProjection,
env);
+        return new Pair<>(newProjection, env);
     }
 
     @Override
     public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectBlock
selectBlock,
             VariableSubstitutionEnvironment env) throws AsterixException {
         Pair<ILangExpression, VariableSubstitutionEnvironment> newFrom = null;
-        Pair<ILangExpression, VariableSubstitutionEnvironment> newLet = null;
+        Pair<ILangExpression, VariableSubstitutionEnvironment> newLet;
         Pair<ILangExpression, VariableSubstitutionEnvironment> newWhere = null;
         Pair<ILangExpression, VariableSubstitutionEnvironment> newGroupby = null;
         Pair<ILangExpression, VariableSubstitutionEnvironment> newHaving = null;
-        Pair<ILangExpression, VariableSubstitutionEnvironment> newSelect = null;
-        List<LetClause> newLetClauses = new ArrayList<LetClause>();
-        List<LetClause> newLetClausesAfterGby = new ArrayList<LetClause>();
+        Pair<ILangExpression, VariableSubstitutionEnvironment> newSelect;
+        List<LetClause> newLetClauses = new ArrayList<>();
+        List<LetClause> newLetClausesAfterGby = new ArrayList<>();
         VariableSubstitutionEnvironment currentEnv = new VariableSubstitutionEnvironment(env);
 
         if (selectBlock.hasFromClause()) {
@@ -243,12 +242,12 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
 
         newSelect = selectBlock.getSelectClause().accept(this, currentEnv);
         currentEnv = newSelect.second;
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(
-                new SelectBlock((SelectClause) newSelect.first, newFrom == null ? null :
(FromClause) newFrom.first,
-                        newLetClauses, newWhere == null ? null : (WhereClause) newWhere.first,
-                        newGroupby == null ? null : (GroupbyClause) newGroupby.first, newLetClausesAfterGby,
-                        newHaving == null ? null : (HavingClause) newHaving.first),
-                currentEnv);
+        FromClause fromClause = newFrom == null ? null : (FromClause) newFrom.first;
+        WhereClause whereClause = newWhere == null ? null : (WhereClause) newWhere.first;
+        GroupbyClause groupbyClause = newGroupby == null ? null : (GroupbyClause) newGroupby.first;
+        HavingClause havingClause = newHaving == null ? null : (HavingClause) newHaving.first;
+        return new Pair<>(new SelectBlock((SelectClause) newSelect.first, fromClause,
newLetClauses, whereClause,
+                groupbyClause, newLetClausesAfterGby, havingClause), currentEnv);
     }
 
     @Override
@@ -256,42 +255,41 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
             VariableSubstitutionEnvironment env) throws AsterixException {
         boolean distinct = selectClause.distinct();
         if (selectClause.selectElement()) {
-            Pair<ILangExpression, VariableSubstitutionEnvironment> newSelectElement
= selectClause.getSelectElement()
-                    .accept(this, env);
-            return new Pair<ILangExpression, VariableSubstitutionEnvironment>(
-                    new SelectClause((SelectElement) newSelectElement.first, null, distinct),
newSelectElement.second);
+            Pair<ILangExpression, VariableSubstitutionEnvironment> newSelectElement
=
+                    selectClause.getSelectElement().accept(this, env);
+            return new Pair<>(new SelectClause((SelectElement) newSelectElement.first,
null, distinct),
+                    newSelectElement.second);
         } else {
-            Pair<ILangExpression, VariableSubstitutionEnvironment> newSelectRegular
= selectClause.getSelectRegular()
-                    .accept(this, env);
-            return new Pair<ILangExpression, VariableSubstitutionEnvironment>(
-                    new SelectClause(null, (SelectRegular) newSelectRegular.first, distinct),
newSelectRegular.second);
+            Pair<ILangExpression, VariableSubstitutionEnvironment> newSelectRegular
=
+                    selectClause.getSelectRegular().accept(this, env);
+            return new Pair<>(new SelectClause(null, (SelectRegular) newSelectRegular.first,
distinct),
+                    newSelectRegular.second);
         }
     }
 
     @Override
     public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectElement
selectElement,
             VariableSubstitutionEnvironment env) throws AsterixException {
-        Pair<ILangExpression, VariableSubstitutionEnvironment> newExpr = selectElement.getExpression().accept(this,
-                env);
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(new SelectElement((Expression)
newExpr.first),
-                newExpr.second);
+        Pair<ILangExpression, VariableSubstitutionEnvironment> newExpr =
+                selectElement.getExpression().accept(this, env);
+        return new Pair<>(new SelectElement((Expression) newExpr.first), newExpr.second);
     }
 
     @Override
     public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectRegular
selectRegular,
             VariableSubstitutionEnvironment env) throws AsterixException {
-        List<Projection> newProjections = new ArrayList<Projection>();
+        List<Projection> newProjections = new ArrayList<>();
         for (Projection projection : selectRegular.getProjections()) {
             newProjections.add((Projection) projection.accept(this, env).first);
         }
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(new SelectRegular(newProjections),
env);
+        return new Pair<>(new SelectRegular(newProjections), env);
     }
 
     @Override
     public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectSetOperation
selectSetOperation,
             VariableSubstitutionEnvironment env) throws AsterixException {
         SetOperationInput leftInput = selectSetOperation.getLeftInput();
-        SetOperationInput newLeftInput = null;
+        SetOperationInput newLeftInput;
 
         // Sets the left input.
         if (leftInput.selectBlock()) {
@@ -303,13 +301,13 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
         }
 
         // Sets the right input
-        List<SetOperationRight> newRightInputs = new ArrayList<SetOperationRight>();
+        List<SetOperationRight> newRightInputs = new ArrayList<>();
         for (SetOperationRight right : selectSetOperation.getRightInputs()) {
-            SetOperationInput newRightInput = null;
+            SetOperationInput newRightInput;
             SetOperationInput rightInput = right.getSetOperationRightInput();
             if (rightInput.selectBlock()) {
-                Pair<ILangExpression, VariableSubstitutionEnvironment> p = rightInput.getSelectBlock().accept(this,
-                        env);
+                Pair<ILangExpression, VariableSubstitutionEnvironment> p =
+                        rightInput.getSelectBlock().accept(this, env);
                 newRightInput = new SetOperationInput((SelectBlock) p.first, null);
             } else {
                 Pair<ILangExpression, VariableSubstitutionEnvironment> p = rightInput.getSubquery().accept(this,
env);
@@ -318,20 +316,20 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
             newRightInputs.add(new SetOperationRight(right.getSetOpType(), right.isSetSemantics(),
newRightInput));
         }
         SelectSetOperation newSelectSetOperation = new SelectSetOperation(newLeftInput, newRightInputs);
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newSelectSetOperation,
env);
+        return new Pair<>(newSelectSetOperation, env);
     }
 
     @Override
     public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(SelectExpression
selectExpression,
             VariableSubstitutionEnvironment env) throws AsterixException {
         boolean subquery = selectExpression.isSubquery();
-        List<LetClause> newLetList = new ArrayList<LetClause>();
-        SelectSetOperation newSelectSetOperation = null;
+        List<LetClause> newLetList = new ArrayList<>();
+        SelectSetOperation newSelectSetOperation;
         OrderbyClause newOrderbyClause = null;
         LimitClause newLimitClause = null;
 
         VariableSubstitutionEnvironment currentEnv = env;
-        Pair<ILangExpression, VariableSubstitutionEnvironment> p = null;
+        Pair<ILangExpression, VariableSubstitutionEnvironment> p;
         if (selectExpression.hasLetClauses()) {
             for (LetClause letClause : selectExpression.getLetList()) {
                 p = letClause.accept(this, currentEnv);
@@ -355,7 +353,7 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
             newLimitClause = (LimitClause) p.first;
             currentEnv = p.second;
         }
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(
+        return new Pair<>(
                 new SelectExpression(newLetList, newSelectSetOperation, newOrderbyClause,
newLimitClause, subquery),
                 currentEnv);
     }
@@ -365,7 +363,7 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
             VariableSubstitutionEnvironment env) throws AsterixException {
         Pair<ILangExpression, VariableSubstitutionEnvironment> p = havingClause.getFilterExpression().accept(this,
env);
         HavingClause newHavingClause = new HavingClause((Expression) p.first);
-        return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newHavingClause,
p.second);
+        return new Pair<>(newHavingClause, p.second);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppFormatPrintVisitor.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppFormatPrintVisitor.java
b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppFormatPrintVisitor.java
index e03d711..2dca532 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppFormatPrintVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppFormatPrintVisitor.java
@@ -127,7 +127,10 @@ public class SqlppFormatPrintVisitor extends FormatPrintVisitor implements
ISqlp
     @Override
     public Void visit(Projection projection, Integer step) throws AsterixException {
         projection.getExpression().accept(this, step);
-        out.print(" as " + projection.getName());
+        String name = projection.getName();
+        if (name != null) {
+            out.print(" as " + name);
+        }
         return null;
     }
 

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteExpressionsVisitor.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteExpressionsVisitor.java
b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteExpressionsVisitor.java
new file mode 100644
index 0000000..af160bf
--- /dev/null
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteExpressionsVisitor.java
@@ -0,0 +1,272 @@
+/*
+ * 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.
+ */
+
+package org.apache.asterix.lang.sqlpp.visitor;
+
+import java.util.List;
+
+import org.apache.asterix.common.exceptions.AsterixException;
+import org.apache.asterix.lang.common.base.Expression;
+import org.apache.asterix.lang.common.clause.GroupbyClause;
+import org.apache.asterix.lang.common.clause.LetClause;
+import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
+import org.apache.asterix.lang.common.rewrites.ExpressionSubstitutionEnvironment;
+import org.apache.asterix.lang.common.visitor.SubstituteExpressionVisitor;
+import org.apache.asterix.lang.sqlpp.clause.AbstractBinaryCorrelateClause;
+import org.apache.asterix.lang.sqlpp.clause.FromClause;
+import org.apache.asterix.lang.sqlpp.clause.FromTerm;
+import org.apache.asterix.lang.sqlpp.clause.HavingClause;
+import org.apache.asterix.lang.sqlpp.clause.JoinClause;
+import org.apache.asterix.lang.sqlpp.clause.NestClause;
+import org.apache.asterix.lang.sqlpp.clause.Projection;
+import org.apache.asterix.lang.sqlpp.clause.SelectBlock;
+import org.apache.asterix.lang.sqlpp.clause.SelectClause;
+import org.apache.asterix.lang.sqlpp.clause.SelectElement;
+import org.apache.asterix.lang.sqlpp.clause.SelectRegular;
+import org.apache.asterix.lang.sqlpp.clause.SelectSetOperation;
+import org.apache.asterix.lang.sqlpp.clause.UnnestClause;
+import org.apache.asterix.lang.sqlpp.expression.IndependentSubquery;
+import org.apache.asterix.lang.sqlpp.expression.SelectExpression;
+import org.apache.asterix.lang.sqlpp.struct.SetOperationRight;
+import org.apache.asterix.lang.sqlpp.util.SqlppRewriteUtil;
+import org.apache.asterix.lang.sqlpp.visitor.base.ISqlppVisitor;
+
+public class SqlppSubstituteExpressionsVisitor extends SubstituteExpressionVisitor
+        implements ISqlppVisitor<Expression, ExpressionSubstitutionEnvironment> {
+
+    public SqlppSubstituteExpressionsVisitor() {
+        super(SqlppRewriteUtil::deepCopy);
+    }
+
+    @Override
+    public Expression visit(GroupbyClause gc, ExpressionSubstitutionEnvironment env) throws
AsterixException {
+        for (GbyVariableExpressionPair pair : gc.getGbyPairList()) {
+            pair.setExpr(pair.getExpr().accept(this, env));
+        }
+        // Forces from binding variables to exit their scopes, i.e.,
+        // one can still replace from binding variables.
+        env.pop();
+        for (GbyVariableExpressionPair pair : gc.getGbyPairList()) {
+            env.disableVariable(pair.getVar());
+        }
+        return null;
+    }
+
+    @Override
+    public Expression visit(FromClause fromClause, ExpressionSubstitutionEnvironment env)
throws AsterixException {
+        // Marks the states before the from clause, and a consequent
+        // group-by clause will reset env to the state.
+        env.mark();
+        for (FromTerm fromTerm : fromClause.getFromTerms()) {
+            fromTerm.accept(this, env);
+            // From terms are correlated and thus we mask binding variables after
+            // visiting each individual from term.
+            env.disableVariable(fromTerm.getLeftVariable());
+            if (fromTerm.hasPositionalVariable()) {
+                env.disableVariable(fromTerm.getLeftVariable());
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public Expression visit(FromTerm fromTerm, ExpressionSubstitutionEnvironment env) throws
AsterixException {
+        fromTerm.setLeftExpression(fromTerm.getLeftExpression().accept(this, env));
+        if (fromTerm.hasCorrelateClauses()) {
+            for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses())
{
+                correlateClause.accept(this, env);
+            }
+            // correlate clauses are independent and thus we mask their binding variables
+            // after visiting them.
+            for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses())
{
+                env.disableVariable(correlateClause.getRightVariable());
+                if (correlateClause.hasPositionalVariable()) {
+                    env.disableVariable(correlateClause.getPositionalVariable());
+                }
+            }
+        }
+        return null;
+    }
+
+    @Override
+    public Expression visit(JoinClause joinClause, ExpressionSubstitutionEnvironment env)
throws AsterixException {
+        joinClause.setRightExpression(joinClause.getRightExpression().accept(this, env));
+        // Condition expressions can see the join binding variables, thus we have to mask
them for replacement.
+        env.disableVariable(joinClause.getRightVariable());
+        if (joinClause.hasPositionalVariable()) {
+            env.disableVariable(joinClause.getPositionalVariable());
+        }
+        joinClause.setConditionExpression(joinClause.getConditionExpression().accept(this,
env));
+        // Re-enable them.
+        env.enableVariable(joinClause.getRightVariable());
+        if (joinClause.hasPositionalVariable()) {
+            env.enableVariable(joinClause.getPositionalVariable());
+        }
+        return null;
+    }
+
+    @Override
+    public Expression visit(NestClause nestClause, ExpressionSubstitutionEnvironment env)
throws AsterixException {
+        nestClause.setRightExpression(nestClause.getRightExpression().accept(this, env));
+        // Condition expressions can see the join binding variables, thus we have to mask
them for replacement.
+        env.disableVariable(nestClause.getRightVariable());
+        if (nestClause.hasPositionalVariable()) {
+            env.disableVariable(nestClause.getPositionalVariable());
+        }
+        nestClause.setConditionExpression(nestClause.getConditionExpression().accept(this,
env));
+        // Re-enable them.
+        env.enableVariable(nestClause.getRightVariable());
+        if (nestClause.hasPositionalVariable()) {
+            env.enableVariable(nestClause.getPositionalVariable());
+        }
+        return null;
+    }
+
+    @Override
+    public Expression visit(Projection projection, ExpressionSubstitutionEnvironment env)
throws AsterixException {
+        if (!projection.star()) {
+            projection.setExpression(projection.getExpression().accept(this, env));
+        }
+        return null;
+    }
+
+    @Override
+    public Expression visit(SelectBlock selectBlock, ExpressionSubstitutionEnvironment env)
throws AsterixException {
+        // Traverses the select block in the order of "from", "let"s, "where",
+        // "group by", "let"s, "having" and "select".
+        if (selectBlock.hasFromClause()) {
+            selectBlock.getFromClause().accept(this, env);
+        }
+        if (selectBlock.hasLetClauses()) {
+            List<LetClause> letList = selectBlock.getLetList();
+            for (LetClause letClause : letList) {
+                letClause.accept(this, env);
+            }
+        }
+        if (selectBlock.hasWhereClause()) {
+            selectBlock.getWhereClause().accept(this, env);
+        }
+        if (selectBlock.hasGroupbyClause()) {
+            selectBlock.getGroupbyClause().accept(this, env);
+        }
+        if (selectBlock.hasLetClausesAfterGroupby()) {
+            List<LetClause> letListAfterGby = selectBlock.getLetListAfterGroupby();
+            for (LetClause letClauseAfterGby : letListAfterGby) {
+                letClauseAfterGby.accept(this, env);
+            }
+        }
+        if (selectBlock.hasHavingClause()) {
+            selectBlock.getHavingClause().accept(this, env);
+        }
+        selectBlock.getSelectClause().accept(this, env);
+        return null;
+    }
+
+    @Override
+    public Expression visit(SelectClause selectClause, ExpressionSubstitutionEnvironment
env) throws AsterixException {
+        if (selectClause.selectElement()) {
+            selectClause.getSelectElement().accept(this, env);
+        } else {
+            selectClause.getSelectRegular().accept(this, env);
+        }
+        return null;
+    }
+
+    @Override
+    public Expression visit(SelectElement selectElement, ExpressionSubstitutionEnvironment
env)
+            throws AsterixException {
+        selectElement.setExpression(selectElement.getExpression().accept(this, env));
+        return null;
+    }
+
+    @Override
+    public Expression visit(SelectRegular selectRegular, ExpressionSubstitutionEnvironment
env)
+            throws AsterixException {
+        for (Projection projection : selectRegular.getProjections()) {
+            projection.accept(this, env);
+        }
+        return null;
+    }
+
+    @Override
+    public Expression visit(SelectSetOperation selectSetOperation, ExpressionSubstitutionEnvironment
env)
+            throws AsterixException {
+        selectSetOperation.getLeftInput().accept(this, env);
+        for (SetOperationRight right : selectSetOperation.getRightInputs()) {
+            right.getSetOperationRightInput().accept(this, env);
+        }
+        return null;
+    }
+
+    @Override
+    public Expression visit(SelectExpression selectExpression, ExpressionSubstitutionEnvironment
env)
+            throws AsterixException {
+        // Backups the current states of env and the end of this method will reset to the
returned depth.
+        int depth = env.mark();
+        // visit let list
+        if (selectExpression.hasLetClauses()) {
+            for (LetClause letClause : selectExpression.getLetList()) {
+                letClause.accept(this, env);
+            }
+        }
+
+        // visit the main select.
+        selectExpression.getSelectSetOperation().accept(this, env);
+
+        // visit order by.
+        if (selectExpression.hasOrderby()) {
+            selectExpression.getOrderbyClause().accept(this, env);
+        }
+
+        // visit limit
+        if (selectExpression.hasLimit()) {
+            selectExpression.getLimitClause().accept(this, env);
+        }
+
+        // re-enable the replacements all of all binding variables in the current scope.
+        if (selectExpression.hasLetClauses()) {
+            for (LetClause letClause : selectExpression.getLetList()) {
+                env.enableVariable(letClause.getVarExpr());
+            }
+        }
+        // Restores the states of env to be the same as the beginning .
+        env.reset(depth);
+        return selectExpression;
+    }
+
+    @Override
+    public Expression visit(UnnestClause unnestClause, ExpressionSubstitutionEnvironment
env) throws AsterixException {
+        unnestClause.setRightExpression(unnestClause.getRightExpression().accept(this, env));
+        return null;
+    }
+
+    @Override
+    public Expression visit(HavingClause havingClause, ExpressionSubstitutionEnvironment
env) throws AsterixException {
+        havingClause.setFilterExpression(havingClause.getFilterExpression().accept(this,
env));
+        return null;
+    }
+
+    @Override
+    public Expression visit(IndependentSubquery independentSubquery, ExpressionSubstitutionEnvironment
env)
+            throws AsterixException {
+        independentSubquery.setExpr(independentSubquery.getExpr().accept(this, env));
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppExpressionScopingVisitor.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppExpressionScopingVisitor.java
b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppExpressionScopingVisitor.java
index dfb15b0..4fbb697 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppExpressionScopingVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppExpressionScopingVisitor.java
@@ -60,8 +60,8 @@ import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
 
 public class AbstractSqlppExpressionScopingVisitor extends AbstractSqlppSimpleExpressionVisitor
{
 
-    protected final FunctionSignature resolveFunction = new FunctionSignature(MetadataConstants.METADATA_DATAVERSE_NAME,
-            "resolve", FunctionIdentifier.VARARGS);
+    protected final FunctionSignature resolveFunction =
+            new FunctionSignature(MetadataConstants.METADATA_DATAVERSE_NAME, "resolve", FunctionIdentifier.VARARGS);
     protected final ScopeChecker scopeChecker = new ScopeChecker();
     protected final LangRewritingContext context;
 

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppSimpleExpressionVisitor.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppSimpleExpressionVisitor.java
b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppSimpleExpressionVisitor.java
index 2098156..9fd5a6e 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppSimpleExpressionVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppSimpleExpressionVisitor.java
@@ -250,9 +250,7 @@ public class AbstractSqlppSimpleExpressionVisitor
 
         // visit order by
         if (selectExpression.hasOrderby()) {
-            for (Expression orderExpr : selectExpression.getOrderbyClause().getOrderbyList())
{
-                orderExpr.accept(this, selectExpression);
-            }
+            selectExpression.getOrderbyClause().accept(this, selectExpression);
         }
 
         // visit limit

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj b/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
index dff69ab..06b067e 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
+++ b/asterixdb/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
@@ -2376,7 +2376,10 @@ Projection Projection() throws ParseException: {
   )
   {
     if(!star && name == null){
-       name = SqlppVariableUtil.toUserDefinedName(ExpressionToVariableUtil.getGeneratedIdentifier(expr));
+       String generatedColumnIdentifier = ExpressionToVariableUtil.getGeneratedIdentifier(expr,
false);
+       if(generatedColumnIdentifier != null){
+            name = SqlppVariableUtil.toUserDefinedName(generatedColumnIdentifier);
+       }
     }
     return new Projection(expr, name, star, exprStar);
   }
@@ -2422,7 +2425,7 @@ FromTerm FromTerm() throws ParseException :
   )*
   {
     if(leftVar==null){
-        leftVar = ExpressionToVariableUtil.getGeneratedVariable(leftExpr);
+        leftVar = ExpressionToVariableUtil.getGeneratedVariable(leftExpr, true);
     }
     return new FromTerm(leftExpr, leftVar, posVar, correlateClauses);
   }
@@ -2439,7 +2442,7 @@ JoinClause JoinClause(JoinType joinType) throws ParseException :
   <JOIN> rightExpr = Expression() ((<AS>)? rightVar = Variable())? (<AT>
posVar = Variable())? <ON> conditionExpr = Expression()
   {
     if(rightVar==null){
-        rightVar = ExpressionToVariableUtil.getGeneratedVariable(rightExpr);
+        rightVar = ExpressionToVariableUtil.getGeneratedVariable(rightExpr, true);
     }
     return new JoinClause(joinType, rightExpr, rightVar, posVar, conditionExpr);
   }
@@ -2455,7 +2458,7 @@ UnnestClause UnnestClause(JoinType joinType) throws ParseException :
   (<UNNEST>|<CORRELATE>|<FLATTEN>) rightExpr = Expression() ((<AS>)?
rightVar = Variable()) (<AT> posVar = Variable())?
   {
     if(rightVar==null){
-        rightVar = ExpressionToVariableUtil.getGeneratedVariable(rightExpr);
+        rightVar = ExpressionToVariableUtil.getGeneratedVariable(rightExpr, true);
     }
     return new UnnestClause(joinType, rightExpr, rightVar, posVar);
   }
@@ -2586,7 +2589,7 @@ GroupbyClause GroupbyClause()throws ParseException :
         )?
         {
             if(var==null){
-                var = ExpressionToVariableUtil.getGeneratedVariable(expr);
+                var = ExpressionToVariableUtil.getGeneratedVariable(expr, false);
             }
             GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
             vePairList.add(pair1);
@@ -2601,7 +2604,7 @@ GroupbyClause GroupbyClause()throws ParseException :
          )?
          {
              if(var==null){
-                var = ExpressionToVariableUtil.getGeneratedVariable(expr);
+                var = ExpressionToVariableUtil.getGeneratedVariable(expr, false);
              }
              GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
              vePairList.add(pair2);


Mime
View raw message