ConstantExpressionUtil improvements

Change-Id: I636538d6ea6c6a527aac1eb9cacdcc40a8489c02
Reviewed-on: https://asterix-gerrit.ics.uci.edu/1074
Sonar-Qube: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Yingyi Bu <buyingyi@gmail.com>
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ByNameToByIndexFieldAccessRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ByNameToByIndexFieldAccessRule.java
index 6e8bd69..3ba7084 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ByNameToByIndexFieldAccessRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ByNameToByIndexFieldAccessRule.java
@@ -23,10 +23,9 @@
 import java.util.Collections;
 
 import org.apache.asterix.algebra.base.AsterixOperatorAnnotations;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.asterix.lang.common.util.FunctionUtil;
 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.functions.AsterixBuiltinFunctions;
 import org.apache.asterix.om.types.ARecordType;
@@ -141,7 +140,7 @@
     @SuppressWarnings("unchecked")
     private static ILogicalExpression createFieldAccessByIndex(ARecordType recType,
             AbstractFunctionCallExpression fce) {
-        String s = getStringSecondArgument(fce);
+        String s = ConstantExpressionUtil.getStringArgument(fce, 1);
         if (s == null) {
             return null;
         }
@@ -151,22 +150,6 @@
         }
         return new ScalarFunctionCallExpression(
                 FunctionUtil.getFunctionInfo(AsterixBuiltinFunctions.FIELD_ACCESS_BY_INDEX), fce.getArguments().get(0),
-                new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AInt32(k)))));
-    }
-
-    private static String getStringSecondArgument(AbstractFunctionCallExpression expr) {
-        ILogicalExpression e2 = expr.getArguments().get(1).getValue();
-        if (e2.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
-            return null;
-        }
-        ConstantExpression c = (ConstantExpression) e2;
-        if (!(c.getValue() instanceof AsterixConstantValue)) {
-            return null;
-        }
-        IAObject v = ((AsterixConstantValue) c.getValue()).getObject();
-        if (v.getType().getTypeTag() != ATypeTag.STRING) {
-            return null;
-        }
-        return ((AString) v).getStringValue();
+                new MutableObject<>(new ConstantExpression(new AsterixConstantValue(new AInt32(k)))));
     }
 }
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ConstantFoldingRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ConstantFoldingRule.java
index 9d9dda0..fec564e 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ConstantFoldingRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ConstantFoldingRule.java
@@ -23,6 +23,7 @@
 import java.nio.ByteBuffer;
 import java.util.List;
 
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.asterix.common.config.GlobalConfig;
 import org.apache.asterix.dataflow.data.common.AqlExpressionTypeComputer;
 import org.apache.asterix.dataflow.data.nontagged.AqlMissingWriterFactory;
@@ -35,7 +36,6 @@
 import org.apache.asterix.formats.nontagged.AqlSerializerDeserializerProvider;
 import org.apache.asterix.formats.nontagged.AqlTypeTraitProvider;
 import org.apache.asterix.jobgen.QueryLogicalExpressionJobGen;
