[ASTERIXDB-3301][COMP] NPE for subquery with only SELECT ELEMENT

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

Details:
When a subquery consists of only SELECT ELEMENT, a NullPointerException
is encountered. The exception is due to the fact that the base/input
operator for the SUBPLAN of the subquery starts with a 'null' op that
gets replaced by a NTS op later after translating the subquery. This
works fine, but when the subquery is only a SELECT ELEMENT
expression, there are no intermediate operators translated and therefore
the root op of the subplan tries to reference the input op directly which
is 'null' at the time.

Change-Id: I5b314c014798daf4890f75998138954e3d858445
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17928
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Wail Alkowaileet <wael.y.k@gmail.com>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
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 daa1d2f..dabe327 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
@@ -1776,7 +1776,7 @@
 
     protected Pair<ILogicalOperator, LogicalVariable> aggListifyForSubquery(LogicalVariable var,
             Mutable<ILogicalOperator> opRef, boolean bProject) {
-        SourceLocation sourceLoc = opRef.getValue().getSourceLocation();
+        SourceLocation sourceLoc = opRef.getValue() != null ? opRef.getValue().getSourceLocation() : null;
         AggregateFunctionCallExpression funAgg =
                 BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.LISTIFY, new ArrayList<>());
         funAgg.getArguments().add(new MutableObject<>(new VariableReferenceExpression(var)));
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
index ddabaa0..1d9c57b 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/translator/SqlppExpressionToPlanTranslator.java
@@ -206,7 +206,9 @@
         }
         Pair<ILogicalOperator, LogicalVariable> select =
                 selectExpression.getSelectSetOperation().accept(this, currentOpRef);
-        currentOpRef = new MutableObject<>(select.first);
+        if (select.first != null) {
+            currentOpRef = new MutableObject<>(select.first);
+        }
         if (selectExpression.hasOrderby()) {
             currentOpRef = new MutableObject<>(selectExpression.getOrderbyClause().accept(this, currentOpRef).first);
         }
@@ -768,7 +770,9 @@
         } else {
             ProjectOperator pr = new ProjectOperator(resVar);
             pr.getInputs().add(returnOpRef);
-            pr.setSourceLocation(returnOpRef.getValue().getSourceLocation());
+            if (returnOpRef.getValue() != null) {
+                pr.setSourceLocation(returnOpRef.getValue().getSourceLocation());
+            }
             return new Pair<>(pr, resVar);
         }
     }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.01.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.01.ddl.sqlpp
