ASTERIXDB-1231, ASTERIXDB-636: fixed Self-join

 - Fixed self-join with index-out-of-boud exception during the compilation
 - Added a test case for ASTERIXDB-636

Change-Id: I8d5d9cb0cb54473fbe7a5e43934e9608548c1dbb
Reviewed-on: https://asterix-gerrit.ics.uci.edu/635
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
diff --git a/asterix-app/src/test/resources/runtimets/queries/orderby_limit/orderby_limit_02/orderby_limit_02.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/orderby_limit/orderby_limit_02/orderby_limit_02.1.ddl.aql
new file mode 100644
index 0000000..33722e7
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/orderby_limit/orderby_limit_02/orderby_limit_02.1.ddl.aql
@@ -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.
+ */
+
+/*
+ *  Description     : Range search query with LIMIT (and ORDER BY) should work fine.
+ *  Issue           : ASTERIXDB-636
+ *  Expected Result : Success
+ *
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type Emp as open
+{ id : int32, name: string, salary: int32 }
+
+create dataset Employee(Emp) primary key id;
+
+create index idx-02 on Employee(name);
diff --git a/asterix-app/src/test/resources/runtimets/queries/orderby_limit/orderby_limit_02/orderby_limit_02.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/orderby_limit/orderby_limit_02/orderby_limit_02.2.update.aql
new file mode 100644
index 0000000..d0b363f
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/orderby_limit/orderby_limit_02/orderby_limit_02.2.update.aql
@@ -0,0 +1,60 @@
+/*
+ * 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.
+ */
+
+/*
+ *  Description     : Range search query with LIMIT (and ORDER BY) should work fine.
+ *  Issue           : ASTERIXDB-636
+ *  Expected Result : Success
+ *
+ */
+
+
+use dataverse test;
+
+insert into dataset Employee (
+    {"id":123,"name":"Kevin","salary":10000}
+);
+
+insert into dataset Employee (
+    {"id":13,"name":"John","salary":5000}
+);
+
+insert into dataset Employee (
+    {"id":23,"name":"Susan","salary":7500}
+);
+
+insert into dataset Employee (
+    {"id":12,"name":"Smith","salary":4000}
+);
+
+insert into dataset Employee (
+    {"id":113,"name":"Roger","salary":8000}
+);
+
+insert into dataset Employee (
+    {"id":143,"name":"Raj","salary":6000}
+);
+
+insert into dataset Employee (
+    {"id":149,"name":"Ramesh","salary":5000}
+);
+
+insert into dataset Employee (
+    {"id":240,"name":"Ravi","salary":6500}
+);
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/orderby_limit/orderby_limit_02/orderby_limit_02.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/orderby_limit/orderby_limit_02/orderby_limit_02.3.query.aql
new file mode 100644
index 0000000..93288aa
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/orderby_limit/orderby_limit_02/orderby_limit_02.3.query.aql
@@ -0,0 +1,33 @@
+/*
+ * 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.
+ */
+
+/*
+ *  Description     : Range search query with LIMIT (and ORDER BY) should work fine.
+ *  Issue           : ASTERIXDB-636
+ *  Expected Result : Success
+ *
+ */
+
+use dataverse test;
+
+for $l in dataset Employee
+where $l.name >= "A" and $l.name <= "Z"
+limit 5
+order by $l.name desc
+return {"name": $l.name}
diff --git a/asterix-app/src/test/resources/runtimets/results/orderby_limit/orderby_limit_02/orderby_limit_02.1.adm b/asterix-app/src/test/resources/runtimets/results/orderby_limit/orderby_limit_02/orderby_limit_02.1.adm
new file mode 100644
index 0000000..e027c88
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/orderby_limit/orderby_limit_02/orderby_limit_02.1.adm
@@ -0,0 +1,5 @@
+{ "name": "Susan" }
+{ "name": "Smith" }
+{ "name": "Roger" }
+{ "name": "Kevin" }
+{ "name": "John" }
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index 48dcbb4..bcc80ad 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -4306,6 +4306,11 @@
             </compilation-unit>
         </test-case>
         <test-case FilePath="orderby_limit">
+            <compilation-unit name="orderby_limit_02">
+                <output-dir compare="Text">orderby_limit_02</output-dir>
+            </compilation-unit>
+        </test-case>
+        <test-case FilePath="orderby_limit">
             <compilation-unit name="orderby_limit_offset_01">
                 <output-dir compare="Text">orderby_limit_offset_01</output-dir>
             </compilation-unit>