ASTERIXDB-865: fix query compilation for if-else expression.

Change-Id: I80e7995e814180fe567818263c79493901af284c
Reviewed-on: https://asterix-gerrit.ics.uci.edu/703
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Till Westmann <tillw@apache.org>
diff --git a/asterix-app/src/test/resources/optimizerts/queries/query-ASTERIXDB-1203.aql b/asterix-app/src/test/resources/optimizerts/queries/query-ASTERIXDB-1203.aql
new file mode 100644
index 0000000..c3944b1
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/query-ASTERIXDB-1203.aql
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+
+/* This test is to verify the fix for ASTERIXDB-1203. */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type ifType as open
+{
+    id: int32
+}
+
+create dataset ifds(ifType)
+primary key id;
+
+for $x in dataset ifds
+where (
+if ($x.names.count = "1") then
+   $x.names.name.firstName = "Tom"
+else
+   (some $v in $x.names.name satisfies $v.firstName = "Tom")
+)
+return $x;
diff --git a/asterix-app/src/test/resources/optimizerts/queries/query-ASTERIXDB-865.aql b/asterix-app/src/test/resources/optimizerts/queries/query-ASTERIXDB-865.aql
new file mode 100644
index 0000000..185e74b
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/queries/query-ASTERIXDB-865.aql
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+/* This test is to verify the fix for ASTERIXDB-865. */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type page_info_type as open {}
+
+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;
+
+for $t in dataset page_views
+let $header := if ($t.action = 1)
+    then [ $t.page_info.a ]
+    else
+        for $pl in $t.page_links
+        return $pl.b
+for $h in $header
+return
+{ "user": $t.user, "header": $h }
+;
diff --git a/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1203.plan b/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1203.plan
new file mode 100644
index 0000000..9dc7a8a
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-1203.plan
@@ -0,0 +1,36 @@
+-- DISTRIBUTE_RESULT  |PARTITIONED|
+  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+    -- STREAM_PROJECT  |PARTITIONED|
+      -- STREAM_SELECT  |PARTITIONED|
+        -- STREAM_PROJECT  |PARTITIONED|
+          -- UNNEST  |PARTITIONED|
+            -- STREAM_PROJECT  |PARTITIONED|
+              -- ASSIGN  |PARTITIONED|
+                -- SUBPLAN  |PARTITIONED|
+                        {
+                          -- AGGREGATE  |LOCAL|
+                            -- AGGREGATE  |LOCAL|
+                              -- STREAM_SELECT  |LOCAL|
+                                -- ASSIGN  |LOCAL|
+                                  -- UNNEST  |LOCAL|
+                                    -- ASSIGN  |LOCAL|
+                                      -- ASSIGN  |LOCAL|
+                                        -- STREAM_SELECT  |LOCAL|
+                                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                        }
+                  -- SUBPLAN  |PARTITIONED|
+                          {
+                            -- AGGREGATE  |LOCAL|
+                              -- ASSIGN  |LOCAL|
+                                -- ASSIGN  |LOCAL|
+                                  -- ASSIGN  |LOCAL|
+                                    -- ASSIGN  |LOCAL|
+                                      -- STREAM_SELECT  |LOCAL|
+                                        -- NESTED_TUPLE_SOURCE  |LOCAL|
+                          }
+                    -- ASSIGN  |PARTITIONED|
+                      -- STREAM_PROJECT  |PARTITIONED|
+                        -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                          -- DATASOURCE_SCAN  |PARTITIONED|
+                            -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                              -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-865.plan b/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-865.plan
new file mode 100644
index 0000000..6ff5cee
--- /dev/null
+++ b/asterix-app/src/test/resources/optimizerts/results/query-ASTERIXDB-865.plan
@@ -0,0 +1,35 @@
+-- DISTRIBUTE_RESULT  |PARTITIONED|
+  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+    -- STREAM_PROJECT  |PARTITIONED|
+      -- ASSIGN  |PARTITIONED|
+        -- STREAM_PROJECT  |PARTITIONED|
+          -- UNNEST  |PARTITIONED|
+            -- STREAM_PROJECT  |PARTITIONED|
+              -- UNNEST  |PARTITIONED|
+                -- STREAM_PROJECT  |PARTITIONED|
+                  -- ASSIGN  |PARTITIONED|
+                    -- STREAM_PROJECT  |PARTITIONED|
+                      -- SUBPLAN  |PARTITIONED|
+                              {
+                                -- AGGREGATE  |LOCAL|
+                                  -- AGGREGATE  |LOCAL|
+                                    -- ASSIGN  |LOCAL|
+                                      -- UNNEST  |LOCAL|
+                                        -- ASSIGN  |LOCAL|
+                                          -- STREAM_SELECT  |LOCAL|
+                                            -- NESTED_TUPLE_SOURCE  |LOCAL|
+                              }
+                        -- SUBPLAN  |PARTITIONED|
+                                {
+                                  -- AGGREGATE  |LOCAL|
+                                    -- ASSIGN  |LOCAL|
+                                      -- ASSIGN  |LOCAL|
+                                        -- ASSIGN  |LOCAL|
+                                          -- STREAM_SELECT  |LOCAL|
+                                            -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                }
+                          -- ASSIGN  |PARTITIONED|
+                            -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                              -- DATASOURCE_SCAN  |PARTITIONED|
+                                -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+                                  -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
diff --git a/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
index ebbee79..3664ed8 100644
--- a/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
+++ b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-1.plan
@@ -23,23 +23,25 @@
                                 -- ASSIGN  |UNPARTITIONED|
                                   -- STREAM_PROJECT  |UNPARTITIONED|
                                     -- UNNEST  |UNPARTITIONED|
