ASTERIXDB-1005, ASTERIXDB-1263: Clean up subplan flattening:
1. Inline NestedTupleSource and remove SubplanOperator for special cases that
   join operators inside the SubplanOperator can be re-targeted for
   correlations;
2. Blindly inline NestedTupleSource and remove SubplanOperator for general cases
   where the condition of the special case is not met.

Change-Id: I4dd130a25f3c81272cc23f844ea20e376e990612
Reviewed-on: https://asterix-gerrit.ics.uci.edu/579
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterix-app/src/test/resources/runtimets/queries/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.aql b/asterix-app/src/test/resources/runtimets/queries/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.aql
new file mode 100644
index 0000000..7fe6baa
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.aql
@@ -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.
+ */
+use dataverse TinySocial;
+
+for $t in dataset TweetMessages
+group by $d:= get-hour($t.send-time) with $t
+return { "hour": $d,
+         "count": count($t),
+         "finer":  for $k in $t
+                   group by $min:= get-minute($k.send-time) with $k
+                   order by $min
+                   return {  "minute": $min, "sum": count($k)}
+}
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/nest_aggregate2/nest_aggregate2.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/nest_aggregate2/nest_aggregate2.1.ddl.aql
new file mode 100644
index 0000000..14ccb1b
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/nest_aggregate2/nest_aggregate2.1.ddl.aql
@@ -0,0 +1,88 @@
+/*
+ * 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.
+ */
+/*
+ * Description  : This test case is to verify the fix for issue782
+ * https://code.google.com/p/asterixdb/issues/detail?id=782
+ * Expected Res : SUCCESS
+ * Date         : 2nd Jun 2014
+ */
+
+drop dataverse tpch if exists;
+create dataverse tpch;
+
+use dataverse tpch;
+
+create type OrderType as closed {
+  o_orderkey: int64,
+  o_custkey: int64,
+  o_orderstatus: string,
+  o_totalprice: double,
+  o_orderdate: string,
+  o_orderpriority: string,
+  o_clerk: string,
+  o_shippriority: int64,
+  o_comment: string
+}
+
+create type CustomerType as closed {
+  c_custkey: int64,
+  c_name: string,
+  c_address: string,
+  c_nationkey: int64,
+  c_phone: string,
+  c_acctbal: double,
+  c_mktsegment: string,
+  c_comment: string
+}
+
+create type SupplierType as closed {
+  s_suppkey: int64,
+  s_name: string,
+  s_address: string,
+  s_nationkey: int64,
+  s_phone: string,
+  s_acctbal: double,
+  s_comment: string
+}
+
+create type NationType as closed {
+  n_nationkey: int64,
+  n_name: string,
+  n_regionkey: int64,
+  n_comment: string
+}
+
+create type RegionType as closed {
+  r_regionkey: int64,
+  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/nest_aggregate2/nest_aggregate2.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/nest_aggregate2/nest_aggregate2.2.update.aql
new file mode 100644
index 0000000..5faabc5
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/nest_aggregate2/nest_aggregate2.2.update.aql
@@ -0,0 +1,50 @@
+/*
+ * 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.
+ */
+/*
+ * Description  : This test case is to verify the fix for issue782
+ * https://code.google.com/p/asterixdb/issues/detail?id=782
+ * Expected Res : SUCCESS
+ * Date         : 2nd Jun 2014
+ */
+
+use dataverse tpch;
+
+load dataset Orders
+using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="asterix_nc1://data/tpch0.001/orders.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load dataset Supplier
+using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="asterix_nc1://data/tpch0.001/supplier.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load dataset Region
+using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="asterix_nc1://data/tpch0.001/region.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load dataset Nation
+using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="asterix_nc1://data/tpch0.001/nation.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load dataset Customer
+using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="asterix_nc1://data/tpch0.001/customer.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load dataset SelectedNation
+using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="asterix_nc1://data/tpch0.001/selectednation.tbl"),("format"="delimited-text"),("delimiter"="|"));
diff --git a/asterix-app/src/test/resources/runtimets/queries/tpch/nest_aggregate2/nest_aggregate2.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/tpch/nest_aggregate2/nest_aggregate2.3.query.aql
new file mode 100644
index 0000000..96cebcf
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/tpch/nest_aggregate2/nest_aggregate2.3.query.aql
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+/*
+ * Description  : This test case is to verify the fix for issue782
+ * https://code.google.com/p/asterixdb/issues/detail?id=782
+ * Expected Res : SUCCESS
+ * Date         : 2nd Jun 2014
+ */
+
+use dataverse tpch;
+
+for $nation in dataset Nation
+for $sn in dataset SelectedNation
+where $nation.n_nationkey /*+ indexnl */ = $sn.n_nationkey
+order by $nation.n_nationkey
+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
+                limit 3
+                return $orderdate
+}
diff --git a/asterix-app/src/test/resources/runtimets/queries_sqlpp/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.1.ddl.sqlpp b/asterix-app/src/test/resources/runtimets/queries_sqlpp/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.1.ddl.sqlpp
new file mode 100644
index 0000000..f7b7de9
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries_sqlpp/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.1.ddl.sqlpp
@@ -0,0 +1,53 @@
+/*
+ * 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.
+ */
+/*
+ * Description     : This test case is to verify the fix for ASTERIXDB-1005
+ * https://issues.apache.org/jira/browse/ASTERIXDB-1005
+ * Expected Result : Success
+ * Date            : 10th January 2016
+ */
+
+drop database deliber if exists;
+create database deliber;
+use deliber;
+
+// Dish served by a restaurant
+create type DishesType as open
+{ name: string, price: double }
+
+// Bank account type
+create type BankAccountType as open{
+ bank_account_number: string,
+ bank_account_routing_number: string
+}
+
+// Restaurant's information
+create type RestaurantsType as open {
+restr_id: int64,
+name: string,
+address: string,
+bank_account: BankAccountType,
+last_bank_transaction_datetime: datetime,
+cuisine: {{ string }},
+dish: {{ DishesType }}
+}
+
+// Creating datasets for Users, Restaurants, and Orders
+create table Restaurants(RestaurantsType)
+primary key restr_id;
diff --git a/asterix-app/src/test/resources/runtimets/queries_sqlpp/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.2.update.sqlpp b/asterix-app/src/test/resources/runtimets/queries_sqlpp/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.2.update.sqlpp
new file mode 100644
index 0000000..89eff78
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries_sqlpp/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.2.update.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.
+ */
+/*
+ * Description     : This test case is to verify the fix for ASTERIXDB-1005
+ * https://issues.apache.org/jira/browse/ASTERIXDB-1005
+ * Expected Result : Success
+ * Date            : 10th January 2016
+ */
+
+use deliber;
+
+
+load  table Restaurants using
+"org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="asterix_nc1://data/restaurants/restaurants.adm"),("format"="adm"));
diff --git a/asterix-app/src/test/resources/runtimets/queries_sqlpp/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.3.query.sqlpp b/asterix-app/src/test/resources/runtimets/queries_sqlpp/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.3.query.sqlpp
new file mode 100644
index 0000000..a0f3345
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries_sqlpp/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.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.
+ */
+/*
+ * Description     : This test case is to verify the fix for ASTERIXDB-1005
+ * https://issues.apache.org/jira/browse/ASTERIXDB-1005
+ * Expected Result : Success
+ * Date            : 10th January 2016
+ */
+
+use deliber;
+
+select r.name as name, r.cuisine as cuisines
+from Restaurants as r
+where some c in r.cuisine satisfies c = 'Mexican' or c = 'Italian'
+order by r.name;
diff --git a/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.sqlpp b/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.sqlpp
new file mode 100644
index 0000000..f5139e1
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.sqlpp
@@ -0,0 +1,30 @@
+/*
+ * 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 TinySocial;
+
+select h as "hour",
+       count(t) as "count",
+       (
+         select min as "minute", count(k) as "sum"
+         from t as k
+         group by "get-minute"(k."send-time") as min
+         order by min
+       ) as "finer"
+from TweetMessages as t
+group by "get-hour"(t."send-time") as h;
diff --git a/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.1.ddl.sqlpp b/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.1.ddl.sqlpp
new file mode 100644
index 0000000..3b070de
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.1.ddl.sqlpp
@@ -0,0 +1,94 @@
+/*
+ * 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.
+ */
+/*
+ * Description  : This test case is to verify the fix for issue782
+ * https://code.google.com/p/asterixdb/issues/detail?id=782
+ * Expected Res : SUCCESS
+ * Date         : 2nd Jun 2014
+ */
+
+drop  database tpch if exists;
+create  database tpch;
+
+use tpch;
+
+
+create type tpch.OrderType as
+ closed {
+  o_orderkey : int64,
+  o_custkey : int64,
+  o_orderstatus : string,
+  o_totalprice : double,
+  o_orderdate : string,
+  o_orderpriority : string,
+  o_clerk : string,
+  o_shippriority : int64,
+  o_comment : string
+}
+
+create type tpch.CustomerType as
+ closed {
+  c_custkey : int64,
+  c_name : string,
+  c_address : string,
+  c_nationkey : int64,
+  c_phone : string,
+  c_acctbal : double,
+  c_mktsegment : string,
+  c_comment : string
+}
+
+create type tpch.SupplierType as
+ closed {
+  s_suppkey : int64,
+  s_name : string,
+  s_address : string,
+  s_nationkey : int64,
+  s_phone : string,
+  s_acctbal : double,
+  s_comment : string
+}
+
+create type tpch.NationType as
+ closed {
+  n_nationkey : int64,
+  n_name : string,
+  n_regionkey : int64,
+  n_comment : string
+}
+
+create type tpch.RegionType as
+ closed {
+  r_regionkey : int64,
+  r_name : string,
+  r_comment : string
+}
+
+create  table Orders(OrderType) primary key o_orderkey;
+
+create  table Supplier(SupplierType) primary key s_suppkey;
+
+create  table Region(RegionType) primary key r_regionkey;
+
+create  table Nation(NationType) primary key n_nationkey;
+
+create  table Customer(CustomerType) primary key c_custkey;
+
+create  table SelectedNation(NationType) primary key n_nationkey;
+
diff --git a/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.2.update.sqlpp b/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.2.update.sqlpp
new file mode 100644
index 0000000..24ff26a
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.2.update.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.
+ */
+/*
+ * Description  : This test case is to verify the fix for issue782
+ * https://code.google.com/p/asterixdb/issues/detail?id=782
+ * Expected Res : SUCCESS
+ * Date         : 2nd Jun 2014
+ */
+
+use tpch;
+
+
+load  table Orders using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter" (("path"="asterix_nc1://data/tpch0.001/orders.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load  table Supplier using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter" (("path"="asterix_nc1://data/tpch0.001/supplier.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load  table Region using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter" (("path"="asterix_nc1://data/tpch0.001/region.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load  table Nation using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter" (("path"="asterix_nc1://data/tpch0.001/nation.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load  table Customer using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter" (("path"="asterix_nc1://data/tpch0.001/customer.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
+load  table SelectedNation using "org.apache.asterix.external.dataset.adapter.NCFileSystemAdapter" (("path"="asterix_nc1://data/tpch0.001/selectednation.tbl"),("format"="delimited-text"),("delimiter"="|"));
+
diff --git a/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.3.query.sqlpp b/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.3.query.sqlpp
new file mode 100644
index 0000000..1aa687c
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.3.query.sqlpp
@@ -0,0 +1,46 @@
+/*
+ * 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.
+ */
+/*
+ * Description  : This test case is to verify the fix for issue782
+ * https://code.google.com/p/asterixdb/issues/detail?id=782
+ * Expected Res : SUCCESS
+ * Date         : 2nd Jun 2014
+ */
+
+use tpch;
+
+
+select element {'nation_key':nation.n_nationkey,'name':nation.n_name,'aggregates':(
+        select element orderdate
+        from  Orders as orders,
+              Customer as customer
+        where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = nation.n_nationkey))
+        group by orders.o_orderdate as orderdate
+        with  sum as tpch.sum((
+              select element o.o_totalprice
+              from  orders as o
+          ))
+        order by sum
+        limit 3
+    )}
+from  Nation as nation,
+      SelectedNation as sn
+where (nation.n_nationkey /*+ indexnl */ = sn.n_nationkey)
+order by nation.n_nationkey
+;
diff --git a/asterix-app/src/test/resources/runtimets/results/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.1.adm b/asterix-app/src/test/resources/runtimets/results/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.1.adm
new file mode 100644
index 0000000..fbf67d7
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/quantifiers/query-ASTERIXDB-1005/query-ASTERIXDB-1005.1.adm
@@ -0,0 +1,15 @@
+{ "name": "Afo Seafood", "cuisines": {{ "American", "Italian" }} }
+{ "name": "Aig craspe", "cuisines": {{ "Italian" }} }
+{ "name": "Ano Argio", "cuisines": {{ "American", "Italian" }} }
+{ "name": "Cit Spicy", "cuisines": {{ "Italian" }} }
+{ "name": "Dan Urban Plates", "cuisines": {{ "Asian", "Indian", "Mexican" }} }
+{ "name": "Gar Chiden", "cuisines": {{ "Italian" }} }
+{ "name": "Ith Javie", "cuisines": {{ "Italian" }} }
+{ "name": "Ken Fish Grill", "cuisines": {{ "Italian", "Mexican" }} }
+{ "name": "Ley Cucina", "cuisines": {{ "Italian" }} }
+{ "name": "Lly taik", "cuisines": {{ "Italian" }} }
+{ "name": "Nor Kingchops", "cuisines": {{ "Italian" }} }
+{ "name": "Nye Boilin", "cuisines": {{ "Italian" }} }
+{ "name": "Pat Urban Seoul", "cuisines": {{ "American", "Mexican" }} }
+{ "name": "Rno Fukad", "cuisines": {{ "Italian" }} }
+{ "name": "Sar Tang 190", "cuisines": {{ "French", "Greek", "Italian" }} }
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/temporal/overlap_bins_gby_3/overlap_bins_gby_3.1.adm b/asterix-app/src/test/resources/runtimets/results/temporal/overlap_bins_gby_3/overlap_bins_gby_3.1.adm
index 8e166b9..c1ffd9b 100644
--- a/asterix-app/src/test/resources/runtimets/results/temporal/overlap_bins_gby_3/overlap_bins_gby_3.1.adm
+++ b/asterix-app/src/test/resources/runtimets/results/temporal/overlap_bins_gby_3/overlap_bins_gby_3.1.adm
@@ -1,42 +1,42 @@
-{ "timebin": interval-time("10:27:00.000Z, 10:28:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 }, { "subgid": "Email", "item_count": 2 }, { "subgid": "Facebook", "item_count": 2 } ] } 
-{ "timebin": interval-time("10:28:00.000Z, 10:29:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 }, { "subgid": "Facebook", "item_count": 1 } ] }
-{ "timebin": interval-time("10:29:00.000Z, 10:30:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 }, { "subgid": "Email", "item_count": 1 }, { "subgid": "Facebook", "item_count": 2 } ] }
-{ "timebin": interval-time("10:30:00.000Z, 10:31:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:31:00.000Z, 10:32:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:32:00.000Z, 10:33:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:33:00.000Z, 10:34:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:34:00.000Z, 10:35:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:35:00.000Z, 10:36:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:36:00.000Z, 10:37:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:37:00.000Z, 10:38:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:38:00.000Z, 10:39:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:39:00.000Z, 10:40:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:40:00.000Z, 10:41:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:41:00.000Z, 10:42:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:42:00.000Z, 10:43:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:43:00.000Z, 10:44:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:44:00.000Z, 10:45:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:45:00.000Z, 10:46:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:46:00.000Z, 10:47:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:47:00.000Z, 10:48:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:48:00.000Z, 10:49:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:49:00.000Z, 10:50:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:50:00.000Z, 10:51:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:51:00.000Z, 10:52:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:52:00.000Z, 10:53:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:53:00.000Z, 10:54:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:54:00.000Z, 10:55:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:55:00.000Z, 10:56:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:56:00.000Z, 10:57:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:57:00.000Z, 10:58:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:58:00.000Z, 10:59:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("10:59:00.000Z, 11:00:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("11:00:00.000Z, 11:01:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("11:01:00.000Z, 11:02:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("11:02:00.000Z, 11:03:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("11:03:00.000Z, 11:04:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("11:04:00.000Z, 11:05:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("11:05:00.000Z, 11:06:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("11:06:00.000Z, 11:07:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
-{ "timebin": interval-time("11:07:00.000Z, 11:08:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 }, { "subgid": "Email", "item_count": 2 } ] }
-{ "timebin": interval-time("11:08:00.000Z, 11:09:00.000Z"), "subgroups": [ { "subgid": null, "item_count": 0 } ] }
+{ "timebin": interval-time("10:27:00.000Z, 10:28:00.000Z"), "subgroups": [ { "subgid": "Email", "item_count": 2 }, { "subgid": "Facebook", "item_count": 2 } ] }
+{ "timebin": interval-time("10:28:00.000Z, 10:29:00.000Z"), "subgroups": [ { "subgid": "Facebook", "item_count": 1 } ] }
+{ "timebin": interval-time("10:29:00.000Z, 10:30:00.000Z"), "subgroups": [ { "subgid": "Email", "item_count": 1 }, { "subgid": "Facebook", "item_count": 2 } ] }
+{ "timebin": interval-time("10:30:00.000Z, 10:31:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:31:00.000Z, 10:32:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:32:00.000Z, 10:33:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:33:00.000Z, 10:34:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:34:00.000Z, 10:35:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:35:00.000Z, 10:36:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:36:00.000Z, 10:37:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:37:00.000Z, 10:38:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:38:00.000Z, 10:39:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:39:00.000Z, 10:40:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:40:00.000Z, 10:41:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:41:00.000Z, 10:42:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:42:00.000Z, 10:43:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:43:00.000Z, 10:44:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:44:00.000Z, 10:45:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:45:00.000Z, 10:46:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:46:00.000Z, 10:47:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:47:00.000Z, 10:48:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:48:00.000Z, 10:49:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:49:00.000Z, 10:50:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:50:00.000Z, 10:51:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:51:00.000Z, 10:52:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:52:00.000Z, 10:53:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:53:00.000Z, 10:54:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:54:00.000Z, 10:55:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:55:00.000Z, 10:56:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:56:00.000Z, 10:57:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:57:00.000Z, 10:58:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:58:00.000Z, 10:59:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("10:59:00.000Z, 11:00:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("11:00:00.000Z, 11:01:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("11:01:00.000Z, 11:02:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("11:02:00.000Z, 11:03:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("11:03:00.000Z, 11:04:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("11:04:00.000Z, 11:05:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("11:05:00.000Z, 11:06:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("11:06:00.000Z, 11:07:00.000Z"), "subgroups": [  ] }
+{ "timebin": interval-time("11:07:00.000Z, 11:08:00.000Z"), "subgroups": [ { "subgid": "Email", "item_count": 2 } ] }
+{ "timebin": interval-time("11:08:00.000Z, 11:09:00.000Z"), "subgroups": [  ] }
diff --git a/asterix-app/src/test/resources/runtimets/results/tinysocial/tinysocial-suite/tinysocial-suite.23.adm b/asterix-app/src/test/resources/runtimets/results/tinysocial/tinysocial-suite/tinysocial-suite.23.adm
new file mode 100644
index 0000000..e073ee2
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/tinysocial/tinysocial-suite/tinysocial-suite.23.adm
@@ -0,0 +1 @@
+{ "hour": 10, "count": 12, "finer": [ { "minute": 10, "sum": 12 } ] }
diff --git a/asterix-app/src/test/resources/runtimets/results/tpch/nest_aggregate2/nest_aggregate2.1.adm b/asterix-app/src/test/resources/runtimets/results/tpch/nest_aggregate2/nest_aggregate2.1.adm
new file mode 100644
index 0000000..3834939
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/tpch/nest_aggregate2/nest_aggregate2.1.adm
@@ -0,0 +1,11 @@
+{ "nation_key": 0, "name": "ALGERIA", "aggregates": [ "1994-05-27", "1994-05-08", "1993-08-27" ] }
+{ "nation_key": 1, "name": "ARGENTINA", "aggregates": [ "1997-08-14", "1997-11-26", "1998-04-20" ] }
+{ "nation_key": 2, "name": "BRAZIL", "aggregates": [ "1993-03-05", "1994-08-31", "1997-05-04" ] }
+{ "nation_key": 3, "name": "CANADA", "aggregates": [ "1992-02-22", "1992-11-28", "1995-02-17" ] }
+{ "nation_key": 4, "name": "EGYPT", "aggregates": [ "1998-04-19", "1996-03-12", "1997-07-25" ] }
+{ "nation_key": 19, "name": "ROMANIA", "aggregates": [ "1994-07-05", "1994-11-17", "1997-02-07" ] }
+{ "nation_key": 20, "name": "SAUDI ARABIA", "aggregates": [ "1994-04-30", "1992-05-10", "1994-01-31" ] }
+{ "nation_key": 21, "name": "VIETNAM", "aggregates": [ "1994-02-17", "1995-08-05", "1994-06-01" ] }
+{ "nation_key": 22, "name": "RUSSIA", "aggregates": [ "1993-11-16", "1996-01-11", "1995-07-15" ] }
+{ "nation_key": 23, "name": "UNITED KINGDOM", "aggregates": [ "1997-12-18", "1995-05-26", "1997-05-13" ] }
+{ "nation_key": 24, "name": "UNITED STATES", "aggregates": [  ] }
diff --git a/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1263.23.ast b/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1263.23.ast
new file mode 100644
index 0000000..59998f2
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1263.23.ast
@@ -0,0 +1,61 @@
+DataverseUse TinySocial
+Query:
+SELECT [
+Variable [ Name=h ]
+hour
+FunctionCall TinySocial.count@1[
+  Variable [ Name=t ]
+]
+count
+(
+  SELECT [
+  Variable [ Name=min ]
+  minute
+  FunctionCall TinySocial.count@1[
+    Variable [ Name=k ]
+  ]
+  sum
+  ]
+  FROM [    Variable [ Name=t ]
+    AS
+    Variable [ Name=k ]
+  ]
+  Groupby
+    Variable [ Name=min ]
+    :=
+    FunctionCall TinySocial.get-minute@1[
+      FieldAccessor [
+        Variable [ Name=k ]
+        Field=send-time
+      ]
+    ]
+    With
+    Variable [ Name=k ]
+    Variable [ Name=t ]
+    Variable [ Name=h ]
+
+  Orderby
+    Variable [ Name=min ]
+    ASC
+
+)
+finer
+]
+FROM [  FunctionCall Metadata.dataset@1[
+    LiteralExpr [STRING] [TweetMessages]
+  ]
+  AS
+  Variable [ Name=t ]
+]
+Groupby
+  Variable [ Name=h ]
+  :=
+  FunctionCall TinySocial.get-hour@1[
+    FieldAccessor [
+      Variable [ Name=t ]
+      Field=send-time
+    ]
+  ]
+  With
+  Variable [ Name=t ]
+
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index a20dc15..5d55f2d 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -5220,6 +5220,11 @@
             </compilation-unit>
         </test-case>
         <test-case FilePath="tpch">
+            <compilation-unit name="nest_aggregate2">
+                <output-dir compare="Text">nest_aggregate2</output-dir>
+            </compilation-unit>
+        </test-case>
+        <test-case FilePath="tpch">
             <compilation-unit name="query-issue638">
                 <output-dir compare="Text">query-issue638</output-dir>
             </compilation-unit>
diff --git a/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml b/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index 58d9323..d56cdad 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -4077,6 +4077,11 @@
                 <output-dir compare="Text">everysat_01</output-dir>
             </compilation-unit>
         </test-case>
+        <test-case FilePath="quantifiers">
+            <compilation-unit name="query-ASTERIXDB-1005">
+                <output-dir compare="Text">query-ASTERIXDB-1005</output-dir>
+            </compilation-unit>
+        </test-case>
         <!--
         <test-case FilePath="quantifiers">
           <compilation-unit name="everysat_02">
@@ -5047,6 +5052,11 @@
             </compilation-unit>
         </test-case>
         <test-case FilePath="tpch">
+            <compilation-unit name="nest_aggregate2">
+                <output-dir compare="Text">nest_aggregate2</output-dir>
+            </compilation-unit>
+        </test-case>
+        <test-case FilePath="tpch">
             <compilation-unit name="query-issue638">
                 <output-dir compare="Text">query-issue638</output-dir>
             </compilation-unit>