asterixdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From buyin...@apache.org
Subject [02/34] incubator-asterixdb git commit: SQL++ support in AsterixDB: 1. implemented SQL++ expression to logical plan translator; 2. refactored REST API to be agnostic of query languages; 3. disabled fuzzy join queries for SQL++ runtime tests; 4. fixed sev
Date Tue, 17 Nov 2015 00:56:34 GMT
http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppRewriter.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppRewriter.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppRewriter.java
deleted file mode 100644
index 4caecfe..0000000
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppRewriter.java
+++ /dev/null
@@ -1,318 +0,0 @@
-/*
- * 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.rewrites;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
-
-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.LetClause;
-import org.apache.asterix.lang.common.rewrites.LangRewritingContext;
-import org.apache.asterix.lang.common.statement.FunctionDecl;
-import org.apache.asterix.lang.common.statement.Query;
-import org.apache.asterix.lang.common.visitor.GatherFunctionCallsVisitor;
-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.FunctionUtils;
-import org.apache.asterix.lang.sqlpp.visitor.InlineColumnAliasVisitor;
-import org.apache.asterix.lang.sqlpp.visitor.SqlppInlineUdfsVisitor;
-import org.apache.asterix.lang.sqlpp.visitor.VariableCheckAndRewriteVisitor;
-import org.apache.asterix.lang.sqlpp.visitor.base.ISqlppVisitor;
-import org.apache.asterix.metadata.MetadataManager;
-import org.apache.asterix.metadata.MetadataTransactionContext;
-import org.apache.asterix.metadata.declared.AqlMetadataProvider;
-import org.apache.asterix.metadata.entities.Function;
-import org.apache.asterix.om.functions.AsterixBuiltinFunctions;
-
-public final class SqlppRewriter {
-
-    private Query topExpr;
-    private final List<FunctionDecl> declaredFunctions;
-    private final LangRewritingContext context;
-    private final MetadataTransactionContext mdTxnCtx;
-    private final AqlMetadataProvider metadataProvider;
-
-    public SqlppRewriter(List<FunctionDecl> declaredFunctions, Query topExpr, AqlMetadataProvider metadataProvider) {
-        this.topExpr = topExpr;
-        context = new LangRewritingContext(topExpr.getVarCounter());
-        this.declaredFunctions = declaredFunctions;
-        this.mdTxnCtx = metadataProvider.getMetadataTxnContext();
-        this.metadataProvider = metadataProvider;
-    }
-
-    public Query getExpr() {
-        return topExpr;
-    }
-
-    public int getVarCounter() {
-        return context.getVarCounter();
-    }
-
-    public void rewrite() throws AsterixException {
-        // Inlines column aliases.
-        inlineColumnAlias();
-
-        // Replace global variable access with the dataset function.
-        variableCheckAndRewrite();
-
-        // Inlines functions.
-        inlineDeclaredUdfs();
-    }
-
-    private void inlineColumnAlias() throws AsterixException {
-        if (topExpr == null) {
-            return;
-        }
-        // Inline column aliases.
-        InlineColumnAliasVisitor inlineColumnAliasVisitor = new InlineColumnAliasVisitor();
-        inlineColumnAliasVisitor.visit(topExpr, false);
-    }
-
-    private void variableCheckAndRewrite() throws AsterixException {
-        if (topExpr == null) {
-            return;
-        }
-        VariableCheckAndRewriteVisitor variableCheckAndRewriteVisitor = new VariableCheckAndRewriteVisitor();
-        variableCheckAndRewriteVisitor.visit(topExpr, null);
-    }
-
-    private void inlineDeclaredUdfs() throws AsterixException {
-        if (topExpr == null) {
-            return;
-        }
-        List<FunctionSignature> funIds = new ArrayList<FunctionSignature>();
-        for (FunctionDecl fdecl : declaredFunctions) {
-            funIds.add(fdecl.getSignature());
-        }
-
-        List<FunctionDecl> otherFDecls = new ArrayList<FunctionDecl>();
-        buildOtherUdfs(topExpr.getBody(), otherFDecls, funIds);
-        declaredFunctions.addAll(otherFDecls);
-        if (!declaredFunctions.isEmpty()) {
-            SqlppInlineUdfsVisitor visitor = new SqlppInlineUdfsVisitor(context);
-            while (topExpr.accept(visitor, declaredFunctions)) {
-                // loop until no more changes
-            }
-        }
-        declaredFunctions.removeAll(otherFDecls);
-    }
-
-    private void buildOtherUdfs(Expression expression, List<FunctionDecl> functionDecls,
-            List<FunctionSignature> declaredFunctions) throws AsterixException {
-        if (expression == null) {
-            return;
-        }
-        String value = metadataProvider.getConfig().get(FunctionUtils.IMPORT_PRIVATE_FUNCTIONS);
-        boolean includePrivateFunctions = (value != null) ? Boolean.valueOf(value.toLowerCase()) : false;
-        Set<FunctionSignature> functionCalls = getFunctionCalls(expression);
-        for (FunctionSignature signature : functionCalls) {
-
-            if (declaredFunctions != null && declaredFunctions.contains(signature)) {
-                continue;
-            }
-
-            Function function = lookupUserDefinedFunctionDecl(signature);
-            if (function == null) {
-                if (AsterixBuiltinFunctions.isBuiltinCompilerFunction(signature, includePrivateFunctions)) {
-                    continue;
-                }
-                StringBuilder messageBuilder = new StringBuilder();
-                if (functionDecls.size() > 0) {
-                    messageBuilder.append(" function " + functionDecls.get(functionDecls.size() - 1).getSignature()
-                            + " depends upon function " + signature + " which is undefined");
-                } else {
-                    messageBuilder.append(" function " + signature + " is undefined ");
-                }
-                throw new AsterixException(messageBuilder.toString());
-            }
-
-            if (function.getLanguage().equalsIgnoreCase(Function.LANGUAGE_AQL)) {
-                FunctionDecl functionDecl = FunctionUtils.getFunctionDecl(function);
-                if (functionDecl != null) {
-                    if (functionDecls.contains(functionDecl)) {
-                        throw new AsterixException("ERROR:Recursive invocation "
-                                + functionDecls.get(functionDecls.size() - 1).getSignature() + " <==> "
-                                + functionDecl.getSignature());
-                    }
-                    functionDecls.add(functionDecl);
-                    buildOtherUdfs(functionDecl.getFuncBody(), functionDecls, declaredFunctions);
-                }
-            }
-        }
-
-    }
-
-    private Function lookupUserDefinedFunctionDecl(FunctionSignature signature) throws AsterixException {
-        if (signature.getNamespace() == null) {
-            return null;
-        }
-        return MetadataManager.INSTANCE.getFunction(mdTxnCtx, signature);
-    }
-
-    private Set<FunctionSignature> getFunctionCalls(Expression expression) throws AsterixException {
-        GatherFunctionCalls gfc = new GatherFunctionCalls();
-        expression.accept(gfc, null);
-        return gfc.getCalls();
-    }
-
-    private static class GatherFunctionCalls extends GatherFunctionCallsVisitor implements ISqlppVisitor<Void, Void> {
-
-        public GatherFunctionCalls() {
-        }
-
-        @Override
-        public Void visit(FromClause fromClause, Void arg) throws AsterixException {
-            for (FromTerm fromTerm : fromClause.getFromTerms()) {
-                fromTerm.accept(this, arg);
-            }
-            return null;
-        }
-
-        @Override
-        public Void visit(FromTerm fromTerm, Void arg) throws AsterixException {
-            fromTerm.getLeftExpression().accept(this, arg);
-            for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
-                correlateClause.accept(this, arg);
-            }
-            return null;
-        }
-
-        @Override
-        public Void visit(JoinClause joinClause, Void arg) throws AsterixException {
-            joinClause.getRightExpression().accept(this, arg);
-            joinClause.getConditionExpression().accept(this, arg);
-            return null;
-        }
-
-        @Override
-        public Void visit(NestClause nestClause, Void arg) throws AsterixException {
-            nestClause.getRightExpression().accept(this, arg);
-            nestClause.getConditionExpression().accept(this, arg);
-            return null;
-        }
-
-        @Override
-        public Void visit(Projection projection, Void arg) throws AsterixException {
-            projection.getExpression().accept(this, arg);
-            return null;
-        }
-
-        @Override
-        public Void visit(SelectBlock selectBlock, Void arg) throws AsterixException {
-            if (selectBlock.hasFromClause()) {
-                selectBlock.getFromClause().accept(this, arg);
-            }
-            if (selectBlock.hasLetClauses()) {
-                for (LetClause letClause : selectBlock.getLetList()) {
-                    letClause.accept(this, arg);
-                }
-            }
-            if (selectBlock.hasWhereClause()) {
-                selectBlock.getWhereClause().accept(this, arg);
-            }
-            if (selectBlock.hasGroupbyClause()) {
-                selectBlock.getGroupbyClause().accept(this, arg);
-            }
-            if (selectBlock.hasLetClausesAfterGroupby()) {
-                for (LetClause letClause : selectBlock.getLetListAfterGroupby()) {
-                    letClause.accept(this, arg);
-                }
-            }
-            if (selectBlock.hasHavingClause()) {
-                selectBlock.getHavingClause().accept(this, arg);
-            }
-            selectBlock.getSelectClause().accept(this, arg);
-            return null;
-        }
-
-        @Override
-        public Void visit(SelectClause selectClause, Void arg) throws AsterixException {
-            if (selectClause.selectElement()) {
-                selectClause.getSelectElement().accept(this, arg);
-            } else {
-                selectClause.getSelectRegular().accept(this, arg);
-            }
-            return null;
-        }
-
-        @Override
-        public Void visit(SelectElement selectElement, Void arg) throws AsterixException {
-            selectElement.getExpression().accept(this, arg);
-            return null;
-        }
-
-        @Override
-        public Void visit(SelectRegular selectRegular, Void arg) throws AsterixException {
-            for (Projection projection : selectRegular.getProjections()) {
-                projection.accept(this, arg);
-            }
-            return null;
-        }
-
-        @Override
-        public Void visit(SelectSetOperation selectSetOperation, Void arg) throws AsterixException {
-            selectSetOperation.getLeftInput().accept(this, arg);
-            for (SetOperationRight setOperationRight : selectSetOperation.getRightInputs()) {
-                setOperationRight.getSetOperationRightInput().accept(this, arg);
-            }
-            return null;
-        }
-
-        @Override
-        public Void visit(SelectExpression selectStatement, Void arg) throws AsterixException {
-            selectStatement.getSelectSetOperation().accept(this, arg);
-            if (selectStatement.hasOrderby()) {
-                selectStatement.getOrderbyClause().accept(this, arg);
-            }
-            if (selectStatement.hasLimit()) {
-                selectStatement.getLimitClause().accept(this, arg);
-            }
-            return null;
-        }
-
-        @Override
-        public Void visit(UnnestClause unnestClause, Void arg) throws AsterixException {
-            unnestClause.getRightExpression().accept(this, arg);
-            return null;
-        }
-
-        @Override
-        public Void visit(HavingClause havingClause, Void arg) throws AsterixException {
-            havingClause.getFilterExpression().accept(this, arg);
-            return null;
-        }
-
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppRewriterFactory.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppRewriterFactory.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppRewriterFactory.java
new file mode 100644
index 0000000..64fa576
--- /dev/null
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppRewriterFactory.java
@@ -0,0 +1,37 @@
+/*
+ * 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.rewrites;
+
+import org.apache.asterix.lang.common.base.IQueryRewriter;
+import org.apache.asterix.lang.common.base.IRewriterFactory;
+import org.apache.asterix.lang.common.base.IStatementRewriter;
+
+public class SqlppRewriterFactory implements IRewriterFactory {
+
+    @Override
+    public IQueryRewriter createQueryRewriter() {
+        return new SqlppQueryRewriter();
+    }
+
+    @Override
+    public IStatementRewriter createStatementRewriter() {
+        return new SqlppStatementRewriter();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppStatementRewriter.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppStatementRewriter.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppStatementRewriter.java
new file mode 100644
index 0000000..01cb0f1
--- /dev/null
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/SqlppStatementRewriter.java
@@ -0,0 +1,39 @@
+/*
+ * 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.rewrites;
+
+import org.apache.asterix.common.exceptions.AsterixException;
+import org.apache.asterix.lang.common.base.IStatementRewriter;
+import org.apache.asterix.lang.common.base.Statement;
+import org.apache.asterix.lang.sqlpp.visitor.SqlppDeleteRewriteVisitor;
+
+class SqlppStatementRewriter implements IStatementRewriter {
+
+    @Override
+    public void rewrite(Statement stmt) throws AsterixException {
+        rewriteDeleteStatement(stmt);
+    }
+
+    private void rewriteDeleteStatement(Statement stmt) throws AsterixException {
+        if (stmt != null) {
+            SqlppDeleteRewriteVisitor visitor = new SqlppDeleteRewriteVisitor();
+            stmt.accept(visitor, null);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/struct/SetOperationRight.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/struct/SetOperationRight.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/struct/SetOperationRight.java
index ae0572d..5e061e0 100644
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/struct/SetOperationRight.java
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/struct/SetOperationRight.java
@@ -36,7 +36,7 @@ public class SetOperationRight {
         return opType;
     }
 
-    public boolean setSemantics() {
+    public boolean isSetSemantics() {
         return setSemantics;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/FunctionUtils.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/FunctionUtils.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/FunctionUtils.java
deleted file mode 100644
index 9100ecd..0000000
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/FunctionUtils.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * 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.io.StringReader;
-import java.util.ArrayList;
-import java.util.List;
-
-import org.apache.asterix.common.exceptions.AsterixException;
-import org.apache.asterix.common.functions.FunctionSignature;
-import org.apache.asterix.lang.common.base.Statement;
-import org.apache.asterix.lang.common.statement.FunctionDecl;
-import org.apache.asterix.lang.common.struct.VarIdentifier;
-import org.apache.asterix.lang.sqlpp.parser.SQLPPParser;
-import org.apache.asterix.metadata.entities.Function;
-import org.apache.asterix.om.functions.AsterixBuiltinFunctions;
-import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
-import org.apache.hyracks.algebricks.core.algebra.functions.IFunctionInfo;
-
-public class FunctionUtils {
-
-    public static final String IMPORT_PRIVATE_FUNCTIONS = "import-private-functions";
-
-    public static FunctionDecl getFunctionDecl(Function function) throws AsterixException {
-        String functionBody = function.getFunctionBody();
-        List<String> params = function.getParams();
-        List<VarIdentifier> varIdentifiers = new ArrayList<VarIdentifier>();
-
-        StringBuilder builder = new StringBuilder();
-        builder.append(" use dataverse " + function.getDataverseName() + ";");
-        builder.append(" declare function " + function.getName().split("@")[0]);
-        builder.append("(");
-        for (String param : params) {
-            VarIdentifier varId = new VarIdentifier(param);
-            varIdentifiers.add(varId);
-            builder.append(param);
-            builder.append(",");
-        }
-        if (params.size() > 0) {
-            builder.delete(builder.length() - 1, builder.length());
-        }
-        builder.append(")");
-        builder.append("{");
-        builder.append("\n");
-        builder.append(functionBody);
-        builder.append("\n");
-        builder.append("}");
-
-        SQLPPParser parser = new SQLPPParser(new StringReader(new String(builder)));
-
-        List<Statement> statements = null;
-        try {
-            statements = parser.parse();
-        } catch (AsterixException pe) {
-            throw new AsterixException(pe);
-        }
-
-        FunctionDecl decl = (FunctionDecl) statements.get(1);
-        return decl;
-    }
-
-    public static IFunctionInfo getFunctionInfo(FunctionIdentifier fi) {
-        return AsterixBuiltinFunctions.getAsterixFunctionInfo(fi);
-    }
-
-    public static IFunctionInfo getFunctionInfo(FunctionSignature fs) {
-        return getFunctionInfo(new FunctionIdentifier(fs.getNamespace(), fs.getName(), fs.getArity()));
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppAstPrintUtil.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppAstPrintUtil.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppAstPrintUtil.java
index 0f7ec4a..5888194 100644
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppAstPrintUtil.java
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppAstPrintUtil.java
@@ -24,15 +24,19 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.asterix.common.exceptions.AsterixException;
+import org.apache.asterix.lang.common.base.IAstPrintVisitorFactory;
 import org.apache.asterix.lang.common.base.ILangExpression;
 import org.apache.asterix.lang.common.base.Statement;
-import org.apache.asterix.lang.sqlpp.visitor.SqlppPrintVisitor;
+import org.apache.asterix.lang.common.visitor.QueryPrintVisitor;
+import org.apache.asterix.lang.sqlpp.visitor.SqlppAstPrintVisitorFactory;
 
 public class SqlppAstPrintUtil {
 
+    private static final IAstPrintVisitorFactory astPrintVisitor = new SqlppAstPrintVisitorFactory();
+
     /**
      * Prints the AST (abstract syntax tree) of an ILangExpression.
-     * 
+     *
      * @param expr
      *            the language expression.
      * @param output
@@ -40,13 +44,14 @@ public class SqlppAstPrintUtil {
      * @throws AsterixException
      */
     public static void print(ILangExpression expr, PrintWriter output) throws AsterixException {
-        SqlppPrintVisitor visitor = new SqlppPrintVisitor(output);
+        QueryPrintVisitor visitor = astPrintVisitor.createLangVisitor(output);
         expr.accept(visitor, 0);
+        output.flush();
     }
 
     /**
      * Prints the AST of a list of top-level language statements.
-     * 
+     *
      * @param statements
      *            a list of statements of a query
      * @param output
@@ -54,10 +59,11 @@ public class SqlppAstPrintUtil {
      * @throws AsterixException
      */
     public static void print(List<Statement> statements, PrintWriter output) throws AsterixException {
-        SqlppPrintVisitor visitor = new SqlppPrintVisitor(output);
+        QueryPrintVisitor visitor = astPrintVisitor.createLangVisitor(output);
         for (Statement statement : statements) {
             statement.accept(visitor, 0);
         }
+        output.flush();
     }
 
     /**
@@ -81,7 +87,7 @@ public class SqlppAstPrintUtil {
     public static String toString(List<ILangExpression> exprs) throws AsterixException {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         PrintWriter output = new PrintWriter(bos);
-        SqlppPrintVisitor visitor = new SqlppPrintVisitor(output);
+        QueryPrintVisitor visitor = astPrintVisitor.createLangVisitor(output);
         for (ILangExpression expr : exprs) {
             expr.accept(visitor, 0);
         }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppFormatPrintUtil.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppFormatPrintUtil.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppFormatPrintUtil.java
index f10c9fc..41760f5 100644
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppFormatPrintUtil.java
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppFormatPrintUtil.java
@@ -32,7 +32,7 @@ public class SqlppFormatPrintUtil {
 
     /**
      * Prints the formatted output of an ILangExpression.
-     * 
+     *
      * @param expr
      *            the language expression.
      * @param output
@@ -46,7 +46,7 @@ public class SqlppFormatPrintUtil {
 
     /**
      * Prints the formatted output of a list of top-level language statements.
-     * 
+     *
      * @param statements
      *            a list of statements of a query
      * @param output

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableSubstitutionUtil.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableSubstitutionUtil.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableSubstitutionUtil.java
index 0a4c2c4..6438f07 100644
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableSubstitutionUtil.java
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableSubstitutionUtil.java
@@ -18,8 +18,6 @@
  */
 package org.apache.asterix.lang.sqlpp.util;
 
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Map;
 
 import org.apache.asterix.common.exceptions.AsterixException;