-                                      -- SUBPLAN  |UNPARTITIONED|
-                                              {
-                                                -- SUBPLAN  |UNPARTITIONED|
-                                                        {
-                                                          -- AGGREGATE  |UNPARTITIONED|
-                                                            -- STREAM_SELECT  |UNPARTITIONED|
-                                                              -- UNNEST  |UNPARTITIONED|
-                                                                -- SUBPLAN  |UNPARTITIONED|
-                                                                        {
-                                                                          -- AGGREGATE  |UNPARTITIONED|
-                                                                            -- IN_MEMORY_STABLE_SORT [$$21(ASC)]  |UNPARTITIONED|
-                                                                              -- UNNEST  |UNPARTITIONED|
-                                                                                -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
-                                                                        }
-                                                                  -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
-                                                        }
-                                                  -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
-                                              }
-                                        -- ASSIGN  |UNPARTITIONED|
-                                          -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
+                                      -- STREAM_PROJECT  |UNPARTITIONED|
+                                        -- SUBPLAN  |UNPARTITIONED|
+                                                {
+                                                  -- ASSIGN  |UNPARTITIONED|
+                                                    -- SUBPLAN  |UNPARTITIONED|
+                                                            {
+                                                              -- AGGREGATE  |UNPARTITIONED|
+                                                                -- STREAM_SELECT  |UNPARTITIONED|
+                                                                  -- UNNEST  |UNPARTITIONED|
+                                                                    -- SUBPLAN  |UNPARTITIONED|
+                                                                            {
+                                                                              -- AGGREGATE  |UNPARTITIONED|
+                                                                                -- IN_MEMORY_STABLE_SORT [$$21(ASC)]  |UNPARTITIONED|
+                                                                                  -- UNNEST  |UNPARTITIONED|
+                                                                                    -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                                                                            }
+                                                                      -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                                                            }
+                                                      -- NESTED_TUPLE_SOURCE  |UNPARTITIONED|
+                                                }
+                                          -- ASSIGN  |UNPARTITIONED|
+                                            -- EMPTY_TUPLE_SOURCE  |UNPARTITIONED|
diff --git a/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
index a6e7496..caf4c48 100644
--- a/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
+++ b/asterix-app/src/test/resources/optimizerts/results/udfs/query-ASTERIXDB-1308-2.plan
@@ -9,22 +9,23 @@
                             {
                               -- SUBPLAN  |LOCAL|
                                       {
-                                        -- SUBPLAN  |LOCAL|
-                                                {
-                                                  -- AGGREGATE  |LOCAL|
-                                                    -- STREAM_SELECT  |LOCAL|
-                                                      -- ASSIGN  |LOCAL|
-                                                        -- UNNEST  |LOCAL|
-                                                          -- SUBPLAN  |LOCAL|
-                                                                  {
-                                                                    -- AGGREGATE  |LOCAL|
-                                                                      -- IN_MEMORY_STABLE_SORT [$$38(ASC)]  |LOCAL|
-                                                                        -- UNNEST  |LOCAL|
-                                                                          -- NESTED_TUPLE_SOURCE  |LOCAL|
-                                                                  }
-                                                            -- NESTED_TUPLE_SOURCE  |LOCAL|
-                                                }
-                                          -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                        -- ASSIGN  |LOCAL|
+                                          -- SUBPLAN  |LOCAL|
+                                                  {
+                                                    -- AGGREGATE  |LOCAL|
+                                                      -- STREAM_SELECT  |LOCAL|
+                                                        -- ASSIGN  |LOCAL|
+                                                          -- UNNEST  |LOCAL|
+                                                            -- SUBPLAN  |LOCAL|
+                                                                    {
+                                                                      -- AGGREGATE  |LOCAL|
+                                                                        -- IN_MEMORY_STABLE_SORT [$$38(ASC)]  |LOCAL|
+                                                                          -- UNNEST  |LOCAL|
+                                                                            -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                                                    }
+                                                              -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                                  }
+                                            -- NESTED_TUPLE_SOURCE  |LOCAL|
                                       }
                                 -- SUBPLAN  |LOCAL|
                                         {
@@ -38,22 +39,23 @@
                             }
                       -- SUBPLAN  |LOCAL|
                               {
-                                -- SUBPLAN  |LOCAL|
-                                        {
-                                          -- AGGREGATE  |LOCAL|
-                                            -- STREAM_SELECT  |LOCAL|
-                                              -- ASSIGN  |LOCAL|
-                                                -- UNNEST  |LOCAL|
-                                                  -- SUBPLAN  |LOCAL|
-                                                          {
-                                                            -- AGGREGATE  |LOCAL|
-                                                              -- IN_MEMORY_STABLE_SORT [$$31(ASC)]  |LOCAL|
-                                                                -- UNNEST  |LOCAL|
-                                                                  -- NESTED_TUPLE_SOURCE  |LOCAL|
-                                                          }
-                                                    -- NESTED_TUPLE_SOURCE  |LOCAL|
-                                        }
-                                  -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                -- ASSIGN  |LOCAL|
+                                  -- SUBPLAN  |LOCAL|
+                                          {
+                                            -- AGGREGATE  |LOCAL|
+                                              -- STREAM_SELECT  |LOCAL|
+                                                -- ASSIGN  |LOCAL|
+                                                  -- UNNEST  |LOCAL|
+                                                    -- SUBPLAN  |LOCAL|
+                                                            {
+                                                              -- AGGREGATE  |LOCAL|
+                                                                -- IN_MEMORY_STABLE_SORT [$$31(ASC)]  |LOCAL|
+                                                                  -- UNNEST  |LOCAL|
+                                                                    -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                                            }
+                                                      -- NESTED_TUPLE_SOURCE  |LOCAL|
+                                          }
+                                    -- NESTED_TUPLE_SOURCE  |LOCAL|
                               }
                         -- NESTED_TUPLE_SOURCE  |LOCAL|
                   }
