[ASTERIXDB-3590]: Fix parse-only to consider all statements
- user model changes: yes
- storage format changes: no
- interface changes: no
details:
- Currently, parse-only parameter only collects external variables
from the last statement.
- For example, "select $p1, $p2; select $p3, $p4;" only returns ["p3", "p4"] instead of
["p1", "p2", "p3", "p4"].
- The fix processes all statements
- Test cases added.
Ext-ref: MB-62708
Change-Id: Ia28d5b504f7eee0837647118422fed2b6101fbed
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/19607
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
Reviewed-by: Janhavi Tripurwar <janhavi.tripurwar@couchbase.com>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/QueryServiceServlet.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/QueryServiceServlet.java
index 12859fb..2b78498 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/QueryServiceServlet.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/api/http/server/QueryServiceServlet.java
@@ -27,6 +27,7 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -389,10 +390,15 @@
IParser parser = factory.createParser(statementsText);
List<Statement> stmts = parser.parse();
QueryTranslator.validateStatements(stmts, true, RequestParameters.NO_CATEGORY_RESTRICTION_MASK);
- Query query = (Query) stmts.get(stmts.size() - 1);
- Set<VariableExpr> extVars =
- compilationProvider.getRewriterFactory().createQueryRewriter().getExternalVariables(query.getBody());
- return new ResultUtil.ParseOnlyResult(extVars);
+
+ Set<VariableExpr> allExtVars = new HashSet<>();
+ for (Statement stmt : stmts) {
+ Query query = (Query) stmt;
+ Set<VariableExpr> stmtExtVars = compilationProvider.getRewriterFactory().createQueryRewriter()
+ .getExternalVariables(query.getBody());
+ allExtVars.addAll(stmtExtVars);
+ }
+ return new ResultUtil.ParseOnlyResult(allExtVars);
}
protected void executeStatement(IServletRequest request, IRequestReference requestReference, String statementsText,
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries/parseonly/001/parseonly_01.6.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries/parseonly/001/parseonly_01.6.query.sqlpp
new file mode 100644
index 0000000..0364940
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries/parseonly/001/parseonly_01.6.query.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.
+ */
+
+/*
+ * Description : Test named statement parameters with json encoded request
+ * Expected Res : Success
+ * Date : April 2025
+ */
+
+-- param parse-only:string=true
+
+select $p1, $p2; select 1; select $p3, $p1;
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/parseonly/001/parseonly_01.6.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/parseonly/001/parseonly_01.6.adm
new file mode 100644
index 0000000..8071244
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/parseonly/001/parseonly_01.6.adm
@@ -0,0 +1 @@
+{ "statement-parameters": [ "p1", "p2", "p3" ] }
\ No newline at end of file