[NO ISSUE][COMP] Fix AbstractIntroduceGroupByCombinerRule

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

Details:
- AbstractIntroduceGroupByCombinerRule did not always
  restore the plan to its original state when failing

Change-Id: I9cada7f324a271ff017c64b69326b9d4a1a32a85
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/5925
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-nested-01/gby-nested-01.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-nested-01/gby-nested-01.1.ddl.sqlpp
new file mode 100644
index 0000000..859c349
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-nested-01/gby-nested-01.1.ddl.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.
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+
+use test;
+
+create dataset prd(p_id integer not null) open type primary key p_id;
+create dataset sls(s_id integer not null) open type primary key s_id;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-nested-01/gby-nested-01.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-nested-01/gby-nested-01.2.update.sqlpp
new file mode 100644
index 0000000..1fb010e
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-nested-01/gby-nested-01.2.update.sqlpp
@@ -0,0 +1,66 @@
+/*
+ * 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;
+
+insert into prd
+([
+  {
+    "p_id": 1,
+    "p_name": "PRODUCT_1"
+  },
+  {
+    "p_id": 2,
+    "p_name": "PRODUCT_2"
+  }
+]);
+
+insert into sls
+([
+  {
+    "s_id": 10,
+    "s_pid": 1,
+    "s_year": 2019,
+    "s_city": [
+      { "ct_id": "SFO", "ct_sum": 10, "ct_avg": 5.0 },
+      { "ct_id": "SJC", "ct_sum": 12, "ct_avg": 6.0 },
+      { "ct_id": "RNO", "ct_sum": 14, "ct_avg": 7.0 },
+      { "ct_id": "LAS", "ct_sum": 16, "ct_avg": 8.0 }
+    ],
+    "s_state": [
+      { "st_id": "CA", "st_sum": 22, "st_avg": 5.5 },
+      { "st_id": "NV", "st_sum": 30, "st_avg": 7.5 }
+    ]
+  },
+  {
+    "s_id": 11,
+    "s_pid": 1,
+    "s_year": 2020,
+    "s_city": [
+      { "st_id": "SFO", "ct_sum": 20, "ct_avg": 10.0 },
+      { "st_id": "SJC", "ct_sum": 22, "ct_avg": 11.0 },
+      { "st_id": "RNO", "ct_sum": 24, "ct_avg": 12.0 },
+      { "st_id": "LAS", "ct_sum": 26, "ct_avg": 13.0 }
+    ],
+    "s_state": [
+      { "st_id": "CA", "st_sum": 42, "st_avg": 10.5 },
+      { "st_id": "NV", "st_sum": 50, "st_avg": 12.5 }
+    ]
+  }
+]);
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-nested-01/gby-nested-01.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-nested-01/gby-nested-01.3.query.sqlpp
new file mode 100644
index 0000000..f417383
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/gby-nested-01/gby-nested-01.3.query.sqlpp
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+USE test;
+
+SELECT p.p_id, p.p_name,
+   (
+     SELECT c.ct_id,
+       SUM(c.ct_sum) AS ct_sum,
+       SUM(c.ct_avg) AS ct_avg
+     FROM scs, scs.s_city AS c
+     GROUP BY c.ct_id
+     ORDER BY c.ct_id
+   ) AS ct,
+   (
+     SELECT s.st_id,
+       SUM(s.st_sum) AS st_sum,
+       SUM(s.st_avg) AS st_avg
+     FROM scs, scs.s_state AS s
+     GROUP BY s.st_id
+     ORDER BY s.st_id
+   ) AS st
+FROM prd AS p
+LET scs = (
+   SELECT s.s_city, s.s_state
+   FROM sls s
+   WHERE p.p_id = s.s_pid
+)
+ORDER BY p.p_id
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-nested-01/gby-nested-01.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-nested-01/gby-nested-01.3.adm
new file mode 100644
index 0000000..caba454
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/gby-nested-01/gby-nested-01.3.adm
@@ -0,0 +1,2 @@
+{ "p_id": 1, "ct": [ { "ct_sum": 92, "ct_avg": 46.0 }, { "ct_id": "LAS", "ct_sum": 16, "ct_avg": 8.0 }, { "ct_id": "RNO", "ct_sum": 14, "ct_avg": 7.0 }, { "ct_id": "SFO", "ct_sum": 10, "ct_avg": 5.0 }, { "ct_id": "SJC", "ct_sum": 12, "ct_avg": 6.0 } ], "st": [ { "st_id": "CA", "st_sum": 64, "st_avg": 16.0 }, { "st_id": "NV", "st_sum": 80, "st_avg": 20.0 } ], "p_name": "PRODUCT_1" }
+{ "p_id": 2, "ct": [  ], "st": [  ], "p_name": "PRODUCT_2" }
\ No newline at end of file
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 bed1a09..fc5ca89 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -5206,6 +5206,11 @@
       </compilation-unit>
     </test-case>
     <test-case FilePath="group-by">
+      <compilation-unit name="gby-nested-01">
+        <output-dir compare="Text">gby-nested-01</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="group-by">
       <compilation-unit name="gby-record">
         <output-dir compare="Text">gby-record</output-dir>
       </compilation-unit>
diff --git a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/AbstractIntroduceGroupByCombinerRule.java b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/AbstractIntroduceGroupByCombinerRule.java
index d9746b2..6c42929 100644
--- a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/AbstractIntroduceGroupByCombinerRule.java
+++ b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/AbstractIntroduceGroupByCombinerRule.java
@@ -171,6 +171,9 @@
 
         // Nothing is pushed.
         if (bi.modifyGbyMap.isEmpty()) {
+            // Reset the group-by operator with original nested plans.
+            gbyNestedPlans.clear();
+            gbyNestedPlans.addAll(backupNestedPlans);
             return null;
         }
 
@@ -188,6 +191,9 @@
                         LogicalVariable v2 = newOpGbyList.get(i);
                         if (v != v2) {
                             // cannot linearize
+                            // Reset the group-by operator with original nested plans.
+                            gbyNestedPlans.clear();
+                            gbyNestedPlans.addAll(backupNestedPlans);
                             return null;
                         }
                     } else {