Fix issue785 -- using the algebricks fixes for subplans.
Fix RemoveRedundantListifyRule -- listify/scan-collection pair within a nested plan can be removed.
Fix issue550.
Add a regression test for issue638.

Change-Id: Id56539cd5ab7ed0cc05897b468c26aec169102fd
Reviewed-on: http://fulliautomatix.ics.uci.edu:8443/150
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Preston Carman <ecarm002@ucr.edu>
Reviewed-by: Till Westmann <westmann@gmail.com>
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java
index 0e2aabd..5cc5ede 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java
@@ -73,6 +73,7 @@
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.CopyLimitDownRule;
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.EliminateGroupByEmptyKeyRule;
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.EliminateSubplanRule;
+import edu.uci.ics.hyracks.algebricks.rewriter.rules.EliminateSubplanWithInputCardinalityOneRule;
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.EnforceOrderByAfterSubplan;
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.EnforceStructuralPropertiesRule;
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.ExtractCommonExpressionsRule;
@@ -99,6 +100,7 @@
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.PushProjectDownRule;
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.PushSelectDownRule;
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.PushSelectIntoJoinRule;
+import edu.uci.ics.hyracks.algebricks.rewriter.rules.PushSubplanIntoGroupByRule;
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.PushSubplanWithAggregateDownThroughProductRule;
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.PushUnnestDownThroughProductRule;
 import edu.uci.ics.hyracks.algebricks.rewriter.rules.ReinferAllTypesRule;
@@ -184,6 +186,9 @@
         condPushDownAndJoinInference.add(new NestGroupByRule());
         condPushDownAndJoinInference.add(new EliminateGroupByEmptyKeyRule());
         condPushDownAndJoinInference.add(new LeftOuterJoinToInnerJoinRule());
+        condPushDownAndJoinInference.add(new PushSubplanIntoGroupByRule());
+        condPushDownAndJoinInference.add(new NestedSubplanToJoinRule());
+        condPushDownAndJoinInference.add(new EliminateSubplanWithInputCardinalityOneRule());
 
         return condPushDownAndJoinInference;
     }
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/NestedSubplanToJoinRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/NestedSubplanToJoinRule.java
index c5d415e..6929158 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/NestedSubplanToJoinRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/NestedSubplanToJoinRule.java
@@ -82,6 +82,12 @@
 
             /** get the input operator of the subplan operator */
             ILogicalOperator subplanInput = subplan.getInputs().get(0).getValue();
+            AbstractLogicalOperator subplanInputOp = (AbstractLogicalOperator) subplanInput;
+
+            /** If the other join branch is a trivial plan, do not do the rewriting. */
+            if (subplanInputOp.getOperatorTag() == LogicalOperatorTag.EMPTYTUPLESOURCE) {
+                continue;
+            }
 
             /** get all nested top operators */
             List<ILogicalPlan> nestedPlans = subplan.getNestedPlans();