@@ -33,39 +31,59 @@ import org.apache.asterix.lang.sqlpp.visitor.SqlppSubstituteVariablesVisitor;
 
 public class SqlppVariableSubstitutionUtil {
 
-    public static List<ILangExpression> substituteVariable(List<ILangExpression> expressions,
-            VariableSubstitutionEnvironment env) throws AsterixException {
-        SqlppCloneAndSubstituteVariablesVisitor visitor = new SqlppSubstituteVariablesVisitor(
-                new LangRewritingContext(0));
-        List<ILangExpression> newExprs = new ArrayList<ILangExpression>();
-        for (ILangExpression expression : expressions) {
-            newExprs.add(expression.accept(visitor, env).first);
-        }
-        return newExprs;
+    /**
+     * Substitute variables with corresponding expressions according to the varExprMap.
+     * The substitution should be done BEFORE unique ids are assigned to variables.
+     * In other words, when we call this method, ids of all variables are zero.
+     *
+     * @param expression,
+     *            the expression for substituting variables.
+     * @param varExprMap
+     *            a map that maps variables to their corresponding expressions.
+     * @return a new expression in which variables are substituted.
+     * @throws AsterixException
+     */
+    public static ILangExpression substituteVariableWithoutContext(ILangExpression expression,
+            Map<VariableExpr, Expression> varExprMap) throws AsterixException {
+        VariableSubstitutionEnvironment env = new VariableSubstitutionEnvironment(varExprMap);
+        return substituteVariableWithoutContext(expression, env);
     }
 
-    public static ILangExpression substituteVariable(ILangExpression expression, VariableSubstitutionEnvironment env)
-            throws AsterixException {
-        SqlppSubstituteVariablesVisitor visitor = new SqlppSubstituteVariablesVisitor(new LangRewritingContext(0));
+    /**
+     * Substitute variables with corresponding expressions according to the varExprMap.
+     * The substitution should be done BEFORE unique ids are assigned to variables.
+     * In other words, when we call this method, ids of all variables are zero.
+     *
+     * @param expression,
+     *            the expression for substituting variables.
+     * @param env,
+     *            internally contains a map that maps variables to their corresponding expressions.
+     * @return a new expression in which variables are substituted.
+     * @throws AsterixException
+     */
+    public static ILangExpression substituteVariableWithoutContext(ILangExpression expression,
+            VariableSubstitutionEnvironment env) throws AsterixException {
+        SqlppSubstituteVariablesVisitor visitor = new SqlppSubstituteVariablesVisitor();
         return expression.accept(visitor, env).first;
     }
 
-    public static List<ILangExpression> substituteVariable(List<ILangExpression> expressions,
-            Map<VariableExpr, Expression> varExprMap) throws AsterixException {
-        SqlppSubstituteVariablesVisitor visitor = new SqlppSubstituteVariablesVisitor(new LangRewritingContext(0));
-        VariableSubstitutionEnvironment env = new VariableSubstitutionEnvironment(varExprMap);
-        List<ILangExpression> newExprs = new ArrayList<ILangExpression>();
-        for (ILangExpression expression : expressions) {
-            newExprs.add(expression.accept(visitor, env).first);
-        }
-        return newExprs;
-    }
-
-    public static ILangExpression substituteVariable(ILangExpression expression,
-            Map<VariableExpr, Expression> varExprMap) throws AsterixException {
-        SqlppSubstituteVariablesVisitor visitor = new SqlppSubstituteVariablesVisitor(new LangRewritingContext(0));
+    /**
+     * Substitute variables with corresponding expressions according to the varExprMap.
+     * The substitution should be done AFTER unique ids are assigned to different variables.
+     *
+     * @param expression,
+     *            the expression for substituting variables.
+     * @param varExprMap,
+     *            a map that maps variables to their corresponding expressions.
+     * @param context,
+     *            manages the ids of variables so as to guarantee the uniqueness of ids for different variables (even with the same name).
+     * @return a cloned new expression in which variables are substituted, and its bounded variables have new unique ids.
+     * @throws AsterixException
+     */
+    public static ILangExpression cloneAndSubstituteVariable(ILangExpression expression,
+            Map<VariableExpr, Expression> varExprMap, LangRewritingContext context) throws AsterixException {
+        SqlppCloneAndSubstituteVariablesVisitor visitor = new SqlppCloneAndSubstituteVariablesVisitor(context);
         VariableSubstitutionEnvironment env = new VariableSubstitutionEnvironment(varExprMap);
         return expression.accept(visitor, env).first;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/InlineColumnAliasVisitor.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/InlineColumnAliasVisitor.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/InlineColumnAliasVisitor.java
index d6955c0..bafac89 100644
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/InlineColumnAliasVisitor.java
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/InlineColumnAliasVisitor.java
@@ -227,7 +227,7 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
         VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope().getVarSubstitutionEnvironment();
         if (rewrite) {
             Expression newBindExpr = (Expression) SqlppVariableSubstitutionUtil
-                    .substituteVariable(letClause.getBindingExpr(), env);
+                    .substituteVariableWithoutContext(letClause.getBindingExpr(), env);
             letClause.setBindingExpr(newBindExpr);
         }
         letClause.getBindingExpr().accept(this, false);
@@ -241,7 +241,7 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
         VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope().getVarSubstitutionEnvironment();
         List<Expression> orderExprs = new ArrayList<Expression>();
         for (Expression orderExpr : oc.getOrderbyList()) {
-            orderExprs.add((Expression) SqlppVariableSubstitutionUtil.substituteVariable(orderExpr, env));
+            orderExprs.add((Expression) SqlppVariableSubstitutionUtil.substituteVariableWithoutContext(orderExpr, env));
             orderExpr.accept(this, arg);
         }
         oc.setOrderbyList(orderExprs);
@@ -252,7 +252,7 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
     public Void visit(GroupbyClause gc, Boolean arg) throws AsterixException {
         VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope().getVarSubstitutionEnvironment();
         for (GbyVariableExpressionPair gbyVarExpr : gc.getGbyPairList()) {
-            Expression newExpr = (Expression) SqlppVariableSubstitutionUtil.substituteVariable(gbyVarExpr.getExpr(),
+            Expression newExpr = (Expression) SqlppVariableSubstitutionUtil.substituteVariableWithoutContext(gbyVarExpr.getExpr(),
                     env);
             newExpr.accept(this, arg);
             gbyVarExpr.setExpr(newExpr);
@@ -274,7 +274,7 @@ public class InlineColumnAliasVisitor extends AbstractSqlppQueryExpressionVisito
     public Void visit(HavingClause havingClause, Boolean arg) throws AsterixException {
         VariableSubstitutionEnvironment env = scopeChecker.getCurrentScope().getVarSubstitutionEnvironment();
         Expression newFilterExpr = (Expression) SqlppVariableSubstitutionUtil
-                .substituteVariable(havingClause.getFilterExpression(), env);
+                .substituteVariableWithoutContext(havingClause.getFilterExpression(), env);
         newFilterExpr.accept(this, arg);
         havingClause.setFilterExpression(newFilterExpr);
         return null;

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitor.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitor.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitor.java
new file mode 100644
index 0000000..06b5027
--- /dev/null
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitor.java
@@ -0,0 +1,240 @@
+/*
+ * 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.lang.common.clause.LetClause;
+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.visitor.base.ISqlppVisitor;
+
+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;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitorFactory.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitorFactory.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitorFactory.java
new file mode 100644
index 0000000..accc799
--- /dev/null
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppAstPrintVisitorFactory.java
@@ -0,0 +1,33 @@
+/*
+ * 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.lang.common.base.IAstPrintVisitorFactory;
+import org.apache.asterix.lang.common.visitor.QueryPrintVisitor;
+
+public class SqlppAstPrintVisitorFactory implements IAstPrintVisitorFactory {
+
+    @Override
+    public QueryPrintVisitor createLangVisitor(PrintWriter writer) {
+        return new SqlppAstPrintVisitor(writer);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppCloneAndSubstituteVariablesVisitor.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppCloneAndSubstituteVariablesVisitor.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppCloneAndSubstituteVariablesVisitor.java
index 3cb7b28..e06fdb5 100644
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppCloneAndSubstituteVariablesVisitor.java
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppCloneAndSubstituteVariablesVisitor.java
@@ -313,7 +313,7 @@ public class SqlppCloneAndSubstituteVariablesVisitor extends CloneAndSubstituteV
                 Pair<ILangExpression, VariableSubstitutionEnvironment> p = rightInput.getSubquery().accept(this, env);
                 newRightInput = new SetOperationInput(null, (SelectExpression) p.first);
             }
-            newRightInputs.add(new SetOperationRight(right.getSetOpType(), right.setSemantics(), newRightInput));
+            newRightInputs.add(new SetOperationRight(right.getSetOpType(), right.isSetSemantics(), newRightInput));
         }
         SelectSetOperation newSelectSetOperation = new SelectSetOperation(newLeftInput, newRightInputs);
         return new Pair<ILangExpression, VariableSubstitutionEnvironment>(newSelectSetOperation, env);

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppDeleteRewriteVisitor.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppDeleteRewriteVisitor.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppDeleteRewriteVisitor.java
new file mode 100644
index 0000000..5a15772
--- /dev/null
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppDeleteRewriteVisitor.java
@@ -0,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.setBody(selectExpression);
+
+        // return the delete statement.
+        deleteStmt.setQuery(query);
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppFormatPrintVisitor.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppFormatPrintVisitor.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppFormatPrintVisitor.java
index 4a583c2..a449402 100644
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppFormatPrintVisitor.java
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppFormatPrintVisitor.java
@@ -196,7 +196,7 @@ public class SqlppFormatPrintVisitor extends FormatPrintVisitor implements ISqlp
         selectSetOperation.getLeftInput().accept(this, step);
         if (selectSetOperation.hasRightInputs()) {
             for (SetOperationRight right : selectSetOperation.getRightInputs()) {
-                String all = right.setSemantics() ? " " : " all ";
+                String all = right.isSetSemantics() ? " " : " all ";
                 out.print(right.getSetOpType() + all);
                 right.getSetOperationRightInput().accept(this, step);
             }

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppInlineUdfsVisitor.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppInlineUdfsVisitor.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppInlineUdfsVisitor.java
index 2931d25..37c8be8 100644
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppInlineUdfsVisitor.java
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppInlineUdfsVisitor.java
@@ -24,6 +24,7 @@ import java.util.Map;
 
 import org.apache.asterix.common.exceptions.AsterixException;
 import org.apache.asterix.lang.common.base.Expression;
+import org.apache.asterix.lang.common.base.IRewriterFactory;
 import org.apache.asterix.lang.common.clause.LetClause;
 import org.apache.asterix.lang.common.expression.VariableExpr;
 import org.apache.asterix.lang.common.rewrites.LangRewritingContext;
@@ -46,20 +47,35 @@ import org.apache.asterix.lang.sqlpp.expression.SelectExpression;
 import org.apache.asterix.lang.sqlpp.struct.SetOperationRight;
 import org.apache.asterix.lang.sqlpp.util.SqlppVariableSubstitutionUtil;
 import org.apache.asterix.lang.sqlpp.visitor.base.ISqlppVisitor;
+import org.apache.asterix.metadata.declared.AqlMetadataProvider;
 import org.apache.hyracks.algebricks.common.utils.Pair;
 
 public class SqlppInlineUdfsVisitor extends AbstractInlineUdfsVisitor
         implements ISqlppVisitor<Boolean, List<FunctionDecl>> {
 
-    public SqlppInlineUdfsVisitor(LangRewritingContext context) {
-        super(context, new SqlppCloneAndSubstituteVariablesVisitor(context));
+    /**
+     * @param context,
+     *            manages ids of variables and guarantees uniqueness of variables.
+     * @param rewriterFactory,
+     *            a rewrite factory for rewriting user-defined functions.
+     * @param declaredFunctions,
+     *            a list of declared functions associated with the query.
+     * @param metadataProvider,
+     *            providing the definition of created (i.e., stored) user-defined functions.
+     */
+    public SqlppInlineUdfsVisitor(LangRewritingContext context, IRewriterFactory rewriterFactory,
+            List<FunctionDecl> declaredFunctions, AqlMetadataProvider metadataProvider) {
+        super(context, rewriterFactory, declaredFunctions, metadataProvider,
+                new SqlppCloneAndSubstituteVariablesVisitor(context));
     }
 
     @Override
     protected Expression generateQueryExpression(List<LetClause> letClauses, Expression returnExpr)
             throws AsterixException {
         Map<VariableExpr, Expression> varExprMap = extractLetBindingVariableExpressionMappings(letClauses);
-        return (Expression) SqlppVariableSubstitutionUtil.substituteVariable(returnExpr, varExprMap);
+        Expression inlinedReturnExpr = (Expression) SqlppVariableSubstitutionUtil
+                .substituteVariableWithoutContext(returnExpr, varExprMap);
+        return inlinedReturnExpr;
     }
 
     @Override
@@ -74,7 +90,9 @@ public class SqlppInlineUdfsVisitor extends AbstractInlineUdfsVisitor
     @Override
     public Boolean visit(FromTerm fromTerm, List<FunctionDecl> func) throws AsterixException {
         boolean changed = false;
-        changed |= fromTerm.getLeftExpression().accept(this, func);
+        Pair<Boolean, Expression> p = inlineUdfsInExpr(fromTerm.getLeftExpression(), func);
+        fromTerm.setLeftExpression(p.second);
+        changed |= p.first;
         for (AbstractBinaryCorrelateClause correlateClause : fromTerm.getCorrelateClauses()) {
             changed |= correlateClause.accept(this, func);
         }
@@ -87,7 +105,7 @@ public class SqlppInlineUdfsVisitor extends AbstractInlineUdfsVisitor
         joinClause.setRightExpression(p1.second);
         Pair<Boolean, Expression> p2 = inlineUdfsInExpr(joinClause.getConditionExpression(), funcs);
         joinClause.setConditionExpression(p2.second);
-        return p1.first && p2.first;
+        return p1.first || p2.first;
     }
 
     @Override
@@ -96,7 +114,7 @@ public class SqlppInlineUdfsVisitor extends AbstractInlineUdfsVisitor
         nestClause.setRightExpression(p1.second);
         Pair<Boolean, Expression> p2 = inlineUdfsInExpr(nestClause.getConditionExpression(), funcs);
         nestClause.setConditionExpression(p2.second);
-        return p1.first && p2.first;
+        return p1.first || p2.first;
     }
 
     @Override
@@ -209,8 +227,8 @@ public class SqlppInlineUdfsVisitor extends AbstractInlineUdfsVisitor
         Map<VariableExpr, Expression> varExprMap = new HashMap<VariableExpr, Expression>();
         for (LetClause lc : letClauses) {
             // inline let variables one by one iteratively.
-            lc.setBindingExpr(
-                    (Expression) SqlppVariableSubstitutionUtil.substituteVariable(lc.getBindingExpr(), varExprMap));
+            lc.setBindingExpr((Expression) SqlppVariableSubstitutionUtil
+                    .substituteVariableWithoutContext(lc.getBindingExpr(), varExprMap));
             varExprMap.put(lc.getVarExpr(), lc.getBindingExpr());
         }
         return varExprMap;

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppPrintVisitor.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppPrintVisitor.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppPrintVisitor.java
deleted file mode 100644
index 9938c1e..0000000
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppPrintVisitor.java
+++ /dev/null
@@ -1,238 +0,0 @@
-/*
- * 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.lang.common.clause.LetClause;
-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.visitor.base.ISqlppVisitor;
-
-public class SqlppPrintVisitor extends QueryPrintVisitor implements ISqlppVisitor<Void, Integer> {
-
-    private final PrintWriter out;
-
-    public SqlppPrintVisitor() {
-        super();
-        out = new PrintWriter(System.out);
-    }
-
-    public SqlppPrintVisitor(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);
-        }
-        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);
-        }
-        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.setSemantics() ? " 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;
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteVariablesVisitor.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteVariablesVisitor.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteVariablesVisitor.java
index b00afe0..a82bb49 100644
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteVariablesVisitor.java
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteVariablesVisitor.java
@@ -25,8 +25,8 @@ import org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment;
 
 public class SqlppSubstituteVariablesVisitor extends SqlppCloneAndSubstituteVariablesVisitor {
 
-    public SqlppSubstituteVariablesVisitor(LangRewritingContext context) {
-        super(context);
+    public SqlppSubstituteVariablesVisitor() {
+        super(null);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/VariableCheckAndRewriteVisitor.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/VariableCheckAndRewriteVisitor.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/VariableCheckAndRewriteVisitor.java
index 9dd223d..b016b62 100644
--- a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/VariableCheckAndRewriteVisitor.java
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/VariableCheckAndRewriteVisitor.java
@@ -46,6 +46,7 @@ import org.apache.asterix.lang.common.expression.UnaryExpr;
 import org.apache.asterix.lang.common.expression.VariableExpr;
 import org.apache.asterix.lang.common.literal.StringLiteral;
 import org.apache.asterix.lang.common.parser.ScopeChecker;
+import org.apache.asterix.lang.common.rewrites.LangRewritingContext;
 import org.apache.asterix.lang.common.statement.FunctionDecl;
 import org.apache.asterix.lang.common.statement.Query;
 import org.apache.asterix.lang.common.struct.Identifier;
@@ -69,10 +70,27 @@ import org.apache.asterix.lang.sqlpp.struct.SetOperationRight;
 import org.apache.asterix.lang.sqlpp.util.SqlppFormatPrintUtil;
 import org.apache.asterix.lang.sqlpp.visitor.base.AbstractSqlppQueryExpressionVisitor;
 import org.apache.asterix.metadata.bootstrap.MetadataConstants;
+import org.apache.hyracks.algebricks.core.algebra.base.Counter;
 
 public class VariableCheckAndRewriteVisitor extends AbstractSqlppQueryExpressionVisitor<Expression, Void> {
 
     private final ScopeChecker scopeChecker = new ScopeChecker();
+    private final LangRewritingContext context;
+    private final boolean overwrite;
+
+    /**
+     * @param context,
+     *            manages ids of variables and guarantees uniqueness of variables.
+     * @param overwrite,
+     *            whether rewrite unbounded variables to dataset function calls.
+     *            This flag can only be true for rewriting a top-level query.
+     *            It should be false for rewriting the body expression of a user-defined function.
+     */
+    public VariableCheckAndRewriteVisitor(LangRewritingContext context, boolean overwrite) {
+        this.context = context;
+        scopeChecker.setVarCounter(new Counter(context.getVarCounter()));
+        this.overwrite = overwrite;
+    }
 
     @Override
     public Expression visit(FromClause fromClause, Void arg) throws AsterixException {
@@ -88,7 +106,7 @@ public class VariableCheckAndRewriteVisitor extends AbstractSqlppQueryExpression
         // Visit the left expression of a from term.
         fromTerm.setLeftExpression(fromTerm.getLeftExpression().accept(this, arg));
 
-        // Registers the data item variable
+        // Registers the data item variable.
         VariableExpr leftVar = fromTerm.getLeftVariable();
         scopeChecker.getCurrentScope().addNewVarSymbolToScope(leftVar.getVar());
 
@@ -249,6 +267,8 @@ public class VariableCheckAndRewriteVisitor extends AbstractSqlppQueryExpression
     @Override
     public Expression visit(Query q, Void arg) throws AsterixException {
         q.setBody(q.getBody().accept(this, arg));
+        q.setVarCounter(scopeChecker.getVarCounter());
+        context.setVarCounter(scopeChecker.getVarCounter());
         return null;
     }
 
@@ -316,8 +336,8 @@ public class VariableCheckAndRewriteVisitor extends AbstractSqlppQueryExpression
     @Override
     public Expression visit(LetClause letClause, Void arg) throws AsterixException {
         scopeChecker.extendCurrentScope();
-        scopeChecker.getCurrentScope().addNewVarSymbolToScope(letClause.getVarExpr().getVar());
         letClause.setBindingExpr(letClause.getBindingExpr().accept(this, arg));
+        scopeChecker.getCurrentScope().addNewVarSymbolToScope(letClause.getVarExpr().getVar());
         return null;
     }
 
@@ -449,7 +469,7 @@ public class VariableCheckAndRewriteVisitor extends AbstractSqlppQueryExpression
     public Expression visit(IndexAccessor ia, Void arg) throws AsterixException {
         ia.setExpr(ia.getExpr().accept(this, arg));
         if (ia.getIndexExpr() != null) {
-            ia.setIndexExpr(ia.getExpr());
+            ia.setIndexExpr(ia.getIndexExpr());
         }
         return ia;
     }
@@ -471,6 +491,9 @@ public class VariableCheckAndRewriteVisitor extends AbstractSqlppQueryExpression
 
     // Rewrites for global variable (e.g., dataset) references.
     private Expression datasetRewrite(Expression expr) throws AsterixException {
+        if (!overwrite) {
+            return expr;
+        }
         String funcName = "dataset";
         String dataverse = MetadataConstants.METADATA_DATAVERSE_NAME;
         FunctionSignature signature = new FunctionSignature(dataverse, funcName, 1);

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppAstVisitor.java
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppAstVisitor.java b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppAstVisitor.java
new file mode 100644
index 0000000..9907999
--- /dev/null
+++ b/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/base/AbstractSqlppAstVisitor.java
@@ -0,0 +1,107 @@
+/*
+ * 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.base;
+
+import org.apache.asterix.common.exceptions.AsterixException;
+import org.apache.asterix.lang.common.visitor.base.AbstractAstVisitor;
+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;
+
+/**
+ * A dummy abstract visitor to allow an implementation to only fill in necessary stuff.
+ */
+public abstract class AbstractSqlppAstVisitor<R, T> extends AbstractAstVisitor<R, T> implements ISqlppVisitor<R, T> {
+
+    @Override
+    public R visit(FromClause fromClause, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(FromTerm fromTerm, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(JoinClause joinClause, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(NestClause nestClause, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(Projection projection, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(SelectBlock selectBlock, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(SelectClause selectClause, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(SelectElement selectElement, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(SelectRegular selectRegular, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(SelectSetOperation selectSetOperation, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(SelectExpression selectStatement, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(UnnestClause unnestClause, T arg) throws AsterixException {
+        return null;
+    }
+
+    @Override
+    public R visit(HavingClause havingClause, T arg) throws AsterixException {
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-asterixdb/blob/caea8f0e/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
----------------------------------------------------------------------
diff --git a/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj b/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
index 62899d6..af68d20 100644
--- a/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
+++ b/asterix-lang-sqlpp/src/main/javacc/SQLPP.jj
@@ -149,7 +149,7 @@ import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
 
 
 
-public class SQLPPParser extends ScopeChecker implements IParser {
+class SQLPPParser extends ScopeChecker implements IParser {
 
     // optimizer hints
     private static final String AUTO_HINT = "auto";
@@ -736,14 +736,12 @@ List<VarIdentifier> ParameterList() throws ParseException:
       var = new VarIdentifier();
       var.setValue(token.image);
       paramList.add(var);
-      getCurrentScope().addNewVarSymbolToScope(var);
     }
   (<COMMA> <IDENTIFIER>
     {
       var = new VarIdentifier();
       var.setValue(token.image);
       paramList.add(var);
-      getCurrentScope().addNewVarSymbolToScope(var);
     }
   )*)? <RIGHTPAREN>
     {
@@ -897,7 +895,7 @@ InsertStatement InsertStatement() throws ParseException:
 {
   <INSERT> <INTO> nameComponents = QualifiedName() query = Query()
     {
-      query.setTopLevel(false);
+      query.setTopLevel(true);
       return new InsertStatement(nameComponents.first, nameComponents.second, query, getVarCounter());
     }
 }
@@ -914,9 +912,6 @@ DeleteStatement DeleteStatement() throws ParseException:
 }
 {
   <DELETE> var = Variable()
-    {
-      getCurrentScope().addNewVarSymbolToScope(var.getVar());
-    }
   <FROM> nameComponents  = QualifiedName()
   (<WHERE> condition = Expression())?
     {
@@ -1507,7 +1502,6 @@ Query Query() throws ParseException:
   )
   {
     query.setBody(expr);
-    query.setVarCounter(getVarCounter());
     // we remove the pointers to the locked entities before we return the query object
     setDataverses(null);
     setDatasets(null);
@@ -1909,7 +1903,7 @@ VariableExpr Variable() throws ParseException:
        varExp.setIsNewVar(false);
      }
      varExp.setVar(var);
-     var.setValue(token.image);
+     var.setValue(id);
      return varExp;
     }
 }
@@ -2469,9 +2463,7 @@ GroupbyClause GroupbyClause()throws ParseException :
        expr = Expression()
        (LOOKAHEAD(1) (<AS>)?
         var = Variable()
-        {
-            newScope.addNewVarSymbolToScope(var.getVar());
-        })?
+        )?
         {
             GbyVariableExpressionPair pair1 = new GbyVariableExpressionPair(var, expr);
             vePairList.add(pair1);
@@ -2480,9 +2472,7 @@ GroupbyClause GroupbyClause()throws ParseException :
          expr = Expression()
          (LOOKAHEAD(1)  (<AS>)?
          var = Variable()
-         {
-             newScope.addNewVarSymbolToScope(var.getVar());
-         })?
+         )?
          {
              GbyVariableExpressionPair pair2 = new GbyVariableExpressionPair(var, expr);
              vePairList.add(pair2);
@@ -2544,14 +2534,12 @@ QuantifiedExpression QuantifiedExpression()throws ParseException:
     var = Variable() <IN> inExpr = Expression()
     {
       pair = new QuantifiedPair(var, inExpr);
-      getCurrentScope().addNewVarSymbolToScope(var.getVar());
       quantifiedList.add(pair);
     }
     (
     <COMMA> var = Variable() <IN> inExpr = Expression()
     {
       pair = new QuantifiedPair(var, inExpr);
-      getCurrentScope().addNewVarSymbolToScope(var.getVar());
       quantifiedList.add(pair);
     }
     )*
@@ -2574,7 +2562,6 @@ LetClause LetElement() throws ParseException:
 {
     varExp = Variable() <EQ> beExp = Expression()
     {
-      getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
       lc.setVarExpr(varExp);
       lc.setBindingExpr(beExp);
       return lc;
@@ -2591,7 +2578,6 @@ LetClause WithElement() throws ParseException:
 {
     varExp = Variable() <AS> beExp = Expression()
     {
-      getCurrentScope().addNewVarSymbolToScope(varExp.getVar());
       lc.setVarExpr(varExp);
       lc.setBindingExpr(beExp);
       return lc;


Mime
View raw message