diff --git a/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.1.ddl.aql
new file mode 100644
index 0000000..44468b1
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.1.ddl.aql
@@ -0,0 +1,32 @@
+/*
+ * 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 ifType as open
+{
+    id: int32
+}
+
+create dataset ifds(ifType)
+primary key id;
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.2.update.aql
new file mode 100644
index 0000000..66817ec
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.2.update.aql
@@ -0,0 +1,52 @@
+/*
+ * 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;
+
+insert into dataset ifds(
+{
+    "id":0,
+    "names":{
+        "count": "1",
+        "name" :{
+            "firstName" : "Tom",
+            "lastName" : "Smith"
+        }
+    }
+}
+)
+
+insert into dataset ifds(
+{
+    "id":1,
+    "names":{
+        "count": "2",
+        "name" :[
+        {
+            "firstName" : "Tom",
+            "lastName" : "Smith"
+        },
+        {
+            "firstName" : "Bob",
+            "lastName" : "Jones"
+        }
+        ]
+    }
+}
+)
diff --git a/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.3.query.aql
new file mode 100644
index 0000000..9a27a1d
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.3.query.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.
+ */
+
+/* This test is to verify the fix for ASTERIXDB-1203. */
+
+use dataverse test;
+
+for $x in dataset ifds
+where (
+if ($x.names.count = "1") then
+   $x.names.name.firstName = "Tom"
+else
+   (some $v in $x.names.name satisfies $v.firstName = "Tom")
+)
+return $x;
diff --git a/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.1.ddl.aql
new file mode 100644
index 0000000..bbb9c2a
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.1.ddl.aql
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type page_info_type as open {}
+
+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;
diff --git a/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.2.update.aql
new file mode 100644
index 0000000..e42334b
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.2.update.aql
@@ -0,0 +1,23 @@
+/*
+ * 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"));
diff --git a/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.3.query.aql
new file mode 100644
index 0000000..f7165ed
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.3.query.aql
@@ -0,0 +1,35 @@
+/*
+ * 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.
+ */
+
+/* This test is to verify the fix for ASTERIXDB-865. */
+
+use dataverse test;
+
+for $t in dataset page_views
+let $header := if ($t.action = 1)
+    then [ $t.page_info.a ]
+    else
+        for $pl in $t.page_links
+        return $pl.b
+for $h in $header
+order by $t.user, $h
+return
+{ "user": $t.user, "header": $h }
+;
+
diff --git a/asterix-app/src/test/resources/runtimets/results/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.1.adm b/asterix-app/src/test/resources/runtimets/results/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.1.adm
new file mode 100644
index 0000000..373b4c7
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/misc/query-ASTERIXDB-1203/query-ASTERIXDB-1203.1.adm
@@ -0,0 +1,2 @@
+{ "id": 1i32, "names": { "count": "2", "name": [ { "firstName": "Tom", "lastName": "Smith" }, { "firstName": "Bob", "lastName": "Jones" } ] } }
+{ "id": 0i32, "names": { "count": "1", "name": { "firstName": "Tom", "lastName": "Smith" } } }
diff --git a/asterix-app/src/test/resources/runtimets/results/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.1.adm b/asterix-app/src/test/resources/runtimets/results/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.1.adm
new file mode 100644
index 0000000..204e1b6
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/misc/query-ASTERIXDB-865/query-ASTERIXDB-865.1.adm
@@ -0,0 +1,3 @@
+{ "user": "Bill", "header": null }
+{ "user": "Bill", "header": "bbb" }
+{ "user": "John", "header": "aaa" }
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index 0ac4a1e..9dcd883 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -3058,6 +3058,16 @@
                 <output-dir compare="Text">prefix-search</output-dir>
             </compilation-unit>
         </test-case>
+        <test-case FilePath="misc">
+            <compilation-unit name="query-ASTERIXDB-865">
+                <output-dir compare="Text">query-ASTERIXDB-865</output-dir>
+            </compilation-unit>
+        </test-case>
+        <test-case FilePath="misc">
+            <compilation-unit name="query-ASTERIXDB-1203">
+                <output-dir compare="Text">query-ASTERIXDB-1203</output-dir>
+            </compilation-unit>
+        </test-case>
     </test-group>
     <test-group name="open-index-enforced">
         <test-group FilePath="open-index-enforced/error-checking">