ASTERIXDB-883: fix and a regression test.

Change-Id: I34dbe4e12492337746d630a9b130331abfefe78e
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1059
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.1.ddl.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.1.ddl.aql
new file mode 100644
index 0000000..f13d569
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.1.ddl.aql
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type page_info_type as {
+}
+
+create type page_views_type as closed {
+  user: string,
+  action: int32,
+  timespent: int32,
+  query_term: string,
+  ip_addr: int32,
+  timestamp: int32,
+  estimated_revenue: double,
+  page_info: page_info_type,
+  page_links: {{ page_info_type}}
+}
+
+create dataset page_views(page_views_type) primary key user;
+
+create type tmp_type as closed {
+  id: uuid,
+  groups: [page_views_type]
+}
+
+create dataset tmp(tmp_type) primary key id autogenerated;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.aql
new file mode 100644
index 0000000..aae9127
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.aql
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use dataverse test;
+
+load dataset page_views using localfs
+(("path"="asterix_nc1://data/page_views.adm"),("format"="adm"));
+
+insert into dataset tmp(
+  for $t in dataset page_views
+  group by $t.user with $t
+  return {
+     "groups": $t
+  }
+);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.3.query.aql b/asterixdb/asterix-app/src/test/resources/runtimets/queries/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.3.query.aql
new file mode 100644
index 0000000..62622608
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.3.query.aql
@@ -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 dataverse test;
+
+let $beth :=
+  for $i in dataset tmp
+  let $groups := $i.groups
+  for $item in $groups
+  distinct by $item.user, $item.action
+  return
+  {
+    "user": $item.user,
+    "action": $item.action
+  }
+
+let $rev :=
+  for $i in dataset tmp
+  let $groups := $i.groups
+  for $item in $groups
+  distinct by $item.user, $item.estimated_revenue
+  return
+  {
+     "user": $item.user,
+     "estimated_revenue": $item.estimated_revenue
+  }
+
+let $ts :=
+  for $i in dataset tmp
+  let $groups := $i.groups
+  for $item in $groups
+  distinct by $item.user, $item.timespent
+  return
+  {
+     "user": $item.user,
+     "timespent": $item.timespent
+  }
+
+for $a in $beth
+for $c in $rev
+for $b in $ts
+where $a.user=$b.user and $a.user=$c.user and $b.user=$c.user
+order by $a.user
+return
+{
+  "user": $a.user,
+  "beth": count($beth),
+  "ts":  avg(for $l in $ts return $l.timespent),
+  "rev": sum (for $k in $rev return $k.estimated_revenue)
+}
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.1.ddl.sqlpp
new file mode 100644
index 0000000..68947ea
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.1.ddl.sqlpp
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use test;
+
+create type page_info_type as {
+}
+
+create type page_views_type as closed {
+  user: string,
+  action: int32,
+  timespent: int32,
+  query_term: string,
+  ip_addr: int32,
+  timestamp: int32,
+  estimated_revenue: double,
+  page_info: page_info_type,
+  page_links: {{ page_info_type}}
+}
+
+create dataset page_views(page_views_type) primary key user;
+
+create type tmp_type as closed {
+  id: uuid,
+  groups: [page_views_type]
+}
+
+create dataset tmp(tmp_type) primary key id autogenerated;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.sqlpp
new file mode 100644
index 0000000..f288510
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.2.update.sqlpp
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use test;
+
+load dataset page_views using localfs
+(("path"="asterix_nc1://data/page_views.adm"),("format"="adm"));
+
+insert into tmp(
+  from page_views as t
+  group by t.user
+  select value {
+     "groups": t
+  }
+);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.3.query.sqlpp
new file mode 100644
index 0000000..aab13a4
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.3.query.sqlpp
@@ -0,0 +1,61 @@
+/*
+ * 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;
+
+WITH beth AS(
+  FROM tmp AS i, i.groups AS item
+  SELECT DISTINCT VALUE
+  {
+    "user": item.user,
+    "action": item.action
+  }
+)
+,
+rev AS(
+  FROM tmp i, i.groups AS item
+  SELECT DISTINCT VALUE
+  {
+     "user": item.user,
+     "estimated_revenue":  item.estimated_revenue
+  }
+)
+,
+ts AS (
+  FROM tmp i, i.groups AS item
+  SELECT DISTINCT VALUE
+  {
+     "user": item.user,
+     "timespent": item.timespent
+  }
+)
+
+FROM beth AS a,
+     ts AS b,
+     rev AS c
+WHERE a.user=b.user AND a.user=c.user AND b.user=c.user
+SELECT VALUE
+{
+   "user": a.user,
+   "beth": coll_count(beth),
+   "ts": coll_avg( ( FROM ts AS l SELECT VALUE l.timespent ) ),
+   "rev": coll_sum( ( FROM rev AS k SELECT VALUE k.estimated_revenue ) )
+}
+ORDER BY a.user;
+
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.1.adm
new file mode 100644
index 0000000..5fa6e48
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/flwor/query-ASTERIXDB-883/query-ASTERIXDB-883.1.adm
@@ -0,0 +1,2 @@
+{ "user": "Bill", "beth": 2, "ts": 2.0, "rev": 10.0 }
+{ "user": "John", "beth": 2, "ts": 2.0, "rev": 10.0 }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml
index 6e9e7ab..0d05c31 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -343,6 +343,11 @@
         <output-dir compare="Text">query-issue550</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="flwor">
+      <compilation-unit name="query-ASTERIXDB-883">
+        <output-dir compare="Text">query-ASTERIXDB-883</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="union">
     <test-case FilePath="union">
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 d392dbd..7d2af51 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -62,6 +62,11 @@
         <output-dir compare="Text">query-issue550</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="flwor">
+      <compilation-unit name="query-ASTERIXDB-883">
+        <output-dir compare="Text">query-ASTERIXDB-883</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="explain">
     <test-case FilePath="explain">
diff --git a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ComplexUnnestToProductRule.java b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ComplexUnnestToProductRule.java
index 270d8e4..2ab8520 100644
--- a/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ComplexUnnestToProductRule.java
+++ b/hyracks-fullstack/algebricks/algebricks-rewriter/src/main/java/org/apache/hyracks/algebricks/rewriter/rules/ComplexUnnestToProductRule.java
@@ -92,11 +92,12 @@
         // If we found a join, simply use it as the outer root.
         if (unnestOrJoin.getOperatorTag() != LogicalOperatorTag.INNERJOIN
                 && unnestOrJoin.getOperatorTag() != LogicalOperatorTag.LEFTOUTERJOIN) {
-            // We've found a second unnest. First, sanity check that the unnest does not produce any vars that are used by the plan above (until the first unnest).
-            List<LogicalVariable> producedVars = new ArrayList<LogicalVariable>();
-            VariableUtilities.getProducedVariables(unnestOrJoin, producedVars);
-            for (LogicalVariable producedVar : producedVars) {
-                if (innerUsedVars.contains(producedVar)) {
+            // We've found a second unnest. First, sanity check that the unnest does not output any live variables
+            // that are used by the plan above (until the first unnest).
+            List<LogicalVariable> liveVars = new ArrayList<>();
+            VariableUtilities.getLiveVariables(unnestOrJoin, liveVars);
+            for (LogicalVariable liveVar : liveVars) {
+                if (innerUsedVars.contains(liveVar)) {
                     return false;
                 }
             }