[NO ISSUE] Fix subqueries for IndexAccessor syntax

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

Details:
- Fixed subqueries for IndexAccessor syntax.
- Added test cases for function and subquery calls as arguments.

Change-Id: Iba16c6c04a526aec117ca3adcb168cdd4ba916a8
Reviewed-on: https://asterix-gerrit.ics.uci.edu/3091
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Dmitry Lychagin <dmitry.lychagin@couchbase.com>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
index 6d70ba5..c231fdf 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/LangExpressionToPlanTranslator.java
@@ -728,23 +728,34 @@
     public Pair<ILogicalOperator, LogicalVariable> visit(IndexAccessor ia, Mutable<ILogicalOperator> tupSource)
             throws CompilationException {
         SourceLocation sourceLoc = ia.getSourceLocation();
-        Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = langExprToAlgExpression(ia.getExpr(), tupSource);
+
+        // Expression pair
+        Pair<ILogicalExpression, Mutable<ILogicalOperator>> expressionPair =
+                langExprToAlgExpression(ia.getExpr(), tupSource);
         LogicalVariable v = context.newVar();
         AbstractFunctionCallExpression f;
+
+        // Index expression
+        Pair<ILogicalExpression, Mutable<ILogicalOperator>> indexPair = null;
+
         if (ia.isAny()) {
             f = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.ANY_COLLECTION_MEMBER));
-            f.getArguments().add(new MutableObject<>(p.first));
-            f.setSourceLocation(sourceLoc);
+            f.getArguments().add(new MutableObject<>(expressionPair.first));
         } else {
-            Pair<ILogicalExpression, Mutable<ILogicalOperator>> indexPair =
-                    langExprToAlgExpression(ia.getIndexExpr(), tupSource);
+            indexPair = langExprToAlgExpression(ia.getIndexExpr(), expressionPair.second);
             f = new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.GET_ITEM));
-            f.getArguments().add(new MutableObject<>(p.first));
+            f.getArguments().add(new MutableObject<>(expressionPair.first));
             f.getArguments().add(new MutableObject<>(indexPair.first));
-            f.setSourceLocation(sourceLoc);
         }
+
+        f.setSourceLocation(sourceLoc);
         AssignOperator a = new AssignOperator(v, new MutableObject<>(f));
-        a.getInputs().add(p.second);
+
+        if (ia.isAny()) {
+            a.getInputs().add(expressionPair.second);
+        } else {
+            a.getInputs().add(indexPair.second); // NOSONAR: Called only if value exists
+        }
         a.setSourceLocation(sourceLoc);
         return new Pair<>(a, v);
     }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/list/get-item_03/get-item_03.1.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/list/get-item_03/get-item_03.1.ddl.sqlpp
new file mode 100644
index 0000000..f5237ec
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/list/get-item_03/get-item_03.1.ddl.sqlpp
@@ -0,0 +1,34 @@
+/*
+ * 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;
+
+drop type testType if exists;
+create type testType as open {
+id: int64,
+groupId: int64,
+name: string,
+arrayItems: [int64],
+multisetItems: {{ int64 }}
+};
+
+drop dataset test if exists;
+create dataset test(testType) primary key id;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/list/get-item_03/get-item_03.2.update.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/list/get-item_03/get-item_03.2.update.sqlpp
new file mode 100644
index 0000000..12a761d
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/list/get-item_03/get-item_03.2.update.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.
+ */
+
+use test;
+
+insert into test.test([
+{"id": 1, "groupId": 1, "name": "Name value", "arrayItems": [1, 2, 3], "multisetItems": {{1, 2, 3}}},
+{"id": 2, "groupId": 1, "name": "Name value", "arrayItems": [1, 2, 3, 4], "multisetItems": {{1, 2, 3, 4}}, "openArrayItems": null},
+{"id": 3, "groupId": 2, "name": "Name value", "arrayItems": [1, 2, 3, 4, 5], "multisetItems": {{1, 2, 3, 4, 5}}, "openArrayItems": [1, 2, 3]}
+]);
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/list/get-item_03/get-item_03.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/list/get-item_03/get-item_03.3.query.sqlpp
new file mode 100644
index 0000000..8bd3ef1
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/list/get-item_03/get-item_03.3.query.sqlpp
@@ -0,0 +1,29 @@
+/*
+ * 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;
+
+use test;
+{
+"t1": [1, 2, 3][int32("0")],
+"t2": [1, 2, 3][double("0")],
+"t3": [1, 2, 3][[0, 1][0]],
+"t4": [1, 2, 3][(select value 2)[0]],
+"t5": [1, 2, 3][(select value id from test where id = 1)[0]]
+};
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/list/get-item_03/get-item_03.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/list/get-item_03/get-item_03.1.adm
new file mode 100644
index 0000000..e4a212f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/list/get-item_03/get-item_03.1.adm
@@ -0,0 +1,2 @@
+{ "t1": 1, "t2": 1, "t3": 1, "t4": 3, "t5": 2 }
+
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 ad08081..9efdcbb 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -4621,6 +4621,11 @@
       </compilation-unit>
     </test-case>
     <test-case FilePath="list">
+      <compilation-unit name="get-item_03">
+        <output-dir compare="Text">get-item_03</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="list">
       <compilation-unit name="len_01">
         <output-dir compare="Text">len_01</output-dir>
       </compilation-unit>
diff --git a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/visitor/GatherFunctionCallsVisitor.java b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/visitor/GatherFunctionCallsVisitor.java
index 3d149fb..625d3e0c 100644
--- a/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/visitor/GatherFunctionCallsVisitor.java
+++ b/asterixdb/asterix-lang-common/src/main/java/org/apache/asterix/lang/common/visitor/GatherFunctionCallsVisitor.java
@@ -91,6 +91,11 @@
     @Override
     public Void visit(IndexAccessor ia, Void arg) throws CompilationException {
         ia.getExpr().accept(this, arg);
+
+        if (!ia.isAny()) {
+            ia.getIndexExpr().accept(this, arg);
+        }
+
         return null;
     }
 
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/CheckSql92AggregateVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/CheckSql92AggregateVisitor.java
index 6ea21a6..daf2ded 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/CheckSql92AggregateVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/visitor/CheckSql92AggregateVisitor.java
@@ -118,7 +118,15 @@
 
     @Override
     public Boolean visit(IndexAccessor ia, ILangExpression parentSelectBlock) throws CompilationException {
-        return ia.getExpr().accept(this, parentSelectBlock);
+        if (ia.getExpr().accept(this, parentSelectBlock)) {
+            return true;
+        }
+
+        if (!ia.isAny() && ia.getIndexExpr().accept(this, parentSelectBlock)) {
+            return true;
+        }
+
+        return false;
     }
 
     @Override