1. support bag-based field names and add a test case  2. fix ByNameToByIndexFieldAccessRule

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_opentype@277 eaa15691-b419-025a-1212-ee371bd00084
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java
index 2824d27..43fdf4b 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/base/RuleCollections.java
@@ -82,6 +82,9 @@
         normalization.add(new ExtractGbyExpressionsRule());
         normalization.add(new ExtractDistinctByExpressionsRule());
         normalization.add(new ExtractOrderExpressionsRule());
+
+        // TopdownTypeInferenceRule should go before IntroduceCastRecordRule to
+        // avoid unnecessary casting
         normalization.add(new TopDownTypeInferenceRule());
         normalization.add(new IntroduceCastRecordRule());
         normalization.add(new ConstantFoldingRule());
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ByNameToByIndexFieldAccessRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ByNameToByIndexFieldAccessRule.java
index e97961f..25412d8 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ByNameToByIndexFieldAccessRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ByNameToByIndexFieldAccessRule.java
@@ -1,6 +1,7 @@
 package edu.uci.ics.asterix.optimizer.rules;
 
 import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.commons.lang3.mutable.Mutable;
 import org.apache.commons.lang3.mutable.MutableObject;
@@ -41,7 +42,8 @@
     }
 
     @Override
-    public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
+    public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
+            throws AlgebricksException {
         AbstractLogicalOperator op = (AbstractLogicalOperator) opRef.getValue();
         if (op.getOperatorTag() != LogicalOperatorTag.ASSIGN) {
             return false;
@@ -55,64 +57,70 @@
         if (assign.getExpressions().get(0).getValue().getExpressionTag() != LogicalExpressionTag.FUNCTION_CALL) {
             return false;
         }
-        AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) assign.getExpressions().get(0)
-                .getValue();
-        if (fce.getFunctionIdentifier() != AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME) {
-            return false;
-        }
-        IVariableTypeEnvironment env = context.getOutputTypeEnvironment(op);
 
-        ILogicalExpression a0 = fce.getArguments().get(0).getValue();
-        if (a0.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
-            LogicalVariable var1 = context.newVar();
-            ArrayList<LogicalVariable> varArray = new ArrayList<LogicalVariable>(1);
-            varArray.add(var1);
-            ArrayList<Mutable<ILogicalExpression>> exprArray = new ArrayList<Mutable<ILogicalExpression>>(1);
-            exprArray.add(new MutableObject<ILogicalExpression>(a0));
-            AssignOperator assignVar = new AssignOperator(varArray, exprArray);
-            fce.getArguments().get(0).setValue(new VariableReferenceExpression(var1));
-            assignVar.getInputs().add(new MutableObject<ILogicalOperator>(assign.getInputs().get(0).getValue()));
-            assign.getInputs().get(0).setValue(assignVar);
-            context.computeAndSetTypeEnvironmentForOperator(assignVar);
-            context.computeAndSetTypeEnvironmentForOperator(assign);
-        }
-
-        IAType t = (IAType) env.getType(fce.getArguments().get(0).getValue());
-        switch (t.getTypeTag()) {
-            case ANY: {
-                return false;
+        List<Mutable<ILogicalExpression>> expressions = assign.getExpressions();
+        boolean changed = false;
+        for (int i = 0; i < expressions.size(); i++) {
+            AbstractFunctionCallExpression fce = (AbstractFunctionCallExpression) expressions.get(i).getValue();
+            if (fce.getFunctionIdentifier() != AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME) {
+                continue;
             }
-            case RECORD: {
-                ARecordType recType = (ARecordType) t;
-                ILogicalExpression fai = createFieldAccessByIndex(recType, fce);
-                if (fai == null) {
+            IVariableTypeEnvironment env = context.getOutputTypeEnvironment(op);
+
+            ILogicalExpression a0 = fce.getArguments().get(0).getValue();
+            if (a0.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                LogicalVariable var1 = context.newVar();
+                ArrayList<LogicalVariable> varArray = new ArrayList<LogicalVariable>(1);
+                varArray.add(var1);
+                ArrayList<Mutable<ILogicalExpression>> exprArray = new ArrayList<Mutable<ILogicalExpression>>(1);
+                exprArray.add(new MutableObject<ILogicalExpression>(a0));
+                AssignOperator assignVar = new AssignOperator(varArray, exprArray);
+                fce.getArguments().get(0).setValue(new VariableReferenceExpression(var1));
+                assignVar.getInputs().add(new MutableObject<ILogicalOperator>(assign.getInputs().get(0).getValue()));
+                assign.getInputs().get(0).setValue(assignVar);
+                context.computeAndSetTypeEnvironmentForOperator(assignVar);
+                context.computeAndSetTypeEnvironmentForOperator(assign);
+            }
+
+            IAType t = (IAType) env.getType(fce.getArguments().get(0).getValue());
+            switch (t.getTypeTag()) {
+                case ANY: {
                     return false;
                 }
-                assign.getExpressions().get(0).setValue(fai);
-                break;
-            }
-            case UNION: {
-                AUnionType unionT = (AUnionType) t;
-                if (unionT.isNullableType()) {
-                    IAType t2 = unionT.getUnionList().get(1);
-                    if (t2.getTypeTag() == ATypeTag.RECORD) {
-                        ARecordType recType = (ARecordType) t2;
-                        ILogicalExpression fai = createFieldAccessByIndex(recType, fce);
-                        if (fai == null) {
-                            return false;
-                        }
-                        assign.getExpressions().get(0).setValue(fai);
-                        break;
+                case RECORD: {
+                    ARecordType recType = (ARecordType) t;
+                    ILogicalExpression fai = createFieldAccessByIndex(recType, fce);
+                    if (fai == null) {
+                        return false;
                     }
+                    expressions.get(i).setValue(fai);
+                    changed = true;
+                    break;
                 }
-                throw new NotImplementedException("Union " + unionT);
-            }
-            default: {
-                throw new AlgebricksException("Cannot call field-access on data of type " + t);
+                case UNION: {
+                    AUnionType unionT = (AUnionType) t;
+                    if (unionT.isNullableType()) {
+                        IAType t2 = unionT.getUnionList().get(1);
+                        if (t2.getTypeTag() == ATypeTag.RECORD) {
+                            ARecordType recType = (ARecordType) t2;
+                            ILogicalExpression fai = createFieldAccessByIndex(recType, fce);
+                            if (fai == null) {
+                                return false;
+                            }
+                            expressions.get(i).setValue(fai);
+                            changed = true;
+                            break;
+                        }
+                    }
+                    throw new NotImplementedException("Union " + unionT);
+                }
+                default: {
+                    throw new AlgebricksException("Cannot call field-access on data of type " + t);
+                }
             }
         }
         assign.removeAnnotation(AsterixOperatorAnnotations.PUSHED_FIELD_ACCESS);
-        return true;
+        return changed;
     }
 
     private static ILogicalExpression createFieldAccessByIndex(ARecordType recType, AbstractFunctionCallExpression fce) {
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceCastRecordRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceCastRecordRule.java
index a63451a..fc96940 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceCastRecordRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceCastRecordRule.java
@@ -63,7 +63,13 @@
 
         List<LogicalVariable> usedVariables = new ArrayList<LogicalVariable>();
         VariableUtilities.getUsedVariables(oldAssignOperator, usedVariables);
-        LogicalVariable inputRecordVar = usedVariables.get(0);
+        LogicalVariable inputRecordVar;
+        if (usedVariables.size() > 0) {
+            inputRecordVar = usedVariables.get(0);
+        } else {
+            VariableUtilities.getLiveVariables(oldAssignOperator, usedVariables);
+            inputRecordVar = usedVariables.get(0);
+        }
         IVariableTypeEnvironment env = oldAssignOperator.computeInputTypeEnvironment(context);
         ARecordType inputRecordType = (ARecordType) env.getVarType(inputRecordVar);
 
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/LoadRecordFieldsRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/LoadRecordFieldsRule.java
index d5deb3c..0a68315 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/LoadRecordFieldsRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/LoadRecordFieldsRule.java
@@ -50,11 +50,13 @@
     }
 
     @Override
-    public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
+    public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
+            throws AlgebricksException {
         AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
         if (context.checkIfInDontApplySet(this, op1)) {
             return false;
         }
+
         if (op1.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
             AssignOperator a1 = (AssignOperator) op1;
             ILogicalExpression expr = getFirstExpr(a1);
@@ -209,7 +211,7 @@
      * @param toPushThroughChildRef
      */
     private static void pushAccessAboveOpRef(AssignOperator toPush, Mutable<ILogicalOperator> toPushThroughChildRef,
-            IOptimizationContext context) {
+            IOptimizationContext context) throws AlgebricksException {
         List<Mutable<ILogicalOperator>> tpInpList = toPush.getInputs();
         tpInpList.clear();
         tpInpList.add(new MutableObject<ILogicalOperator>(toPushThroughChildRef.getValue()));
@@ -233,7 +235,7 @@
      * 
      * @param toPush
      */
-    private static boolean findAndEliminateRedundantFieldAccess(AssignOperator assign) {
+    private static boolean findAndEliminateRedundantFieldAccess(AssignOperator assign) throws AlgebricksException {
         ILogicalExpression expr = getFirstExpr(assign);
         AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expr;
         ILogicalExpression arg0 = f.getArguments().get(0).getValue();
@@ -250,9 +252,20 @@
         if (f.getFunctionIdentifier().equals(AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME)) {
             String fldName = ((AString) ((AsterixConstantValue) ce.getValue()).getObject()).getStringValue();
             ILogicalExpression fldExpr = findFieldExpression(assign, recordVar, fldName);
+
             if (fldExpr != null) {
-                assign.getExpressions().get(0).setValue(fldExpr);
-                return true;
+                // check the liveness of the new expression
+                List<LogicalVariable> usedVariables = new ArrayList<LogicalVariable>();
+                fldExpr.getUsedVariables(usedVariables);
+                List<LogicalVariable> liveInputVars = new ArrayList<LogicalVariable>();
+                VariableUtilities.getLiveVariables(assign, liveInputVars);
+                usedVariables.removeAll(liveInputVars);
+                if (usedVariables.size() == 0) {
+                    assign.getExpressions().get(0).setValue(fldExpr);
+                    return true;
+                } else {
+                    return false;
+                }
             } else {
                 return false;
             }
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
index 4fd6dde..b1970ac 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/AqlExpressionToPlanTranslator.java
@@ -81,6 +81,7 @@
 import edu.uci.ics.asterix.metadata.MetadataTransactionContext;
 import edu.uci.ics.asterix.metadata.bootstrap.AsterixProperties;
 import edu.uci.ics.asterix.metadata.declared.AqlCompiledDatasetDecl;
+import edu.uci.ics.asterix.metadata.declared.AqlCompiledInternalDatasetDetails;
 import edu.uci.ics.asterix.metadata.declared.AqlCompiledMetadataDeclarations;
 import edu.uci.ics.asterix.metadata.declared.AqlDataSource;
 import edu.uci.ics.asterix.metadata.declared.AqlLogicalPlanAndMetadataImpl;
@@ -88,7 +89,6 @@
 import edu.uci.ics.asterix.metadata.declared.AqlSourceId;
 import edu.uci.ics.asterix.metadata.declared.FileSplitDataSink;
 import edu.uci.ics.asterix.metadata.declared.FileSplitSinkId;
-import edu.uci.ics.asterix.metadata.utils.DatasetUtils;
 import edu.uci.ics.asterix.om.base.AInt32;
 import edu.uci.ics.asterix.om.base.AString;
 import edu.uci.ics.asterix.om.constants.AsterixConstantValue;
@@ -142,7 +142,6 @@
 import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.WriteOperator;
 import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.WriteResultOperator;
 import edu.uci.ics.hyracks.algebricks.core.algebra.plan.ALogicalPlanImpl;
-import edu.uci.ics.hyracks.algebricks.runtime.base.IEvaluatorFactory;
 import edu.uci.ics.hyracks.api.io.FileReference;
 import edu.uci.ics.hyracks.dataflow.std.file.FileSplit;
 
@@ -150,10 +149,9 @@
  * Each visit returns a pair of an operator and a variable. The variable
  * corresponds to the new column, if any, added to the tuple flow. E.g., for
  * Unnest, the column is the variable bound to the elements in the list, for
- * Subplan it is null.
- * The first argument of a visit method is the expression which is translated.
- * The second argument of a visit method is the tuple source for the current
- * subtree.
+ * Subplan it is null. The first argument of a visit method is the expression
+ * which is translated. The second argument of a visit method is the tuple
+ * source for the current subtree.
  */
 
 public class AqlExpressionToPlanTranslator extends AbstractAqlTranslator implements
@@ -236,9 +234,18 @@
             ArrayList<LogicalVariable> vars = new ArrayList<LogicalVariable>();
             ArrayList<Mutable<ILogicalExpression>> exprs = new ArrayList<Mutable<ILogicalExpression>>();
             List<Mutable<ILogicalExpression>> varRefsForLoading = new ArrayList<Mutable<ILogicalExpression>>();
-            for (Triple<IEvaluatorFactory, ScalarFunctionCallExpression, IAType> partitioner : DatasetUtils
-                    .getPartitioningFunctions(adecl)) {
-                AbstractFunctionCallExpression f = partitioner.second.cloneExpression();
+
+            AqlCompiledInternalDatasetDetails datasetDetails = (AqlCompiledInternalDatasetDetails) adecl
+                    .getAqlCompiledDatasetDetails();
+            List<String> partitionKeys = datasetDetails.getPartitioningExprs();
+            for (String keyFieldName : partitionKeys) {
+                IFunctionInfo finfoAccess = AsterixBuiltinFunctions
+                        .getAsterixFunctionInfo(AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME);
+                @SuppressWarnings("unchecked")
+                ScalarFunctionCallExpression f = new ScalarFunctionCallExpression(finfoAccess,
+                        new MutableObject<ILogicalExpression>(new VariableReferenceExpression(METADATA_DUMMY_VAR)),
+                        new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(new AString(
+                                keyFieldName)))));
                 f.substituteVar(METADATA_DUMMY_VAR, resVar);
                 exprs.add(new MutableObject<ILogicalExpression>(f));
                 LogicalVariable v = context.newVar();
@@ -401,8 +408,8 @@
             Mutable<ILogicalOperator> tupSource) throws AsterixException {
         Pair<ILogicalExpression, Mutable<ILogicalOperator>> p = aqlExprToAlgExpression(fa.getExpr(), tupSource);
         LogicalVariable v = context.newVar();
-        AbstractFunctionCallExpression fldAccess = new ScalarFunctionCallExpression(FunctionUtils
-                .getFunctionInfo(AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME));
+        AbstractFunctionCallExpression fldAccess = new ScalarFunctionCallExpression(
+                FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.FIELD_ACCESS_BY_NAME));
         fldAccess.getArguments().add(new MutableObject<ILogicalExpression>(p.first));
         ILogicalExpression faExpr = new ConstantExpression(new AsterixConstantValue(new AString(fa.getIdent()
                 .getValue())));
@@ -421,8 +428,8 @@
         AbstractFunctionCallExpression f;
         int i = ia.getIndex();
         if (i == IndexAccessor.ANY) {
-            f = new ScalarFunctionCallExpression(FunctionUtils
-                    .getFunctionInfo(AsterixBuiltinFunctions.ANY_COLLECTION_MEMBER));
+            f = new ScalarFunctionCallExpression(
+                    FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.ANY_COLLECTION_MEMBER));
             f.getArguments().add(new MutableObject<ILogicalExpression>(p.first));
         } else {
             f = new ScalarFunctionCallExpression(FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.GET_ITEM));
@@ -469,7 +476,8 @@
             }
         }
 
-        FunctionIdentifier fi = new FunctionIdentifier(AlgebricksBuiltinFunctions.ALGEBRICKS_NS, fid.getFunctionName(), false);
+        FunctionIdentifier fi = new FunctionIdentifier(AlgebricksBuiltinFunctions.ALGEBRICKS_NS, fid.getFunctionName(),
+                false);
         AsterixFunctionInfo afi = AsterixBuiltinFunctions.lookupFunction(fi);
         FunctionIdentifier builtinAquafi = afi == null ? null : afi.getFunctionIdentifier();
 
@@ -486,8 +494,8 @@
         if (AsterixBuiltinFunctions.isBuiltinAggregateFunction(fi)) {
             f = AsterixBuiltinFunctions.makeAggregateFunctionExpression(fi, args);
         } else if (AsterixBuiltinFunctions.isBuiltinUnnestingFunction(fi)) {
-            UnnestingFunctionCallExpression ufce = new UnnestingFunctionCallExpression(FunctionUtils
-                    .getFunctionInfo(fi), args);
+            UnnestingFunctionCallExpression ufce = new UnnestingFunctionCallExpression(
+                    FunctionUtils.getFunctionInfo(fi), args);
             ufce.setReturnsUniqueValues(AsterixBuiltinFunctions.returnsUniqueValues(fi));
             f = ufce;
         } else {
@@ -590,9 +598,9 @@
         sel1.getInputs().add(new MutableObject<ILogicalOperator>(pThen.first));
 
         Pair<ILogicalOperator, LogicalVariable> pElse = ifexpr.getElseExpr().accept(this, nestedSource);
-        AbstractFunctionCallExpression notVarCond = new ScalarFunctionCallExpression(FunctionUtils
-                .getFunctionInfo(AlgebricksBuiltinFunctions.NOT), new MutableObject<ILogicalExpression>(
-                new VariableReferenceExpression(varCond)));
+        AbstractFunctionCallExpression notVarCond = new ScalarFunctionCallExpression(
+                FunctionUtils.getFunctionInfo(AlgebricksBuiltinFunctions.NOT), new MutableObject<ILogicalExpression>(
+                        new VariableReferenceExpression(varCond)));
         SelectOperator sel2 = new SelectOperator(new MutableObject<ILogicalExpression>(notVarCond));
         sel2.getInputs().add(new MutableObject<ILogicalOperator>(pElse.first));
 
@@ -605,10 +613,10 @@
         sp.getInputs().add(opCondRef);
 
         LogicalVariable resV = context.newVar();
-        AbstractFunctionCallExpression concatNonNull = new ScalarFunctionCallExpression(FunctionUtils
-                .getFunctionInfo(AsterixBuiltinFunctions.CONCAT_NON_NULL), new MutableObject<ILogicalExpression>(
-                new VariableReferenceExpression(pThen.second)), new MutableObject<ILogicalExpression>(
-                new VariableReferenceExpression(pElse.second)));
+        AbstractFunctionCallExpression concatNonNull = new ScalarFunctionCallExpression(
+                FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.CONCAT_NON_NULL),
+                new MutableObject<ILogicalExpression>(new VariableReferenceExpression(pThen.second)),
+                new MutableObject<ILogicalExpression>(new VariableReferenceExpression(pElse.second)));
         AssignOperator a = new AssignOperator(resV, new MutableObject<ILogicalExpression>(concatNonNull));
         a.getInputs().add(new MutableObject<ILogicalOperator>(sp));
 
@@ -716,9 +724,8 @@
             OrderModifier m = modifIter.next();
             OrderOperator.IOrder comp = (m == OrderModifier.ASC) ? OrderOperator.ASC_ORDER : OrderOperator.DESC_ORDER;
             ord.getOrderExpressions()
-                    .add(
-                            new Pair<IOrder, Mutable<ILogicalExpression>>(comp, new MutableObject<ILogicalExpression>(
-                                    p.first)));
+                    .add(new Pair<IOrder, Mutable<ILogicalExpression>>(comp, new MutableObject<ILogicalExpression>(
+                            p.first)));
             topOp = p.second;
         }
         ord.getInputs().add(topOp);
@@ -772,8 +779,8 @@
         } else { // EVERY
             List<Mutable<ILogicalExpression>> satExprList = new ArrayList<Mutable<ILogicalExpression>>(1);
             satExprList.add(new MutableObject<ILogicalExpression>(eo2.first));
-            s = new SelectOperator(new MutableObject<ILogicalExpression>(new ScalarFunctionCallExpression(FunctionUtils
-                    .getFunctionInfo(AlgebricksBuiltinFunctions.NOT), satExprList)));
+            s = new SelectOperator(new MutableObject<ILogicalExpression>(new ScalarFunctionCallExpression(
+                    FunctionUtils.getFunctionInfo(AlgebricksBuiltinFunctions.NOT), satExprList)));
             s.getInputs().add(eo2.second);
             fAgg = AsterixBuiltinFunctions.makeAggregateFunctionExpression(AsterixBuiltinFunctions.EMPTY_STREAM,
                     new ArrayList<Mutable<ILogicalExpression>>());
@@ -794,8 +801,8 @@
     @Override
     public Pair<ILogicalOperator, LogicalVariable> visitRecordConstructor(RecordConstructor rc,
             Mutable<ILogicalOperator> tupSource) throws AsterixException {
-        AbstractFunctionCallExpression f = new ScalarFunctionCallExpression(FunctionUtils
-                .getFunctionInfo(AsterixBuiltinFunctions.OPEN_RECORD_CONSTRUCTOR));
+        AbstractFunctionCallExpression f = new ScalarFunctionCallExpression(
+                FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.OPEN_RECORD_CONSTRUCTOR));
         LogicalVariable v1 = context.newVar();
         AssignOperator a = new AssignOperator(v1, new MutableObject<ILogicalExpression>(f));
         Mutable<ILogicalOperator> topOp = tupSource;
@@ -839,8 +846,8 @@
         if (u.getSign() == Sign.POSITIVE) {
             a = new AssignOperator(v1, new MutableObject<ILogicalExpression>(eo.first));
         } else {
-            AbstractFunctionCallExpression m = new ScalarFunctionCallExpression(FunctionUtils
-                    .getFunctionInfo(AsterixBuiltinFunctions.NUMERIC_UNARY_MINUS));
+            AbstractFunctionCallExpression m = new ScalarFunctionCallExpression(
+                    FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.NUMERIC_UNARY_MINUS));
             m.getArguments().add(new MutableObject<ILogicalExpression>(eo.first));
             a = new AssignOperator(v1, new MutableObject<ILogicalExpression>(m));
         }
@@ -1263,8 +1270,8 @@
     private ILogicalExpression makeUnnestExpression(ILogicalExpression expr) {
         switch (expr.getExpressionTag()) {
             case VARIABLE: {
-                return new UnnestingFunctionCallExpression(FunctionUtils
-                        .getFunctionInfo(AsterixBuiltinFunctions.SCAN_COLLECTION),
+                return new UnnestingFunctionCallExpression(
+                        FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.SCAN_COLLECTION),
                         new MutableObject<ILogicalExpression>(expr));
             }
             case FUNCTION_CALL: {
@@ -1272,8 +1279,8 @@
                 if (fce.getKind() == FunctionKind.UNNEST) {
                     return expr;
                 } else {
-                    return new UnnestingFunctionCallExpression(FunctionUtils
-                            .getFunctionInfo(AsterixBuiltinFunctions.SCAN_COLLECTION),
+                    return new UnnestingFunctionCallExpression(
+                            FunctionUtils.getFunctionInfo(AsterixBuiltinFunctions.SCAN_COLLECTION),
                             new MutableObject<ILogicalExpression>(expr));
                 }
             }
diff --git a/asterix-app/src/test/resources/runtimets/queries/dml/opentype-o2o.aql b/asterix-app/src/test/resources/runtimets/queries/dml/opentype-o2o.aql
index b2f6d14..6793180 100644
--- a/asterix-app/src/test/resources/runtimets/queries/dml/opentype-o2o.aql
+++ b/asterix-app/src/test/resources/runtimets/queries/dml/opentype-o2o.aql
@@ -3,8 +3,8 @@
 use dataverse testdv2;
 
 create type testtype as open {
-  id: string,
-  name: string
+  name: string,
+  id: string
 }
 
 create type testtype2 as open {
@@ -18,19 +18,19 @@
 create dataset testds2(testtype2) partitioned by key id;
  
 insert into dataset testds (
-{ "id": "001", "name": "Person Three", "hobby": "music"}
+{ "name": "Person One",  "id": "001", "hobby": "music"}
 );
 
 insert into dataset testds (
-{ "id": "002", "name": "Person Three", "hobby": "football", "city":"irvine"}
+{ "name": "Person Two", "id": "002",  "hobby": "football", "city":"irvine"}
 );
 
 insert into dataset testds (
-{ "id": "003", "name": "Person Three", "hobby": "movie"}
+{ "name": "Person Three",  "id": "003",  "hobby": "movie"}
 );
 
 insert into dataset testds (
-{ "id": "004", "name": "Person Three", "hobby": "swimming", "phone":"102-304-506"}
+{ "name": "Person Four", "id": "004",  "hobby": "swimming", "phone":"102-304-506"}
 );
 
 insert into dataset testds2 (
diff --git a/asterix-app/src/test/resources/runtimets/results/dml/opentype-o2o.adm b/asterix-app/src/test/resources/runtimets/results/dml/opentype-o2o.adm
index f5a5985..3b25366 100644
--- a/asterix-app/src/test/resources/runtimets/results/dml/opentype-o2o.adm
+++ b/asterix-app/src/test/resources/runtimets/results/dml/opentype-o2o.adm
@@ -1,4 +1,4 @@
-{ "id": "001", "name": "Person Three", "hobby": "music" }
-{ "id": "002", "name": "Person Three", "hobby": "football", "city": "irvine" }
+{ "id": "001", "name": "Person One", "hobby": "music" }
+{ "id": "002", "name": "Person Two", "hobby": "football", "city": "irvine" }
 { "id": "003", "name": "Person Three", "hobby": "movie" }
-{ "id": "004", "name": "Person Three", "hobby": "swimming", "phone": "102-304-506" }
+{ "id": "004", "name": "Person Four", "hobby": "swimming", "phone": "102-304-506" }