asterixdb-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Yingyi Bu (Code Review)" <do-not-re...@asterixdb.incubator.apache.org>
Subject Change in asterixdb[master]: [ASTERIXDB-1753][SQL] Disable auto-plural for group-by.
Date Thu, 29 Jun 2017 16:49:34 GMT
Yingyi Bu has uploaded a new change for review.

  https://asterix-gerrit.ics.uci.edu/1860

Change subject: [ASTERIXDB-1753][SQL] Disable auto-plural for group-by.
......................................................................

[ASTERIXDB-1753][SQL] Disable auto-plural for group-by.

Change-Id: I5d6383fc7453fe7537fc291c3483708b59df3871
---
M asterixdb/asterix-app/src/test/resources/optimizerts/results/aggregate/constant-gby-agg.plan
M asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1806.plan
M asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/exists.plan
M asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/not_exists.plan
M asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1580.plan
M asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1581-correlated.plan
M asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1581.plan
M asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping.plan
M asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping_broadcast.plan
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/big-object/big_object_groupby/big_object_groupby.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at02/at02.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at03/at03.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at04/at04.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at05/at05.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-issue550/query-issue550.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/having-2/having-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-01-negative/sugar-01-negative.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04-2/sugar-04-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04/sugar-04.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_02/hdfs_02.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_03/hdfs_03.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_shortcircuit/hdfs_shortcircuit.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hints/issue_251_dataset_hint_6/issue_251_dataset_hint_6.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/groupby-orderby-count/groupby-orderby-count.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/open-closed/query-issue456/query-issue456.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/semistructured/count-nullable/count-nullable.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/spatial/cell-aggregation-with-filtering/cell-aggregation-with-filtering.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/spatial/cell-aggregation/cell-aggregation.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temp-dataset/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temporal/overlap_bins_gby_0/overlap_bins_gby_0.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temporal/overlap_bins_gby_1/overlap_bins_gby_1.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/query-ASTERIXDB-1063.23.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.15.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.16.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.17.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.15.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.16.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.17.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpcds/q24a/q24a.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpcds/q24b/q24b.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q04_order_priority/q04_order_priority.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q06_forecast_revenue_change/q06_forecast_revenue_change.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q08_national_market_share/q08_national_market_share.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q10_returned_item/q10_returned_item.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q11_important_stock/q11_important_stock.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q12_shipping/q12_shipping.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q15_top_supplier/q15_top_supplier.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q19_discounted_revenue/q19_discounted_revenue.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue601/query-issue601.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue638/query-issue638.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue785-2/query-issue785-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue785/query-issue785.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue786/query-issue786.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue785-2/query-issue785-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue785/query-issue785.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810-2/query-issue810-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810-3/query-issue810-3.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810/query-issue810.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue785-2/query-issue785-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue785/query-issue785.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810-2/query-issue810-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810-3/query-issue810-3.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810/query-issue810.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/nest_aggregate/nest_aggregate.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/nest_aggregate2/nest_aggregate2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q06_forecast_revenue_change/q06_forecast_revenue_change.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q08_national_market_share/q08_national_market_share.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q10_returned_item/q10_returned_item.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q11_important_stock/q11_important_stock.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q12_shipping/q12_shipping.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q15_top_supplier/q15_top_supplier.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q19_discounted_revenue/q19_discounted_revenue.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue562/query-issue562.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue601/query-issue601.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue785-2/query-issue785-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue785/query-issue785.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue786/query-issue786.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810-2/query-issue810-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810-3/query-issue810-3.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810/query-issue810.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue827/query-issue827.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate/nest_aggregate.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q08_national_market_share/q08_national_market_share.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q10_returned_item/q10_returned_item.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q11_important_stock/q11_important_stock.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q12_shipping/q12_shipping.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q15_top_supplier/q15_top_supplier.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue562/query-issue562.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue601/query-issue601.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue638/query-issue638.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue785-2/query-issue785-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue785/query-issue785.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue786/query-issue786.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810-2/query-issue810-2.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810-3/query-issue810-3.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810/query-issue810.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue827/query-issue827.3.query.sqlpp
M asterixdb/asterix-app/src/test/resources/runtimets/results/explain/explain_field_access/explain_field_access.1.adm
M asterixdb/asterix-app/src/test/resources/runtimets/results/explain/explain_field_access_closed/explain_field_access_closed.1.adm
M asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
M asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupByVisitor.java
152 files changed, 935 insertions(+), 811 deletions(-)


  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/60/1860/1

diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/aggregate/constant-gby-agg.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/aggregate/constant-gby-agg.plan
index d6399f7..26601ba 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/aggregate/constant-gby-agg.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/aggregate/constant-gby-agg.plan
@@ -3,13 +3,13 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-          -- SORT_GROUP_BY[$$20]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$18]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
-            -- HASH_PARTITION_EXCHANGE [$$20]  |PARTITIONED|
-              -- SORT_GROUP_BY[$$16]  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$18]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$14]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1806.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1806.plan
index 7ea82a1..ad30823 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1806.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1806.plan
@@ -3,13 +3,13 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- SORT_MERGE_EXCHANGE [$$l_returnflag(ASC), $$l_linestatus(ASC) ]  |PARTITIONED|
-          -- SORT_GROUP_BY[$$98, $$99]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$96, $$97]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
-            -- HASH_PARTITION_EXCHANGE [$$98, $$99]  |PARTITIONED|
-              -- SORT_GROUP_BY[$$66, $$67]  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$96, $$97]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$64, $$65]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/exists.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/exists.plan
index 3b92cdd..3f47dac 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/exists.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/exists.plan
@@ -3,13 +3,13 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- SORT_MERGE_EXCHANGE [$$cntrycode(ASC) ]  |PARTITIONED|
-          -- SORT_GROUP_BY[$$82]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$78]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
-            -- HASH_PARTITION_EXCHANGE [$$82]  |PARTITIONED|
-              -- SORT_GROUP_BY[$$59]  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$78]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$55]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
@@ -21,25 +21,25 @@
                         -- STREAM_SELECT  |PARTITIONED|
                           -- STREAM_PROJECT  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                              -- SORT_GROUP_BY[$$79]  |PARTITIONED|
+                              -- SORT_GROUP_BY[$$75]  |PARTITIONED|
                                       {
                                         -- AGGREGATE  |LOCAL|
                                           -- NESTED_TUPLE_SOURCE  |LOCAL|
                                       }
-                                -- HASH_PARTITION_EXCHANGE [$$79]  |PARTITIONED|
-                                  -- PRE_CLUSTERED_GROUP_BY[$$73]  |PARTITIONED|
+                                -- HASH_PARTITION_EXCHANGE [$$75]  |PARTITIONED|
+                                  -- PRE_CLUSTERED_GROUP_BY[$$69]  |PARTITIONED|
                                           {
                                             -- AGGREGATE  |LOCAL|
                                               -- STREAM_SELECT  |LOCAL|
                                                 -- NESTED_TUPLE_SOURCE  |LOCAL|
                                           }
                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                      -- STABLE_SORT [$$73(ASC)]  |PARTITIONED|
+                                      -- STABLE_SORT [$$69(ASC)]  |PARTITIONED|
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                           -- STREAM_PROJECT  |PARTITIONED|
                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                              -- HYBRID_HASH_JOIN [$$68][$$65]  |PARTITIONED|
-                                                -- HASH_PARTITION_EXCHANGE [$$68]  |PARTITIONED|
+                                              -- HYBRID_HASH_JOIN [$$64][$$61]  |PARTITIONED|
+                                                -- HASH_PARTITION_EXCHANGE [$$64]  |PARTITIONED|
                                                   -- ASSIGN  |PARTITIONED|
                                                     -- STREAM_PROJECT  |PARTITIONED|
                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -66,7 +66,7 @@
                                                                                 -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                     -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                -- HASH_PARTITION_EXCHANGE [$$65]  |PARTITIONED|
+                                                -- HASH_PARTITION_EXCHANGE [$$61]  |PARTITIONED|
                                                   -- ASSIGN  |PARTITIONED|
                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                       -- DATASOURCE_SCAN  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/not_exists.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/not_exists.plan
index 06257a1..311ab6d 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/not_exists.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/subquery/not_exists.plan
@@ -3,13 +3,13 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- SORT_MERGE_EXCHANGE [$$cntrycode(ASC) ]  |PARTITIONED|
-          -- SORT_GROUP_BY[$$83]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$79]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
-            -- HASH_PARTITION_EXCHANGE [$$83]  |PARTITIONED|
-              -- SORT_GROUP_BY[$$60]  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$79]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$56]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
@@ -21,25 +21,25 @@
                         -- STREAM_SELECT  |PARTITIONED|
                           -- STREAM_PROJECT  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                              -- SORT_GROUP_BY[$$80]  |PARTITIONED|
+                              -- SORT_GROUP_BY[$$76]  |PARTITIONED|
                                       {
                                         -- AGGREGATE  |LOCAL|
                                           -- NESTED_TUPLE_SOURCE  |LOCAL|
                                       }
-                                -- HASH_PARTITION_EXCHANGE [$$80]  |PARTITIONED|
-                                  -- PRE_CLUSTERED_GROUP_BY[$$74]  |PARTITIONED|
+                                -- HASH_PARTITION_EXCHANGE [$$76]  |PARTITIONED|
+                                  -- PRE_CLUSTERED_GROUP_BY[$$70]  |PARTITIONED|
                                           {
                                             -- AGGREGATE  |LOCAL|
                                               -- STREAM_SELECT  |LOCAL|
                                                 -- NESTED_TUPLE_SOURCE  |LOCAL|
                                           }
                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                      -- STABLE_SORT [$$74(ASC)]  |PARTITIONED|
+                                      -- STABLE_SORT [$$70(ASC)]  |PARTITIONED|
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                           -- STREAM_PROJECT  |PARTITIONED|
                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                              -- HYBRID_HASH_JOIN [$$69][$$66]  |PARTITIONED|
-                                                -- HASH_PARTITION_EXCHANGE [$$69]  |PARTITIONED|
+                                              -- HYBRID_HASH_JOIN [$$65][$$62]  |PARTITIONED|
+                                                -- HASH_PARTITION_EXCHANGE [$$65]  |PARTITIONED|
                                                   -- ASSIGN  |PARTITIONED|
                                                     -- STREAM_PROJECT  |PARTITIONED|
                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -66,7 +66,7 @@
                                                                                 -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                     -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                -- HASH_PARTITION_EXCHANGE [$$66]  |PARTITIONED|
+                                                -- HASH_PARTITION_EXCHANGE [$$62]  |PARTITIONED|
                                                   -- ASSIGN  |PARTITIONED|
                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                       -- DATASOURCE_SCAN  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1580.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1580.plan
index d3669c4..78d1566 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1580.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1580.plan
@@ -4,21 +4,21 @@
       -- STREAM_PROJECT  |PARTITIONED|
         -- ASSIGN  |PARTITIONED|
           -- STREAM_PROJECT  |PARTITIONED|
-            -- SORT_MERGE_EXCHANGE [$$69(ASC) ]  |PARTITIONED|
+            -- SORT_MERGE_EXCHANGE [$$59(ASC) ]  |PARTITIONED|
               -- STREAM_LIMIT  |PARTITIONED|
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                  -- STABLE_SORT [topK: 100] [$$69(ASC)]  |PARTITIONED|
+                  -- STABLE_SORT [topK: 100] [$$59(ASC)]  |PARTITIONED|
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                       -- STREAM_PROJECT  |PARTITIONED|
                         -- STREAM_SELECT  |PARTITIONED|
                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                            -- SORT_GROUP_BY[$$79]  |PARTITIONED|
+                            -- SORT_GROUP_BY[$$69]  |PARTITIONED|
                                     {
                                       -- AGGREGATE  |LOCAL|
                                         -- NESTED_TUPLE_SOURCE  |LOCAL|
                                     }