new file mode 100644
index 0000000..e3c9e55
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.01.ddl.sqlpp
@@ -0,0 +1,28 @@
+/*
+ * 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 OR REPLACE FUNCTION `fun0` (data) { data + 1 };
+CREATE OR REPLACE FUNCTION `fun1` (data) { ( SELECT RAW data ) };
+CREATE OR REPLACE FUNCTION `fun2` (data) { ( SELECT RAW (SELECT RAW data) ) };
+CREATE OR REPLACE FUNCTION `fun3` (data) { ( SELECT data ) };
+CREATE OR REPLACE FUNCTION `fun4` (data) { ( SELECT (SELECT data) AS x) };
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.02.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.02.query.sqlpp
new file mode 100644
index 0000000..0a298b2
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.02.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+FROM [1,2,3] a
+LET test = fun0(a)
+SELECT a, test;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.03.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.03.query.sqlpp
new file mode 100644
index 0000000..d4c60c9
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.03.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+FROM [1,2,3] a
+LET test = fun1(a)
+SELECT a, test;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.04.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.04.query.sqlpp
new file mode 100644
index 0000000..7dc96f0
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.04.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+FROM [1,2,3] a
+LET test = fun2(a)
+SELECT a, test;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.05.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.05.query.sqlpp
new file mode 100644
index 0000000..4e57970
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.05.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+FROM [1,2,3] a
+LET test = fun3(a)
+SELECT a, test;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.06.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.06.query.sqlpp
new file mode 100644
index 0000000..fc0402b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.06.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+FROM [1,2,3] a
+LET test = fun4(a)
+SELECT a, test;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.07.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.07.query.sqlpp
new file mode 100644
index 0000000..60faeb8
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.07.query.sqlpp
@@ -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 test;
+
+FROM [1,2,3] a
+SELECT a, fun0(a);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.08.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.08.query.sqlpp
new file mode 100644
index 0000000..6d470c7
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.08.query.sqlpp
@@ -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 test;
+
+FROM [1,2,3] a
+SELECT a, fun1(a);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.09.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.09.query.sqlpp
new file mode 100644
index 0000000..7c96370
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.09.query.sqlpp
@@ -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 test;
+
+FROM [1,2,3] a
+SELECT a, fun2(a);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.10.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.10.query.sqlpp
new file mode 100644
index 0000000..975b708
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.10.query.sqlpp
@@ -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 test;
+
+FROM [1,2,3] a
+SELECT a, fun3(a);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.11.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.11.query.sqlpp
new file mode 100644
index 0000000..fc50baa
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.11.query.sqlpp
@@ -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 test;
+
+FROM [1,2,3] a
+SELECT a, fun4(a);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.12.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.12.query.sqlpp
new file mode 100644
index 0000000..0c3dc54
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.12.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+FROM [1,2,3] a
+LET test = (SELECT RAW a)
+SELECT a, test;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.13.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.13.query.sqlpp
new file mode 100644
index 0000000..54e82fd
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.13.query.sqlpp
@@ -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 test;
+
+FROM [1,2,3] a
+SELECT a, (SELECT RAW a);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.14.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.14.query.sqlpp
new file mode 100644
index 0000000..4bc8a67
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.14.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+FROM [1,2,3] a
+LET test = ( SELECT RAW (SELECT RAW a) )
+SELECT a, test;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.15.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.15.query.sqlpp
new file mode 100644
index 0000000..cfa7a65
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.15.query.sqlpp
@@ -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 test;
+
+FROM [1,2,3] a
+SELECT a, ( SELECT RAW (SELECT RAW a) );
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.16.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.16.query.sqlpp
new file mode 100644
index 0000000..978b6cb
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.16.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+FROM [1,2,3] a
+LET test = ( (LET x = 6 SELECT RAW a+x) )
+SELECT a, test;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.17.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.17.query.sqlpp
new file mode 100644
index 0000000..392da09
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.17.query.sqlpp
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+FROM [1,2,3] a
+LET test = ( (LET x = random() SELECT RAW a+x) )
+SELECT a, floor(test[0]);
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.99.ddl.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.99.ddl.sqlpp
new file mode 100644
index 0000000..36b2bab
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/subquery/select_element/select_element.99.ddl.sqlpp
@@ -0,0 +1,20 @@
+/*
+ * 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;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.02.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.02.adm
new file mode 100644
index 0000000..f6fb5bd
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.02.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "test": 2 }
+{ "a": 2, "test": 3 }
+{ "a": 3, "test": 4 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.03.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.03.adm
new file mode 100644
index 0000000..ea413fe
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.03.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "test": [ 1 ] }
+{ "a": 2, "test": [ 2 ] }
+{ "a": 3, "test": [ 3 ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.04.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.04.adm
new file mode 100644
index 0000000..2cf7df3
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.04.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "test": [ [ 1 ] ] }
+{ "a": 2, "test": [ [ 2 ] ] }
+{ "a": 3, "test": [ [ 3 ] ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.05.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.05.adm
new file mode 100644
index 0000000..59b19361
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.05.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "test": [ { "data": 1 } ] }
+{ "a": 2, "test": [ { "data": 2 } ] }
+{ "a": 3, "test": [ { "data": 3 } ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.06.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.06.adm
new file mode 100644
index 0000000..3eed37c
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.06.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "test": [ { "x": [ { "data": 1 } ] } ] }
+{ "a": 2, "test": [ { "x": [ { "data": 2 } ] } ] }
+{ "a": 3, "test": [ { "x": [ { "data": 3 } ] } ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.07.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.07.adm
new file mode 100644
index 0000000..944e5a2
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.07.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "$1": 2 }
+{ "a": 2, "$1": 3 }
+{ "a": 3, "$1": 4 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.08.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.08.adm
new file mode 100644
index 0000000..c8f629b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.08.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "$1": [ 1 ] }
+{ "a": 2, "$1": [ 2 ] }
+{ "a": 3, "$1": [ 3 ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.09.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.09.adm
new file mode 100644
index 0000000..325d24f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.09.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "$1": [ [ 1 ] ] }
+{ "a": 2, "$1": [ [ 2 ] ] }
+{ "a": 3, "$1": [ [ 3 ] ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.10.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.10.adm
new file mode 100644
index 0000000..a5ef5cd
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.10.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "$1": [ { "data": 1 } ] }
+{ "a": 2, "$1": [ { "data": 2 } ] }
+{ "a": 3, "$1": [ { "data": 3 } ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.11.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.11.adm
new file mode 100644
index 0000000..1936e65
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.11.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "$1": [ { "x": [ { "data": 1 } ] } ] }
+{ "a": 2, "$1": [ { "x": [ { "data": 2 } ] } ] }
+{ "a": 3, "$1": [ { "x": [ { "data": 3 } ] } ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.12.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.12.adm
new file mode 100644
index 0000000..ea413fe
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.12.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "test": [ 1 ] }
+{ "a": 2, "test": [ 2 ] }
+{ "a": 3, "test": [ 3 ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.13.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.13.adm
new file mode 100644
index 0000000..c8f629b
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.13.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "$1": [ 1 ] }
+{ "a": 2, "$1": [ 2 ] }
+{ "a": 3, "$1": [ 3 ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.14.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.14.adm
new file mode 100644
index 0000000..2cf7df3
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.14.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "test": [ [ 1 ] ] }
+{ "a": 2, "test": [ [ 2 ] ] }
+{ "a": 3, "test": [ [ 3 ] ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.15.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.15.adm
new file mode 100644
index 0000000..325d24f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.15.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "$1": [ [ 1 ] ] }
+{ "a": 2, "$1": [ [ 2 ] ] }
+{ "a": 3, "$1": [ [ 3 ] ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.16.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.16.adm
new file mode 100644
index 0000000..0f584e9
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.16.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "test": [ 7 ] }
+{ "a": 2, "test": [ 8 ] }
+{ "a": 3, "test": [ 9 ] }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.17.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.17.adm
new file mode 100644
index 0000000..74fd97f
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/subquery/select_element/select_element.17.adm
@@ -0,0 +1,3 @@
+{ "a": 1, "$1": 1.0 }
+{ "a": 2, "$1": 2.0 }
+{ "a": 3, "$1": 3.0 }
\ 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 ee5fafb..0ab9672 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -11327,6 +11327,11 @@
   </test-group>
   <test-group name="subquery">
     <test-case FilePath="subquery">
+      <compilation-unit name="select_element">
+        <output-dir compare="Text">select_element</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="subquery">
       <compilation-unit name="aggregate_join">
         <output-dir compare="Text">aggregate_join</output-dir>
       </compilation-unit>