@@ -91,7 +97,7 @@
             }
             if (nestedRoots.size() == 0) {
                 /** there is no nested top operators */
-                return false;
+                continue;
             }
 
             /** expend the input and roots into a DAG of nested loop joins */
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/PushAggregateIntoGroupbyRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/PushAggregateIntoGroupbyRule.java
index 1312c32..f95490a 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/PushAggregateIntoGroupbyRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/PushAggregateIntoGroupbyRule.java
@@ -359,21 +359,25 @@
 
             Mutable<ILogicalOperator> opRef1InSubplan = aggInSubplanOp.getInputs().get(0);
             if (opRef1InSubplan.getValue().getInputs().size() > 0) {
-                List<Mutable<ILogicalOperator>> gbyInpList = gbyAgg.getInputs();
-                gbyInpList.clear();
-                gbyInpList.add(opRef1InSubplan);
-                while (true) {
-                    Mutable<ILogicalOperator> opRef2InSubplan = opRef1InSubplan.getValue().getInputs().get(0);
-                    AbstractLogicalOperator op2InSubplan = (AbstractLogicalOperator) opRef2InSubplan.getValue();
-                    if (op2InSubplan.getOperatorTag() == LogicalOperatorTag.UNNEST) {
-                        List<Mutable<ILogicalOperator>> opInpList = opRef1InSubplan.getValue().getInputs();
-                        opInpList.clear();
-                        opInpList.add(gbyAggChildRef);
-                        break;
-                    }
-                    opRef1InSubplan = opRef2InSubplan;
-                    if (opRef1InSubplan.getValue().getInputs().size() == 0) {
-                        throw new IllegalStateException("PushAggregateIntoGroupbyRule: could not find UNNEST.");
+                Mutable<ILogicalOperator> opRef2InSubplan = opRef1InSubplan.getValue().getInputs().get(0);
+                AbstractLogicalOperator op2InSubplan = (AbstractLogicalOperator) opRef2InSubplan.getValue();
+                if (op2InSubplan.getOperatorTag() != LogicalOperatorTag.NESTEDTUPLESOURCE) {
+                    List<Mutable<ILogicalOperator>> gbyInpList = gbyAgg.getInputs();
+                    gbyInpList.clear();
+                    gbyInpList.add(opRef1InSubplan);
+                    while (true) {
+                        opRef2InSubplan = opRef1InSubplan.getValue().getInputs().get(0);
+                        op2InSubplan = (AbstractLogicalOperator) opRef2InSubplan.getValue();
+                        if (op2InSubplan.getOperatorTag() == LogicalOperatorTag.UNNEST) {
+                            List<Mutable<ILogicalOperator>> opInpList = opRef1InSubplan.getValue().getInputs();
+                            opInpList.clear();
+                            opInpList.add(gbyAggChildRef);
+                            break;
+                        }
+                        opRef1InSubplan = opRef2InSubplan;
+                        if (opRef1InSubplan.getValue().getInputs().size() == 0) {
+                            throw new IllegalStateException("PushAggregateIntoGroupbyRule: could not find UNNEST.");
+                        }
                     }
                 }
             }
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/RemoveRedundantListifyRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/RemoveRedundantListifyRule.java
index f42c983..cdad37c 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/RemoveRedundantListifyRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/RemoveRedundantListifyRule.java
@@ -24,6 +24,7 @@
 import edu.uci.ics.asterix.aql.util.FunctionUtils;
 import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.common.utils.ListSet;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalOperator;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalPlan;
@@ -84,6 +85,10 @@
         AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
         VariableUtilities.getUsedVariables(op, varSet);
         if (op.hasNestedPlans()) {
+            // Variables used by the parent operators should be live at op.
+            Set<LogicalVariable> localLiveVars = new ListSet<LogicalVariable>();
+            VariableUtilities.getLiveVariables(op, localLiveVars);
+            varSet.retainAll(localLiveVars);
             AbstractOperatorWithNestedPlans aonp = (AbstractOperatorWithNestedPlans) op;
             for (ILogicalPlan p : aonp.getNestedPlans()) {
                 for (Mutable<ILogicalOperator> r : p.getRoots()) {
diff --git a/asterix-app/src/test/resources/optimizerts/queries/query-issue550.aql b/asterix-app/src/test/resources/optimizerts/queries/query-issue550.aql
new file mode 100644
index 0000000..7a01a3b
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/query-issue550.aql
@@ -0,0 +1,22 @@
+/*
+ * Description     : This test case is to verify the fix for issue550
+ * https://code.google.com/p/asterixdb/issues/detail?id=550
+ * Expected Result : Success
+ * Date            : 25th October 2014
+ */
+
+let $sample :=
+{{ 
+   {"r": 1, "uid": "1a2b", "t": datetime("2000-01-01T01:00:00"), "event": "e1"},
+   {"r": 2, "uid": "1a2b", "t": datetime("2000-01-01T01:01:00"), "event": "e2"},
+   {"r": 3, "uid": "3c4d", "t": datetime("2000-01-01T01:02:00"), "event": "e1"},
+   {"r": 4, "uid": "3c4d", "t": datetime("2000-01-01T01:03:00"), "event": "e3"},
+   {"r": 5, "uid": "1a2b", "t": datetime("2000-01-01T01:04:00"), "event": "e1"},
+   {"r": 6, "uid": "1a2b", "t": datetime("2000-01-01T01:05:00"), "event": "e4"}
+}}
+for $s in $sample
+group by $u := $s.uid with $s
+return {
+   "u": $u,
+   "recs": ( for $srec in $s return $srec )
+ };
diff --git a/asterix-app/src/test/resources/optimizerts/queries/query-issue785.aql b/asterix-app/src/test/resources/optimizerts/queries/query-issue785.aql
new file mode 100644
index 0000000..89a9531
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/query-issue785.aql
@@ -0,0 +1,105 @@
+/*
+ * Description  : This test case is to verify the fix for issue785
+ * https://code.google.com/p/asterixdb/issues/detail?id=785
+ * Expected Res : SUCCESS
+ * Date         : 2nd Oct. 2014
+ */
+
+drop dataverse tpch if exists;
+create dataverse tpch;
+
+use dataverse tpch;
+
+create type OrderType as closed {
+  o_orderkey: int32,
+  o_custkey: int32,
+  o_orderstatus: string,
+  o_totalprice: double,
+  o_orderdate: string,
+  o_orderpriority: string,
+  o_clerk: string,
+  o_shippriority: int32,
+  o_comment: string
+}
+
+create type CustomerType as closed {
+  c_custkey: int32,
+  c_name: string,
+  c_address: string,
+  c_nationkey: int32,
+  c_phone: string,
+  c_acctbal: double,
+  c_mktsegment: string,
+  c_comment: string
+}
+
+create type SupplierType as closed {
+  s_suppkey: int32,
+  s_name: string,
+  s_address: string,
+  s_nationkey: int32,
+  s_phone: string,
+  s_acctbal: double,
+  s_comment: string
+}
+
+create type NationType as closed {
+  n_nationkey: int32,
+  n_name: string,
+  n_regionkey: int32,
+  n_comment: string
+}
+
+create type RegionType as closed {
+  r_regionkey: int32,
+  r_name: string,
+  r_comment: string
+}
+
+create dataset Orders(OrderType)
+  primary key o_orderkey;
+create dataset Supplier(SupplierType)
+  primary key s_suppkey;
+create dataset Region(RegionType)
+  primary key r_regionkey;
+create dataset Nation(NationType)
+  primary key n_nationkey;
+create dataset Customer(CustomerType)
+  primary key c_custkey;
+create dataset SelectedNation(NationType)
+  primary key n_nationkey;
+
+let $t := for $nation in dataset Nation
+for $sn in dataset SelectedNation
+where $nation.n_nationkey = $sn.n_nationkey  /*+ indexnl */
+return {
+    "n_nationkey": $nation.n_nationkey,
+    "n_name": $nation.n_name
+}
+
+let $X := (
+for $n in $t
+for $customer in dataset Customer
+for $order in dataset Orders
+where $order.o_custkey = $customer.c_custkey
+and  $customer.c_nationkey = $n.n_nationkey
+group by $orderdate := $order.o_orderdate, $nation_key := $n.n_nationkey with $order
+let $sum := sum(for $o in $order return $o.o_totalprice)
+return {
+    "nation_key": $nation_key,
+    "order_date": $orderdate,
+    "sum_price": $sum 
+})
+
+for $x in $X
+group by $nation_key := $x.nation_key with $x
+return {
+    "nation_key": $nation_key,
+    "sum_price": for $y in $x
+                  order by $y.sum_price desc
+                  limit 3
+                  return {
+                    "orderdate": $y.order_date,
+                    "sum_price": $y.sum_price
+                  }
+}
diff --git a/asterix-app/src/test/resources/optimizerts/results/query-issue550.plan b/asterix-app/src/test/resources/optimizerts/results/query-issue550.plan
new file mode 100644
index 0000000..be23b06
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/query-issue550.plan
@@ -0,0 +1,18 @@
+-- DISTRIBUTE_RESULT  |LOCAL|
+  -- ONE_TO_ONE_EXCHANGE  |LOCAL|
+    -- STREAM_PROJECT  |LOCAL|
+      -- ASSIGN  |LOCAL|
+        -- ONE_TO_ONE_EXCHANGE  |LOCAL|
+          -- PRE_CLUSTERED_GROUP_BY[$$22]  |LOCAL|
+                  {
+                    -- AGGREGATE  |LOCAL|
+                      -- NESTED_TUPLE_SOURCE  |LOCAL|
+                  }
+            -- ONE_TO_ONE_EXCHANGE  |LOCAL|
+              -- STABLE_SORT [$$22(ASC)]  |LOCAL|
+                -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
+                  -- ASSIGN  |UNPARTITIONED|
+                    -- STREAM_PROJECT  |UNPARTITIONED|
+                      -- UNNEST  |UNPARTITIONED|
+                        -- ASSIGN  |UNPARTITIONED|
+                          -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
diff --git a/asterix-app/src/test/resources/optimizerts/results/query-issue785.plan b/asterix-app/src/test/resources/optimizerts/results/query-issue785.plan
new file mode 100644
index 0000000..7b95d4d
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/query-issue785.plan
@@ -0,0 +1,70 @@
+-- DISTRIBUTE_RESULT  |PARTITIONED|
+  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+    -- STREAM_PROJECT  |PARTITIONED|
+      -- ASSIGN  |PARTITIONED|
+        -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+          -- PRE_CLUSTERED_GROUP_BY[$$52]  |PARTITIONED|
+                  {
+                    -- AGGREGATE  |LOCAL|
+                      -- STREAM_LIMIT  |LOCAL|
+                        -- IN_MEMORY_STABLE_SORT [$$65(DESC)]  |LOCAL|
+                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                  }
+            -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+              -- STABLE_SORT [$$52(ASC)]  |PARTITIONED|
+                -- HASH_PARTITION_EXCHANGE [$$52]  |PARTITIONED|
+                  -- STREAM_PROJECT  |PARTITIONED|
+                    -- ASSIGN  |PARTITIONED|
+                      -- STREAM_PROJECT  |PARTITIONED|
+                        -- ASSIGN  |PARTITIONED|
+                          -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                            -- PRE_CLUSTERED_GROUP_BY[$$69, $$70]  |PARTITIONED|
+                                    {
+                                      -- AGGREGATE  |LOCAL|
+                                        -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                    }
+                              -- HASH_PARTITION_MERGE_EXCHANGE MERGE:[$$69(ASC), $$70(ASC)] HASH:[$$69, $$70]  |PARTITIONED|
+                                -- SORT_GROUP_BY[$$50, $$54]  |PARTITIONED|
+                                        {
+                                          -- AGGREGATE  |LOCAL|
+                                            -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                        }
+                                  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                    -- STREAM_PROJECT  |PARTITIONED|
+                                      -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                        -- HYBRID_HASH_JOIN [$$56][$$60]  |PARTITIONED|
+                                          -- HASH_PARTITION_EXCHANGE [$$56]  |PARTITIONED|
+                                            -- STREAM_PROJECT  |PARTITIONED|
+                                              -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                -- HYBRID_HASH_JOIN [$$54][$$63]  |PARTITIONED|
+                                                  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                    -- STREAM_PROJECT  |PARTITIONED|
+                                                      -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                        -- HYBRID_HASH_JOIN [$$54][$$55]  |PARTITIONED|
+                                                          -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                            -- STREAM_PROJECT  |PARTITIONED|
+                                                              -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                                -- DATASOURCE_SCAN  |PARTITIONED|
+                                                                  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                                    -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
+                                                          -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                            -- STREAM_PROJECT  |PARTITIONED|
+                                                              -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                                -- DATASOURCE_SCAN  |PARTITIONED|
+                                                                  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                                    -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
+                                                  -- HASH_PARTITION_EXCHANGE [$$63]  |PARTITIONED|
+                                                    -- STREAM_PROJECT  |PARTITIONED|
+                                                      -- ASSIGN  |PARTITIONED|
+                                                        -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                          -- DATASOURCE_SCAN  |PARTITIONED|
+                                                            -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                              -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
+                                          -- HASH_PARTITION_EXCHANGE [$$60]  |PARTITIONED|
+                                            -- STREAM_PROJECT  |PARTITIONED|
+                                              -- ASSIGN  |PARTITIONED|
+                                                -- STREAM_PROJECT  |PARTITIONED|
+                                                  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                    -- DATASOURCE_SCAN  |PARTITIONED|
+                                                      -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                                        -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterix-app/src/test/resources/runtimets/queries/flwor/query-issue550/query-issue550.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/flwor/query-issue550/query-issue550.1.ddl.aql
new file mode 100644
index 0000000..8a250f5
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/flwor/query-issue550/query-issue550.1.ddl.aql
@@ -0,0 +1,7 @@
+/*
+ * Description     : This test case is to verify the fix for issue550
+ * https://code.google.com/p/asterixdb/issues/detail?id=550
+ * Expected Result : Success
+ * Date            : 25th October 2014
+ */
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/flwor/query-issue550/query-issue550.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/flwor/query-issue550/query-issue550.2.update.aql
new file mode 100644
index 0000000..8a250f5
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/flwor/query-issue550/query-issue550.2.update.aql
@@ -0,0 +1,7 @@
+/*
+ * Description     : This test case is to verify the fix for issue550
+ * https://code.google.com/p/asterixdb/issues/detail?id=550
+ * Expected Result : Success
+ * Date            : 25th October 2014
+ */
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/flwor/query-issue550/query-issue550.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/flwor/query-issue550/query-issue550.3.query.aql
new file mode 100644
index 0000000..ebe8512
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/flwor/query-issue550/query-issue550.3.query.aql
@@ -0,0 +1,23 @@
+/*
+ * Description     : This test case is to verify the fix for issue550
+ * https://code.google.com/p/asterixdb/issues/detail?id=550
+ * Expected Result : Success
+ * Date            : 25th October 2014
+ */
+
+let $sample :=
+{{ 
+   {"r": 1, "uid": "1a2b", "t": datetime("2000-01-01T01:00:00"), "event": "e1"},
+   {"r": 2, "uid": "1a2b", "t": datetime("2000-01-01T01:01:00"), "event": "e2"},
+   {"r": 3, "uid": "3c4d", "t": datetime("2000-01-01T01:02:00"), "event": "e1"},
+   {"r": 4, "uid": "3c4d", "t": datetime("2000-01-01T01:03:00"), "event": "e3"},
+   {"r": 5, "uid": "1a2b", "t": datetime("2000-01-01T01:04:00"), "event": "e1"},
+   {"r": 6, "uid": "1a2b", "t": datetime("2000-01-01T01:05:00"), "event": "e4"}
+}}
+for $s in $sample
+group by $u := $s.uid with $s
+return {
+   "u": $u,
+   "recs": ( for $srec in $s return $srec )
+ };
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue638/query-issue638.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue638/query-issue638.1.ddl.aql
new file mode 100644
index 0000000..86995b6
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue638/query-issue638.1.ddl.aql
@@ -0,0 +1,129 @@
+/*
+ * Description  : This test case is to verify the fix for issue638
+ * https://code.google.com/p/asterixdb/issues/detail?id=638
+ * Expected Res : SUCCESS
+ * Date         : 24th Oct. 2014
+ */
+
+drop dataverse tpch if exists;
+create dataverse tpch;
+
+use dataverse tpch;
+
+create type LineItemType as closed {
+  l_orderkey: int32,
+  l_partkey: int32,
+  l_suppkey: int32,
+  l_linenumber: int32,
+  l_quantity: int32,
+  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 type OrderType as closed {
+  o_orderkey: int32,
+  o_custkey: int32,
+  o_orderstatus: string,
+  o_totalprice: double,
+  o_orderdate: string,
+  o_orderpriority: string,
+  o_clerk: string,
+  o_shippriority: int32,
+  o_comment: string
+}
+
+create type CustomerType as closed {
+  c_custkey: int32,
+  c_name: string,
+  c_address: string,
+  c_nationkey: int32,
+  c_phone: string,
+  c_acctbal: double,
+  c_mktsegment: string,
+  c_comment: string
+}
+
+create type SupplierType as closed {
+  s_suppkey: int32,
+  s_name: string,
+  s_address: string,
+  s_nationkey: int32,
+  s_phone: string,
+  s_acctbal: double,
+  s_comment: string
+}
+
+create type NationType as closed {
+  n_nationkey: int32,
+  n_name: string,
+  n_regionkey: int32,
+  n_comment: string
+}
+
+create type RegionType as closed {
+	r_regionkey: int32,
+	r_name: string,
+	r_comment: string
+} 
+
+create type PartType as closed {
+  p_partkey: int32,
+  p_name: string,
+  p_mfgr: string,
+  p_brand: string,
+  p_type: string,
+  p_size: int32,
+  p_container: string,
+  p_retailprice: double,
+  p_comment: string
+}
+
+create type PartSuppType as closed {
+  ps_partkey: int32, 
+  ps_suppkey: int32,
+  ps_availqty: int32,
+  ps_supplycost: double,
+  ps_comment: string 
+}
+
+create external dataset LineItem(LineItemType)
+using localfs
+(("path"="nc1://data/tpch0.001/lineitem.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+create external dataset Orders(OrderType)
+using localfs
+(("path"="nc1://data/tpch0.001/orders.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+create external dataset Supplier(SupplierType)
+using localfs
+(("path"="nc1://data/tpch0.001/supplier.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+create external dataset Region(RegionType) 
+using localfs
+(("path"="nc1://data/tpch0.001/region.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+create external dataset Nation(NationType) 
+using localfs
+(("path"="nc1://data/tpch0.001/nation.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+create external dataset Part(PartType)
+using localfs
+(("path"="nc1://data/tpch0.001/part.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+create external dataset Partsupp(PartSuppType)
+using localfs
+(("path"="nc1://data/tpch0.001/partsupp.tbl"),("format"="delimited-text"),("delimiter"="|")); 
+
+create external dataset Customer(CustomerType) 
+using localfs
+(("path"="nc1://data/tpch0.001/customer.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue638/query-issue638.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue638/query-issue638.2.update.aql
new file mode 100644
index 0000000..9a6d5bc
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue638/query-issue638.2.update.aql
@@ -0,0 +1,6 @@
+/*
+ * Description  : This test case is to verify the fix for issue638
+ * https://code.google.com/p/asterixdb/issues/detail?id=638
+ * Expected Res : SUCCESS
+ * Date         : 24th Oct. 2014
+ */
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue638/query-issue638.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue638/query-issue638.3.query.aql
new file mode 100644
index 0000000..49d1385
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue638/query-issue638.3.query.aql
@@ -0,0 +1,74 @@
+/*
+ * Description  : This test case is to verify the fix for issue638
+ * https://code.google.com/p/asterixdb/issues/detail?id=638
+ * Expected Res : SUCCESS
+ * Date         : 24th Oct. 2014
+ */
+
+use dataverse tpch;
+
+for $profit in (
+  for $o in dataset('Orders')
+  for $l3 in (
+    for $p in dataset('Part')
+    for $l2 in (
+      for $ps in dataset('Partsupp')
+      for $l1 in (
+        for $s1 in (
+          for $s in dataset('Supplier')
+          for $n in dataset('Nation')
+          where $n.n_nationkey = $s.s_nationkey
+          return {
+            "s_suppkey": $s.s_suppkey,
+            "n_name": $n.n_name
+          }
+        )
+        for $l in dataset('LineItem')
+        where $s1.s_suppkey = $l.l_suppkey       
+        return  {
+          "l_suppkey": $l.l_suppkey,
+          "l_extendedprice": $l.l_extendedprice,
+          "l_discount": $l.l_discount,
+          "l_quantity": $l.l_quantity,
+          "l_partkey": $l.l_partkey,
+          "l_orderkey": $l.l_orderkey,
+          "n_name": $s1.n_name
+        }
+      )
+      where $ps.ps_suppkey = $l1.l_suppkey and $ps.ps_partkey = $l1.l_partkey
+      return {
+        "l_extendedprice": $l1.l_extendedprice,
+        "l_discount": $l1.l_discount,
+        "l_quantity": $l1.l_quantity,
+        "l_partkey": $l1.l_partkey,
+        "l_orderkey": $l1.l_orderkey,
+        "n_name": $l1.n_name,
+        "ps_supplycost": $ps.ps_supplycost
+      }
+    )
+    where contains($p.p_name, 'green') and $p.p_partkey = $l2.l_partkey
+    return {
+      "l_extendedprice": $l2.l_extendedprice,
+      "l_discount": $l2.l_discount,
+      "l_quantity": $l2.l_quantity,
+      "l_orderkey": $l2.l_orderkey,
+      "n_name": $l2.n_name,
+      "ps_supplycost": $l2.ps_supplycost
+    }
+  )
+  where $o.o_orderkey = $l3.l_orderkey
+  let $amount := $l3.l_extendedprice * (1 - $l3.l_discount) -  $l3.ps_supplycost * $l3.l_quantity 
+  let $o_year := get-year($o.o_orderdate)
+  return {
+    "nation": $l3.n_name,
+    "o_year": $o_year,
+    "amount": $amount
+  }
+)
+group by $nation := $profit.nation, $o_year := $profit.o_year with $profit
+order by $nation, $o_year desc
+return {
+  "nation": $nation, 
+  "o_year": $o_year, 
+  "sum_profit": sum( for $pr in $profit return $pr.amount )
+}
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785-2/query-issue785-2.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785-2/query-issue785-2.1.ddl.aql
new file mode 100644
index 0000000..42a0b79
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785-2/query-issue785-2.1.ddl.aql
@@ -0,0 +1,70 @@
+/*
+ * Description  : This test case is to verify the fix for issue785
+ * https://code.google.com/p/asterixdb/issues/detail?id=785
+ * Expected Res : SUCCESS
+ * Date         : 2nd Oct. 2014
+ */
+
+drop dataverse tpch if exists;
+create dataverse tpch;
+
+use dataverse tpch;
+
+create type OrderType as closed {
+  o_orderkey: int32,
+  o_custkey: int32,
+  o_orderstatus: string,
+  o_totalprice: double,
+  o_orderdate: string,
+  o_orderpriority: string,
+  o_clerk: string,
+  o_shippriority: int32,
+  o_comment: string
+}
+
+create type CustomerType as closed {
+  c_custkey: int32,
+  c_name: string,
+  c_address: string,
+  c_nationkey: int32,
+  c_phone: string,
+  c_acctbal: double,
+  c_mktsegment: string,
+  c_comment: string
+}
+
+create type SupplierType as closed {
+  s_suppkey: int32,
+  s_name: string,
+  s_address: string,
+  s_nationkey: int32,
+  s_phone: string,
+  s_acctbal: double,
+  s_comment: string
+}
+
+create type NationType as closed {
+  n_nationkey: int32,
+  n_name: string,
+  n_regionkey: int32,
+  n_comment: string
+}
+
+create type RegionType as closed {
+  r_regionkey: int32,
+  r_name: string,
+  r_comment: string
+}
+
+create dataset Orders(OrderType)
+  primary key o_orderkey;
+create dataset Supplier(SupplierType)
+  primary key s_suppkey;
+create dataset Region(RegionType)
+  primary key r_regionkey;
+create dataset Nation(NationType)
+  primary key n_nationkey;
+create dataset Customer(CustomerType)
+  primary key c_custkey;
+create dataset SelectedNation(NationType)
+  primary key n_nationkey;
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785-2/query-issue785-2.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785-2/query-issue785-2.2.update.aql
new file mode 100644
index 0000000..beb9d08
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785-2/query-issue785-2.2.update.aql
@@ -0,0 +1,32 @@
+/*
+ * Description  : This test case is to verify the fix for issue785
+ * https://code.google.com/p/asterixdb/issues/detail?id=785
+ * Expected Res : SUCCESS
+ * Date         : 2nd Oct. 2014
+ */
+
+use dataverse tpch;
+
+load dataset Orders
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/tpch0.001/orders.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load dataset Supplier
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/tpch0.001/supplier.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load dataset Region
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/tpch0.001/region.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load dataset Nation
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/tpch0.001/nation.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load dataset Customer
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/tpch0.001/customer.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load dataset SelectedNation
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/tpch0.001/selectednation.tbl"),("format"="delimited-text"),("delimiter"="|"));
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785-2/query-issue785-2.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785-2/query-issue785-2.3.query.aql
new file mode 100644
index 0000000..5b18b0e
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785-2/query-issue785-2.3.query.aql
@@ -0,0 +1,43 @@
+/*
+ * Description  : This test case is to verify the fix for issue785
+ * https://code.google.com/p/asterixdb/issues/detail?id=785
+ * Expected Res : SUCCESS
+ * Date         : 2nd Oct. 2014
+ */
+
+use dataverse tpch;
+
+let $t := for $nation in dataset Nation
+for $sn in dataset SelectedNation
+where $nation.n_nationkey = $sn.n_nationkey  /*+ indexnl */
+return {
+    "n_nationkey": $nation.n_nationkey,
+    "n_name": $nation.n_name
+}
+
+let $X := (
+for $n in $t
+for $customer in dataset Customer
+for $order in dataset Orders
+where $order.o_custkey = $customer.c_custkey
+and  $customer.c_nationkey = $n.n_nationkey
+group by $orderdate := $order.o_orderdate, $nation_key := $n.n_nationkey with $order
+let $sum := sum(for $o in $order return $o.o_totalprice)
+return {
+    "nation_key": $nation_key,
+    "order_date": $orderdate,
+    "sum_price": $sum 
+})
+
+for $x in $X
+group by $nation_key := $x.nation_key with $x
+return {
+    "nation_key": $nation_key,
+    "sum_price": for $y in $x
+                  order by $y.sum_price desc
+                  limit 3
+                  return {
+                    "orderdate": $y.order_date,
+                    "sum_price": $y.sum_price
+                  }
+}
diff --git a/asterix-app/src/test/resources/runtimets/results/flwor/query-issue550/query-issue550.1.adm b/asterix-app/src/test/resources/runtimets/results/flwor/query-issue550/query-issue550.1.adm
new file mode 100644
index 0000000..58a7dea
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/flwor/query-issue550/query-issue550.1.adm
@@ -0,0 +1,3 @@
+[ { "u": "1a2b", "recs": [ { "r": 1, "uid": "1a2b", "t": datetime("2000-01-01T01:00:00.000Z"), "event": "e1" }, { "r": 2, "uid": "1a2b", "t": datetime("2000-01-01T01:01:00.000Z"), "event": "e2" }, { "r": 5, "uid": "1a2b", "t": datetime("2000-01-01T01:04:00.000Z"), "event": "e1" }, { "r": 6, "uid": "1a2b", "t": datetime("2000-01-01T01:05:00.000Z"), "event": "e4" } ] }
+, { "u": "3c4d", "recs": [ { "r": 3, "uid": "3c4d", "t": datetime("2000-01-01T01:02:00.000Z"), "event": "e1" }, { "r": 4, "uid": "3c4d", "t": datetime("2000-01-01T01:03:00.000Z"), "event": "e3" } ] }
+ ]
diff --git a/asterix-app/src/test/resources/runtimets/results/tpch/query-issue638/query-issue638.1.adm b/asterix-app/src/test/resources/runtimets/results/tpch/query-issue638/query-issue638.1.adm
new file mode 100644
index 0000000..d13d4f5
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/tpch/query-issue638/query-issue638.1.adm
@@ -0,0 +1,60 @@
+[ { "nation": "ARGENTINA", "o_year": 1997, "sum_profit": 18247.873399999993d }
+, { "nation": "ARGENTINA", "o_year": 1996, "sum_profit": 7731.089399999995d }
+, { "nation": "ARGENTINA", "o_year": 1995, "sum_profit": 134490.5697d }
+, { "nation": "ARGENTINA", "o_year": 1994, "sum_profit": 36767.101500000004d }
+, { "nation": "ARGENTINA", "o_year": 1993, "sum_profit": 35857.08d }
+, { "nation": "ARGENTINA", "o_year": 1992, "sum_profit": 35740.0d }
+, { "nation": "ETHIOPIA", "o_year": 1998, "sum_profit": 2758.7801999999992d }
+, { "nation": "ETHIOPIA", "o_year": 1997, "sum_profit": 19419.294599999994d }
+, { "nation": "ETHIOPIA", "o_year": 1995, "sum_profit": 51231.87439999999d }
+, { "nation": "ETHIOPIA", "o_year": 1994, "sum_profit": 3578.9478999999974d }
+, { "nation": "ETHIOPIA", "o_year": 1992, "sum_profit": 1525.8234999999986d }
+, { "nation": "IRAN", "o_year": 1998, "sum_profit": 37817.229600000006d }
+, { "nation": "IRAN", "o_year": 1997, "sum_profit": 52643.77359999999d }
+, { "nation": "IRAN", "o_year": 1996, "sum_profit": 70143.77609999999d }
+, { "nation": "IRAN", "o_year": 1995, "sum_profit": 84094.58260000001d }
+, { "nation": "IRAN", "o_year": 1994, "sum_profit": 18140.925599999995d }
+, { "nation": "IRAN", "o_year": 1993, "sum_profit": 78655.1676d }
+, { "nation": "IRAN", "o_year": 1992, "sum_profit": 87142.2396d }
+, { "nation": "IRAQ", "o_year": 1998, "sum_profit": 22860.8082d }
+, { "nation": "IRAQ", "o_year": 1997, "sum_profit": 93676.24359999999d }
+, { "nation": "IRAQ", "o_year": 1996, "sum_profit": 45103.3242d }
+, { "nation": "IRAQ", "o_year": 1994, "sum_profit": 36010.728599999995d }
+, { "nation": "IRAQ", "o_year": 1993, "sum_profit": 33221.9399d }
+, { "nation": "IRAQ", "o_year": 1992, "sum_profit": 47755.05900000001d }
+, { "nation": "KENYA", "o_year": 1998, "sum_profit": 44194.831999999995d }
+, { "nation": "KENYA", "o_year": 1997, "sum_profit": 57578.3626d }
+, { "nation": "KENYA", "o_year": 1996, "sum_profit": 59195.9021d }
+, { "nation": "KENYA", "o_year": 1995, "sum_profit": 79262.6278d }
+, { "nation": "KENYA", "o_year": 1994, "sum_profit": 102360.66609999999d }
+, { "nation": "KENYA", "o_year": 1993, "sum_profit": 128422.01959999999d }
+, { "nation": "KENYA", "o_year": 1992, "sum_profit": 181517.20890000003d }
+, { "nation": "MOROCCO", "o_year": 1998, "sum_profit": 41797.823199999984d }
+, { "nation": "MOROCCO", "o_year": 1997, "sum_profit": 23685.801799999997d }
+, { "nation": "MOROCCO", "o_year": 1996, "sum_profit": 62115.19579999999d }
+, { "nation": "MOROCCO", "o_year": 1995, "sum_profit": 42442.64300000001d }
+, { "nation": "MOROCCO", "o_year": 1994, "sum_profit": 48655.87800000001d }
+, { "nation": "MOROCCO", "o_year": 1993, "sum_profit": 22926.744400000003d }
+, { "nation": "MOROCCO", "o_year": 1992, "sum_profit": 32239.8088d }
+, { "nation": "PERU", "o_year": 1998, "sum_profit": 86999.36459999997d }
+, { "nation": "PERU", "o_year": 1997, "sum_profit": 121110.41070000001d }
+, { "nation": "PERU", "o_year": 1996, "sum_profit": 177040.40759999998d }
+, { "nation": "PERU", "o_year": 1995, "sum_profit": 122247.94519999999d }
+, { "nation": "PERU", "o_year": 1994, "sum_profit": 88046.2533d }
+, { "nation": "PERU", "o_year": 1993, "sum_profit": 49379.813799999996d }
+, { "nation": "PERU", "o_year": 1992, "sum_profit": 80646.86050000001d }
+, { "nation": "UNITED KINGDOM", "o_year": 1998, "sum_profit": 50577.25560000001d }
+, { "nation": "UNITED KINGDOM", "o_year": 1997, "sum_profit": 114288.86049999998d }
+, { "nation": "UNITED KINGDOM", "o_year": 1996, "sum_profit": 147684.46480000002d }
+, { "nation": "UNITED KINGDOM", "o_year": 1995, "sum_profit": 225267.6576d }
+, { "nation": "UNITED KINGDOM", "o_year": 1994, "sum_profit": 140595.58639999997d }
+, { "nation": "UNITED KINGDOM", "o_year": 1993, "sum_profit": 322548.49210000003d }
+, { "nation": "UNITED KINGDOM", "o_year": 1992, "sum_profit": 67747.88279999999d }
+, { "nation": "UNITED STATES", "o_year": 1998, "sum_profit": 3957.0431999999996d }
+, { "nation": "UNITED STATES", "o_year": 1997, "sum_profit": 94729.5704d }
+, { "nation": "UNITED STATES", "o_year": 1996, "sum_profit": 79297.8567d }
+, { "nation": "UNITED STATES", "o_year": 1995, "sum_profit": 62201.23360000001d }
+, { "nation": "UNITED STATES", "o_year": 1994, "sum_profit": 43075.62989999999d }
+, { "nation": "UNITED STATES", "o_year": 1993, "sum_profit": 27168.486199999996d }
+, { "nation": "UNITED STATES", "o_year": 1992, "sum_profit": 34092.366d }
+ ]
diff --git a/asterix-app/src/test/resources/runtimets/results/tpch/query-issue785-2/query-issue785-2.1.adm b/asterix-app/src/test/resources/runtimets/results/tpch/query-issue785-2/query-issue785-2.1.adm
new file mode 100644
index 0000000..4837987
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/tpch/query-issue785-2/query-issue785-2.1.adm
@@ -0,0 +1,11 @@
+[ { "nation_key": 1, "sum_price": [ { "orderdate": "1993-05-26", "sum_price": 221036.31d }, { "orderdate": "1992-03-20", "sum_price": 216230.27000000002d }, { "orderdate": "1993-12-24", "sum_price": 211925.95d } ] }
+, { "nation_key": 2, "sum_price": [ { "orderdate": "1996-03-01", "sum_price": 218697.85d }, { "orderdate": "1996-08-13", "sum_price": 217709.03d }, { "orderdate": "1992-08-21", "sum_price": 207364.8d } ] }
+, { "nation_key": 19, "sum_price": [ { "orderdate": "1993-12-29", "sum_price": 328959.87d }, { "orderdate": "1997-08-04", "sum_price": 244636.7d }, { "orderdate": "1996-11-20", "sum_price": 222274.54d } ] }
+, { "nation_key": 21, "sum_price": [ { "orderdate": "1994-02-27", "sum_price": 198360.22d }, { "orderdate": "1992-07-07", "sum_price": 180692.9d }, { "orderdate": "1996-06-28", "sum_price": 139915.23d } ] }
+, { "nation_key": 3, "sum_price": [ { "orderdate": "1997-04-23", "sum_price": 351762.82999999996d }, { "orderdate": "1995-11-13", "sum_price": 242588.87d }, { "orderdate": "1993-07-15", "sum_price": 214494.39d } ] }
+, { "nation_key": 23, "sum_price": [ { "orderdate": "1993-06-08", "sum_price": 161307.05d }, { "orderdate": "1995-12-07", "sum_price": 153048.74d }, { "orderdate": "1994-08-22", "sum_price": 147071.86d } ] }
+, { "nation_key": 4, "sum_price": [ { "orderdate": "1993-09-20", "sum_price": 226806.66d }, { "orderdate": "1992-03-04", "sum_price": 219709.6d }, { "orderdate": "1996-01-06", "sum_price": 190490.78d } ] }
+, { "nation_key": 22, "sum_price": [ { "orderdate": "1998-02-27", "sum_price": 263411.29d }, { "orderdate": "1993-04-11", "sum_price": 221636.83d }, { "orderdate": "1993-05-07", "sum_price": 220715.14d } ] }
+, { "nation_key": 0, "sum_price": [ { "orderdate": "1997-01-13", "sum_price": 241837.88d }, { "orderdate": "1997-01-21", "sum_price": 240284.95d }, { "orderdate": "1997-08-24", "sum_price": 231831.35d } ] }
+, { "nation_key": 20, "sum_price": [ { "orderdate": "1993-01-31", "sum_price": 190960.69d }, { "orderdate": "1998-07-17", "sum_price": 187156.38d }, { "orderdate": "1993-03-25", "sum_price": 167017.39d } ] }
+ ]
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index 6ac41a8..2dfd991 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -49,6 +49,11 @@
         <output-dir compare="Text">at06</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="flwor">
+      <compilation-unit name="query-issue550">
+        <output-dir compare="Text">query-issue550</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-case FilePath="flwor">
       <compilation-unit name="let33">
@@ -4369,11 +4374,21 @@
       </compilation-unit>
     </test-case>
     <test-case FilePath="tpch">
+      <compilation-unit name="query-issue638">
+        <output-dir compare="Text">query-issue638</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="tpch">
       <compilation-unit name="query-issue785">
         <output-dir compare="Text">query-issue785</output-dir>
       </compilation-unit>
     </test-case>
     <test-case FilePath="tpch">
+      <compilation-unit name="query-issue785-2">
+        <output-dir compare="Text">query-issue785-2</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="tpch">
       <compilation-unit name="query-issue786">
         <output-dir compare="Text">query-issue786</output-dir>
       </compilation-unit>
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/runningaggregates/std/TidRunningAggregateDescriptor.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/runningaggregates/std/TidRunningAggregateDescriptor.java
index 185e28a..a199f2d 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/runningaggregates/std/TidRunningAggregateDescriptor.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/runningaggregates/std/TidRunningAggregateDescriptor.java
@@ -38,6 +38,7 @@
 
     private static final long serialVersionUID = 1L;
     public static final IFunctionDescriptorFactory FACTORY = new IFunctionDescriptorFactory() {
+        @Override
         public IFunctionDescriptor createFunctionDescriptor() {
             return new TidRunningAggregateDescriptor();
         }