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

[4/4] asterixdb git commit: Support SQL-compliant group-by syntax.

Support SQL-compliant group-by syntax.

1. Add AST-level deep equality and expression subsutitions;
2. Fix DeepCopyVisitor and InlineColumnAliasVisitor for edge cases;
3. Add deep equality tests;
4. Add group-by tests for the SQL-compliant syntax.

Change-Id: Ia1cbe1fab216b5f47577d75fd870a537cfe1e84f
Reviewed-on: https://asterix-gerrit.ics.uci.edu/990
Sonar-Qube: Jenkins <je...@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <je...@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <je...@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <ti...@apache.org>


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

Branch: refs/heads/master
Commit: 5b2d4c89cefb8d7780e76b8c81ebe064c24d278a
Parents: 6d999ed
Author: Yingyi Bu <yi...@couchbase.com>
Authored: Wed Jul 13 17:56:48 2016 -0700
Committer: Yingyi Bu <bu...@gmail.com>
Committed: Wed Jul 13 19:29:42 2016 -0700

----------------------------------------------------------------------
 .../asterix/test/sqlpp/ParserTestExecutor.java  |  11 +-
 .../queries_sqlpp/dapd/q2-6/q2-6.1.ddl.sqlpp    |  55 ++++
 .../queries_sqlpp/dapd/q2-6/q2-6.2.update.sqlpp |  19 ++
 .../queries_sqlpp/dapd/q2-6/q2-6.3.query.sqlpp  |  35 +++
 .../group-by/gby-expr-2/gby-expr-2.1.ddl.sqlpp  |  39 +++
 .../gby-expr-2/gby-expr-2.2.update.sqlpp        |  43 +++
 .../gby-expr-2/gby-expr-2.3.query.sqlpp         |  27 ++
 .../group-by/gby-expr-3/gby-expr-3.1.ddl.sqlpp  |  39 +++
 .../gby-expr-3/gby-expr-3.2.update.sqlpp        |  43 +++
 .../gby-expr-3/gby-expr-3.3.query.sqlpp         |  33 +++
 .../group-by/gby-expr/gby-expr.1.ddl.sqlpp      |  39 +++
 .../group-by/gby-expr/gby-expr.2.update.sqlpp   |  43 +++
 .../group-by/gby-expr/gby-expr.3.query.sqlpp    |  27 ++
 .../group-by/sugar-01-2/sugar-01-2.1.ddl.sqlpp  |  29 ++
 .../sugar-01-2/sugar-01-2.2.update.sqlpp        |  32 +++
 .../sugar-01-2/sugar-01-2.3.query.sqlpp         |  26 ++
 .../group-by/sugar-02-2/sugar-02-2.1.ddl.sqlpp  |  39 +++
 .../sugar-02-2/sugar-02-2.2.update.sqlpp        |  43 +++
 .../sugar-02-2/sugar-02-2.3.query.sqlpp         |  27 ++
 .../sugar-02-2/sugar-02-2.4.query.sqlpp         |  31 +++
 .../group-by/sugar-03-2/sugar-03-2.1.ddl.sqlpp  |  40 +++
 .../sugar-03-2/sugar-03-2.2.update.sqlpp        |  42 +++
 .../sugar-03-2/sugar-03-2.3.query.sqlpp         |  32 +++
 .../group-by/sugar-04-2/sugar-04-2.1.ddl.sqlpp  |  40 +++
 .../sugar-04-2/sugar-04-2.2.update.sqlpp        |  55 ++++
 .../sugar-04-2/sugar-04-2.3.query.sqlpp         |  31 +++
 .../group-by/sugar-05-2/sugar-05-2.1.ddl.sqlpp  |  35 +++
 .../sugar-05-2/sugar-05-2.2.update.sqlpp        |  59 ++++
 .../sugar-05-2/sugar-05-2.3.query.sqlpp         |  38 +++
 .../alias_negative/alias_negative.1.query.sqlpp |  24 ++
 .../q01_pricing_summary_report_2.1.ddl.sqlpp    |  47 ++++
 .../q01_pricing_summary_report_2.2.update.sqlpp |  24 ++
 .../q01_pricing_summary_report_2.3.query.sqlpp  |  43 +++
 .../group-by/gby-expr-2/gby-expr-2.1.adm        |   1 +
 .../group-by/gby-expr-3/gby-expr-3.1.adm        |   1 +
 .../results/group-by/gby-expr/gby-expr.1.adm    |   1 +
 .../resources/runtimets/testsuite_sqlpp.xml     |  56 ++++
 .../lang/common/clause/GroupbyClause.java       |  32 ++-
 .../asterix/lang/common/clause/LetClause.java   |  18 ++
 .../asterix/lang/common/clause/LimitClause.java |  27 +-
 .../lang/common/clause/OrderbyClause.java       |  20 ++
 .../lang/common/clause/UpdateClause.java        |  22 ++
 .../asterix/lang/common/clause/WhereClause.java |  31 ++-
 .../common/expression/AbstractAccessor.java     |  18 ++
 .../lang/common/expression/CallExpr.java        |  21 +-
 .../lang/common/expression/FieldAccessor.java   |  18 ++
 .../lang/common/expression/FieldBinding.java    |  24 ++
 .../expression/GbyVariableExpressionPair.java   |  22 ++
 .../asterix/lang/common/expression/IfExpr.java  |  25 ++
 .../lang/common/expression/IndexAccessor.java   |  36 ++-
 .../lang/common/expression/ListConstructor.java |  19 ++
 .../lang/common/expression/LiteralExpr.java     |  19 ++
 .../lang/common/expression/OperatorExpr.java    |  19 ++
 .../expression/OrderedListTypeDefinition.java   |  19 +-
 .../common/expression/QuantifiedExpression.java |  20 ++
 .../common/expression/RecordConstructor.java    |  18 ++
 .../common/expression/RecordTypeDefinition.java |  38 ++-
 .../lang/common/expression/TypeExpression.java  |   4 +-
 .../expression/TypeReferenceExpression.java     |  20 +-
 .../lang/common/expression/UnaryExpr.java       |  19 ++
 .../expression/UnorderedListTypeDefinition.java |  19 +-
 .../lang/common/expression/VariableExpr.java    |   8 +-
 .../lang/common/literal/DoubleLiteral.java      |  26 +-
 .../lang/common/literal/FalseLiteral.java       |  12 +-
 .../lang/common/literal/FloatLiteral.java       |  26 +-
 .../lang/common/literal/IntegerLiteral.java     |  27 +-
 .../lang/common/literal/LongIntegerLiteral.java |  26 +-
 .../lang/common/literal/MissingLiteral.java     |   8 +-
 .../lang/common/literal/NullLiteral.java        |  12 +-
 .../lang/common/literal/StringLiteral.java      |  24 ++
 .../lang/common/literal/TrueLiteral.java        |   3 +-
 .../ExpressionSubstitutionEnvironment.java      | 165 +++++++++++
 .../common/rewrites/LangRewritingContext.java   |   4 +-
 .../VariableSubstitutionEnvironment.java        |   2 +-
 .../lang/common/statement/DeleteStatement.java  |  22 ++
 .../lang/common/statement/InsertStatement.java  |  19 ++
 .../asterix/lang/common/statement/Query.java    |  25 +-
 .../lang/common/statement/UpdateStatement.java  |  19 ++
 .../lang/common/statement/UpsertStatement.java  |  16 ++
 .../lang/common/struct/AdmSplitInfo.java        |  37 ---
 .../asterix/lang/common/struct/Identifier.java  |  14 +-
 .../lang/common/struct/QuantifiedPair.java      |  15 +
 .../lang/common/struct/VarIdentifier.java       |   6 +-
 .../util/VariableCloneAndSubstitutionUtil.java  |  17 +-
 .../CloneAndSubstituteVariablesVisitor.java     |  72 +++--
 .../visitor/SubstituteExpressionVisitor.java    | 245 +++++++++++++++++
 .../clause/AbstractBinaryCorrelateClause.java   |  19 ++
 ...tractBinaryCorrelateWithConditionClause.java |  17 ++
 .../asterix/lang/sqlpp/clause/FromClause.java   |  19 +-
 .../asterix/lang/sqlpp/clause/FromTerm.java     |  26 +-
 .../asterix/lang/sqlpp/clause/HavingClause.java |  17 ++
 .../asterix/lang/sqlpp/clause/Projection.java   |  19 ++
 .../asterix/lang/sqlpp/clause/SelectBlock.java  |  67 ++++-
 .../asterix/lang/sqlpp/clause/SelectClause.java |  21 +-
 .../lang/sqlpp/clause/SelectElement.java        |  17 ++
 .../lang/sqlpp/clause/SelectRegular.java        |  17 ++
 .../lang/sqlpp/clause/SelectSetOperation.java   |  36 ++-
 .../sqlpp/expression/IndependentSubquery.java   |  17 ++
 .../lang/sqlpp/expression/SelectExpression.java |  46 +++-
 .../rewrites/SqlppFunctionBodyRewriter.java     |   6 +
 .../lang/sqlpp/rewrites/SqlppQueryRewriter.java |  37 ++-
 .../visitor/GenerateColumnNameVisitor.java      |  66 +++++
 .../visitor/InlineColumnAliasVisitor.java       |  47 ++--
 .../visitor/InlineWithExpressionVisitor.java    |  13 +-
 ...uteGroupbyExpressionWithVariableVisitor.java |  89 ++++++
 .../lang/sqlpp/struct/SetOperationInput.java    |  23 ++
 .../lang/sqlpp/struct/SetOperationRight.java    |  27 ++
 .../sqlpp/util/ExpressionToVariableUtil.java    |  65 ++++-
 .../lang/sqlpp/util/SqlppAstPrintUtil.java      |  14 +-
 .../lang/sqlpp/util/SqlppFormatPrintUtil.java   |   6 +-
 .../lang/sqlpp/util/SqlppRewriteUtil.java       |   4 +-
 .../lang/sqlpp/visitor/DeepCopyVisitor.java     |  30 +-
 ...SqlppCloneAndSubstituteVariablesVisitor.java |  98 ++++---
 .../sqlpp/visitor/SqlppFormatPrintVisitor.java  |   5 +-
 .../SqlppSubstituteExpressionsVisitor.java      | 272 +++++++++++++++++++
 .../AbstractSqlppExpressionScopingVisitor.java  |   4 +-
 .../AbstractSqlppSimpleExpressionVisitor.java   |   4 +-
 .../asterix-lang-sqlpp/src/main/javacc/SQLPP.jj |  15 +-
 118 files changed, 3486 insertions(+), 304 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/sqlpp/ParserTestExecutor.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/sqlpp/ParserTestExecutor.java b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/sqlpp/ParserTestExecutor.java