-                              -- HASH_PARTITION_EXCHANGE [$$79]  |PARTITIONED|
-                                -- SORT_GROUP_BY[$$57]  |PARTITIONED|
+                              -- HASH_PARTITION_EXCHANGE [$$69]  |PARTITIONED|
+                                -- SORT_GROUP_BY[$$47]  |PARTITIONED|
                                         {
                                           -- AGGREGATE  |LOCAL|
                                             -- NESTED_TUPLE_SOURCE  |LOCAL|
@@ -26,33 +26,33 @@
                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                     -- STREAM_PROJECT  |PARTITIONED|
                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                        -- HYBRID_HASH_JOIN [$$61][$$64]  |PARTITIONED|
-                                          -- HASH_PARTITION_EXCHANGE [$$61]  |PARTITIONED|
+                                        -- HYBRID_HASH_JOIN [$$51][$$54]  |PARTITIONED|
+                                          -- HASH_PARTITION_EXCHANGE [$$51]  |PARTITIONED|
                                             -- STREAM_PROJECT  |PARTITIONED|
                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                -- HYBRID_HASH_JOIN [$$70][$$63]  |PARTITIONED|
-                                                  -- HASH_PARTITION_EXCHANGE [$$70]  |PARTITIONED|
+                                                -- HYBRID_HASH_JOIN [$$60][$$53]  |PARTITIONED|
+                                                  -- HASH_PARTITION_EXCHANGE [$$60]  |PARTITIONED|
                                                     -- STREAM_PROJECT  |PARTITIONED|
                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                        -- HYBRID_HASH_JOIN [$$60][$$75]  |PARTITIONED|
-                                                          -- HASH_PARTITION_EXCHANGE [$$60]  |PARTITIONED|
+                                                        -- HYBRID_HASH_JOIN [$$50][$$65]  |PARTITIONED|
+                                                          -- HASH_PARTITION_EXCHANGE [$$50]  |PARTITIONED|
                                                             -- STREAM_PROJECT  |PARTITIONED|
                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                -- HYBRID_HASH_JOIN [$$59][$$73]  |PARTITIONED|
+                                                                -- HYBRID_HASH_JOIN [$$49][$$63]  |PARTITIONED|
                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                     -- ASSIGN  |PARTITIONED|
                                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                         -- DATASOURCE_SCAN  |PARTITIONED|
                                                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                             -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                                  -- HASH_PARTITION_EXCHANGE [$$73]  |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 [$$75]  |PARTITIONED|
+                                                          -- HASH_PARTITION_EXCHANGE [$$65]  |PARTITIONED|
                                                             -- STREAM_PROJECT  |PARTITIONED|
                                                               -- ASSIGN  |PARTITIONED|
                                                                 -- STREAM_PROJECT  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1581-correlated.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1581-correlated.plan
index 9c17663..5ac6b62 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1581-correlated.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1581-correlated.plan
@@ -8,7 +8,7 @@
               -- ASSIGN  |PARTITIONED|
                 -- STREAM_PROJECT  |PARTITIONED|
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                    -- PRE_CLUSTERED_GROUP_BY[$$101]  |PARTITIONED|
+                    -- PRE_CLUSTERED_GROUP_BY[$$95]  |PARTITIONED|
                             {
                               -- AGGREGATE  |LOCAL|
                                 -- AGGREGATE  |LOCAL|
@@ -18,13 +18,13 @@
                                         -- NESTED_TUPLE_SOURCE  |LOCAL|
                             }
                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                        -- STABLE_SORT [$$101(ASC)]  |PARTITIONED|
+                        -- STABLE_SORT [$$95(ASC)]  |PARTITIONED|
                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                             -- STREAM_PROJECT  |PARTITIONED|
                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                -- HYBRID_HASH_JOIN [$$101][$$102]  |PARTITIONED|
-                                  -- HASH_PARTITION_EXCHANGE [$$101]  |PARTITIONED|
-                                    -- PRE_CLUSTERED_GROUP_BY[$$89]  |PARTITIONED|
+                                -- HYBRID_HASH_JOIN [$$95][$$96]  |PARTITIONED|
+                                  -- HASH_PARTITION_EXCHANGE [$$95]  |PARTITIONED|
+                                    -- PRE_CLUSTERED_GROUP_BY[$$83]  |PARTITIONED|
                                             {
                                               -- AGGREGATE  |LOCAL|
                                                 -- AGGREGATE  |LOCAL|
@@ -34,13 +34,13 @@
                                                         -- NESTED_TUPLE_SOURCE  |LOCAL|
                                             }
                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                        -- STABLE_SORT [$$89(ASC)]  |PARTITIONED|
+                                        -- STABLE_SORT [$$83(ASC)]  |PARTITIONED|
                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                             -- STREAM_PROJECT  |PARTITIONED|
                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                -- HYBRID_HASH_JOIN [$$89][$$90]  |PARTITIONED|
-                                                  -- HASH_PARTITION_EXCHANGE [$$89]  |PARTITIONED|
-                                                    -- PRE_CLUSTERED_GROUP_BY[$$69]  |PARTITIONED|
+                                                -- HYBRID_HASH_JOIN [$$83][$$84]  |PARTITIONED|
+                                                  -- HASH_PARTITION_EXCHANGE [$$83]  |PARTITIONED|
+                                                    -- PRE_CLUSTERED_GROUP_BY[$$63]  |PARTITIONED|
                                                             {
                                                               -- AGGREGATE  |LOCAL|
                                                                 -- AGGREGATE  |LOCAL|
@@ -50,7 +50,7 @@
                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                         -- STREAM_PROJECT  |PARTITIONED|
                                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                            -- HYBRID_HASH_JOIN [$$69][$$79]  |PARTITIONED|
+                                                            -- HYBRID_HASH_JOIN [$$63][$$73]  |PARTITIONED|
                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                                   -- ASSIGN  |PARTITIONED|
@@ -71,7 +71,7 @@
                                                                   -- ASSIGN  |PARTITIONED|
                                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                       -- REPLICATE  |PARTITIONED|
-                                                                        -- HASH_PARTITION_EXCHANGE [$$126]  |PARTITIONED|
+                                                                        -- HASH_PARTITION_EXCHANGE [$$120]  |PARTITIONED|
                                                                           -- ASSIGN  |PARTITIONED|
                                                                             -- STREAM_PROJECT  |PARTITIONED|
                                                                               -- ASSIGN  |PARTITIONED|
@@ -83,23 +83,23 @@
                                                                                           -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                               -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                  -- HASH_PARTITION_EXCHANGE [$$90]  |PARTITIONED|
+                                                  -- HASH_PARTITION_EXCHANGE [$$84]  |PARTITIONED|
                                                     -- ASSIGN  |PARTITIONED|
                                                       -- STREAM_PROJECT  |PARTITIONED|
                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                          -- HYBRID_HASH_JOIN [$$83][$$82]  |PARTITIONED|
+                                                          -- HYBRID_HASH_JOIN [$$77][$$76]  |PARTITIONED|
                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                               -- STREAM_PROJECT  |PARTITIONED|
                                                                 -- ASSIGN  |PARTITIONED|
                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                     -- REPLICATE  |PARTITIONED|
-                                                                      -- HASH_PARTITION_EXCHANGE [$$120]  |PARTITIONED|
+                                                                      -- HASH_PARTITION_EXCHANGE [$$114]  |PARTITIONED|
                                                                         -- STREAM_PROJECT  |PARTITIONED|
                                                                           -- ASSIGN  |PARTITIONED|
                                                                             -- STREAM_PROJECT  |PARTITIONED|
                                                                               -- STREAM_SELECT  |PARTITIONED|
                                                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                                  -- PRE_CLUSTERED_GROUP_BY[$$125]  |PARTITIONED|
+                                                                                  -- PRE_CLUSTERED_GROUP_BY[$$119]  |PARTITIONED|
                                                                                           {
                                                                                             -- AGGREGATE  |LOCAL|
                                                                                               -- AGGREGATE  |LOCAL|
@@ -109,7 +109,7 @@
                                                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                       -- STREAM_PROJECT  |PARTITIONED|
                                                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                                          -- HYBRID_HASH_JOIN [$$125][$$126]  |PARTITIONED|
+                                                                                          -- HYBRID_HASH_JOIN [$$119][$$120]  |PARTITIONED|
                                                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                               -- REPLICATE  |PARTITIONED|
                                                                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -119,7 +119,7 @@
                                                                                                         -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
                                                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                               -- REPLICATE  |PARTITIONED|
-                                                                                                -- HASH_PARTITION_EXCHANGE [$$126]  |PARTITIONED|
+                                                                                                -- HASH_PARTITION_EXCHANGE [$$120]  |PARTITIONED|
                                                                                                   -- ASSIGN  |PARTITIONED|
                                                                                                     -- STREAM_PROJECT  |PARTITIONED|
                                                                                                       -- ASSIGN  |PARTITIONED|
@@ -131,7 +131,7 @@
                                                                                                                   -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                                       -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                            -- HASH_PARTITION_EXCHANGE [$$82]  |PARTITIONED|
+                                                            -- HASH_PARTITION_EXCHANGE [$$76]  |PARTITIONED|
                                                               -- STREAM_PROJECT  |PARTITIONED|
                                                                 -- ASSIGN  |PARTITIONED|
                                                                   -- STREAM_PROJECT  |PARTITIONED|
@@ -144,13 +144,13 @@
                                                                                 -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                     -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                  -- HASH_PARTITION_EXCHANGE [$$102]  |PARTITIONED|
+                                  -- HASH_PARTITION_EXCHANGE [$$96]  |PARTITIONED|
                                     -- ASSIGN  |PARTITIONED|
                                       -- STREAM_PROJECT  |PARTITIONED|
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                          -- HYBRID_HASH_JOIN [$$85][$$84]  |PARTITIONED|
-                                            -- HASH_PARTITION_EXCHANGE [$$85]  |PARTITIONED|
-                                              -- PRE_CLUSTERED_GROUP_BY[$$103]  |PARTITIONED|
+                                          -- HYBRID_HASH_JOIN [$$79][$$78]  |PARTITIONED|
+                                            -- HASH_PARTITION_EXCHANGE [$$79]  |PARTITIONED|
+                                              -- PRE_CLUSTERED_GROUP_BY[$$97]  |PARTITIONED|
                                                       {
                                                         -- AGGREGATE  |LOCAL|
                                                           -- AGGREGATE  |LOCAL|
@@ -159,18 +159,18 @@
                                                                 -- NESTED_TUPLE_SOURCE  |LOCAL|
                                                       }
                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                  -- STABLE_SORT [$$103(ASC)]  |PARTITIONED|
+                                                  -- STABLE_SORT [$$97(ASC)]  |PARTITIONED|
                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                       -- STREAM_PROJECT  |PARTITIONED|
                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                          -- HYBRID_HASH_JOIN [$$103][$$106]  |PARTITIONED|
-                                                            -- HASH_PARTITION_EXCHANGE [$$103]  |PARTITIONED|
+                                                          -- HYBRID_HASH_JOIN [$$97][$$100]  |PARTITIONED|
+                                                            -- HASH_PARTITION_EXCHANGE [$$97]  |PARTITIONED|
                                                               -- STREAM_PROJECT  |PARTITIONED|
                                                                 -- STREAM_SELECT  |PARTITIONED|
                                                                   -- STREAM_PROJECT  |PARTITIONED|
                                                                     -- ASSIGN  |PARTITIONED|
                                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                        -- PRE_CLUSTERED_GROUP_BY[$$107]  |PARTITIONED|
+                                                                        -- PRE_CLUSTERED_GROUP_BY[$$101]  |PARTITIONED|
                                                                                 {
                                                                                   -- AGGREGATE  |LOCAL|
                                                                                     -- AGGREGATE  |LOCAL|
@@ -180,7 +180,7 @@
                                                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                             -- STREAM_PROJECT  |PARTITIONED|
                                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                                -- HYBRID_HASH_JOIN [$$107][$$108]  |PARTITIONED|
+                                                                                -- HYBRID_HASH_JOIN [$$101][$$102]  |PARTITIONED|
                                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                     -- REPLICATE  |PARTITIONED|
                                                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -198,7 +198,7 @@
                                                                                       -- ASSIGN  |PARTITIONED|
                                                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                           -- REPLICATE  |PARTITIONED|
-                                                                                            -- HASH_PARTITION_EXCHANGE [$$126]  |PARTITIONED|
+                                                                                            -- HASH_PARTITION_EXCHANGE [$$120]  |PARTITIONED|
                                                                                               -- ASSIGN  |PARTITIONED|
                                                                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                                                                   -- ASSIGN  |PARTITIONED|
@@ -210,20 +210,20 @@
                                                                                                               -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                                   -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                            -- HASH_PARTITION_EXCHANGE [$$106]  |PARTITIONED|
+                                                            -- HASH_PARTITION_EXCHANGE [$$100]  |PARTITIONED|
                                                               -- ASSIGN  |PARTITIONED|
                                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                    -- HYBRID_HASH_JOIN [$$120][$$119]  |PARTITIONED|
+                                                                    -- HYBRID_HASH_JOIN [$$114][$$113]  |PARTITIONED|
                                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                         -- REPLICATE  |PARTITIONED|
-                                                                          -- HASH_PARTITION_EXCHANGE [$$120]  |PARTITIONED|
+                                                                          -- HASH_PARTITION_EXCHANGE [$$114]  |PARTITIONED|
                                                                             -- STREAM_PROJECT  |PARTITIONED|
                                                                               -- ASSIGN  |PARTITIONED|
                                                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                                                   -- STREAM_SELECT  |PARTITIONED|
                                                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                                      -- PRE_CLUSTERED_GROUP_BY[$$125]  |PARTITIONED|
+                                                                                      -- PRE_CLUSTERED_GROUP_BY[$$119]  |PARTITIONED|
                                                                                               {
                                                                                                 -- AGGREGATE  |LOCAL|
                                                                                                   -- AGGREGATE  |LOCAL|
@@ -233,7 +233,7 @@
                                                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                           -- STREAM_PROJECT  |PARTITIONED|
                                                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                                              -- HYBRID_HASH_JOIN [$$125][$$126]  |PARTITIONED|
+                                                                                              -- HYBRID_HASH_JOIN [$$119][$$120]  |PARTITIONED|
                                                                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                   -- REPLICATE  |PARTITIONED|
                                                                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -243,7 +243,7 @@
                                                                                                             -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
                                                                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                   -- REPLICATE  |PARTITIONED|
-                                                                                                    -- HASH_PARTITION_EXCHANGE [$$126]  |PARTITIONED|
+                                                                                                    -- HASH_PARTITION_EXCHANGE [$$120]  |PARTITIONED|
                                                                                                       -- ASSIGN  |PARTITIONED|
                                                                                                         -- STREAM_PROJECT  |PARTITIONED|
                                                                                                           -- ASSIGN  |PARTITIONED|
@@ -255,7 +255,7 @@
                                                                                                                       -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                                           -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                                      -- HASH_PARTITION_EXCHANGE [$$119]  |PARTITIONED|
+                                                                      -- HASH_PARTITION_EXCHANGE [$$113]  |PARTITIONED|
                                                                         -- STREAM_PROJECT  |PARTITIONED|
                                                                           -- ASSIGN  |PARTITIONED|
                                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -266,7 +266,7 @@
                                                                                       -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                           -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                            -- HASH_PARTITION_EXCHANGE [$$84]  |PARTITIONED|
+                                            -- HASH_PARTITION_EXCHANGE [$$78]  |PARTITIONED|
                                               -- STREAM_PROJECT  |PARTITIONED|
                                                 -- ASSIGN  |PARTITIONED|
                                                   -- STREAM_PROJECT  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1581.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1581.plan
index 70948fb..14b31eb 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1581.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpcds/query-ASTERIXDB-1581.plan
@@ -16,7 +16,7 @@
                 -- STREAM_PROJECT  |PARTITIONED|
                   -- ASSIGN  |PARTITIONED|
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      -- PRE_CLUSTERED_GROUP_BY[$$98]  |PARTITIONED|
+                      -- PRE_CLUSTERED_GROUP_BY[$$92]  |PARTITIONED|
                               {
                                 -- AGGREGATE  |LOCAL|
                                   -- AGGREGATE  |LOCAL|
@@ -26,13 +26,13 @@
                                           -- NESTED_TUPLE_SOURCE  |LOCAL|
                               }
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                          -- STABLE_SORT [$$98(ASC)]  |PARTITIONED|
+                          -- STABLE_SORT [$$92(ASC)]  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                               -- STREAM_PROJECT  |PARTITIONED|
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                  -- HYBRID_HASH_JOIN [$$98][$$99]  |PARTITIONED|
-                                    -- HASH_PARTITION_EXCHANGE [$$98]  |PARTITIONED|
-                                      -- PRE_CLUSTERED_GROUP_BY[$$36]  |PARTITIONED|
+                                  -- HYBRID_HASH_JOIN [$$92][$$93]  |PARTITIONED|
+                                    -- HASH_PARTITION_EXCHANGE [$$92]  |PARTITIONED|
+                                      -- PRE_CLUSTERED_GROUP_BY[$$32]  |PARTITIONED|
                                               {
                                                 -- AGGREGATE  |LOCAL|
                                                   -- AGGREGATE  |LOCAL|
@@ -42,17 +42,17 @@
                                                           -- NESTED_TUPLE_SOURCE  |LOCAL|
                                               }
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                          -- STABLE_SORT [$$36(ASC)]  |PARTITIONED|
+                                          -- STABLE_SORT [$$32(ASC)]  |PARTITIONED|
                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                               -- STREAM_PROJECT  |PARTITIONED|
                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                  -- HYBRID_HASH_JOIN [$$36][$$91]  |PARTITIONED|
+                                                  -- HYBRID_HASH_JOIN [$$32][$$85]  |PARTITIONED|
                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                       -- STREAM_PROJECT  |PARTITIONED|
                                                         -- ASSIGN  |PARTITIONED|
                                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                             -- REPLICATE  |PARTITIONED|
-                                                              -- HASH_PARTITION_EXCHANGE [$$100]  |PARTITIONED|
+                                                              -- HASH_PARTITION_EXCHANGE [$$94]  |PARTITIONED|
                                                                 -- STREAM_PROJECT  |UNPARTITIONED|
                                                                   -- ASSIGN  |UNPARTITIONED|
                                                                     -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
@@ -70,7 +70,7 @@
                                                                                             -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                 -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                    -- HASH_PARTITION_EXCHANGE [$$91]  |PARTITIONED|
+                                                    -- HASH_PARTITION_EXCHANGE [$$85]  |PARTITIONED|
                                                       -- ASSIGN  |PARTITIONED|
                                                         -- STREAM_PROJECT  |PARTITIONED|
                                                           -- STREAM_SELECT  |PARTITIONED|
@@ -101,7 +101,7 @@
                                                                                                             -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                                 -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                    -- HASH_PARTITION_EXCHANGE [$$99]  |PARTITIONED|
+                                    -- HASH_PARTITION_EXCHANGE [$$93]  |PARTITIONED|
                                       -- ASSIGN  |PARTITIONED|
                                         -- STREAM_PROJECT  |PARTITIONED|
                                           -- STREAM_SELECT  |PARTITIONED|
@@ -115,7 +115,7 @@
                                                           -- STREAM_SELECT  |PARTITIONED|
                                                             -- ASSIGN  |PARTITIONED|
                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                -- PRE_CLUSTERED_GROUP_BY[$$100]  |PARTITIONED|
+                                                                -- PRE_CLUSTERED_GROUP_BY[$$94]  |PARTITIONED|
                                                                         {
                                                                           -- AGGREGATE  |LOCAL|
                                                                             -- AGGREGATE  |LOCAL|
@@ -124,14 +124,14 @@
                                                                                   -- NESTED_TUPLE_SOURCE  |LOCAL|
                                                                         }
                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                    -- STABLE_SORT [$$100(ASC)]  |PARTITIONED|
+                                                                    -- STABLE_SORT [$$94(ASC)]  |PARTITIONED|
                                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                         -- STREAM_PROJECT  |PARTITIONED|
                                                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                            -- HYBRID_HASH_JOIN [$$100][$$101]  |PARTITIONED|
+                                                                            -- HYBRID_HASH_JOIN [$$94][$$95]  |PARTITIONED|
                                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                 -- REPLICATE  |PARTITIONED|
-                                                                                  -- HASH_PARTITION_EXCHANGE [$$100]  |PARTITIONED|
+                                                                                  -- HASH_PARTITION_EXCHANGE [$$94]  |PARTITIONED|
                                                                                     -- STREAM_PROJECT  |UNPARTITIONED|
                                                                                       -- ASSIGN  |UNPARTITIONED|
                                                                                         -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
@@ -149,7 +149,7 @@
                                                                                                                 -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                                     -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                                              -- HASH_PARTITION_EXCHANGE [$$101]  |PARTITIONED|
+                                                                              -- HASH_PARTITION_EXCHANGE [$$95]  |PARTITIONED|
                                                                                 -- ASSIGN  |PARTITIONED|
                                                                                   -- STREAM_PROJECT  |PARTITIONED|
                                                                                     -- STREAM_SELECT  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping.plan
index 57ca6ce..655b2f5 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping.plan
@@ -3,13 +3,13 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- SORT_MERGE_EXCHANGE [$$l_shipmode(ASC) ]  |PARTITIONED|
-          -- SORT_GROUP_BY[$$95]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$91]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
-            -- HASH_PARTITION_EXCHANGE [$$95]  |PARTITIONED|
-              -- SORT_GROUP_BY[$$77]  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$91]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$73]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
@@ -17,12 +17,12 @@
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- STREAM_PROJECT  |PARTITIONED|
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      -- HYBRID_HASH_JOIN [$$77][$$84]  |PARTITIONED|
+                      -- HYBRID_HASH_JOIN [$$73][$$80]  |PARTITIONED|
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                           -- STREAM_PROJECT  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                              -- HYBRID_HASH_JOIN [$$86][$$81]  |PARTITIONED|
-                                -- HASH_PARTITION_EXCHANGE [$$86]  |PARTITIONED|
+                              -- HYBRID_HASH_JOIN [$$82][$$77]  |PARTITIONED|
+                                -- HASH_PARTITION_EXCHANGE [$$82]  |PARTITIONED|
                                   -- STREAM_PROJECT  |PARTITIONED|
                                     -- STREAM_SELECT  |PARTITIONED|
                                       -- ASSIGN  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping_broadcast.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping_broadcast.plan
index 40c1c55..8fd10f5 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping_broadcast.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/tpch/q12_shipping_broadcast.plan
@@ -3,13 +3,13 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- SORT_MERGE_EXCHANGE [$$l_shipmode(ASC) ]  |PARTITIONED|
-          -- SORT_GROUP_BY[$$95]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$91]  |PARTITIONED|
                   {
                     -- AGGREGATE  |LOCAL|
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
-            -- HASH_PARTITION_EXCHANGE [$$95]  |PARTITIONED|
-              -- SORT_GROUP_BY[$$77]  |PARTITIONED|
+            -- HASH_PARTITION_EXCHANGE [$$91]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$73]  |PARTITIONED|
                       {
                         -- AGGREGATE  |LOCAL|
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
@@ -17,11 +17,11 @@
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- STREAM_PROJECT  |PARTITIONED|
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      -- HYBRID_HASH_JOIN [$$77][$$84]  |PARTITIONED|
+                      -- HYBRID_HASH_JOIN [$$73][$$80]  |PARTITIONED|
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                           -- STREAM_PROJECT  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                              -- HYBRID_HASH_JOIN [$$85][$$81]  |PARTITIONED|
+                              -- HYBRID_HASH_JOIN [$$81][$$77]  |PARTITIONED|
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                   -- STREAM_PROJECT  |PARTITIONED|
                                     -- STREAM_SELECT  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/big-object/big_object_groupby/big_object_groupby.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/big-object/big_object_groupby/big_object_groupby.3.query.sqlpp
index 641b8e6..b5388a9 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/big-object/big_object_groupby/big_object_groupby.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/big-object/big_object_groupby/big_object_groupby.3.query.sqlpp
@@ -26,8 +26,9 @@
 use test;
 
 
-select element {'partkey':partkey,'lines':i}
+select element {'partkey':partkey,'lines': (from g select value i) }
 from  Line as i
 group by i.l_partkey as partkey
+group as g
 order by partkey
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at02/at02.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at02/at02.3.query.sqlpp
index 3c6be99..82e458f 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at02/at02.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at02/at02.3.query.sqlpp
@@ -28,14 +28,15 @@
 select element {'group':g,'item':(
         select element {'num':p,'mid':a.`message-id`}
         from  (
-            select element ii
-            from  m as ii
-            order by ii.`message-id`
+            select element ii.m
+            from  gr as ii
+            order by ii.m.`message-id`
         ) as a at p
     )}
 from  FacebookUsers as u,
       FacebookMessages as m
-where (u.id = m.`author-id`)
+where u.id = m.`author-id`
 group by u.id as g
+group as gr
 order by g
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at03/at03.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at03/at03.3.query.sqlpp
index 44ecea4..51b8ff8 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at03/at03.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at03/at03.3.query.sqlpp
@@ -28,15 +28,16 @@
 select element {'user-since':fus,'users':(
         select element {'num':ip,'name':f.name}
         from  (
-            select element i1
-            from  i as i1
-            order by i1.name
+            select element i1.i
+            from  g as i1
+            order by i1.i.name
         ) as f at ip
     )}
 from  (
     select element fb
     from  FacebookUsers as fb
 ) as i
-group by test.`get-year`(i.`user-since`) as fus
+group by `get-year`(i.`user-since`) as fus
+group as g
 order by fus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at04/at04.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at04/at04.3.query.sqlpp
index cbe542a..bb97545 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at04/at04.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at04/at04.3.query.sqlpp
@@ -28,9 +28,9 @@
 select element {'o_custkey':ckey,'users':(
         select element {'num':ip,'orderkey':f.o_orderkey}
         from  (
-            select element i1
-            from  i as i1
-            order by i1.o_orderkey
+            select element i1.i
+            from  g as i1
+            order by i1.i.o_orderkey
         ) as f at ip
     )}
 from  (
@@ -38,6 +38,7 @@
     from  Orders as o
 ) as i
 group by i.o_custkey as ckey
+group as g
 order by ckey
 limit 3
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at05/at05.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at05/at05.3.query.sqlpp
index 7905776..86318a9 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at05/at05.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/at05/at05.3.query.sqlpp
@@ -27,16 +27,17 @@
 
 select element si
 from
-    ( select element {'xx':xx,'x':x,'uid':uid}
+    ( select element {'xx':xx,'x': (from g select value g.x),'uid':uid}
       from  Orders as x
-      where (x.o_custkey < 4)
+      where x.o_custkey < 4
       group by x.o_custkey as uid
-      with  xx as (
+      group as g
+      let  xx = (
             select element {'uid':uid,'seq':i,'item':y.o_orderkey}
             from  (
-                select element xxx
-                from  x as xxx
-                order by xxx.o_orderkey
+                select element xxx.x
+                from  g as xxx
+                order by xxx.x.o_orderkey
             ) as y at i
         )
       order by uid
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.sqlpp
index f288510..a29e59b 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.sqlpp
@@ -25,7 +25,8 @@
 insert into tmp(
   from page_views as t
   group by t.user
+  group as g
   select value {
-     "groups": t
+     "groups": (select value g.t from g)
   }
 );
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-issue550/query-issue550.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-issue550/query-issue550.3.query.sqlpp
index 33c7947..9969f77 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-issue550/query-issue550.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-issue550/query-issue550.3.query.sqlpp
@@ -25,9 +25,10 @@
 
 with  sample as {{{'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'}}}
 select element {'u':u,'recs':(
-        select element srec
-        from  s as srec
+        select element srec.s
+        from  g as srec
     )}
 from  sample as s
 group by s.uid as u
+group as g
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/having-2/having-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/having-2/having-2.3.query.sqlpp
index 7522795..8600077 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/having-2/having-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/having-2/having-2.3.query.sqlpp
@@ -23,6 +23,6 @@
     JOIN Incentive i ON e.job_category = i.job_category
     JOIN SuperStars s ON e.id = s.id
 GROUP BY e.department_id GROUP AS g
-HAVING coll_count( ( SELECT VALUE e FROM g) )>0
+HAVING coll_count( ( SELECT VALUE g.e FROM g) )>0
 SELECT e.department_id as deptId, SUM(e.salary + i.bonus) AS star_cost;
 
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-01-negative/sugar-01-negative.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-01-negative/sugar-01-negative.3.query.sqlpp
index e278914..2bc83ef 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-01-negative/sugar-01-negative.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-01-negative/sugar-01-negative.3.query.sqlpp
@@ -23,5 +23,5 @@
 // cannot be used like a SQL-92 sugar, e.g., AVG. Its input must be a collection.
 FROM Employee e
 GROUP BY e.deptno AS deptno GROUP AS g
-SELECT deptno AS deptno, COLL_AVG(e.salary) AS avgpay,
+SELECT deptno AS deptno, COLL_AVG(g.e.salary) AS avgpay,
        (SELECT i.e.name AS name, i.e.salary AS salary FROM g AS i) AS workers;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04-2/sugar-04-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04-2/sugar-04-2.3.query.sqlpp
index 4ba84b7..3852109 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04-2/sugar-04-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04-2/sugar-04-2.3.query.sqlpp
@@ -22,10 +22,11 @@
 FROM  Employee e, Incentive i
 WHERE e.job_category = i.job_category
 GROUP BY e.department_id
+GROUP AS g
 SELECT e.department_id AS deptId,
-     (  FROM i AS i
-        SELECT i.job_category AS category, i.bonus AS bonus
-        ORDER BY i.bonus DESC LIMIT 3
+     (  FROM g AS g
+        SELECT g.i.job_category AS category, g.i.bonus AS bonus
+        ORDER BY g.i.bonus DESC LIMIT 3
       ) AS job_category_details
 ORDER BY deptId DESC;
 
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04/sugar-04.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04/sugar-04.3.query.sqlpp
index d8fb8c9..abc02ea 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04/sugar-04.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/sugar-04/sugar-04.3.query.sqlpp
@@ -22,10 +22,11 @@
 FROM  Employee e, Incentive i
 WHERE e.job_category = i.job_category
 GROUP BY e.department_id AS deptId
+GROUP AS g
 SELECT deptId AS deptId,
-     (  FROM i AS i
-        SELECT i.job_category AS category, i.bonus AS bonus
-        ORDER BY i.bonus DESC LIMIT 3
+     (  FROM g AS g
+        SELECT g.i.job_category AS category, g.i.bonus AS bonus
+        ORDER BY g.i.bonus DESC LIMIT 3
       ) AS job_category_details
 ORDER BY deptId DESC;
 
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_02/hdfs_02.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_02/hdfs_02.3.query.sqlpp
index ef54a7e..358eeda 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_02/hdfs_02.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_02/hdfs_02.3.query.sqlpp
@@ -26,9 +26,10 @@
 use test;
 
 
-select element {'word':tok,'count':test.coll_count(token)}
+select element {'word':tok,'count':test.coll_count(( from g select value token ))}
 from  TextDataset as line,
       test.`word-tokens`(line.content) as token
 group by token as tok
+group as g
 order by tok
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_03/hdfs_03.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_03/hdfs_03.3.query.sqlpp
index da1ffb5..fb67e5b 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_03/hdfs_03.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_03/hdfs_03.3.query.sqlpp
@@ -28,9 +28,10 @@
 use test;
 
 
-select element {'word':tok,'count':test.coll_count(token)}
+select element {'word':tok,'count':test.coll_count((from g select value token))}
 from  TextDataset as line,
       test.`word-tokens`(line.content) as token
 group by token as tok
+group as g
 order by tok
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_shortcircuit/hdfs_shortcircuit.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_shortcircuit/hdfs_shortcircuit.3.query.sqlpp
index 3fe03f1..998dd43 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_shortcircuit/hdfs_shortcircuit.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hdfs/hdfs_shortcircuit/hdfs_shortcircuit.3.query.sqlpp
@@ -27,9 +27,10 @@
 use test;
 
 
-select element {'word':tok,'count':test.coll_count(token)}
+select element {'word':tok,'count':test.coll_count((from g select value token))}
 from  TextDataset as line,
       test.`word-tokens`(line.content) as token
 group by token as tok
+group as g
 order by tok
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hints/issue_251_dataset_hint_6/issue_251_dataset_hint_6.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hints/issue_251_dataset_hint_6/issue_251_dataset_hint_6.3.query.sqlpp
index 97e4186..b889ba9 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hints/issue_251_dataset_hint_6/issue_251_dataset_hint_6.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/hints/issue_251_dataset_hint_6/issue_251_dataset_hint_6.3.query.sqlpp
@@ -27,9 +27,10 @@
 use test;
 
 
-select element {'word':tok,'count':test.coll_count(token)}
+select element {'word':tok,'count':test.coll_count((from g select value token))}
 from  TextDataset as line,
       test.`word-tokens`(line.content) as token
 group by token as tok
+group as g
 order by tok
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/groupby-orderby-count/groupby-orderby-count.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/groupby-orderby-count/groupby-orderby-count.3.query.sqlpp
index ea20ddf..075bfb8 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/groupby-orderby-count/groupby-orderby-count.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/groupby-orderby-count/groupby-orderby-count.3.query.sqlpp
@@ -20,9 +20,10 @@
 use twitter;
 
 
-select element {'word':tok,'count':twitter.coll_count(token)}
+select element {'word':tok,'count':twitter.coll_count(( from g select value token ))}
 from  TwitterData as t,
-      twitter.`word-tokens`(t.text) as token
+      `word-tokens`(t.text) as token
 group by token as tok
-order by twitter.coll_count(token) desc,tok
+group as g
+order by coll_count(( from g select value token )) desc,tok
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/open-closed/query-issue456/query-issue456.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/open-closed/query-issue456/query-issue456.3.query.sqlpp
index 5eb9a68..341481a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/open-closed/query-issue456/query-issue456.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/open-closed/query-issue456/query-issue456.3.query.sqlpp
@@ -26,9 +26,10 @@
 use test;
 
 
-select element [x.int_m,test.count(id)]
+select element [x.int_m, coll_count(( from g select value x ))]
 from  DataOpen as x
-with  id as x.id
+let id = x.id
 group by x.int_m
+group as g
 order by x.int_m
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/semistructured/count-nullable/count-nullable.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/semistructured/count-nullable/count-nullable.3.query.sqlpp
index 7102a14..9705de5 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/semistructured/count-nullable/count-nullable.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/semistructured/count-nullable/count-nullable.3.query.sqlpp
@@ -20,8 +20,9 @@
 use test;
 
 
-select element {'custage':age,'count':test.coll_count(c)}
+select element {'custage':age,'count': coll_count(( from g select value c))}
 from  Customers as c
 group by c.age as age
+group as g
 order by age
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/spatial/cell-aggregation-with-filtering/cell-aggregation-with-filtering.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/spatial/cell-aggregation-with-filtering/cell-aggregation-with-filtering.3.query.sqlpp
index feaa109..63442c4 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/spatial/cell-aggregation-with-filtering/cell-aggregation-with-filtering.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/spatial/cell-aggregation-with-filtering/cell-aggregation-with-filtering.3.query.sqlpp
@@ -22,10 +22,11 @@
 
 select element {'cell':c,'count':num}
 from  TwitterData as t
-with  keywords as 'Allergies',
-      region as test.polygon('\n\t33.80503407287759,-126.41235263538363 \n\t44.9090773200516,-126.41235263538363 \n\t44.9090773200516,-87.65258701038363 \n\t33.80503407287759,-87.65258701038363')
-where (test.`spatial-intersect`(t.loc,region) and (t.time > test.datetime('2011-05-15T00:00:00Z')) and (t.time < test.datetime('2011-05-16T23:59:59Z')) and test.contains(t.text,keywords))
-group by test.`spatial-cell`(t.loc,test.`create-point`(24.5,-125.5),3.0,3.0) as c
-with  num as test.coll_count(t)
+let  keywords = 'Allergies',
+      region = polygon('\n\t33.80503407287759,-126.41235263538363 \n\t44.9090773200516,-126.41235263538363 \n\t44.9090773200516,-87.65258701038363 \n\t33.80503407287759,-87.65258701038363')
+where (`spatial-intersect`(t.loc,region) and (t.time > datetime('2011-05-15T00:00:00Z')) and (t.time < datetime('2011-05-16T23:59:59Z')) and contains(t.text,keywords))
+group by `spatial-cell`(t.loc,`create-point`(24.5,-125.5),3.0,3.0) as c
+group as g
+let num = coll_count((from g select value t))
 order by num
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/spatial/cell-aggregation/cell-aggregation.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/spatial/cell-aggregation/cell-aggregation.3.query.sqlpp
index 7d7cfc8..eac020a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/spatial/cell-aggregation/cell-aggregation.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/spatial/cell-aggregation/cell-aggregation.3.query.sqlpp
@@ -23,8 +23,9 @@
 with  grid as (
       select element {'cell':c,'count':num}
       from  MyData as o
-      group by test.`spatial-cell`(o.loc,test.`create-point`(0.0,0.0),5.0,5.0) as c
-      with  num as test.coll_count(o)
+      group by `spatial-cell`(o.loc, `create-point`(0.0,0.0),5.0,5.0) as c
+      group as g
+      let  num = coll_count(( from g select value o ))
       order by num
   )
 select element g
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temp-dataset/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temp-dataset/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
index 1d4cc5b..4a5eae4 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temp-dataset/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temp-dataset/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
@@ -27,28 +27,15 @@
 
 set `import-private-functions` `true`;
 
-select element {'l_returnflag':l_returnflag,'l_linestatus':l_linestatus,'sum_qty':tpch.coll_sum((
-        select element i.l_quantity
-        from  l as i
-    )),'sum_base_price':tpch.coll_sum((
-        select element i.l_extendedprice
-        from  l as i
-    )),'sum_disc_price':tpch.coll_sum((
-        select element (i.l_extendedprice * (1 - i.l_discount))
-        from  l as i
-    )),'sum_charge':tpch.coll_sum((
-        select element (i.l_extendedprice * (1 - i.l_discount) * (1 + i.l_tax))
-        from  l as i
-    )),'ave_qty':tpch.coll_avg((
-        select element i.l_quantity
-        from  l as i
-    )),'ave_price':tpch.coll_avg((
-        select element i.l_extendedprice
-        from  l as i
-    )),'ave_disc':tpch.coll_avg((
-        select element i.l_discount
-        from  l as i
-    )),'count_order':tpch.coll_count(l)}
+select element {'l_returnflag':l_returnflag,'l_linestatus':l_linestatus,
+    'sum_qty': sum(l.l_quantity),
+    'sum_base_price': sum(l.l_extendedprice),
+    'sum_disc_price': sum(l.l_extendedprice * (1 - l.l_discount)),
+    'sum_charge': sum(l.l_extendedprice * (1 - l.l_discount) * (1 + l.l_tax)),
+    'ave_qty': avg(l.l_quantity),
+    'ave_price': avg(l.l_extendedprice),
+    'ave_disc': avg(l.l_discount),
+    'count_order': count(*)}
 from  LineItem as l
 where (l.l_shipdate <= '1998-09-02')
 /* +hash */
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temporal/overlap_bins_gby_0/overlap_bins_gby_0.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temporal/overlap_bins_gby_0/overlap_bins_gby_0.3.query.sqlpp
index 40fcaea..07fa4fa 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temporal/overlap_bins_gby_0/overlap_bins_gby_0.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temporal/overlap_bins_gby_0/overlap_bins_gby_0.3.query.sqlpp
@@ -26,12 +26,13 @@
 
 select element {'tbin':gen0.bin,'interval':itv,'overlap':test.`get-overlapping-interval`(gen0.bin,itv)}
 from
-    ( select element {'bin':bin,'i':i}
+    ( select element {'bin':bin,'i': (from g select value i) }
       from (select element i from tsdata as i order by i.time) as i,
             test.`overlap-bins`(test.`interval-start-from-time`(i.time,i.duration),test.time('00:00:00'),test.`day_time_duration`('PT1H30M')) as j
       group by j as bin
+      group as g
       order by `get-interval-start`(bin)
     ) as gen0,
     gen0.i as x
-with itv as test.`interval-start-from-time`(x.time,x.duration)
+let itv = test.`interval-start-from-time`(x.time,x.duration)
 order by test.`get-interval-start`(gen0.bin);
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temporal/overlap_bins_gby_1/overlap_bins_gby_1.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temporal/overlap_bins_gby_1/overlap_bins_gby_1.3.query.sqlpp
index 941ecbc..ce3159a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temporal/overlap_bins_gby_1/overlap_bins_gby_1.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/temporal/overlap_bins_gby_1/overlap_bins_gby_1.3.query.sqlpp
@@ -24,16 +24,19 @@
 use test;
 
 
-select element {'timebin':bin,'count':count(i2),'total_ms':coll_sum((
-        select element `ms_from_day_time_duration`(`duration-from-interval`(`get-overlapping-interval`(bin,i3.interval)))
-        from  i2 as i3
+select element {'timebin':bin,
+    'count':count((select value 1 from g)),
+    'total_ms':coll_sum((
+        select element
+        `ms_from_day_time_duration`(`duration-from-interval`(`get-overlapping-interval`(bin, g.i2.interval)))
+        from g
     ))}
 from  (
     select element {'interval':`interval-start-from-time`(i1.time,i1.duration)}
     from  tsdata as i1
     order by i1.time
-) as i2,
-      `overlap-bins`(i2.interval,time('00:00:00'),`day_time_duration`('PT1H30M')) as j
+) as i2, `overlap-bins`(i2.interval,time('00:00:00'),`day_time_duration`('PT1H30M')) as j
 group by j as bin
+group as g
 order by `get-interval-start`(bin)
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/query-ASTERIXDB-1063.23.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/query-ASTERIXDB-1063.23.query.sqlpp
index c6afab6..0cad34a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/query-ASTERIXDB-1063.23.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/query-ASTERIXDB-1063.23.query.sqlpp
@@ -19,12 +19,13 @@
 use TinySocial;
 
 select h as `hour`,
-       count(t) as `count`,
+       count(*) as `count`,
        (
-         select min as `minute`, count(k) as `sum`
-         from t as k
-         group by `get-minute`(k.`send-time`) as min
+         select min as `minute`, count(*) as `sum`
+         from g as k
+         group by `get-minute`(k.t.`send-time`) as min
          order by min
        ) as `finer`
 from TweetMessages as t
-group by `get-hour`(t.`send-time`) as h;
+group by `get-hour`(t.`send-time`) as h
+group as g;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.15.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.15.query.sqlpp
index feec968..598d509 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.15.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.15.query.sqlpp
@@ -23,7 +23,7 @@
 use TinySocial;
 
 
-select element {'user':uid,'count':TinySocial.count(t)}
+select element {'user':uid,'count':TinySocial.count(*)}
 from  TweetMessages as t
 group by t.user.`screen-name` as uid
 order by uid
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.16.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.16.query.sqlpp
index f275bc4..3d114fb 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.16.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.16.query.sqlpp
@@ -23,7 +23,7 @@
 use TinySocial;
 
 
-select element {'user':uid,'count':TinySocial.count(t)}
+select element {'user':uid,'count':TinySocial.count(*)}
 from  TweetMessages as t
 /* +hash */
 group by t.user.`screen-name` as uid
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.17.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.17.query.sqlpp
index 8ad2c9f..c81a86e 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.17.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite-open/tinysocial-suite.17.query.sqlpp
@@ -26,7 +26,7 @@
 select element {'user':uid,'count':c}
 from  TweetMessages as t
 group by t.user.`screen-name` as uid
-with  c as TinySocial.count(t)
+with  c as TinySocial.count(*)
 order by c,uid desc
 limit 3
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.sqlpp
index c6afab6..8ba39bc 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/query-ASTERIXDB-1063.23.query.sqlpp
@@ -19,12 +19,14 @@
 use TinySocial;
 
 select h as `hour`,
-       count(t) as `count`,
+       count(*) as `count`,
        (
-         select min as `minute`, count(k) as `sum`
-         from t as k
-         group by `get-minute`(k.`send-time`) as min
+         select min as `minute`, count(*) as `sum`
+         from g as k
+         group by `get-minute`(k.t.`send-time`) as min
          order by min
        ) as `finer`
 from TweetMessages as t
-group by `get-hour`(t.`send-time`) as h;
+group by `get-hour`(t.`send-time`) as h
+group as g
+;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.15.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.15.query.sqlpp
index feec968..598d509 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.15.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.15.query.sqlpp
@@ -23,7 +23,7 @@
 use TinySocial;
 
 
-select element {'user':uid,'count':TinySocial.count(t)}
+select element {'user':uid,'count':TinySocial.count(*)}
 from  TweetMessages as t
 group by t.user.`screen-name` as uid
 order by uid
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.16.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.16.query.sqlpp
index f275bc4..3d114fb 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.16.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.16.query.sqlpp
@@ -23,7 +23,7 @@
 use TinySocial;
 
 
-select element {'user':uid,'count':TinySocial.count(t)}
+select element {'user':uid,'count':TinySocial.count(*)}
 from  TweetMessages as t
 /* +hash */
 group by t.user.`screen-name` as uid
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.17.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.17.query.sqlpp
index 8ad2c9f..f1c82e3 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.17.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tinysocial/tinysocial-suite/tinysocial-suite.17.query.sqlpp
@@ -26,7 +26,7 @@
 select element {'user':uid,'count':c}
 from  TweetMessages as t
 group by t.user.`screen-name` as uid
-with  c as TinySocial.count(t)
+let  c = TinySocial.count(*)
 order by c,uid desc
 limit 3
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpcds/q24a/q24a.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpcds/q24a/q24a.3.query.sqlpp
index 1286f34..9456961 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpcds/q24a/q24a.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpcds/q24a/q24a.3.query.sqlpp
@@ -65,6 +65,6 @@
 GROUP BY c_last_name
         ,c_first_name
         ,s_store_name
-HAVING paid > (SELECT value (0.05*avg(netpaid))
-                                 FROM ssales)[0]
+GROUP AS g
+HAVING paid > (SELECT value 0.05*avg(g.ssales.netpaid) FROM g)[0]
 ;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpcds/q24b/q24b.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpcds/q24b/q24b.3.query.sqlpp
index 07fa88f..0c80d1d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpcds/q24b/q24b.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpcds/q24b/q24b.3.query.sqlpp
@@ -65,6 +65,6 @@
 GROUP BY c_last_name
         ,c_first_name
         ,s_store_name
-HAVING SUM(netpaid) > (SELECT value (0.05*avg(netpaid))
-                           FROM ssales)[0]
+GROUP AS g
+HAVING paid > (SELECT value 0.05*avg(g.ssales.netpaid) FROM g)[0]
 ;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
index ae276b5..c6f5072 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
@@ -19,34 +19,34 @@
 
 use tpch;
 
+set hash_merge "true"
 
-set `import-private-functions` `true`;
-
-select element {'l_returnflag':l_returnflag,'l_linestatus':l_linestatus,'sum_qty':tpch.coll_sum((
+select element {'l_returnflag':l_returnflag,'l_linestatus':l_linestatus,'sum_qty':COLL_SUM((
         select element i.l_quantity
-        from  l as i
-    )),'sum_base_price':tpch.coll_sum((
+        from  ( from g select value l ) as i
+    )),'sum_base_price':COLL_SUM((
         select element i.l_extendedprice
-        from  l as i
-    )),'sum_disc_price':tpch.coll_sum((
+        from  ( from g select value l ) as i
+    )),'sum_disc_price':COLL_SUM((
         select element (i.l_extendedprice * (1 - i.l_discount))
-        from  l as i
-    )),'sum_charge':tpch.coll_sum((
+        from  ( from g select value l ) as i
+    )),'sum_charge':COLL_SUM((
         select element (i.l_extendedprice * (1 - i.l_discount) * (1 + i.l_tax))
-        from  l as i
-    )),'ave_qty':tpch.coll_avg((
+        from  ( from g select value l ) as i
+    )),'ave_qty':COLL_AVG((
         select element i.l_quantity
-        from  l as i
-    )),'ave_price':tpch.coll_avg((
+        from  ( from g select value l ) as i
+    )),'ave_price':COLL_AVG((
         select element i.l_extendedprice
-        from  l as i
-    )),'ave_disc':tpch.coll_avg((
+        from  ( from g select value l ) as i
+    )),'ave_disc':COLL_AVG((
         select element i.l_discount
-        from  l as i
-    )),'count_order':tpch.count(l)}
+        from  ( from g select value l ) as i
+    )),'count_order':COLL_COUNT(( from g select value l ))}
 from  LineItem as l
 where (l.l_shipdate <= '1998-09-02')
 /* +hash */
 group by l.l_returnflag as l_returnflag,l.l_linestatus as l_linestatus
+group as g
 order by l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
index cbb55c4..24180d2 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
@@ -45,9 +45,9 @@
 };
 declare function tmp2() {
 (
-    select element {'p_partkey':p_partkey,'ps_min_supplycost':tpch.coll_min((
-            select element i.ps_supplycost
-            from  pssrn as i
+    select element {'p_partkey':p_partkey,'ps_min_supplycost':COLL_MIN((
+            select element i.pssrn.ps_supplycost
+            from  g as i
         ))}
     from  Part as p,
           (
@@ -69,6 +69,7 @@
     where p.p_partkey = pssrn.p_partkey and p.p_type like '%BRASS'
     /* +hash */
     group by pssrn.p_partkey as p_partkey
+    group as g
 )
 };
 select element {'s_acctbal':t1.s_acctbal,'s_name':t1.s_name,'n_name':t1.n_name,'p_partkey':t1.p_partkey,'p_mfgr':t1.p_mfgr,'s_address':t1.s_address,'s_phone':t1.s_phone,'s_comment':t1.s_comment}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
index bddbb53..f076d71 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
@@ -27,9 +27,10 @@
 where (((c.c_mktsegment = 'BUILDING') and (c.c_custkey = o.o_custkey)) and ((l.l_orderkey = o.o_orderkey) and (o.o_orderdate < '1995-03-15') and (l.l_shipdate > '1995-03-15')))
 /* +hash */
 group by l.l_orderkey as l_orderkey,o.o_orderdate as o_orderdate,o.o_shippriority as o_shippriority
-with  revenue as tpch.coll_sum((
+group as g
+let  revenue = COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  l as i
+      from (from g select value l) as i
   ))
 order by revenue desc,o_orderdate
 limit 10
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q04_order_priority/q04_order_priority.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q04_order_priority/q04_order_priority.3.query.sqlpp
index 5293499..f693ba3 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q04_order_priority/q04_order_priority.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q04_order_priority/q04_order_priority.3.query.sqlpp
@@ -27,10 +27,10 @@
     where (l.l_commitdate < l.l_receiptdate)
 )
 };
-select element {'order_priority':o_orderpriority,'count':COLL_COUNT(o)}
-from  Orders as o,
-      tpch.tmp() as t
-where ((o.o_orderkey = t.o_orderkey) and (o.o_orderdate >= '1993-07-01') and (o.o_orderdate < '1993-10-01'))
+select element {'order_priority':o_orderpriority,'count':count(o)}
+from  Orders as o join
+      tpch.tmp() as t on o.o_orderkey = t.o_orderkey
+where o.o_orderdate >= '1993-07-01' and o.o_orderdate < '1993-10-01'
 group by o.o_orderpriority as o_orderpriority
 order by o_orderpriority
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
index 9050001..b82242e 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
@@ -46,9 +46,10 @@
 where ((c.c_nationkey = o1.s_nationkey) and (c.c_custkey = o1.o_custkey))
 /* +hash */
 group by o1.n_name as n_name
-with  revenue as tpch.coll_sum((
+group as g
+let revenue = COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  o1 as i
+      from  (from g select value o1) as i
   ))
 order by revenue desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q06_forecast_revenue_change/q06_forecast_revenue_change.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q06_forecast_revenue_change/q06_forecast_revenue_change.3.query.sqlpp
index 94ee1cd..b960393 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q06_forecast_revenue_change/q06_forecast_revenue_change.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q06_forecast_revenue_change/q06_forecast_revenue_change.3.query.sqlpp
@@ -20,8 +20,8 @@
 use tpch;
 
 
-select sum(l.l_extendedprice * l.l_discount) as revenue
-from  LineItem as l
-where l.l_shipdate >= '1994-01-01' and l.l_shipdate < '1995-01-01' and l.l_discount >= 0.05
-  and l.l_discount <= 0.07 and l.l_quantity < 24
-;
+{'revenue':COLL_SUM((
+    select element (l.l_extendedprice * l.l_discount)
+    from  LineItem as l
+    where ((l.l_shipdate >= '1994-01-01') and (l.l_shipdate < '1995-01-01') and (l.l_discount >= 0.05) and (l.l_discount <= 0.07) and (l.l_quantity < 24))
+))};
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
index deeec6b..b6344b2 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
@@ -49,9 +49,10 @@
 with  l_year0 as tpch.`get-year`(locs.l_shipdate)
 where ((locs.c_nationkey = t.c_nationkey) and (locs.s_nationkey = t.s_nationkey))
 group by t.supp_nation as supp_nation,t.cust_nation as cust_nation,l_year0 as l_year
-with  revenue as tpch.coll_sum((
-      select element (i.l_extendedprice * (1 - i.l_discount))
-      from  locs as i
+group as g
+let  revenue = COLL_SUM((
+      select element (i.locs.l_extendedprice * (1 - i.locs.l_discount))
+      from  g as i
   ))
 order by supp_nation,cust_nation,l_year
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q08_national_market_share/q08_national_market_share.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q08_national_market_share/q08_national_market_share.3.query.sqlpp
index a8b4bac..ce12ffd 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q08_national_market_share/q08_national_market_share.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q08_national_market_share/q08_national_market_share.3.query.sqlpp
@@ -20,12 +20,12 @@
 use tpch;
 
 
-select element {'year':year,'mkt_share':(tpch.coll_sum((
-          select element case when i.s_name = 'BRAZIL' then i.revenue else 0.0 end
-          from  t as i
-      )) / tpch.coll_sum((
+select element {'year':year,'mkt_share':(COLL_SUM((
+          select element case i.s_name = 'BRAZIL' when true then i.revenue when false then 0.0 end
+          from  (from g select value t) as i
+      )) / COLL_SUM((
           select element i.revenue
-          from  t as i
+          from  (from g select value t) as i
       )))}
 from  (
     select element {'year':o_year,'revenue':(slnrcop.l_extendedprice * (1 - slnrcop.l_discount)),'s_name':n2.n_name}
@@ -65,5 +65,6 @@
     where (slnrcop.s_nationkey = n2.n_nationkey)
 ) as t
 group by t.year as year
+group as g
 order by year
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
index aa5ccf7..f469707 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
@@ -20,19 +20,19 @@
 use tpch;
 
 
-select element {'nation':nation,'o_year':o_year,'sum_profit':COLL_SUM((
+select element {'nation':nation,'o_year':o_year,'sum_profit':tpch.coll_sum((
         select element pr.amount
-        from  profit as pr
+        from (from g select value profit) as pr
     ))}
 from  (
     select element {'nation':l3.n_name,'o_year':o_year,'amount':amount}
     from  Orders as o,
           (
         select element {'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}
-        from  Part as p,
+        from  Part as p join
               (
             select element {'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}
-            from  Partsupp as ps,
+            from  Partsupp as ps join
                   (
                 select element {'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}
                 from  (
@@ -40,18 +40,19 @@
                     from  Supplier as s,
                           Nation as n
                     where (n.n_nationkey = s.s_nationkey)
-                ) as s1,
+                ) as s1 join
                       LineItem as l
-                where (s1.s_suppkey = l.l_suppkey)
+                on (s1.s_suppkey = l.l_suppkey)
             ) as l1
-            where ((ps.ps_suppkey = l1.l_suppkey) and (ps.ps_partkey = l1.l_partkey))
+            on ((ps.ps_suppkey = l1.l_suppkey) and (ps.ps_partkey = l1.l_partkey))
         ) as l2
-        where (tpch.contains(p.p_name,'green') and (p.p_partkey = l2.l_partkey))
+         on (tpch.contains(p.p_name,'green') and (p.p_partkey = l2.l_partkey))
     ) as l3
     with  amount as ((l3.l_extendedprice * (1 - l3.l_discount)) - (l3.ps_supplycost * l3.l_quantity)),
           o_year as tpch.`get-year`(o.o_orderdate)
     where (o.o_orderkey = l3.l_orderkey)
 ) as profit
 group by profit.nation as nation,profit.o_year as o_year
+group as g
 order by nation,o_year desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q10_returned_item/q10_returned_item.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q10_returned_item/q10_returned_item.3.query.sqlpp
index cce248c..6038d15 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q10_returned_item/q10_returned_item.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q10_returned_item/q10_returned_item.3.query.sqlpp
@@ -34,9 +34,10 @@
     where ((l.l_orderkey = ocn.o_orderkey) and (l.l_selectflag = 'R'))
 ) as locn
 group by locn.c_custkey as c_custkey,locn.c_name as c_name,locn.c_acctbal as c_acctbal,locn.c_phone as c_phone,locn.n_name as n_name,locn.c_address as c_address,locn.c_comment as c_comment
-with  revenue as COLL_SUM((
+group as g
+let revenue = COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  locn as i
+      from  (from g select value locn) as i
   ))
 order by revenue desc
 limit 20
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
index cce248c..3a6ac4a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
@@ -34,9 +34,10 @@
     where ((l.l_orderkey = ocn.o_orderkey) and (l.l_selectflag = 'R'))
 ) as locn
 group by locn.c_custkey as c_custkey,locn.c_name as c_name,locn.c_acctbal as c_acctbal,locn.c_phone as c_phone,locn.n_name as n_name,locn.c_address as c_address,locn.c_comment as c_comment
-with  revenue as COLL_SUM((
+group as g
+let  revenue = COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  locn as i
+      from  (from g select value locn) as i
   ))
 order by revenue desc
 limit 20
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q11_important_stock/q11_important_stock.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q11_important_stock/q11_important_stock.3.query.sqlpp
index 073835c..2fb8110 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q11_important_stock/q11_important_stock.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q11_important_stock/q11_important_stock.3.query.sqlpp
@@ -20,32 +20,33 @@
 use tpch;
 
 
-with  sum as tpch.coll_sum((
+with  sum as COLL_SUM((
       select element (ps.ps_supplycost * ps.ps_availqty)
       from  Partsupp as ps,
             (
           select element {'s_suppkey':s.s_suppkey}
           from  Supplier as s,
                 Nation as n
-          where (s.s_nationkey = n.n_nationkey)
+          where s.s_nationkey = n.n_nationkey
       ) as sn
-      where (ps.ps_suppkey = sn.s_suppkey)
+      where ps.ps_suppkey = sn.s_suppkey
   ))
 select element {'partkey':t1.ps_partkey,'part_value':t1.part_value}
 from  (
-    select element {'ps_partkey':ps_partkey,'part_value':tpch.coll_sum((
-            select element (i.ps_supplycost * i.ps_availqty)
-            from  ps as i
+    select element {'ps_partkey':ps_partkey,'part_value':COLL_SUM((
+            select element (i.ps.ps_supplycost * i.ps.ps_availqty)
+            from  g as i
         ))}
     from  Partsupp as ps,
           (
         select element {'s_suppkey':s.s_suppkey}
         from  Supplier as s,
               Nation as n
-        where (s.s_nationkey = n.n_nationkey)
+        where s.s_nationkey = n.n_nationkey
     ) as sn
-    where (ps.ps_suppkey = sn.s_suppkey)
+    where ps.ps_suppkey = sn.s_suppkey
     group by ps.ps_partkey as ps_partkey
+    group as g
 ) as t1
 where (t1.part_value > (sum * 0.00001))
 order by t1.part_value desc
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q12_shipping/q12_shipping.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q12_shipping/q12_shipping.3.query.sqlpp
index c576c6c..fa54051 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q12_shipping/q12_shipping.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q12_shipping/q12_shipping.3.query.sqlpp
@@ -20,22 +20,20 @@
 use tpch;
 
 
-select element {'l_shipmode':l_shipmode,'high_line_count':tpch.coll_sum((
-        select element case i.o_orderpriority = '1-URGENT' or i.o_orderpriority = '2-HIGH'
-                       when true then 1
-                       when false then 0
-                       end
-        from  o as i
-    )),'low_line_count':tpch.coll_sum((
-        select element case i.o_orderpriority = '1-URGENT' or i.o_orderpriority = '2-HIGH'
-                       when true then 0
-                       when false then 1
-                       end
-        from  o as i
+select element {'l_shipmode':l_shipmode,'high_line_count':COLL_SUM((
+        select element
+            CASE i.o_orderpriority = '1-URGENT' OR i.o_orderpriority = '2-HIGH' WHEN true THEN 1 WHEN false THEN 0 END
+        from  (select value o from g) as i
+    )),'low_line_count':COLL_SUM((
+        select element case when i.o_orderpriority = '1-URGENT' OR i.o_orderpriority = '2-HIGH' then 0 else 1 end
+        from (select value o from g) as i
     ))}
 from  LineItem as l,
       Orders as o
-where ((o.o_orderkey = l.l_orderkey) and (l.l_commitdate < l.l_receiptdate) and (l.l_shipdate < l.l_commitdate) and (l.l_receiptdate >= '1994-01-01') and (l.l_receiptdate < '1995-01-01') and ((l.l_shipmode = 'MAIL') or (l.l_shipmode = 'SHIP')))
+where o.o_orderkey = l.l_orderkey and l.l_commitdate < l.l_receiptdate and l.l_shipdate < l.l_commitdate
+      and l.l_receiptdate >= '1994-01-01' and l.l_receiptdate < '1995-01-01'
+      and (l.l_shipmode = 'MAIL' or l.l_shipmode = 'SHIP')
 group by l.l_shipmode as l_shipmode
+group as g
 order by l_shipmode
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
index 24f4f46..4823a39 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
@@ -24,9 +24,9 @@
 
 select element {'c_count':c_count,'custdist':custdist}
 from  (
-    select element {'c_custkey':c_custkey,'c_count':tpch.coll_sum((
+    select element {'c_custkey':c_custkey,'c_count':COLL_SUM((
             select element i.o_orderkey_count
-            from  co as i
+            from (select value co from g2) as i
         ))}
     from  (
         select element {'c_custkey':c.c_custkey,'o_orderkey_count':coll_count((
@@ -37,8 +37,10 @@
         from  Customer as c
     ) as co
     group by co.c_custkey as c_custkey
+    group as g2
 ) as gco
 group by gco.c_count as c_count
-with  custdist as tpch.count(gco)
+group as g
+let  custdist = count(( select value gco from g ))
 order by custdist desc,c_count desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
index 445da07..2ec6a9c 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
@@ -20,19 +20,16 @@
 use tpch;
 
 
-select element (100.0 * tpch.coll_sum((
-      select element case i.p_type like 'PROMO%'
-                     when true then i.l_extendedprice * (1 - i.l_discount)
-                     when false then 0.0
-                     end
-      from  lp as i
-  )) / tpch.coll_sum((
+select element (100.0 * COLL_SUM((
+      select element case i.p_type like 'PROMO%' when true then i.l_extendedprice * (1 - i.l_discount) else 0.0 end
+      from (from g select value lp) as i
+  )) / COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  lp as i
+      from (from g select value lp) as i
   )))
 from  LineItem as l,
       Part as p
 let lp = {'p_type': p.p_type, 'l_extendedprice': l.l_extendedprice, 'l_discount': l.l_discount}
-where ((l.l_partkey = p.p_partkey) and (l.l_shipdate >= '1995-09-01') and (l.l_shipdate < '1995-10-01'))
-group by 1 as t group as g(lp as lp)
-;
+where l.l_partkey = p.p_partkey and l.l_shipdate >= '1995-09-01' and l.l_shipdate < '1995-10-01'
+group by 1 as t
+group as g(lp as lp);
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q15_top_supplier/q15_top_supplier.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q15_top_supplier/q15_top_supplier.3.query.sqlpp
index fdc4fcc..acd8537 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q15_top_supplier/q15_top_supplier.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q15_top_supplier/q15_top_supplier.3.query.sqlpp
@@ -22,21 +22,22 @@
 
 declare function revenue() {
 (
-    select element {'supplier_no':l_suppkey,'total_revenue':tpch.coll_sum((
-            select element (i.l_extendedprice * (1 - i.l_discount))
-            from  l as i
+    select element {'supplier_no':l_suppkey,'total_revenue':COLL_SUM((
+            select element (i.l.l_extendedprice * (1 - i.l.l_discount))
+            from g as i
         ))}
     from  LineItem as l
-    where ((l.l_shipdate >= '1996-01-01') and (l.l_shipdate < '1996-04-01'))
+    where l.l_shipdate >= '1996-01-01' and l.l_shipdate < '1996-04-01'
     group by l.l_suppkey as l_suppkey
+    group as g
 )
 };
-with  m as tpch.coll_max((
+with  m as COLL_MAX((
       select element r2.total_revenue
-      from  tpch.revenue() as r2
+      from  revenue() as r2
   ))
 select element {'s_suppkey':s.s_suppkey,'s_name':s.s_name,'s_address':s.s_address,'s_phone':s.s_phone,'total_revenue':r.total_revenue}
 from  Supplier as s,
       tpch.revenue() as r
-where ((s.s_suppkey = r.supplier_no) and (r.total_revenue < (m + 0.000000001)) and (r.total_revenue > (m - 0.000000001)))
+where s.s_suppkey = r.supplier_no and r.total_revenue < (m + 0.000000001) and r.total_revenue > (m - 0.000000001)
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
index 131c8a0..47dc549 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
@@ -41,9 +41,10 @@
     group by t.p_brand as p_brand1,t.p_type as p_type1,t.p_size as p_size1,t.ps_suppkey as ps_suppkey1
 ) as t2
 group by t2.p_brand as p_brand,t2.p_type as p_type,t2.p_size as p_size
-with  supplier_cnt as coll_count((
+group as g
+let  supplier_cnt = COLL_COUNT((
       select element i.ps_suppkey
-      from  t2 as i
+      from  (from g select value t2) as i
   ))
 order by supplier_cnt desc,p_brand,p_type,p_size
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
index 2e057d7..f22d29e 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
@@ -20,38 +20,40 @@
 use tpch;
 
 
-select element {'t_partkey':l_partkey,'t_count':tpch.count(l),'t_avg_quantity':(0.2 * tpch.coll_avg((
+select element {'t_partkey':l_partkey,'t_count':COLL_COUNT((from g select value l)),
+    't_avg_quantity':(0.2 * COLL_AVG((
           select element i.l_quantity
-          from  l as i
-      ))),'t_max_suppkey':tpch.coll_max((
+          from  (from g select value l) as i
+    ))),'t_max_suppkey':COLL_MAX((
         select element i.l_suppkey
-        from  l as i
-    )),'t_max_linenumber':tpch.coll_max((
+        from  (from g select value l) as i
+    )),'t_max_linenumber':COLL_MAX((
         select element i.l_linenumber
-        from  l as i
-    )),'t_avg_extendedprice':tpch.coll_avg((
+        from  (from g select value l) as i
+    )),'t_avg_extendedprice':COLL_AVG((
         select element i.l_extendedprice
-        from  l as i
-    )),'t_avg_discount':tpch.coll_avg((
+        from  (from g select value l) as i
+    )),'t_avg_discount':COLL_AVG((
         select element i.l_discount
-        from  l as i
-    )),'t_avg_tax':tpch.coll_avg((
+        from  (from g select value l) as i
+    )),'t_avg_tax':COLL_AVG((
         select element i.l_tax
-        from  l as i
-    )),'t_max_shipdate':tpch.coll_max((
+        from  (from g select value l) as i
+    )),'t_max_shipdate':COLL_MAX((
         select element i.l_shipdate
-        from  l as i
-    )),'t_min_commitdate':tpch.coll_min((
+        from  (from g select value l) as i
+    )),'t_min_commitdate':COLL_MIN((
         select element i.l_commitdate
-        from  l as i
-    )),'t_min_receiptdate':tpch.coll_min((
+        from  (from g select value l) as i
+    )),'t_min_receiptdate':COLL_MIN((
         select element i.l_receiptdate
-        from  l as i
-    )),'t_max_comment':tpch.coll_max((
+        from  (from g select value l) as i
+    )),'t_max_comment':COLL_MAX((
         select element i.l_comment
-        from  l as i
+        from  (from g select value l) as i
     ))}
 from  LineItem as l
 group by l.l_partkey as l_partkey
+group as g
 order by l_partkey
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
index 2f48cf3..010d03a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
@@ -22,19 +22,20 @@
 
 declare function tmp() {
 (
-    select element {'t_partkey':l_partkey,'t_avg_quantity':(0.2 * COLL_AVG((
+    select element {'t_partkey':l_partkey,'t_avg_quantity':(0.2 * tpch.coll_avg((
               select element i.l_quantity
-              from  l as i
+              from  (from g select value l) as i
           )))}
     from  LineItem as l
     group by l.l_partkey as l_partkey
+    group as g
 )
 };
 
-select element (COLL_SUM((
+select element (coll_sum((
       select element l.l_extendedprice
-      from  LineItem as l,
-            Part as p,
-            tpch.tmp() as t
-      where (((p.p_partkey = l.l_partkey) and (p.p_container = 'MED BOX')) and ((l.l_partkey = t.t_partkey) and (l.l_quantity < t.t_avg_quantity)))
+      from  tmp() as t,
+            LineItem as l,
+            Part as p
+      where p.p_partkey = l.l_partkey and p.p_container = 'MED BOX' and l.l_partkey = t.t_partkey and l.l_quantity < t.t_avg_quantity
   )) / 7.0);
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
index 5215013..1a11819 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
@@ -20,23 +20,27 @@
 use tpch;
 
 
-select element {'c_name':c_name,'c_custkey':c_custkey,'o_orderkey':o_orderkey,'o_orderdate':o_orderdate,'o_totalprice':o_totalprice,'sum_quantity':COLL_SUM((
+select element {'c_name':c_name,'c_custkey':c_custkey,'o_orderkey':o_orderkey,'o_orderdate':o_orderdate,'o_totalprice':o_totalprice,'sum_quantity':tpch.coll_sum((
         select element j.l_quantity
-        from  l as j
+        from  (from g select value l) as j
     ))}
 from  Customer as c,
       Orders as o,
       (
-    select element {'l_orderkey':l_orderkey,'t_sum_quantity':COLL_SUM((
+    select element {'l_orderkey':l_orderkey,'t_sum_quantity':tpch.coll_sum((
             select element i.l_quantity
-            from  l as i
+            from  (select value l from g2) as i
         ))}
     from  LineItem as l
     group by l.l_orderkey as l_orderkey
+    group as g2
 ) as t,
       LineItem as l
-where ((c.c_custkey = o.o_custkey) and ((o.o_orderkey = t.l_orderkey) and (t.t_sum_quantity > 30)) and (l.l_orderkey = o.o_orderkey))
-group by c.c_name as c_name,c.c_custkey as c_custkey,o.o_orderkey as o_orderkey,o.o_orderdate as o_orderdate,o.o_totalprice as o_totalprice
+where c.c_custkey = o.o_custkey and o.o_orderkey = t.l_orderkey and t.t_sum_quantity > 30
+      and l.l_orderkey = t.l_orderkey
+group by c.c_name as c_name,c.c_custkey as c_custkey,o.o_orderkey as o_orderkey,o.o_orderdate as o_orderdate,
+         o.o_totalprice as o_totalprice
+group as g
 order by o_totalprice desc,o_orderdate
 limit 100
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q19_discounted_revenue/q19_discounted_revenue.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q19_discounted_revenue/q19_discounted_revenue.3.query.sqlpp
index 9057ccb..df4e276 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q19_discounted_revenue/q19_discounted_revenue.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q19_discounted_revenue/q19_discounted_revenue.3.query.sqlpp
@@ -22,7 +22,7 @@
 
 set `import-private-functions` `true`;
 
-select element coll_sum((
+select element COLL_SUM((
     select element (l.l_extendedprice * (1 - l.l_discount))
     from  LineItem as l,
           Part as p
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
index 82e38bf..2355162 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
@@ -24,12 +24,13 @@
 from  (
     select distinct element {'ps_suppkey':pst1.ps_suppkey}
     from  (
-        select element {'l_partkey':l_partkey,'l_suppkey':l_suppkey,'sum_quantity':(0.5 * tpch.coll_sum((
+        select element {'l_partkey':l_partkey,'l_suppkey':l_suppkey,'sum_quantity':(0.5 * COLL_SUM((
                   select element i.l_quantity
-                  from  l as i
+                  from  (from g select value l) as i
               )))}
         from  LineItem as l
         group by l.l_partkey as l_partkey,l.l_suppkey as l_suppkey
+        group as g
     ) as t2,
           (
         select element {'ps_suppkey':ps.ps_suppkey,'ps_partkey':ps.ps_partkey,'ps_availqty':ps.ps_availqty}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
index 578d72e..3963de0 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
@@ -22,12 +22,12 @@
 
 declare function tmp1() {
 (
-    select element {'l_orderkey':l_orderkey,'count_suppkey':COLL_COUNT((
+    select element {'l_orderkey':l_orderkey,'count_suppkey':coll_count((
             select element i.l_suppkey
-            from  l2 as i
-        )),'max_suppkey':tpch.coll_max((
+            from  (from g1 select value g1.l2) as i
+        )),'max_suppkey': coll_max((
             select element i.l_suppkey
-            from  l2 as i
+            from  (from g1 select value g1.l2) as i
         ))}
     from  (
         select element {'l_orderkey':l_orderkey1,'l_suppkey':l_suppkey1}
@@ -35,16 +35,17 @@
         group by l.l_orderkey as l_orderkey1,l.l_suppkey as l_suppkey1
     ) as l2
     group by l2.l_orderkey as l_orderkey
+    group as g1
 )
 };
 declare function tmp2() {
 (
-    select element {'l_orderkey':l_orderkey,'count_suppkey':COLL_COUNT((
+    select element {'l_orderkey':l_orderkey,'count_suppkey':coll_count((
             select element i.l_suppkey
-            from  l2 as i
-        )),'max_suppkey':COLL_MAX((
+            from  (from g2 select value g2.l2) as i
+        )),'max_suppkey': coll_max((
             select element i.l_suppkey
-            from  l2 as i
+            from  (from g2 select value g2.l2) as i
         ))}
     from  (
         select element {'l_orderkey':l_orderkey1,'l_suppkey':l_suppkey1}
@@ -53,28 +54,30 @@
         group by l.l_orderkey as l_orderkey1,l.l_suppkey as l_suppkey1
     ) as l2
     group by l2.l_orderkey as l_orderkey
+    group as g2
 )
 };
 select element {'s_name':s_name,'numwait':numwait}
 from  (
     select element {'s_name':t3.s_name,'l_suppkey':t3.l_suppkey,'l_orderkey':t2.l_orderkey,'count_suppkey':t2.count_suppkey,'max_suppkey':t2.max_suppkey}
     from  (
-        select element {'s_name':ns.s_name,'l_orderkey':t1.l_orderkey,'l_suppkey':l.l_suppkey}
-        from  LineItem as l,
-              (
-            select element {'s_name':s.s_name,'s_suppkey':s.s_suppkey}
-            from  Nation as n,
-                  Supplier as s
-            where (s.s_nationkey = n.n_nationkey)
-        ) as ns,
-              Orders as o,
-              tpch.tmp1() as t1
-        where (((ns.s_suppkey = l.l_suppkey) and (l.l_receiptdate > l.l_commitdate)) and (o.o_orderkey = l.l_orderkey) and (l.l_orderkey = t1.l_orderkey))
+            select element {'s_name':ns.s_name,'l_orderkey':t1.l_orderkey,'l_suppkey':l.l_suppkey}
+            from  LineItem as l,
+                  (
+                        select element {'s_name':s.s_name,'s_suppkey':s.s_suppkey}
+                        from  Nation as n,
+                        Supplier as s
+                        where s.s_nationkey = n.n_nationkey
+                   ) as ns,
+                   Orders as o,
+                   tpch.tmp1() as t1
+            where ns.s_suppkey = l.l_suppkey and l.l_receiptdate > l.l_commitdate and o.o_orderkey = l.l_orderkey and l.l_orderkey = t1.l_orderkey
     ) as t3,
-          tpch.tmp2() as t2
-    where ((t2.count_suppkey >= 0) and (t3.l_orderkey = t2.l_orderkey))
+      tpch.tmp2() as t2
+    where t2.count_suppkey >= 0 and t3.l_orderkey = t2.l_orderkey
 ) as t4
 group by t4.s_name as s_name
-with  numwait as COLL_COUNT(t4)
+group as g
+let  numwait = count(( from g select value t4 ))
 order by numwait desc,s_name
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
index 328c753..bdd7323 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
@@ -22,7 +22,7 @@
 
 declare function q22_customer_tmp() {
 (
-    select element {'c_acctbal':c.c_acctbal,'c_custkey':c.c_custkey,'cntrycode':tpch.substring(c.c_phone,1,2)}
+    select element {'c_acctbal':c.c_acctbal,'c_custkey':c.c_custkey,'cntrycode':SUBSTR(c.c_phone,1,2)}
     from  Customer as c
 )
 };
@@ -31,12 +31,13 @@
       from  Customer as c
       where (c.c_acctbal > 0.0)
   ))
-select element {'cntrycode':cntrycode,'numcust':tpch.count(ct),'totacctbal':tpch.coll_sum((
+select element {'cntrycode':cntrycode,'numcust':COLL_COUNT(( from g select value ct )),'totacctbal':COLL_SUM((
         select element i.c_acctbal
-        from  ct as i
+        from  (from g select value ct) as i
     ))}
 from  tpch.q22_customer_tmp() as ct
 where (ct.c_acctbal > avg)
 group by ct.cntrycode as cntrycode
+group as g
 order by cntrycode
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue601/query-issue601.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue601/query-issue601.3.query.sqlpp
index 86568ca..3f3a50a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue601/query-issue601.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue601/query-issue601.3.query.sqlpp
@@ -17,7 +17,7 @@
  * under the License.
  */
 /*
- * Description  : This test case is to verify the fix from issue601
+ * 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
@@ -26,8 +26,9 @@
 use tpch;
 
 
-select element {'l_linenumber':l_linenumber,'count_order':COLL_COUNT(l)}
+select element {'l_linenumber':l.l_linenumber,'count_order': count((from g select value g))}
 from  LineItem as l
-group by l.l_linenumber as l_linenumber
-order by l_linenumber
+group by l.l_linenumber
+group as g
+order by l.l_linenumber
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue638/query-issue638.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue638/query-issue638.3.query.sqlpp
index 3507b9f..46a278c 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue638/query-issue638.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue638/query-issue638.3.query.sqlpp
@@ -17,7 +17,7 @@
  * under the License.
  */
 /*
- * Description  : This test case is to verify the fix from issue638
+ * 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
@@ -26,9 +26,9 @@
 use tpch;
 
 
-select element {'nation':nation,'o_year':o_year,'sum_profit':COLL_SUM((
-        select element pr.amount
-        from  profit as pr
+select element {'nation':nation,'o_year':o_year,'sum_profit':tpch.coll_sum((
+        select element g.profit.amount
+        from  g
     ))}
 from  (
     select element {'nation':l3.n_name,'o_year':o_year,'amount':amount}
@@ -52,12 +52,13 @@
             ) as l1
             where ((ps.ps_suppkey = l1.l_suppkey) and (ps.ps_partkey = l1.l_partkey))
         ) as l2
-        where (CONTAINS(p.p_name,'green') and (p.p_partkey = l2.l_partkey))
+        where (tpch.contains(p.p_name,'green') and (p.p_partkey = l2.l_partkey))
     ) as l3
     with  amount as ((l3.l_extendedprice * (1 - l3.l_discount)) - (l3.ps_supplycost * l3.l_quantity)),
           o_year as tpch.`get-year`(o.o_orderdate)
     where (o.o_orderkey = l3.l_orderkey)
 ) as profit
 group by profit.nation as nation,profit.o_year as o_year
+group as g
 order by nation,o_year desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue785-2/query-issue785-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue785-2/query-issue785-2.3.query.sqlpp
index d07b004..7a40cec 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue785-2/query-issue785-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue785-2/query-issue785-2.3.query.sqlpp
@@ -17,7 +17,7 @@
  * under the License.
  */
 /*
- * Description  : This test case is to verify the fix from issue785
+ * 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
@@ -27,30 +27,32 @@
 
 
 with  t as (
-      select element {'n_nationkey':nation.n_nationkey,'n_name':nation.n_name}
-      from  Nation as nation,
+       select element {'n_nationkey':nation.n_nationkey,'n_name':nation.n_name}
+       from  Nation as nation,
             SelectedNation as sn
-      where (nation.n_nationkey = sn.n_nationkey)
-  ),
+       where nation.n_nationkey = sn.n_nationkey
+      ),
       X as (
       select element {'nation_key':nation_key,'order_date':orderdate,'sum_price':sum}
       from  t as n,
             Customer as customer,
             Orders as orders
-      where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = n.n_nationkey))
+      where orders.o_custkey = customer.c_custkey and customer.c_nationkey = n.n_nationkey
       group by orders.o_orderdate as orderdate,n.n_nationkey as nation_key
-      with  sum as COLL_SUM((
-            select element o.o_totalprice
-            from  orders as o
-        ))
+      group as g2
+      let  sum = coll_sum((
+            select element g2.orders.o_totalprice
+            from g2
+      ))
   )
-select element {'nation_key':x.nation_key,'sum_price':(
-        select element {'orderdate':y.order_date,'sum_price':y.sum_price}
-        from  x as y
-        order by y.sum_price desc
+select element {'nation_key':nation_key,'sum_price':(
+        select element {'orderdate':g.x.order_date,'sum_price': g.x.sum_price}
+        from  g
+        order by g.x.sum_price desc
         limit 3
     )}
 from  X as x
-group by x.nation_key
-order by x.nation_key
+group by x.nation_key as nation_key
+group as g
+order by nation_key
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue785/query-issue785.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue785/query-issue785.3.query.sqlpp
index 02edeea..6c7d7bd 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue785/query-issue785.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue785/query-issue785.3.query.sqlpp
@@ -17,7 +17,7 @@
  * under the License.
  */
 /*
- * Description  : This test case is to verify the fix from issue785
+ * 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
@@ -26,28 +26,32 @@
 use tpch;
 
 
-select element {'nation_key':x.nation_key,'sum_price':(
-        select element {'orderdate':od,'sum_price':sum}
-        from  x as i
-        group by i.order_date as od
-        with  sum as COLL_SUM((
-              select element s.sum_price
-              from  i as s
+select element {'nation_key':nation_key,'sum_price':(
+        select element {'orderdate': od,'sum_price':sum}
+        from  g as i
+        group by i.x.order_date as od
+        group as g2
+        let sum = coll_sum((
+              select element g2.i.x.sum_price
+              from g2
           ))
         order by sum desc
         limit 3
     )}
 from  (
-    select element {'nation_key':nation_key,'order_date':orderdate,'sum_price':COLL_SUM((
-            select element o.o_totalprice
-            from  orders as o
-        ))}
+    select element {'nation_key':nation_key,'order_date':orderdate,
+                    'sum_price': coll_sum((
+                        select value g3.orders.o_totalprice
+                        from g3
+                    ))}
     from  Nation as n,
           Customer as customer,
           Orders as orders
-    where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = n.n_nationkey))
+    where orders.o_custkey = customer.c_custkey and customer.c_nationkey = n.n_nationkey
     group by orders.o_orderdate as orderdate,n.n_nationkey as nation_key
+    group as g3
 ) as x
-group by x.nation_key
-order by x.nation_key
+group by x.nation_key as nation_key
+group as g
+order by nation_key
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue786/query-issue786.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue786/query-issue786.3.query.sqlpp
index 7098a55..8d37f4d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue786/query-issue786.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-like/query-issue786/query-issue786.3.query.sqlpp
@@ -17,7 +17,7 @@
  * under the License.
  */
 /*
- * Description  : This test case is to verify the fix from issue786
+ * 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
@@ -30,16 +30,17 @@
         select element {'order_date':orderdate,'sum_price':sum}
         from  Orders as orders,
               Customer as customer
-        where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = nation.n_nationkey))
+        where orders.o_custkey = customer.c_custkey and customer.c_nationkey = nation.n_nationkey
         group by orders.o_orderdate as orderdate
-        with  sum as COLL_SUM((
-              select element o.o_totalprice
-              from  orders as o
-          ))
+        group as g
+        let  sum = coll_sum((
+              select element g.orders.o_totalprice
+              from g
+        ))
         order by sum desc
         limit 3
     )}
 from  Nation as nation,
       SelectedNation as sn
-where (nation.n_nationkey = sn.sn_nationkey)
+where nation.n_nationkey = sn.sn_nationkey
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue785-2/query-issue785-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue785-2/query-issue785-2.3.query.sqlpp
index 7f17b7a..9254656 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue785-2/query-issue785-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue785-2/query-issue785-2.3.query.sqlpp
@@ -43,12 +43,13 @@
 
 SELECT nation_key,
        (
-            SELECT order_date AS orderdate, sum_price
-            FROM  X // the X here refers to implicit variable X defined in the outer FROM.
+            SELECT g.X.order_date AS orderdate, g.X.sum_price
+            FROM  g // the X here refers to implicit variable X defined in the outer FROM.
             ORDER BY sum_price desc
             LIMIT 3
         ) AS sum_price
 FROM  X
 GROUP BY nation_key
+GROUP AS g
 ORDER BY nation_key
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue785/query-issue785.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue785/query-issue785.3.query.sqlpp
index c775c9a..8d4460a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue785/query-issue785.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue785/query-issue785.3.query.sqlpp
@@ -29,7 +29,7 @@
 SELECT  nation_key,
         (
             SELECT od AS orderdate,  sum_price
-            FROM x
+            FROM (FROM g SELECT VALUE g.x) x
             GROUP BY order_date AS od
             LET sum_price = sum(sum_price)
             ORDER BY sum_price desc
@@ -44,5 +44,6 @@
         GROUP BY o_orderdate as orderdate, n_nationkey as nation_key
 ) AS x
 GROUP BY nation_key
+GROUP AS g
 ORDER BY nation_key
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810-2/query-issue810-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810-2/query-issue810-2.3.query.sqlpp
index e361e74..e0eed94 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810-2/query-issue810-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810-2/query-issue810-2.3.query.sqlpp
@@ -34,11 +34,12 @@
 WHERE l_shipdate <= '1998-09-02'
 /* +hash */
 GROUP BY l_returnflag, l_linestatus
-WITH  cheaps AS (
+GROUP AS g
+LET  cheaps = (
       SELECT ELEMENT l
-      FROM  l
+      FROM  (FROM g SELECT VALUE l) l
       WHERE l_discount > 0.05
   ),
-total_charges AS sum(l_extendedprice * (1 - l_discount) * (1 + l_tax))
+total_charges = sum(l_extendedprice * (1 - l_discount) * (1 + l_tax))
 ORDER BY l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810-3/query-issue810-3.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810-3/query-issue810-3.3.query.sqlpp
index d8c9222..5186119 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810-3/query-issue810-3.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810-3/query-issue810-3.3.query.sqlpp
@@ -36,14 +36,15 @@
 WHERE l_shipdate <= '1998-09-02'
 /* +hash */
 GROUP BY l_returnflag, l_linestatus
+GROUP AS g
 WITH  expensives AS (
       SELECT ELEMENT l_discount
-      FROM  l
+      FROM  (FROM g SELECT VALUE l) l
       WHERE l_discount <= 0.05
       ),
 cheaps as (
       SELECT ELEMENT l
-      FROM  l
+      FROM  (FROM g SELECT VALUE l) l
       WHERE l_discount > 0.05
   ),
 sum_disc_prices AS sum(l_extendedprice * (1 - l_discount)),
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810/query-issue810.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810/query-issue810.3.query.sqlpp
index cf9582a..9d1597c 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810/query-issue810.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql-sugar/query-issue810/query-issue810.3.query.sqlpp
@@ -34,14 +34,15 @@
 WHERE l_shipdate <= '1998-09-02'
 /* +hash */
 GROUP BY l_returnflag, l_linestatus
+GROUP AS g
 LET  cheap = (
       SELECT ELEMENT l
-      FROM l
+      FROM (FROM g SELECT VALUE l) l
       WHERE l_discount > 0.05
 ),
 expensive = (
       SELECT ELEMENT l
-      FROM l
+      FROM (FROM g SELECT VALUE l) l
       WHERE l_discount <= 0.05
 )
 ORDER BY l_returnflag,l_linestatus
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue785-2/query-issue785-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue785-2/query-issue785-2.3.query.sqlpp
index 864f97b..eaa9369 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue785-2/query-issue785-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue785-2/query-issue785-2.3.query.sqlpp
@@ -44,11 +44,12 @@
 SELECT x.nation_key As nation_key,
        (
             SELECT y.order_date AS orderdate, y.sum_price As sum_price
-            FROM  x AS y
+            FROM  (FROM g SELECT VALUE x) AS y
             ORDER BY y.sum_price desc
             LIMIT 3
         ) AS sum_price
 FROM  X AS x
 GROUP BY x.nation_key
+GROUP AS g
 ORDER BY x.nation_key
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue785/query-issue785.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue785/query-issue785.3.query.sqlpp
index 7cdce6d..4283a19 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue785/query-issue785.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue785/query-issue785.3.query.sqlpp
@@ -29,9 +29,9 @@
 SELECT  x.nation_key AS nation_key,
         (
             SELECT od AS orderdate, sum_price AS sum_price
-            FROM x as i
+            FROM (FROM g SELECT VALUE x) as i
             GROUP BY i.order_date AS od
-            WITH sum_price AS sum(i.sum_price)
+            LET sum_price = sum(i.sum_price)
             ORDER BY sum_price desc
             LIMIT 3
         ) AS sum_price
@@ -44,5 +44,6 @@
         GROUP BY orders.o_orderdate as orderdate,n.n_nationkey as nation_key
 ) AS x
 GROUP BY x.nation_key
+GROUP AS g
 ORDER BY x.nation_key
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810-2/query-issue810-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810-2/query-issue810-2.3.query.sqlpp
index af3421f..bfd62c9 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810-2/query-issue810-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810-2/query-issue810-2.3.query.sqlpp
@@ -34,11 +34,12 @@
 WHERE l.l_shipdate <= '1998-09-02'
 /* +hash */
 GROUP BY l.l_returnflag AS l_returnflag,l.l_linestatus AS l_linestatus
-WITH  cheaps AS (
-      SELECT ELEMENT m
-      FROM  l AS m
-      WHERE m.l_discount > 0.05
+GROUP AS g
+LET  cheaps = (
+      SELECT ELEMENT m.l
+      FROM  g AS m
+      WHERE m.l.l_discount > 0.05
   ),
-total_charges AS sum(l.l_extendedprice * (1 - l.l_discount) * (1 + l.l_tax))
+total_charges = sum(l.l_extendedprice * (1 - l.l_discount) * (1 + l.l_tax))
 ORDER BY l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810-3/query-issue810-3.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810-3/query-issue810-3.3.query.sqlpp
index dbb9b88..3ee3a31 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810-3/query-issue810-3.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810-3/query-issue810-3.3.query.sqlpp
@@ -36,17 +36,18 @@
 WHERE l.l_shipdate <= '1998-09-02'
 /* +hash */
 GROUP BY l.l_returnflag AS l_returnflag,l.l_linestatus AS l_linestatus
-WITH  expensives AS (
-      SELECT ELEMENT i.l_discount
-      FROM  l AS i
-      WHERE i.l_discount <= 0.05
+GROUP AS g
+LET  expensives = (
+      SELECT ELEMENT i.l.l_discount
+      FROM  g AS i
+      WHERE i.l.l_discount <= 0.05
       ),
-cheaps as (
-      SELECT ELEMENT i
-      FROM  l AS i
-      WHERE i.l_discount > 0.05
+cheaps = (
+      SELECT ELEMENT i.l
+      FROM  g AS i
+      WHERE i.l.l_discount > 0.05
   ),
-sum_disc_prices AS sum(l.l_extendedprice * (1 - l.l_discount)),
-total_charges AS sum(l.l_extendedprice * (1 - l.l_discount) * (1 + l.l_tax))
+sum_disc_prices = sum(l.l_extendedprice * (1 - l.l_discount)),
+total_charges = sum(l.l_extendedprice * (1 - l.l_discount) * (1 + l.l_tax))
 ORDER BY l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810/query-issue810.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810/query-issue810.3.query.sqlpp
index fc80184..17e37b3 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810/query-issue810.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-sql/query-issue810/query-issue810.3.query.sqlpp
@@ -34,14 +34,15 @@
 WHERE l.l_shipdate <= '1998-09-02'
 /* +hash */
 GROUP BY l.l_returnflag AS l_returnflag,l.l_linestatus AS l_linestatus
-with  cheap as (
+GROUP AS g
+LET  cheap = (
       SELECT ELEMENT m
-      FROM l AS m
+      FROM (FROM g SELECT VALUE l) AS m
       WHERE m.l_discount > 0.05
 ),
-expensive AS (
+expensive = (
       SELECT ELEMENT a
-      FROM l AS a
+      FROM (FROM g SELECT VALUE l) AS a
       WHERE a.l_discount <= 0.05
 )
 ORDER BY l_returnflag,l_linestatus
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/nest_aggregate/nest_aggregate.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/nest_aggregate/nest_aggregate.3.query.sqlpp
index 8efcb7f..389a35d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/nest_aggregate/nest_aggregate.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/nest_aggregate/nest_aggregate.3.query.sqlpp
@@ -30,11 +30,12 @@
         select element {'order_date':orderdate,'sum_price':sum}
         from  Orders as orders,
               Customer as customer
-        where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = nation.n_nationkey))
+        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.coll_sum((
-              select element o.o_totalprice
-              from  orders as o
+        group as g
+        let  sum = coll_sum((
+              select element g.orders.o_totalprice
+              from  g
           ))
         order by sum
         limit 3
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/nest_aggregate2/nest_aggregate2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/nest_aggregate2/nest_aggregate2.3.query.sqlpp
index 1b70f0e..e137e9f9 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/nest_aggregate2/nest_aggregate2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/nest_aggregate2/nest_aggregate2.3.query.sqlpp
@@ -32,9 +32,10 @@
               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.coll_sum((
-              select element o.o_totalprice
-              from  orders as o
+        group as g
+        let  sum = tpch.coll_sum((
+              select element g.orders.o_totalprice
+              from  g
           ))
         order by sum
         limit 3
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
index ae276b5..c6f5072 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
@@ -19,34 +19,34 @@
 
 use tpch;
 
+set hash_merge "true"
 
-set `import-private-functions` `true`;
-
-select element {'l_returnflag':l_returnflag,'l_linestatus':l_linestatus,'sum_qty':tpch.coll_sum((
+select element {'l_returnflag':l_returnflag,'l_linestatus':l_linestatus,'sum_qty':COLL_SUM((
         select element i.l_quantity
-        from  l as i
-    )),'sum_base_price':tpch.coll_sum((
+        from  ( from g select value l ) as i
+    )),'sum_base_price':COLL_SUM((
         select element i.l_extendedprice
-        from  l as i
-    )),'sum_disc_price':tpch.coll_sum((
+        from  ( from g select value l ) as i
+    )),'sum_disc_price':COLL_SUM((
         select element (i.l_extendedprice * (1 - i.l_discount))
-        from  l as i
-    )),'sum_charge':tpch.coll_sum((
+        from  ( from g select value l ) as i
+    )),'sum_charge':COLL_SUM((
         select element (i.l_extendedprice * (1 - i.l_discount) * (1 + i.l_tax))
-        from  l as i
-    )),'ave_qty':tpch.coll_avg((
+        from  ( from g select value l ) as i
+    )),'ave_qty':COLL_AVG((
         select element i.l_quantity
-        from  l as i
-    )),'ave_price':tpch.coll_avg((
+        from  ( from g select value l ) as i
+    )),'ave_price':COLL_AVG((
         select element i.l_extendedprice
-        from  l as i
-    )),'ave_disc':tpch.coll_avg((
+        from  ( from g select value l ) as i
+    )),'ave_disc':COLL_AVG((
         select element i.l_discount
-        from  l as i
-    )),'count_order':tpch.count(l)}
+        from  ( from g select value l ) as i
+    )),'count_order':COLL_COUNT(( from g select value l ))}
 from  LineItem as l
 where (l.l_shipdate <= '1998-09-02')
 /* +hash */
 group by l.l_returnflag as l_returnflag,l.l_linestatus as l_linestatus
+group as g
 order by l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
index cbb55c4..24180d2 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
@@ -45,9 +45,9 @@
 };
 declare function tmp2() {
 (
-    select element {'p_partkey':p_partkey,'ps_min_supplycost':tpch.coll_min((
-            select element i.ps_supplycost
-            from  pssrn as i
+    select element {'p_partkey':p_partkey,'ps_min_supplycost':COLL_MIN((
+            select element i.pssrn.ps_supplycost
+            from  g as i
         ))}
     from  Part as p,
           (
@@ -69,6 +69,7 @@
     where p.p_partkey = pssrn.p_partkey and p.p_type like '%BRASS'
     /* +hash */
     group by pssrn.p_partkey as p_partkey
+    group as g
 )
 };
 select element {'s_acctbal':t1.s_acctbal,'s_name':t1.s_name,'n_name':t1.n_name,'p_partkey':t1.p_partkey,'p_mfgr':t1.p_mfgr,'s_address':t1.s_address,'s_phone':t1.s_phone,'s_comment':t1.s_comment}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
index bddbb53..f076d71 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
@@ -27,9 +27,10 @@
 where (((c.c_mktsegment = 'BUILDING') and (c.c_custkey = o.o_custkey)) and ((l.l_orderkey = o.o_orderkey) and (o.o_orderdate < '1995-03-15') and (l.l_shipdate > '1995-03-15')))
 /* +hash */
 group by l.l_orderkey as l_orderkey,o.o_orderdate as o_orderdate,o.o_shippriority as o_shippriority
-with  revenue as tpch.coll_sum((
+group as g
+let  revenue = COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  l as i
+      from (from g select value l) as i
   ))
 order by revenue desc,o_orderdate
 limit 10
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
index 9050001..b82242e 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
@@ -46,9 +46,10 @@
 where ((c.c_nationkey = o1.s_nationkey) and (c.c_custkey = o1.o_custkey))
 /* +hash */
 group by o1.n_name as n_name
-with  revenue as tpch.coll_sum((
+group as g
+let revenue = COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  o1 as i
+      from  (from g select value o1) as i
   ))
 order by revenue desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q06_forecast_revenue_change/q06_forecast_revenue_change.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q06_forecast_revenue_change/q06_forecast_revenue_change.3.query.sqlpp
index e1012b1..b960393 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q06_forecast_revenue_change/q06_forecast_revenue_change.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q06_forecast_revenue_change/q06_forecast_revenue_change.3.query.sqlpp
@@ -20,7 +20,7 @@
 use tpch;
 
 
-{'revenue':tpch.coll_sum((
+{'revenue':COLL_SUM((
     select element (l.l_extendedprice * l.l_discount)
     from  LineItem as l
     where ((l.l_shipdate >= '1994-01-01') and (l.l_shipdate < '1995-01-01') and (l.l_discount >= 0.05) and (l.l_discount <= 0.07) and (l.l_quantity < 24))
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
index deeec6b..b6344b2 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
@@ -49,9 +49,10 @@
 with  l_year0 as tpch.`get-year`(locs.l_shipdate)
 where ((locs.c_nationkey = t.c_nationkey) and (locs.s_nationkey = t.s_nationkey))
 group by t.supp_nation as supp_nation,t.cust_nation as cust_nation,l_year0 as l_year
-with  revenue as tpch.coll_sum((
-      select element (i.l_extendedprice * (1 - i.l_discount))
-      from  locs as i
+group as g
+let  revenue = COLL_SUM((
+      select element (i.locs.l_extendedprice * (1 - i.locs.l_discount))
+      from  g as i
   ))
 order by supp_nation,cust_nation,l_year
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q08_national_market_share/q08_national_market_share.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q08_national_market_share/q08_national_market_share.3.query.sqlpp
index b9b739d..ce12ffd 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q08_national_market_share/q08_national_market_share.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q08_national_market_share/q08_national_market_share.3.query.sqlpp
@@ -20,12 +20,12 @@
 use tpch;
 
 
-select element {'year':year,'mkt_share':(tpch.coll_sum((
+select element {'year':year,'mkt_share':(COLL_SUM((
           select element case i.s_name = 'BRAZIL' when true then i.revenue when false then 0.0 end
-          from  t as i
-      )) / tpch.coll_sum((
+          from  (from g select value t) as i
+      )) / COLL_SUM((
           select element i.revenue
-          from  t as i
+          from  (from g select value t) as i
       )))}
 from  (
     select element {'year':o_year,'revenue':(slnrcop.l_extendedprice * (1 - slnrcop.l_discount)),'s_name':n2.n_name}
@@ -65,5 +65,6 @@
     where (slnrcop.s_nationkey = n2.n_nationkey)
 ) as t
 group by t.year as year
+group as g
 order by year
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
index 0f56186..f469707 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
@@ -22,7 +22,7 @@
 
 select element {'nation':nation,'o_year':o_year,'sum_profit':tpch.coll_sum((
         select element pr.amount
-        from  profit as pr
+        from (from g select value profit) as pr
     ))}
 from  (
     select element {'nation':l3.n_name,'o_year':o_year,'amount':amount}
@@ -53,5 +53,6 @@
     where (o.o_orderkey = l3.l_orderkey)
 ) as profit
 group by profit.nation as nation,profit.o_year as o_year
+group as g
 order by nation,o_year desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q10_returned_item/q10_returned_item.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q10_returned_item/q10_returned_item.3.query.sqlpp
index 7278f81..e48e264 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q10_returned_item/q10_returned_item.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q10_returned_item/q10_returned_item.3.query.sqlpp
@@ -34,9 +34,10 @@
     where ((l.l_orderkey = ocn.o_orderkey) and (l.l_returnflag = 'R'))
 ) as locn
 group by locn.c_custkey as c_custkey,locn.c_name as c_name,locn.c_acctbal as c_acctbal,locn.c_phone as c_phone,locn.n_name as n_name,locn.c_address as c_address,locn.c_comment as c_comment
-with  revenue as tpch.coll_sum((
-      select element (i.l_extendedprice * (1 - i.l_discount))
-      from  locn as i
+group as g
+let revenue = coll_sum((
+      select element (i.locn.l_extendedprice * (1 - i.locn.l_discount))
+      from  g as i
   ))
 order by revenue desc
 limit 20
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
index 7278f81..e48e264 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
@@ -34,9 +34,10 @@
     where ((l.l_orderkey = ocn.o_orderkey) and (l.l_returnflag = 'R'))
 ) as locn
 group by locn.c_custkey as c_custkey,locn.c_name as c_name,locn.c_acctbal as c_acctbal,locn.c_phone as c_phone,locn.n_name as n_name,locn.c_address as c_address,locn.c_comment as c_comment
-with  revenue as tpch.coll_sum((
-      select element (i.l_extendedprice * (1 - i.l_discount))
-      from  locn as i
+group as g
+let revenue = coll_sum((
+      select element (i.locn.l_extendedprice * (1 - i.locn.l_discount))
+      from  g as i
   ))
 order by revenue desc
 limit 20
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q11_important_stock/q11_important_stock.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q11_important_stock/q11_important_stock.3.query.sqlpp
index 073835c..2fb8110 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q11_important_stock/q11_important_stock.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q11_important_stock/q11_important_stock.3.query.sqlpp
@@ -20,32 +20,33 @@
 use tpch;
 
 
-with  sum as tpch.coll_sum((
+with  sum as COLL_SUM((
       select element (ps.ps_supplycost * ps.ps_availqty)
       from  Partsupp as ps,
             (
           select element {'s_suppkey':s.s_suppkey}
           from  Supplier as s,
                 Nation as n
-          where (s.s_nationkey = n.n_nationkey)
+          where s.s_nationkey = n.n_nationkey
       ) as sn
-      where (ps.ps_suppkey = sn.s_suppkey)
+      where ps.ps_suppkey = sn.s_suppkey
   ))
 select element {'partkey':t1.ps_partkey,'part_value':t1.part_value}
 from  (
-    select element {'ps_partkey':ps_partkey,'part_value':tpch.coll_sum((
-            select element (i.ps_supplycost * i.ps_availqty)
-            from  ps as i
+    select element {'ps_partkey':ps_partkey,'part_value':COLL_SUM((
+            select element (i.ps.ps_supplycost * i.ps.ps_availqty)
+            from  g as i
         ))}
     from  Partsupp as ps,
           (
         select element {'s_suppkey':s.s_suppkey}
         from  Supplier as s,
               Nation as n
-        where (s.s_nationkey = n.n_nationkey)
+        where s.s_nationkey = n.n_nationkey
     ) as sn
-    where (ps.ps_suppkey = sn.s_suppkey)
+    where ps.ps_suppkey = sn.s_suppkey
     group by ps.ps_partkey as ps_partkey
+    group as g
 ) as t1
 where (t1.part_value > (sum * 0.00001))
 order by t1.part_value desc
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q12_shipping/q12_shipping.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q12_shipping/q12_shipping.3.query.sqlpp
index 395279b..fa54051 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q12_shipping/q12_shipping.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q12_shipping/q12_shipping.3.query.sqlpp
@@ -20,22 +20,20 @@
 use tpch;
 
 
-select element {'l_shipmode':l_shipmode,'high_line_count':tpch.coll_sum((
-        select element case
-                        when i.o_orderpriority = '1-URGENT' or i.o_orderpriority = '2-HIGH' then 1
-                        else 0
-                        end
-        from  o as i
-    )),'low_line_count':tpch.coll_sum((
-        select element case
-                         when i.o_orderpriority = '1-URGENT' or i.o_orderpriority = '2-HIGH' then 0
-                         else 1
-                        end
-        from  o as i
+select element {'l_shipmode':l_shipmode,'high_line_count':COLL_SUM((
+        select element
+            CASE i.o_orderpriority = '1-URGENT' OR i.o_orderpriority = '2-HIGH' WHEN true THEN 1 WHEN false THEN 0 END
+        from  (select value o from g) as i
+    )),'low_line_count':COLL_SUM((
+        select element case when i.o_orderpriority = '1-URGENT' OR i.o_orderpriority = '2-HIGH' then 0 else 1 end
+        from (select value o from g) as i
     ))}
 from  LineItem as l,
       Orders as o
-where ((o.o_orderkey = l.l_orderkey) and (l.l_commitdate < l.l_receiptdate) and (l.l_shipdate < l.l_commitdate) and (l.l_receiptdate >= '1994-01-01') and (l.l_receiptdate < '1995-01-01') and ((l.l_shipmode = 'MAIL') or (l.l_shipmode = 'SHIP')))
+where o.o_orderkey = l.l_orderkey and l.l_commitdate < l.l_receiptdate and l.l_shipdate < l.l_commitdate
+      and l.l_receiptdate >= '1994-01-01' and l.l_receiptdate < '1995-01-01'
+      and (l.l_shipmode = 'MAIL' or l.l_shipmode = 'SHIP')
 group by l.l_shipmode as l_shipmode
+group as g
 order by l_shipmode
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
index 24f4f46..4823a39 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
@@ -24,9 +24,9 @@
 
 select element {'c_count':c_count,'custdist':custdist}
 from  (
-    select element {'c_custkey':c_custkey,'c_count':tpch.coll_sum((
+    select element {'c_custkey':c_custkey,'c_count':COLL_SUM((
             select element i.o_orderkey_count
-            from  co as i
+            from (select value co from g2) as i
         ))}
     from  (
         select element {'c_custkey':c.c_custkey,'o_orderkey_count':coll_count((
@@ -37,8 +37,10 @@
         from  Customer as c
     ) as co
     group by co.c_custkey as c_custkey
+    group as g2
 ) as gco
 group by gco.c_count as c_count
-with  custdist as tpch.count(gco)
+group as g
+let  custdist = count(( select value gco from g ))
 order by custdist desc,c_count desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
index c20b6ae..2ec6a9c 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
@@ -20,19 +20,16 @@
 use tpch;
 
 
-select element (100.0 * tpch.coll_sum((
-      select element case
-                       when i.p_type like 'PROMO%' then i.l_extendedprice * (1 - i.l_discount)
-                       else 0.0
-                     end
-      from  lp as i
-  )) / tpch.coll_sum((
+select element (100.0 * COLL_SUM((
+      select element case i.p_type like 'PROMO%' when true then i.l_extendedprice * (1 - i.l_discount) else 0.0 end
+      from (from g select value lp) as i
+  )) / COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  lp as i
+      from (from g select value lp) as i
   )))
 from  LineItem as l,
       Part as p
 let lp = {'p_type': p.p_type, 'l_extendedprice': l.l_extendedprice, 'l_discount': l.l_discount}
-where ((l.l_partkey = p.p_partkey) and (l.l_shipdate >= '1995-09-01') and (l.l_shipdate < '1995-10-01'))
-group by 1 as t group as g(lp as lp)
-;
+where l.l_partkey = p.p_partkey and l.l_shipdate >= '1995-09-01' and l.l_shipdate < '1995-10-01'
+group by 1 as t
+group as g(lp as lp);
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q15_top_supplier/q15_top_supplier.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q15_top_supplier/q15_top_supplier.3.query.sqlpp
index fdc4fcc..acd8537 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q15_top_supplier/q15_top_supplier.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q15_top_supplier/q15_top_supplier.3.query.sqlpp
@@ -22,21 +22,22 @@
 
 declare function revenue() {
 (
-    select element {'supplier_no':l_suppkey,'total_revenue':tpch.coll_sum((
-            select element (i.l_extendedprice * (1 - i.l_discount))
-            from  l as i
+    select element {'supplier_no':l_suppkey,'total_revenue':COLL_SUM((
+            select element (i.l.l_extendedprice * (1 - i.l.l_discount))
+            from g as i
         ))}
     from  LineItem as l
-    where ((l.l_shipdate >= '1996-01-01') and (l.l_shipdate < '1996-04-01'))
+    where l.l_shipdate >= '1996-01-01' and l.l_shipdate < '1996-04-01'
     group by l.l_suppkey as l_suppkey
+    group as g
 )
 };
-with  m as tpch.coll_max((
+with  m as COLL_MAX((
       select element r2.total_revenue
-      from  tpch.revenue() as r2
+      from  revenue() as r2
   ))
 select element {'s_suppkey':s.s_suppkey,'s_name':s.s_name,'s_address':s.s_address,'s_phone':s.s_phone,'total_revenue':r.total_revenue}
 from  Supplier as s,
       tpch.revenue() as r
-where ((s.s_suppkey = r.supplier_no) and (r.total_revenue < (m + 0.000000001)) and (r.total_revenue > (m - 0.000000001)))
+where s.s_suppkey = r.supplier_no and r.total_revenue < (m + 0.000000001) and r.total_revenue > (m - 0.000000001)
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
index 131c8a0..47dc549 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
@@ -41,9 +41,10 @@
     group by t.p_brand as p_brand1,t.p_type as p_type1,t.p_size as p_size1,t.ps_suppkey as ps_suppkey1
 ) as t2
 group by t2.p_brand as p_brand,t2.p_type as p_type,t2.p_size as p_size
-with  supplier_cnt as coll_count((
+group as g
+let  supplier_cnt = COLL_COUNT((
       select element i.ps_suppkey
-      from  t2 as i
+      from  (from g select value t2) as i
   ))
 order by supplier_cnt desc,p_brand,p_type,p_size
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
index 2e057d7..f22d29e 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
@@ -20,38 +20,40 @@
 use tpch;
 
 
-select element {'t_partkey':l_partkey,'t_count':tpch.count(l),'t_avg_quantity':(0.2 * tpch.coll_avg((
+select element {'t_partkey':l_partkey,'t_count':COLL_COUNT((from g select value l)),
+    't_avg_quantity':(0.2 * COLL_AVG((
           select element i.l_quantity
-          from  l as i
-      ))),'t_max_suppkey':tpch.coll_max((
+          from  (from g select value l) as i
+    ))),'t_max_suppkey':COLL_MAX((
         select element i.l_suppkey
-        from  l as i
-    )),'t_max_linenumber':tpch.coll_max((
+        from  (from g select value l) as i
+    )),'t_max_linenumber':COLL_MAX((
         select element i.l_linenumber
-        from  l as i
-    )),'t_avg_extendedprice':tpch.coll_avg((
+        from  (from g select value l) as i
+    )),'t_avg_extendedprice':COLL_AVG((
         select element i.l_extendedprice
-        from  l as i
-    )),'t_avg_discount':tpch.coll_avg((
+        from  (from g select value l) as i
+    )),'t_avg_discount':COLL_AVG((
         select element i.l_discount
-        from  l as i
-    )),'t_avg_tax':tpch.coll_avg((
+        from  (from g select value l) as i
+    )),'t_avg_tax':COLL_AVG((
         select element i.l_tax
-        from  l as i
-    )),'t_max_shipdate':tpch.coll_max((
+        from  (from g select value l) as i
+    )),'t_max_shipdate':COLL_MAX((
         select element i.l_shipdate
-        from  l as i
-    )),'t_min_commitdate':tpch.coll_min((
+        from  (from g select value l) as i
+    )),'t_min_commitdate':COLL_MIN((
         select element i.l_commitdate
-        from  l as i
-    )),'t_min_receiptdate':tpch.coll_min((
+        from  (from g select value l) as i
+    )),'t_min_receiptdate':COLL_MIN((
         select element i.l_receiptdate
-        from  l as i
-    )),'t_max_comment':tpch.coll_max((
+        from  (from g select value l) as i
+    )),'t_max_comment':COLL_MAX((
         select element i.l_comment
-        from  l as i
+        from  (from g select value l) as i
     ))}
 from  LineItem as l
 group by l.l_partkey as l_partkey
+group as g
 order by l_partkey
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
index ddbbecc..010d03a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
@@ -24,16 +24,17 @@
 (
     select element {'t_partkey':l_partkey,'t_avg_quantity':(0.2 * tpch.coll_avg((
               select element i.l_quantity
-              from  l as i
+              from  (from g select value l) as i
           )))}
     from  LineItem as l
     group by l.l_partkey as l_partkey
+    group as g
 )
 };
 
-select element (tpch.coll_sum((
+select element (coll_sum((
       select element l.l_extendedprice
-      from  tpch.tmp() as t,
+      from  tmp() as t,
             LineItem as l,
             Part as p
       where p.p_partkey = l.l_partkey and p.p_container = 'MED BOX' and l.l_partkey = t.t_partkey and l.l_quantity < t.t_avg_quantity
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
index e0976a0..1a11819 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
@@ -22,21 +22,25 @@
 
 select element {'c_name':c_name,'c_custkey':c_custkey,'o_orderkey':o_orderkey,'o_orderdate':o_orderdate,'o_totalprice':o_totalprice,'sum_quantity':tpch.coll_sum((
         select element j.l_quantity
-        from  l as j
+        from  (from g select value l) as j
     ))}
 from  Customer as c,
       Orders as o,
       (
     select element {'l_orderkey':l_orderkey,'t_sum_quantity':tpch.coll_sum((
             select element i.l_quantity
-            from  l as i
+            from  (select value l from g2) as i
         ))}
     from  LineItem as l
     group by l.l_orderkey as l_orderkey
+    group as g2
 ) as t,
       LineItem as l
-where ((c.c_custkey = o.o_custkey) and (o.o_orderkey = t.l_orderkey) and (t.t_sum_quantity > 30) and (l.l_orderkey = t.l_orderkey))
-group by c.c_name as c_name,c.c_custkey as c_custkey,o.o_orderkey as o_orderkey,o.o_orderdate as o_orderdate,o.o_totalprice as o_totalprice
+where c.c_custkey = o.o_custkey and o.o_orderkey = t.l_orderkey and t.t_sum_quantity > 30
+      and l.l_orderkey = t.l_orderkey
+group by c.c_name as c_name,c.c_custkey as c_custkey,o.o_orderkey as o_orderkey,o.o_orderdate as o_orderdate,
+         o.o_totalprice as o_totalprice
+group as g
 order by o_totalprice desc,o_orderdate
 limit 100
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q19_discounted_revenue/q19_discounted_revenue.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q19_discounted_revenue/q19_discounted_revenue.3.query.sqlpp
index 9057ccb..df4e276 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q19_discounted_revenue/q19_discounted_revenue.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q19_discounted_revenue/q19_discounted_revenue.3.query.sqlpp
@@ -22,7 +22,7 @@
 
 set `import-private-functions` `true`;
 
-select element coll_sum((
+select element COLL_SUM((
     select element (l.l_extendedprice * (1 - l.l_discount))
     from  LineItem as l,
           Part as p
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
index 82e38bf..2355162 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
@@ -24,12 +24,13 @@
 from  (
     select distinct element {'ps_suppkey':pst1.ps_suppkey}
     from  (
-        select element {'l_partkey':l_partkey,'l_suppkey':l_suppkey,'sum_quantity':(0.5 * tpch.coll_sum((
+        select element {'l_partkey':l_partkey,'l_suppkey':l_suppkey,'sum_quantity':(0.5 * COLL_SUM((
                   select element i.l_quantity
-                  from  l as i
+                  from  (from g select value l) as i
               )))}
         from  LineItem as l
         group by l.l_partkey as l_partkey,l.l_suppkey as l_suppkey
+        group as g
     ) as t2,
           (
         select element {'ps_suppkey':ps.ps_suppkey,'ps_partkey':ps.ps_partkey,'ps_availqty':ps.ps_availqty}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
index 3ccb9b8..3963de0 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
@@ -24,10 +24,10 @@
 (
     select element {'l_orderkey':l_orderkey,'count_suppkey':coll_count((
             select element i.l_suppkey
-            from  l2 as i
-        )),'max_suppkey':tpch.coll_max((
+            from  (from g1 select value g1.l2) as i
+        )),'max_suppkey': coll_max((
             select element i.l_suppkey
-            from  l2 as i
+            from  (from g1 select value g1.l2) as i
         ))}
     from  (
         select element {'l_orderkey':l_orderkey1,'l_suppkey':l_suppkey1}
@@ -35,16 +35,17 @@
         group by l.l_orderkey as l_orderkey1,l.l_suppkey as l_suppkey1
     ) as l2
     group by l2.l_orderkey as l_orderkey
+    group as g1
 )
 };
 declare function tmp2() {
 (
     select element {'l_orderkey':l_orderkey,'count_suppkey':coll_count((
             select element i.l_suppkey
-            from  l2 as i
-        )),'max_suppkey':tpch.coll_max((
+            from  (from g2 select value g2.l2) as i
+        )),'max_suppkey': coll_max((
             select element i.l_suppkey
-            from  l2 as i
+            from  (from g2 select value g2.l2) as i
         ))}
     from  (
         select element {'l_orderkey':l_orderkey1,'l_suppkey':l_suppkey1}
@@ -53,6 +54,7 @@
         group by l.l_orderkey as l_orderkey1,l.l_suppkey as l_suppkey1
     ) as l2
     group by l2.l_orderkey as l_orderkey
+    group as g2
 )
 };
 select element {'s_name':s_name,'numwait':numwait}
@@ -65,16 +67,17 @@
                         select element {'s_name':s.s_name,'s_suppkey':s.s_suppkey}
                         from  Nation as n,
                         Supplier as s
-                        where (s.s_nationkey = n.n_nationkey)
+                        where s.s_nationkey = n.n_nationkey
                    ) as ns,
                    Orders as o,
                    tpch.tmp1() as t1
             where ns.s_suppkey = l.l_suppkey and l.l_receiptdate > l.l_commitdate and o.o_orderkey = l.l_orderkey and l.l_orderkey = t1.l_orderkey
     ) as t3,
       tpch.tmp2() as t2
-    where ((t2.count_suppkey >= 0) and (t3.l_orderkey = t2.l_orderkey))
+    where t2.count_suppkey >= 0 and t3.l_orderkey = t2.l_orderkey
 ) as t4
 group by t4.s_name as s_name
-with  numwait as tpch.count(t4)
+group as g
+let  numwait = count(( from g select value t4 ))
 order by numwait desc,s_name
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
index 328c753..bdd7323 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
@@ -22,7 +22,7 @@
 
 declare function q22_customer_tmp() {
 (
-    select element {'c_acctbal':c.c_acctbal,'c_custkey':c.c_custkey,'cntrycode':tpch.substring(c.c_phone,1,2)}
+    select element {'c_acctbal':c.c_acctbal,'c_custkey':c.c_custkey,'cntrycode':SUBSTR(c.c_phone,1,2)}
     from  Customer as c
 )
 };
@@ -31,12 +31,13 @@
       from  Customer as c
       where (c.c_acctbal > 0.0)
   ))
-select element {'cntrycode':cntrycode,'numcust':tpch.count(ct),'totacctbal':tpch.coll_sum((
+select element {'cntrycode':cntrycode,'numcust':COLL_COUNT(( from g select value ct )),'totacctbal':COLL_SUM((
         select element i.c_acctbal
-        from  ct as i
+        from  (from g select value ct) as i
     ))}
 from  tpch.q22_customer_tmp() as ct
 where (ct.c_acctbal > avg)
 group by ct.cntrycode as cntrycode
+group as g
 order by cntrycode
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue562/query-issue562.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue562/query-issue562.3.query.sqlpp
index eaac9ce..dc54947 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue562/query-issue562.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue562/query-issue562.3.query.sqlpp
@@ -30,26 +30,27 @@
 (
     select element {'c_acctbal':c.c_acctbal,'c_custkey':c.c_custkey,'cntrycode':phone_substr}
     from  Customer as c
-    with  phone_substr as tpch.substring(c.c_phone,1,2)
+    let  phone_substr = tpch.substring(c.c_phone,1,2)
     where ((phone_substr = '13') or (phone_substr = '31') or (phone_substr = '23') or (phone_substr = '29') or (phone_substr = '30') or (phone_substr = '18') or (phone_substr = '17'))
 )
 };
 with  avg as tpch.coll_avg((
       select element c.c_acctbal
       from  Customer as c
-      with  phone_substr as tpch.substring(c.c_phone,1,2)
+      let  phone_substr = tpch.substring(c.c_phone,1,2)
       where ((c.c_acctbal > 0.0) and ((phone_substr = '13') or (phone_substr = '31') or (phone_substr = '23') or (phone_substr = '29') or (phone_substr = '30') or (phone_substr = '18') or (phone_substr = '17')))
   ))
 select element {'cntrycode':cntrycode,'numcust':tpch.count(ct),'totacctbal':tpch.coll_sum((
         select element i.c_acctbal
-        from  ct as i
+        from  (from g select value ct) as i
     ))}
 from  tpch.q22_customer_tmp() as ct
 where (coll_count((
     select element o
     from  Orders as o
-    where (ct.c_custkey = o.o_custkey)
+    where ct.c_custkey = o.o_custkey
 )) = 0)
 group by ct.cntrycode as cntrycode
+group as g
 order by cntrycode
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue601/query-issue601.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue601/query-issue601.3.query.sqlpp
index 4613e75..3f3a50a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue601/query-issue601.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue601/query-issue601.3.query.sqlpp
@@ -26,8 +26,9 @@
 use tpch;
 
 
-select element {'l_linenumber':l.l_linenumber,'count_order':tpch.count(l)}
+select element {'l_linenumber':l.l_linenumber,'count_order': count((from g select value g))}
 from  LineItem as l
 group by l.l_linenumber
+group as g
 order by l.l_linenumber
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue785-2/query-issue785-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue785-2/query-issue785-2.3.query.sqlpp
index a694e97..7a40cec 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue785-2/query-issue785-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue785-2/query-issue785-2.3.query.sqlpp
@@ -27,30 +27,32 @@
 
 
 with  t as (
-      select element {'n_nationkey':nation.n_nationkey,'n_name':nation.n_name}
-      from  Nation as nation,
+       select element {'n_nationkey':nation.n_nationkey,'n_name':nation.n_name}
+       from  Nation as nation,
             SelectedNation as sn
-      where (nation.n_nationkey = sn.n_nationkey)
-  ),
+       where nation.n_nationkey = sn.n_nationkey
+      ),
       X as (
       select element {'nation_key':nation_key,'order_date':orderdate,'sum_price':sum}
       from  t as n,
             Customer as customer,
             Orders as orders
-      where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = n.n_nationkey))
+      where orders.o_custkey = customer.c_custkey and customer.c_nationkey = n.n_nationkey
       group by orders.o_orderdate as orderdate,n.n_nationkey as nation_key
-      with  sum as tpch.coll_sum((
-            select element o.o_totalprice
-            from  orders as o
-        ))
+      group as g2
+      let  sum = coll_sum((
+            select element g2.orders.o_totalprice
+            from g2
+      ))
   )
-select element {'nation_key':x.nation_key,'sum_price':(
-        select element {'orderdate':y.order_date,'sum_price':y.sum_price}
-        from  x as y
-        order by y.sum_price desc
+select element {'nation_key':nation_key,'sum_price':(
+        select element {'orderdate':g.x.order_date,'sum_price': g.x.sum_price}
+        from  g
+        order by g.x.sum_price desc
         limit 3
     )}
 from  X as x
-group by x.nation_key
-order by x.nation_key
+group by x.nation_key as nation_key
+group as g
+order by nation_key
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue785/query-issue785.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue785/query-issue785.3.query.sqlpp
index 214a7c2..6c7d7bd 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue785/query-issue785.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue785/query-issue785.3.query.sqlpp
@@ -26,28 +26,32 @@
 use tpch;
 
 
-select element {'nation_key':x.nation_key,'sum_price':(
-        select element {'orderdate':od,'sum_price':sum}
-        from  x as i
-        group by i.order_date as od
-        with  sum as tpch.coll_sum((
-              select element s.sum_price
-              from  i as s
+select element {'nation_key':nation_key,'sum_price':(
+        select element {'orderdate': od,'sum_price':sum}
+        from  g as i
+        group by i.x.order_date as od
+        group as g2
+        let sum = coll_sum((
+              select element g2.i.x.sum_price
+              from g2
           ))
         order by sum desc
         limit 3
     )}
 from  (
-    select element {'nation_key':nation_key,'order_date':orderdate,'sum_price':tpch.coll_sum((
-            select element o.o_totalprice
-            from  orders as o
-        ))}
+    select element {'nation_key':nation_key,'order_date':orderdate,
+                    'sum_price': coll_sum((
+                        select value g3.orders.o_totalprice
+                        from g3
+                    ))}
     from  Nation as n,
           Customer as customer,
           Orders as orders
-    where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = n.n_nationkey))
+    where orders.o_custkey = customer.c_custkey and customer.c_nationkey = n.n_nationkey
     group by orders.o_orderdate as orderdate,n.n_nationkey as nation_key
+    group as g3
 ) as x
-group by x.nation_key
-order by x.nation_key
+group by x.nation_key as nation_key
+group as g
+order by nation_key
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue786/query-issue786.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue786/query-issue786.3.query.sqlpp
index 37e3c92..8d37f4d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue786/query-issue786.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue786/query-issue786.3.query.sqlpp
@@ -30,16 +30,17 @@
         select element {'order_date':orderdate,'sum_price':sum}
         from  Orders as orders,
               Customer as customer
-        where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = nation.n_nationkey))
+        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.coll_sum((
-              select element o.o_totalprice
-              from  orders as o
-          ))
+        group as g
+        let  sum = coll_sum((
+              select element g.orders.o_totalprice
+              from g
+        ))
         order by sum desc
         limit 3
     )}
 from  Nation as nation,
       SelectedNation as sn
-where (nation.n_nationkey = sn.sn_nationkey)
+where nation.n_nationkey = sn.sn_nationkey
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810-2/query-issue810-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810-2/query-issue810-2.3.query.sqlpp
index d75ea7b..38ba16f 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810-2/query-issue810-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810-2/query-issue810-2.3.query.sqlpp
@@ -31,14 +31,15 @@
 where (l.l_shipdate <= '1998-09-02')
 /* +hash */
 group by l.l_returnflag as l_returnflag,l.l_linestatus as l_linestatus
+group as g
 with  cheaps as (
-      select element m
-      from  l as m
-      where (m.l_discount > 0.05)
+      select element m.l
+      from  g as m
+      where (m.l.l_discount > 0.05)
   ),
       charges as (
-      select element (a.l_extendedprice * (1 - a.l_discount) * (1 + a.l_tax))
-      from  l as a
+      select element (a.l.l_extendedprice * (1 - a.l.l_discount) * (1 + a.l.l_tax))
+      from  g as a
   )
 order by l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810-3/query-issue810-3.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810-3/query-issue810-3.3.query.sqlpp
index f2656ee..f25f99d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810-3/query-issue810-3.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810-3/query-issue810-3.3.query.sqlpp
@@ -31,23 +31,24 @@
 where (l.l_shipdate <= '1998-09-02')
 /* +hash */
 group by l.l_returnflag as l_returnflag,l.l_linestatus as l_linestatus
-with  expensives as (
+group as g
+let expensives = (
       select element i.l_discount
-      from  l as i
+      from  (from g select value l) as i
       where (i.l_discount <= 0.05)
   ),
-      cheaps as (
+      cheaps = (
       select element i
-      from  l as i
+      from  (from g select value l) as i
       where (i.l_discount > 0.05)
   ),
-      charges as (
+      charges = (
       select element (i.l_extendedprice * (1 - i.l_discount) * (1 + i.l_tax))
-      from  l as i
+      from  (from g select value l)  as i
   ),
-      disc_prices as (
+      disc_prices = (
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  l as i
+      from  (from g select value l)  as i
   )
 order by l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810/query-issue810.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810/query-issue810.3.query.sqlpp
index f4638f2..6877db8 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810/query-issue810.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue810/query-issue810.3.query.sqlpp
@@ -31,15 +31,16 @@
 where (l.l_shipdate <= '1998-09-02')
 /* +hash */
 group by l.l_returnflag as l_returnflag,l.l_linestatus as l_linestatus
-with  cheap as (
-      select element m
-      from  l as m
-      where (m.l_discount > 0.05)
+group as g
+let  cheap = (
+      select element m.l
+      from  g as m
+      where (m.l.l_discount > 0.05)
   ),
-      expensive as (
-      select element a
-      from  l as a
-      where (a.l_discount <= 0.05)
+      expensive = (
+      select element a.l
+      from  g as a
+      where (a.l.l_discount <= 0.05)
   )
 order by l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue827/query-issue827.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue827/query-issue827.3.query.sqlpp
index e735107..c488ab1 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue827/query-issue827.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch-with-index/query-issue827/query-issue827.3.query.sqlpp
@@ -26,10 +26,10 @@
 use tpch;
 
 
-{'count_cheaps':coll_count((
+{'count_cheaps': coll_count((
     select element l.l_quantity
     from  LineItem as l
-)),'count_expensives':tpch.coll_sum((
+)),'count_expensives': coll_sum((
     select element e
     from  (
         select element l.l_extendedprice
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate/nest_aggregate.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate/nest_aggregate.3.query.sqlpp
index 8efcb7f..389a35d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate/nest_aggregate.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate/nest_aggregate.3.query.sqlpp
@@ -30,11 +30,12 @@
         select element {'order_date':orderdate,'sum_price':sum}
         from  Orders as orders,
               Customer as customer
-        where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = nation.n_nationkey))
+        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.coll_sum((
-              select element o.o_totalprice
-              from  orders as o
+        group as g
+        let  sum = coll_sum((
+              select element g.orders.o_totalprice
+              from  g
           ))
         order by sum
         limit 3
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.3.query.sqlpp
index 1b70f0e..e137e9f9 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/nest_aggregate2/nest_aggregate2.3.query.sqlpp
@@ -32,9 +32,10 @@
               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.coll_sum((
-              select element o.o_totalprice
-              from  orders as o
+        group as g
+        let  sum = tpch.coll_sum((
+              select element g.orders.o_totalprice
+              from  g
           ))
         order by sum
         limit 3
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
index 9ac57fd..c6f5072 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q01_pricing_summary_report_nt/q01_pricing_summary_report_nt.3.query.sqlpp
@@ -23,29 +23,30 @@
 
 select element {'l_returnflag':l_returnflag,'l_linestatus':l_linestatus,'sum_qty':COLL_SUM((
         select element i.l_quantity
-        from  l as i
+        from  ( from g select value l ) as i
     )),'sum_base_price':COLL_SUM((
         select element i.l_extendedprice
-        from  l as i
+        from  ( from g select value l ) as i
     )),'sum_disc_price':COLL_SUM((
         select element (i.l_extendedprice * (1 - i.l_discount))
-        from  l as i
+        from  ( from g select value l ) as i
     )),'sum_charge':COLL_SUM((
         select element (i.l_extendedprice * (1 - i.l_discount) * (1 + i.l_tax))
-        from  l as i
+        from  ( from g select value l ) as i
     )),'ave_qty':COLL_AVG((
         select element i.l_quantity
-        from  l as i
+        from  ( from g select value l ) as i
     )),'ave_price':COLL_AVG((
         select element i.l_extendedprice
-        from  l as i
+        from  ( from g select value l ) as i
     )),'ave_disc':COLL_AVG((
         select element i.l_discount
-        from  l as i
-    )),'count_order':COLL_COUNT(l)}
+        from  ( from g select value l ) as i
+    )),'count_order':COLL_COUNT(( from g select value l ))}
 from  LineItem as l
 where (l.l_shipdate <= '1998-09-02')
 /* +hash */
 group by l.l_returnflag as l_returnflag,l.l_linestatus as l_linestatus
+group as g
 order by l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
index f6ad005..24180d2 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q02_minimum_cost_supplier/q02_minimum_cost_supplier.3.query.sqlpp
@@ -46,8 +46,8 @@
 declare function tmp2() {
 (
     select element {'p_partkey':p_partkey,'ps_min_supplycost':COLL_MIN((
-            select element i.ps_supplycost
-            from  pssrn as i
+            select element i.pssrn.ps_supplycost
+            from  g as i
         ))}
     from  Part as p,
           (
@@ -69,6 +69,7 @@
     where p.p_partkey = pssrn.p_partkey and p.p_type like '%BRASS'
     /* +hash */
     group by pssrn.p_partkey as p_partkey
+    group as g
 )
 };
 select element {'s_acctbal':t1.s_acctbal,'s_name':t1.s_name,'n_name':t1.n_name,'p_partkey':t1.p_partkey,'p_mfgr':t1.p_mfgr,'s_address':t1.s_address,'s_phone':t1.s_phone,'s_comment':t1.s_comment}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
index ef0d15d..f076d71 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q03_shipping_priority_nt/q03_shipping_priority_nt.3.query.sqlpp
@@ -27,9 +27,10 @@
 where (((c.c_mktsegment = 'BUILDING') and (c.c_custkey = o.o_custkey)) and ((l.l_orderkey = o.o_orderkey) and (o.o_orderdate < '1995-03-15') and (l.l_shipdate > '1995-03-15')))
 /* +hash */
 group by l.l_orderkey as l_orderkey,o.o_orderdate as o_orderdate,o.o_shippriority as o_shippriority
-with  revenue as COLL_SUM((
+group as g
+let  revenue = COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  l as i
+      from (from g select value l) as i
   ))
 order by revenue desc,o_orderdate
 limit 10
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
index f30ccbe..b82242e 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q05_local_supplier_volume/q05_local_supplier_volume.3.query.sqlpp
@@ -46,9 +46,10 @@
 where ((c.c_nationkey = o1.s_nationkey) and (c.c_custkey = o1.o_custkey))
 /* +hash */
 group by o1.n_name as n_name
-with  revenue as COLL_SUM((
+group as g
+let revenue = COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  o1 as i
+      from  (from g select value o1) as i
   ))
 order by revenue desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
index eacf03d..b6344b2 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q07_volume_shipping/q07_volume_shipping.3.query.sqlpp
@@ -49,9 +49,10 @@
 with  l_year0 as tpch.`get-year`(locs.l_shipdate)
 where ((locs.c_nationkey = t.c_nationkey) and (locs.s_nationkey = t.s_nationkey))
 group by t.supp_nation as supp_nation,t.cust_nation as cust_nation,l_year0 as l_year
-with  revenue as COLL_SUM((
-      select element (i.l_extendedprice * (1 - i.l_discount))
-      from  locs as i
+group as g
+let  revenue = COLL_SUM((
+      select element (i.locs.l_extendedprice * (1 - i.locs.l_discount))
+      from  g as i
   ))
 order by supp_nation,cust_nation,l_year
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q08_national_market_share/q08_national_market_share.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q08_national_market_share/q08_national_market_share.3.query.sqlpp
index 70b59e0..ce12ffd 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q08_national_market_share/q08_national_market_share.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q08_national_market_share/q08_national_market_share.3.query.sqlpp
@@ -22,10 +22,10 @@
 
 select element {'year':year,'mkt_share':(COLL_SUM((
           select element case i.s_name = 'BRAZIL' when true then i.revenue when false then 0.0 end
-          from  t as i
+          from  (from g select value t) as i
       )) / COLL_SUM((
           select element i.revenue
-          from  t as i
+          from  (from g select value t) as i
       )))}
 from  (
     select element {'year':o_year,'revenue':(slnrcop.l_extendedprice * (1 - slnrcop.l_discount)),'s_name':n2.n_name}
@@ -65,5 +65,6 @@
     where (slnrcop.s_nationkey = n2.n_nationkey)
 ) as t
 group by t.year as year
+group as g
 order by year
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
index 0f56186..f469707 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q09_product_type_profit_nt/q09_product_type_profit_nt.3.query.sqlpp
@@ -22,7 +22,7 @@
 
 select element {'nation':nation,'o_year':o_year,'sum_profit':tpch.coll_sum((
         select element pr.amount
-        from  profit as pr
+        from (from g select value profit) as pr
     ))}
 from  (
     select element {'nation':l3.n_name,'o_year':o_year,'amount':amount}
@@ -53,5 +53,6 @@
     where (o.o_orderkey = l3.l_orderkey)
 ) as profit
 group by profit.nation as nation,profit.o_year as o_year
+group as g
 order by nation,o_year desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q10_returned_item/q10_returned_item.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q10_returned_item/q10_returned_item.3.query.sqlpp
index 7278f81..e48e264 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q10_returned_item/q10_returned_item.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q10_returned_item/q10_returned_item.3.query.sqlpp
@@ -34,9 +34,10 @@
     where ((l.l_orderkey = ocn.o_orderkey) and (l.l_returnflag = 'R'))
 ) as locn
 group by locn.c_custkey as c_custkey,locn.c_name as c_name,locn.c_acctbal as c_acctbal,locn.c_phone as c_phone,locn.n_name as n_name,locn.c_address as c_address,locn.c_comment as c_comment
-with  revenue as tpch.coll_sum((
-      select element (i.l_extendedprice * (1 - i.l_discount))
-      from  locn as i
+group as g
+let revenue = coll_sum((
+      select element (i.locn.l_extendedprice * (1 - i.locn.l_discount))
+      from  g as i
   ))
 order by revenue desc
 limit 20
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
index 7278f81..e48e264 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q10_returned_item_int64/q10_returned_item_int64.3.query.sqlpp
@@ -34,9 +34,10 @@
     where ((l.l_orderkey = ocn.o_orderkey) and (l.l_returnflag = 'R'))
 ) as locn
 group by locn.c_custkey as c_custkey,locn.c_name as c_name,locn.c_acctbal as c_acctbal,locn.c_phone as c_phone,locn.n_name as n_name,locn.c_address as c_address,locn.c_comment as c_comment
-with  revenue as tpch.coll_sum((
-      select element (i.l_extendedprice * (1 - i.l_discount))
-      from  locn as i
+group as g
+let revenue = coll_sum((
+      select element (i.locn.l_extendedprice * (1 - i.locn.l_discount))
+      from  g as i
   ))
 order by revenue desc
 limit 20
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q11_important_stock/q11_important_stock.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q11_important_stock/q11_important_stock.3.query.sqlpp
index 6956c4b..2fb8110 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q11_important_stock/q11_important_stock.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q11_important_stock/q11_important_stock.3.query.sqlpp
@@ -27,25 +27,26 @@
           select element {'s_suppkey':s.s_suppkey}
           from  Supplier as s,
                 Nation as n
-          where (s.s_nationkey = n.n_nationkey)
+          where s.s_nationkey = n.n_nationkey
       ) as sn
-      where (ps.ps_suppkey = sn.s_suppkey)
+      where ps.ps_suppkey = sn.s_suppkey
   ))
 select element {'partkey':t1.ps_partkey,'part_value':t1.part_value}
 from  (
     select element {'ps_partkey':ps_partkey,'part_value':COLL_SUM((
-            select element (i.ps_supplycost * i.ps_availqty)
-            from  ps as i
+            select element (i.ps.ps_supplycost * i.ps.ps_availqty)
+            from  g as i
         ))}
     from  Partsupp as ps,
           (
         select element {'s_suppkey':s.s_suppkey}
         from  Supplier as s,
               Nation as n
-        where (s.s_nationkey = n.n_nationkey)
+        where s.s_nationkey = n.n_nationkey
     ) as sn
-    where (ps.ps_suppkey = sn.s_suppkey)
+    where ps.ps_suppkey = sn.s_suppkey
     group by ps.ps_partkey as ps_partkey
+    group as g
 ) as t1
 where (t1.part_value > (sum * 0.00001))
 order by t1.part_value desc
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q12_shipping/q12_shipping.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q12_shipping/q12_shipping.3.query.sqlpp
index 58d0c9e..fa54051 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q12_shipping/q12_shipping.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q12_shipping/q12_shipping.3.query.sqlpp
@@ -21,16 +21,19 @@
 
 
 select element {'l_shipmode':l_shipmode,'high_line_count':COLL_SUM((
-        select element 
+        select element
             CASE i.o_orderpriority = '1-URGENT' OR i.o_orderpriority = '2-HIGH' WHEN true THEN 1 WHEN false THEN 0 END
-        from  o as i
+        from  (select value o from g) as i
     )),'low_line_count':COLL_SUM((
         select element case when i.o_orderpriority = '1-URGENT' OR i.o_orderpriority = '2-HIGH' then 0 else 1 end
-        from  o as i
+        from (select value o from g) as i
     ))}
 from  LineItem as l,
       Orders as o
-where ((o.o_orderkey = l.l_orderkey) and (l.l_commitdate < l.l_receiptdate) and (l.l_shipdate < l.l_commitdate) and (l.l_receiptdate >= '1994-01-01') and (l.l_receiptdate < '1995-01-01') and ((l.l_shipmode = 'MAIL') or (l.l_shipmode = 'SHIP')))
+where o.o_orderkey = l.l_orderkey and l.l_commitdate < l.l_receiptdate and l.l_shipdate < l.l_commitdate
+      and l.l_receiptdate >= '1994-01-01' and l.l_receiptdate < '1995-01-01'
+      and (l.l_shipmode = 'MAIL' or l.l_shipmode = 'SHIP')
 group by l.l_shipmode as l_shipmode
+group as g
 order by l_shipmode
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
index e68fcc4..4823a39 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q13_customer_distribution/q13_customer_distribution.3.query.sqlpp
@@ -26,7 +26,7 @@
 from  (
     select element {'c_custkey':c_custkey,'c_count':COLL_SUM((
             select element i.o_orderkey_count
-            from  co as i
+            from (select value co from g2) as i
         ))}
     from  (
         select element {'c_custkey':c.c_custkey,'o_orderkey_count':coll_count((
@@ -37,8 +37,10 @@
         from  Customer as c
     ) as co
     group by co.c_custkey as c_custkey
+    group as g2
 ) as gco
 group by gco.c_count as c_count
-with  custdist as tpch.count(gco)
+group as g
+let  custdist = count(( select value gco from g ))
 order by custdist desc,c_count desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
index 89ade9d..2ec6a9c 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q14_promotion_effect/q14_promotion_effect.3.query.sqlpp
@@ -22,14 +22,14 @@
 
 select element (100.0 * COLL_SUM((
       select element case i.p_type like 'PROMO%' when true then i.l_extendedprice * (1 - i.l_discount) else 0.0 end
-      from  lp as i
+      from (from g select value lp) as i
   )) / COLL_SUM((
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  lp as i
+      from (from g select value lp) as i
   )))
 from  LineItem as l,
       Part as p
 let lp = {'p_type': p.p_type, 'l_extendedprice': l.l_extendedprice, 'l_discount': l.l_discount}
-where ((l.l_partkey = p.p_partkey) and (l.l_shipdate >= '1995-09-01') and (l.l_shipdate < '1995-10-01'))
+where l.l_partkey = p.p_partkey and l.l_shipdate >= '1995-09-01' and l.l_shipdate < '1995-10-01'
 group by 1 as t
 group as g(lp as lp);
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q15_top_supplier/q15_top_supplier.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q15_top_supplier/q15_top_supplier.3.query.sqlpp
index 70b5d38..acd8537 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q15_top_supplier/q15_top_supplier.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q15_top_supplier/q15_top_supplier.3.query.sqlpp
@@ -23,20 +23,21 @@
 declare function revenue() {
 (
     select element {'supplier_no':l_suppkey,'total_revenue':COLL_SUM((
-            select element (i.l_extendedprice * (1 - i.l_discount))
-            from  l as i
+            select element (i.l.l_extendedprice * (1 - i.l.l_discount))
+            from g as i
         ))}
     from  LineItem as l
-    where ((l.l_shipdate >= '1996-01-01') and (l.l_shipdate < '1996-04-01'))
+    where l.l_shipdate >= '1996-01-01' and l.l_shipdate < '1996-04-01'
     group by l.l_suppkey as l_suppkey
+    group as g
 )
 };
 with  m as COLL_MAX((
       select element r2.total_revenue
-      from  tpch.revenue() as r2
+      from  revenue() as r2
   ))
 select element {'s_suppkey':s.s_suppkey,'s_name':s.s_name,'s_address':s.s_address,'s_phone':s.s_phone,'total_revenue':r.total_revenue}
 from  Supplier as s,
       tpch.revenue() as r
-where ((s.s_suppkey = r.supplier_no) and (r.total_revenue < (m + 0.000000001)) and (r.total_revenue > (m - 0.000000001)))
+where s.s_suppkey = r.supplier_no and r.total_revenue < (m + 0.000000001) and r.total_revenue > (m - 0.000000001)
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
index 0cdbcfd..47dc549 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q16_parts_supplier_relationship/q16_parts_supplier_relationship.3.query.sqlpp
@@ -41,9 +41,10 @@
     group by t.p_brand as p_brand1,t.p_type as p_type1,t.p_size as p_size1,t.ps_suppkey as ps_suppkey1
 ) as t2
 group by t2.p_brand as p_brand,t2.p_type as p_type,t2.p_size as p_size
-with  supplier_cnt as COLL_COUNT((
+group as g
+let  supplier_cnt = COLL_COUNT((
       select element i.ps_suppkey
-      from  t2 as i
+      from  (from g select value t2) as i
   ))
 order by supplier_cnt desc,p_brand,p_type,p_size
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
index d39c75a..f22d29e 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q17_large_gby_variant/q17_large_gby_variant.3.query.sqlpp
@@ -20,38 +20,40 @@
 use tpch;
 
 
-select element {'t_partkey':l_partkey,'t_count':COLL_COUNT(l),'t_avg_quantity':(0.2 * COLL_AVG((
+select element {'t_partkey':l_partkey,'t_count':COLL_COUNT((from g select value l)),
+    't_avg_quantity':(0.2 * COLL_AVG((
           select element i.l_quantity
-          from  l as i
-      ))),'t_max_suppkey':COLL_MAX((
+          from  (from g select value l) as i
+    ))),'t_max_suppkey':COLL_MAX((
         select element i.l_suppkey
-        from  l as i
+        from  (from g select value l) as i
     )),'t_max_linenumber':COLL_MAX((
         select element i.l_linenumber
-        from  l as i
+        from  (from g select value l) as i
     )),'t_avg_extendedprice':COLL_AVG((
         select element i.l_extendedprice
-        from  l as i
+        from  (from g select value l) as i
     )),'t_avg_discount':COLL_AVG((
         select element i.l_discount
-        from  l as i
+        from  (from g select value l) as i
     )),'t_avg_tax':COLL_AVG((
         select element i.l_tax
-        from  l as i
+        from  (from g select value l) as i
     )),'t_max_shipdate':COLL_MAX((
         select element i.l_shipdate
-        from  l as i
+        from  (from g select value l) as i
     )),'t_min_commitdate':COLL_MIN((
         select element i.l_commitdate
-        from  l as i
+        from  (from g select value l) as i
     )),'t_min_receiptdate':COLL_MIN((
         select element i.l_receiptdate
-        from  l as i
+        from  (from g select value l) as i
     )),'t_max_comment':COLL_MAX((
         select element i.l_comment
-        from  l as i
+        from  (from g select value l) as i
     ))}
 from  LineItem as l
 group by l.l_partkey as l_partkey
+group as g
 order by l_partkey
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
index ddbbecc..010d03a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q17_small_quantity_order_revenue/q17_small_quantity_order_revenue.3.query.sqlpp
@@ -24,16 +24,17 @@
 (
     select element {'t_partkey':l_partkey,'t_avg_quantity':(0.2 * tpch.coll_avg((
               select element i.l_quantity
-              from  l as i
+              from  (from g select value l) as i
           )))}
     from  LineItem as l
     group by l.l_partkey as l_partkey
+    group as g
 )
 };
 
-select element (tpch.coll_sum((
+select element (coll_sum((
       select element l.l_extendedprice
-      from  tpch.tmp() as t,
+      from  tmp() as t,
             LineItem as l,
             Part as p
       where p.p_partkey = l.l_partkey and p.p_container = 'MED BOX' and l.l_partkey = t.t_partkey and l.l_quantity < t.t_avg_quantity
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
index e0976a0..1a11819 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q18_large_volume_customer/q18_large_volume_customer.3.query.sqlpp
@@ -22,21 +22,25 @@
 
 select element {'c_name':c_name,'c_custkey':c_custkey,'o_orderkey':o_orderkey,'o_orderdate':o_orderdate,'o_totalprice':o_totalprice,'sum_quantity':tpch.coll_sum((
         select element j.l_quantity
-        from  l as j
+        from  (from g select value l) as j
     ))}
 from  Customer as c,
       Orders as o,
       (
     select element {'l_orderkey':l_orderkey,'t_sum_quantity':tpch.coll_sum((
             select element i.l_quantity
-            from  l as i
+            from  (select value l from g2) as i
         ))}
     from  LineItem as l
     group by l.l_orderkey as l_orderkey
+    group as g2
 ) as t,
       LineItem as l
-where ((c.c_custkey = o.o_custkey) and (o.o_orderkey = t.l_orderkey) and (t.t_sum_quantity > 30) and (l.l_orderkey = t.l_orderkey))
-group by c.c_name as c_name,c.c_custkey as c_custkey,o.o_orderkey as o_orderkey,o.o_orderdate as o_orderdate,o.o_totalprice as o_totalprice
+where c.c_custkey = o.o_custkey and o.o_orderkey = t.l_orderkey and t.t_sum_quantity > 30
+      and l.l_orderkey = t.l_orderkey
+group by c.c_name as c_name,c.c_custkey as c_custkey,o.o_orderkey as o_orderkey,o.o_orderdate as o_orderdate,
+         o.o_totalprice as o_totalprice
+group as g
 order by o_totalprice desc,o_orderdate
 limit 100
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
index 2bc3b25..2355162 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q20_potential_part_promotion/q20_potential_part_promotion.3.query.sqlpp
@@ -26,10 +26,11 @@
     from  (
         select element {'l_partkey':l_partkey,'l_suppkey':l_suppkey,'sum_quantity':(0.5 * COLL_SUM((
                   select element i.l_quantity
-                  from  l as i
+                  from  (from g select value l) as i
               )))}
         from  LineItem as l
         group by l.l_partkey as l_partkey,l.l_suppkey as l_suppkey
+        group as g
     ) as t2,
           (
         select element {'ps_suppkey':ps.ps_suppkey,'ps_partkey':ps.ps_partkey,'ps_availqty':ps.ps_availqty}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
index 3ccb9b8..3963de0 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q21_suppliers_who_kept_orders_waiting/q21_suppliers_who_kept_orders_waiting.3.query.sqlpp
@@ -24,10 +24,10 @@
 (
     select element {'l_orderkey':l_orderkey,'count_suppkey':coll_count((
             select element i.l_suppkey
-            from  l2 as i
-        )),'max_suppkey':tpch.coll_max((
+            from  (from g1 select value g1.l2) as i
+        )),'max_suppkey': coll_max((
             select element i.l_suppkey
-            from  l2 as i
+            from  (from g1 select value g1.l2) as i
         ))}
     from  (
         select element {'l_orderkey':l_orderkey1,'l_suppkey':l_suppkey1}
@@ -35,16 +35,17 @@
         group by l.l_orderkey as l_orderkey1,l.l_suppkey as l_suppkey1
     ) as l2
     group by l2.l_orderkey as l_orderkey
+    group as g1
 )
 };
 declare function tmp2() {
 (
     select element {'l_orderkey':l_orderkey,'count_suppkey':coll_count((
             select element i.l_suppkey
-            from  l2 as i
-        )),'max_suppkey':tpch.coll_max((
+            from  (from g2 select value g2.l2) as i
+        )),'max_suppkey': coll_max((
             select element i.l_suppkey
-            from  l2 as i
+            from  (from g2 select value g2.l2) as i
         ))}
     from  (
         select element {'l_orderkey':l_orderkey1,'l_suppkey':l_suppkey1}
@@ -53,6 +54,7 @@
         group by l.l_orderkey as l_orderkey1,l.l_suppkey as l_suppkey1
     ) as l2
     group by l2.l_orderkey as l_orderkey
+    group as g2
 )
 };
 select element {'s_name':s_name,'numwait':numwait}
@@ -65,16 +67,17 @@
                         select element {'s_name':s.s_name,'s_suppkey':s.s_suppkey}
                         from  Nation as n,
                         Supplier as s
-                        where (s.s_nationkey = n.n_nationkey)
+                        where s.s_nationkey = n.n_nationkey
                    ) as ns,
                    Orders as o,
                    tpch.tmp1() as t1
             where ns.s_suppkey = l.l_suppkey and l.l_receiptdate > l.l_commitdate and o.o_orderkey = l.l_orderkey and l.l_orderkey = t1.l_orderkey
     ) as t3,
       tpch.tmp2() as t2
-    where ((t2.count_suppkey >= 0) and (t3.l_orderkey = t2.l_orderkey))
+    where t2.count_suppkey >= 0 and t3.l_orderkey = t2.l_orderkey
 ) as t4
 group by t4.s_name as s_name
-with  numwait as tpch.count(t4)
+group as g
+let  numwait = count(( from g select value t4 ))
 order by numwait desc,s_name
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
index 274adbc..bdd7323 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/q22_global_sales_opportunity/q22_global_sales_opportunity.3.query.sqlpp
@@ -31,12 +31,13 @@
       from  Customer as c
       where (c.c_acctbal > 0.0)
   ))
-select element {'cntrycode':cntrycode,'numcust':COLL_COUNT(ct),'totacctbal':COLL_SUM((
+select element {'cntrycode':cntrycode,'numcust':COLL_COUNT(( from g select value ct )),'totacctbal':COLL_SUM((
         select element i.c_acctbal
-        from  ct as i
+        from  (from g select value ct) as i
     ))}
 from  tpch.q22_customer_tmp() as ct
 where (ct.c_acctbal > avg)
 group by ct.cntrycode as cntrycode
+group as g
 order by cntrycode
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue562/query-issue562.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue562/query-issue562.3.query.sqlpp
index eaac9ce..dc54947 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue562/query-issue562.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue562/query-issue562.3.query.sqlpp
@@ -30,26 +30,27 @@
 (
     select element {'c_acctbal':c.c_acctbal,'c_custkey':c.c_custkey,'cntrycode':phone_substr}
     from  Customer as c
-    with  phone_substr as tpch.substring(c.c_phone,1,2)
+    let  phone_substr = tpch.substring(c.c_phone,1,2)
     where ((phone_substr = '13') or (phone_substr = '31') or (phone_substr = '23') or (phone_substr = '29') or (phone_substr = '30') or (phone_substr = '18') or (phone_substr = '17'))
 )
 };
 with  avg as tpch.coll_avg((
       select element c.c_acctbal
       from  Customer as c
-      with  phone_substr as tpch.substring(c.c_phone,1,2)
+      let  phone_substr = tpch.substring(c.c_phone,1,2)
       where ((c.c_acctbal > 0.0) and ((phone_substr = '13') or (phone_substr = '31') or (phone_substr = '23') or (phone_substr = '29') or (phone_substr = '30') or (phone_substr = '18') or (phone_substr = '17')))
   ))
 select element {'cntrycode':cntrycode,'numcust':tpch.count(ct),'totacctbal':tpch.coll_sum((
         select element i.c_acctbal
-        from  ct as i
+        from  (from g select value ct) as i
     ))}
 from  tpch.q22_customer_tmp() as ct
 where (coll_count((
     select element o
     from  Orders as o
-    where (ct.c_custkey = o.o_custkey)
+    where ct.c_custkey = o.o_custkey
 )) = 0)
 group by ct.cntrycode as cntrycode
+group as g
 order by cntrycode
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue601/query-issue601.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue601/query-issue601.3.query.sqlpp
index 4613e75..3f3a50a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue601/query-issue601.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue601/query-issue601.3.query.sqlpp
@@ -26,8 +26,9 @@
 use tpch;
 
 
-select element {'l_linenumber':l.l_linenumber,'count_order':tpch.count(l)}
+select element {'l_linenumber':l.l_linenumber,'count_order': count((from g select value g))}
 from  LineItem as l
 group by l.l_linenumber
+group as g
 order by l.l_linenumber
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue638/query-issue638.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue638/query-issue638.3.query.sqlpp
index 6da0192..46a278c 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue638/query-issue638.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue638/query-issue638.3.query.sqlpp
@@ -27,8 +27,8 @@
 
 
 select element {'nation':nation,'o_year':o_year,'sum_profit':tpch.coll_sum((
-        select element pr.amount
-        from  profit as pr
+        select element g.profit.amount
+        from  g
     ))}
 from  (
     select element {'nation':l3.n_name,'o_year':o_year,'amount':amount}
@@ -59,5 +59,6 @@
     where (o.o_orderkey = l3.l_orderkey)
 ) as profit
 group by profit.nation as nation,profit.o_year as o_year
+group as g
 order by nation,o_year desc
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue785-2/query-issue785-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue785-2/query-issue785-2.3.query.sqlpp
index 7141505..7a40cec 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue785-2/query-issue785-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue785-2/query-issue785-2.3.query.sqlpp
@@ -27,30 +27,32 @@
 
 
 with  t as (
-      select element {'n_nationkey':nation.n_nationkey,'n_name':nation.n_name}
-      from  Nation as nation,
+       select element {'n_nationkey':nation.n_nationkey,'n_name':nation.n_name}
+       from  Nation as nation,
             SelectedNation as sn
-      where (nation.n_nationkey = sn.n_nationkey)
-  ),
+       where nation.n_nationkey = sn.n_nationkey
+      ),
       X as (
       select element {'nation_key':nation_key,'order_date':orderdate,'sum_price':sum}
       from  t as n,
             Customer as customer,
             Orders as orders
-      where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = n.n_nationkey))
+      where orders.o_custkey = customer.c_custkey and customer.c_nationkey = n.n_nationkey
       group by orders.o_orderdate as orderdate,n.n_nationkey as nation_key
-      with  sum as tpch.coll_sum((
-            select element o.o_totalprice
-            from  orders as o
-        ))
+      group as g2
+      let  sum = coll_sum((
+            select element g2.orders.o_totalprice
+            from g2
+      ))
   )
 select element {'nation_key':nation_key,'sum_price':(
-        select element {'orderdate':y.order_date,'sum_price':y.sum_price}
-        from  x as y
-        order by y.sum_price desc
+        select element {'orderdate':g.x.order_date,'sum_price': g.x.sum_price}
+        from  g
+        order by g.x.sum_price desc
         limit 3
     )}
 from  X as x
 group by x.nation_key as nation_key
+group as g
 order by nation_key
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue785/query-issue785.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue785/query-issue785.3.query.sqlpp
index 1f5e8e3..6c7d7bd 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue785/query-issue785.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue785/query-issue785.3.query.sqlpp
@@ -27,27 +27,31 @@
 
 
 select element {'nation_key':nation_key,'sum_price':(
-        select element {'orderdate':od,'sum_price':sum}
-        from  x as i
-        group by i.order_date as od
-        with  sum as tpch.coll_sum((
-              select element s.sum_price
-              from  i as s
+        select element {'orderdate': od,'sum_price':sum}
+        from  g as i
+        group by i.x.order_date as od
+        group as g2
+        let sum = coll_sum((
+              select element g2.i.x.sum_price
+              from g2
           ))
         order by sum desc
         limit 3
     )}
 from  (
-    select element {'nation_key':nation_key,'order_date':orderdate,'sum_price':tpch.coll_sum((
-            select element o.o_totalprice
-            from  orders as o
-        ))}
+    select element {'nation_key':nation_key,'order_date':orderdate,
+                    'sum_price': coll_sum((
+                        select value g3.orders.o_totalprice
+                        from g3
+                    ))}
     from  Nation as n,
           Customer as customer,
           Orders as orders
-    where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = n.n_nationkey))
+    where orders.o_custkey = customer.c_custkey and customer.c_nationkey = n.n_nationkey
     group by orders.o_orderdate as orderdate,n.n_nationkey as nation_key
+    group as g3
 ) as x
 group by x.nation_key as nation_key
+group as g
 order by nation_key
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue786/query-issue786.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue786/query-issue786.3.query.sqlpp
index 37e3c92..8d37f4d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue786/query-issue786.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue786/query-issue786.3.query.sqlpp
@@ -30,16 +30,17 @@
         select element {'order_date':orderdate,'sum_price':sum}
         from  Orders as orders,
               Customer as customer
-        where ((orders.o_custkey = customer.c_custkey) and (customer.c_nationkey = nation.n_nationkey))
+        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.coll_sum((
-              select element o.o_totalprice
-              from  orders as o
-          ))
+        group as g
+        let  sum = coll_sum((
+              select element g.orders.o_totalprice
+              from g
+        ))
         order by sum desc
         limit 3
     )}
 from  Nation as nation,
       SelectedNation as sn
-where (nation.n_nationkey = sn.sn_nationkey)
+where nation.n_nationkey = sn.sn_nationkey
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810-2/query-issue810-2.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810-2/query-issue810-2.3.query.sqlpp
index d75ea7b..38ba16f 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810-2/query-issue810-2.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810-2/query-issue810-2.3.query.sqlpp
@@ -31,14 +31,15 @@
 where (l.l_shipdate <= '1998-09-02')
 /* +hash */
 group by l.l_returnflag as l_returnflag,l.l_linestatus as l_linestatus
+group as g
 with  cheaps as (
-      select element m
-      from  l as m
-      where (m.l_discount > 0.05)
+      select element m.l
+      from  g as m
+      where (m.l.l_discount > 0.05)
   ),
       charges as (
-      select element (a.l_extendedprice * (1 - a.l_discount) * (1 + a.l_tax))
-      from  l as a
+      select element (a.l.l_extendedprice * (1 - a.l.l_discount) * (1 + a.l.l_tax))
+      from  g as a
   )
 order by l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810-3/query-issue810-3.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810-3/query-issue810-3.3.query.sqlpp
index f2656ee..f25f99d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810-3/query-issue810-3.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810-3/query-issue810-3.3.query.sqlpp
@@ -31,23 +31,24 @@
 where (l.l_shipdate <= '1998-09-02')
 /* +hash */
 group by l.l_returnflag as l_returnflag,l.l_linestatus as l_linestatus
-with  expensives as (
+group as g
+let expensives = (
       select element i.l_discount
-      from  l as i
+      from  (from g select value l) as i
       where (i.l_discount <= 0.05)
   ),
-      cheaps as (
+      cheaps = (
       select element i
-      from  l as i
+      from  (from g select value l) as i
       where (i.l_discount > 0.05)
   ),
-      charges as (
+      charges = (
       select element (i.l_extendedprice * (1 - i.l_discount) * (1 + i.l_tax))
-      from  l as i
+      from  (from g select value l)  as i
   ),
-      disc_prices as (
+      disc_prices = (
       select element (i.l_extendedprice * (1 - i.l_discount))
-      from  l as i
+      from  (from g select value l)  as i
   )
 order by l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810/query-issue810.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810/query-issue810.3.query.sqlpp
index f4638f2..6877db8 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810/query-issue810.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue810/query-issue810.3.query.sqlpp
@@ -31,15 +31,16 @@
 where (l.l_shipdate <= '1998-09-02')
 /* +hash */
 group by l.l_returnflag as l_returnflag,l.l_linestatus as l_linestatus
-with  cheap as (
-      select element m
-      from  l as m
-      where (m.l_discount > 0.05)
+group as g
+let  cheap = (
+      select element m.l
+      from  g as m
+      where (m.l.l_discount > 0.05)
   ),
-      expensive as (
-      select element a
-      from  l as a
-      where (a.l_discount <= 0.05)
+      expensive = (
+      select element a.l
+      from  g as a
+      where (a.l.l_discount <= 0.05)
   )
 order by l_returnflag,l_linestatus
 ;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue827/query-issue827.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue827/query-issue827.3.query.sqlpp
index e735107..c488ab1 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue827/query-issue827.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/tpch/query-issue827/query-issue827.3.query.sqlpp
@@ -26,10 +26,10 @@
 use tpch;
 
 
-{'count_cheaps':coll_count((
+{'count_cheaps': coll_count((
     select element l.l_quantity
     from  LineItem as l
-)),'count_expensives':tpch.coll_sum((
+)),'count_expensives': coll_sum((
     select element e
     from  (
         select element l.l_extendedprice
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/explain/explain_field_access/explain_field_access.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/explain/explain_field_access/explain_field_access.1.adm
index 055512a..6463d6d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/explain/explain_field_access/explain_field_access.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/explain/explain_field_access/explain_field_access.1.adm
@@ -1,44 +1,44 @@
-distribute result [$$19]
+distribute result [$$17]
 -- DISTRIBUTE_RESULT  |PARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-    project ([$$19])
+    project ([$$17])
     -- STREAM_PROJECT  |PARTITIONED|
-      assign [$$19] <- [{"deptId": $#1, "star_cost": $$22}]
+      assign [$$17] <- [{"deptId": $#1, "star_cost": $$20}]
       -- ASSIGN  |PARTITIONED|
         exchange
         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-          group by ([$#1 := $$27]) decor ([]) {
-                    aggregate [$$22] <- [agg-sql-sum($$26)]
+          group by ([$#1 := $$25]) decor ([]) {
+                    aggregate [$$20] <- [agg-sql-sum($$24)]
                     -- AGGREGATE  |LOCAL|
                       nested tuple source
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                  }
-          -- SORT_GROUP_BY[$$27]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$25]  |PARTITIONED|
             exchange
-            -- HASH_PARTITION_EXCHANGE [$$27]  |PARTITIONED|
-              group by ([$$27 := $$20]) decor ([]) {
-                        aggregate [$$26] <- [agg-local-sql-sum($$17)]
+            -- HASH_PARTITION_EXCHANGE [$$25]  |PARTITIONED|
+              group by ([$$25 := $$18]) decor ([]) {
+                        aggregate [$$24] <- [agg-local-sql-sum($$15)]
                         -- AGGREGATE  |LOCAL|
                           nested tuple source
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
                      }
-              -- SORT_GROUP_BY[$$20]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$18]  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                  project ([$$17, $$20])
+                  project ([$$18, $$15])
                   -- STREAM_PROJECT  |PARTITIONED|
-                    assign [$$20] <- [substring($$24.getField("department_id"), 1)]
+                    assign [$$18] <- [substring($$22.getField("department_id"), 1)]
                     -- ASSIGN  |PARTITIONED|
-                      project ([$$17, $$24])
+                      project ([$$22, $$15])
                       -- STREAM_PROJECT  |PARTITIONED|
-                        assign [$$17, $$24] <- [$$e.getField("salary"), $$e.getField("dept")]
+                        assign [$$15, $$22] <- [$$e.getField("salary"), $$e.getField("dept")]
                         -- ASSIGN  |PARTITIONED|
                           project ([$$e])
                           -- STREAM_PROJECT  |PARTITIONED|
                             exchange
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                              data-scan []<-[$$21, $$e] <- gby.Employee
+                              data-scan []<-[$$19, $$e] <- gby.Employee
                               -- DATASOURCE_SCAN  |PARTITIONED|
                                 exchange
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/explain/explain_field_access_closed/explain_field_access_closed.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/explain/explain_field_access_closed/explain_field_access_closed.1.adm
index d7f1893..864391b 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/explain/explain_field_access_closed/explain_field_access_closed.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/explain/explain_field_access_closed/explain_field_access_closed.1.adm
@@ -1,40 +1,40 @@
-distribute result [$$18]
+distribute result [$$16]
 -- DISTRIBUTE_RESULT  |PARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-    project ([$$18])
+    project ([$$16])
     -- STREAM_PROJECT  |PARTITIONED|
-      assign [$$18] <- [{"deptId": $#1, "star_cost": $$21}]
+      assign [$$16] <- [{"deptId": $#1, "star_cost": $$19}]
       -- ASSIGN  |PARTITIONED|
         exchange
         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-          group by ([$#1 := $$25]) decor ([]) {
-                    aggregate [$$21] <- [agg-sql-sum($$24)]
+          group by ([$#1 := $$23]) decor ([]) {
+                    aggregate [$$19] <- [agg-sql-sum($$22)]
                     -- AGGREGATE  |LOCAL|
                       nested tuple source
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                  }
-          -- SORT_GROUP_BY[$$25]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$23]  |PARTITIONED|
             exchange
-            -- HASH_PARTITION_EXCHANGE [$$25]  |PARTITIONED|
-              group by ([$$25 := $$19]) decor ([]) {
-                        aggregate [$$24] <- [agg-local-sql-sum($$16)]
+            -- HASH_PARTITION_EXCHANGE [$$23]  |PARTITIONED|
+              group by ([$$23 := $$17]) decor ([]) {
+                        aggregate [$$22] <- [agg-local-sql-sum($$14)]
                         -- AGGREGATE  |LOCAL|
                           nested tuple source
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
                      }
-              -- SORT_GROUP_BY[$$19]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$17]  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                  project ([$$16, $$19])
+                  project ([$$17, $$14])
                   -- STREAM_PROJECT  |PARTITIONED|
-                    assign [$$19, $$16] <- [substring($$e.getField(1), 1), $$e.getField(2)]
+                    assign [$$17, $$14] <- [substring($$e.getField(1), 1), $$e.getField(2)]
                     -- ASSIGN  |PARTITIONED|
                       project ([$$e])
                       -- STREAM_PROJECT  |PARTITIONED|
                         exchange
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                          data-scan []<-[$$20, $$e] <- gby.Employee
+                          data-scan []<-[$$18, $$e] <- gby.Employee
                           -- DATASOURCE_SCAN  |PARTITIONED|
                             exchange
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index 93ba5b0..6afaacc 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -2585,7 +2585,7 @@
     <test-case FilePath="global-aggregate">
       <compilation-unit name="q05_error">
         <output-dir compare="Text">q01</output-dir>
-        <expected-error>Type mismatch: function field-access-by-name expects its 1st input parameter to be type object, but the actual input type is array</expected-error>
+        <expected-error>Cannot find dataset u in dataverse TinySocial nor an alias with name u</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="global-aggregate">
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupByVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupByVisitor.java
index b928285..3f173c8 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupByVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupByVisitor.java
@@ -184,7 +184,7 @@
             if (needResolution) {
                 // Tracks used variables, including WITH variables.
                 decorVars.retainAll(freeVariables);
-                // Adds all non-WITH outer scope variables, for path resolution.
+                // Adds all outer scope variables, for path resolution.
                 Collection<VariableExpr> visibleOuterScopeNonWithVars = SqlppVariableUtil.getLiveVariables(
                         scopeChecker.getCurrentScope(), false);
                 visibleOuterScopeNonWithVars.removeAll(visibleVarsInCurrentScope);
@@ -224,13 +224,10 @@
         FromClause fromClause = (FromClause) arg;
         Collection<VariableExpr> fromBindingVars =
                 fromClause == null ? new ArrayList<>() : SqlppVariableUtil.getBindingVariables(fromClause);
-        Map<Expression, VariableExpr> withVarMap = new HashMap<>();
         for (VariableExpr fromBindingVar : fromBindingVars) {
             VariableExpr varExpr = new VariableExpr();
             varExpr.setIsNewVar(false);
             varExpr.setVar(fromBindingVar.getVar());
-            VariableExpr newVarExpr = (VariableExpr) SqlppRewriteUtil.deepCopy(varExpr);
-            withVarMap.put(varExpr, newVarExpr);
         }
         // Sets the field list for the group variable.
         List<Pair<Expression, Identifier>> groupFieldList = new ArrayList<>();
@@ -245,12 +242,9 @@
             for (Pair<Expression, Identifier> groupField : gc.getGroupFieldList()) {
                 Expression newFieldExpr = groupField.first.accept(this, arg);
                 groupFieldList.add(new Pair<>(newFieldExpr, groupField.second));
-                // Adds a field binding variable into withVarList.
-                VariableExpr bindingVar = new VariableExpr(
-                        new VarIdentifier(SqlppVariableUtil.toInternalVariableName(groupField.second.getValue())));
-                withVarMap.put(newFieldExpr, bindingVar);
             }
         }
+
         gc.setGroupFieldList(groupFieldList);
 
         // Sets the group variable.
@@ -262,6 +256,7 @@
         // Adds the group variable into the "with" (i.e., re-binding) variable list.
         VariableExpr gbyVarRef = new VariableExpr(gc.getGroupVar().getVar());
         gbyVarRef.setIsNewVar(false);
+        Map<Expression, VariableExpr> withVarMap = new HashMap<>();
         withVarMap.put(gbyVarRef, (VariableExpr) SqlppRewriteUtil.deepCopy(gbyVarRef));
         gc.setWithVarMap(withVarMap);
 

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/1860
To unsubscribe, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5d6383fc7453fe7537fc291c3483708b59df3871
Gerrit-PatchSet: 1
Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Owner: Yingyi Bu <buyingyi@gmail.com>

Mime
View raw message