[ASTERIXDB-2524][COMP] Fix name resolution for ratio_to_report()

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

Details:
- Fixes illegal state error when using ratio_to_report()
  caused by incorrect identifier resolution for this function

Change-Id: I604ff1ee340fe58250500b2ff459f9b46433a269
Reviewed-on: https://asterix-gerrit.ics.uci.edu/3244
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Contrib: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/ratio_to_report_01/ratio_to_report_01.5.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/ratio_to_report_01/ratio_to_report_01.5.query.sqlpp
new file mode 100644
index 0000000..6cae150
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/window/ratio_to_report_01/ratio_to_report_01.5.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.
+ */
+/*
+ * Description  : Test identifier resolution in RATIO_TO_REPORT()
+ * Expected Res : SUCCESS
+ */
+
+FROM (
+  FROM range(1, 2) x, range(1, 2) y SELECT x, y
+) t
+SELECT x, y,
+  round_half_to_even(ratio_to_report(x) over (partition by y), 2) as `ratio_to_report`
+ORDER BY y, x;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/window/ratio_to_report_01/ratio_to_report_01.5.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/ratio_to_report_01/ratio_to_report_01.5.adm
new file mode 100644
index 0000000..8b88f83
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/window/ratio_to_report_01/ratio_to_report_01.5.adm
@@ -0,0 +1,4 @@
+{ "x": 1, "y": 1, "ratio_to_report": 0.33 }
+{ "x": 2, "y": 1, "ratio_to_report": 0.67 }
+{ "x": 1, "y": 2, "ratio_to_report": 0.33 }
+{ "x": 2, "y": 2, "ratio_to_report": 0.67 }
\ No newline at end of file
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowAggregationSugarVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowAggregationSugarVisitor.java
index 777f6db..2216901 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowAggregationSugarVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowAggregationSugarVisitor.java
@@ -36,7 +36,6 @@
 import org.apache.asterix.lang.sqlpp.clause.SelectBlock;
 import org.apache.asterix.lang.sqlpp.expression.WindowExpression;
 import org.apache.asterix.lang.sqlpp.util.FunctionMapUtil;
-import org.apache.asterix.lang.sqlpp.util.SqlppRewriteUtil;
 import org.apache.asterix.lang.sqlpp.util.SqlppVariableUtil;
 import org.apache.asterix.lang.sqlpp.visitor.base.AbstractSqlppExpressionScopingVisitor;
 import org.apache.asterix.om.functions.BuiltinFunctions;
@@ -87,7 +86,6 @@
         FunctionIdentifier winfi = FunctionMapUtil.getInternalWindowFunction(signature);
         if (winfi != null) {
             winExpr.setFunctionSignature(new FunctionSignature(winfi));
-            rewriteSpecificWindowFunctions(winfi, winExpr);
             if (BuiltinFunctions.builtinFunctionHasProperty(winfi,
                     BuiltinFunctions.WindowFunctionProperty.HAS_LIST_ARG)) {
                 wrapAggregationArguments(winExpr, 1);
@@ -100,7 +98,7 @@
         return super.visit(winExpr, arg);
     }
 
-    private void wrapAggregationArguments(WindowExpression winExpr, int limit) throws CompilationException {
+    void wrapAggregationArguments(WindowExpression winExpr, int limit) throws CompilationException {
         Set<VariableExpr> liveVars = scopeChecker.getCurrentScope().getLiveVariables();
 
         VariableExpr winVar = winExpr.getWindowVar();
@@ -139,27 +137,4 @@
             }
         }
     }
-
-    /**
-     * Apply rewritings for specific window functions:
-     * <ul>
-     * <li>
-     * {@code ratio_to_report(x) -> ratio_to_report_impl(x, x)}.
-     * The first argument will then be rewritten by {@link #wrapAggregationArguments(WindowExpression, int)}.
-     * The remaining rewriting to {@code x/sum(x)} will be done by the expression to plan translator
-     * </li>
-     * </ul>
-     */
-    private void rewriteSpecificWindowFunctions(FunctionIdentifier winfi, WindowExpression winExpr)
-            throws CompilationException {
-        if (BuiltinFunctions.RATIO_TO_REPORT_IMPL.equals(winfi)) {
-            duplicateLastArgument(winExpr);
-        }
-    }
-
-    private void duplicateLastArgument(WindowExpression winExpr) throws CompilationException {
-        List<Expression> exprList = winExpr.getExprList();
-        Expression arg = exprList.get(exprList.size() - 1);
-        exprList.add((Expression) SqlppRewriteUtil.deepCopy(arg));
-    }
 }
diff --git a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowRewriteVisitor.java b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowRewriteVisitor.java
index 42a4282..0d744b3 100644
--- a/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowRewriteVisitor.java
+++ b/asterixdb/asterix-lang-sqlpp/src/main/java/org/apache/asterix/lang/sqlpp/rewrites/visitor/SqlppWindowRewriteVisitor.java
@@ -32,6 +32,7 @@
 import org.apache.asterix.lang.sqlpp.clause.FromClause;
 import org.apache.asterix.lang.sqlpp.expression.WindowExpression;
 import org.apache.asterix.lang.sqlpp.util.FunctionMapUtil;
+import org.apache.asterix.lang.sqlpp.util.SqlppRewriteUtil;
 import org.apache.asterix.om.functions.BuiltinFunctions;
 import org.apache.hyracks.algebricks.common.utils.Pair;
 import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
@@ -64,6 +65,7 @@
         FunctionSignature signature = winExpr.getFunctionSignature();
         FunctionIdentifier winfi = FunctionMapUtil.getInternalWindowFunction(signature);
         if (winfi != null) {
+            rewriteSpecificWindowFunctions(winfi, winExpr);
             if (BuiltinFunctions.builtinFunctionHasProperty(winfi,
                     BuiltinFunctions.WindowFunctionProperty.HAS_LIST_ARG)) {
                 List<Expression> newExprList = extractExpressions(winExpr.getExprList(), 1);
@@ -99,4 +101,28 @@
             throws CompilationException {
         throw new CompilationException(ErrorCode.COMPILATION_UNEXPECTED_WINDOW_EXPRESSION, clause.getSourceLocation());
     }
+
+    /**
+     * Apply rewritings for specific window functions:
+     * <ul>
+     * <li>
+     * {@code ratio_to_report(x) -> ratio_to_report_impl(x, x)}.
+     * The first argument will then be rewritten by
+     * {@link SqlppWindowAggregationSugarVisitor#wrapAggregationArguments(WindowExpression, int)}.
+     * The remaining rewriting to {@code x/sum(x)} will be done by the expression to plan translator
+     * </li>
+     * </ul>
+     */
+    private void rewriteSpecificWindowFunctions(FunctionIdentifier winfi, WindowExpression winExpr)
+            throws CompilationException {
+        if (BuiltinFunctions.RATIO_TO_REPORT_IMPL.equals(winfi)) {
+            duplicateLastArgument(winExpr);
+        }
+    }
+
+    private void duplicateLastArgument(WindowExpression winExpr) throws CompilationException {
+        List<Expression> exprList = winExpr.getExprList();
+        Expression arg = exprList.get(exprList.size() - 1);
+        exprList.add((Expression) SqlppRewriteUtil.deepCopy(arg));
+    }
 }