index fa7b272..0989545 100644
--- a/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/sqlpp/ParserTestExecutor.java
+++ b/asterixdb/asterix-app/src/test/java/org/apache/asterix/test/sqlpp/ParserTestExecutor.java
@@ -45,6 +45,7 @@ import org.apache.asterix.lang.common.util.FunctionUtil;
 import org.apache.asterix.lang.sqlpp.parser.SqlppParserFactory;
 import org.apache.asterix.lang.sqlpp.rewrites.SqlppRewriterFactory;
 import org.apache.asterix.lang.sqlpp.util.SqlppAstPrintUtil;
+import org.apache.asterix.lang.sqlpp.util.SqlppRewriteUtil;
 import org.apache.asterix.metadata.declared.AqlMetadataProvider;
 import org.apache.asterix.metadata.entities.Dataset;
 import org.apache.asterix.test.aql.TestExecutor;
@@ -52,6 +53,7 @@ import org.apache.asterix.testframework.context.TestCaseContext;
 import org.apache.asterix.testframework.context.TestFileContext;
 import org.apache.asterix.testframework.xml.TestCase.CompilationUnit;
 import org.apache.asterix.testframework.xml.TestGroup;
+import org.junit.Assert;
 
 import junit.extensions.PA;
 
@@ -80,8 +82,8 @@ public class ParserTestExecutor extends TestExecutor {
 
                     // Runs the test query.
                     File expectedResultFile = expectedResultFileCtxs.get(queryCount).getFile();
-                    File actualResultFile = testCaseCtx.getActualResultFile(cUnit, expectedResultFile,
-                            new File(actualPath));
+                    File actualResultFile =
+                            testCaseCtx.getActualResultFile(cUnit, expectedResultFile, new File(actualPath));
                     testSQLPPParser(testFile, actualResultFile, expectedResultFile);
 
                     LOGGER.info(
@@ -132,6 +134,11 @@ public class ParserTestExecutor extends TestExecutor {
                     IQueryRewriter rewriter = sqlppRewriterFactory.createQueryRewriter();
                     rewrite(rewriter, functions, query, aqlMetadataProvider,
                             new LangRewritingContext(query.getVarCounter()));
+
+                    // Tests deep copy and deep equality.
+                    Query copiedQuery = (Query) SqlppRewriteUtil.deepCopy(query);
+                    Assert.assertEquals(query.hashCode(), copiedQuery.hashCode());
+                    Assert.assertEquals(query, copiedQuery);
                 }
                 SqlppAstPrintUtil.print(st, writer);
             }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.1.ddl.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.1.ddl.sqlpp
new file mode 100644
index 0000000..90482a7
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.1.ddl.sqlpp
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+drop  database test if exists;
+create  database test;
+
+use test;
+
+
+create type test.AddressType as
+ closed {
+  street : string,
+  city : string,
+  zip : string,
+  latlong : point
+}
+
+create type test.EventType as
+ closed {
+  event_id : int64,
+  name : string,
+  location : AddressType?,
+  organizers : {{{
+          name : string
+      }
+}},
+  sponsoring_sigs : [{
+          sig_id : int64,
+          chapter_name : string
+      }
+],
+  interest_keywords : {{string}},
+  price : double?,
+  start_time : datetime,
+  end_time : datetime
+}
+
+create external  table Event(EventType) using localfs((`path`=`asterix_nc1://data/events/tiny/event.adm`),(`format`=`adm`));
+

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.2.update.sqlpp
new file mode 100644
index 0000000..bd244d0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.2.update.sqlpp
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.3.query.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.3.query.sqlpp
new file mode 100644
index 0000000..1c7e44a
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/dapd/q2-6/q2-6.3.query.sqlpp
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+USE test;
+
+
+SELECT sponsor.sig_id, COUNT(1) total_count,
+       (
+         SELECT e.sponsor.chapter_name, COUNT(e) AS escount
+         FROM es AS e
+         GROUP BY e.sponsor.chapter_name
+       ) chapter_breakdown
+FROM  Event,
+      Event.sponsoring_sigs AS sponsor
+GROUP BY sponsor.sig_id GROUP AS es
+ORDER BY total_count DESC
+LIMIT 5
+;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-2/gby-expr-2.1.ddl.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-2/gby-expr-2.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-2/gby-expr-2.1.ddl.sqlpp
new file mode 100644
index 0000000..b4ebc49
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-2/gby-expr-2.1.ddl.sqlpp
@@ -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.
+ */
+
+DROP DATABASE gby IF EXISTS;
+CREATE DATABASE gby;
+
+USE gby;
+
+CREATE TYPE EmployeeType AS {
+  id : string
+}
+
+CREATE TYPE IncentiveType AS {
+  job_category: string
+}
+
+CREATE TYPE SuperStarType AS {
+  id : string
+}
+
+CREATE TABLE Employee(EmployeeType) PRIMARY KEY id;
+CREATE TABLE Incentive(IncentiveType) PRIMARY KEY job_category;
+CREATE TABLE SuperStars(SuperStarType) PRIMARY KEY id;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-2/gby-expr-2.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-2/gby-expr-2.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-2/gby-expr-2.2.update.sqlpp
new file mode 100644
index 0000000..e6545e0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-2/gby-expr-2.2.update.sqlpp
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+USE gby;
+
+INSERT INTO Employee (
+   {
+    'id': '123',
+    'job_category': 'Cook',
+    'salary': 2000,
+    'department_id': 'K55'
+   }
+);
+
+INSERT INTO Incentive (
+  { 'job_category': 'Cook',
+    'bonus': 1000
+  }
+)
+
+INSERT INTO SuperStars (
+  {
+    'id': '123'
+  }
+)
+

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

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.1.ddl.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.1.ddl.sqlpp
new file mode 100644
index 0000000..b4ebc49
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.1.ddl.sqlpp
@@ -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.
+ */
+
+DROP DATABASE gby IF EXISTS;
+CREATE DATABASE gby;
+
+USE gby;
+
+CREATE TYPE EmployeeType AS {
+  id : string
+}
+
+CREATE TYPE IncentiveType AS {
+  job_category: string
+}
+
+CREATE TYPE SuperStarType AS {
+  id : string
+}
+
+CREATE TABLE Employee(EmployeeType) PRIMARY KEY id;
+CREATE TABLE Incentive(IncentiveType) PRIMARY KEY job_category;
+CREATE TABLE SuperStars(SuperStarType) PRIMARY KEY id;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.2.update.sqlpp
new file mode 100644
index 0000000..e6545e0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.2.update.sqlpp
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+USE gby;
+
+INSERT INTO Employee (
+   {
+    'id': '123',
+    'job_category': 'Cook',
+    'salary': 2000,
+    'department_id': 'K55'
+   }
+);
+
+INSERT INTO Incentive (
+  { 'job_category': 'Cook',
+    'bonus': 1000
+  }
+)
+
+INSERT INTO SuperStars (
+  {
+    'id': '123'
+  }
+)
+

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.3.query.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.3.query.sqlpp
new file mode 100644
index 0000000..c2fe80b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr-3/gby-expr-3.3.query.sqlpp
@@ -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.
+ */
+
+USE gby;
+
+/*
+ *  This query tests that the expression substitution rewriter should
+ *  try to visit (i.e., substitute) parent expressions first before visiting
+ *  child expressions, e.g.: i.bonus + e.salary is a parent expression of i.bonus.
+ */
+
+FROM Employee e
+    JOIN Incentive i ON e.job_category = i.job_category
+    JOIN SuperStars s ON e.id = s.id
+GROUP BY i.bonus, i.bonus + e.salary
+SELECT i.bonus, i.bonus + e.salary, COUNT(1);
+

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr/gby-expr.1.ddl.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr/gby-expr.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr/gby-expr.1.ddl.sqlpp
new file mode 100644
index 0000000..b4ebc49
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr/gby-expr.1.ddl.sqlpp
@@ -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.
+ */
+
+DROP DATABASE gby IF EXISTS;
+CREATE DATABASE gby;
+
+USE gby;
+
+CREATE TYPE EmployeeType AS {
+  id : string
+}
+
+CREATE TYPE IncentiveType AS {
+  job_category: string
+}
+
+CREATE TYPE SuperStarType AS {
+  id : string
+}
+
+CREATE TABLE Employee(EmployeeType) PRIMARY KEY id;
+CREATE TABLE Incentive(IncentiveType) PRIMARY KEY job_category;
+CREATE TABLE SuperStars(SuperStarType) PRIMARY KEY id;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr/gby-expr.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr/gby-expr.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr/gby-expr.2.update.sqlpp
new file mode 100644
index 0000000..e6545e0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-expr/gby-expr.2.update.sqlpp
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+USE gby;
+
+INSERT INTO Employee (
+   {
+    'id': '123',
+    'job_category': 'Cook',
+    'salary': 2000,
+    'department_id': 'K55'
+   }
+);
+
+INSERT INTO Incentive (
+  { 'job_category': 'Cook',
+    'bonus': 1000
+  }
+)
+
+INSERT INTO SuperStars (
+  {
+    'id': '123'
+  }
+)
+

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

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

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-01-2/sugar-01-2.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-01-2/sugar-01-2.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-01-2/sugar-01-2.2.update.sqlpp
new file mode 100644
index 0000000..99116f6
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-01-2/sugar-01-2.2.update.sqlpp
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+USE gby;
+
+INSERT INTO Employee (
+  [
+   { 'name': 'Bill',
+     'deptno': 'K55',
+     'salary': 2000 },
+
+   { 'name': 'Fred',
+     'deptno': 'K55',
+     'salary': 3000 }
+  ]
+);
\ No newline at end of file

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

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02-2/sugar-02-2.1.ddl.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02-2/sugar-02-2.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02-2/sugar-02-2.1.ddl.sqlpp
new file mode 100644
index 0000000..b4ebc49
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02-2/sugar-02-2.1.ddl.sqlpp
@@ -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.
+ */
+
+DROP DATABASE gby IF EXISTS;
+CREATE DATABASE gby;
+
+USE gby;
+
+CREATE TYPE EmployeeType AS {
+  id : string
+}
+
+CREATE TYPE IncentiveType AS {
+  job_category: string
+}
+
+CREATE TYPE SuperStarType AS {
+  id : string
+}
+
+CREATE TABLE Employee(EmployeeType) PRIMARY KEY id;
+CREATE TABLE Incentive(IncentiveType) PRIMARY KEY job_category;
+CREATE TABLE SuperStars(SuperStarType) PRIMARY KEY id;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02-2/sugar-02-2.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02-2/sugar-02-2.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02-2/sugar-02-2.2.update.sqlpp
new file mode 100644
index 0000000..e6545e0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-02-2/sugar-02-2.2.update.sqlpp
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+USE gby;
+
+INSERT INTO Employee (
+   {
+    'id': '123',
+    'job_category': 'Cook',
+    'salary': 2000,
+    'department_id': 'K55'
+   }
+);
+
+INSERT INTO Incentive (
+  { 'job_category': 'Cook',
+    'bonus': 1000
+  }
+)
+
+INSERT INTO SuperStars (
+  {
+    'id': '123'
+  }
+)
+

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

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

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

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-03-2/sugar-03-2.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-03-2/sugar-03-2.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-03-2/sugar-03-2.2.update.sqlpp
new file mode 100644
index 0000000..19f0c1f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-03-2/sugar-03-2.2.update.sqlpp
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+USE gby;
+
+INSERT INTO Employee (
+   {
+    'id': '123',
+    'job_category': 'Cook',
+    'salary': 2000,
+    'department_id': 'K55'
+   }
+);
+
+INSERT INTO Incentive (
+  { 'job_category': 'Cook',
+    'bonus': 1000
+  }
+)
+
+INSERT INTO SuperStars (
+  {
+    'id': '123'
+  }
+)
+

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

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

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04-2/sugar-04-2.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04-2/sugar-04-2.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04-2/sugar-04-2.2.update.sqlpp
new file mode 100644
index 0000000..52443b5
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04-2/sugar-04-2.2.update.sqlpp
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+USE gby;
+
+INSERT INTO Employee (
+  [
+   {
+    'id': '123',
+    'job_category': 'Cook',
+    'salary': 2000,
+    'department_id': 'K55'
+   },
+   {
+    'id': '234',
+    'job_category': 'Teach',
+    'salary': 2500,
+    'department_id': 'K54'
+   }
+  ]
+);
+
+INSERT INTO Incentive (
+ [
+  { 'job_category': 'Cook',
+    'bonus': 1000
+  },
+  { 'job_category': 'Teach',
+    'bonus': 2000
+  }
+ ]
+)
+
+INSERT INTO SuperStars (
+  {
+    'id': '123'
+  }
+)
+

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

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

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-05-2/sugar-05-2.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-05-2/sugar-05-2.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-05-2/sugar-05-2.2.update.sqlpp
new file mode 100644
index 0000000..275e4fc
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-05-2/sugar-05-2.2.update.sqlpp
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+USE gby;
+
+INSERT INTO Car (
+  [
+   {
+    'cid': '1',
+    'make': 'Honda',
+    'model': 'Civic',
+    'price': 28000,
+    'tire_size': 'P205/55R16'
+   },
+   {
+    'cid': '2',
+    'make': 'Toyota',
+    'model': 'Corolla',
+    'price': 27000,
+    'tire_size': 'P205/55R16'
+   }
+  ]
+);
+
+INSERT INTO Tire (
+ [
+   {
+        'tid': '1',
+        'mfr': 'Bridgestone',
+        'brand': 'Turanza',
+        'size': 'P205/55R16',
+        'price': 150
+    },
+    {
+        'tid': '2',
+        'mfr': 'Goodyear',
+        'brand': 'Eagle',
+        'size': 'P205/55R16',
+        'price': 100
+    }
+ ]
+)
+

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-05-2/sugar-05-2.3.query.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-05-2/sugar-05-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-05-2/sugar-05-2.3.query.sqlpp
new file mode 100644
index 0000000..4bc3555
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-05-2/sugar-05-2.3.query.sqlpp
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+USE gby;
+
+SELECT ELEMENT {
+        'Vehicle prices including 4 tires':
+        (
+            FROM Car c JOIN Tire t ON c.tire_size = t.size
+            GROUP BY c.tire_size GROUP AS g
+            SELECT c.tire_size,
+                   AVG(c.price + 4 * t.price) AS avg_total_price,
+                   ( FROM g AS g
+                     SELECT g.c.make AS make,
+                            g.c.model AS model,
+                            g.t.mfr AS mfr,
+                            g.t.brand AS brand,
+                            g.c.price + 4 * g.t.price AS price
+                     ORDER BY g.c.make, g.c.model, g.t.mfr, g.t.brand
+                   ) AS combinations
+        )
+    };

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/alias_negative/alias_negative.1.query.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/alias_negative/alias_negative.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/alias_negative/alias_negative.1.query.sqlpp
new file mode 100644
index 0000000..a05a418
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/alias_negative/alias_negative.1.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+USE test;
+
+/** This test case checks the error message for a missing alias.*/
+
+SELECT * FROM (SELECT 1);

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.1.ddl.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.1.ddl.sqlpp
new file mode 100644
index 0000000..8649a3b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.1.ddl.sqlpp
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+drop  database tpch if exists;
+create  database tpch;
+
+use tpch;
+
+
+create type tpch.LineItemType as
+ closed {
+  l_orderkey : int64,
+  l_partkey : int64,
+  l_suppkey : int64,
+  l_linenumber : int64,
+  l_quantity : double,
+  l_extendedprice : double,
+  l_discount : double,
+  l_tax : double,
+  l_returnflag : string,
+  l_linestatus : string,
+  l_shipdate : string,
+  l_commitdate : string,
+  l_receiptdate : string,
+  l_shipinstruct : string,
+  l_shipmode : string,
+  l_comment : string
+}
+
+create  table LineItem(LineItemType) primary key l_orderkey,l_linenumber;
+

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.2.update.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.2.update.sqlpp
new file mode 100644
index 0000000..d996e74
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.2.update.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use tpch;
+
+
+load  table LineItem using localfs ((`path`=`asterix_nc1://data/tpch0.001/lineitem.tbl`),(`format`=`delimited-text`),(`delimiter`=`|`)) pre-sorted;
+

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.3.query.sqlpp
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.3.query.sqlpp
new file mode 100644
index 0000000..3999ace
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/q01_pricing_summary_report_2/q01_pricing_summary_report_2.3.query.sqlpp
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+USE tpch;
+
+/**
+ * This query tests the different treatments of l.l_returnflag
+ * for the case that it is in a non-aggregate projection expression and
+ * the case that it is in a SQL-92 aggregate function projection.
+ */
+
+SELECT  l.l_returnflag,
+        l.l_linestatus,
+        sum(l_quantity) AS sum_qty,
+        sum(l_extendedprice) AS sum_base_price,
+        sum(l_extendedprice * (1 - l_discount)) AS sum_disc_price,
+        sum(l_extendedprice * (1 - l_discount) * (1 + l_tax)) AS sum_charge,
+        avg(l_quantity) AS ave_qty,
+        avg(l_extendedprice) AS ave_price,
+        avg(l_discount) AS ave_disc,
+        count(l.l_returnflag) AS count_order
+FROM  LineItem l
+WHERE l_shipdate <= '1998-09-02'
+/* +hash */
+GROUP BY l.l_returnflag, l.l_linestatus
+ORDER BY l.l_returnflag, l.l_linestatus
+;

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-expr-2/gby-expr-2.1.adm
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-expr-2/gby-expr-2.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-expr-2/gby-expr-2.1.adm
new file mode 100644
index 0000000..fe9bd8c
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-expr-2/gby-expr-2.1.adm
@@ -0,0 +1 @@
+{ "$1": "K55", "$2": 3000 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-expr-3/gby-expr-3.1.adm
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-expr-3/gby-expr-3.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-expr-3/gby-expr-3.1.adm
new file mode 100644
index 0000000..fdb2461
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-expr-3/gby-expr-3.1.adm
@@ -0,0 +1 @@
+{ "$2": 1, "bonus": 1000, "$1": 3000 }

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

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index 494e830..f66e3a8 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -1419,6 +1419,11 @@
         <output-dir compare="Text">q2</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="dapd">
+      <compilation-unit name="q2-6">
+        <output-dir compare="Text">q2</output-dir>
+      </compilation-unit>
+    </test-case>
     <!--
         <test-case FilePath="dapd">
           <compilation-unit name="q3">
@@ -2388,30 +2393,70 @@
       </compilation-unit>
     </test-case>
     <test-case FilePath="group-by">
+      <compilation-unit name="sugar-01-2">
+        <output-dir compare="Text">core-01</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="group-by">
       <compilation-unit name="sugar-02">
         <output-dir compare="Text">core-02</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="group-by">
+      <compilation-unit name="sugar-02-2">
+        <output-dir compare="Text">core-02</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="group-by">
       <compilation-unit name="sugar-03">
         <output-dir compare="Text">core-03</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="group-by">
+      <compilation-unit name="sugar-03-2">
+        <output-dir compare="Text">core-03</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="group-by">
       <compilation-unit name="sugar-04">
         <output-dir compare="Text">core-04</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="group-by">
+      <compilation-unit name="sugar-04-2">
+        <output-dir compare="Text">core-04</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="group-by">
       <compilation-unit name="sugar-05">
         <output-dir compare="Text">core-05</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="group-by">
+      <compilation-unit name="sugar-05-2">
+        <output-dir compare="Text">core-05</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="group-by">
       <compilation-unit name="null">
         <output-dir compare="Text">null</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="group-by">
+      <compilation-unit name="gby-expr">
+        <output-dir compare="Text">gby-expr</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="group-by">
+      <compilation-unit name="gby-expr-2">
+        <output-dir compare="Text">gby-expr-2</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="group-by">
+      <compilation-unit name="gby-expr-3">
+        <output-dir compare="Text">gby-expr-3</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="index-join">
     <test-case FilePath="index-join">
@@ -5271,6 +5316,12 @@
         <output-dir compare="Text">not_in</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="subquery">
+      <compilation-unit name="alias_negative">
+        <output-dir compare="Text">alias_negative</output-dir>
+        <expected-error>Need an alias for the enclosed expression</expected-error>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="subset-collection">
     <test-case FilePath="subset-collection">
@@ -5852,6 +5903,11 @@
       </compilation-unit>
     </test-case>
     <test-case FilePath="tpch-sql-sugar">
+      <compilation-unit name="q01_pricing_summary_report_2">
+        <output-dir compare="Text">q01_pricing_summary_report_nt</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="tpch-sql-sugar">
       <compilation-unit name="q20_potential_part_promotion">
         <output-dir compare="Text">q20_potential_part_promotion</output-dir>
       </compilation-unit>

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/GroupbyClause.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/GroupbyClause.java b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/GroupbyClause.java
index b5d78e2..3565c5a 100644
--- a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/GroupbyClause.java
+++ b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/GroupbyClause.java
@@ -28,6 +28,7 @@ import org.apache.asterix.lang.common.expression.GbyVariableExpressionPair;
 import org.apache.asterix.lang.common.expression.VariableExpr;
 import org.apache.asterix.lang.common.struct.Identifier;
 import org.apache.asterix.lang.common.visitor.base.ILangVisitor;
+import org.apache.commons.lang3.ObjectUtils;
 import org.apache.hyracks.algebricks.common.utils.Pair;
 
 public class GroupbyClause implements Clause {
@@ -36,11 +37,12 @@ public class GroupbyClause implements Clause {
     private List<GbyVariableExpressionPair> decorPairList;
     private List<VariableExpr> withVarList;
     private VariableExpr groupVar;
-    private List<Pair<Expression, Identifier>> groupFieldList = new ArrayList<Pair<Expression, Identifier>>();
+    private List<Pair<Expression, Identifier>> groupFieldList = new ArrayList<>();
     private boolean hashGroupByHint;
     private boolean groupAll;
 
     public GroupbyClause() {
+        // Default constructor.
     }
 
     public GroupbyClause(List<GbyVariableExpressionPair> gbyPairList, List<GbyVariableExpressionPair> decorPairList,
@@ -122,11 +124,11 @@ public class GroupbyClause implements Clause {
     }
 
     public boolean hasDecorList() {
-        return decorPairList != null && decorPairList.size() > 0;
+        return decorPairList != null && !decorPairList.isEmpty();
     }
 
     public boolean hasWithList() {
-        return withVarList != null && withVarList.size() > 0;
+        return withVarList != null && !withVarList.isEmpty();
     }
 
     public boolean hasGroupVar() {
@@ -134,10 +136,32 @@ public class GroupbyClause implements Clause {
     }
 
     public boolean hasGroupFieldList() {
-        return groupFieldList != null && groupFieldList.size() > 0;
+        return groupFieldList != null && !groupFieldList.isEmpty();
     }
 
     public boolean isGroupAll() {
         return groupAll;
     }
+
+    @Override
+    public int hashCode() {
+        return ObjectUtils.hashCodeMulti(decorPairList, gbyPairList, groupAll, groupFieldList, groupVar,
+                hashGroupByHint, withVarList);
+    }
+
+    @Override
+    public boolean equals(Object object) {
+        if (this == object) {
+            return true;
+        }
+        if (!(object instanceof GroupbyClause)) {
+            return false;
+        }
+        GroupbyClause target = (GroupbyClause) object;
+        boolean equals = ObjectUtils.equals(decorPairList, target.decorPairList)
+                && ObjectUtils.equals(gbyPairList, target.gbyPairList) && groupAll == target.groupAll
+                && ObjectUtils.equals(groupFieldList, target.groupFieldList);
+        return equals && ObjectUtils.equals(groupVar, target.groupVar) && hashGroupByHint == target.hashGroupByHint
+                && ObjectUtils.equals(withVarList, target.withVarList);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/LetClause.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/LetClause.java b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/LetClause.java
index 6a4866d..7043a34 100644
--- a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/LetClause.java
+++ b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/LetClause.java
@@ -23,6 +23,7 @@ import org.apache.asterix.lang.common.base.Clause;
 import org.apache.asterix.lang.common.base.Expression;
 import org.apache.asterix.lang.common.expression.VariableExpr;
 import org.apache.asterix.lang.common.visitor.base.ILangVisitor;
+import org.apache.commons.lang3.ObjectUtils;
 
 public class LetClause implements Clause {
     private VariableExpr varExpr;
@@ -64,4 +65,21 @@ public class LetClause implements Clause {
         return visitor.visit(this, arg);
     }
 
+    @Override
+    public int hashCode() {
+        return ObjectUtils.hashCodeMulti(bindExpr, varExpr);
+    }
+
+    @Override
+    public boolean equals(Object object) {
+        if (this == object) {
+            return true;
+        }
+        if (!(object instanceof LetClause)) {
+            return false;
+        }
+        LetClause target = (LetClause) object;
+        return bindExpr.equals(target.getBindingExpr()) && varExpr.equals(target.getVarExpr());
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/LimitClause.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/LimitClause.java b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/LimitClause.java
index 7ebedc2..f9123ef 100644
--- a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/LimitClause.java
+++ b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/LimitClause.java
@@ -22,25 +22,27 @@ import org.apache.asterix.common.exceptions.AsterixException;
 import org.apache.asterix.lang.common.base.Clause;
 import org.apache.asterix.lang.common.base.Expression;
 import org.apache.asterix.lang.common.visitor.base.ILangVisitor;
+import org.apache.commons.lang3.ObjectUtils;
 
 public class LimitClause implements Clause {
-    private Expression limitexpr;
+    private Expression limitExpr;
     private Expression offset;
 
     public LimitClause() {
+        // Default constructor.
     }
 
     public LimitClause(Expression limitexpr, Expression offset) {
-        this.limitexpr = limitexpr;
+        this.limitExpr = limitexpr;
         this.offset = offset;
     }
 
     public Expression getLimitExpr() {
-        return limitexpr;
+        return limitExpr;
     }
 
     public void setLimitExpr(Expression limitexpr) {
-        this.limitexpr = limitexpr;
+        this.limitExpr = limitexpr;
     }
 
     public Expression getOffset() {
@@ -64,4 +66,21 @@ public class LimitClause implements Clause {
     public <R, T> R accept(ILangVisitor<R, T> visitor, T arg) throws AsterixException {
         return visitor.visit(this, arg);
     }
+
+    @Override
+    public int hashCode() {
+        return ObjectUtils.hashCodeMulti(limitExpr, offset);
+    }
+
+    @Override
+    public boolean equals(Object object) {
+        if (this == object) {
+            return true;
+        }
+        if (!(object instanceof LimitClause)) {
+            return false;
+        }
+        LimitClause target = (LimitClause) object;
+        return limitExpr.equals(target.getLimitExpr()) && ObjectUtils.equals(offset, target.getOffset());
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/OrderbyClause.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/OrderbyClause.java b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/OrderbyClause.java
index 58acd43..8574c76 100644
--- a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/OrderbyClause.java
+++ b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/OrderbyClause.java
@@ -24,6 +24,7 @@ import org.apache.asterix.common.exceptions.AsterixException;
 import org.apache.asterix.lang.common.base.Clause;
 import org.apache.asterix.lang.common.base.Expression;
 import org.apache.asterix.lang.common.visitor.base.ILangVisitor;
+import org.apache.commons.lang3.ObjectUtils;
 import org.apache.hyracks.dataflow.common.data.partition.range.IRangeMap;
 
 public class OrderbyClause implements Clause {
@@ -34,6 +35,7 @@ public class OrderbyClause implements Clause {
     private int numTuples = -1;
 
     public OrderbyClause() {
+        // Default constructor.
     }
 
     public OrderbyClause(List<Expression> orderbyList, List<OrderModifier> modifierList) {
@@ -95,4 +97,22 @@ public class OrderbyClause implements Clause {
     public void setRangeMap(IRangeMap rangeMap) {
         this.rangeMap = rangeMap;
     }
+
+    @Override
+    public int hashCode() {
+        return ObjectUtils.hashCodeMulti(modifierList, numFrames, numTuples, orderbyList);
+    }
+
+    @Override
+    public boolean equals(Object object) {
+        if (this == object) {
+            return true;
+        }
+        if (!(object instanceof OrderbyClause)) {
+            return false;
+        }
+        OrderbyClause target = (OrderbyClause) object;
+        return ObjectUtils.equals(modifierList, target.modifierList) && numFrames == target.numFrames
+                && numTuples == target.numTuples && orderbyList.equals(target.orderbyList);
+    }
 }

http://git-wip-us.apache.org/repos/asf/asterixdb/blob/5b2d4c89/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/UpdateClause.java
----------------------------------------------------------------------
diff --git a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/UpdateClause.java b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/UpdateClause.java
index 90576ce..50bf7a5 100644
--- a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/UpdateClause.java
+++ b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/clause/UpdateClause.java
@@ -25,6 +25,7 @@ import org.apache.asterix.lang.common.statement.DeleteStatement;
 import org.apache.asterix.lang.common.statement.InsertStatement;
 import org.apache.asterix.lang.common.statement.UpdateStatement;
 import org.apache.asterix.lang.common.visitor.base.ILangVisitor;
+import org.apache.commons.lang3.ObjectUtils;
 
 public class UpdateClause implements Clause {
 
@@ -115,4 +116,25 @@ public class UpdateClause implements Clause {
         return elsebranch != null;
     }
 
+    @Override
+    public int hashCode() {
+        return ObjectUtils.hashCodeMulti(condition, ds, elsebranch, ifbranch, is, target, us, value);
+    }
+
+    @Override
+    public boolean equals(Object object) {
+        if (this == object) {
+            return false;
+        }
+        if (!(object instanceof UpdateClause)) {
+            return false;
+        }
+        UpdateClause other = (UpdateClause) object;
+        boolean equals = ObjectUtils.equals(condition, other.condition) && ObjectUtils.equals(ds, other.ds)
+                && ObjectUtils.equals(elsebranch, other.elsebranch) && ObjectUtils.equals(ifbranch, other.ifbranch);
+        equals = equals && ObjectUtils.equals(is, other.is) && ObjectUtils.equals(target, other.target)
+                && ObjectUtils.equals(us, other.us);
+        return equals && ObjectUtils.equals(value, other.value);
+    }
+
 }