[ASTERIXDB-2778][COMP] Incorrect result order with grouping sets

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
- Fix incorrect order produced by ORDER BY
  in the presence of GROUP BY with GROUPING SETS
- SqlppExpressionToPlanTranslator should include
  GroupBy's decor variables when processing SELECT *
- UnionAll runtime should not emit next frame
  if it's in the failed state

Change-Id: If9ccdb70b0905ae34a46e1cc112a424eb00406b8
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/7884
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
index 92a9ab5..466a69c 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
@@ -857,6 +857,14 @@
                 outFieldBindings.add(getFieldBinding(var, outFieldNames));
             }
         }
+        if (groupbyClause.hasDecorList()) {
+            for (GbyVariableExpressionPair pair : groupbyClause.getDecorPairList()) {
+                VariableExpr var = pair.getVar();
+                if (gbyKeyVars.add(var)) {
+                    outFieldBindings.add(getFieldBinding(var, outFieldNames));
+                }
+            }
+        }
         if (groupbyClause.hasGroupVar()) {
             outFieldBindings.add(getFieldBinding(groupbyClause.getGroupVar(), outFieldNames));
         }
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/ASTERIXDB-2402.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/ASTERIXDB-2402.plan
index b6d1d39..0a4b517 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/ASTERIXDB-2402.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/ASTERIXDB-2402.plan
@@ -5,12 +5,12 @@
               {
                 -- AGGREGATE  |LOCAL|
                   -- ASSIGN  |LOCAL|
-                    -- MICRO_PRE_CLUSTERED_GROUP_BY[$$200]  |LOCAL|
+                    -- MICRO_PRE_CLUSTERED_GROUP_BY[$$215]  |LOCAL|
                             {
                               -- AGGREGATE  |LOCAL|
                                 -- NESTED_TUPLE_SOURCE  |LOCAL|
                             }
-                      -- MICRO_STABLE_SORT [$$200(ASC)]  |LOCAL|
+                      -- MICRO_STABLE_SORT [$$215(ASC)]  |LOCAL|
                         -- ASSIGN  |LOCAL|
                           -- UNNEST  |LOCAL|
                             -- SUBPLAN  |LOCAL|
@@ -27,7 +27,7 @@
             -- STREAM_PROJECT  |PARTITIONED|
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                 -- INSERT_DELETE  |PARTITIONED|
-                  -- HASH_PARTITION_EXCHANGE [$$184]  |PARTITIONED|
+                  -- HASH_PARTITION_EXCHANGE [$$199]  |PARTITIONED|
                     -- ASSIGN  |PARTITIONED|
                       -- STREAM_PROJECT  |PARTITIONED|
                         -- ASSIGN  |PARTITIONED|
@@ -37,28 +37,28 @@
                                 -- ASSIGN  |PARTITIONED|
                                   -- STREAM_PROJECT  |PARTITIONED|
                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                      -- PRE_CLUSTERED_GROUP_BY[$$238]  |PARTITIONED|
+                                      -- PRE_CLUSTERED_GROUP_BY[$$253]  |PARTITIONED|
                                               {
                                                 -- AGGREGATE  |LOCAL|
                                                   -- STREAM_SELECT  |LOCAL|
                                                     -- NESTED_TUPLE_SOURCE  |LOCAL|
                                               }
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                          -- STABLE_SORT [$$238(ASC)]  |PARTITIONED|
-                                            -- HASH_PARTITION_EXCHANGE [$$238]  |PARTITIONED|
+                                          -- STABLE_SORT [$$253(ASC)]  |PARTITIONED|
+                                            -- HASH_PARTITION_EXCHANGE [$$253]  |PARTITIONED|
                                               -- STREAM_PROJECT  |PARTITIONED|
                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                  -- HYBRID_HASH_JOIN [$$294][$$207]  |PARTITIONED|
-                                                    -- HASH_PARTITION_EXCHANGE [$$294]  |PARTITIONED|
+                                                  -- HYBRID_HASH_JOIN [$$309][$$222]  |PARTITIONED|
+                                                    -- HASH_PARTITION_EXCHANGE [$$309]  |PARTITIONED|
                                                       -- ASSIGN  |PARTITIONED|
                                                         -- STREAM_PROJECT  |PARTITIONED|
                                                           -- UNNEST  |PARTITIONED|
                                                             -- STREAM_PROJECT  |PARTITIONED|
                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                -- PRE_CLUSTERED_GROUP_BY[$$298]  |PARTITIONED|
+                                                                -- PRE_CLUSTERED_GROUP_BY[$$317]  |PARTITIONED|
                                                                         {
                                                                           -- AGGREGATE  |LOCAL|
-                                                                            -- MICRO_PRE_CLUSTERED_GROUP_BY[$$300, $$302]  |LOCAL|
+                                                                            -- MICRO_PRE_CLUSTERED_GROUP_BY[$$319, $$321]  |LOCAL|
                                                                                     {
                                                                                       -- AGGREGATE  |LOCAL|
                                                                                         -- STREAM_SELECT  |LOCAL|
@@ -68,8 +68,8 @@
                                                                                 -- NESTED_TUPLE_SOURCE  |LOCAL|
                                                                         }
                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                    -- STABLE_SORT [$$298(ASC), $$300(ASC), $$302(ASC)]  |PARTITIONED|
-                                                                      -- HASH_PARTITION_EXCHANGE [$$298]  |PARTITIONED|
+                                                                    -- STABLE_SORT [$$317(ASC), $$319(ASC), $$321(ASC)]  |PARTITIONED|
+                                                                      -- HASH_PARTITION_EXCHANGE [$$317]  |PARTITIONED|
                                                                         -- UNION_ALL  |PARTITIONED|
                                                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                             -- STREAM_PROJECT  |PARTITIONED|
@@ -106,7 +106,7 @@
                                                                                                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                                               -- BTREE_SEARCH  |PARTITIONED|
                                                                                                                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                                                                                  -- STABLE_SORT [$$245(ASC)]  |PARTITIONED|
+                                                                                                                                  -- STABLE_SORT [$$260(ASC)]  |PARTITIONED|
                                                                                                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                                                       -- STREAM_PROJECT  |PARTITIONED|
                                                                                                                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -152,7 +152,7 @@
                                                                                                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                                         -- BTREE_SEARCH  |PARTITIONED|
                                                                                                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                                                                                            -- STABLE_SORT [$$245(ASC)]  |PARTITIONED|
+                                                                                                                            -- STABLE_SORT [$$260(ASC)]  |PARTITIONED|
                                                                                                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                                                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -166,12 +166,12 @@
                                                                                                                 -- DATASOURCE_SCAN  |PARTITIONED|
                                                                                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                                                                                     -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-                                                    -- HASH_PARTITION_EXCHANGE [$$207]  |PARTITIONED|
+                                                    -- HASH_PARTITION_EXCHANGE [$$222]  |PARTITIONED|
                                                       -- ASSIGN  |PARTITIONED|
                                                         -- STREAM_PROJECT  |PARTITIONED|
                                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                                            -- HYBRID_HASH_JOIN [$$215, $$217][$$209, $$210]  |PARTITIONED|
-                                                              -- HASH_PARTITION_EXCHANGE [$$215, $$217]  |PARTITIONED|
+                                                            -- HYBRID_HASH_JOIN [$$230, $$232][$$224, $$225]  |PARTITIONED|
+                                                              -- HASH_PARTITION_EXCHANGE [$$230, $$232]  |PARTITIONED|
                                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                                   -- ASSIGN  |PARTITIONED|
                                                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_01.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_01.plan
index 2348735..3584dca 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_01.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_01.plan
@@ -15,14 +15,14 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$22(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$23(ASC) ]  |PARTITIONED|
           -- STREAM_PROJECT  |PARTITIONED|
             -- STREAM_SELECT  |PARTITIONED|
               -- ASSIGN  |PARTITIONED|
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- BTREE_SEARCH  |PARTITIONED|
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      -- STABLE_SORT [$$29(ASC)]  |PARTITIONED|
+                      -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                           -- STREAM_PROJECT  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_01_ps.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_01_ps.plan
index 1b855f5..473c2d8 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_01_ps.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_01_ps.plan
@@ -16,8 +16,8 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-          -- STABLE_SORT [$$22(ASC)]  |PARTITIONED|
-            -- RANGE_PARTITION_EXCHANGE [$$22(ASC)]  |PARTITIONED|
+          -- STABLE_SORT [$$23(ASC)]  |PARTITIONED|
+            -- RANGE_PARTITION_EXCHANGE [$$23(ASC)]  |PARTITIONED|
               -- FORWARD  |PARTITIONED|
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- REPLICATE  |PARTITIONED|
@@ -28,7 +28,7 @@
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                               -- BTREE_SEARCH  |PARTITIONED|
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                  -- STABLE_SORT [$$29(ASC)]  |PARTITIONED|
+                                  -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                       -- STREAM_PROJECT  |PARTITIONED|
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -50,7 +50,7 @@
                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                         -- BTREE_SEARCH  |PARTITIONED|
                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                            -- STABLE_SORT [$$29(ASC)]  |PARTITIONED|
+                                            -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_02.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_02.plan
index 6bb0d82..fa82276 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_02.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_02.plan
@@ -15,14 +15,14 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$24(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$25(ASC) ]  |PARTITIONED|
           -- STREAM_SELECT  |PARTITIONED|
             -- STREAM_PROJECT  |PARTITIONED|
               -- ASSIGN  |PARTITIONED|
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- BTREE_SEARCH  |PARTITIONED|
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
+                      -- STABLE_SORT [$$31(ASC)]  |PARTITIONED|
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                           -- STREAM_PROJECT  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_02_ps.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_02_ps.plan
index 179b91c..26faa03 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_02_ps.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive-open_02_ps.plan
@@ -16,8 +16,8 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-          -- STABLE_SORT [$$24(ASC)]  |PARTITIONED|
-            -- RANGE_PARTITION_EXCHANGE [$$24(ASC)]  |PARTITIONED|
+          -- STABLE_SORT [$$25(ASC)]  |PARTITIONED|
+            -- RANGE_PARTITION_EXCHANGE [$$25(ASC)]  |PARTITIONED|
               -- FORWARD  |PARTITIONED|
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- REPLICATE  |PARTITIONED|
@@ -28,7 +28,7 @@
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                               -- BTREE_SEARCH  |PARTITIONED|
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                  -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
+                                  -- STABLE_SORT [$$31(ASC)]  |PARTITIONED|
                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                       -- STREAM_PROJECT  |PARTITIONED|
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -50,7 +50,7 @@
                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                         -- BTREE_SEARCH  |PARTITIONED|
                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                            -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
+                                            -- STABLE_SORT [$$31(ASC)]  |PARTITIONED|
                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_01.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_01.plan
index 2348735..3584dca 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_01.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_01.plan
@@ -15,14 +15,14 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$22(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$23(ASC) ]  |PARTITIONED|
           -- STREAM_PROJECT  |PARTITIONED|
             -- STREAM_SELECT  |PARTITIONED|
               -- ASSIGN  |PARTITIONED|
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- BTREE_SEARCH  |PARTITIONED|
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      -- STABLE_SORT [$$29(ASC)]  |PARTITIONED|
+                      -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                           -- STREAM_PROJECT  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_01_ps.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_01_ps.plan
index 1b855f5..473c2d8 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_01_ps.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_01_ps.plan
@@ -16,8 +16,8 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-          -- STABLE_SORT [$$22(ASC)]  |PARTITIONED|
-            -- RANGE_PARTITION_EXCHANGE [$$22(ASC)]  |PARTITIONED|
+          -- STABLE_SORT [$$23(ASC)]  |PARTITIONED|
+            -- RANGE_PARTITION_EXCHANGE [$$23(ASC)]  |PARTITIONED|
               -- FORWARD  |PARTITIONED|
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- REPLICATE  |PARTITIONED|
@@ -28,7 +28,7 @@
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                               -- BTREE_SEARCH  |PARTITIONED|
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                  -- STABLE_SORT [$$29(ASC)]  |PARTITIONED|
+                                  -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                       -- STREAM_PROJECT  |PARTITIONED|
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -50,7 +50,7 @@
                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                         -- BTREE_SEARCH  |PARTITIONED|
                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                            -- STABLE_SORT [$$29(ASC)]  |PARTITIONED|
+                                            -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_02.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_02.plan
index 6bb0d82..fa82276 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_02.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_02.plan
@@ -15,14 +15,14 @@
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
-        -- SORT_MERGE_EXCHANGE [$$24(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$25(ASC) ]  |PARTITIONED|
           -- STREAM_SELECT  |PARTITIONED|
             -- STREAM_PROJECT  |PARTITIONED|
               -- ASSIGN  |PARTITIONED|
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- BTREE_SEARCH  |PARTITIONED|
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
+                      -- STABLE_SORT [$$31(ASC)]  |PARTITIONED|
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                           -- STREAM_PROJECT  |PARTITIONED|
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_02_ps.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_02_ps.plan
index 179b91c..26faa03 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_02_ps.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/orders-index-search-conjunctive_02_ps.plan
@@ -16,8 +16,8 @@
     -- STREAM_PROJECT  |PARTITIONED|
       -- ASSIGN  |PARTITIONED|
         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-          -- STABLE_SORT [$$24(ASC)]  |PARTITIONED|
-            -- RANGE_PARTITION_EXCHANGE [$$24(ASC)]  |PARTITIONED|
+          -- STABLE_SORT [$$25(ASC)]  |PARTITIONED|
+            -- RANGE_PARTITION_EXCHANGE [$$25(ASC)]  |PARTITIONED|
               -- FORWARD  |PARTITIONED|
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- REPLICATE  |PARTITIONED|
@@ -28,7 +28,7 @@
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                               -- BTREE_SEARCH  |PARTITIONED|
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                  -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
+                                  -- STABLE_SORT [$$31(ASC)]  |PARTITIONED|
                                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                       -- STREAM_PROJECT  |PARTITIONED|
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
@@ -50,7 +50,7 @@
                                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                         -- BTREE_SEARCH  |PARTITIONED|
                                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                            -- STABLE_SORT [$$30(ASC)]  |PARTITIONED|
+                                            -- STABLE_SORT [$$31(ASC)]  |PARTITIONED|
                                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                                 -- STREAM_PROJECT  |PARTITIONED|
                                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/q01_pricing_summary_report_nt_ps.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/q01_pricing_summary_report_nt_ps.plan
index f774187..b99c3c6 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/q01_pricing_summary_report_nt_ps.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/q01_pricing_summary_report_nt_ps.plan
@@ -22,13 +22,13 @@
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                   -- REPLICATE  |PARTITIONED|
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      -- EXTERNAL_GROUP_BY[$$201, $$202]  |PARTITIONED|
+                      -- EXTERNAL_GROUP_BY[$$212, $$213]  |PARTITIONED|
                               {
                                 -- AGGREGATE  |LOCAL|
                                   -- NESTED_TUPLE_SOURCE  |LOCAL|
                               }
-                        -- HASH_PARTITION_EXCHANGE [$$201, $$202]  |PARTITIONED|
-                          -- EXTERNAL_GROUP_BY[$$170, $$171]  |PARTITIONED|
+                        -- HASH_PARTITION_EXCHANGE [$$212, $$213]  |PARTITIONED|
+                          -- EXTERNAL_GROUP_BY[$$181, $$182]  |PARTITIONED|
                                   {
                                     -- AGGREGATE  |LOCAL|
                                       -- NESTED_TUPLE_SOURCE  |LOCAL|
@@ -51,13 +51,13 @@
                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                             -- REPLICATE  |PARTITIONED|
                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                -- EXTERNAL_GROUP_BY[$$201, $$202]  |PARTITIONED|
+                                -- EXTERNAL_GROUP_BY[$$212, $$213]  |PARTITIONED|
                                         {
                                           -- AGGREGATE  |LOCAL|
                                             -- NESTED_TUPLE_SOURCE  |LOCAL|
                                         }
-                                  -- HASH_PARTITION_EXCHANGE [$$201, $$202]  |PARTITIONED|
-                                    -- EXTERNAL_GROUP_BY[$$170, $$171]  |PARTITIONED|
+                                  -- HASH_PARTITION_EXCHANGE [$$212, $$213]  |PARTITIONED|
+                                    -- EXTERNAL_GROUP_BY[$$181, $$182]  |PARTITIONED|
                                             {
                                               -- AGGREGATE  |LOCAL|
                                                 -- NESTED_TUPLE_SOURCE  |LOCAL|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query_issue849-2.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query_issue849-2.plan
index 496ae5a..05a9e35 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query_issue849-2.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query_issue849-2.plan
@@ -20,12 +20,12 @@
       -- ASSIGN  |PARTITIONED|
         -- STREAM_PROJECT  |PARTITIONED|
           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-            -- HYBRID_HASH_JOIN [$$41][$$43]  |PARTITIONED|
+            -- HYBRID_HASH_JOIN [$$44][$$46]  |PARTITIONED|
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                 -- DATASOURCE_SCAN  |PARTITIONED|
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                     -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
-              -- HASH_PARTITION_EXCHANGE [$$43]  |PARTITIONED|
+              -- HASH_PARTITION_EXCHANGE [$$46]  |PARTITIONED|
                 -- STREAM_PROJECT  |UNPARTITIONED|
                   -- ASSIGN  |UNPARTITIONED|
                     -- UNNEST  |UNPARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query_issue849.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query_issue849.plan
index 08fa6a0..2638f71 100644
--- a/asterixdb/asterix-app/src/test/resources/optimizerts/results/query_issue849.plan
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/query_issue849.plan
@@ -20,8 +20,8 @@
       -- ASSIGN  |PARTITIONED|
         -- STREAM_PROJECT  |PARTITIONED|
           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-            -- HYBRID_HASH_JOIN [$$42][$$41]  |PARTITIONED|
-              -- HASH_PARTITION_EXCHANGE [$$42]  |PARTITIONED|
+            -- HYBRID_HASH_JOIN [$$45][$$44]  |PARTITIONED|
+              -- HASH_PARTITION_EXCHANGE [$$45]  |PARTITIONED|
                 -- ASSIGN  |UNPARTITIONED|
                   -- UNNEST  |UNPARTITIONED|
                     -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.10.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.10.query.sqlpp
new file mode 100644
index 0000000..26a3c1f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.10.query.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select four, s1, s2
+from tenk
+group by rollup(four)
+let s1 = sum(two), s2 = sum(unique1)
+order by s1 desc, s2 desc;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.11.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.11.query.sqlpp
new file mode 100644
index 0000000..fe073cc
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.11.query.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select four, s1
+from tenk
+group by rollup(four)
+let s1 = sum(two), s2 = sum(unique1)
+order by s1 desc, s2 desc;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.12.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.12.query.sqlpp
new file mode 100644
index 0000000..abc2f29
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.12.query.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select four
+from tenk
+group by rollup(four)
+let s1 = sum(two), s2 = sum(unique1)
+order by s1 desc, s2 desc;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.13.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.13.query.sqlpp
new file mode 100644
index 0000000..faba3d2
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.13.query.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select value four
+from tenk
+group by rollup(four)
+let s1 = sum(two), s2 = sum(unique1)
+order by s1 desc, s2 desc;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.14.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.14.query.sqlpp
new file mode 100644
index 0000000..6539da30
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.14.query.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select distinct four, s1
+from tenk
+group by grouping sets((four), (four))
+let s1 = sum(two)
+order by s1 desc, four desc;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.15.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.15.query.sqlpp
new file mode 100644
index 0000000..fd90e93
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.15.query.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select distinct value s1
+from tenk
+group by grouping sets((four), (four))
+let s1 = sum(two)
+order by s1 desc;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.16.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.16.query.sqlpp
new file mode 100644
index 0000000..14bd768
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.16.query.sqlpp
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select four, sum(two) s
+from tenk
+group by rollup(four)
+order by four;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.17.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.17.query.sqlpp
new file mode 100644
index 0000000..e341ffd
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.17.query.sqlpp
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select value to_string(if_null(four, "null")) || "," || to_string(sum(two))
+from tenk
+group by rollup(four)
+order by four;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.18.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.18.query.sqlpp
new file mode 100644
index 0000000..b95e1d5
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.18.query.sqlpp
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select two, null as four, sum(unique1) s
+from tenk
+group by rollup(two)
+union all
+select null as two, four, sum(unique1) s
+from tenk
+group by rollup(four)
+
+order by s desc;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.4.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.4.query.sqlpp
index 6fcac65..ea7b81e 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.4.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.4.query.sqlpp
@@ -23,6 +23,6 @@
 use test;
 
 select v2, v4, grouping(v2, v4) as grp, sum(ten) as agg_sum
-  from tenk
-  group by rollup(two as v2, four as v4)
-  order by v2, v4;
+from tenk
+group by rollup(two as v2, four as v4)
+order by v2, v4;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.5.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.5.query.sqlpp
index 38a80f1..d894f88 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.5.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.5.query.sqlpp
@@ -24,6 +24,6 @@
 use test;
 
 select v2, v4, grouping(v2, v4) as grp, sum(ten) as agg_sum
-  from tenk
-  group by two as v2, rollup(two, four as v4)
-  order by v2, v4;
+from tenk
+group by two as v2, rollup(two, four as v4)
+order by v2, v4;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.6.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.6.query.sqlpp
new file mode 100644
index 0000000..45e085b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.6.query.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select two, four, s
+from tenk
+group by rollup(two, four)
+let s = sum(unique1)
+order by s desc;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.7.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.7.query.sqlpp
new file mode 100644
index 0000000..cc0f2bd
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.7.query.sqlpp
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select two, four
+from tenk
+group by rollup(two, four)
+let s = sum(unique1)
+order by s desc;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.8.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.8.query.sqlpp
new file mode 100644
index 0000000..d4ce070
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.8.query.sqlpp
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+select value
+  to_string(if_null(two, "null")) || ',' || to_string(if_null(four, "null"))
+from tenk
+group by rollup(two, four)
+let s = sum(unique1)
+order by s desc;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.9.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.9.query.sqlpp
new file mode 100644
index 0000000..9565c39
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-2/grouping-sets-2.9.query.sqlpp
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+set `compiler.groupmemory` "4MB";
+set `compiler.sortmemory` "4MB";
+
+use test;
+
+with tenk_subset as (
+  select two, unique1
+  from tenk
+  where unique2 % 2000 = 0
+),
+
+tenk_sum as (
+  select *
+  from tenk_subset t
+  group by rollup(two) group as g
+  let s = sum(unique1)
+  order by s desc
+  limit 3
+)
+
+select s, two, gg
+from tenk_sum tt
+let gg = ( select gi.* from tt.g as gi order by gi.t.unique1 )
+order by s desc;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-3-negative/grouping-sets-3-negative.9.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-3-negative/grouping-sets-3-negative.9.query.sqlpp
new file mode 100644
index 0000000..7d05dee
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/grouping-sets-3-negative/grouping-sets-3-negative.9.query.sqlpp
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/*
+ * ORDER BY after GROUPING SETS with SELECT v.*
+ */
+
+with hundred as (
+ select r % 2 as two, r %3 as three, r % 4 as four,
+  r % 5 as five, r % 6 as six, r % 7 as seven,
+  t % 8 as eight, r % 9 as nine, r % 10 as ten
+ from range(1, 100) r
+)
+
+select v.*
+from hundred
+group by rollup(two, four)
+let v = { two, four },
+    s = sum(ten)
+order by s
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/count_dataset/count_dataset.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/count_dataset/count_dataset.1.adm
index 486b421..9a0762a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/count_dataset/count_dataset.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/count_dataset/count_dataset.1.adm
@@ -1,24 +1,24 @@
-distribute result [$$24]
+distribute result [$$25]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
-    aggregate [$$24] <- [agg-sql-sum($$27)]
+    aggregate [$$25] <- [agg-sql-sum($$28)]
     -- AGGREGATE  |UNPARTITIONED|
-      aggregate [$$27] <- [agg-sql-count(1)]
+      aggregate [$$28] <- [agg-sql-count(1)]
       -- AGGREGATE  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$25(ASC) ]  |PARTITIONED|
-          project ([$$25])
+        -- SORT_MERGE_EXCHANGE [$$26(ASC) ]  |PARTITIONED|
+          project ([$$26])
           -- STREAM_PROJECT  |PARTITIONED|
-            select (and(ge($$23, 1), le($$23, 10)))
+            select (and(ge($$24, 1), le($$24, 10)))
             -- STREAM_SELECT  |PARTITIONED|
-              project ([$$25, $$23])
+              project ([$$26, $$24])
               -- STREAM_PROJECT  |PARTITIONED|
-                assign [$$23] <- [$$Tweet.getField(1)]
+                assign [$$24] <- [$$Tweet.getField(1)]
                 -- ASSIGN  |PARTITIONED|
                   exchange
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                    data-scan []<-[$$25, $$Tweet] <- Twitter.Tweet
+                    data-scan []<-[$$26, $$Tweet] <- Twitter.Tweet
                     -- DATASOURCE_SCAN  |PARTITIONED|
                       exchange
                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/count_dataset/count_dataset.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/count_dataset/count_dataset.1.adm
index 42728fe..f56f3a1 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/count_dataset/count_dataset.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/count_dataset/count_dataset.1.adm
@@ -1,24 +1,24 @@
-distribute result [$$24]
+distribute result [$$25]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
-    aggregate [$$24] <- [agg-sum($$27)]
+    aggregate [$$25] <- [agg-sum($$28)]
     -- AGGREGATE  |UNPARTITIONED|
-      aggregate [$$27] <- [agg-count(1)]
+      aggregate [$$28] <- [agg-count(1)]
       -- AGGREGATE  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$25(ASC) ]  |PARTITIONED|
-          project ([$$25])
+        -- SORT_MERGE_EXCHANGE [$$26(ASC) ]  |PARTITIONED|
+          project ([$$26])
           -- STREAM_PROJECT  |PARTITIONED|
-            select (and(ge($$23, 1), le($$23, 10)))
+            select (and(ge($$24, 1), le($$24, 10)))
             -- STREAM_SELECT  |PARTITIONED|
-              project ([$$25, $$23])
+              project ([$$26, $$24])
               -- STREAM_PROJECT  |PARTITIONED|
-                assign [$$23] <- [$$Tweet.getField(1)]
+                assign [$$24] <- [$$Tweet.getField(1)]
                 -- ASSIGN  |PARTITIONED|
                   exchange
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                    data-scan []<-[$$25, $$Tweet] <- Twitter.Tweet
+                    data-scan []<-[$$26, $$Tweet] <- Twitter.Tweet
                     -- DATASOURCE_SCAN  |PARTITIONED|
                       exchange
                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
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 f7790a0..31ef021 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,40 +1,40 @@
-distribute result [$$44]
+distribute result [$$47]
 -- DISTRIBUTE_RESULT  |PARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-    project ([$$44])
+    project ([$$47])
     -- STREAM_PROJECT  |PARTITIONED|
-      assign [$$44] <- [{"deptId": $#1, "star_cost": $$47}]
+      assign [$$47] <- [{"deptId": $#1, "star_cost": $$50}]
       -- ASSIGN  |PARTITIONED|
         exchange
         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-          group by ([$#1 := $$52]) decor ([]) {
-                    aggregate [$$47] <- [agg-global-sql-sum($$51)]
+          group by ([$#1 := $$55]) decor ([]) {
+                    aggregate [$$50] <- [agg-global-sql-sum($$54)]
                     -- AGGREGATE  |LOCAL|
                       nested tuple source
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                  }
-          -- SORT_GROUP_BY[$$52]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$55]  |PARTITIONED|
             exchange
-            -- HASH_PARTITION_EXCHANGE [$$52]  |PARTITIONED|
-              group by ([$$52 := $$45]) decor ([]) {
-                        aggregate [$$51] <- [agg-local-sql-sum($$42)]
+            -- HASH_PARTITION_EXCHANGE [$$55]  |PARTITIONED|
+              group by ([$$55 := $$48]) decor ([]) {
+                        aggregate [$$54] <- [agg-local-sql-sum($$45)]
                         -- AGGREGATE  |LOCAL|
                           nested tuple source
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
                      }
-              -- SORT_GROUP_BY[$$45]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$48]  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                  project ([$$42, $$45])
+                  project ([$$45, $$48])
                   -- STREAM_PROJECT  |PARTITIONED|
-                    assign [$$45, $$42] <- [substring($$e.getField("dept").getField("department_id"), 0), $$e.getField("salary")]
+                    assign [$$48, $$45] <- [substring($$e.getField("dept").getField("department_id"), 0), $$e.getField("salary")]
                     -- ASSIGN  |PARTITIONED|
                       project ([$$e])
                       -- STREAM_PROJECT  |PARTITIONED|
                         exchange
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                          data-scan []<-[$$46, $$e] <- gby.Employee
+                          data-scan []<-[$$49, $$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 7bfd96b..bbdbb57 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 [$$43]
+distribute result [$$46]
 -- DISTRIBUTE_RESULT  |PARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-    project ([$$43])
+    project ([$$46])
     -- STREAM_PROJECT  |PARTITIONED|
-      assign [$$43] <- [{"deptId": $#1, "star_cost": $$46}]
+      assign [$$46] <- [{"deptId": $#1, "star_cost": $$49}]
       -- ASSIGN  |PARTITIONED|
         exchange
         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-          group by ([$#1 := $$50]) decor ([]) {
-                    aggregate [$$46] <- [agg-global-sql-sum($$49)]
+          group by ([$#1 := $$53]) decor ([]) {
+                    aggregate [$$49] <- [agg-global-sql-sum($$52)]
                     -- AGGREGATE  |LOCAL|
                       nested tuple source
                       -- NESTED_TUPLE_SOURCE  |LOCAL|
                  }
-          -- SORT_GROUP_BY[$$50]  |PARTITIONED|
+          -- SORT_GROUP_BY[$$53]  |PARTITIONED|
             exchange
-            -- HASH_PARTITION_EXCHANGE [$$50]  |PARTITIONED|
-              group by ([$$50 := $$44]) decor ([]) {
-                        aggregate [$$49] <- [agg-local-sql-sum($$41)]
+            -- HASH_PARTITION_EXCHANGE [$$53]  |PARTITIONED|
+              group by ([$$53 := $$47]) decor ([]) {
+                        aggregate [$$52] <- [agg-local-sql-sum($$44)]
                         -- AGGREGATE  |LOCAL|
                           nested tuple source
                           -- NESTED_TUPLE_SOURCE  |LOCAL|
                      }
-              -- SORT_GROUP_BY[$$44]  |PARTITIONED|
+              -- SORT_GROUP_BY[$$47]  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                  project ([$$41, $$44])
+                  project ([$$44, $$47])
                   -- STREAM_PROJECT  |PARTITIONED|
-                    assign [$$44, $$41] <- [substring($$e.getField(1), 0), $$e.getField(2)]
+                    assign [$$47, $$44] <- [substring($$e.getField(1), 0), $$e.getField(2)]
                     -- ASSIGN  |PARTITIONED|
                       project ([$$e])
                       -- STREAM_PROJECT  |PARTITIONED|
                         exchange
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                          data-scan []<-[$$45, $$e] <- gby.Employee
+                          data-scan []<-[$$48, $$e] <- gby.Employee
                           -- DATASOURCE_SCAN  |PARTITIONED|
                             exchange
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.10.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.10.adm
new file mode 100644
index 0000000..c1805e8
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.10.adm
@@ -0,0 +1,5 @@
+{ "four": null, "s1": 5000, "s2": 49995000 }
+{ "four": 3, "s1": 2500, "s2": 12502500 }
+{ "four": 1, "s1": 2500, "s2": 12497500 }
+{ "four": 2, "s1": 0, "s2": 12500000 }
+{ "four": 0, "s1": 0, "s2": 12495000 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.11.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.11.adm
new file mode 100644
index 0000000..884812f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.11.adm
@@ -0,0 +1,5 @@
+{ "four": null, "s1": 5000 }
+{ "four": 3, "s1": 2500 }
+{ "four": 1, "s1": 2500 }
+{ "four": 2, "s1": 0 }
+{ "four": 0, "s1": 0 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.12.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.12.adm
new file mode 100644
index 0000000..c3606f4
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.12.adm
@@ -0,0 +1,5 @@
+{ "four": null }
+{ "four": 3 }
+{ "four": 1 }
+{ "four": 2 }
+{ "four": 0 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.13.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.13.adm
new file mode 100644
index 0000000..133210b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.13.adm
@@ -0,0 +1,5 @@
+null
+3
+1
+2
+0
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.14.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.14.adm
new file mode 100644
index 0000000..97f939a
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.14.adm
@@ -0,0 +1,4 @@
+{ "four": 3, "s1": 2500 }
+{ "four": 1, "s1": 2500 }
+{ "four": 2, "s1": 0 }
+{ "four": 0, "s1": 0 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.15.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.15.adm
new file mode 100644
index 0000000..443edd5
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.15.adm
@@ -0,0 +1,2 @@
+2500
+0
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.16.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.16.adm
new file mode 100644
index 0000000..7721059
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.16.adm
@@ -0,0 +1,5 @@
+{ "four": null, "s": 5000 }
+{ "four": 0, "s": 0 }
+{ "four": 1, "s": 2500 }
+{ "four": 2, "s": 0 }
+{ "four": 3, "s": 2500 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.17.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.17.adm
new file mode 100644
index 0000000..ca4480e
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.17.adm
@@ -0,0 +1,5 @@
+"null,5000"
+"0,0"
+"1,2500"
+"2,0"
+"3,2500"
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.18.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.18.adm
new file mode 100644
index 0000000..eecbe7f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.18.adm
@@ -0,0 +1,8 @@
+{ "two": null, "four": null, "s": 49995000 }
+{ "two": null, "four": null, "s": 49995000 }
+{ "two": 1, "four": null, "s": 25000000 }
+{ "two": 0, "four": null, "s": 24995000 }
+{ "two": null, "four": 3, "s": 12502500 }
+{ "two": null, "four": 2, "s": 12500000 }
+{ "two": null, "four": 1, "s": 12497500 }
+{ "two": null, "four": 0, "s": 12495000 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.6.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.6.adm
new file mode 100644
index 0000000..b5229c0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.6.adm
@@ -0,0 +1,7 @@
+{ "two": null, "four": null, "s": 49995000 }
+{ "two": 1, "four": null, "s": 25000000 }
+{ "two": 0, "four": null, "s": 24995000 }
+{ "two": 1, "four": 3, "s": 12502500 }
+{ "two": 0, "four": 2, "s": 12500000 }
+{ "two": 1, "four": 1, "s": 12497500 }
+{ "two": 0, "four": 0, "s": 12495000 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.7.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.7.adm
new file mode 100644
index 0000000..c9af23f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.7.adm
@@ -0,0 +1,7 @@
+{ "two": null, "four": null }
+{ "two": 1, "four": null }
+{ "two": 0, "four": null }
+{ "two": 1, "four": 3 }
+{ "two": 0, "four": 2 }
+{ "two": 1, "four": 1 }
+{ "two": 0, "four": 0 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.8.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.8.adm
new file mode 100644
index 0000000..bbff6b9
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.8.adm
@@ -0,0 +1,7 @@
+"null,null"
+"1,null"
+"0,null"
+"1,3"
+"0,2"
+"1,1"
+"0,0"
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.9.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.9.adm
new file mode 100644
index 0000000..8eea2ad
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/grouping-sets-2/grouping-sets-2.9.adm
@@ -0,0 +1,3 @@
+{ "s": 32630, "two": null, "gg": [ { "t": { "two": 0, "unique1": 1858 } }, { "t": { "two": 0, "unique1": 3362 } }, { "t": { "two": 0, "unique1": 8800 } }, { "t": { "two": 1, "unique1": 9267 } }, { "t": { "two": 1, "unique1": 9343 } } ] }
+{ "s": 18610, "two": 1, "gg": [ { "t": { "two": 1, "unique1": 9267 } }, { "t": { "two": 1, "unique1": 9343 } } ] }
+{ "s": 14020, "two": 0, "gg": [ { "t": { "two": 0, "unique1": 1858 } }, { "t": { "two": 0, "unique1": 3362 } }, { "t": { "two": 0, "unique1": 8800 } } ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup-select/push-limit-to-primary-lookup-select.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup-select/push-limit-to-primary-lookup-select.3.adm
index 1f1e3a0..21618d0 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup-select/push-limit-to-primary-lookup-select.3.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup-select/push-limit-to-primary-lookup-select.3.adm
@@ -7,28 +7,28 @@
       project ([$$c])
       -- STREAM_PROJECT  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$16(ASC), $$17(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$17(ASC), $$18(ASC) ]  |PARTITIONED|
           limit 10
           -- STREAM_LIMIT  |PARTITIONED|
             exchange
             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-              unnest-map [$$16, $$17, $$c] <- index-search("LineItem", 0, "test", "LineItem", FALSE, FALSE, 2, $$22, $$23, 2, $$22, $$23, TRUE, TRUE, TRUE) condition (and(lt($$c.getField(2), 150), lt($$c.getField(5), 10000))) limit 10
+              unnest-map [$$17, $$18, $$c] <- index-search("LineItem", 0, "test", "LineItem", FALSE, FALSE, 2, $$23, $$24, 2, $$23, $$24, TRUE, TRUE, TRUE) condition (and(lt($$c.getField(2), 150), lt($$c.getField(5), 10000))) limit 10
               -- BTREE_SEARCH  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                  order (ASC, $$22) (ASC, $$23)
-                  -- STABLE_SORT [$$22(ASC), $$23(ASC)]  |PARTITIONED|
+                  order (ASC, $$23) (ASC, $$24)
+                  -- STABLE_SORT [$$23(ASC), $$24(ASC)]  |PARTITIONED|
                     exchange
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      project ([$$22, $$23])
+                      project ([$$23, $$24])
                       -- STREAM_PROJECT  |PARTITIONED|
                         exchange
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                          unnest-map [$$21, $$22, $$23] <- index-search("idx_LineItem_suppkey", 0, "test", "LineItem", FALSE, FALSE, 0, 1, $$20, TRUE, FALSE, FALSE)
+                          unnest-map [$$22, $$23, $$24] <- index-search("idx_LineItem_suppkey", 0, "test", "LineItem", FALSE, FALSE, 0, 1, $$21, TRUE, FALSE, FALSE)
                           -- BTREE_SEARCH  |PARTITIONED|
                             exchange
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                              assign [$$20] <- [150]
+                              assign [$$21] <- [150]
                               -- ASSIGN  |PARTITIONED|
                                 empty-tuple-source
                                 -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup-select/push-limit-to-primary-lookup-select.5.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup-select/push-limit-to-primary-lookup-select.5.adm
index a0e070b..1f4ef66 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup-select/push-limit-to-primary-lookup-select.5.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup-select/push-limit-to-primary-lookup-select.5.adm
@@ -1,40 +1,40 @@
-distribute result [$$18]
+distribute result [$$19]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
     limit 5
     -- STREAM_LIMIT  |UNPARTITIONED|
-      project ([$$18])
+      project ([$$19])
       -- STREAM_PROJECT  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$20(ASC), $$21(ASC) ]  |PARTITIONED|
-          project ([$$20, $$21, $$18])
+        -- SORT_MERGE_EXCHANGE [$$21(ASC), $$22(ASC) ]  |PARTITIONED|
+          project ([$$21, $$22, $$19])
           -- STREAM_PROJECT  |PARTITIONED|
-            assign [$$18] <- [{"shipdate": substring($$c.getField(10), 0, 4), "suppkey": gt($$19, 0)}]
+            assign [$$19] <- [{"shipdate": substring($$c.getField(10), 0, 4), "suppkey": gt($$20, 0)}]
             -- ASSIGN  |PARTITIONED|
               limit 5
               -- STREAM_LIMIT  |PARTITIONED|
-                assign [$$19] <- [$$c.getField(2)]
+                assign [$$20] <- [$$c.getField(2)]
                 -- ASSIGN  |PARTITIONED|
                   exchange
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                    unnest-map [$$20, $$21, $$c] <- index-search("LineItem", 0, "test", "LineItem", FALSE, FALSE, 2, $$25, $$26, 2, $$25, $$26, TRUE, TRUE, TRUE) condition (lt($$c.getField(2), 150)) limit 5
+                    unnest-map [$$21, $$22, $$c] <- index-search("LineItem", 0, "test", "LineItem", FALSE, FALSE, 2, $$26, $$27, 2, $$26, $$27, TRUE, TRUE, TRUE) condition (lt($$c.getField(2), 150)) limit 5
                     -- BTREE_SEARCH  |PARTITIONED|
                       exchange
                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                        order (ASC, $$25) (ASC, $$26)
-                        -- STABLE_SORT [$$25(ASC), $$26(ASC)]  |PARTITIONED|
+                        order (ASC, $$26) (ASC, $$27)
+                        -- STABLE_SORT [$$26(ASC), $$27(ASC)]  |PARTITIONED|
                           exchange
                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                            project ([$$25, $$26])
+                            project ([$$26, $$27])
                             -- STREAM_PROJECT  |PARTITIONED|
                               exchange
                               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                unnest-map [$$24, $$25, $$26] <- index-search("idx_LineItem_suppkey", 0, "test", "LineItem", FALSE, FALSE, 0, 1, $$23, TRUE, FALSE, FALSE)
+                                unnest-map [$$25, $$26, $$27] <- index-search("idx_LineItem_suppkey", 0, "test", "LineItem", FALSE, FALSE, 0, 1, $$24, TRUE, FALSE, FALSE)
                                 -- BTREE_SEARCH  |PARTITIONED|
                                   exchange
                                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                    assign [$$23] <- [150]
+                                    assign [$$24] <- [150]
                                     -- ASSIGN  |PARTITIONED|
                                       empty-tuple-source
                                       -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup/push-limit-to-primary-lookup.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup/push-limit-to-primary-lookup.3.adm
index 4aba1a7..d070b2b 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup/push-limit-to-primary-lookup.3.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup/push-limit-to-primary-lookup.3.adm
@@ -7,28 +7,28 @@
       project ([$$c])
       -- STREAM_PROJECT  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$13(ASC), $$14(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$14(ASC), $$15(ASC) ]  |PARTITIONED|
           limit 10
           -- STREAM_LIMIT  |PARTITIONED|
             exchange
             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-              unnest-map [$$13, $$14, $$c] <- index-search("LineItem", 0, "test", "LineItem", FALSE, FALSE, 2, $$18, $$19, 2, $$18, $$19, TRUE, TRUE, TRUE) condition (lt($$c.getField(2), 150)) limit 10
+              unnest-map [$$14, $$15, $$c] <- index-search("LineItem", 0, "test", "LineItem", FALSE, FALSE, 2, $$19, $$20, 2, $$19, $$20, TRUE, TRUE, TRUE) condition (lt($$c.getField(2), 150)) limit 10
               -- BTREE_SEARCH  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                  order (ASC, $$18) (ASC, $$19)
-                  -- STABLE_SORT [$$18(ASC), $$19(ASC)]  |PARTITIONED|
+                  order (ASC, $$19) (ASC, $$20)
+                  -- STABLE_SORT [$$19(ASC), $$20(ASC)]  |PARTITIONED|
                     exchange
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      project ([$$18, $$19])
+                      project ([$$19, $$20])
                       -- STREAM_PROJECT  |PARTITIONED|
                         exchange
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                          unnest-map [$$17, $$18, $$19] <- index-search("idx_LineItem_suppkey", 0, "test", "LineItem", FALSE, FALSE, 0, 1, $$16, TRUE, FALSE, FALSE)
+                          unnest-map [$$18, $$19, $$20] <- index-search("idx_LineItem_suppkey", 0, "test", "LineItem", FALSE, FALSE, 0, 1, $$17, TRUE, FALSE, FALSE)
                           -- BTREE_SEARCH  |PARTITIONED|
                             exchange
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                              assign [$$16] <- [150]
+                              assign [$$17] <- [150]
                               -- ASSIGN  |PARTITIONED|
                                 empty-tuple-source
                                 -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup/push-limit-to-primary-lookup.5.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup/push-limit-to-primary-lookup.5.adm
index f611c2b..1e25eea 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup/push-limit-to-primary-lookup.5.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-lookup/push-limit-to-primary-lookup.5.adm
@@ -7,28 +7,28 @@
       project ([$$c])
       -- STREAM_PROJECT  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$15(ASC), $$16(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$16(ASC), $$17(ASC) ]  |PARTITIONED|
           limit 10
           -- STREAM_LIMIT  |PARTITIONED|
             exchange
             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-              unnest-map [$$15, $$16, $$c] <- index-search("LineItem", 0, "test", "LineItem", FALSE, FALSE, 2, $$20, $$21, 2, $$20, $$21, TRUE, TRUE, TRUE) condition (lt($$c.getField(2), 150)) limit 10
+              unnest-map [$$16, $$17, $$c] <- index-search("LineItem", 0, "test", "LineItem", FALSE, FALSE, 2, $$21, $$22, 2, $$21, $$22, TRUE, TRUE, TRUE) condition (lt($$c.getField(2), 150)) limit 10
               -- BTREE_SEARCH  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                  order (ASC, $$20) (ASC, $$21)
-                  -- STABLE_SORT [$$20(ASC), $$21(ASC)]  |PARTITIONED|
+                  order (ASC, $$21) (ASC, $$22)
+                  -- STABLE_SORT [$$21(ASC), $$22(ASC)]  |PARTITIONED|
                     exchange
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      project ([$$20, $$21])
+                      project ([$$21, $$22])
                       -- STREAM_PROJECT  |PARTITIONED|
                         exchange
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                          unnest-map [$$19, $$20, $$21] <- index-search("idx_LineItem_suppkey", 0, "test", "LineItem", FALSE, FALSE, 0, 1, $$18, TRUE, FALSE, FALSE)
+                          unnest-map [$$20, $$21, $$22] <- index-search("idx_LineItem_suppkey", 0, "test", "LineItem", FALSE, FALSE, 0, 1, $$19, TRUE, FALSE, FALSE)
                           -- BTREE_SEARCH  |PARTITIONED|
                             exchange
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                              assign [$$18] <- [150]
+                              assign [$$19] <- [150]
                               -- ASSIGN  |PARTITIONED|
                                 empty-tuple-source
                                 -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.3.adm
index 82e42e8..a1f79bb 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.3.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.3.adm
@@ -7,12 +7,12 @@
       project ([$$paper])
       -- STREAM_PROJECT  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$13(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$14(ASC) ]  |PARTITIONED|
           limit 10
           -- STREAM_LIMIT  |PARTITIONED|
             exchange
             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-              data-scan []<-[$$13, $$paper] <- test.DBLP1 condition (contains($$paper.getField(1), "kimL89")) limit 10
+              data-scan []<-[$$14, $$paper] <- test.DBLP1 condition (contains($$paper.getField(1), "kimL89")) limit 10
               -- DATASOURCE_SCAN  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.5.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.5.adm
index 9fd4ea8..957c52b 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.5.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.5.adm
@@ -1,4 +1,4 @@
-distribute result [$$33]
+distribute result [$$35]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
@@ -6,57 +6,57 @@
     -- STREAM_LIMIT  |UNPARTITIONED|
       exchange
       -- RANDOM_MERGE_EXCHANGE  |PARTITIONED|
-        project ([$$33])
+        project ([$$35])
         -- STREAM_PROJECT  |PARTITIONED|
-          assign [$$33] <- [{"dblpid": $$34}]
+          assign [$$35] <- [{"dblpid": $$36}]
           -- ASSIGN  |PARTITIONED|
             limit 2
             -- STREAM_LIMIT  |PARTITIONED|
-              project ([$$34])
+              project ([$$36])
               -- STREAM_PROJECT  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                  join (eq($$34, $$37))
-                  -- HYBRID_HASH_JOIN [$$34][$$37]  |PARTITIONED|
+                  join (eq($$36, $$39))
+                  -- HYBRID_HASH_JOIN [$$36][$$39]  |PARTITIONED|
                     exchange
-                    -- HASH_PARTITION_EXCHANGE [$$34]  |PARTITIONED|
-                      project ([$$34])
+                    -- HASH_PARTITION_EXCHANGE [$$36]  |PARTITIONED|
+                      project ([$$36])
                       -- STREAM_PROJECT  |PARTITIONED|
-                        assign [$$34] <- [$$d.getField(1)]
+                        assign [$$36] <- [$$d.getField(1)]
                         -- ASSIGN  |PARTITIONED|
                           project ([$$d])
                           -- STREAM_PROJECT  |PARTITIONED|
                             exchange
                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                              data-scan []<-[$$35, $$d] <- test.DBLP1
+                              data-scan []<-[$$37, $$d] <- test.DBLP1
                               -- DATASOURCE_SCAN  |PARTITIONED|
                                 exchange
                                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
                                   empty-tuple-source
                                   -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
                     exchange
-                    -- HASH_PARTITION_EXCHANGE [$$37]  |PARTITIONED|
-                      project ([$$37])
+                    -- HASH_PARTITION_EXCHANGE [$$39]  |PARTITIONED|
+                      project ([$$39])
                       -- STREAM_PROJECT  |UNPARTITIONED|
-                        assign [$$37] <- [get-item($$26, 0).getField(0).getField(1)]
+                        assign [$$39] <- [get-item($$28, 0).getField(0).getField(1)]
                         -- ASSIGN  |UNPARTITIONED|
-                          aggregate [$$26] <- [listify($$25)]
+                          aggregate [$$28] <- [listify($$27)]
                           -- AGGREGATE  |UNPARTITIONED|
                             limit 1
                             -- STREAM_LIMIT  |UNPARTITIONED|
-                              project ([$$25])
+                              project ([$$27])
                               -- STREAM_PROJECT  |PARTITIONED|
                                 exchange
-                                -- SORT_MERGE_EXCHANGE [$$36(ASC) ]  |PARTITIONED|
-                                  project ([$$36, $$25])
+                                -- SORT_MERGE_EXCHANGE [$$38(ASC) ]  |PARTITIONED|
+                                  project ([$$38, $$27])
                                   -- STREAM_PROJECT  |PARTITIONED|
-                                    assign [$$25] <- [{"d": $$d}]
+                                    assign [$$27] <- [{"d": $$d}]
                                     -- ASSIGN  |PARTITIONED|
                                       limit 1
                                       -- STREAM_LIMIT  |PARTITIONED|
                                         exchange
                                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                                          data-scan []<-[$$36, $$d] <- test.DBLP1 condition (ends-with($$d.getField(1), "Blakeley95")) limit 1
+                                          data-scan []<-[$$38, $$d] <- test.DBLP1 condition (ends-with($$d.getField(1), "Blakeley95")) limit 1
                                           -- DATASOURCE_SCAN  |PARTITIONED|
                                             exchange
                                             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.6.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.6.adm
index 8ffb399..7509819 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.6.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.6.adm
@@ -1,26 +1,26 @@
-distribute result [$$17]
+distribute result [$$18]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
     limit 1
     -- STREAM_LIMIT  |UNPARTITIONED|
-      project ([$$17])
+      project ([$$18])
       -- STREAM_PROJECT  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$19(ASC) ]  |PARTITIONED|
-          project ([$$19, $$17])
+        -- SORT_MERGE_EXCHANGE [$$20(ASC) ]  |PARTITIONED|
+          project ([$$20, $$18])
           -- STREAM_PROJECT  |PARTITIONED|
-            assign [$$17] <- [{"$1": substring($$18, 0, 21)}]
+            assign [$$18] <- [{"$1": substring($$19, 0, 21)}]
             -- ASSIGN  |PARTITIONED|
               limit 1
               -- STREAM_LIMIT  |PARTITIONED|
-                project ([$$19, $$18])
+                project ([$$20, $$19])
                 -- STREAM_PROJECT  |PARTITIONED|
-                  assign [$$18] <- [$$DBLP1.getField(1)]
+                  assign [$$19] <- [$$DBLP1.getField(1)]
                   -- ASSIGN  |PARTITIONED|
                     exchange
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      data-scan []<-[$$19, $$DBLP1] <- test.DBLP1 condition (gt($$DBLP1.getField(1), "series")) limit 1
+                      data-scan []<-[$$20, $$DBLP1] <- test.DBLP1 condition (gt($$DBLP1.getField(1), "series")) limit 1
                       -- DATASOURCE_SCAN  |PARTITIONED|
                         exchange
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.8.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.8.adm
index bc86e3a..5b408ad 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.8.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan-select/push-limit-to-primary-scan-select.8.adm
@@ -1,26 +1,26 @@
-distribute result [$$20]
+distribute result [$$21]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
     limit 2
     -- STREAM_LIMIT  |UNPARTITIONED|
-      project ([$$20])
+      project ([$$21])
       -- STREAM_PROJECT  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$23(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$24(ASC) ]  |PARTITIONED|
           limit 2
           -- STREAM_LIMIT  |PARTITIONED|
-            project ([$$23, $$20])
+            project ([$$24, $$21])
             -- STREAM_PROJECT  |PARTITIONED|
-              assign [$$20] <- [$$24.getField("lang")]
+              assign [$$21] <- [$$25.getField("lang")]
               -- ASSIGN  |PARTITIONED|
-                project ([$$23, $$24])
+                project ([$$24, $$25])
                 -- STREAM_PROJECT  |PARTITIONED|
-                  assign [$$24] <- [$$t.getField("user")]
+                  assign [$$25] <- [$$t.getField("user")]
                   -- ASSIGN  |PARTITIONED|
                     exchange
                     -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                      data-scan []<-[$$23, $$t] <- test.TweetMessages condition (and(ge($$t.getField("user").getField("friends_count"), 0), le($$t.getField("user").getField("friends_count"), 150))) limit 2
+                      data-scan []<-[$$24, $$t] <- test.TweetMessages condition (and(ge($$t.getField("user").getField("friends_count"), 0), le($$t.getField("user").getField("friends_count"), 150))) limit 2
                       -- DATASOURCE_SCAN  |PARTITIONED|
                         exchange
                         -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan/push-limit-to-primary-scan.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan/push-limit-to-primary-scan.3.adm
index a810489..ee3e565 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan/push-limit-to-primary-scan.3.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan/push-limit-to-primary-scan.3.adm
@@ -7,12 +7,12 @@
       project ([$$paper])
       -- STREAM_PROJECT  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$11(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$12(ASC) ]  |PARTITIONED|
           limit 10
           -- STREAM_LIMIT  |PARTITIONED|
             exchange
             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-              data-scan []<-[$$11, $$paper] <- test.DBLP1 limit 10
+              data-scan []<-[$$12, $$paper] <- test.DBLP1 limit 10
               -- DATASOURCE_SCAN  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan/push-limit-to-primary-scan.5.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan/push-limit-to-primary-scan.5.adm
index 41880a5..939637d 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan/push-limit-to-primary-scan.5.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/limit/push-limit-to-primary-scan/push-limit-to-primary-scan.5.adm
@@ -7,12 +7,12 @@
       project ([$$paper])
       -- STREAM_PROJECT  |PARTITIONED|
         exchange
-        -- SORT_MERGE_EXCHANGE [$$13(ASC) ]  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$14(ASC) ]  |PARTITIONED|
           limit 10
           -- STREAM_LIMIT  |PARTITIONED|
             exchange
             -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-              data-scan []<-[$$13, $$paper] <- test.DBLP1 limit 10
+              data-scan []<-[$$14, $$paper] <- test.DBLP1 limit 10
               -- DATASOURCE_SCAN  |PARTITIONED|
                 exchange
                 -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/list/enforcing_item_type/enforcing_item_type.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/list/enforcing_item_type/enforcing_item_type.1.adm
index 81de56f..f0da628 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/list/enforcing_item_type/enforcing_item_type.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/list/enforcing_item_type/enforcing_item_type.1.adm
@@ -1,10 +1,10 @@
-distribute result [$$19]
+distribute result [$$20]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
-    project ([$$19])
+    project ([$$20])
     -- STREAM_PROJECT  |UNPARTITIONED|
-      assign [$$19] <- [{"id": get-item($$t, 0), "v": get-item($$t, 1)}]
+      assign [$$20] <- [{"id": get-item($$t, 0), "v": get-item($$t, 1)}]
       -- ASSIGN  |UNPARTITIONED|
         unnest $$t <- scan-collection(ordered-list-constructor(ordered-list-constructor(29, cast({ f1: "a", f2: 3 }))))
         -- UNNEST  |UNPARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.3.adm
index 5b24cb2..aafd2d0 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.3.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.3.adm
@@ -1,8 +1,8 @@
-distribute result [$$13]
+distribute result [$$14]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
-    assign [$$13] <- [TRUE]
+    assign [$$14] <- [TRUE]
     -- ASSIGN  |UNPARTITIONED|
       project ([])
       -- STREAM_PROJECT  |UNPARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.5.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.5.adm
index d3f67c2..7209f96 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.5.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.5.adm
@@ -1,10 +1,10 @@
-distribute result [$$13]
+distribute result [$$14]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
-    project ([$$13])
+    project ([$$14])
     -- STREAM_PROJECT  |UNPARTITIONED|
-      assign [$$13] <- [le($$x, 2)]
+      assign [$$14] <- [le($$x, 2)]
       -- ASSIGN  |UNPARTITIONED|
         exchange
         -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.6.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.6.adm
index 77dc263..351c8e5 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.6.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.6.adm
@@ -1,10 +1,10 @@
-distribute result [$$13]
+distribute result [$$14]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
-    project ([$$13])
+    project ([$$14])
     -- STREAM_PROJECT  |UNPARTITIONED|
-      assign [$$13] <- [or(null, le($$x, 2))]
+      assign [$$14] <- [or(null, le($$x, 2))]
       -- ASSIGN  |UNPARTITIONED|
         exchange
         -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.7.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.7.adm
index 0fe9557..d2f3187 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.7.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/constant_folding/constant_folding.7.adm
@@ -1,10 +1,10 @@
-distribute result [$$15]
+distribute result [$$16]
 -- DISTRIBUTE_RESULT  |UNPARTITIONED|
   exchange
   -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
-    project ([$$15])
+    project ([$$16])
     -- STREAM_PROJECT  |UNPARTITIONED|
-      assign [$$15] <- [or(TRUE, lt(get-year(current-date()), $$x))]
+      assign [$$16] <- [or(TRUE, lt(get-year(current-date()), $$x))]
       -- ASSIGN  |UNPARTITIONED|
         exchange
         -- ONE_TO_ONE_EXCHANGE  |UNPARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/rebalance/single_dataset_with_index/single_dataset_with_index.13.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/rebalance/single_dataset_with_index/single_dataset_with_index.13.adm
index 73492d27..e14f391 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/rebalance/single_dataset_with_index/single_dataset_with_index.13.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/rebalance/single_dataset_with_index/single_dataset_with_index.13.adm
@@ -5,28 +5,28 @@
     project ([$$l])
     -- STREAM_PROJECT  |PARTITIONED|
       exchange
-      -- SORT_MERGE_EXCHANGE [$$15(ASC), $$16(ASC) ]  |PARTITIONED|
+      -- SORT_MERGE_EXCHANGE [$$16(ASC), $$17(ASC) ]  |PARTITIONED|
         select (eq($$l.getField(10), "1994-01-20"))
         -- STREAM_SELECT  |PARTITIONED|
           exchange
           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-            unnest-map [$$15, $$16, $$l] <- index-search("LineItem", 0, "tpch", "LineItem", FALSE, FALSE, 2, $$23, $$24, 2, $$23, $$24, TRUE, TRUE, TRUE)
+            unnest-map [$$16, $$17, $$l] <- index-search("LineItem", 0, "tpch", "LineItem", FALSE, FALSE, 2, $$24, $$25, 2, $$24, $$25, TRUE, TRUE, TRUE)
             -- BTREE_SEARCH  |PARTITIONED|
               exchange
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                order (ASC, $$23) (ASC, $$24) 
-                -- STABLE_SORT [$$23(ASC), $$24(ASC)]  |PARTITIONED|
+                order (ASC, $$24) (ASC, $$25)
+                -- STABLE_SORT [$$24(ASC), $$25(ASC)]  |PARTITIONED|
                   exchange
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                    project ([$$23, $$24])
+                    project ([$$24, $$25])
                     -- STREAM_PROJECT  |PARTITIONED|
                       exchange
                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                        unnest-map [$$22, $$23, $$24] <- index-search("lineitem_shipdateIx", 0, "tpch", "LineItem", FALSE, FALSE, 1, $$20, 1, $$21, TRUE, TRUE, TRUE)
+                        unnest-map [$$23, $$24, $$25] <- index-search("lineitem_shipdateIx", 0, "tpch", "LineItem", FALSE, FALSE, 1, $$21, 1, $$22, TRUE, TRUE, TRUE)
                         -- BTREE_SEARCH  |PARTITIONED|
                           exchange
                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                            assign [$$20, $$21] <- ["1994-01-20", "1994-01-20"]
+                            assign [$$21, $$22] <- ["1994-01-20", "1994-01-20"]
                             -- ASSIGN  |PARTITIONED|
                               empty-tuple-source
                               -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/rebalance/single_dataset_with_index/single_dataset_with_index.8.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/rebalance/single_dataset_with_index/single_dataset_with_index.8.adm
index 73492d27..e14f391 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/rebalance/single_dataset_with_index/single_dataset_with_index.8.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/rebalance/single_dataset_with_index/single_dataset_with_index.8.adm
@@ -5,28 +5,28 @@
     project ([$$l])
     -- STREAM_PROJECT  |PARTITIONED|
       exchange
-      -- SORT_MERGE_EXCHANGE [$$15(ASC), $$16(ASC) ]  |PARTITIONED|
+      -- SORT_MERGE_EXCHANGE [$$16(ASC), $$17(ASC) ]  |PARTITIONED|
         select (eq($$l.getField(10), "1994-01-20"))
         -- STREAM_SELECT  |PARTITIONED|
           exchange
           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-            unnest-map [$$15, $$16, $$l] <- index-search("LineItem", 0, "tpch", "LineItem", FALSE, FALSE, 2, $$23, $$24, 2, $$23, $$24, TRUE, TRUE, TRUE)
+            unnest-map [$$16, $$17, $$l] <- index-search("LineItem", 0, "tpch", "LineItem", FALSE, FALSE, 2, $$24, $$25, 2, $$24, $$25, TRUE, TRUE, TRUE)
             -- BTREE_SEARCH  |PARTITIONED|
               exchange
               -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                order (ASC, $$23) (ASC, $$24) 
-                -- STABLE_SORT [$$23(ASC), $$24(ASC)]  |PARTITIONED|
+                order (ASC, $$24) (ASC, $$25)
+                -- STABLE_SORT [$$24(ASC), $$25(ASC)]  |PARTITIONED|
                   exchange
                   -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                    project ([$$23, $$24])
+                    project ([$$24, $$25])
                     -- STREAM_PROJECT  |PARTITIONED|
                       exchange
                       -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                        unnest-map [$$22, $$23, $$24] <- index-search("lineitem_shipdateIx", 0, "tpch", "LineItem", FALSE, FALSE, 1, $$20, 1, $$21, TRUE, TRUE, TRUE)
+                        unnest-map [$$23, $$24, $$25] <- index-search("lineitem_shipdateIx", 0, "tpch", "LineItem", FALSE, FALSE, 1, $$21, 1, $$22, TRUE, TRUE, TRUE)
                         -- BTREE_SEARCH  |PARTITIONED|
                           exchange
                           -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
-                            assign [$$20, $$21] <- ["1994-01-20", "1994-01-20"]
+                            assign [$$21, $$22] <- ["1994-01-20", "1994-01-20"]
                             -- ASSIGN  |PARTITIONED|
                               empty-tuple-source
                               -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.10.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.10.ast
new file mode 100644
index 0000000..d992e77
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.10.ast
@@ -0,0 +1,144 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+Variable [ Name=#2 ]
+]
+FROM [  (
+    SELECT [
+    Variable [ Name=$four ]
+    four
+    Variable [ Name=$s1 ]
+    s1
+    Variable [ Name=$s2 ]
+    s2
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    Let Variable [ Name=$s1 ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#3 ]
+              Field=tenk
+            ]
+            Field=two
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#3 ]
+          ]
+        )
+      ]
+    Let Variable [ Name=$s2 ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#4 ]
+              Field=tenk
+            ]
+            Field=unique1
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#4 ]
+          ]
+        )
+      ]
+    UNION
+      SELECT [
+      Variable [ Name=$four ]
+      four
+      Variable [ Name=$s1 ]
+      s1
+      Variable [ Name=$s2 ]
+      s2
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        GROUPING SET (
+        )
+        DECOR
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s1 ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#5 ]
+                Field=tenk
+              ]
+              Field=two
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#5 ]
+            ]
+          )
+        ]
+      Let Variable [ Name=$s2 ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#6 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#6 ]
+            ]
+          )
+        ]
+  )
+  AS Variable [ Name=#2 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#2 ]
+    Field=s1
+  ]
+  DESC
+  FieldAccessor [
+    Variable [ Name=#2 ]
+    Field=s2
+  ]
+  DESC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.11.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.11.ast
new file mode 100644
index 0000000..f290ca3
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.11.ast
@@ -0,0 +1,147 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+FunctionCall asterix.object-remove@2[
+  Variable [ Name=#3 ]
+  LiteralExpr [STRING] [$2]
+]
+]
+FROM [  (
+    SELECT [
+    Variable [ Name=$four ]
+    four
+    Variable [ Name=$s1 ]
+    s1
+    Variable [ Name=$s2 ]
+    $2
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    Let Variable [ Name=$s1 ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#4 ]
+              Field=tenk
+            ]
+            Field=two
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#4 ]
+          ]
+        )
+      ]
+    Let Variable [ Name=$s2 ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#5 ]
+              Field=tenk
+            ]
+            Field=unique1
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#5 ]
+          ]
+        )
+      ]
+    UNION
+      SELECT [
+      Variable [ Name=$four ]
+      four
+      Variable [ Name=$s1 ]
+      s1
+      Variable [ Name=$s2 ]
+      $2
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        GROUPING SET (
+        )
+        DECOR
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s1 ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#6 ]
+                Field=tenk
+              ]
+              Field=two
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#6 ]
+            ]
+          )
+        ]
+      Let Variable [ Name=$s2 ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#7 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#7 ]
+            ]
+          )
+        ]
+  )
+  AS Variable [ Name=#3 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#3 ]
+    Field=s1
+  ]
+  DESC
+  FieldAccessor [
+    Variable [ Name=#3 ]
+    Field=$2
+  ]
+  DESC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.12.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.12.ast
new file mode 100644
index 0000000..df9d569
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.12.ast
@@ -0,0 +1,150 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+FunctionCall asterix.object-remove@2[
+  FunctionCall asterix.object-remove@2[
+    Variable [ Name=#4 ]
+    LiteralExpr [STRING] [$2]
+  ]
+  LiteralExpr [STRING] [$3]
+]
+]
+FROM [  (
+    SELECT [
+    Variable [ Name=$four ]
+    four
+    Variable [ Name=$s1 ]
+    $2
+    Variable [ Name=$s2 ]
+    $3
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    Let Variable [ Name=$s1 ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#5 ]
+              Field=tenk
+            ]
+            Field=two
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#5 ]
+          ]
+        )
+      ]
+    Let Variable [ Name=$s2 ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#6 ]
+              Field=tenk
+            ]
+            Field=unique1
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#6 ]
+          ]
+        )
+      ]
+    UNION
+      SELECT [
+      Variable [ Name=$four ]
+      four
+      Variable [ Name=$s1 ]
+      $2
+      Variable [ Name=$s2 ]
+      $3
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        GROUPING SET (
+        )
+        DECOR
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s1 ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#7 ]
+                Field=tenk
+              ]
+              Field=two
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#7 ]
+            ]
+          )
+        ]
+      Let Variable [ Name=$s2 ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#8 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#8 ]
+            ]
+          )
+        ]
+  )
+  AS Variable [ Name=#4 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#4 ]
+    Field=$2
+  ]
+  DESC
+  FieldAccessor [
+    Variable [ Name=#4 ]
+    Field=$3
+  ]
+  DESC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.13.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.13.ast
new file mode 100644
index 0000000..8761774
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.13.ast
@@ -0,0 +1,147 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+FunctionCall asterix.field-access-by-name@2[
+  Variable [ Name=#5 ]
+  LiteralExpr [STRING] [$4]
+]
+]
+FROM [  (
+    SELECT [
+    Variable [ Name=$four ]
+    $4
+    Variable [ Name=$s1 ]
+    $2
+    Variable [ Name=$s2 ]
+    $3
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    Let Variable [ Name=$s1 ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#6 ]
+              Field=tenk
+            ]
+            Field=two
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#6 ]
+          ]
+        )
+      ]
+    Let Variable [ Name=$s2 ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#7 ]
+              Field=tenk
+            ]
+            Field=unique1
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#7 ]
+          ]
+        )
+      ]
+    UNION
+      SELECT [
+      Variable [ Name=$four ]
+      $4
+      Variable [ Name=$s1 ]
+      $2
+      Variable [ Name=$s2 ]
+      $3
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        GROUPING SET (
+        )
+        DECOR
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s1 ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#8 ]
+                Field=tenk
+              ]
+              Field=two
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#8 ]
+            ]
+          )
+        ]
+      Let Variable [ Name=$s2 ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#9 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#9 ]
+            ]
+          )
+        ]
+  )
+  AS Variable [ Name=#5 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#5 ]
+    Field=$2
+  ]
+  DESC
+  FieldAccessor [
+    Variable [ Name=#5 ]
+    Field=$3
+  ]
+  DESC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.14.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.14.ast
new file mode 100644
index 0000000..00d6c8a
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.14.ast
@@ -0,0 +1,104 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+Variable [ Name=#2 ]
+]
+FROM [  (
+    SELECT [
+    Variable [ Name=$four ]
+    four
+    Variable [ Name=$s1 ]
+    s1
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    Let Variable [ Name=$s1 ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#3 ]
+              Field=tenk
+            ]
+            Field=two
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#3 ]
+          ]
+        )
+      ]
+    UNION
+      SELECT [
+      Variable [ Name=$four ]
+      four
+      Variable [ Name=$s1 ]
+      s1
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        Variable [ Name=$four ]
+        :=
+        FieldAccessor [
+          Variable [ Name=$tenk ]
+          Field=four
+        ]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s1 ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#4 ]
+                Field=tenk
+              ]
+              Field=two
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#4 ]
+            ]
+          )
+        ]
+  )
+  AS Variable [ Name=#2 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#2 ]
+    Field=s1
+  ]
+  DESC
+  FieldAccessor [
+    Variable [ Name=#2 ]
+    Field=four
+  ]
+  DESC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.15.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.15.ast
new file mode 100644
index 0000000..7db8adf
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.15.ast
@@ -0,0 +1,102 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+FunctionCall asterix.field-access-by-name@2[
+  Variable [ Name=#4 ]
+  LiteralExpr [STRING] [$3]
+]
+]
+FROM [  (
+    SELECT [
+    Variable [ Name=$s1 ]
+    $3
+    Variable [ Name=$s1 ]
+    $2
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    Let Variable [ Name=$s1 ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#5 ]
+              Field=tenk
+            ]
+            Field=two
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#5 ]
+          ]
+        )
+      ]
+    UNION
+      SELECT [
+      Variable [ Name=$s1 ]
+      $3
+      Variable [ Name=$s1 ]
+      $2
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        Variable [ Name=$four ]
+        :=
+        FieldAccessor [
+          Variable [ Name=$tenk ]
+          Field=four
+        ]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s1 ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#6 ]
+                Field=tenk
+              ]
+              Field=two
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#6 ]
+            ]
+          )
+        ]
+  )
+  AS Variable [ Name=#4 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#4 ]
+    Field=$2
+  ]
+  DESC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.16.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.16.ast
new file mode 100644
index 0000000..178385f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.16.ast
@@ -0,0 +1,93 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+Variable [ Name=#2 ]
+]
+FROM [  (
+    SELECT [
+    Variable [ Name=$four ]
+    four
+    FunctionCall asterix.sql-sum@1[
+      (
+        SELECT ELEMENT [
+        FieldAccessor [
+          FieldAccessor [
+            Variable [ Name=#3 ]
+            Field=tenk
+          ]
+          Field=two
+        ]
+        ]
+        FROM [          Variable [ Name=#1 ]
+          AS Variable [ Name=#3 ]
+        ]
+      )
+    ]
+    s
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    UNION
+      SELECT [
+      Variable [ Name=$four ]
+      four
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#4 ]
+              Field=tenk
+            ]
+            Field=two
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#4 ]
+          ]
+        )
+      ]
+      s
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        GROUPING SET (
+        )
+        DECOR
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+  )
+  AS Variable [ Name=#2 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#2 ]
+    Field=four
+  ]
+  ASC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.17.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.17.ast
new file mode 100644
index 0000000..f57186c
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.17.ast
@@ -0,0 +1,122 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+FunctionCall asterix.field-access-by-name@2[
+  Variable [ Name=#4 ]
+  LiteralExpr [STRING] [$3]
+]
+]
+FROM [  (
+    SELECT [
+    OperatorExpr [
+      FunctionCall asterix.to-string@1[
+        FunctionCall asterix.if-null[
+          Variable [ Name=$four ]
+          LiteralExpr [STRING] [null]
+        ]
+      ]
+      ||
+      LiteralExpr [STRING] [,]
+      ||
+      FunctionCall asterix.to-string@1[
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#5 ]
+                Field=tenk
+              ]
+              Field=two
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#5 ]
+            ]
+          )
+        ]
+      ]
+    ]
+    $3
+    Variable [ Name=$four ]
+    $2
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    UNION
+      SELECT [
+      OperatorExpr [
+        FunctionCall asterix.to-string@1[
+          FunctionCall asterix.if-null[
+            Variable [ Name=$four ]
+            LiteralExpr [STRING] [null]
+          ]
+        ]
+        ||
+        LiteralExpr [STRING] [,]
+        ||
+        FunctionCall asterix.to-string@1[
+          FunctionCall asterix.sql-sum@1[
+            (
+              SELECT ELEMENT [
+              FieldAccessor [
+                FieldAccessor [
+                  Variable [ Name=#6 ]
+                  Field=tenk
+                ]
+                Field=two
+              ]
+              ]
+              FROM [                Variable [ Name=#1 ]
+                AS Variable [ Name=#6 ]
+              ]
+            )
+          ]
+        ]
+      ]
+      $3
+      Variable [ Name=$four ]
+      $2
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        GROUPING SET (
+        )
+        DECOR
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+  )
+  AS Variable [ Name=#4 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#4 ]
+    Field=$2
+  ]
+  ASC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.18.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.18.ast
new file mode 100644
index 0000000..c80accb
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.18.ast
@@ -0,0 +1,193 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+Variable [ Name=#3 ]
+]
+FROM [  (
+    SELECT ELEMENT [
+    Variable [ Name=#4 ]
+    ]
+    FROM [      (
+        SELECT [
+        Variable [ Name=$two ]
+        two
+        LiteralExpr [NULL]
+        four
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#6 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#6 ]
+            ]
+          )
+        ]
+        s
+        ]
+        FROM [          FunctionCall asterix.dataset@1[
+            LiteralExpr [STRING] [test.tenk]
+          ]
+          AS Variable [ Name=$tenk ]
+        ]
+        Groupby
+          Variable [ Name=$two ]
+          :=
+          FieldAccessor [
+            Variable [ Name=$tenk ]
+            Field=two
+          ]
+          GROUP AS Variable [ Name=#1 ]
+          (
+            tenk:=Variable [ Name=$tenk ]
+          )
+
+        UNION
+          SELECT [
+          Variable [ Name=$two ]
+          two
+          LiteralExpr [NULL]
+          four
+          FunctionCall asterix.sql-sum@1[
+            (
+              SELECT ELEMENT [
+              FieldAccessor [
+                FieldAccessor [
+                  Variable [ Name=#7 ]
+                  Field=tenk
+                ]
+                Field=unique1
+              ]
+              ]
+              FROM [                Variable [ Name=#1 ]
+                AS Variable [ Name=#7 ]
+              ]
+            )
+          ]
+          s
+          ]
+          FROM [            FunctionCall asterix.dataset@1[
+              LiteralExpr [STRING] [test.tenk]
+            ]
+            AS Variable [ Name=$tenk ]
+          ]
+          Groupby
+            GROUPING SET (
+            )
+            DECOR
+            Variable [ Name=$two ]
+            :=
+            LiteralExpr [NULL]
+            GROUP AS Variable [ Name=#1 ]
+            (
+              tenk:=Variable [ Name=$tenk ]
+            )
+
+      )
+      AS Variable [ Name=#4 ]
+    ]
+    UNION
+      SELECT ELEMENT [
+      Variable [ Name=#5 ]
+      ]
+      FROM [        (
+          SELECT [
+          LiteralExpr [NULL]
+          two
+          Variable [ Name=$four ]
+          four
+          FunctionCall asterix.sql-sum@1[
+            (
+              SELECT ELEMENT [
+              FieldAccessor [
+                FieldAccessor [
+                  Variable [ Name=#8 ]
+                  Field=tenk
+                ]
+                Field=unique1
+              ]
+              ]
+              FROM [                Variable [ Name=#2 ]
+                AS Variable [ Name=#8 ]
+              ]
+            )
+          ]
+          s
+          ]
+          FROM [            FunctionCall asterix.dataset@1[
+              LiteralExpr [STRING] [test.tenk]
+            ]
+            AS Variable [ Name=$tenk ]
+          ]
+          Groupby
+            Variable [ Name=$four ]
+            :=
+            FieldAccessor [
+              Variable [ Name=$tenk ]
+              Field=four
+            ]
+            GROUP AS Variable [ Name=#2 ]
+            (
+              tenk:=Variable [ Name=$tenk ]
+            )
+
+          UNION
+            SELECT [
+            LiteralExpr [NULL]
+            two
+            Variable [ Name=$four ]
+            four
+            FunctionCall asterix.sql-sum@1[
+              (
+                SELECT ELEMENT [
+                FieldAccessor [
+                  FieldAccessor [
+                    Variable [ Name=#9 ]
+                    Field=tenk
+                  ]
+                  Field=unique1
+                ]
+                ]
+                FROM [                  Variable [ Name=#2 ]
+                  AS Variable [ Name=#9 ]
+                ]
+              )
+            ]
+            s
+            ]
+            FROM [              FunctionCall asterix.dataset@1[
+                LiteralExpr [STRING] [test.tenk]
+              ]
+              AS Variable [ Name=$tenk ]
+            ]
+            Groupby
+              GROUPING SET (
+              )
+              DECOR
+              Variable [ Name=$four ]
+              :=
+              LiteralExpr [NULL]
+              GROUP AS Variable [ Name=#2 ]
+              (
+                tenk:=Variable [ Name=$tenk ]
+              )
+
+        )
+        AS Variable [ Name=#5 ]
+      ]
+  )
+  AS Variable [ Name=#3 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#3 ]
+    Field=s
+  ]
+  DESC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.6.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.6.ast
new file mode 100644
index 0000000..496b915
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.6.ast
@@ -0,0 +1,160 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+Variable [ Name=#2 ]
+]
+FROM [  (
+    SELECT [
+    Variable [ Name=$two ]
+    two
+    Variable [ Name=$four ]
+    four
+    Variable [ Name=$s ]
+    s
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$two ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=two
+      ]
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    Let Variable [ Name=$s ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#3 ]
+              Field=tenk
+            ]
+            Field=unique1
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#3 ]
+          ]
+        )
+      ]
+    UNION
+      SELECT [
+      Variable [ Name=$two ]
+      two
+      Variable [ Name=$four ]
+      four
+      Variable [ Name=$s ]
+      s
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        Variable [ Name=$two ]
+        :=
+        FieldAccessor [
+          Variable [ Name=$tenk ]
+          Field=two
+        ]
+        DECOR
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#4 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#4 ]
+            ]
+          )
+        ]
+    UNION
+      SELECT [
+      Variable [ Name=$two ]
+      two
+      Variable [ Name=$four ]
+      four
+      Variable [ Name=$s ]
+      s
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        GROUPING SET (
+        )
+        DECOR
+        Variable [ Name=$two ]
+        :=
+        LiteralExpr [NULL]
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#5 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#5 ]
+            ]
+          )
+        ]
+  )
+  AS Variable [ Name=#2 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#2 ]
+    Field=s
+  ]
+  DESC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.7.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.7.ast
new file mode 100644
index 0000000..751a23d
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.7.ast
@@ -0,0 +1,163 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+FunctionCall asterix.object-remove@2[
+  Variable [ Name=#3 ]
+  LiteralExpr [STRING] [$2]
+]
+]
+FROM [  (
+    SELECT [
+    Variable [ Name=$two ]
+    two
+    Variable [ Name=$four ]
+    four
+    Variable [ Name=$s ]
+    $2
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$two ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=two
+      ]
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    Let Variable [ Name=$s ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#4 ]
+              Field=tenk
+            ]
+            Field=unique1
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#4 ]
+          ]
+        )
+      ]
+    UNION
+      SELECT [
+      Variable [ Name=$two ]
+      two
+      Variable [ Name=$four ]
+      four
+      Variable [ Name=$s ]
+      $2
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        Variable [ Name=$two ]
+        :=
+        FieldAccessor [
+          Variable [ Name=$tenk ]
+          Field=two
+        ]
+        DECOR
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#5 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#5 ]
+            ]
+          )
+        ]
+    UNION
+      SELECT [
+      Variable [ Name=$two ]
+      two
+      Variable [ Name=$four ]
+      four
+      Variable [ Name=$s ]
+      $2
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        GROUPING SET (
+        )
+        DECOR
+        Variable [ Name=$two ]
+        :=
+        LiteralExpr [NULL]
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#6 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#6 ]
+            ]
+          )
+        ]
+  )
+  AS Variable [ Name=#3 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#3 ]
+    Field=$2
+  ]
+  DESC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.8.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.8.ast
new file mode 100644
index 0000000..a7b455a
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.8.ast
@@ -0,0 +1,205 @@
+DataverseUse test
+Query:
+SELECT ELEMENT [
+FunctionCall asterix.field-access-by-name@2[
+  Variable [ Name=#4 ]
+  LiteralExpr [STRING] [$3]
+]
+]
+FROM [  (
+    SELECT [
+    OperatorExpr [
+      FunctionCall asterix.to-string@1[
+        FunctionCall asterix.if-null[
+          Variable [ Name=$two ]
+          LiteralExpr [STRING] [null]
+        ]
+      ]
+      ||
+      LiteralExpr [STRING] [,]
+      ||
+      FunctionCall asterix.to-string@1[
+        FunctionCall asterix.if-null[
+          Variable [ Name=$four ]
+          LiteralExpr [STRING] [null]
+        ]
+      ]
+    ]
+    $3
+    Variable [ Name=$s ]
+    $2
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Groupby
+      Variable [ Name=$two ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=two
+      ]
+      Variable [ Name=$four ]
+      :=
+      FieldAccessor [
+        Variable [ Name=$tenk ]
+        Field=four
+      ]
+      GROUP AS Variable [ Name=#1 ]
+      (
+        tenk:=Variable [ Name=$tenk ]
+      )
+
+    Let Variable [ Name=$s ]
+      :=
+      FunctionCall asterix.sql-sum@1[
+        (
+          SELECT ELEMENT [
+          FieldAccessor [
+            FieldAccessor [
+              Variable [ Name=#5 ]
+              Field=tenk
+            ]
+            Field=unique1
+          ]
+          ]
+          FROM [            Variable [ Name=#1 ]
+            AS Variable [ Name=#5 ]
+          ]
+        )
+      ]
+    UNION
+      SELECT [
+      OperatorExpr [
+        FunctionCall asterix.to-string@1[
+          FunctionCall asterix.if-null[
+            Variable [ Name=$two ]
+            LiteralExpr [STRING] [null]
+          ]
+        ]
+        ||
+        LiteralExpr [STRING] [,]
+        ||
+        FunctionCall asterix.to-string@1[
+          FunctionCall asterix.if-null[
+            Variable [ Name=$four ]
+            LiteralExpr [STRING] [null]
+          ]
+        ]
+      ]
+      $3
+      Variable [ Name=$s ]
+      $2
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        Variable [ Name=$two ]
+        :=
+        FieldAccessor [
+          Variable [ Name=$tenk ]
+          Field=two
+        ]
+        DECOR
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#6 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#6 ]
+            ]
+          )
+        ]
+    UNION
+      SELECT [
+      OperatorExpr [
+        FunctionCall asterix.to-string@1[
+          FunctionCall asterix.if-null[
+            Variable [ Name=$two ]
+            LiteralExpr [STRING] [null]
+          ]
+        ]
+        ||
+        LiteralExpr [STRING] [,]
+        ||
+        FunctionCall asterix.to-string@1[
+          FunctionCall asterix.if-null[
+            Variable [ Name=$four ]
+            LiteralExpr [STRING] [null]
+          ]
+        ]
+      ]
+      $3
+      Variable [ Name=$s ]
+      $2
+      ]
+      FROM [        FunctionCall asterix.dataset@1[
+          LiteralExpr [STRING] [test.tenk]
+        ]
+        AS Variable [ Name=$tenk ]
+      ]
+      Groupby
+        GROUPING SET (
+        )
+        DECOR
+        Variable [ Name=$two ]
+        :=
+        LiteralExpr [NULL]
+        Variable [ Name=$four ]
+        :=
+        LiteralExpr [NULL]
+        GROUP AS Variable [ Name=#1 ]
+        (
+          tenk:=Variable [ Name=$tenk ]
+        )
+
+      Let Variable [ Name=$s ]
+        :=
+        FunctionCall asterix.sql-sum@1[
+          (
+            SELECT ELEMENT [
+            FieldAccessor [
+              FieldAccessor [
+                Variable [ Name=#7 ]
+                Field=tenk
+              ]
+              Field=unique1
+            ]
+            ]
+            FROM [              Variable [ Name=#1 ]
+              AS Variable [ Name=#7 ]
+            ]
+          )
+        ]
+  )
+  AS Variable [ Name=#4 ]
+]
+Orderby
+  FieldAccessor [
+    Variable [ Name=#4 ]
+    Field=$2
+  ]
+  DESC
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.9.ast b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.9.ast
new file mode 100644
index 0000000..18c8211
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results_parser_sqlpp/group-by/grouping-sets-2/grouping-sets-2.9.ast
@@ -0,0 +1,179 @@
+Set compiler.groupmemory=4MB
+Set compiler.sortmemory=4MB
+DataverseUse test
+Query:
+Let Variable [ Name=$tenk_subset ]
+  :=
+  (
+    SELECT [
+    FieldAccessor [
+      Variable [ Name=$tenk ]
+      Field=two
+    ]
+    two
+    FieldAccessor [
+      Variable [ Name=$tenk ]
+      Field=unique1
+    ]
+    unique1
+    ]
+    FROM [      FunctionCall asterix.dataset@1[
+        LiteralExpr [STRING] [test.tenk]
+      ]
+      AS Variable [ Name=$tenk ]
+    ]
+    Where
+      OperatorExpr [
+        OperatorExpr [
+          FieldAccessor [
+            Variable [ Name=$tenk ]
+            Field=unique2
+          ]
+          mod
+          LiteralExpr [LONG] [2000]
+        ]
+        =
+        LiteralExpr [LONG] [0]
+      ]
+  )
+Let Variable [ Name=$tenk_sum ]
+  :=
+  (
+    SELECT ELEMENT [
+    Variable [ Name=#1 ]
+    ]
+    FROM [      (
+        SELECT [
+        *
+        ]
+        FROM [          Variable [ Name=$tenk_subset ]
+          AS Variable [ Name=$t ]
+        ]
+        Groupby
+          Variable [ Name=$two ]
+          :=
+          FieldAccessor [
+            Variable [ Name=$t ]
+            Field=two
+          ]
+          GROUP AS Variable [ Name=$g ]
+          (
+            t:=Variable [ Name=$t ]
+          )
+
+        Let Variable [ Name=$s ]
+          :=
+          FunctionCall asterix.sql-sum@1[
+            (
+              SELECT ELEMENT [
+              FieldAccessor [
+                FieldAccessor [
+                  Variable [ Name=#2 ]
+                  Field=t
+                ]
+                Field=unique1
+              ]
+              ]
+              FROM [                Variable [ Name=$g ]
+                AS Variable [ Name=#2 ]
+              ]
+            )
+          ]
+        UNION
+          SELECT [
+          *
+          ]
+          FROM [            Variable [ Name=$tenk_subset ]
+            AS Variable [ Name=$t ]
+          ]
+          Groupby
+            GROUPING SET (
+            )
+            DECOR
+            Variable [ Name=$two ]
+            :=
+            LiteralExpr [NULL]
+            GROUP AS Variable [ Name=$g ]
+            (
+              t:=Variable [ Name=$t ]
+            )
+
+          Let Variable [ Name=$s ]
+            :=
+            FunctionCall asterix.sql-sum@1[
+              (
+                SELECT ELEMENT [
+                FieldAccessor [
+                  FieldAccessor [
+                    Variable [ Name=#3 ]
+                    Field=t
+                  ]
+                  Field=unique1
+                ]
+                ]
+                FROM [                  Variable [ Name=$g ]
+                  AS Variable [ Name=#3 ]
+                ]
+              )
+            ]
+      )
+      AS Variable [ Name=#1 ]
+    ]
+    Orderby
+      FieldAccessor [
+        Variable [ Name=#1 ]
+        Field=s
+      ]
+      DESC
+
+    Limit
+      LiteralExpr [LONG] [3]
+  )
+SELECT [
+FieldAccessor [
+  Variable [ Name=$tt ]
+  Field=s
+]
+s
+FieldAccessor [
+  Variable [ Name=$tt ]
+  Field=two
+]
+two
+Variable [ Name=$gg ]
+gg
+]
+FROM [  Variable [ Name=$tenk_sum ]
+  AS Variable [ Name=$tt ]
+]
+Let Variable [ Name=$gg ]
+  :=
+  (
+    SELECT [
+    Variable [ Name=$gi ]
+    .*
+    ]
+    FROM [      FieldAccessor [
+        Variable [ Name=$tt ]
+        Field=g
+      ]
+      AS Variable [ Name=$gi ]
+    ]
+    Orderby
+      FieldAccessor [
+        FieldAccessor [
+          Variable [ Name=$gi ]
+          Field=t
+        ]
+        Field=unique1
+      ]
+      ASC
+
+  )
+Orderby
+  FieldAccessor [
+    Variable [ Name=$tt ]
+    Field=s
+  ]
+  DESC
+
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 7d4dbdd..687b743 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -5628,6 +5628,7 @@
         <expected-error>ASX1119: Invalid argument to grouping() function</expected-error>
         <expected-error>ASX1119: Invalid argument to grouping() function</expected-error>
         <expected-error>ASX1118: Too many grouping sets in group by clause: 512. Maximum allowed: 128.</expected-error>
+        <expected-error>ASX1129: Cannot compile SELECT variable.* with GROUP BY GROUPING SETS/ROLLUP/CUBE followed by ORDER BY/LIMIT</expected-error>
       </compilation-unit>
     </test-case>
     <test-case FilePath="group-by">
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
index 5208b5d..2c7bf09 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
@@ -219,6 +219,7 @@
     public static final int INVALID_EXTERNAL_IDENTIFIER_SIZE = 1126;
     public static final int UNSUPPORTED_ADAPTER_LANGUAGE = 1127;
     public static final int INCONSISTENT_FILTER_INDICATOR = 1128;
+    public static final int UNSUPPORTED_GBY_OBY_SELECT_COMBO = 1129;
 
     // Feed errors
     public static final int DATAFLOW_ILLEGAL_STATE = 3001;
diff --git a/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties b/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
index 07a496c..75801a4 100644
--- a/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
+++ b/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
@@ -214,6 +214,7 @@
 1126 = Invalid number of elements in external identifier. Expected %1$s elements for %2$s language
 1127 = Unsupported adapter language: %1$s
 1128 = Filter field is not defined properly
+1129 = Cannot compile SELECT variable.* with GROUP BY GROUPING SETS/ROLLUP/CUBE followed by ORDER BY/LIMIT
 
 # Feed Errors
 3001 = Illegal state.
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/clause/SelectBlock.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/clause/SelectBlock.java
index c934e32..9c743ad 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/clause/SelectBlock.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/clause/SelectBlock.java
@@ -64,10 +64,18 @@
         return selectClause;
     }
 
+    public void setSelectClause(SelectClause selectClause) {
+        this.selectClause = selectClause;
+    }
+
     public FromClause getFromClause() {
         return fromClause;
     }
 
+    public void setFromClause(FromClause fromClause) {
+        this.fromClause = fromClause;
+    }
+
     public List<AbstractClause> getLetWhereList() {
         return letWhereClauses;
     }
@@ -76,6 +84,10 @@
         return groupbyClause;
     }
 
+    public void setGroupbyClause(GroupbyClause groupbyClause) {
+        this.groupbyClause = groupbyClause;
+    }
+
     public List<AbstractClause> getLetHavingListAfterGroupby() {
         return letHavingClausesAfterGby;
     }
@@ -96,10 +108,6 @@
         return !letHavingClausesAfterGby.isEmpty();
     }
 
-    public void setGroupbyClause(GroupbyClause groupbyClause) {
-        this.groupbyClause = groupbyClause;
-    }
-
     @Override
     public int hashCode() {
         return Objects.hash(fromClause, groupbyClause, letWhereClauses, letHavingClausesAfterGby, selectClause);
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/clause/SelectClause.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/clause/SelectClause.java
index 2627870..3b550a3 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/clause/SelectClause.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/clause/SelectClause.java
@@ -68,6 +68,10 @@
         return distinct;
     }
 
+    public void setDistinct(boolean distinct) {
+        this.distinct = distinct;
+    }
+
     @Override
     public String toString() {
         return "select " + (distinct ? "distinct " : "")
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SetOperationVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SetOperationVisitor.java
index fe86cfc..da8b5f2 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SetOperationVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SetOperationVisitor.java
@@ -21,6 +21,7 @@
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.function.BiFunction;
 
 import org.apache.asterix.common.exceptions.CompilationException;
 import org.apache.asterix.lang.common.base.Expression;
@@ -87,7 +88,7 @@
         // Wraps the set operation part with a subquery.
         SelectExpression nestedSelectExpression = new SelectExpression(null, selectSetOperation, null, null, true);
         nestedSelectExpression.setSourceLocation(sourceLoc);
-        SelectBlock selectBlock = createSelectBlock(nestedSelectExpression, context);
+        SelectBlock selectBlock = createSelectBlock(nestedSelectExpression, false, null, null, context);
         SelectSetOperation newSelectSetOperation =
                 new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
         newSelectSetOperation.setSourceLocation(sourceLoc);
@@ -98,7 +99,9 @@
         return super.visit(newSelectExpression, arg);
     }
 
-    static SelectBlock createSelectBlock(Expression inputExpr, LangRewritingContext context) {
+    static <P> SelectBlock createSelectBlock(Expression inputExpr, boolean distinct,
+            BiFunction<? super VariableExpr, P, Expression> valueExprTransformer, P valueExprTransformerArg,
+            LangRewritingContext context) {
         SourceLocation sourceLoc = inputExpr.getSourceLocation();
         VariableExpr newBindingVar = new VariableExpr(context.newVariable()); // Binding variable for the subquery.
         newBindingVar.setSourceLocation(sourceLoc);
@@ -106,7 +109,9 @@
         newFromTerm.setSourceLocation(sourceLoc);
         FromClause newFromClause = new FromClause(new ArrayList<>(Collections.singletonList(newFromTerm)));
         newFromClause.setSourceLocation(sourceLoc);
-        SelectClause selectClause = new SelectClause(new SelectElement(newBindingVar), null, false);
+        Expression valueExpr = valueExprTransformer != null
+                ? valueExprTransformer.apply(newBindingVar, valueExprTransformerArg) : newBindingVar;
+        SelectClause selectClause = new SelectClause(new SelectElement(valueExpr), null, distinct);
         selectClause.setSourceLocation(sourceLoc);
         SelectBlock selectBlock = new SelectBlock(selectClause, newFromClause, null, null, null);
         selectBlock.setSourceLocation(sourceLoc);
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupingSetsVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupingSetsVisitor.java
index a6892c1..c474ee3 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupingSetsVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupingSetsVisitor.java
@@ -22,12 +22,16 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import org.apache.asterix.common.exceptions.CompilationException;
 import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.functions.FunctionSignature;
 import org.apache.asterix.lang.common.base.Clause;
 import org.apache.asterix.lang.common.base.Expression;
 import org.apache.asterix.lang.common.base.ILangExpression;
@@ -39,24 +43,32 @@
 import org.apache.asterix.lang.common.expression.VariableExpr;
 import org.apache.asterix.lang.common.literal.LongIntegerLiteral;
 import org.apache.asterix.lang.common.literal.NullLiteral;
+import org.apache.asterix.lang.common.literal.StringLiteral;
 import org.apache.asterix.lang.common.rewrites.LangRewritingContext;
+import org.apache.asterix.lang.sqlpp.clause.Projection;
 import org.apache.asterix.lang.sqlpp.clause.SelectBlock;
+import org.apache.asterix.lang.sqlpp.clause.SelectClause;
+import org.apache.asterix.lang.sqlpp.clause.SelectElement;
+import org.apache.asterix.lang.sqlpp.clause.SelectRegular;
 import org.apache.asterix.lang.sqlpp.clause.SelectSetOperation;
 import org.apache.asterix.lang.sqlpp.expression.SelectExpression;
 import org.apache.asterix.lang.sqlpp.optype.SetOpType;
 import org.apache.asterix.lang.sqlpp.struct.SetOperationInput;
 import org.apache.asterix.lang.sqlpp.struct.SetOperationRight;
 import org.apache.asterix.lang.sqlpp.util.SqlppRewriteUtil;
-import org.apache.asterix.lang.sqlpp.visitor.base.AbstractSqlppSimpleExpressionVisitor;
+import org.apache.asterix.lang.sqlpp.util.SqlppVariableUtil;
+import org.apache.asterix.lang.sqlpp.visitor.FreeVariableVisitor;
+import org.apache.asterix.lang.sqlpp.visitor.SqlppSubstituteExpressionVisitor;
+import org.apache.asterix.lang.sqlpp.visitor.base.AbstractSqlppExpressionScopingVisitor;
 import org.apache.asterix.om.functions.BuiltinFunctions;
+import org.apache.hyracks.algebricks.common.utils.ListSet;
+import org.apache.hyracks.algebricks.common.utils.Pair;
 
 /**
  * Rewrites GROUP BY clauses with multiple grouping sets into UNION ALL.
  * Also rewrites valid GROUPING(...) operations into constants.
  */
-public final class SqlppGroupingSetsVisitor extends AbstractSqlppSimpleExpressionVisitor {
-
-    private final LangRewritingContext context;
+public final class SqlppGroupingSetsVisitor extends AbstractSqlppExpressionScopingVisitor {
 
     private final List<List<GbyVariableExpressionPair>> tmpGroupingSets = new ArrayList<>(1);
 
@@ -67,7 +79,7 @@
     private final Set<VariableExpr> tmpCurrentGroupingSetVars = new LinkedHashSet<>();
 
     public SqlppGroupingSetsVisitor(LangRewritingContext context) {
-        this.context = context;
+        super(context);
     }
 
     @Override
@@ -76,15 +88,30 @@
 
         SetOperationInput setOpInputLeft = setOp.getLeftInput();
         SelectBlock selectBlockLeft = setOpInputLeft.getSelectBlock();
+
         if (selectBlockLeft != null && selectBlockLeft.hasGroupbyClause()) {
-            setOpInputLeft.setSelectBlock(rewriteSelectBlock(selectBlockLeft));
+            SelectExpression selectExpression = (SelectExpression) arg;
+            boolean hasPostSetOpClauses = selectExpression.hasOrderby() || selectExpression.hasLimit();
+            Map<VariableExpr, String> extraProjections = Collections.emptyMap();
+            if (hasPostSetOpClauses && !setOp.hasRightInputs() && isMultiGroupingSets(selectBlockLeft)) {
+                // We need to make sure that ORDER BY/LIMIT free vars are still available after UNION ALL rewriting.
+                // To do that we expose these variables by adding them to SELECT so they can be used after UNION ALL.
+                // Then we rewrite ORDERBY/LIMIT to replace these free vars with field accessors on top of SELECT.
+                // Then we remove these extra projections from the final output.
+                extraProjections = rewritePostSetOpClauses(selectExpression, selectBlockLeft);
+            }
+
+            SelectBlock newSelectBlockLeft = rewriteSelectBlock(selectBlockLeft, extraProjections);
+            setOpInputLeft.setSelectBlock(newSelectBlockLeft);
         }
+
         if (setOp.hasRightInputs()) {
             for (SetOperationRight setOpRight : setOp.getRightInputs()) {
                 SetOperationInput setOpInputRight = setOpRight.getSetOperationRightInput();
                 SelectBlock selectBlockRight = setOpInputRight.getSelectBlock();
                 if (selectBlockRight != null && selectBlockRight.hasGroupbyClause()) {
-                    setOpInputRight.setSelectBlock(rewriteSelectBlock(selectBlockRight));
+                    SelectBlock newSelectBlockRight = rewriteSelectBlock(selectBlockRight, Collections.emptyMap());
+                    setOpInputRight.setSelectBlock(newSelectBlockRight);
                 }
             }
         }
@@ -92,13 +119,170 @@
         return null;
     }
 
-    private SelectBlock rewriteSelectBlock(SelectBlock selectBlock) throws CompilationException {
-        return selectBlock.getGroupbyClause().getGbyPairList().size() <= 1 ? rewriteZeroOrOneGroupingSet(selectBlock)
-                : rewriteMultipleGroupingSets(selectBlock);
+    private Map<VariableExpr, String> rewritePostSetOpClauses(SelectExpression selectExpression,
+            SelectBlock selectBlockLeft) throws CompilationException {
+        // 1. Get free variables in ORDERBY/LIMIT
+        Set<VariableExpr> freeVarsPostSetOp = getFreeVarsPostSetOp(selectExpression);
+        if (freeVarsPostSetOp.isEmpty()) {
+            return Collections.emptyMap();
+        }
+
+        // 2. Compute substitution map for ORDERBY/LIMIT to replace free vars with field accessors
+        Pair<Map<VariableExpr, VariableExpr>, Map<VariableExpr, String>> p =
+                computePostSetOpSubstMap(selectBlockLeft, freeVarsPostSetOp);
+        Map<VariableExpr, VariableExpr> freeVarsPostSetOpSubstMap = p.first;
+        Map<VariableExpr, String> extraProjections = p.second;
+
+        // 3. Rewrite ORDERBY/LIMIT using this substitution
+        SqlppSubstituteExpressionVisitor substVisitor =
+                new SqlppSubstituteExpressionVisitor(context, freeVarsPostSetOpSubstMap);
+        if (selectExpression.hasOrderby()) {
+            selectExpression.getOrderbyClause().accept(substVisitor, selectExpression);
+        }
+        if (selectExpression.hasLimit()) {
+            selectExpression.getLimitClause().accept(substVisitor, selectExpression);
+        }
+
+        // 4. return extra projections that should be added to SELECT
+        return extraProjections;
+    }
+
+    private Pair<Map<VariableExpr, VariableExpr>, Map<VariableExpr, String>> computePostSetOpSubstMap(
+            SelectBlock selectBlockLeft, Set<VariableExpr> freeVarsPostSetOp) throws CompilationException {
+        SelectClause selectClause = selectBlockLeft.getSelectClause();
+        if (selectClause.selectRegular()) {
+            return computePostSetOpSubstMapForSelectRegular(freeVarsPostSetOp, selectBlockLeft);
+        } else if (selectClause.selectElement()) {
+            return computePostSetOpSubstMapForSelectElement(freeVarsPostSetOp);
+        } else {
+            throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, selectBlockLeft.getSourceLocation(),
+                    "");
+        }
+    }
+
+    private Pair<Map<VariableExpr, VariableExpr>, Map<VariableExpr, String>> computePostSetOpSubstMapForSelectRegular(
+            Set<VariableExpr> freeVarsPostSetOp, SelectBlock selectBlock) throws CompilationException {
+        // For regular SELECT we need to add ORDERBY/LIMIT free variables to the projection list of the SELECT clause,
+        // so they can be accessed using field-access-by-name after SELECT.
+        // Some of these variables might be already projected by SELECT, so we need to account for that.
+        // We currently do not support (fail) SELECT v.* because in this case we cannot statically compute
+        // the schema of the record produced by the SELECT and therefore cannot guarantee that the field
+        // names we generate will not conflict with the existing fields in the SELECT output.
+        // The added projections will be later removed by the outer query.
+
+        Map<VariableExpr, String> extraProjections = Collections.emptyMap();
+        Map<VariableExpr, VariableExpr> freeVarsPostSetOpSubstMap = new HashMap<>();
+
+        List<Projection> projectionList = selectBlock.getSelectClause().getSelectRegular().getProjections();
+        List<VariableExpr> gbyBindingVars = null, postGbyBindingVars = null;
+        for (VariableExpr freeVarPostSetOp : freeVarsPostSetOp) {
+            String projectionName = null;
+            for (int i = projectionList.size() - 1; i >= 0; i--) {
+                Projection projection = projectionList.get(i);
+                if (projection.varStar()) {
+                    throw new CompilationException(ErrorCode.UNSUPPORTED_GBY_OBY_SELECT_COMBO,
+                            selectBlock.getSourceLocation());
+                } else if (projection.star()) {
+                    if (gbyBindingVars == null) {
+                        gbyBindingVars = SqlppVariableUtil.getBindingVariables(selectBlock.getGroupbyClause());
+                    }
+                    if (postGbyBindingVars == null) {
+                        postGbyBindingVars = selectBlock.hasLetHavingClausesAfterGroupby()
+                                ? SqlppVariableUtil.getLetBindingVariables(selectBlock.getLetHavingListAfterGroupby())
+                                : Collections.emptyList();
+                    }
+                    if (gbyBindingVars.contains(freeVarPostSetOp) || postGbyBindingVars.contains(freeVarPostSetOp)) {
+                        projectionName = SqlppVariableUtil
+                                .variableNameToDisplayedFieldName(freeVarPostSetOp.getVar().getValue());
+                        break;
+                    }
+                } else if (projection.hasName()) {
+                    if (projection.getExpression().equals(freeVarPostSetOp)) {
+                        projectionName = projection.getName();
+                        break;
+                    }
+                } else {
+                    throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, selectBlock.getSourceLocation(),
+                            "");
+                }
+            }
+
+            boolean newProjection = false;
+            if (projectionName == null) {
+                newProjection = true;
+                projectionName = generateProjectionName(projectionList);
+            }
+
+            VariableExpr substExpr = new VariableExpr(SqlppVariableUtil.toInternalVariableIdentifier(projectionName));
+            substExpr.setSourceLocation(freeVarPostSetOp.getSourceLocation());
+            freeVarsPostSetOpSubstMap.put(freeVarPostSetOp, substExpr);
+
+            if (newProjection) {
+                if (extraProjections.isEmpty()) {
+                    extraProjections = new LinkedHashMap<>();
+                }
+                extraProjections.put(freeVarPostSetOp, projectionName);
+            }
+        }
+
+        return new Pair<>(freeVarsPostSetOpSubstMap, extraProjections);
+    }
+
+    private Pair<Map<VariableExpr, VariableExpr>, Map<VariableExpr, String>> computePostSetOpSubstMapForSelectElement(
+            Set<VariableExpr> freeVarsPostSetOp) {
+        // For SELECT VALUE we first need to convert it to the regular SELECT, so we can then
+        // add ORDERBY/LIMIT free variables to the projection list of this regular SELECT clause.
+        // They can be accessed using field-access-by-name after SELECT.
+        // SELECT VALUE to regular conversion will be reversed later.
+        Map<VariableExpr, VariableExpr> freeVarsPostSetOpSubstMap = new HashMap<>();
+        Map<VariableExpr, String> extraProjections = new LinkedHashMap<>();
+        for (VariableExpr freeVarPostSelect : freeVarsPostSetOp) {
+            String projectionName =
+                    SqlppVariableUtil.variableNameToDisplayedFieldName(context.newVariable().getValue());
+            VariableExpr substExpr = new VariableExpr(SqlppVariableUtil.toInternalVariableIdentifier(projectionName));
+            substExpr.setSourceLocation(freeVarPostSelect.getSourceLocation());
+            freeVarsPostSetOpSubstMap.put(freeVarPostSelect, substExpr);
+            extraProjections.put(freeVarPostSelect, projectionName);
+        }
+
+        return new Pair<>(freeVarsPostSetOpSubstMap, extraProjections);
+    }
+
+    private Set<VariableExpr> getFreeVarsPostSetOp(SelectExpression selectExpression) throws CompilationException {
+        if (selectExpression.hasOrderby() || selectExpression.hasLimit()) {
+            Set<VariableExpr> freeVars = new ListSet<>();
+            FreeVariableVisitor freeVarsVisitor = new FreeVariableVisitor();
+            if (selectExpression.hasOrderby()) {
+                selectExpression.getOrderbyClause().accept(freeVarsVisitor, freeVars);
+            }
+            if (selectExpression.hasLimit()) {
+                selectExpression.getLimitClause().accept(freeVarsVisitor, freeVars);
+            }
+            return freeVars;
+        } else {
+            return Collections.emptySet();
+        }
+    }
+
+    private boolean isMultiGroupingSets(SelectBlock selectBlock) {
+        return selectBlock.getGroupbyClause().getGbyPairList().size() > 1;
+    }
+
+    private SelectBlock rewriteSelectBlock(SelectBlock selectBlock, Map<VariableExpr, String> extraProjections)
+            throws CompilationException {
+        if (isMultiGroupingSets(selectBlock)) {
+            return rewriteMultipleGroupingSets(selectBlock, extraProjections);
+        } else {
+            if (!extraProjections.isEmpty()) {
+                throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, selectBlock.getSourceLocation(),
+                        extraProjections.toString());
+            }
+            return rewriteZeroOrOneGroupingSet(selectBlock);
+        }
     }
 
     private SelectBlock rewriteZeroOrOneGroupingSet(SelectBlock selectBlock) throws CompilationException {
-        // no UNION ALL, we only need to rewrite GROUPING(..) operations
+        // no UNION ALL if there's only one grouping set (or zero), but we still need to rewrite GROUPING(..) operations
         GroupbyClause gby = selectBlock.getGroupbyClause();
         List<List<GbyVariableExpressionPair>> groupingSets = gby.getGbyPairList();
         if (groupingSets.size() > 1) {
@@ -110,17 +294,29 @@
         return selectBlock;
     }
 
-    private SelectBlock rewriteMultipleGroupingSets(SelectBlock selectBlock) throws CompilationException {
+    private SelectBlock rewriteMultipleGroupingSets(SelectBlock selectBlock, Map<VariableExpr, String> extraProjections)
+            throws CompilationException {
         GroupbyClause gby = selectBlock.getGroupbyClause();
         List<List<GbyVariableExpressionPair>> groupingSets = gby.getGbyPairList();
-        if (groupingSets.size() <= 1 || !gby.getDecorPairList().isEmpty()) {
+        int nGroupingSets = groupingSets.size();
+        if (nGroupingSets <= 1 || !gby.getDecorPairList().isEmpty()) {
             throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, gby.getSourceLocation(), "");
         }
 
+        SelectClause selectClause = selectBlock.getSelectClause();
+        boolean distinct = selectClause.distinct();
+        boolean selectElement = selectClause.selectElement();
+        boolean selectElementConvertToRegular = selectElement && !extraProjections.isEmpty();
+
+        // If we're converting SELECT VALUE to regular SELECT then we need to generate a field name
+        // that'll hold SELECT VALUE expression after the conversion.
+        // SELECT VALUE expr -> SELECT expr AS main_projection_name
+        String selectElementMainProjectionName = selectElementConvertToRegular
+                ? SqlppVariableUtil.variableNameToDisplayedFieldName(context.newVariable().getValue()) : null;
+
         tmpAllGroupingSetsVars.clear();
         getAllGroupingSetsVars(groupingSets, tmpAllGroupingSetsVars);
 
-        int nGroupingSets = groupingSets.size();
         List<SetOperationRight> newSetOpRightInputs = new ArrayList<>(nGroupingSets - 1);
 
         // process 2nd and following grouping sets
@@ -136,12 +332,12 @@
             gby.setGbyPairList(tmpGroupingSets); // will be cloned by deepCopy() below
 
             tmpDecorPairList.clear();
-            computeDecorVars(tmpAllGroupingSetsVars, tmpCurrentGroupingSetVars, tmpDecorPairList);
+            computeDecorVars(tmpCurrentGroupingSetVars, tmpAllGroupingSetsVars, tmpDecorPairList);
             gby.setDecorPairList(tmpDecorPairList); // will be cloned by deepCopy() below
 
             SelectBlock newSelectBlock = (SelectBlock) SqlppRewriteUtil.deepCopy(selectBlock);
-
-            rewriteGroupingOperations(newSelectBlock, tmpAllGroupingSetsVars, tmpCurrentGroupingSetVars);
+            rewriteGroupingOperations(newSelectBlock, tmpCurrentGroupingSetVars, tmpAllGroupingSetsVars);
+            rewriteSelectClause(newSelectBlock, extraProjections, selectElementMainProjectionName);
 
             SetOperationRight newSetOpRight =
                     new SetOperationRight(SetOpType.UNION, false, new SetOperationInput(newSelectBlock, null));
@@ -157,10 +353,11 @@
         getGroupingSetVars(groupingSet, tmpCurrentGroupingSetVars);
 
         List<GbyVariableExpressionPair> newDecorPairList = new ArrayList<>();
-        computeDecorVars(tmpAllGroupingSetsVars, tmpCurrentGroupingSetVars, newDecorPairList);
+        computeDecorVars(tmpCurrentGroupingSetVars, tmpAllGroupingSetsVars, newDecorPairList);
         gby.setDecorPairList(newDecorPairList);
 
-        rewriteGroupingOperations(selectBlock, tmpAllGroupingSetsVars, tmpCurrentGroupingSetVars);
+        rewriteGroupingOperations(selectBlock, tmpCurrentGroupingSetVars, tmpAllGroupingSetsVars);
+        rewriteSelectClause(selectBlock, extraProjections, selectElementMainProjectionName);
 
         SetOperationInput newSetOpInput = new SetOperationInput(selectBlock, null);
 
@@ -170,7 +367,100 @@
         SelectExpression newSelectExpr = new SelectExpression(null, newSetOp, null, null, true);
         newSelectExpr.setSourceLocation(selectBlock.getSourceLocation());
 
-        return SetOperationVisitor.createSelectBlock(newSelectExpr, context);
+        return selectElement
+                ? SetOperationVisitor.createSelectBlock(newSelectExpr, distinct,
+                        selectElementConvertToRegular ? SqlppGroupingSetsVisitor::getFieldByName : null,
+                        selectElementConvertToRegular ? selectElementMainProjectionName : null, context)
+                : SetOperationVisitor.createSelectBlock(newSelectExpr, distinct,
+                        SqlppGroupingSetsVisitor::removeFieldsByName, extraProjections.values(), context);
+    }
+
+    private static void rewriteSelectClause(SelectBlock selectBlock, Map<VariableExpr, String> extraProjections,
+            String selectElementMainProjectionName) throws CompilationException {
+        SelectClause selectClause = selectBlock.getSelectClause();
+        // unset distinct. we'll have it in the outer select if needed
+        selectClause.setDistinct(false);
+        if (!extraProjections.isEmpty()) {
+            if (selectClause.selectElement()) {
+                selectClause = convertSelectElementToRegular(selectClause, selectElementMainProjectionName);
+            }
+            insertExtraProjections(selectClause, extraProjections);
+        }
+        selectBlock.setSelectClause(selectClause);
+    }
+
+    private static SelectClause convertSelectElementToRegular(SelectClause selectClause, String mainProjectionName)
+            throws CompilationException {
+        if (!selectClause.selectElement() || selectClause.distinct()) {
+            throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, selectClause.getSourceLocation(), "");
+        }
+        SelectElement selectElement = selectClause.getSelectElement();
+        List<Projection> projectionList = new ArrayList<>(1);
+        projectionList.add(new Projection(selectElement.getExpression(), mainProjectionName, false, false));
+        SelectRegular newSelectRegular = new SelectRegular(projectionList);
+        newSelectRegular.setSourceLocation(selectElement.getSourceLocation());
+        SelectClause newSelectClause = new SelectClause(null, newSelectRegular, selectClause.distinct());
+        newSelectClause.setSourceLocation(selectClause.getSourceLocation());
+        return newSelectClause;
+    }
+
+    private static void insertExtraProjections(SelectClause selectClause, Map<VariableExpr, String> projections)
+            throws CompilationException {
+        if (!selectClause.selectRegular()) {
+            // "SELECT VALUE expr" should've been rewritten earlier into "SELECT expr AS $n"
+            throw new CompilationException(ErrorCode.COMPILATION_ILLEGAL_STATE, selectClause.getSourceLocation(), "");
+        }
+        SelectRegular selectRegular = selectClause.getSelectRegular();
+        for (Map.Entry<VariableExpr, String> me : projections.entrySet()) {
+            Projection newProjection =
+                    new Projection((VariableExpr) SqlppRewriteUtil.deepCopy(me.getKey()), me.getValue(), false, false);
+            selectRegular.getProjections().add(newProjection);
+        }
+    }
+
+    private static Expression removeFieldsByName(Expression inExpr, Collection<String> fieldNames) {
+        //TODO(dmitry): need to enhance RECORD_REMOVE to accept an array of field names the should be removed.
+        //Until that's implemented we can only remove them one by one.
+        Expression resultExpr = inExpr;
+        for (String fieldName : fieldNames) {
+            LiteralExpr fieldNameExpr = new LiteralExpr(new StringLiteral(fieldName));
+            fieldNameExpr.setSourceLocation(resultExpr.getSourceLocation());
+            List<Expression> argList = new ArrayList<>(2);
+            argList.add(resultExpr);
+            argList.add(fieldNameExpr);
+            CallExpr callExpr = new CallExpr(new FunctionSignature(BuiltinFunctions.RECORD_REMOVE), argList);
+            callExpr.setSourceLocation(resultExpr.getSourceLocation());
+            resultExpr = callExpr;
+        }
+        return resultExpr;
+    }
+
+    private static Expression getFieldByName(Expression inExpr, String fieldName) {
+        LiteralExpr fieldNameExpr = new LiteralExpr(new StringLiteral(fieldName));
+        fieldNameExpr.setSourceLocation(inExpr.getSourceLocation());
+        List<Expression> argList = new ArrayList<>(2);
+        argList.add(inExpr);
+        argList.add(fieldNameExpr);
+        CallExpr callExpr = new CallExpr(new FunctionSignature(BuiltinFunctions.FIELD_ACCESS_BY_NAME), argList);
+        callExpr.setSourceLocation(inExpr.getSourceLocation());
+        return callExpr;
+    }
+
+    private String generateProjectionName(List<Projection> projectionList) {
+        String projectionName;
+        do {
+            projectionName = SqlppVariableUtil.variableNameToDisplayedFieldName(context.newVariable().getValue());
+        } while (findProjectionByName(projectionList, projectionName) != null);
+        return projectionName;
+    }
+
+    private static Projection findProjectionByName(List<Projection> projectionList, String name) {
+        for (Projection projection : projectionList) {
+            if (projection.hasName() && projection.getName().equals(name)) {
+                return projection;
+            }
+        }
+        return null;
     }
 
     /**
@@ -180,16 +470,16 @@
      * The remaining GROUPING() operations are invalid and will lead to a compile-time failure later
      * because there's no runtime for GROUPING() function.
      */
-    private void rewriteGroupingOperations(SelectBlock selectBlock, Set<VariableExpr> allGroupingSetsVars,
-            Set<VariableExpr> currentGroupingSetVars) throws CompilationException {
+    private static void rewriteGroupingOperations(SelectBlock selectBlock, Set<VariableExpr> currentGroupingSetVars,
+            Set<VariableExpr> allGroupingSetsVars) throws CompilationException {
         if (selectBlock.hasLetHavingClausesAfterGroupby()) {
             for (Clause clause : selectBlock.getLetHavingListAfterGroupby()) {
                 if (clause.getClauseType() == Clause.ClauseType.LET_CLAUSE) {
                     LetClause letClause = (LetClause) clause;
                     Expression letExpr = letClause.getBindingExpr();
                     if (SqlppGroupByVisitor.isGroupingOperation(letExpr)) {
-                        Expression newLetExpr = rewriteGroupingOperation((CallExpr) letExpr, allGroupingSetsVars,
-                                currentGroupingSetVars);
+                        Expression newLetExpr = rewriteGroupingOperation((CallExpr) letExpr, currentGroupingSetVars,
+                                allGroupingSetsVars);
                         letClause.setBindingExpr(newLetExpr);
                     }
                 }
@@ -197,8 +487,8 @@
         }
     }
 
-    private Expression rewriteGroupingOperation(CallExpr callExpr, Set<VariableExpr> allGroupingSetsVars,
-            Set<VariableExpr> currentGroupingSetVars) throws CompilationException {
+    private static Expression rewriteGroupingOperation(CallExpr callExpr, Set<VariableExpr> currentGroupingSetVars,
+            Set<VariableExpr> allGroupingSetsVars) throws CompilationException {
         List<Expression> argList = callExpr.getExprList();
         if (argList.isEmpty()) {
             throw new CompilationException(ErrorCode.COMPILATION_INVALID_NUM_OF_ARGS, callExpr.getSourceLocation(),
@@ -247,8 +537,8 @@
         }
     }
 
-    private static void computeDecorVars(Set<VariableExpr> allGroupingSetsVars,
-            Set<VariableExpr> currentGroupingSetVars, List<GbyVariableExpressionPair> outDecorPairList) {
+    private static void computeDecorVars(Set<VariableExpr> currentGroupingSetVars,
+            Set<VariableExpr> allGroupingSetsVars, List<GbyVariableExpressionPair> outDecorPairList) {
         for (VariableExpr var : allGroupingSetsVars) {
             if (!currentGroupingSetVars.contains(var)) {
                 LiteralExpr nullExpr = new LiteralExpr(NullLiteral.INSTANCE);
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteExpressionVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteExpressionVisitor.java
index 636f8a6..127e17a 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteExpressionVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/SqlppSubstituteExpressionVisitor.java
@@ -39,7 +39,8 @@
 
     private final Map<Expression, Expression> exprMap = new HashMap<>();
 
-    public SqlppSubstituteExpressionVisitor(LangRewritingContext context, Map<Expression, Expression> exprMap) {
+    public SqlppSubstituteExpressionVisitor(LangRewritingContext context,
+            Map<? extends Expression, ? extends Expression> exprMap) {
         super(context);
         this.exprMap.putAll(exprMap);
     }
diff --git a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/union/UnionAllOperatorDescriptor.java b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/union/UnionAllOperatorDescriptor.java
index a03d8d7..a1ea25b 100644
--- a/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/union/UnionAllOperatorDescriptor.java
+++ b/hyracks-fullstack/hyracks/hyracks-dataflow-std/src/main/java/org/apache/hyracks/dataflow/std/union/UnionAllOperatorDescriptor.java
@@ -92,7 +92,9 @@
                 @Override
                 public void nextFrame(ByteBuffer buffer) throws HyracksDataException {
                     synchronized (UnionOperator.this) {
-                        writer.nextFrame(buffer);
+                        if (!failed) {
+                            writer.nextFrame(buffer);
+                        }
                     }
                 }
 
@@ -100,9 +102,9 @@
                 public void fail() throws HyracksDataException {
                     synchronized (UnionOperator.this) {
                         if (!failed) {
+                            failed = true;
                             writer.fail();
                         }
-                        failed = true;
                     }
                 }