Fix issue 786, issue 601, the prefix search issue reported by Abdullah,
and the workaround query for issue 785.

Change-Id: I8bc4f18cd34c3d581f99f1509a4531c093aeec31
Reviewed-on: http://fulliautomatix.ics.uci.edu:8443/145
Reviewed-by: abdullah alamoudi <bamousaa@gmail.com>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/algebra/operators/physical/BTreeSearchPOperator.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/algebra/operators/physical/BTreeSearchPOperator.java
index d767eb2..5a4b52a 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/algebra/operators/physical/BTreeSearchPOperator.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/algebra/operators/physical/BTreeSearchPOperator.java
@@ -3,9 +3,9 @@
  * Licensed 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 from
- * 
+ *
  *     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.
@@ -129,6 +129,7 @@
         builder.contributeGraphEdge(srcExchange, 0, unnestMap, 0);
     }
 
+    @Override
     public PhysicalRequirements getRequiredPropertiesForChildren(ILogicalOperator op,
             IPhysicalPropertiesVector reqdByParent) {
         if (requiresBroadcast) {
@@ -140,9 +141,11 @@
                 searchKeyVars.addAll(highKeyVarList);
                 // Also, add a local sorting property to enforce a sort before the primary-index operator.
                 List<ILocalStructuralProperty> propsLocal = new ArrayList<ILocalStructuralProperty>();
+                List<OrderColumn> orderColumns = new ArrayList<OrderColumn>();
                 for (LogicalVariable orderVar : searchKeyVars) {
-                    propsLocal.add(new LocalOrderProperty(new OrderColumn(orderVar, OrderKind.ASC)));
+                    orderColumns.add(new OrderColumn(orderVar, OrderKind.ASC));
                 }
+                propsLocal.add(new LocalOrderProperty(orderColumns));
                 pv[0] = new StructuralPropertiesVector(new UnorderedPartitionedProperty(searchKeyVars, null),
                         propsLocal);
                 return new PhysicalRequirements(pv, IPartitioningRequirementsCoordinator.NO_COORDINATION);
diff --git a/asterix-app/src/test/resources/optimizerts/queries/query-issue601.aql b/asterix-app/src/test/resources/optimizerts/queries/query-issue601.aql
new file mode 100644
index 0000000..f704e16
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/query-issue601.aql
@@ -0,0 +1,40 @@
+/*
+ * Description  : This test case is to verify the fix for issue601
+ * https://code.google.com/p/asterixdb/issues/detail?id=601
+ * Expected Res : SUCCESS
+ * Date         : 10th 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: 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 dataset LineItem(LineItemType)
+  primary key l_orderkey, l_linenumber;
+  
+for $l in dataset('LineItem')
+group by $l_linenumber := $l.l_linenumber with $l
+return {
+  "l_linenumber": $l_linenumber,
+  "count_order": count($l)
+}
diff --git a/asterix-app/src/test/resources/optimizerts/results/nested_loj3.plan b/asterix-app/src/test/resources/optimizerts/results/nested_loj3.plan
index 6d0e32e..fbb0422 100644
--- a/asterix-app/src/test/resources/optimizerts/results/nested_loj3.plan
+++ b/asterix-app/src/test/resources/optimizerts/results/nested_loj3.plan
@@ -16,11 +16,10 @@
                                                 -- STREAM_SELECT  |LOCAL|
                                                   -- NESTED_TUPLE_SOURCE  |LOCAL|
                                             }
-                                      -- IN_MEMORY_STABLE_SORT [$$37(ASC), $$38(ASC)]  |LOCAL|
-                                        -- STREAM_SELECT  |LOCAL|
-                                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                      -- STREAM_SELECT  |LOCAL|
+                                        -- NESTED_TUPLE_SOURCE  |LOCAL|
                                 }
-                          -- IN_MEMORY_STABLE_SORT [$$40(ASC), $$38(ASC)]  |LOCAL|
+                          -- IN_MEMORY_STABLE_SORT [$$40(ASC), $$37(ASC), $$38(ASC)]  |LOCAL|
                             -- STREAM_SELECT  |LOCAL|
                               -- NESTED_TUPLE_SOURCE  |LOCAL|
                     }
diff --git a/asterix-app/src/test/resources/optimizerts/results/query-issue601.plan b/asterix-app/src/test/resources/optimizerts/results/query-issue601.plan
new file mode 100644
index 0000000..7b628a7
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/query-issue601.plan
@@ -0,0 +1,22 @@
+-- DISTRIBUTE_RESULT  |PARTITIONED|
+  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+    -- STREAM_PROJECT  |PARTITIONED|
+      -- ASSIGN  |PARTITIONED|
+        -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+          -- PRE_CLUSTERED_GROUP_BY[$$13]  |PARTITIONED|
+                  {
+                    -- AGGREGATE  |LOCAL|
+                      -- NESTED_TUPLE_SOURCE  |LOCAL|
+                  }
+            -- HASH_PARTITION_MERGE_EXCHANGE MERGE:[$$13(ASC)] HASH:[$$13]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$10]  |PARTITIONED|
+                      {
+                        -- AGGREGATE  |LOCAL|
+                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                      }
+                -- 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|
diff --git a/asterix-app/src/test/resources/runtimets/queries/misc/prefix-search/prefix-search.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/misc/prefix-search/prefix-search.1.ddl.aql
new file mode 100644
index 0000000..997807b
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/misc/prefix-search/prefix-search.1.ddl.aql
@@ -0,0 +1,33 @@
+/*
+ * Description  : This test case is to verify the fix for a
+ *                prefix search issue reported by Abdullah.
+ * Expected Res : SUCCESS
+ * Date         : 10th 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: 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 dataset LineItem(LineItemType)
+  primary key l_orderkey, l_linenumber;
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/misc/prefix-search/prefix-search.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/misc/prefix-search/prefix-search.2.update.aql
new file mode 100644
index 0000000..bb22016
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/misc/prefix-search/prefix-search.2.update.aql
@@ -0,0 +1,12 @@
+/*
+ * Description  : This test case is to verify the fix for a
+ *                prefix search issue reported by Abdullah.
+ * Expected Res : SUCCESS
+ * Date         : 10th Oct 2014
+ */
+
+use dataverse tpch;
+
+load dataset LineItem
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/tpch0.001/lineitem.tbl"),("format"="delimited-text"),("delimiter"="|")) pre-sorted;
diff --git a/asterix-app/src/test/resources/runtimets/queries/misc/prefix-search/prefix-search.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/misc/prefix-search/prefix-search.3.query.aql
new file mode 100644
index 0000000..330ab2c
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/misc/prefix-search/prefix-search.3.query.aql
@@ -0,0 +1,17 @@
+/*
+ * Description  : This test case is to verify the fix for a
+ *                prefix search issue reported by Abdullah.
+ * Expected Res : SUCCESS
+ * Date         : 10th Oct 2014
+ */
+
+use dataverse tpch;
+
+for $l in dataset('LineItem')
+where $l.l_orderkey>=0
+      and $l.l_orderkey<100
+order by $l.l_linenumber
+return {
+  "l_linenumber": $l.l_linenumber,
+  "l_l_orderkey": $l.l_orderkey
+}
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue601/query-issue601.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue601/query-issue601.1.ddl.aql
new file mode 100644
index 0000000..f37c63b
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue601/query-issue601.1.ddl.aql
@@ -0,0 +1,33 @@
+/*
+ * Description  : This test case is to verify the fix for issue601
+ * https://code.google.com/p/asterixdb/issues/detail?id=601
+ * Expected Res : SUCCESS
+ * Date         : 10th 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: 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 dataset LineItem(LineItemType)
+  primary key l_orderkey, l_linenumber;
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue601/query-issue601.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue601/query-issue601.2.update.aql
new file mode 100644
index 0000000..df889ff
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue601/query-issue601.2.update.aql
@@ -0,0 +1,12 @@
+/*
+ * Description  : This test case is to verify the fix for issue601
+ * https://code.google.com/p/asterixdb/issues/detail?id=601
+ * Expected Res : SUCCESS
+ * Date         : 10th Oct 2014
+ */
+
+use dataverse tpch;
+
+load dataset LineItem
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/tpch0.001/lineitem.tbl"),("format"="delimited-text"),("delimiter"="|")) pre-sorted;
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue601/query-issue601.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue601/query-issue601.3.query.aql
new file mode 100644
index 0000000..6dbe385
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue601/query-issue601.3.query.aql
@@ -0,0 +1,15 @@
+/*
+ * Description  : This test case is to verify the fix for issue601
+ * https://code.google.com/p/asterixdb/issues/detail?id=601
+ * Expected Res : SUCCESS
+ * Date         : 10th Oct 2014
+ */
+
+use dataverse tpch;
+
+for $l in dataset('LineItem')
+group by $l_linenumber := $l.l_linenumber with $l
+return {
+  "l_linenumber": $l_linenumber,
+  "count_order": count($l)
+}
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785/query-issue785.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785/query-issue785.1.ddl.aql
new file mode 100644
index 0000000..42a0b79
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785/query-issue785.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/query-issue785.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785/query-issue785.2.update.aql
new file mode 100644
index 0000000..beb9d08
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785/query-issue785.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/query-issue785.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785/query-issue785.3.query.aql
new file mode 100644
index 0000000..1e8a55f
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue785/query-issue785.3.query.aql
@@ -0,0 +1,35 @@
+/*
+ * 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;
+
+for $x in (
+  for $n in dataset Nation
+  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
+  return {
+	"nation_key": $nation_key,
+    "order_date": $orderdate,
+    "sum_price": sum(for $o in $order return $o.o_totalprice)
+  }
+)
+group by $nation_key := $x.nation_key with $x
+return {
+     "nation_key": $nation_key,
+     "sum_price": for $i in $x
+                  group by $od := $i.order_date with $i
+                  let $sum := sum(for $s in $i return $s.sum_price)
+                  order by $sum desc
+                  limit 3
+                  return{
+                  	"orderdate": $od,
+                  	"sum_price": $sum
+                  }
+}
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue786/query-issue786.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue786/query-issue786.1.ddl.aql
new file mode 100644
index 0000000..0d807da
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue786/query-issue786.1.ddl.aql
@@ -0,0 +1,70 @@
+/*
+ * Description  : This test case is to verify the fix for issue786
+ * https://code.google.com/p/asterixdb/issues/detail?id=786
+ * Expected Res : SUCCESS
+ * Date         : 10th 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-issue786/query-issue786.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue786/query-issue786.2.update.aql
new file mode 100644
index 0000000..8af58f1
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue786/query-issue786.2.update.aql
@@ -0,0 +1,32 @@
+/*
+ * Description  : This test case is to verify the fix for issue786
+ * https://code.google.com/p/asterixdb/issues/detail?id=786
+ * Expected Res : SUCCESS
+ * Date         : 10th 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-issue786/query-issue786.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue786/query-issue786.3.query.aql
new file mode 100644
index 0000000..227bb89
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/query-issue786/query-issue786.3.query.aql
@@ -0,0 +1,28 @@
+/*
+ * Description  : This test case is to verify the fix for issue786
+ * https://code.google.com/p/asterixdb/issues/detail?id=786
+ * Expected Res : SUCCESS
+ * Date         : 10th Oct. 2014
+ */
+
+use dataverse tpch;
+
+for $nation in dataset Nation
+for $sn in dataset SelectedNation
+where $nation.n_nationkey = $sn.sn_nationkey  /*+ indexnl */
+return {
+  "nation_key": $nation.n_nationkey,
+  "name": $nation.n_name,
+  "aggregates": for $order in dataset Orders
+                for $customer in dataset Customer
+                where $order.o_custkey = $customer.c_custkey
+                and  $customer.c_nationkey = $nation.n_nationkey
+                group by $orderdate := $order.o_orderdate with $order
+                let $sum := sum(for $o in $order return $o.o_totalprice)
+                order by $sum desc
+                limit 3
+                return {
+                  "order_date": $orderdate,
+                  "sum_price": $sum
+                }
+}
diff --git a/asterix-app/src/test/resources/runtimets/results/misc/prefix-search/prefix-search.1.adm b/asterix-app/src/test/resources/runtimets/results/misc/prefix-search/prefix-search.1.adm
new file mode 100644
index 0000000..14de02d
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/misc/prefix-search/prefix-search.1.adm
@@ -0,0 +1,106 @@
+[ { "l_linenumber": 1, "l_l_orderkey": 2 }
+, { "l_linenumber": 1, "l_l_orderkey": 4 }
+, { "l_linenumber": 1, "l_l_orderkey": 6 }
+, { "l_linenumber": 1, "l_l_orderkey": 33 }
+, { "l_linenumber": 1, "l_l_orderkey": 34 }
+, { "l_linenumber": 1, "l_l_orderkey": 39 }
+, { "l_linenumber": 1, "l_l_orderkey": 67 }
+, { "l_linenumber": 1, "l_l_orderkey": 68 }
+, { "l_linenumber": 1, "l_l_orderkey": 70 }
+, { "l_linenumber": 1, "l_l_orderkey": 98 }
+, { "l_linenumber": 1, "l_l_orderkey": 5 }
+, { "l_linenumber": 1, "l_l_orderkey": 7 }
+, { "l_linenumber": 1, "l_l_orderkey": 36 }
+, { "l_linenumber": 1, "l_l_orderkey": 37 }
+, { "l_linenumber": 1, "l_l_orderkey": 38 }
+, { "l_linenumber": 1, "l_l_orderkey": 64 }
+, { "l_linenumber": 1, "l_l_orderkey": 96 }
+, { "l_linenumber": 1, "l_l_orderkey": 3 }
+, { "l_linenumber": 1, "l_l_orderkey": 32 }
+, { "l_linenumber": 1, "l_l_orderkey": 66 }
+, { "l_linenumber": 1, "l_l_orderkey": 69 }
+, { "l_linenumber": 1, "l_l_orderkey": 99 }
+, { "l_linenumber": 1, "l_l_orderkey": 1 }
+, { "l_linenumber": 1, "l_l_orderkey": 35 }
+, { "l_linenumber": 1, "l_l_orderkey": 65 }
+, { "l_linenumber": 1, "l_l_orderkey": 71 }
+, { "l_linenumber": 1, "l_l_orderkey": 97 }
+, { "l_linenumber": 2, "l_l_orderkey": 33 }
+, { "l_linenumber": 2, "l_l_orderkey": 34 }
+, { "l_linenumber": 2, "l_l_orderkey": 35 }
+, { "l_linenumber": 2, "l_l_orderkey": 39 }
+, { "l_linenumber": 2, "l_l_orderkey": 67 }
+, { "l_linenumber": 2, "l_l_orderkey": 68 }
+, { "l_linenumber": 2, "l_l_orderkey": 70 }
+, { "l_linenumber": 2, "l_l_orderkey": 97 }
+, { "l_linenumber": 2, "l_l_orderkey": 98 }
+, { "l_linenumber": 2, "l_l_orderkey": 5 }
+, { "l_linenumber": 2, "l_l_orderkey": 7 }
+, { "l_linenumber": 2, "l_l_orderkey": 32 }
+, { "l_linenumber": 2, "l_l_orderkey": 37 }
+, { "l_linenumber": 2, "l_l_orderkey": 66 }
+, { "l_linenumber": 2, "l_l_orderkey": 96 }
+, { "l_linenumber": 2, "l_l_orderkey": 3 }
+, { "l_linenumber": 2, "l_l_orderkey": 69 }
+, { "l_linenumber": 2, "l_l_orderkey": 99 }
+, { "l_linenumber": 2, "l_l_orderkey": 1 }
+, { "l_linenumber": 2, "l_l_orderkey": 65 }
+, { "l_linenumber": 2, "l_l_orderkey": 71 }
+, { "l_linenumber": 3, "l_l_orderkey": 5 }
+, { "l_linenumber": 3, "l_l_orderkey": 7 }
+, { "l_linenumber": 3, "l_l_orderkey": 37 }
+, { "l_linenumber": 3, "l_l_orderkey": 33 }
+, { "l_linenumber": 3, "l_l_orderkey": 34 }
+, { "l_linenumber": 3, "l_l_orderkey": 35 }
+, { "l_linenumber": 3, "l_l_orderkey": 39 }
+, { "l_linenumber": 3, "l_l_orderkey": 67 }
+, { "l_linenumber": 3, "l_l_orderkey": 68 }
+, { "l_linenumber": 3, "l_l_orderkey": 70 }
+, { "l_linenumber": 3, "l_l_orderkey": 98 }
+, { "l_linenumber": 3, "l_l_orderkey": 1 }
+, { "l_linenumber": 3, "l_l_orderkey": 65 }
+, { "l_linenumber": 3, "l_l_orderkey": 71 }
+, { "l_linenumber": 3, "l_l_orderkey": 97 }
+, { "l_linenumber": 3, "l_l_orderkey": 3 }
+, { "l_linenumber": 3, "l_l_orderkey": 32 }
+, { "l_linenumber": 3, "l_l_orderkey": 69 }
+, { "l_linenumber": 3, "l_l_orderkey": 99 }
+, { "l_linenumber": 4, "l_l_orderkey": 3 }
+, { "l_linenumber": 4, "l_l_orderkey": 33 }
+, { "l_linenumber": 4, "l_l_orderkey": 35 }
+, { "l_linenumber": 4, "l_l_orderkey": 99 }
+, { "l_linenumber": 4, "l_l_orderkey": 32 }
+, { "l_linenumber": 4, "l_l_orderkey": 1 }
+, { "l_linenumber": 4, "l_l_orderkey": 39 }
+, { "l_linenumber": 4, "l_l_orderkey": 68 }
+, { "l_linenumber": 4, "l_l_orderkey": 69 }
+, { "l_linenumber": 4, "l_l_orderkey": 98 }
+, { "l_linenumber": 4, "l_l_orderkey": 7 }
+, { "l_linenumber": 4, "l_l_orderkey": 67 }
+, { "l_linenumber": 4, "l_l_orderkey": 70 }
+, { "l_linenumber": 4, "l_l_orderkey": 71 }
+, { "l_linenumber": 5, "l_l_orderkey": 3 }
+, { "l_linenumber": 5, "l_l_orderkey": 32 }
+, { "l_linenumber": 5, "l_l_orderkey": 69 }
+, { "l_linenumber": 5, "l_l_orderkey": 1 }
+, { "l_linenumber": 5, "l_l_orderkey": 71 }
+, { "l_linenumber": 5, "l_l_orderkey": 35 }
+, { "l_linenumber": 5, "l_l_orderkey": 39 }
+, { "l_linenumber": 5, "l_l_orderkey": 67 }
+, { "l_linenumber": 5, "l_l_orderkey": 68 }
+, { "l_linenumber": 5, "l_l_orderkey": 70 }
+, { "l_linenumber": 5, "l_l_orderkey": 7 }
+, { "l_linenumber": 6, "l_l_orderkey": 3 }
+, { "l_linenumber": 6, "l_l_orderkey": 7 }
+, { "l_linenumber": 6, "l_l_orderkey": 67 }
+, { "l_linenumber": 6, "l_l_orderkey": 70 }
+, { "l_linenumber": 6, "l_l_orderkey": 1 }
+, { "l_linenumber": 6, "l_l_orderkey": 32 }
+, { "l_linenumber": 6, "l_l_orderkey": 39 }
+, { "l_linenumber": 6, "l_l_orderkey": 68 }
+, { "l_linenumber": 6, "l_l_orderkey": 69 }
+, { "l_linenumber": 6, "l_l_orderkey": 35 }
+, { "l_linenumber": 6, "l_l_orderkey": 71 }
+, { "l_linenumber": 7, "l_l_orderkey": 7 }
+, { "l_linenumber": 7, "l_l_orderkey": 68 }
+ ]
diff --git a/asterix-app/src/test/resources/runtimets/results/tpch/query-issue601/query-issue601.1.adm b/asterix-app/src/test/resources/runtimets/results/tpch/query-issue601/query-issue601.1.adm
new file mode 100644
index 0000000..5dd52b1
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/tpch/query-issue601/query-issue601.1.adm
@@ -0,0 +1,8 @@
+[ { "l_linenumber": 1, "count_order": 1500i64 }
+, { "l_linenumber": 2, "count_order": 1291i64 }
+, { "l_linenumber": 3, "count_order": 1077i64 }
+, { "l_linenumber": 6, "count_order": 432i64 }
+, { "l_linenumber": 7, "count_order": 211i64 }
+, { "l_linenumber": 4, "count_order": 862i64 }
+, { "l_linenumber": 5, "count_order": 632i64 }
+ ]
diff --git a/asterix-app/src/test/resources/runtimets/results/tpch/query-issue785/query-issue785.1.adm b/asterix-app/src/test/resources/runtimets/results/tpch/query-issue785/query-issue785.1.adm
new file mode 100644
index 0000000..5808972
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/tpch/query-issue785/query-issue785.1.adm
@@ -0,0 +1,25 @@
+[ { "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": 8, "sum_price": [ { "orderdate": "1995-07-26", "sum_price": 244704.23d }, { "orderdate": "1994-12-03", "sum_price": 234763.73d }, { "orderdate": "1994-09-09", "sum_price": 228002.51d } ] }
+, { "nation_key": 9, "sum_price": [ { "orderdate": "1992-08-19", "sum_price": 240457.56d }, { "orderdate": "1995-03-02", "sum_price": 228136.49d }, { "orderdate": "1992-07-30", "sum_price": 226314.91d } ] }
+, { "nation_key": 10, "sum_price": [ { "orderdate": "1992-08-15", "sum_price": 232194.74d }, { "orderdate": "1997-01-03", "sum_price": 219920.62d }, { "orderdate": "1992-01-02", "sum_price": 210713.88d } ] }
+, { "nation_key": 13, "sum_price": [ { "orderdate": "1998-02-08", "sum_price": 223537.09d }, { "orderdate": "1993-11-24", "sum_price": 222392.53d }, { "orderdate": "1995-09-13", "sum_price": 197031.52d } ] }
+, { "nation_key": 18, "sum_price": [ { "orderdate": "1995-10-03", "sum_price": 245976.74d }, { "orderdate": "1992-06-03", "sum_price": 233161.66d }, { "orderdate": "1996-09-20", "sum_price": 219707.84d } ] }
+, { "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": 6, "sum_price": [ { "orderdate": "1992-05-28", "sum_price": 335178.33d }, { "orderdate": "1997-05-26", "sum_price": 216826.73d }, { "orderdate": "1996-04-30", "sum_price": 180054.29d } ] }
+, { "nation_key": 7, "sum_price": [ { "orderdate": "1995-03-19", "sum_price": 207925.83d }, { "orderdate": "1992-03-15", "sum_price": 206742.11d }, { "orderdate": "1992-05-10", "sum_price": 203904.8d } ] }
+, { "nation_key": 12, "sum_price": [ { "orderdate": "1995-05-01", "sum_price": 245388.06d }, { "orderdate": "1997-02-17", "sum_price": 225518.72d }, { "orderdate": "1996-08-20", "sum_price": 220636.82d } ] }
+, { "nation_key": 17, "sum_price": [ { "orderdate": "1997-07-05", "sum_price": 233874.09d }, { "orderdate": "1993-10-31", "sum_price": 224724.11d }, { "orderdate": "1996-04-18", "sum_price": 220727.97d } ] }
+, { "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": 5, "sum_price": [ { "orderdate": "1997-04-04", "sum_price": 258779.02d }, { "orderdate": "1998-07-20", "sum_price": 209155.48d }, { "orderdate": "1994-04-27", "sum_price": 202917.72d } ] }
+, { "nation_key": 11, "sum_price": [ { "orderdate": "1994-12-15", "sum_price": 249900.42d }, { "orderdate": "1996-12-24", "sum_price": 237947.61d }, { "orderdate": "1992-12-01", "sum_price": 218116.21d } ] }
+, { "nation_key": 14, "sum_price": [ { "orderdate": "1993-12-27", "sum_price": 230949.45d }, { "orderdate": "1992-04-26", "sum_price": 134333.33d }, { "orderdate": "1997-03-09", "sum_price": 132838.49d } ] }
+, { "nation_key": 15, "sum_price": [ { "orderdate": "1998-05-31", "sum_price": 366291.52d }, { "orderdate": "1994-04-24", "sum_price": 228054.01d }, { "orderdate": "1993-01-29", "sum_price": 223995.46d } ] }
+, { "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": 16, "sum_price": [ { "orderdate": "1994-09-20", "sum_price": 231012.22d }, { "orderdate": "1992-06-30", "sum_price": 221320.76d }, { "orderdate": "1993-05-14", "sum_price": 207291.83d } ] }
+, { "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/results/tpch/query-issue786/query-issue786.1.adm b/asterix-app/src/test/resources/runtimets/results/tpch/query-issue786/query-issue786.1.adm
new file mode 100644
index 0000000..e3b97f5
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/tpch/query-issue786/query-issue786.1.adm
@@ -0,0 +1 @@
+[  ]
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index 555126d..6ac41a8 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -2759,6 +2759,11 @@
         <output-dir compare="Text">string_eq_01</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="misc">
+      <compilation-unit name="prefix-search">
+        <output-dir compare="Text">prefix-search</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="nestrecords">
     <test-case FilePath="nestrecords">
@@ -4364,6 +4369,21 @@
       </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-issue786">
+        <output-dir compare="Text">query-issue786</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="tpch">
+      <compilation-unit name="query-issue601">
+        <output-dir compare="Text">query-issue601</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="tpch">
       <compilation-unit name="q10_returned_item">
         <output-dir compare="Text">q10_returned_item</output-dir>
       </compilation-unit>
diff --git a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AdaptedLoadableDataSource.java b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AdaptedLoadableDataSource.java
index cfe0e05..92f70b1 100644
--- a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AdaptedLoadableDataSource.java
+++ b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AdaptedLoadableDataSource.java
@@ -1,6 +1,7 @@
 package edu.uci.ics.asterix.metadata.declared;
 
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
@@ -65,9 +66,11 @@
     public void computeLocalStructuralProperties(List<ILocalStructuralProperty> localProps,
             List<LogicalVariable> variables) {
         if (alreadySorted) {
+            List<OrderColumn> orderColumns = new ArrayList<OrderColumn>();
             for (int i = 0; i < partitioningKeys.size(); ++i) {
-                localProps.add(new LocalOrderProperty(new OrderColumn(variables.get(i), OrderKind.ASC)));
+                orderColumns.add(new OrderColumn(variables.get(i), OrderKind.ASC));
             }
+            localProps.add(new LocalOrderProperty(orderColumns));
         }
     }
 
diff --git a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlDataSource.java b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlDataSource.java
index acbfc83..f7d27d7 100644
--- a/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlDataSource.java
+++ b/asterix-metadata/src/main/java/edu/uci/ics/asterix/metadata/declared/AqlDataSource.java
@@ -42,10 +42,10 @@
 
 public abstract class AqlDataSource implements IDataSource<AqlSourceId> {
 
-    private AqlSourceId id;
-    private String datasourceDataverse;
-    private String datasourceName;
-    private AqlDataSourceType datasourceType;
+    private final AqlSourceId id;
+    private final String datasourceDataverse;
+    private final String datasourceName;
+    private final AqlDataSourceType datasourceType;
     protected IAType[] schemaTypes;
     protected INodeDomain domain;
     private Map<String, Serializable> properties = new HashMap<String, Serializable>();
@@ -73,6 +73,7 @@
         return datasourceName;
     }
 
+    @Override
     public abstract IAType[] getSchemaTypes();
 
     public abstract INodeDomain getDomain();
@@ -172,9 +173,11 @@
                         pp = new UnorderedPartitionedProperty(pvars, domain);
                     }
                     propsLocal = new ArrayList<ILocalStructuralProperty>();
+                    List<OrderColumn> orderColumns = new ArrayList<OrderColumn>();
                     for (int i = 0; i < n - 1; i++) {
-                        propsLocal.add(new LocalOrderProperty(new OrderColumn(scanVariables.get(i), OrderKind.ASC)));
+                        orderColumns.add(new OrderColumn(scanVariables.get(i), OrderKind.ASC));
                     }
+                    propsLocal.add(new LocalOrderProperty(orderColumns));
                     propsVector = new StructuralPropertiesVector(pp, propsLocal);
                     break;