[NO ISSUE][COMP] Range scan on a prefix of a composite key

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

Details:
- Fixed an issue with range scan on a prefix of a composite
  key not using both bounds to probe the index

Change-Id: I35fb979191b254857211da5c0ec28bae4a370ba8
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/6124
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
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/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
index 48b027c..6b045ea 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
@@ -381,20 +381,18 @@
                     }
 
                     // Check if any field name in the optFuncExpr matches.
-                    if (optFuncExpr.findFieldName(keyField) != -1) {
-                        foundKeyField =
-                                typeMatch && optFuncExpr.getOperatorSubTree(exprAndVarIdx.second).hasDataSourceScan();
-                        if (foundKeyField) {
-                            matchedExpressions.add(exprAndVarIdx.first);
-                            numMatchedKeys++;
-                            if (lastFieldMatched == i - 1) {
-                                lastFieldMatched = i;
-                            }
-                            break;
-                        }
+                    if (typeMatch && optFuncExpr.findFieldName(keyField) != -1
+                            && optFuncExpr.getOperatorSubTree(exprAndVarIdx.second).hasDataSourceScan()) {
+                        foundKeyField = true;
+                        matchedExpressions.add(exprAndVarIdx.first);
                     }
                 }
-                if (!foundKeyField) {
+                if (foundKeyField) {
+                    numMatchedKeys++;
+                    if (lastFieldMatched == i - 1) {
+                        lastFieldMatched = i;
+                    }
+                } else {
                     allUsed = false;
                     // if any expression was matched, remove the non-matched expressions, otherwise the index is unusable
                     if (lastFieldMatched >= 0) {
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/queries/composite-key/composite-prefix-low-high.sqlpp b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/composite-key/composite-prefix-low-high.sqlpp
new file mode 100644
index 0000000..1de056e
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/queries/composite-key/composite-prefix-low-high.sqlpp
@@ -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.
+ */
+
+/*
+ * Description  : This test case is to verify composite key prefix search with range
+ * Expected Res : Success
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+
+use test;
+
+create type PointType as closed {
+x:int,
+y:int,
+z:int
+};
+
+create dataset Points(PointType) primary key x, y;
+
+select x, y, z
+from Points
+where x between 2 and 3
+order by x, y;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/optimizerts/results/composite-key/composite-prefix-low-high.plan b/asterixdb/asterix-app/src/test/resources/optimizerts/results/composite-key/composite-prefix-low-high.plan
new file mode 100644
index 0000000..5b71c1c
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/optimizerts/results/composite-key/composite-prefix-low-high.plan
@@ -0,0 +1,9 @@
+-- DISTRIBUTE_RESULT  |PARTITIONED|
+  -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+    -- STREAM_PROJECT  |PARTITIONED|
+      -- ASSIGN  |PARTITIONED|
+        -- SORT_MERGE_EXCHANGE [$$24(ASC), $$25(ASC) ]  |PARTITIONED|
+          -- BTREE_SEARCH  |PARTITIONED|
+            -- ONE_TO_ONE_EXCHANGE  |PARTITIONED|
+              -- ASSIGN  |PARTITIONED|
+                -- EMPTY_TUPLE_SOURCE  |PARTITIONED|
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/composite-key/composite-prefix-low-high/composite-prefix-low-high.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/composite-key/composite-prefix-low-high/composite-prefix-low-high.1.ddl.sqlpp
new file mode 100644
index 0000000..d42eb84
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/composite-key/composite-prefix-low-high/composite-prefix-low-high.1.ddl.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.
+ */
+
+/*
+ * Description  : This test case is to verify composite key prefix search with range
+ * Expected Res : Success
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+
+use test;
+
+create type PointType as closed {
+  x:int,
+  y:int,
+  z:int
+};
+
+create dataset Points(PointType) primary key x, y;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/composite-key/composite-prefix-low-high/composite-prefix-low-high.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/composite-key/composite-prefix-low-high/composite-prefix-low-high.2.update.sqlpp
new file mode 100644
index 0000000..75b03db
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/composite-key/composite-prefix-low-high/composite-prefix-low-high.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;
+
+insert into Points {"x": 1, "y": 1, "z": 100};
+insert into Points {"x": 2, "y": 1, "z": 101};
+insert into Points {"x": 2, "y": 2, "z": 102};
+insert into Points {"x": 2, "y": 3, "z": 103};
+insert into Points {"x": 2, "y": 4, "z": 104};
+insert into Points {"x": 2, "y": 5, "z": 105};
+insert into Points {"x": 3, "y": 1, "z": 106};
+insert into Points {"x": 3, "y": 2, "z": 107};
+insert into Points {"x": 4, "y": 1, "z": 108};
+insert into Points {"x": 4, "y": 9, "z": 109};
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/composite-key/composite-prefix-low-high/composite-prefix-low-high.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/composite-key/composite-prefix-low-high/composite-prefix-low-high.3.query.sqlpp
new file mode 100644
index 0000000..3de9b3a
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/composite-key/composite-prefix-low-high/composite-prefix-low-high.3.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 x, y, z
+from Points
+where x between 2 and 3
+order by x, y;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/composite-key/composite-prefix-low-high/composite-prefix-low-high.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/composite-key/composite-prefix-low-high/composite-prefix-low-high.3.adm
new file mode 100644
index 0000000..6fd8edb
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/composite-key/composite-prefix-low-high/composite-prefix-low-high.3.adm
@@ -0,0 +1,7 @@
+{ "x": 2, "y": 1, "z": 101 }
+{ "x": 2, "y": 2, "z": 102 }
+{ "x": 2, "y": 3, "z": 103 }
+{ "x": 2, "y": 4, "z": 104 }
+{ "x": 2, "y": 5, "z": 105 }
+{ "x": 3, "y": 1, "z": 106 }
+{ "x": 3, "y": 2, "z": 107 }
\ 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 fc7372e..5d0f639 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -13392,6 +13392,11 @@
         <output-dir compare="Text">composite-prefix</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="composite-key">
+      <compilation-unit name="composite-prefix-low-high">
+        <output-dir compare="Text">composite-prefix-low-high</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="limit">
     <test-case FilePath="limit">