introduce ConstantExpressionUtil

Change-Id: Ic0034bd6ba7f449a03eda48070a8de614608c2dd
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1069
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/algebra/util/ConstantExpressionUtil.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/algebra/util/ConstantExpressionUtil.java
new file mode 100644
index 0000000..300ef9d
--- /dev/null
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/algebra/util/ConstantExpressionUtil.java
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+package org.apache.asterix.algebra.util;
+
+import org.apache.asterix.om.base.AInt32;
+import org.apache.asterix.om.base.AString;
+import org.apache.asterix.om.base.IAObject;
+import org.apache.asterix.om.constants.AsterixConstantValue;
+import org.apache.asterix.om.types.ATypeTag;
+import org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression;
+import org.apache.hyracks.algebricks.core.algebra.base.LogicalExpressionTag;
+import org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
+import org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression;
+import org.apache.hyracks.algebricks.core.algebra.expressions.IAlgebricksConstantValue;
+
+public class ConstantExpressionUtil {
+
+    private static IAObject getAsterixConstantValue(AbstractFunctionCallExpression f, int index, ATypeTag typeTag) {
+        final ILogicalExpression expr = f.getArguments().get(index).getValue();
+        if (expr.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+            return null;
+        }
+        final IAlgebricksConstantValue acv = ((ConstantExpression) expr).getValue();
+        if (!(acv instanceof AsterixConstantValue)) {
+            return null;
+        }
+        final IAObject iaObject = ((AsterixConstantValue) acv).getObject();
+        return iaObject.getType().getTypeTag() == typeTag ? iaObject : null;
+    }
+
+    public static Integer getIntArgument(AbstractFunctionCallExpression f, int index) {
+        final IAObject iaObject = getAsterixConstantValue(f, index, ATypeTag.INT32);
+        return iaObject != null ? ((AInt32) iaObject).getIntegerValue() : null;
+    }
+
+    public static String getStringArgument(AbstractFunctionCallExpression f, int index) {
+        final IAObject iaObject = getAsterixConstantValue(f, index, ATypeTag.STRING);
+        return iaObject != null ? ((AString) iaObject).getStringValue() : null;
+    }
+
+    public static Integer getIntArgument(ILogicalExpression expr, int index) {
+        return expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL
+                ? getIntArgument((AbstractFunctionCallExpression) expr, index) : null;
+    }
+
+    public static String getStringArgument(ILogicalExpression expr, int index) {
+        return expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL
+                ? getStringArgument((AbstractFunctionCallExpression) expr, index) : null;
+    }
+}
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/UnnestToDataScanRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/UnnestToDataScanRule.java
index d623830..69c9f7d 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/UnnestToDataScanRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/UnnestToDataScanRule.java
@@ -18,6 +18,8 @@
  */
 package org.apache.asterix.optimizer.rules;
 
+import static org.apache.asterix.algebra.util.ConstantExpressionUtil.getStringArgument;
+
 import java.util.ArrayList;
 import java.util.List;
 
@@ -297,23 +299,4 @@
         }
         return new Pair<String, String>(dataverseName, datasetName);
     }
-
-    private String getStringArgument(AbstractFunctionCallExpression f, int index) {
-
-        ILogicalExpression expr = f.getArguments().get(index).getValue();
-        if (expr.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
-            return null;
-        }
-        ConstantExpression ce = (ConstantExpression) expr;
-        IAlgebricksConstantValue acv = ce.getValue();
-        if (!(acv instanceof AsterixConstantValue)) {
-            return null;
-        }
-        AsterixConstantValue acv2 = (AsterixConstantValue) acv;
-        if (acv2.getObject().getType().getTypeTag() != ATypeTag.STRING) {
-            return null;
-        }
-        String argument = ((AString) acv2.getObject()).getStringValue();
-        return argument;
-    }
 }
\ No newline at end of file
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 5081587..d85419e 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
@@ -40,7 +40,6 @@
 import org.apache.asterix.lang.common.base.Expression;
 import org.apache.asterix.lang.common.base.Expression.Kind;
 import org.apache.asterix.lang.common.base.ILangExpression;
-import org.apache.asterix.lang.common.base.ILangExpression;
 import org.apache.asterix.lang.common.base.Statement;
 import org.apache.asterix.lang.common.clause.GroupbyClause;
 import org.apache.asterix.lang.common.clause.LetClause;