You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@asterixdb.apache.org by im...@apache.org on 2016/04/07 16:59:44 UTC

[09/50] [abbrv] incubator-asterixdb git commit: Merge branch 'master' into hyracks-merge2

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/8516517e/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableUtil.java
----------------------------------------------------------------------
diff --cc asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableUtil.java
index 8a63aa5,0000000..59e9389
mode 100644,000000..100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableUtil.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableUtil.java
@@@ -1,47 -1,0 +1,131 @@@
 +/*
 + * 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.util;
 +
++import java.util.Collection;
++import java.util.HashSet;
++import java.util.List;
++import java.util.Set;
++
++import org.apache.asterix.common.exceptions.AsterixException;
++import org.apache.asterix.lang.common.base.ILangExpression;
++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.expression.VariableExpr;
 +import org.apache.asterix.lang.common.struct.VarIdentifier;
++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.visitor.FreeVariableVisitor;
 +
 +public class SqlppVariableUtil {
 +
 +    private static String USER_VAR_PREFIX = "$";
 +
 +    public static VarIdentifier toUserDefinedVariableName(VarIdentifier var) {
 +        String varName = var.getValue();
 +        return toUserDefinedVariableName(varName);
 +    }
 +
 +    public static VarIdentifier toUserDefinedVariableName(String varName) {
 +        if (varName.startsWith(USER_VAR_PREFIX)) {
 +            return new VarIdentifier(varName.substring(1));
 +        }
 +        return new VarIdentifier(varName);
 +    }
 +
 +    public static String toInternalVariableName(String varName) {
 +        return USER_VAR_PREFIX + varName;
 +    }
 +
 +    public static VarIdentifier toInternalVariableIdentifier(String idName) {
 +        return new VarIdentifier(USER_VAR_PREFIX + idName);
 +    }
 +
++    public static Collection<VariableExpr> getFreeVariables(ILangExpression langExpr) throws AsterixException {
++        Collection<VariableExpr> freeVars = new HashSet<>();
++        FreeVariableVisitor visitor = new FreeVariableVisitor();
++        langExpr.accept(visitor, freeVars);
++        return freeVars;
++    }
++
++    public static Collection<VariableExpr> getBindingVariables(FromClause fromClause) {
++        Set<VariableExpr> bindingVars = new HashSet<>();
++        if (fromClause == null) {
++            return bindingVars;
++        }
++        for (FromTerm fromTerm : fromClause.getFromTerms()) {
++            bindingVars.addAll(getBindingVariables(fromTerm));
++        }
++        return bindingVars;
++    }
++
++    public static Collection<VariableExpr> getBindingVariables(FromTerm fromTerm) {
++        Set<VariableExpr> bindingVars = new HashSet<>();
++        if (fromTerm == null) {
++            return bindingVars;
++        }
++        bindingVars.add(fromTerm.getLeftVariable());
++        if (fromTerm.hasPositionalVariable()) {
++            bindingVars.add(fromTerm.getPositionalVariable());
++        }
++        for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
++            bindingVars.add(correlateClause.getRightVariable());
++            if (correlateClause.hasPositionalVariable()) {
++                bindingVars.add(correlateClause.getPositionalVariable());
++            }
++        }
++        return bindingVars;
++    }
++
++    public static Collection<VariableExpr> getBindingVariables(GroupbyClause gbyClause) {
++        Set<VariableExpr> bindingVars = new HashSet<>();
++        if (gbyClause == null) {
++            return bindingVars;
++        }
++        for (GbyVariableExpressionPair gbyKey : gbyClause.getGbyPairList()) {
++            VariableExpr var = gbyKey.getVar();
++            if (var != null) {
++                bindingVars.add(var);
++            }
++        }
++        for (GbyVariableExpressionPair gbyKey : gbyClause.getDecorPairList()) {
++            VariableExpr var = gbyKey.getVar();
++            if (var != null) {
++                bindingVars.add(var);
++            }
++        }
++        bindingVars.addAll(gbyClause.getWithVarList());
++        bindingVars.add(gbyClause.getGroupVar());
++        return bindingVars;
++    }
++
++    public static Collection<VariableExpr> getBindingVariables(List<LetClause> letClauses) {
++        Set<VariableExpr> bindingVars = new HashSet<>();
++        if (letClauses == null || letClauses.isEmpty()) {
++            return bindingVars;
++        }
++        for (LetClause letClause : letClauses) {
++            bindingVars.add(letClause.getVarExpr());
++        }
++        return bindingVars;
++    }
++
 +}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/8516517e/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitor.java
----------------------------------------------------------------------
diff --cc asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitor.java
index df32b01,0000000..0f36646
mode 100644,000000..100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitor.java
@@@ -1,271 -1,0 +1,295 @@@
 +/*
 + * 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.io.PrintWriter;
 +
 +import org.apache.asterix.common.exceptions.AsterixException;
++import org.apache.asterix.common.functions.FunctionSignature;
 +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.CallExpr;
 +import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
 +import org.apache.asterix.lang.common.struct.Identifier;
 +import org.apache.asterix.lang.common.visitor.QueryPrintVisitor;
 +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.SelectExpression;
 +import org.apache.asterix.lang.sqlpp.struct.SetOperationRight;
++import org.apache.asterix.lang.sqlpp.util.FunctionMapUtil;
 +import org.apache.asterix.lang.sqlpp.visitor.base.ISqlppVisitor;
++import org.apache.asterix.om.functions.AsterixBuiltinFunctions;
 +import org.apache.hyracks.algebricks.common.utils.Pair;
 +
 +public class SqlppAstPrintVisitor extends QueryPrintVisitor implements ISqlppVisitor<Void, Integer> {
 +
 +    private final PrintWriter out;
 +
 +    public SqlppAstPrintVisitor() {
 +        super();
 +        out = new PrintWriter(System.out);
 +    }
 +
 +    public SqlppAstPrintVisitor(PrintWriter out) {
 +        super(out);
 +        this.out = out;
 +    }
 +
 +    @Override
 +    public Void visit(FromClause fromClause, Integer step) throws AsterixException {
 +        out.print(skip(step) + "FROM [");
 +        int index = 0;
 +        for (FromTerm fromTerm : fromClause.getFromTerms()) {
 +            if (index > 0) {
 +                out.println(",");
 +            }
 +            fromTerm.accept(this, step + 1);
 +            ++index;
 +        }
 +        out.println(skip(step) + "]");
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(FromTerm fromTerm, Integer step) throws AsterixException {
 +        fromTerm.getLeftExpression().accept(this, step);
 +        out.println(skip(step) + "AS");
 +        fromTerm.getLeftVariable().accept(this, step);
 +        if (fromTerm.hasPositionalVariable()) {
 +            out.println(skip(step) + "AT");
 +            fromTerm.getPositionalVariable().accept(this, step);
 +        }
 +        if (fromTerm.hasCorrelateClauses()) {
 +            for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
 +                correlateClause.accept(this, step);
 +            }
 +        }
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(JoinClause joinClause, Integer step) throws AsterixException {
 +        out.println(skip(step) + joinClause.getJoinType() + " JOIN");
 +        joinClause.getRightExpression().accept(this, step + 1);
 +        out.println(skip(step + 1) + "AS");
 +        joinClause.getRightVariable().accept(this, step + 1);
 +        if (joinClause.hasPositionalVariable()) {
 +            out.println(skip(step + 1) + "AT");
 +            joinClause.getPositionalVariable().accept(this, step + 1);
 +        }
 +        out.println(skip(step + 1) + "ON");
 +        joinClause.getConditionExpression().accept(this, step + 1);
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(NestClause nestClause, Integer step) throws AsterixException {
 +        out.println(skip(step) + nestClause.getJoinType() + " NEST");
 +        nestClause.getRightExpression().accept(this, step + 1);
 +        out.println(skip(step + 1) + "AS");
 +        nestClause.getRightVariable().accept(this, step + 1);
 +        if (nestClause.hasPositionalVariable()) {
 +            out.println(skip(step + 1) + "AT");
 +            nestClause.getPositionalVariable().accept(this, step + 1);
 +        }
 +        out.println(skip(step + 1) + "ON");
 +        nestClause.getConditionExpression().accept(this, step + 1);
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(Projection projection, Integer step) throws AsterixException {
 +        projection.getExpression().accept(this, step);
 +        out.println(skip(step) + projection.getName());
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(SelectBlock selectBlock, Integer step) throws AsterixException {
 +        selectBlock.getSelectClause().accept(this, step);
 +        if (selectBlock.hasFromClause()) {
 +            selectBlock.getFromClause().accept(this, step);
 +        }
 +        if (selectBlock.hasLetClauses()) {
 +            for (LetClause letClause : selectBlock.getLetList()) {
 +                letClause.accept(this, step);
 +            }
 +        }
 +        if (selectBlock.hasWhereClause()) {
 +            selectBlock.getWhereClause().accept(this, step);
 +        }
 +        if (selectBlock.hasGroupbyClause()) {
 +            selectBlock.getGroupbyClause().accept(this, step);
 +            if (selectBlock.hasLetClausesAfterGroupby()) {
 +                for (LetClause letClause : selectBlock.getLetListAfterGroupby()) {
 +                    letClause.accept(this, step);
 +                }
 +            }
 +        }
 +        if (selectBlock.hasHavingClause()) {
 +            selectBlock.getHavingClause().accept(this, step);
 +        }
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(SelectClause selectClause, Integer step) throws AsterixException {
 +        if (selectClause.selectRegular()) {
 +            selectClause.getSelectRegular().accept(this, step);
 +        }
 +        if (selectClause.selectElement()) {
 +            selectClause.getSelectElement().accept(this, step);
 +        }
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(SelectElement selectElement, Integer step) throws AsterixException {
 +        out.println(skip(step) + "SELECT ELEMENT [");
 +        selectElement.getExpression().accept(this, step);
 +        out.println(skip(step) + "]");
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(SelectRegular selectRegular, Integer step) throws AsterixException {
 +        out.println(skip(step) + "SELECT [");
 +        for (Projection projection : selectRegular.getProjections()) {
 +            projection.accept(this, step);
 +        }
 +        out.println(skip(step) + "]");
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(SelectSetOperation selectSetOperation, Integer step) throws AsterixException {
 +        selectSetOperation.getLeftInput().accept(this, step);
 +        if (selectSetOperation.hasRightInputs()) {
 +            for (SetOperationRight right : selectSetOperation.getRightInputs()) {
 +                String all = right.isSetSemantics() ? " ALL " : "";
 +                out.println(skip(step) + right.getSetOpType() + all);
 +                right.getSetOperationRightInput().accept(this, step + 1);
 +            }
 +        }
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(SelectExpression selectStatement, Integer step) throws AsterixException {
 +        if (selectStatement.isSubquery()) {
 +            out.println(skip(step) + "(");
 +        }
 +        int selectStep = selectStatement.isSubquery() ? step + 1 : step;
 +        if (selectStatement.hasLetClauses()) {
 +            for (LetClause letClause : selectStatement.getLetList()) {
 +                letClause.accept(this, selectStep);
 +            }
 +        }
 +        selectStatement.getSelectSetOperation().accept(this, selectStep);
 +        if (selectStatement.hasOrderby()) {
 +            selectStatement.getOrderbyClause().accept(this, selectStep);
 +        }
 +        if (selectStatement.hasLimit()) {
 +            selectStatement.getLimitClause().accept(this, selectStep);
 +        }
 +        if (selectStatement.isSubquery()) {
 +            out.println(skip(step) + ")");
 +        }
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(UnnestClause unnestClause, Integer step) throws AsterixException {
 +        out.println(skip(step) + unnestClause.getJoinType() + " UNNEST");
 +        unnestClause.getRightExpression().accept(this, step + 1);
 +        out.println(skip(step + 1) + " AS");
 +        unnestClause.getRightVariable().accept(this, step + 1);
 +        if (unnestClause.hasPositionalVariable()) {
 +            out.println(skip(step + 1) + " AT");
 +            unnestClause.getPositionalVariable().accept(this, step + 1);
 +        }
 +        return null;
 +    }
 +
 +    @Override
 +    public Void visit(HavingClause havingClause, Integer step) throws AsterixException {
 +        out.println(skip(step) + " HAVING");
 +        havingClause.getFilterExpression().accept(this, step + 1);
 +        return null;
 +    }
 +
 +    @Override
++    public Void visit(CallExpr pf, Integer step) throws AsterixException {
++        FunctionSignature functionSignature = pf.getFunctionSignature();
++        FunctionSignature normalizedFunctionSignature = FunctionMapUtil
++                .normalizeBuiltinFunctionSignature(functionSignature, false);
++        if (AsterixBuiltinFunctions.isBuiltinCompilerFunction(normalizedFunctionSignature, true)) {
++            functionSignature = normalizedFunctionSignature;
++        }
++        out.println(skip(step) + "FunctionCall " + functionSignature.toString() + "[");
++        for (Expression expr : pf.getExprList()) {
++            expr.accept(this, step + 1);
++        }
++        out.println(skip(step) + "]");
++        return null;
++    }
++
++    @Override
 +    public Void visit(GroupbyClause gc, Integer step) throws AsterixException {
++        if (gc.isGroupAll()) {
++            out.println(skip(step) + "Group All");
++            return null;
++        }
 +        out.println(skip(step) + "Groupby");
 +        for (GbyVariableExpressionPair pair : gc.getGbyPairList()) {
 +            if (pair.getVar() != null) {
 +                pair.getVar().accept(this, step + 1);
 +                out.println(skip(step + 1) + ":=");
 +            }
 +            pair.getExpr().accept(this, step + 1);
 +        }
 +        if (gc.hasGroupVar()) {
 +            out.println(skip(step + 1) + "GROUP AS");
 +            gc.getGroupVar().accept(this, step + 1);
 +            if (gc.hasGroupFieldList()) {
 +                out.println(skip(step + 1) + "(");
 +                for (Pair<Expression, Identifier> field : gc.getGroupFieldList()) {
 +                    field.first.accept(this, step + 1);
 +                    out.println(skip(step + 1) + " AS " + field.second);
 +                }
 +                out.println(skip(step + 1) + ")");
 +            }
 +        }
 +        out.println();
 +        return null;
 +    }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/8516517e/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppDeleteRewriteVisitor.java
----------------------------------------------------------------------
diff --cc asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppDeleteRewriteVisitor.java
index 5a15772,0000000..efff18e
mode 100644,000000..100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppDeleteRewriteVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppDeleteRewriteVisitor.java
@@@ -1,95 -1,0 +1,95 @@@
 +/*
 + * 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.ArrayList;
 +import java.util.List;
 +
 +import org.apache.asterix.common.functions.FunctionConstants;
 +import org.apache.asterix.common.functions.FunctionSignature;
 +import org.apache.asterix.lang.common.base.Expression;
 +import org.apache.asterix.lang.common.clause.WhereClause;
 +import org.apache.asterix.lang.common.expression.CallExpr;
 +import org.apache.asterix.lang.common.expression.LiteralExpr;
 +import org.apache.asterix.lang.common.expression.VariableExpr;
 +import org.apache.asterix.lang.common.literal.StringLiteral;
 +import org.apache.asterix.lang.common.statement.DeleteStatement;
 +import org.apache.asterix.lang.common.statement.Query;
 +import org.apache.asterix.lang.common.struct.Identifier;
 +import org.apache.asterix.lang.sqlpp.clause.FromClause;
 +import org.apache.asterix.lang.sqlpp.clause.FromTerm;
 +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.SelectSetOperation;
 +import org.apache.asterix.lang.sqlpp.expression.SelectExpression;
 +import org.apache.asterix.lang.sqlpp.struct.SetOperationInput;
 +import org.apache.asterix.lang.sqlpp.visitor.base.AbstractSqlppAstVisitor;
 +import org.mortbay.util.SingletonList;
 +
 +/**
 + * This class rewrites delete statement to contain a query that specifying
 + * what to delete.
 + */
 +public class SqlppDeleteRewriteVisitor extends AbstractSqlppAstVisitor<Void, Void> {
 +
 +    @Override
 +    public Void visit(DeleteStatement deleteStmt, Void visitArg) {
 +        List<Expression> arguments = new ArrayList<Expression>();
 +        Identifier dataverseName = deleteStmt.getDataverseName();
 +        Identifier datasetName = deleteStmt.getDatasetName();
 +        String arg = dataverseName == null ? datasetName.getValue()
 +                : dataverseName.getValue() + "." + datasetName.getValue();
 +        LiteralExpr argumentLiteral = new LiteralExpr(new StringLiteral(arg));
 +        arguments.add(argumentLiteral);
 +        CallExpr callExpression = new CallExpr(new FunctionSignature(FunctionConstants.ASTERIX_NS, "dataset", 1),
 +                arguments);
 +
 +        // From clause.
 +        VariableExpr var = deleteStmt.getVariableExpr();
 +        FromTerm fromTerm = new FromTerm(callExpression, var, null, null);
 +        @SuppressWarnings("unchecked")
 +        FromClause fromClause = new FromClause(SingletonList.newSingletonList(fromTerm));
 +
 +        // Where clause.
 +        WhereClause whereClause = null;
 +        Expression condition = deleteStmt.getCondition();
 +        if (condition != null) {
 +            whereClause = new WhereClause(condition);
 +        }
 +
 +        // Select clause.
 +        VariableExpr returnExpr = new VariableExpr(var.getVar());
 +        returnExpr.setIsNewVar(false);
 +        SelectElement selectElement = new SelectElement(returnExpr);
 +        SelectClause selectClause = new SelectClause(selectElement, null, false);
 +
 +        // Construct the select expression.
 +        SelectBlock selectBlock = new SelectBlock(selectClause, fromClause, null, whereClause, null, null, null);
 +        SelectSetOperation selectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
 +        SelectExpression selectExpression = new SelectExpression(null, selectSetOperation, null, null, false);
-         Query query = new Query();
++        Query query = new Query(false, selectExpression, 0, new ArrayList<>(), new ArrayList<>());
 +        query.setBody(selectExpression);
 +
 +        // return the delete statement.
 +        deleteStmt.setQuery(query);
 +        return null;
 +    }
 +
 +}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/8516517e/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteVariablesVisitor.java