-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.functions.AsterixBuiltinFunctions;
@@ -204,9 +204,7 @@
             }
             if (expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME)) {
                 ARecordType rt = (ARecordType) _emptyTypeEnv.getType(expr.getArguments().get(0).getValue());
-                String str =
-                        ((AString) ((AsterixConstantValue) ((ConstantExpression) expr.getArguments().get(1).getValue())
-                                .getValue()).getObject()).getStringValue();
+                String str = ConstantExpressionUtil.getStringConstant(expr.getArguments().get(1).getValue());
                 int k = rt.getFieldIndex(str);
                 if (k >= 0) {
                     // wait for the ByNameToByIndex rule to apply
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/LoadRecordFieldsRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/LoadRecordFieldsRule.java
index 36e93c4..d444aca 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/LoadRecordFieldsRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/LoadRecordFieldsRule.java
@@ -29,6 +29,7 @@
 import org.apache.asterix.om.base.AString;
 import org.apache.asterix.om.constants.AsterixConstantValue;
 import org.apache.asterix.om.functions.AsterixBuiltinFunctions;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.asterix.optimizer.base.AnalysisUtil;
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.commons.lang3.mutable.MutableObject;
@@ -344,12 +345,8 @@
         Iterator<Mutable<ILogicalExpression>> fldIter = fce.getArguments().iterator();
         while (fldIter.hasNext()) {
             ILogicalExpression fldExpr = fldIter.next().getValue();
-            if (fldExpr.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
-                ConstantExpression ce = (ConstantExpression) fldExpr;
-                String f2 = ((AString) ((AsterixConstantValue) ce.getValue()).getObject()).getStringValue();
-                if (fldName.equals(f2)) {
-                    return fldIter.next().getValue();
-                }
+            if (fldName.equals(ConstantExpressionUtil.getStringConstant(fldExpr))) {
+                return fldIter.next().getValue();
             }
             fldIter.next();
         }
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushFieldAccessRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushFieldAccessRule.java
index 165d749..079f61a 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushFieldAccessRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/PushFieldAccessRule.java
@@ -24,6 +24,7 @@
 import java.util.List;
 
 import org.apache.asterix.algebra.base.AsterixOperatorAnnotations;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.asterix.common.config.DatasetConfig.DatasetType;
 import org.apache.asterix.metadata.declared.AqlDataSource;
 import org.apache.asterix.metadata.declared.AqlDataSource.AqlDataSourceType;
@@ -131,14 +132,8 @@
         if (dataset.getDatasetType() != DatasetType.INTERNAL) {
             return false;
         }
-        ILogicalExpression e1 = accessFun.getArguments().get(1).getValue();
-        if (e1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
-            return false;
-        }
-        ConstantExpression ce = (ConstantExpression) e1;
-        IAObject obj = ((AsterixConstantValue) ce.getValue()).getObject();
-        if (obj.getType().getTypeTag() != ATypeTag.STRING) {
-            int pos = ((AInt32) obj).getIntegerValue();
+        final Integer pos = ConstantExpressionUtil.getIntConstant(accessFun.getArguments().get(1).getValue());
+        if (pos != null) {
             String tName = dataset.getItemTypeName();
             IAType t = mp.findType(dataset.getItemTypeDataverseName(), tName);
             if (t.getTypeTag() != ATypeTag.RECORD) {
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ResolveVariableRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ResolveVariableRule.java
index 1d3e904..2cce9b0 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ResolveVariableRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/ResolveVariableRule.java
@@ -34,6 +34,7 @@
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.AUnionType;
 import org.apache.asterix.om.types.IAType;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.commons.lang3.mutable.MutableObject;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
@@ -45,7 +46,6 @@
 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;
 import org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment;
 import org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression;
 import org.apache.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
@@ -326,19 +326,10 @@
 
     // Extracts the name of an undefined variable.
     private String extractConstantString(ILogicalExpression arg) throws AlgebricksException {
-        if (arg.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
-            throw new AlgebricksException("The argument is expected to be a constant value.");
-        }
-        ConstantExpression ce = (ConstantExpression) arg;
-        IAlgebricksConstantValue acv = ce.getValue();
-        if (!(acv instanceof AsterixConstantValue)) {
-            throw new AlgebricksException("The argument is expected to be an Asterix constant value.");
-        }
-        AsterixConstantValue acv2 = (AsterixConstantValue) acv;
-        if (acv2.getObject().getType().getTypeTag() != ATypeTag.STRING) {
+        final String str = ConstantExpressionUtil.getStringConstant(arg);
+        if (str == null) {
             throw new AlgebricksException("The argument is expected to be a string constant value.");
         }
-        return ((AString) acv2.getObject()).getStringValue();
+        return str;
     }
-
 }
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 69c9f7d..bb382f0 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,7 +18,7 @@
  */
 package org.apache.asterix.optimizer.rules;
 
-import static org.apache.asterix.algebra.util.ConstantExpressionUtil.getStringArgument;
+import static org.apache.asterix.om.util.ConstantExpressionUtil.getStringArgument;
 
 import java.util.ArrayList;
 import java.util.List;
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
index ed1803d..b3e9e09 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AbstractIntroduceAccessMethodRule.java
@@ -24,13 +24,13 @@
 import java.util.List;
 import java.util.Map;
 
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.asterix.common.config.DatasetConfig.IndexType;
 import org.apache.asterix.dataflow.data.common.AqlExpressionTypeComputer;
 import org.apache.asterix.metadata.api.IMetadataEntity;
 import org.apache.asterix.metadata.declared.AqlMetadataProvider;
 import org.apache.asterix.metadata.entities.Index;
 import org.apache.asterix.metadata.utils.DatasetUtils;
-import org.apache.asterix.om.base.AInt32;
 import org.apache.asterix.om.base.AOrderedList;
 import org.apache.asterix.om.base.AString;
 import org.apache.asterix.om.constants.AsterixConstantValue;
@@ -699,21 +699,18 @@
         List<String> nestedAccessFieldName = null;
         int fieldIndex = -1;
         if (funcIdent == AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME) {
-            ILogicalExpression nameArg = funcExpr.getArguments().get(1).getValue();
-            if (nameArg.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+            fieldName = ConstantExpressionUtil.getStringArgument(funcExpr, 1);
+            if (fieldName == null) {
                 return null;
             }
-            ConstantExpression constExpr = (ConstantExpression) nameArg;
-            fieldName = ((AString) ((AsterixConstantValue) constExpr.getValue()).getObject()).getStringValue();
             isFieldAccess = true;
             isByName = true;
         } else if (funcIdent == AsterixBuiltinFunctions.FIELD_ACCESS_BY_INDEX) {
-            ILogicalExpression idxArg = funcExpr.getArguments().get(1).getValue();
-            if (idxArg.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+            Integer idx = ConstantExpressionUtil.getIntArgument(funcExpr, 1);
+            if (idx == null) {
                 return null;
             }
-            ConstantExpression constExpr = (ConstantExpression) idxArg;
-            fieldIndex = ((AInt32) ((AsterixConstantValue) constExpr.getValue()).getObject()).getIntegerValue();
+            fieldIndex = idx;
             isFieldAccess = true;
         } else if (funcIdent == AsterixBuiltinFunctions.FIELD_ACCESS_NESTED) {
             ILogicalExpression nameArg = funcExpr.getArguments().get(1).getValue();
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodUtils.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodUtils.java
index db2c627..5b7146f 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodUtils.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/AccessMethodUtils.java
@@ -38,15 +38,14 @@
 import org.apache.asterix.metadata.utils.KeyFieldTypeUtils;
 import org.apache.asterix.om.base.ABoolean;
 import org.apache.asterix.om.base.AInt32;
-import org.apache.asterix.om.base.AInt64;
 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.functions.AsterixBuiltinFunctions;
 import org.apache.asterix.om.types.ARecordType;
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.IAType;
 import org.apache.asterix.om.types.hierachy.ATypeHierarchy;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.commons.lang3.mutable.MutableObject;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
@@ -105,31 +104,23 @@
     }
 
     public static ConstantExpression createBooleanConstant(boolean b) {
-        if (b) {
-            return new ConstantExpression(new AsterixConstantValue(ABoolean.TRUE));
-        } else {
-            return new ConstantExpression(new AsterixConstantValue(ABoolean.FALSE));
-        }
+        return new ConstantExpression(new AsterixConstantValue(ABoolean.valueOf(b)));
     }
 
     public static String getStringConstant(Mutable<ILogicalExpression> expr) {
-        IAObject obj = ((AsterixConstantValue) ((ConstantExpression) expr.getValue()).getValue()).getObject();
-        return ((AString) obj).getStringValue();
+        return ConstantExpressionUtil.getStringConstant(expr.getValue());
     }
 
     public static int getInt32Constant(Mutable<ILogicalExpression> expr) {
-        IAObject obj = ((AsterixConstantValue) ((ConstantExpression) expr.getValue()).getValue()).getObject();
-        return ((AInt32) obj).getIntegerValue();
+        return ConstantExpressionUtil.getIntConstant(expr.getValue());
     }
 
     public static long getInt64Constant(Mutable<ILogicalExpression> expr) {
-        IAObject obj = ((AsterixConstantValue) ((ConstantExpression) expr.getValue()).getValue()).getObject();
-        return ((AInt64) obj).getLongValue();
+        return ConstantExpressionUtil.getLongConstant(expr.getValue());
     }
 
     public static boolean getBooleanConstant(Mutable<ILogicalExpression> expr) {
-        IAObject obj = ((AsterixConstantValue) ((ConstantExpression) expr.getValue()).getValue()).getObject();
-        return ((ABoolean) obj).getBoolean();
+        return ConstantExpressionUtil.getBooleanConstant(expr.getValue());
     }
 
     public static boolean analyzeFuncExprArgsForOneConstAndVar(AbstractFunctionCallExpression funcExpr,
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceLSMComponentFilterRule.java b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceLSMComponentFilterRule.java
index a2bf6d9..45c5e34 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceLSMComponentFilterRule.java
+++ b/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/optimizer/rules/am/IntroduceLSMComponentFilterRule.java
@@ -19,7 +19,6 @@
 package org.apache.asterix.optimizer.rules.am;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 
 import org.apache.asterix.common.config.DatasetConfig.DatasetType;
@@ -30,13 +29,11 @@
 import org.apache.asterix.metadata.entities.Index;
 import org.apache.asterix.metadata.utils.DatasetUtils;
 import org.apache.asterix.metadata.utils.KeyFieldTypeUtils;
-import org.apache.asterix.om.base.AInt32;
-import org.apache.asterix.om.base.AString;
-import org.apache.asterix.om.constants.AsterixConstantValue;
 import org.apache.asterix.om.functions.AsterixBuiltinFunctions;
 import org.apache.asterix.om.types.ARecordType;
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.IAType;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.commons.lang3.mutable.MutableObject;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
@@ -49,7 +46,6 @@
 import org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable;
 import org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
 import org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression;
-import org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression;
 import org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment;
 import org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression;
 import org.apache.hyracks.algebricks.core.algebra.functions.AlgebricksBuiltinFunctions;
@@ -441,27 +437,23 @@
             }
 
             if (funcIdent == AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME) {
-                ILogicalExpression nameArg = funcExpr.getArguments().get(1).getValue();
-                if (nameArg.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+                String fieldName = ConstantExpressionUtil.getStringArgument(funcExpr, 1);
+                if (fieldName == null) {
                     return null;
                 }
-                ConstantExpression constExpr = (ConstantExpression) nameArg;
-                returnList.addAll(Arrays.asList(
-                        ((AString) ((AsterixConstantValue) constExpr.getValue()).getObject()).getStringValue()));
-                return new Pair<ARecordType, List<String>>(recType, returnList);
+                returnList.add(fieldName);
+                return new Pair<>(recType, returnList);
             } else if (funcIdent == AsterixBuiltinFunctions.FIELD_ACCESS_BY_INDEX) {
-                ILogicalExpression idxArg = funcExpr.getArguments().get(1).getValue();
-                if (idxArg.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+                Integer fieldIndex = ConstantExpressionUtil.getIntArgument(funcExpr, 1);
+                if (fieldIndex == null) {
                     return null;
                 }
-                ConstantExpression constExpr = (ConstantExpression) idxArg;
-                int fieldIndex = ((AInt32) ((AsterixConstantValue) constExpr.getValue()).getObject()).getIntegerValue();
-                returnList.addAll(Arrays.asList(recType.getFieldNames()[fieldIndex]));
+                returnList.add(recType.getFieldNames()[fieldIndex]);
                 IAType subType = recType.getFieldTypes()[fieldIndex];
                 if (subType.getTypeTag() == ATypeTag.RECORD) {
                     recType = (ARecordType) subType;
                 }
-                return new Pair<ARecordType, List<String>>(recType, returnList);
+                return new Pair<>(recType, returnList);
             }
 
         }
diff --git a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/functions/MetadataBuiltinFunctions.java b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/functions/MetadataBuiltinFunctions.java
index 9bddd8e..10458986 100644
--- a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/functions/MetadataBuiltinFunctions.java
+++ b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/functions/MetadataBuiltinFunctions.java
@@ -20,19 +20,16 @@
 
 import org.apache.asterix.metadata.declared.AqlMetadataProvider;
 import org.apache.asterix.metadata.entities.Dataset;
-import org.apache.asterix.om.base.AString;
-import org.apache.asterix.om.constants.AsterixConstantValue;
 import org.apache.asterix.om.functions.AsterixBuiltinFunctions;
 import org.apache.asterix.om.typecomputer.base.IResultTypeComputer;
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.BuiltinType;
 import org.apache.asterix.om.types.IAType;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 import org.apache.hyracks.algebricks.common.utils.Pair;
 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.IVariableTypeEnvironment;
 import org.apache.hyracks.algebricks.core.algebra.metadata.IMetadataProvider;
 
@@ -67,11 +64,10 @@
                 if (t1.getTypeTag() != ATypeTag.STRING) {
                     throw new AlgebricksException("Illegal type " + t1 + " for dataset() argument.");
                 }
-                if (a1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+                String datasetArg = ConstantExpressionUtil.getStringConstant(a1);
+                if (datasetArg == null) {
                     return BuiltinType.ANY;
                 }
-                AsterixConstantValue acv = (AsterixConstantValue) ((ConstantExpression) a1).getValue();
-                String datasetArg = ((AString) acv.getObject()).getStringValue();
                 AqlMetadataProvider metadata = ((AqlMetadataProvider) mp);
                 Pair<String, String> datasetInfo = getDatasetInfo(metadata, datasetArg);
                 String dataverseName = datasetInfo.first;
@@ -111,11 +107,10 @@
                 if (t1.getTypeTag() != ATypeTag.STRING) {
                     throw new AlgebricksException("Illegal type " + t1 + " for feed-ingest argument.");
                 }
-                if (a1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+                String typeArg = ConstantExpressionUtil.getStringConstant(a1);
+                if (typeArg == null) {
                     return BuiltinType.ANY;
                 }
-                AsterixConstantValue acv = (AsterixConstantValue) ((ConstantExpression) a1).getValue();
-                String typeArg = ((AString) acv.getObject()).getStringValue();
                 AqlMetadataProvider metadata = ((AqlMetadataProvider) mp);
                 Pair<String, String> argInfo = getDatasetInfo(metadata, typeArg);
                 String dataverseName = argInfo.first;
@@ -148,11 +143,10 @@
                 if (t1.getTypeTag() != ATypeTag.STRING) {
                     throw new AlgebricksException("Illegal type " + t1 + " for dataset() argument.");
                 }
-                if (a1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+                String datasetArg = ConstantExpressionUtil.getStringConstant(a1);
+                if (datasetArg == null) {
                     return BuiltinType.ANY;
                 }
-                AsterixConstantValue acv = (AsterixConstantValue) ((ConstantExpression) a1).getValue();
-                String datasetArg = ((AString) acv.getObject()).getStringValue();
                 AqlMetadataProvider metadata = ((AqlMetadataProvider) mp);
                 Pair<String, String> datasetInfo = getDatasetInfo(metadata, datasetArg);
                 String dataverseName = datasetInfo.first;
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/base/ABoolean.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/base/ABoolean.java
index 7e64cb5..2a56cff 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/base/ABoolean.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/base/ABoolean.java
@@ -40,7 +40,7 @@
         return bVal;
     }
 
-    public ABoolean valueOf(boolean b) {
+    public static ABoolean valueOf(boolean b) {
         return b ? TRUE : FALSE;
     }
 
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/ClosedRecordConstructorResultType.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/ClosedRecordConstructorResultType.java
index 1aa06bd..e337ea8 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/ClosedRecordConstructorResultType.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/ClosedRecordConstructorResultType.java
@@ -21,20 +21,17 @@
 
 import java.util.Iterator;
 
-import org.apache.asterix.om.base.AString;
-import org.apache.asterix.om.constants.AsterixConstantValue;
 import org.apache.asterix.om.typecomputer.base.IResultTypeComputer;
 import org.apache.asterix.om.typecomputer.base.TypeCastUtils;
 import org.apache.asterix.om.types.ARecordType;
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.AUnionType;
 import org.apache.asterix.om.types.IAType;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 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.IVariableTypeEnvironment;
 import org.apache.hyracks.algebricks.core.algebra.metadata.IMetadataProvider;
 
@@ -69,10 +66,8 @@
                 e2Type = AUnionType.createUnknownableType(unionType.getActualType());
             }
             fieldTypes[i] = e2Type;
-            if (e1.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
-                ConstantExpression nameExpr = (ConstantExpression) e1;
-                fieldNames[i] = ((AString) ((AsterixConstantValue) nameExpr.getValue()).getObject()).getStringValue();
-            } else {
+            fieldNames[i] = ConstantExpressionUtil.getStringConstant(e1);
+            if (fieldNames[i] == null) {
                 throw new AlgebricksException(
                         "Field name " + i + "(" + e1 + ") in call to closed-record-constructor is not a constant.");
             }
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/FieldAccessByIndexResultType.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/FieldAccessByIndexResultType.java
index 2b4cf59..f46c102 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/FieldAccessByIndexResultType.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/FieldAccessByIndexResultType.java
@@ -18,19 +18,15 @@
  */
 package org.apache.asterix.om.typecomputer.impl;
 
-import org.apache.asterix.om.base.AInt32;
-import org.apache.asterix.om.base.IAObject;
-import org.apache.asterix.om.constants.AsterixConstantValue;
 import org.apache.asterix.om.typecomputer.base.AbstractResultTypeComputer;
 import org.apache.asterix.om.types.ARecordType;
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.BuiltinType;
 import org.apache.asterix.om.types.IAType;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 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;
 
 public class FieldAccessByIndexResultType extends AbstractResultTypeComputer {
 
@@ -56,13 +52,10 @@
             return BuiltinType.ANY;
         }
         AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
-        ILogicalExpression arg1 = funcExpr.getArguments().get(1).getValue();
-        if (arg1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+        Integer pos = ConstantExpressionUtil.getIntArgument(funcExpr, 1);
+        if (pos == null) {
             return BuiltinType.ANY;
         }
-        ConstantExpression ce = (ConstantExpression) arg1;
-        IAObject v = ((AsterixConstantValue) ce.getValue()).getObject();
-        int pos = ((AInt32) v).getIntegerValue();
         ARecordType recType = (ARecordType) firstArgType;
         return recType.getFieldTypes()[pos];
     }
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/FieldAccessByNameResultType.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/FieldAccessByNameResultType.java
index 2931d7e..3e5af3c 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/FieldAccessByNameResultType.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/FieldAccessByNameResultType.java
@@ -18,19 +18,15 @@
  */
 package org.apache.asterix.om.typecomputer.impl;
 
-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.typecomputer.base.AbstractResultTypeComputer;
 import org.apache.asterix.om.types.ARecordType;
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.BuiltinType;
 import org.apache.asterix.om.types.IAType;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 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;
 
 public class FieldAccessByNameResultType extends AbstractResultTypeComputer {
 
@@ -58,13 +54,10 @@
             return BuiltinType.ANY;
         }
         AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
-        ILogicalExpression arg1 = funcExpr.getArguments().get(1).getValue();
-        if (arg1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+        String fieldName = ConstantExpressionUtil.getStringArgument(funcExpr, 1);
+        if (fieldName == null) {
             return BuiltinType.ANY;
         }
-        ConstantExpression ce = (ConstantExpression) arg1;
-        IAObject v = ((AsterixConstantValue) ce.getValue()).getObject();
-        String fieldName = ((AString) v).getStringValue();
         ARecordType recType = (ARecordType) firstArgType;
         IAType fieldType = recType.getFieldType(fieldName);
         return fieldType == null ? BuiltinType.ANY : fieldType;
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/OpenRecordConstructorResultType.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/OpenRecordConstructorResultType.java
index 04d879f..a12bc1d 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/OpenRecordConstructorResultType.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/OpenRecordConstructorResultType.java
@@ -25,19 +25,16 @@
 import java.util.List;
 import java.util.Set;
 
-import org.apache.asterix.om.base.AString;
-import org.apache.asterix.om.constants.AsterixConstantValue;
 import org.apache.asterix.om.typecomputer.base.IResultTypeComputer;
 import org.apache.asterix.om.typecomputer.base.TypeCastUtils;
 import org.apache.asterix.om.types.ARecordType;
 import org.apache.asterix.om.types.IAType;
 import org.apache.asterix.om.types.TypeHelper;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 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.IVariableTypeEnvironment;
 import org.apache.hyracks.algebricks.core.algebra.metadata.IMetadataProvider;
 
@@ -71,12 +68,7 @@
             ILogicalExpression e1 = argIter.next().getValue();
             ILogicalExpression e2 = argIter.next().getValue();
             IAType t2 = (IAType) env.getType(e2);
-            String fieldName = null;
-            if (e1.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
-                ConstantExpression nameExpr = (ConstantExpression) e1;
-                AsterixConstantValue acv = (AsterixConstantValue) nameExpr.getValue();
-                fieldName = ((AString) acv.getObject()).getStringValue();
-            }
+            String fieldName = ConstantExpressionUtil.getStringConstant(e1);
             if (fieldName != null && t2 != null && TypeHelper.isClosed(t2)) {
                 namesList.add(fieldName);
                 typesList.add(t2);
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/RecordAddFieldsTypeComputer.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/RecordAddFieldsTypeComputer.java
index 97f4e92..289db7c 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/RecordAddFieldsTypeComputer.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/RecordAddFieldsTypeComputer.java
@@ -27,9 +27,6 @@
 import java.util.List;
 import java.util.Map;
 
-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.typecomputer.base.IResultTypeComputer;
 import org.apache.asterix.om.types.AOrderedListType;
 import org.apache.asterix.om.types.ARecordType;
@@ -37,13 +34,13 @@
 import org.apache.asterix.om.types.AUnionType;
 import org.apache.asterix.om.types.IAType;
 import org.apache.asterix.om.types.TypeHelper;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 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.AbstractLogicalExpression;
-import org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression;
 import org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment;
 import org.apache.hyracks.algebricks.core.algebra.metadata.IMetadataProvider;
 
@@ -97,7 +94,7 @@
             AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) arg1;
             List<Mutable<ILogicalExpression>> args = f.getArguments();
 
-            AString fieldName = null;
+            String fieldName = null;
             IAType fieldType = null;
 
             // Iterating through the orderlist input
@@ -110,29 +107,17 @@
                     for (int j = 0; j < fn.length; j++) {
                         if (fn[j].equals(FIELD_NAME_NAME)) {
                             ILogicalExpression fieldNameExpr = recConsExpr.getArguments().get(j).getValue();
-                            switch (fieldNameExpr.getExpressionTag()) {
-                                case CONSTANT: // Top fields only
-                                    IAObject object = ((AsterixConstantValue) ((ConstantExpression) fieldNameExpr)
-                                            .getValue()).getObject();
-                                    if (object.getType().getTypeTag() == ATypeTag.STRING) {
-                                        // Get the actual "field-name" string
-                                        ILogicalExpression recFieldExpr = recConsExpr.getArguments().get(j + 1)
-                                                .getValue();
-                                        if (recFieldExpr.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
-                                            fieldName = (AString) ((AsterixConstantValue) ((ConstantExpression) recFieldExpr)
-                                                    .getValue()).getObject();
-                                        }
-                                    }
-                                    break;
-                                default:
-                                    throw new AlgebricksException(fieldNameExpr + " is not supported.");
+                            if (ConstantExpressionUtil.getStringConstant(fieldNameExpr) == null) {
+                                throw new AlgebricksException(fieldNameExpr + " is not supported.");
                             }
+                            // Get the actual "field-name" string
+                            fieldName = ConstantExpressionUtil.getStringArgument(recConsExpr, j + 1);
                         } else if (fn[j].equals(FIELD_VALUE_VALUE)) {
                             fieldType = ft[j];
                         }
                     }
                     if (fieldName != null) {
-                        additionalFields.put(fieldName.getStringValue(), fieldType);
+                        additionalFields.put(fieldName, fieldType);
                     }
                 }
             }
diff --git a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/RecordConstructorResultType.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/RecordConstructorResultType.java
index 1a977db..a5d6989 100644
--- a/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/RecordConstructorResultType.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/typecomputer/impl/RecordConstructorResultType.java
@@ -21,18 +21,15 @@
 
 import java.util.Iterator;
 
-import org.apache.asterix.om.base.AString;
-import org.apache.asterix.om.constants.AsterixConstantValue;
 import org.apache.asterix.om.typecomputer.base.IResultTypeComputer;
 import org.apache.asterix.om.typecomputer.base.TypeCastUtils;
 import org.apache.asterix.om.types.ARecordType;
 import org.apache.asterix.om.types.IAType;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.hyracks.algebricks.common.exceptions.AlgebricksException;
 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.IVariableTypeEnvironment;
 import org.apache.hyracks.algebricks.core.algebra.metadata.IMetadataProvider;
 
@@ -61,19 +58,12 @@
         Iterator<Mutable<ILogicalExpression>> argIter = f.getArguments().iterator();
         while (argIter.hasNext()) {
             ILogicalExpression e1 = argIter.next().getValue();
-            if (e1.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
-                ConstantExpression nameExpr = (ConstantExpression) e1;
-                if (!(nameExpr.getValue() instanceof AsterixConstantValue)) {
-                    throw new AlgebricksException("Expecting a string and found " + nameExpr.getValue() + " instead.");
-                }
-                fieldNames[i] = ((AString) ((AsterixConstantValue) nameExpr.getValue()).getObject()).getStringValue();
+            fieldNames[i] = ConstantExpressionUtil.getStringConstant(e1);
+            if (fieldNames[i] == null) {
+                throw new AlgebricksException("Expecting a string and found " + e1 + " instead.");
             }
             ILogicalExpression e2 = argIter.next().getValue();
-            try {
-                fieldTypes[i] = (IAType) env.getType(e2);
-            } catch (AlgebricksException e) {
-                throw new AlgebricksException(e);
-            }
+            fieldTypes[i] = (IAType) env.getType(e2);
             i++;
         }
         return new ARecordType(null, fieldNames, fieldTypes, isOpen);
diff --git a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/algebra/util/ConstantExpressionUtil.java b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/util/ConstantExpressionUtil.java
similarity index 70%
rename from asterixdb/asterix-algebra/src/main/java/org/apache/asterix/algebra/util/ConstantExpressionUtil.java
rename to asterixdb/asterix-om/src/main/java/org/apache/asterix/om/util/ConstantExpressionUtil.java
index 300ef9d..c67030a 100644
--- a/asterixdb/asterix-algebra/src/main/java/org/apache/asterix/algebra/util/ConstantExpressionUtil.java
+++ b/asterixdb/asterix-om/src/main/java/org/apache/asterix/om/util/ConstantExpressionUtil.java
@@ -16,9 +16,11 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.asterix.algebra.util;
+package org.apache.asterix.om.util;
 
+import org.apache.asterix.om.base.ABoolean;
 import org.apache.asterix.om.base.AInt32;
+import org.apache.asterix.om.base.AInt64;
 import org.apache.asterix.om.base.AString;
 import org.apache.asterix.om.base.IAObject;
 import org.apache.asterix.om.constants.AsterixConstantValue;
@@ -31,8 +33,10 @@
 
 public class ConstantExpressionUtil {
 
-    private static IAObject getAsterixConstantValue(AbstractFunctionCallExpression f, int index, ATypeTag typeTag) {
-        final ILogicalExpression expr = f.getArguments().get(index).getValue();
+    private ConstantExpressionUtil() {
+    }
+
+    private static IAObject getConstantIaObject(ILogicalExpression expr, ATypeTag typeTag) {
         if (expr.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
             return null;
         }
@@ -44,16 +48,34 @@
         return iaObject.getType().getTypeTag() == typeTag ? iaObject : null;
     }
 
-    public static Integer getIntArgument(AbstractFunctionCallExpression f, int index) {
-        final IAObject iaObject = getAsterixConstantValue(f, index, ATypeTag.INT32);
+    public static Long getLongConstant(ILogicalExpression expr) {
+        final IAObject iaObject = getConstantIaObject(expr, ATypeTag.INT64);
+        return iaObject != null ? ((AInt64) iaObject).getLongValue() : null;
+    }
+
+    public static Integer getIntConstant(ILogicalExpression expr) {
+        final IAObject iaObject = getConstantIaObject(expr, 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);
+    public static String getStringConstant(ILogicalExpression expr) {
+        final IAObject iaObject = getConstantIaObject(expr, ATypeTag.STRING);
         return iaObject != null ? ((AString) iaObject).getStringValue() : null;
     }
 
+    public static Boolean getBooleanConstant(ILogicalExpression expr) {
+        final IAObject iaObject = getConstantIaObject(expr, ATypeTag.BOOLEAN);
+        return iaObject != null ? ((ABoolean) iaObject).getBoolean() : null;
+    }
+
+    public static Integer getIntArgument(AbstractFunctionCallExpression f, int index) {
+        return getIntConstant(f.getArguments().get(index).getValue());
+    }
+
+    public static String getStringArgument(AbstractFunctionCallExpression f, int index) {
+        return getStringConstant(f.getArguments().get(index).getValue());
+    }
+
     public static Integer getIntArgument(ILogicalExpression expr, int index) {
         return expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL
                 ? getIntArgument((AbstractFunctionCallExpression) expr, index) : null;
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/formats/NonTaggedDataFormat.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/formats/NonTaggedDataFormat.java
index 8737360..5955e57 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/formats/NonTaggedDataFormat.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/formats/NonTaggedDataFormat.java
@@ -61,6 +61,7 @@
 import org.apache.asterix.om.types.AUnionType;
 import org.apache.asterix.om.types.AUnorderedListType;
 import org.apache.asterix.om.types.IAType;
+import org.apache.asterix.om.util.ConstantExpressionUtil;
 import org.apache.asterix.runtime.aggregates.collections.ListifyAggregateDescriptor;
 import org.apache.asterix.runtime.evaluators.common.CreateMBREvalFactory;
 import org.apache.asterix.runtime.evaluators.common.FunctionManagerImpl;
@@ -77,7 +78,6 @@
 import org.apache.hyracks.algebricks.common.exceptions.NotImplementedException;
 import org.apache.hyracks.algebricks.common.utils.Triple;
 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.base.LogicalVariable;
 import org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
 import org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression;
@@ -468,19 +468,15 @@
                 for (int i = 0; i < n; i++) {
                     Mutable<ILogicalExpression> argRef = expr.getArguments().get(2 * i);
                     ILogicalExpression arg = argRef.getValue();
-                    if (arg.getExpressionTag() == LogicalExpressionTag.CONSTANT) {
-                        String fn =
-                                ((AString) ((AsterixConstantValue) ((ConstantExpression) arg).getValue()).getObject())
-                                        .getStringValue();
-                        open[i] = true;
+                    open[i] = true;
+                    final String fn = ConstantExpressionUtil.getStringConstant(arg);
+                    if (fn != null) {
                         for (String s : recType.getFieldNames()) {
                             if (s.equals(fn)) {
                                 open[i] = false;
                                 break;
                             }
                         }
-                    } else {
-                        open[i] = true;
                     }
                 }
                 return open;