[ASTERIXDB-2413][COMP] Error when statement parameter after group by

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

Details:
- Fix IllegalStateException in SqlppGroupByAggregationSugarVisitor

  when statement parameter is used after group by

Change-Id: I52c948396378e10bf9577109aa8626de52b94d60
Reviewed-on: https://asterix-gerrit.ics.uci.edu/2761
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Xikui Wang <xkkwww@gmail.com>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/statement-params/query-ASTERIXDB-2413/query-ASTERIXDB-2413.1.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/statement-params/query-ASTERIXDB-2413/query-ASTERIXDB-2413.1.query.sqlpp
new file mode 100644
index 0000000..e340140
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/statement-params/query-ASTERIXDB-2413/query-ASTERIXDB-2413.1.query.sqlpp
@@ -0,0 +1,39 @@
+/*
+ * 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 when statement parameter is used after GROUP BY
+ * Expected Res : Success
+ * Date         : Jul 2018
+ */
+
+// requesttype=application/json
+
+// param args:json=[2]
+
+from [
+ { "x": false, "y": 1 },
+ { "x": false, "y": 2 },
+ { "x": true, "y": 3 },
+ { "x": true, "y": 4 },
+ { "x": true, "y": 5 }
+] t
+group by t.x
+having count(t.y) > $1
+select value t.x
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/statement-params/query-ASTERIXDB-2413/query-ASTERIXDB-2413.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/statement-params/query-ASTERIXDB-2413/query-ASTERIXDB-2413.1.adm
new file mode 100644
index 0000000..f32a580
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/statement-params/query-ASTERIXDB-2413/query-ASTERIXDB-2413.1.adm
@@ -0,0 +1 @@
+true
\ 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 8817bc8..3616268 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -6180,6 +6180,11 @@
         <output-dir compare="Text">positional_05</output-dir>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="statement-params">
+      <compilation-unit name="query-ASTERIXDB-2413">
+        <output-dir compare="Text">query-ASTERIXDB-2413</output-dir>
+      </compilation-unit>
+    </test-case>
   </test-group>
   <test-group name="string">
     <test-case FilePath="string">
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupByAggregationSugarVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupByAggregationSugarVisitor.java
index 2663837..a73d44e 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupByAggregationSugarVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppGroupByAggregationSugarVisitor.java
@@ -179,6 +179,7 @@
 
             // Gets the final free variables.
             freeVariables.addAll(freeVariablesInGbyLets);
+            freeVariables.removeIf(SqlppVariableUtil::isExternalVariableReference);
 
             // Gets outer scope variables.
             Collection<VariableExpr> decorVars = scopeChecker.getCurrentScope().getLiveVariables();
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableUtil.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableUtil.java
index 3dbdde5..14dfaeb 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableUtil.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/util/SqlppVariableUtil.java
@@ -92,6 +92,10 @@
         return varId.getValue().startsWith(EXTERNAL_VAR_PREFIX);
     }
 
+    public static boolean isExternalVariableReference(VariableExpr varExpr) {
+        return isExternalVariableIdentifier(varExpr.getVar());
+    }
+
     public static Collection<VariableExpr> getFreeVariables(ILangExpression langExpr) throws CompilationException {
         Collection<VariableExpr> freeVars = new HashSet<>();
         FreeVariableVisitor visitor = new FreeVariableVisitor();