----------------------------------------------------------------------
diff --cc asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteVariablesVisitor.java
index a9aff55,0000000..f737eb7
mode 100644,000000..100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteVariablesVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteVariablesVisitor.java
@@@ -1,45 -1,0 +1,48 @@@
 +/*
 + * 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 org.apache.asterix.common.exceptions.AsterixException;
 +import org.apache.asterix.lang.common.base.Expression;
 +import org.apache.asterix.lang.common.expression.VariableExpr;
 +import org.apache.asterix.lang.common.rewrites.LangRewritingContext;
 +import org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment;
++import org.apache.asterix.lang.sqlpp.util.SqlppRewriteUtil;
 +
 +public class SqlppSubstituteVariablesVisitor extends SqlppCloneAndSubstituteVariablesVisitor {
 +
 +    public SqlppSubstituteVariablesVisitor() {
 +        super(null);
 +    }
 +
 +    @Override
-     protected Expression rewriteVariableExpr(VariableExpr expr, VariableSubstitutionEnvironment env) {
++    protected Expression rewriteVariableExpr(VariableExpr expr, VariableSubstitutionEnvironment env)
++            throws AsterixException {
 +        if (env.constainsOldVar(expr)) {
-             return env.findSubstituion(expr);
++            return (Expression) SqlppRewriteUtil.deepCopy(env.findSubstituion(expr));
 +        }
 +        return expr;
 +    }
 +
 +    @Override
 +    public VariableExpr generateNewVariable(LangRewritingContext context, VariableExpr varExpr) {
 +        return varExpr;
 +    }
 +
 +}