merge 965:1021 from asterix_stabilization to asterix_stabilization_printerfix_staging

git-svn-id: https://asterixdb.googlecode.com/svn/branches/asterix_stabilization_printerfix_staging@1022 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 f42dc1b..f63cc0c 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
@@ -28,7 +28,9 @@
 import edu.uci.ics.asterix.optimizer.rules.FuzzyEqRule;
 import edu.uci.ics.asterix.optimizer.rules.FuzzyJoinRule;
 import edu.uci.ics.asterix.optimizer.rules.IfElseToSwitchCaseFunctionRule;
+import edu.uci.ics.asterix.optimizer.rules.InlineUnnestFunctionRule;
 import edu.uci.ics.asterix.optimizer.rules.IntroduceDynamicTypeCastRule;
+import edu.uci.ics.asterix.optimizer.rules.IntroduceEnforcedListTypeRule;
 import edu.uci.ics.asterix.optimizer.rules.IntroduceSecondaryIndexInsertDeleteRule;
 import edu.uci.ics.asterix.optimizer.rules.IntroduceStaticTypeCastRule;
 import edu.uci.ics.asterix.optimizer.rules.LoadRecordFieldsRule;
@@ -96,6 +98,7 @@
 
     public final static List<IAlgebraicRewriteRule> buildTypeInferenceRuleCollection() {
         List<IAlgebraicRewriteRule> typeInfer = new LinkedList<IAlgebraicRewriteRule>();
+        typeInfer.add(new InlineUnnestFunctionRule());
         typeInfer.add(new InferTypesRule());
         return typeInfer;
     }
@@ -107,14 +110,15 @@
         normalization.add(new BreakSelectIntoConjunctsRule());
         normalization.add(new ExtractGbyExpressionsRule());
         normalization.add(new ExtractDistinctByExpressionsRule());
-        normalization.add(new ExtractOrderExpressionsRule());        
+        normalization.add(new ExtractOrderExpressionsRule());
         normalization.add(new ExtractCommonExpressionsRule());
-        
+
         // IntroduceStaticTypeCastRule should go before
         // IntroduceDynamicTypeCastRule to
         // avoid unnecessary dynamic casting
         normalization.add(new IntroduceStaticTypeCastRule());
         normalization.add(new IntroduceDynamicTypeCastRule());
+        normalization.add(new IntroduceEnforcedListTypeRule());
         normalization.add(new ConstantFoldingRule());
         normalization.add(new UnnestToDataScanRule());
         normalization.add(new IfElseToSwitchCaseFunctionRule());
@@ -125,7 +129,7 @@
 
     public final static List<IAlgebraicRewriteRule> buildCondPushDownAndJoinInferenceRuleCollection() {
         List<IAlgebraicRewriteRule> condPushDownAndJoinInference = new LinkedList<IAlgebraicRewriteRule>();
-                
+
         condPushDownAndJoinInference.add(new PushSelectDownRule());
         condPushDownAndJoinInference.add(new PushDieUpRule());
         condPushDownAndJoinInference.add(new RemoveRedundantListifyRule());
@@ -139,17 +143,17 @@
         condPushDownAndJoinInference.add(new IntroduceGroupByForSubplanRule());
         condPushDownAndJoinInference.add(new SubplanOutOfGroupRule());
         condPushDownAndJoinInference.add(new InsertOuterJoinRule());
-        
+
         condPushDownAndJoinInference.add(new RemoveRedundantVariablesRule());
         condPushDownAndJoinInference.add(new AsterixInlineVariablesRule());
         condPushDownAndJoinInference.add(new RemoveUnusedAssignAndAggregateRule());
-        
+
         condPushDownAndJoinInference.add(new FactorRedundantGroupAndDecorVarsRule());
         condPushDownAndJoinInference.add(new PushAggregateIntoGroupbyRule());
         condPushDownAndJoinInference.add(new EliminateSubplanRule());
         condPushDownAndJoinInference.add(new PushProperJoinThroughProduct());
         condPushDownAndJoinInference.add(new PushGroupByThroughProduct());
-        condPushDownAndJoinInference.add(new NestGroupByRule());                
+        condPushDownAndJoinInference.add(new NestGroupByRule());
 
         return condPushDownAndJoinInference;
     }
@@ -165,7 +169,7 @@
         fieldLoads.add(new RemoveUnusedAssignAndAggregateRule());
         fieldLoads.add(new ConstantFoldingRule());
         fieldLoads.add(new FeedScanCollectionToUnnest());
-        fieldLoads.add(new ComplexJoinInferenceRule());        
+        fieldLoads.add(new ComplexJoinInferenceRule());
         return fieldLoads;
     }
 
@@ -188,7 +192,7 @@
         consolidation.add(new RemoveRedundantGroupByDecorVars());
         return consolidation;
     }
-    
+
     public final static List<IAlgebraicRewriteRule> buildAccessMethodRuleCollection() {
         List<IAlgebraicRewriteRule> accessMethod = new LinkedList<IAlgebraicRewriteRule>();
         accessMethod.add(new IntroduceSelectAccessMethodRule());
@@ -201,7 +205,7 @@
     }
 
     public final static List<IAlgebraicRewriteRule> buildPlanCleanupRuleCollection() {
-        List<IAlgebraicRewriteRule> planCleanupRules = new LinkedList<IAlgebraicRewriteRule>();        
+        List<IAlgebraicRewriteRule> planCleanupRules = new LinkedList<IAlgebraicRewriteRule>();
         planCleanupRules.add(new PushAssignBelowUnionAllRule());
         planCleanupRules.add(new ExtractCommonExpressionsRule());
         planCleanupRules.add(new RemoveRedundantVariablesRule());
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ConstantFoldingRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ConstantFoldingRule.java
index c1d0fea..47e957b 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ConstantFoldingRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/ConstantFoldingRule.java
@@ -37,7 +37,10 @@
 import edu.uci.ics.asterix.om.base.IAObject;
 import edu.uci.ics.asterix.om.constants.AsterixConstantValue;
 import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
 import edu.uci.ics.asterix.om.types.ARecordType;
+import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.asterix.om.types.AbstractCollectionType;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
 import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
@@ -163,11 +166,23 @@
             if (!checkArgs(expr)) {
                 return new Pair<Boolean, ILogicalExpression>(changed, expr);
             }
-            // TODO: currently ARecord is always a closed record
+            //Current ARecord SerDe assumes a closed record, so we do not constant fold open record constructors
             if (expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.OPEN_RECORD_CONSTRUCTOR)
                     || expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.CAST_RECORD)) {
                 return new Pair<Boolean, ILogicalExpression>(false, null);
             }
+            //Current List SerDe assumes a strongly typed list, so we do not constant fold the list constructors if they are not strongly typed
+            if (expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR)
+                    || expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.ORDERED_LIST_CONSTRUCTOR)) {
+                AbstractCollectionType listType = (AbstractCollectionType) TypeComputerUtilities.getRequiredType(expr);
+                if (listType != null
+                        && (listType.getItemType().getTypeTag() == ATypeTag.ANY || listType.getItemType() instanceof AbstractCollectionType)) {
+                    //case1: listType == null,  could be a nested list inside a list<ANY>
+                    //case2: itemType = ANY
+                    //case3: itemType = a nested list
+                    return new Pair<Boolean, ILogicalExpression>(false, null);
+                }
+            }
             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)
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/InlineUnnestFunctionRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/InlineUnnestFunctionRule.java
new file mode 100644
index 0000000..3e57ded
--- /dev/null
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/InlineUnnestFunctionRule.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2009-2010 by The Regents of the University of California
+ * Licensed 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 from
+ * 
+ *     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 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;
+
+import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
+import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalOperator;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.IOptimizationContext;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalExpressionTag;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalOperatorTag;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalVariable;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
+import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AssignOperator;
+import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.UnnestOperator;
+import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.visitors.VariableUtilities;
+import edu.uci.ics.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
+
+/**
+ * This rule is to inline unnest functions that are hold by variables.
+ * This rule is to fix issue 201.
+ */
+public class InlineUnnestFunctionRule implements IAlgebraicRewriteRule {
+
+    @Override
+    public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
+        return false;
+    }
+
+    @Override
+    public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
+            throws AlgebricksException {
+        AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
+        if (context.checkIfInDontApplySet(this, op1))
+            return false;
+        context.addToDontApplySet(this, op1);
+        if (op1.getOperatorTag() != LogicalOperatorTag.UNNEST)
+            return false;
+        UnnestOperator unnestOperator = (UnnestOperator) op1;
+        AbstractFunctionCallExpression expr = (AbstractFunctionCallExpression) unnestOperator.getExpressionRef()
+                .getValue();
+        //we only inline for the scan-collection function
+        if (expr.getFunctionIdentifier() != AsterixBuiltinFunctions.SCAN_COLLECTION)
+            return false;
+
+        // inline all variables from an unnesting function call
+        AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
+        List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
+        for (int i = 0; i < args.size(); i++) {
+            ILogicalExpression argExpr = args.get(i).getValue();
+            if (argExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
+                VariableReferenceExpression varExpr = (VariableReferenceExpression) argExpr;
+                inlineVariable(varExpr.getVariableReference(), unnestOperator);
+            }
+        }
+        return true;
+    }
+
+    /**
+     * This method is to inline one variable
+     * 
+     * @param usedVar
+     *            A variable that is used by the scan-collection function in the unnest operator
+     * @param unnestOp
+     *            The unnest operator.
+     * @throws AlgebricksException
+     */
+    private void inlineVariable(LogicalVariable usedVar, UnnestOperator unnestOp) throws AlgebricksException {
+        AbstractFunctionCallExpression expr = (AbstractFunctionCallExpression) unnestOp.getExpressionRef().getValue();
+        List<Pair<AbstractFunctionCallExpression, Integer>> parentAndIndexList = new ArrayList<Pair<AbstractFunctionCallExpression, Integer>>();
+        getParentFunctionExpression(usedVar, expr, parentAndIndexList);
+        ILogicalExpression usedVarOrginExpr = findUsedVarOrigin(usedVar, unnestOp, (AbstractLogicalOperator) unnestOp
+                .getInputs().get(0).getValue());
+        if (usedVarOrginExpr != null) {
+            for (Pair<AbstractFunctionCallExpression, Integer> parentAndIndex : parentAndIndexList) {
+                //we only rewrite the top scan-collection function
+                if (parentAndIndex.first.getFunctionIdentifier() == AsterixBuiltinFunctions.SCAN_COLLECTION
+                        && parentAndIndex.first == expr) {
+                    unnestOp.getExpressionRef().setValue(usedVarOrginExpr);
+                }
+            }
+        }
+    }
+
+    private void getParentFunctionExpression(LogicalVariable usedVar, ILogicalExpression expr,
+            List<Pair<AbstractFunctionCallExpression, Integer>> parentAndIndexList) {
+        AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
+        List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
+        for (int i = 0; i < args.size(); i++) {
+            ILogicalExpression argExpr = args.get(i).getValue();
+            if (argExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
+                VariableReferenceExpression varExpr = (VariableReferenceExpression) argExpr;
+                if (varExpr.getVariableReference().equals(usedVar))
+                    parentAndIndexList.add(new Pair<AbstractFunctionCallExpression, Integer>(funcExpr, i));
+            }
+            if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                getParentFunctionExpression(usedVar, argExpr, parentAndIndexList);
+            }
+        }
+    }
+
+    private ILogicalExpression findUsedVarOrigin(LogicalVariable usedVar, AbstractLogicalOperator parentOp,
+            AbstractLogicalOperator currentOp) throws AlgebricksException {
+        ILogicalExpression ret = null;
+        if (currentOp.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
+            List<LogicalVariable> producedVars = new ArrayList<LogicalVariable>();
+            VariableUtilities.getProducedVariables(currentOp, producedVars);
+            if (producedVars.contains(usedVar)) {
+                AssignOperator assignOp = (AssignOperator) currentOp;
+                int index = assignOp.getVariables().indexOf(usedVar);
+                ILogicalExpression returnedExpr = assignOp.getExpressions().get(index).getValue();
+                if (returnedExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) returnedExpr;
+                    if (AsterixBuiltinFunctions.isBuiltinUnnestingFunction(funcExpr.getFunctionIdentifier())) {
+                        // we only inline for unnest functions
+                        removeUnecessaryAssign(parentOp, currentOp, assignOp, index);
+                        ret = returnedExpr;
+                    }
+                } else if (returnedExpr.getExpressionTag() == LogicalExpressionTag.VARIABLE) {
+                    //recusively inline
+                    VariableReferenceExpression varExpr = (VariableReferenceExpression) returnedExpr;
+                    LogicalVariable var = varExpr.getVariableReference();
+                    ILogicalExpression finalExpr = findUsedVarOrigin(var, currentOp,
+                            (AbstractLogicalOperator) currentOp.getInputs().get(0).getValue());
+                    if (finalExpr != null) {
+                        removeUnecessaryAssign(parentOp, currentOp, assignOp, index);
+                        ret = finalExpr;
+                    }
+                }
+            }
+        } else {
+            for (Mutable<ILogicalOperator> child : currentOp.getInputs()) {
+                ILogicalExpression expr = findUsedVarOrigin(usedVar, currentOp,
+                        (AbstractLogicalOperator) child.getValue());
+                if (expr != null) {
+                    ret = expr;
+                }
+            }
+        }
+        return ret;
+    }
+
+    private void removeUnecessaryAssign(AbstractLogicalOperator parentOp, AbstractLogicalOperator currentOp,
+            AssignOperator assignOp, int index) {
+        assignOp.getVariables().remove(index);
+        assignOp.getExpressions().remove(index);
+        if (assignOp.getVariables().size() == 0) {
+            int opIndex = parentOp.getInputs().indexOf(new MutableObject<ILogicalOperator>(currentOp));
+            parentOp.getInputs().get(opIndex).setValue(assignOp.getInputs().get(0).getValue());
+        }
+    }
+}
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceDynamicTypeCastRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceDynamicTypeCastRule.java
index c41d908..2dce5f6 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceDynamicTypeCastRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceDynamicTypeCastRule.java
@@ -48,22 +48,18 @@
  * recursive way. It enables: 1. bag-based fields in a record, 2. bidirectional
  * cast of a open field and a matched closed field, and 3. put in null fields
  * when necessary.
- * 
  * Here is an example: A record { "hobby": {{"music", "coding"}}, "id": "001",
  * "name": "Person Three"} which confirms to closed type ( id: string, name:
  * string, hobby: {{string}}? ) can be cast to an open type (id: string ), or
  * vice versa.
- * 
  * However, if the input record is a variable, then we don't know its exact
  * field layout at compile time. For example, records conforming to the same
  * type can have different field orderings and different open parts. That's why
  * we need dynamic type casting.
- * 
  * Note that as we can see in the example, the ordering of fields of a record is
  * not required. Since the open/closed part of a record has completely different
  * underlying memory/storage layout, a cast-record function will change the
  * layout as specified at runtime.
- * 
  * Implementation wise, this rule checks the target dataset type and the input
  * record type, and if the types are different, then it plugs in an assign with
  * a cast-record function, and projects away the original (uncast) field.
@@ -80,9 +76,7 @@
             throws AlgebricksException {
         /**
          * pattern match: sink insert assign
-         * 
          * resulting plan: sink-insert-project-assign
-         * 
          */
         AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
         if (op1.getOperatorTag() != LogicalOperatorTag.SINK)
@@ -90,6 +84,9 @@
         AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
         if (op2.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE)
             return false;
+        InsertDeleteOperator insertDeleteOp = (InsertDeleteOperator) op2;
+        if (insertDeleteOp.getOperation() == InsertDeleteOperator.Kind.DELETE)
+            return false;
         AbstractLogicalOperator op3 = (AbstractLogicalOperator) op2.getInputs().get(0).getValue();
         if (op3.getOperatorTag() != LogicalOperatorTag.ASSIGN)
             return false;
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceEnforcedListTypeRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceEnforcedListTypeRule.java
new file mode 100644
index 0000000..2e8d530
--- /dev/null
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceEnforcedListTypeRule.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2009-2010 by The Regents of the University of California
+ * Licensed 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 from
+ * 
+ *     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 edu.uci.ics.asterix.optimizer.rules;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.commons.lang3.mutable.Mutable;
+
+import edu.uci.ics.asterix.om.types.IAType;
+import edu.uci.ics.asterix.optimizer.rules.typecast.StaticTypeCastUtil;
+import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalOperator;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.IOptimizationContext;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalExpressionTag;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalOperatorTag;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment;
+import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AbstractAssignOperator;
+import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
+import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AbstractUnnestOperator;
+import edu.uci.ics.hyracks.algebricks.core.rewriter.base.IAlgebraicRewriteRule;
+
+/**
+ * This class is to enforce types for function expressions which contain list constructor function calls.
+ * The List constructor is very special because a nested list is of type List<ANY>.
+ * However, the bottom-up type inference (InferTypeRule in algebricks) did not infer that so we need this method to enforce the type.
+ * We do not want to break the generality of algebricks so this method is called in an ASTERIX rule: @ IntroduceEnforcedListTypeRule} .
+ */
+public class IntroduceEnforcedListTypeRule implements IAlgebraicRewriteRule {
+
+    @Override
+    public boolean rewritePre(Mutable<ILogicalOperator> opRef, IOptimizationContext context) throws AlgebricksException {
+        return false;
+    }
+
+    @Override
+    public boolean rewritePost(Mutable<ILogicalOperator> opRef, IOptimizationContext context)
+            throws AlgebricksException {
+        if (context.checkIfInDontApplySet(this, opRef.getValue()))
+            return false;
+        AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
+        context.addToDontApplySet(this, opRef.getValue());
+        boolean changed = false;
+
+        /**
+         * rewrite list constructor types for list constructor functions
+         */
+        if (op1.getOperatorTag() == LogicalOperatorTag.ASSIGN) {
+            AbstractAssignOperator assignOp = (AbstractAssignOperator) op1;
+            List<Mutable<ILogicalExpression>> expressions = assignOp.getExpressions();
+            IVariableTypeEnvironment env = assignOp.computeOutputTypeEnvironment(context);
+            changed = rewriteExpressions(expressions, env);
+        }
+        if (op1.getOperatorTag() == LogicalOperatorTag.UNNEST) {
+            AbstractUnnestOperator unnestOp = (AbstractUnnestOperator) op1;
+            List<Mutable<ILogicalExpression>> expressions = Collections.singletonList(unnestOp.getExpressionRef());
+            IVariableTypeEnvironment env = unnestOp.computeOutputTypeEnvironment(context);
+            changed = rewriteExpressions(expressions, env);
+        }
+        return changed;
+    }
+
+    private boolean rewriteExpressions(List<Mutable<ILogicalExpression>> expressions, IVariableTypeEnvironment env)
+            throws AlgebricksException {
+        boolean changed = false;
+        for (Mutable<ILogicalExpression> exprRef : expressions) {
+            ILogicalExpression expr = exprRef.getValue();
+            if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                AbstractFunctionCallExpression argFuncExpr = (AbstractFunctionCallExpression) expr;
+                IAType exprType = (IAType) env.getType(argFuncExpr);
+                changed = changed || StaticTypeCastUtil.rewriteListExpr(argFuncExpr, exprType, exprType, env);
+            }
+        }
+        return changed;
+    }
+
+}
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java
index 7dc35fe..3aae2dd 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/IntroduceStaticTypeCastRule.java
@@ -16,26 +16,14 @@
 package edu.uci.ics.asterix.optimizer.rules;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 
 import org.apache.commons.lang3.mutable.Mutable;
-import org.apache.commons.lang3.mutable.MutableObject;
 
 import edu.uci.ics.asterix.metadata.declared.AqlDataSource;
-import edu.uci.ics.asterix.om.base.ANull;
-import edu.uci.ics.asterix.om.base.AString;
-import edu.uci.ics.asterix.om.constants.AsterixConstantValue;
-import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
 import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
-import edu.uci.ics.asterix.om.types.ARecordType;
-import edu.uci.ics.asterix.om.types.ATypeTag;
-import edu.uci.ics.asterix.om.types.AUnionType;
-import edu.uci.ics.asterix.om.types.AbstractCollectionType;
-import edu.uci.ics.asterix.om.types.BuiltinType;
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
-import edu.uci.ics.asterix.runtime.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.optimizer.rules.typecast.StaticTypeCastUtil;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalOperator;
@@ -44,9 +32,7 @@
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalOperatorTag;
 import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalVariable;
 import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
-import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.ConstantExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment;
-import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression;
 import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AbstractLogicalOperator;
 import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.AssignOperator;
 import edu.uci.ics.hyracks.algebricks.core.algebra.operators.logical.InsertDeleteOperator;
@@ -58,22 +44,17 @@
  * recursive way. It enables: 1. bag-based fields in a record, 2. bidirectional
  * cast of a open field and a matched closed field, and 3. put in null fields
  * when necessary. It should be fired before the constant folding rule.
- * 
  * This rule is not responsible for type casting between primitive types.
- * 
  * Here is an example: A record { "hobby": {{"music", "coding"}}, "id": "001",
  * "name": "Person Three"} which confirms to closed type ( id: string, name:
  * string, hobby: {{string}}? ) can be cast to an open type (id: string ), or
  * vice versa.
- * 
  * Implementation wise: first, we match the record's type and its target dataset
  * type to see if it is "cast-able"; second, if the types are cast-able, we
  * embed the required type into the original producer expression. If the types
  * are not cast-able, we throw a compile time exception.
- * 
  * Then, at runtime (not in this rule), the corresponding record/list
  * constructors know what to do by checking the required output type.
- * 
  * TODO: right now record/list constructor of the cast result is not done in the
  * ConstantFoldingRule and has to go to the runtime, because the
  * ConstantFoldingRule uses ARecordSerializerDeserializer which seems to have
@@ -96,28 +77,31 @@
         if (context.checkIfInDontApplySet(this, opRef.getValue()))
             return false;
         context.addToDontApplySet(this, opRef.getValue());
+
         AbstractLogicalOperator op1 = (AbstractLogicalOperator) opRef.getValue();
+        List<LogicalVariable> producedVariables = new ArrayList<LogicalVariable>();
+        LogicalVariable oldRecordVariable;
+
         if (op1.getOperatorTag() != LogicalOperatorTag.SINK)
             return false;
         AbstractLogicalOperator op2 = (AbstractLogicalOperator) op1.getInputs().get(0).getValue();
         if (op2.getOperatorTag() != LogicalOperatorTag.INSERT_DELETE)
             return false;
-        AbstractLogicalOperator op3 = (AbstractLogicalOperator) op2.getInputs().get(0).getValue();
-        if (op3.getOperatorTag() != LogicalOperatorTag.ASSIGN)
+        InsertDeleteOperator insertDeleteOp = (InsertDeleteOperator) op2;
+        if (insertDeleteOp.getOperation() == InsertDeleteOperator.Kind.DELETE)
             return false;
-
+        AbstractLogicalOperator assignOp = (AbstractLogicalOperator) op2.getInputs().get(0).getValue();
+        if (assignOp.getOperatorTag() != LogicalOperatorTag.ASSIGN)
+            return false;
         /**
          * get required record type
          */
         InsertDeleteOperator insertDeleteOperator = (InsertDeleteOperator) op2;
-        AssignOperator topAssignOperator = (AssignOperator) op3;
         AqlDataSource dataSource = (AqlDataSource) insertDeleteOperator.getDataSource();
         IAType[] schemaTypes = (IAType[]) dataSource.getSchemaTypes();
-        ARecordType requiredRecordType = (ARecordType) schemaTypes[schemaTypes.length - 1];
+        IAType requiredRecordType = schemaTypes[schemaTypes.length - 1];
 
-        /**
-         * get input record type to the insert operator
-         */
+        AssignOperator topAssignOperator = (AssignOperator) assignOp;
         List<LogicalVariable> usedVariables = new ArrayList<LogicalVariable>();
         VariableUtilities.getUsedVariables(topAssignOperator, usedVariables);
 
@@ -126,14 +110,12 @@
         // empty
         if (usedVariables.size() == 0)
             return false;
-        LogicalVariable oldRecordVariable = usedVariables.get(0);
+        oldRecordVariable = usedVariables.get(0);
         LogicalVariable inputRecordVar = usedVariables.get(0);
         IVariableTypeEnvironment env = topAssignOperator.computeOutputTypeEnvironment(context);
-        ARecordType inputRecordType = (ARecordType) env.getVarType(inputRecordVar);
+        IAType inputRecordType = (IAType) env.getVarType(inputRecordVar);
 
-        AbstractLogicalOperator currentOperator = topAssignOperator;
-        List<LogicalVariable> producedVariables = new ArrayList<LogicalVariable>();
-
+        AbstractLogicalOperator currentOperator = assignOp;
         /**
          * find the assign operator for the "input record" to the insert_delete
          * operator
@@ -153,13 +135,15 @@
                     List<Mutable<ILogicalExpression>> expressionRefs = originalAssign.getExpressions();
                     ILogicalExpression expr = expressionRefs.get(position).getValue();
                     if (expr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
-                        ScalarFunctionCallExpression funcExpr = (ScalarFunctionCallExpression) expr;
+                        AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
                         // that expression has been rewritten, and it will not
                         // fail but just return false
-                        if (TypeComputerUtilities.getRequiredType(funcExpr) != null)
+                        if (TypeComputerUtilities.getRequiredType(funcExpr) != null) {
+                            context.computeAndSetTypeEnvironmentForOperator(assignOp);
                             return false;
-                        IVariableTypeEnvironment assignEnv = topAssignOperator.computeOutputTypeEnvironment(context);
-                        rewriteFuncExpr(funcExpr, requiredRecordType, inputRecordType, assignEnv);
+                        }
+                        IVariableTypeEnvironment assignEnv = assignOp.computeOutputTypeEnvironment(context);
+                        StaticTypeCastUtil.rewriteFuncExpr(funcExpr, requiredRecordType, inputRecordType, assignEnv);
                     }
                     context.computeAndSetTypeEnvironmentForOperator(originalAssign);
                 }
@@ -172,249 +156,4 @@
         return true;
     }
 
-    private void rewriteFuncExpr(ScalarFunctionCallExpression funcExpr, IAType reqType, IAType inputType,
-            IVariableTypeEnvironment env) throws AlgebricksException {
-        if (funcExpr.getFunctionIdentifier() == AsterixBuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR) {
-            rewriteListFuncExpr(funcExpr, (AbstractCollectionType) reqType, (AbstractCollectionType) inputType, env);
-        } else if (funcExpr.getFunctionIdentifier() == AsterixBuiltinFunctions.ORDERED_LIST_CONSTRUCTOR) {
-            rewriteListFuncExpr(funcExpr, (AbstractCollectionType) reqType, (AbstractCollectionType) inputType, env);
-        } else if (reqType.getTypeTag().equals(ATypeTag.RECORD)) {
-            rewriteRecordFuncExpr(funcExpr, (ARecordType) reqType, (ARecordType) inputType, env);
-        }
-    }
-
-    /**
-     * only called when funcExpr is record constructor
-     * 
-     * @param funcExpr
-     *            record constructor function expression
-     * @param requiredListType
-     *            required record type
-     * @param inputRecordType
-     * @param env
-     *            type environment
-     * @throws AlgebricksException
-     */
-    private void rewriteRecordFuncExpr(ScalarFunctionCallExpression funcExpr, ARecordType requiredRecordType,
-            ARecordType inputRecordType, IVariableTypeEnvironment env) throws AlgebricksException {
-        // if already rewritten, the required type is not null
-        if (TypeComputerUtilities.getRequiredType(funcExpr) != null)
-            return;
-        TypeComputerUtilities.setRequiredAndInputTypes(funcExpr, requiredRecordType, inputRecordType);
-        staticRecordTypeCast(funcExpr, requiredRecordType, inputRecordType, env);
-    }
-
-    /**
-     * only called when funcExpr is list constructor
-     * 
-     * @param funcExpr
-     *            list constructor function expression
-     * @param requiredListType
-     *            required list type
-     * @param inputListType
-     * @param env
-     *            type environment
-     * @throws AlgebricksException
-     */
-    private void rewriteListFuncExpr(ScalarFunctionCallExpression funcExpr, AbstractCollectionType requiredListType,
-            AbstractCollectionType inputListType, IVariableTypeEnvironment env) throws AlgebricksException {
-        if (TypeComputerUtilities.getRequiredType(funcExpr) != null)
-            return;
-
-        TypeComputerUtilities.setRequiredAndInputTypes(funcExpr, requiredListType, inputListType);
-        List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
-
-        IAType itemType = requiredListType.getItemType();
-        if (itemType == null || itemType.getTypeTag().equals(ATypeTag.ANY))
-            return;
-        IAType inputItemType = inputListType.getItemType();
-        for (int j = 0; j < args.size(); j++) {
-            ILogicalExpression arg = args.get(j).getValue();
-            if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
-                ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) arg;
-                IAType currentItemType = (IAType) env.getType(argFunc);
-                if (inputItemType == null || inputItemType == BuiltinType.ANY) {
-                    currentItemType = (IAType) env.getType(argFunc);
-                    rewriteFuncExpr(argFunc, itemType, currentItemType, env);
-                } else {
-                    rewriteFuncExpr(argFunc, itemType, inputItemType, env);
-                }
-            }
-        }
-    }
-
-    private void staticRecordTypeCast(ScalarFunctionCallExpression func, ARecordType reqType, ARecordType inputType,
-            IVariableTypeEnvironment env) throws AlgebricksException {
-        IAType[] reqFieldTypes = reqType.getFieldTypes();
-        String[] reqFieldNames = reqType.getFieldNames();
-        IAType[] inputFieldTypes = inputType.getFieldTypes();
-        String[] inputFieldNames = inputType.getFieldNames();
-
-        int[] fieldPermutation = new int[reqFieldTypes.length];
-        boolean[] nullFields = new boolean[reqFieldTypes.length];
-        boolean[] openFields = new boolean[inputFieldTypes.length];
-
-        Arrays.fill(nullFields, false);
-        Arrays.fill(openFields, true);
-        Arrays.fill(fieldPermutation, -1);
-
-        // forward match: match from actual to required
-        boolean matched = false;
-        for (int i = 0; i < inputFieldNames.length; i++) {
-            String fieldName = inputFieldNames[i];
-            IAType fieldType = inputFieldTypes[i];
-
-            if (2 * i + 1 > func.getArguments().size())
-                throw new AlgebricksException("expression index out of bound");
-
-            // 2*i+1 is the index of field value expression
-            ILogicalExpression arg = func.getArguments().get(2 * i + 1).getValue();
-            matched = false;
-            for (int j = 0; j < reqFieldNames.length; j++) {
-                String reqFieldName = reqFieldNames[j];
-                IAType reqFieldType = reqFieldTypes[j];
-                if (fieldName.equals(reqFieldName)) {
-                    if (fieldType.equals(reqFieldType)) {
-                        fieldPermutation[j] = i;
-                        openFields[i] = false;
-                        matched = true;
-
-                        if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
-                            ScalarFunctionCallExpression scalarFunc = (ScalarFunctionCallExpression) arg;
-                            rewriteFuncExpr(scalarFunc, reqFieldType, fieldType, env);
-                        }
-                        break;
-                    }
-
-                    // match the optional field
-                    if (reqFieldType.getTypeTag() == ATypeTag.UNION
-                            && NonTaggedFormatUtil.isOptionalField((AUnionType) reqFieldType)) {
-                        IAType itemType = ((AUnionType) reqFieldType).getUnionList().get(
-                                NonTaggedFormatUtil.OPTIONAL_TYPE_INDEX_IN_UNION_LIST);
-                        reqFieldType = itemType;
-                        if (fieldType.equals(BuiltinType.ANULL) || fieldType.equals(itemType)) {
-                            fieldPermutation[j] = i;
-                            openFields[i] = false;
-                            matched = true;
-
-                            // rewrite record expr
-                            if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
-                                ScalarFunctionCallExpression scalarFunc = (ScalarFunctionCallExpression) arg;
-                                rewriteFuncExpr(scalarFunc, reqFieldType, fieldType, env);
-                            }
-                            break;
-                        }
-                    }
-
-                    // match the record field: need cast
-                    if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
-                        ScalarFunctionCallExpression scalarFunc = (ScalarFunctionCallExpression) arg;
-                        rewriteFuncExpr(scalarFunc, reqFieldType, fieldType, env);
-                        fieldPermutation[j] = i;
-                        openFields[i] = false;
-                        matched = true;
-                        break;
-                    }
-                }
-            }
-            // the input has extra fields
-            if (!matched && !reqType.isOpen())
-                throw new AlgebricksException("static type mismatch: including an extra closed field " + fieldName);
-        }
-
-        // backward match: match from required to actual
-        for (int i = 0; i < reqFieldNames.length; i++) {
-            String reqFieldName = reqFieldNames[i];
-            IAType reqFieldType = reqFieldTypes[i];
-            matched = false;
-            for (int j = 0; j < inputFieldNames.length; j++) {
-                String fieldName = inputFieldNames[j];
-                IAType fieldType = inputFieldTypes[j];
-                if (!fieldName.equals(reqFieldName))
-                    continue;
-                // should check open field here
-                // because number of entries in fieldPermuations is the
-                // number of required schema fields
-                // here we want to check if an input field is matched
-                // the entry index of fieldPermuatons is req field index
-                if (!openFields[j]) {
-                    matched = true;
-                    break;
-                }
-
-                // match the optional field
-                if (reqFieldType.getTypeTag() == ATypeTag.UNION
-                        && NonTaggedFormatUtil.isOptionalField((AUnionType) reqFieldType)) {
-                    IAType itemType = ((AUnionType) reqFieldType).getUnionList().get(
-                            NonTaggedFormatUtil.OPTIONAL_TYPE_INDEX_IN_UNION_LIST);
-                    if (fieldType.equals(BuiltinType.ANULL) || fieldType.equals(itemType)) {
-                        matched = true;
-                        break;
-                    }
-                }
-            }
-            if (matched)
-                continue;
-
-            if (reqFieldType.getTypeTag() == ATypeTag.UNION
-                    && NonTaggedFormatUtil.isOptionalField((AUnionType) reqFieldType)) {
-                // add a null field
-                nullFields[i] = true;
-            } else {
-                // no matched field in the input for a required closed field
-                throw new AlgebricksException("static type mismatch: miss a required closed field " + reqFieldName);
-            }
-        }
-
-        List<Mutable<ILogicalExpression>> arguments = func.getArguments();
-        List<Mutable<ILogicalExpression>> originalArguments = new ArrayList<Mutable<ILogicalExpression>>();
-        originalArguments.addAll(arguments);
-        arguments.clear();
-        // re-order the closed part and fill in null fields
-        for (int i = 0; i < fieldPermutation.length; i++) {
-            int pos = fieldPermutation[i];
-            if (pos >= 0) {
-                arguments.add(originalArguments.get(2 * pos));
-                arguments.add(originalArguments.get(2 * pos + 1));
-            }
-            if (nullFields[i]) {
-                // add a null field
-                arguments.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(
-                        new AString(reqFieldNames[i])))));
-                arguments.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(
-                        ANull.NULL))));
-            }
-        }
-
-        // add the open part
-        for (int i = 0; i < openFields.length; i++) {
-            if (openFields[i]) {
-                arguments.add(originalArguments.get(2 * i));
-                Mutable<ILogicalExpression> fExprRef = originalArguments.get(2 * i + 1);
-                ILogicalExpression argExpr = fExprRef.getValue();
-
-                // we need to handle open fields recursively by their default
-                // types
-                // for list, their item type is any
-                // for record, their
-                if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
-                    IAType reqFieldType = inputFieldTypes[i];
-                    if (inputFieldTypes[i].getTypeTag() == ATypeTag.RECORD) {
-                        reqFieldType = DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
-                    }
-                    if (inputFieldTypes[i].getTypeTag() == ATypeTag.ORDEREDLIST) {
-                        reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AORDERED_LIST_TYPE;
-                    }
-                    if (inputFieldTypes[i].getTypeTag() == ATypeTag.UNORDEREDLIST) {
-                        reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
-                    }
-                    if (TypeComputerUtilities.getRequiredType((AbstractFunctionCallExpression) argExpr) == null) {
-                        ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) argExpr;
-                        rewriteFuncExpr(argFunc, reqFieldType, inputFieldTypes[i], env);
-                    }
-                }
-                arguments.add(fExprRef);
-            }
-        }
-    }
 }
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/SetClosedRecordConstructorsRule.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/SetClosedRecordConstructorsRule.java
index c121ff6..ad70d6f 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/SetClosedRecordConstructorsRule.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/SetClosedRecordConstructorsRule.java
@@ -92,47 +92,55 @@
             boolean changed = false;
             if (expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.OPEN_RECORD_CONSTRUCTOR)) {
                 ARecordType reqType = (ARecordType) TypeComputerUtilities.getRequiredType(expr);
-                if (reqType != null) {
-                    if (reqType.isOpen())
-                        allClosed = false;
-                }
-                int n = expr.getArguments().size();
-                if (n % 2 > 0) {
-                    throw new AlgebricksException("Record constructor expected to have an even number of arguments: "
-                            + expr);
-                }
-                for (int i = 0; i < n / 2; i++) {
-                    ILogicalExpression a0 = expr.getArguments().get(2 * i).getValue();
-                    if (a0.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
-                        allClosed = false;
+                if (reqType == null || !reqType.isOpen()) {
+                    int n = expr.getArguments().size();
+                    if (n % 2 > 0) {
+                        throw new AlgebricksException(
+                                "Record constructor expected to have an even number of arguments: " + expr);
                     }
-                    Mutable<ILogicalExpression> aRef1 = expr.getArguments().get(2 * i + 1);
-                    ILogicalExpression a1 = aRef1.getValue();
-                    ClosedDataInfo cdi = a1.accept(this, arg);
-                    if (!cdi.dataIsClosed) {
-                        allClosed = false;
+                    for (int i = 0; i < n / 2; i++) {
+                        ILogicalExpression a0 = expr.getArguments().get(2 * i).getValue();
+                        if (a0.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
+                            allClosed = false;
+                        }
+                        Mutable<ILogicalExpression> aRef1 = expr.getArguments().get(2 * i + 1);
+                        ILogicalExpression a1 = aRef1.getValue();
+                        ClosedDataInfo cdi = a1.accept(this, arg);
+                        if (!cdi.dataIsClosed) {
+                            allClosed = false;
+                        }
+                        if (cdi.expressionChanged) {
+                            aRef1.setValue(cdi.expression);
+                            changed = true;
+                        }
                     }
-                    if (cdi.expressionChanged) {
-                        aRef1.setValue(cdi.expression);
+                    if (allClosed) {
+                        expr.setFunctionInfo(FunctionUtils
+                                .getFunctionInfo(AsterixBuiltinFunctions.CLOSED_RECORD_CONSTRUCTOR));
+                        GlobalConfig.ASTERIX_LOGGER.finest("Switching to CLOSED record constructor in " + expr + ".\n");
                         changed = true;
                     }
                 }
-                if (allClosed) {
-                    expr.setFunctionInfo(FunctionUtils
-                            .getFunctionInfo(AsterixBuiltinFunctions.CLOSED_RECORD_CONSTRUCTOR));
-                    GlobalConfig.ASTERIX_LOGGER.finest("Switching to CLOSED record constructor in " + expr + ".\n");
-                    changed = true;
-                }
             } else {
-                for (Mutable<ILogicalExpression> e : expr.getArguments()) {
-                    ILogicalExpression ale = e.getValue();
-                    ClosedDataInfo cdi = ale.accept(this, arg);
-                    if (!cdi.dataIsClosed) {
-                        allClosed = false;
+                boolean rewrite = true;
+                if (expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.ORDERED_LIST_CONSTRUCTOR)
+                        || (expr.getFunctionIdentifier().equals(AsterixBuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR))) {
+                    IAType reqType = TypeComputerUtilities.getRequiredType(expr);
+                    if (reqType == null) {
+                        rewrite = false;
                     }
-                    if (cdi.expressionChanged) {
-                        e.setValue(cdi.expression);
-                        changed = true;
+                }
+                if (rewrite) {
+                    for (Mutable<ILogicalExpression> e : expr.getArguments()) {
+                        ILogicalExpression ale = e.getValue();
+                        ClosedDataInfo cdi = ale.accept(this, arg);
+                        if (!cdi.dataIsClosed) {
+                            allClosed = false;
+                        }
+                        if (cdi.expressionChanged) {
+                            e.setValue(cdi.expression);
+                            changed = true;
+                        }
                     }
                 }
             }
@@ -144,7 +152,8 @@
                 throws AlgebricksException {
             Object varType = env.getVarType(expr.getVariableReference());
             if (varType == null) {
-                throw new AlgebricksException("Could not infer type for variable '" + expr.getVariableReference() + "'.");
+                throw new AlgebricksException("Could not infer type for variable '" + expr.getVariableReference()
+                        + "'.");
             }
             boolean dataIsClosed = isClosedRec((IAType) varType);
             return new ClosedDataInfo(false, dataIsClosed, expr);
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/typecast/StaticTypeCastUtil.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/typecast/StaticTypeCastUtil.java
new file mode 100644
index 0000000..0c6f2ea
--- /dev/null
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/optimizer/rules/typecast/StaticTypeCastUtil.java
@@ -0,0 +1,408 @@
+/*
+ * Copyright 2009-2010 by The Regents of the University of California
+ * Licensed 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 from
+ * 
+ *     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 edu.uci.ics.asterix.optimizer.rules.typecast;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.commons.lang3.mutable.Mutable;
+import org.apache.commons.lang3.mutable.MutableObject;
+
+import edu.uci.ics.asterix.om.base.ANull;
+import edu.uci.ics.asterix.om.base.AString;
+import edu.uci.ics.asterix.om.constants.AsterixConstantValue;
+import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
+import edu.uci.ics.asterix.om.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.om.typecomputer.base.TypeComputerUtilities;
+import edu.uci.ics.asterix.om.types.ARecordType;
+import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.asterix.om.types.AUnionType;
+import edu.uci.ics.asterix.om.types.AbstractCollectionType;
+import edu.uci.ics.asterix.om.types.BuiltinType;
+import edu.uci.ics.asterix.om.types.IAType;
+import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
+import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.ILogicalExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.base.LogicalExpressionTag;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.ConstantExpression;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment;
+import edu.uci.ics.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression;
+
+/**
+ * This class is utility to do type cast.
+ * It offers two public methods:
+ * 1. public static boolean rewriteListExpr(AbstractFunctionCallExpression funcExpr, IAType reqType, IAType inputType,
+ * IVariableTypeEnvironment env) throws AlgebricksException, which only enforces the list type recursively.
+ * 2. public static boolean rewriteFuncExpr(AbstractFunctionCallExpression funcExpr, IAType reqType, IAType inputType,
+ * IVariableTypeEnvironment env) throws AlgebricksException, which enforces the list type and the record type recursively.
+ * 
+ * @author yingyib
+ */
+public class StaticTypeCastUtil {
+
+    /**
+     * This method is only called when funcExpr contains list constructor function calls.
+     * The List constructor is very special because a nested list is of type List<ANY>.
+     * However, the bottom-up type inference (InferTypeRule in algebricks) did not infer that so we need this method to enforce the type.
+     * We do not want to break the generality of algebricks so this method is called in an ASTERIX rule: @ IntroduceEnforcedListTypeRule} .
+     * 
+     * @param funcExpr
+     *            record constructor function expression
+     * @param requiredListType
+     *            required record type
+     * @param inputRecordType
+     * @param env
+     *            type environment
+     * @throws AlgebricksException
+     */
+    public static boolean rewriteListExpr(AbstractFunctionCallExpression funcExpr, IAType reqType, IAType inputType,
+            IVariableTypeEnvironment env) throws AlgebricksException {
+        if (funcExpr.getFunctionIdentifier() == AsterixBuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR) {
+            if (reqType.equals(BuiltinType.ANY)) {
+                reqType = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
+            }
+            return rewriteListFuncExpr(funcExpr, (AbstractCollectionType) reqType, (AbstractCollectionType) inputType,
+                    env);
+        } else if (funcExpr.getFunctionIdentifier() == AsterixBuiltinFunctions.ORDERED_LIST_CONSTRUCTOR) {
+            if (reqType.equals(BuiltinType.ANY)) {
+                reqType = DefaultOpenFieldType.NESTED_OPEN_AORDERED_LIST_TYPE;
+            }
+            return rewriteListFuncExpr(funcExpr, (AbstractCollectionType) reqType, (AbstractCollectionType) inputType,
+                    env);
+        } else {
+            List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
+            boolean changed = false;
+            for (Mutable<ILogicalExpression> arg : args) {
+                ILogicalExpression argExpr = arg.getValue();
+                if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                    AbstractFunctionCallExpression argFuncExpr = (AbstractFunctionCallExpression) argExpr;
+                    IAType exprType = (IAType) env.getType(argFuncExpr);
+                    changed = changed || rewriteListExpr(argFuncExpr, exprType, exprType, env);
+                }
+            }
+            return changed;
+        }
+    }
+
+    /**
+     * This method is to recursively enforce required types, for the list type and the record type.
+     * The List constructor is very special because
+     * 1. a nested list in a list is of type List<ANY>;
+     * 2. a nested record in a list is of type Open_Record{}.
+     * The open record constructor is very special because
+     * 1. a nested list in the open part is of type List<ANY>;
+     * 2. a nested record in the open part is of type Open_Record{}.
+     * However, the bottom-up type inference (InferTypeRule in algebricks) did not infer that so we need this method to enforce the type.
+     * We do not want to break the generality of algebricks so this method is called in an ASTERIX rule: @ IntroduceStaticTypeCastRule} .
+     * 
+     * @param funcExpr
+     *            the function expression whose type needs to be top-down enforced
+     * @param reqType
+     *            the required type inferred from parent operators/expressions
+     * @param inputType
+     *            the current inferred
+     * @param env
+     *            the type environment
+     * @return true if the type is casted; otherwise, false.
+     * @throws AlgebricksException
+     */
+    public static boolean rewriteFuncExpr(AbstractFunctionCallExpression funcExpr, IAType reqType, IAType inputType,
+            IVariableTypeEnvironment env) throws AlgebricksException {
+        if (funcExpr.getFunctionIdentifier() == AsterixBuiltinFunctions.UNORDERED_LIST_CONSTRUCTOR) {
+            if (reqType.equals(BuiltinType.ANY)) {
+                reqType = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
+            }
+            return rewriteListFuncExpr(funcExpr, (AbstractCollectionType) reqType, (AbstractCollectionType) inputType,
+                    env);
+        } else if (funcExpr.getFunctionIdentifier() == AsterixBuiltinFunctions.ORDERED_LIST_CONSTRUCTOR) {
+            if (reqType.equals(BuiltinType.ANY)) {
+                reqType = DefaultOpenFieldType.NESTED_OPEN_AORDERED_LIST_TYPE;
+            }
+            return rewriteListFuncExpr(funcExpr, (AbstractCollectionType) reqType, (AbstractCollectionType) inputType,
+                    env);
+        } else if (inputType.getTypeTag().equals(ATypeTag.RECORD)) {
+            if (reqType.equals(BuiltinType.ANY)) {
+                reqType = DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
+            }
+            return rewriteRecordFuncExpr(funcExpr, (ARecordType) reqType, (ARecordType) inputType, env);
+        } else {
+            List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
+            boolean changed = false;
+            for (Mutable<ILogicalExpression> arg : args) {
+                ILogicalExpression argExpr = arg.getValue();
+                if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                    AbstractFunctionCallExpression argFuncExpr = (AbstractFunctionCallExpression) argExpr;
+                    IAType exprType = (IAType) env.getType(argFuncExpr);
+                    changed = changed || rewriteFuncExpr(argFuncExpr, exprType, exprType, env);
+                }
+            }
+            return changed;
+        }
+    }
+
+    /**
+     * only called when funcExpr is record constructor
+     * 
+     * @param funcExpr
+     *            record constructor function expression
+     * @param requiredListType
+     *            required record type
+     * @param inputRecordType
+     * @param env
+     *            type environment
+     * @throws AlgebricksException
+     */
+    private static boolean rewriteRecordFuncExpr(AbstractFunctionCallExpression funcExpr,
+            ARecordType requiredRecordType, ARecordType inputRecordType, IVariableTypeEnvironment env)
+            throws AlgebricksException {
+        // if already rewritten, the required type is not null
+        if (TypeComputerUtilities.getRequiredType(funcExpr) != null)
+            return false;
+        TypeComputerUtilities.setRequiredAndInputTypes(funcExpr, requiredRecordType, inputRecordType);
+        staticRecordTypeCast(funcExpr, requiredRecordType, inputRecordType, env);
+        return true;
+    }
+
+    /**
+     * only called when funcExpr is list constructor
+     * 
+     * @param funcExpr
+     *            list constructor function expression
+     * @param requiredListType
+     *            required list type
+     * @param inputListType
+     * @param env
+     *            type environment
+     * @throws AlgebricksException
+     */
+    private static boolean rewriteListFuncExpr(AbstractFunctionCallExpression funcExpr,
+            AbstractCollectionType requiredListType, AbstractCollectionType inputListType, IVariableTypeEnvironment env)
+            throws AlgebricksException {
+        if (TypeComputerUtilities.getRequiredType(funcExpr) != null)
+            return false;
+
+        TypeComputerUtilities.setRequiredAndInputTypes(funcExpr, requiredListType, inputListType);
+        List<Mutable<ILogicalExpression>> args = funcExpr.getArguments();
+
+        IAType itemType = requiredListType.getItemType();
+        IAType inputItemType = inputListType.getItemType();
+        for (int j = 0; j < args.size(); j++) {
+            ILogicalExpression arg = args.get(j).getValue();
+            if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) arg;
+                IAType currentItemType = (IAType) env.getType(argFunc);
+                if (inputItemType == null || inputItemType == BuiltinType.ANY) {
+                    currentItemType = (IAType) env.getType(argFunc);
+                    rewriteFuncExpr(argFunc, itemType, currentItemType, env);
+                } else {
+                    rewriteFuncExpr(argFunc, itemType, inputItemType, env);
+                }
+            }
+        }
+        return true;
+    }
+
+    /**
+     * This method statically cast the type of records from their current type to the required type.
+     * 
+     * @param func
+     *            The record constructor expression.
+     * @param reqType
+     *            The required type.
+     * @param inputType
+     *            The current type.
+     * @param env
+     *            The type environment.
+     * @throws AlgebricksException
+     */
+    private static void staticRecordTypeCast(AbstractFunctionCallExpression func, ARecordType reqType,
+            ARecordType inputType, IVariableTypeEnvironment env) throws AlgebricksException {
+        IAType[] reqFieldTypes = reqType.getFieldTypes();
+        String[] reqFieldNames = reqType.getFieldNames();
+        IAType[] inputFieldTypes = inputType.getFieldTypes();
+        String[] inputFieldNames = inputType.getFieldNames();
+
+        int[] fieldPermutation = new int[reqFieldTypes.length];
+        boolean[] nullFields = new boolean[reqFieldTypes.length];
+        boolean[] openFields = new boolean[inputFieldTypes.length];
+
+        Arrays.fill(nullFields, false);
+        Arrays.fill(openFields, true);
+        Arrays.fill(fieldPermutation, -1);
+
+        // forward match: match from actual to required
+        boolean matched = false;
+        for (int i = 0; i < inputFieldNames.length; i++) {
+            String fieldName = inputFieldNames[i];
+            IAType fieldType = inputFieldTypes[i];
+
+            if (2 * i + 1 > func.getArguments().size())
+                throw new AlgebricksException("expression index out of bound");
+
+            // 2*i+1 is the index of field value expression
+            ILogicalExpression arg = func.getArguments().get(2 * i + 1).getValue();
+            matched = false;
+            for (int j = 0; j < reqFieldNames.length; j++) {
+                String reqFieldName = reqFieldNames[j];
+                IAType reqFieldType = reqFieldTypes[j];
+                if (fieldName.equals(reqFieldName)) {
+                    if (fieldType.equals(reqFieldType)) {
+                        fieldPermutation[j] = i;
+                        openFields[i] = false;
+                        matched = true;
+
+                        if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                            ScalarFunctionCallExpression scalarFunc = (ScalarFunctionCallExpression) arg;
+                            rewriteFuncExpr(scalarFunc, reqFieldType, fieldType, env);
+                        }
+                        break;
+                    }
+
+                    // match the optional field
+                    if (reqFieldType.getTypeTag() == ATypeTag.UNION
+                            && NonTaggedFormatUtil.isOptionalField((AUnionType) reqFieldType)) {
+                        IAType itemType = ((AUnionType) reqFieldType).getUnionList().get(
+                                NonTaggedFormatUtil.OPTIONAL_TYPE_INDEX_IN_UNION_LIST);
+                        reqFieldType = itemType;
+                        if (fieldType.equals(BuiltinType.ANULL) || fieldType.equals(itemType)) {
+                            fieldPermutation[j] = i;
+                            openFields[i] = false;
+                            matched = true;
+
+                            // rewrite record expr
+                            if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                                ScalarFunctionCallExpression scalarFunc = (ScalarFunctionCallExpression) arg;
+                                rewriteFuncExpr(scalarFunc, reqFieldType, fieldType, env);
+                            }
+                            break;
+                        }
+                    }
+
+                    // match the record field: need cast
+                    if (arg.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                        ScalarFunctionCallExpression scalarFunc = (ScalarFunctionCallExpression) arg;
+                        rewriteFuncExpr(scalarFunc, reqFieldType, fieldType, env);
+                        fieldPermutation[j] = i;
+                        openFields[i] = false;
+                        matched = true;
+                        break;
+                    }
+                }
+            }
+            // the input has extra fields
+            if (!matched && !reqType.isOpen())
+                throw new AlgebricksException("static type mismatch: including an extra closed field " + fieldName);
+        }
+
+        // backward match: match from required to actual
+        for (int i = 0; i < reqFieldNames.length; i++) {
+            String reqFieldName = reqFieldNames[i];
+            IAType reqFieldType = reqFieldTypes[i];
+            matched = false;
+            for (int j = 0; j < inputFieldNames.length; j++) {
+                String fieldName = inputFieldNames[j];
+                IAType fieldType = inputFieldTypes[j];
+                if (!fieldName.equals(reqFieldName))
+                    continue;
+                // should check open field here
+                // because number of entries in fieldPermuations is the
+                // number of required schema fields
+                // here we want to check if an input field is matched
+                // the entry index of fieldPermuatons is req field index
+                if (!openFields[j]) {
+                    matched = true;
+                    break;
+                }
+
+                // match the optional field
+                if (reqFieldType.getTypeTag() == ATypeTag.UNION
+                        && NonTaggedFormatUtil.isOptionalField((AUnionType) reqFieldType)) {
+                    IAType itemType = ((AUnionType) reqFieldType).getUnionList().get(
+                            NonTaggedFormatUtil.OPTIONAL_TYPE_INDEX_IN_UNION_LIST);
+                    if (fieldType.equals(BuiltinType.ANULL) || fieldType.equals(itemType)) {
+                        matched = true;
+                        break;
+                    }
+                }
+            }
+            if (matched)
+                continue;
+
+            if (reqFieldType.getTypeTag() == ATypeTag.UNION
+                    && NonTaggedFormatUtil.isOptionalField((AUnionType) reqFieldType)) {
+                // add a null field
+                nullFields[i] = true;
+            } else {
+                // no matched field in the input for a required closed field
+                throw new AlgebricksException("static type mismatch: miss a required closed field " + reqFieldName);
+            }
+        }
+
+        List<Mutable<ILogicalExpression>> arguments = func.getArguments();
+        List<Mutable<ILogicalExpression>> originalArguments = new ArrayList<Mutable<ILogicalExpression>>();
+        originalArguments.addAll(arguments);
+        arguments.clear();
+        // re-order the closed part and fill in null fields
+        for (int i = 0; i < fieldPermutation.length; i++) {
+            int pos = fieldPermutation[i];
+            if (pos >= 0) {
+                arguments.add(originalArguments.get(2 * pos));
+                arguments.add(originalArguments.get(2 * pos + 1));
+            }
+            if (nullFields[i]) {
+                // add a null field
+                arguments.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(
+                        new AString(reqFieldNames[i])))));
+                arguments.add(new MutableObject<ILogicalExpression>(new ConstantExpression(new AsterixConstantValue(
+                        ANull.NULL))));
+            }
+        }
+
+        // add the open part
+        for (int i = 0; i < openFields.length; i++) {
+            if (openFields[i]) {
+                arguments.add(originalArguments.get(2 * i));
+                Mutable<ILogicalExpression> fExprRef = originalArguments.get(2 * i + 1);
+                ILogicalExpression argExpr = fExprRef.getValue();
+
+                // we need to handle open fields recursively by their default
+                // types
+                // for list, their item type is any
+                // for record, their
+                if (argExpr.getExpressionTag() == LogicalExpressionTag.FUNCTION_CALL) {
+                    IAType reqFieldType = inputFieldTypes[i];
+                    if (inputFieldTypes[i].getTypeTag() == ATypeTag.RECORD) {
+                        reqFieldType = DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
+                    }
+                    if (inputFieldTypes[i].getTypeTag() == ATypeTag.ORDEREDLIST) {
+                        reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AORDERED_LIST_TYPE;
+                    }
+                    if (inputFieldTypes[i].getTypeTag() == ATypeTag.UNORDEREDLIST) {
+                        reqFieldType = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
+                    }
+                    if (TypeComputerUtilities.getRequiredType((AbstractFunctionCallExpression) argExpr) == null) {
+                        ScalarFunctionCallExpression argFunc = (ScalarFunctionCallExpression) argExpr;
+                        rewriteFuncExpr(argFunc, reqFieldType, inputFieldTypes[i], env);
+                    }
+                }
+                arguments.add(fExprRef);
+            }
+        }
+    }
+
+}
diff --git a/asterix-app/data/twitter/tw_messages.adm b/asterix-app/data/twitter/tw_messages.adm
new file mode 100644
index 0000000..88f5e24
--- /dev/null
+++ b/asterix-app/data/twitter/tw_messages.adm
@@ -0,0 +1,10 @@
+{"tweetid":"1","tweetid-copy":"1","user":{"screen-name":"RollandEckhardstein#211","lang":"en","friends_count":3657079,"statuses_count":268,"name":"Rolland Eckhardstein","followers_count":3311368},"sender-location":point("42.13,80.43"),"send-time":datetime("2005-12-05T21:06:41"),"send-time-copy":datetime("2005-12-05T21:06:41"),"referred-topics":{{"samsung","plan"}},"message-text":" love samsung the plan is amazing"}
+{"tweetid":"2","tweetid-copy":"2","user":{"screen-name":"RollandEckhardstein#211","lang":"en","friends_count":3657079,"statuses_count":268,"name":"David Eckhardstein","followers_count":3311368},"sender-location":point("28.86,70.44"),"send-time":datetime("2007-08-15T06:44:17"),"send-time-copy":datetime("2007-08-15T06:44:17"),"referred-topics":{{"sprint","voice-clarity"}},"message-text":" like sprint its voice-clarity is mind-blowing"}
+{"tweetid":"3","tweetid-copy":"3","user":{"screen-name":"RollandEckhard#500","lang":"en","friends_count":3657079,"statuses_count":268,"name":"Rolland Hetfield","followers_count":3311368},"sender-location":point("39.84,86.48"),"send-time":datetime("2008-12-24T00:07:04"),"send-time-copy":datetime("2008-12-24T00:07:04"),"referred-topics":{{"verizon","voice-command"}},"message-text":" can't stand verizon its voice-command is terrible:("}
+{"tweetid":"4","tweetid-copy":"4","user":{"screen-name":"RollandEckhardstein#221","lang":"en","friends_count":3657079,"statuses_count":268,"name":"Rolland Eckhardstinz","followers_count":3311368},"sender-location":point("27.67,87.32"),"send-time":datetime("2007-02-05T16:39:13"),"send-time-copy":datetime("2007-02-05T16:39:13"),"referred-topics":{{"t-mobile","customer-service"}},"message-text":" love t-mobile its customer-service is mind-blowing"}
+{"tweetid":"5","tweetid-copy":"5","user":{"screen-name":"RollandEcstein#211","lang":"en","friends_count":3657079,"statuses_count":268,"name":"Rolland Eckhardst","followers_count":3311368},"sender-location":point("27.3,92.77"),"send-time":datetime("2010-09-12T06:15:28"),"send-time-copy":datetime("2010-09-12T06:15:28"),"referred-topics":{{"t-mobile","customization"}},"message-text":" like t-mobile the customization is amazing:)"}
+{"tweetid":"6","tweetid-copy":"6","user":{"screen-name":"Rollkhardstein#211","lang":"en","friends_count":3657079,"statuses_count":268,"name":"Kirk Hammette ","followers_count":3311368},"sender-location":point("45.62,84.78"),"send-time":datetime("2012-01-23T06:23:13"),"send-time-copy":datetime("2012-01-23T06:23:13"),"referred-topics":{{"iphone","network"}},"message-text":" like iphone its network is awesome:)"}
+{"tweetid":"7","tweetid-copy":"7","user":{"screen-name":"andEckhardstein#211","lang":"en","friends_count":3657079,"statuses_count":268,"name":"Rolland khardstein","followers_count":3311368},"sender-location":point("44.12,81.46"),"send-time":datetime("2012-02-17T17:30:26"),"send-time-copy":datetime("2012-02-17T17:30:26"),"referred-topics":{{"t-mobile","network"}},"message-text":" hate t-mobile the network is bad"}
+{"tweetid":"8","tweetid-copy":"8","user":{"screen-name":"Rolltein#211","lang":"en","friends_count":3657079,"statuses_count":268,"name":"Ron Eckhardstein","followers_count":3311368},"sender-location":point("36.86,90.71"),"send-time":datetime("2009-03-12T13:18:04"),"send-time-copy":datetime("2009-03-12T13:18:04"),"referred-topics":{{"at&t","touch-screen"}},"message-text":" dislike at&t its touch-screen is OMG"}
+{"tweetid":"9","tweetid-copy":"9","user":{"screen-name":"Roldstein#211","lang":"en","friends_count":3657079,"statuses_count":268,"name":"Rolland Eckdstein","followers_count":3311368},"sender-location":point("29.07,97.05"),"send-time":datetime("2012-08-15T20:19:46"),"send-time-copy":datetime("2012-08-15T20:19:46"),"referred-topics":{{"verizon","speed"}},"message-text":" hate verizon its speed is bad"}
+{"tweetid":"10","tweetid-copy":"10","user":{"screen-name":"Rolldstein#211","lang":"en","friends_count":3657079,"statuses_count":268,"name":"Rolland Eckhardstful","followers_count":3311368},"sender-location":point("46.94,93.98"),"send-time":datetime("2011-04-07T14:08:46"),"send-time-copy":datetime("2011-04-07T14:08:46"),"referred-topics":{{"t-mobile","signal"}},"message-text":" like t-mobile the signal is good"}
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/dml/query-issue205.aql b/asterix-app/src/test/resources/runtimets/queries/dml/query-issue205.aql
new file mode 100644
index 0000000..e0a0695
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/dml/query-issue205.aql
@@ -0,0 +1,33 @@
+/*
+ * Description  : This test case is to verify the fix for issue205
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=205
+ * Expected Res : Success
+ * Date         : 26th November 2012
+ */
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type EmployeeStat as open {
+  age: int32,
+  salary:int32
+}
+
+create type EmployeeType as closed {
+  id:string,
+  stat:EmployeeStat,
+  deptCode:int32
+}
+
+create dataset Employees(EmployeeType)
+  partitioned by key id;
+
+insert into dataset Employees({"id":"1234", "stat":{ "age":50, "salary":120000}, "deptCode":32 });
+insert into dataset Employees({"id":"5678", "stat":{ "age":40, "salary":100000}, "deptCode":16 });
+
+delete $l from dataset Employees where $l.id = "1234";
+
+write output to nc1:"rttest/dml_query-issue205.adm";
+for $l in dataset('Employees')
+return $l
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue134.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue134.aql
new file mode 100644
index 0000000..62a9ed8
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue134.aql
@@ -0,0 +1,11 @@
+/*
+ * Description  : This test case is to verify the fix for issue134
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=134
+ * Expected Res : Success
+ * Date         : 26th November 2012
+ */
+
+write output to nc1:"rttest/open-closed_query-issue134.adm";
+
+let $a:=true
+return {{[1,2,3,4,5],[6,5,3,8,9],[44,22,66,-1,0,99.9]}}
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue166.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue166.aql
new file mode 100644
index 0000000..aa0d13b
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue166.aql
@@ -0,0 +1,11 @@
+/*
+ * Description  : This test case is to verify the fix for issue166
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=166
+ * Expected Res : Success
+ * Date         : 26th November 2012
+ */
+ 
+write output to nc1:"rttest/open-closed_query-issue166.adm";
+
+let $a := [[1,2,3],[4,5,6,7]]
+return $a[1]
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue208.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue208.aql
new file mode 100644
index 0000000..e46286c
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue208.aql
@@ -0,0 +1,45 @@
+/*
+ * Description  : This test case is to verify the fix for issue208
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=208
+ * Expected Res : Success
+ * Date         : 26th November 2012
+ */
+
+drop dataverse OpenSocialNetworkData if exists;
+create dataverse OpenSocialNetworkData;
+
+use dataverse OpenSocialNetworkData;
+
+create type TwitterUserType as open {
+screen-name: string,
+lang: string,
+friends_count: int32,
+statuses_count: int32,
+name: string,
+followers_count: int32
+}
+
+create type TweetMessageType as open {
+tweetid: string,
+tweetid-copy: string,
+send-time-copy: datetime
+}
+
+create dataset TweetMessages(TweetMessageType)
+partitioned by key tweetid;
+
+load dataset TweetMessages
+using "edu.uci.ics.asterix.external.dataset.adapter.NCFileSystemAdapter"
+(("path"="nc1://data/twitter/tw_messages.adm"),("format"="adm"));
+
+write output to nc1:"rttest/open-closed_query-issue208.adm"; 
+for $t in dataset('TweetMessages')
+where $t.send-time >= datetime('2005-04-13T17:17:22') and
+$t.send-time <= datetime('2011-04-13T17:18:22')
+group by $uid := $t.user.screen-name with $t
+order by $uid
+return {
+    "user": $uid,
+    "count": count($t)
+}
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue29.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue29.aql
new file mode 100644
index 0000000..8b778ba
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue29.aql
@@ -0,0 +1,70 @@
+/*
+ * Description  : This test case is to verify the fix for issue29
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=29
+ * Expected Res : Success
+ * Date         : 26th November 2012
+ */
+
+write output to nc1:"rttest/open-closed_query-issue29.adm";
+
+let $tweets := 
+{{
+   {
+      "tweetid": "1023",
+      "user": {
+         "screen-name": "dflynn24",
+         "lang": "en",
+         "friends_count": 46,
+         "statuses_count": 987,
+         "name": "danielle flynn",
+         "followers_count": 47
+      },
+      "sender-location": "40.904177,-72.958996",
+      "send-time": "2010-02-21T11:56:02-05:00",
+      "referred-topics": {{ "verizon" }},
+      "message-text": "i need a #verizon phone like nowwwww! :("
+   },
+   {
+      "tweetid": "1024",
+      "user": {
+         "screen-name": "miriamorous",
+         "lang": "en",
+         "friends_count": 69,
+         "statuses_count": 1068,
+         "name": "Miriam Songco",
+         "followers_count": 78
+      },
+      "send-time": "2010-02-21T11:11:43-08:00",
+      "referred-topics": {{ "commercials", "verizon", "att" }},
+      "message-text": "#verizon & #att #commercials, so competitive"
+   },
+   {
+      "tweetid": "1025",
+      "user": {
+         "screen-name": "dj33",
+         "lang": "en",
+         "friends_count": 96,
+         "statuses_count": 1696,
+         "name": "Don Jango",
+         "followers_count": 22
+      },
+      "send-time": "2010-02-21T12:38:44-05:00",
+      "referred-topics": {{ "charlotte" }},
+      "message-text": "Chillin at dca waiting for 900am flight to #charlotte and from there to providenciales"
+   },
+   {
+      "tweetid": "1026",
+      "user": {
+      "screen-name": "reallyleila",
+         "lang": "en",
+         "friends_count": 106,
+         "statuses_count": 107,
+         "name": "Leila Samii",
+         "followers_count": 52
+      },
+      "send-time": "2010-02-21T21:31:57-06:00",
+      "referred-topics": {{ "verizon", "at&t", "iphone" }},
+      "message-text": "I think a switch from #verizon to #at&t may be in my near future... my smartphone is like a land line compared to the #iphone!"
+   }
+}}
+return $tweets
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue55-1.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue55-1.aql
new file mode 100644
index 0000000..11b75d1
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue55-1.aql
@@ -0,0 +1,13 @@
+/*
+ * Description  : This test case is to verify the fix for issue55 query 1
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=55
+ * Expected Res : Success
+ * Date         : 26th November 2012
+ */
+
+write output to nc1:"rttest/open-closed_query-issue55-1.adm";
+
+let $l := [1.1f, 1.0f, 1.2f, 0.9, 1.3, 1, 2]
+for $i in $l
+for $j in $l
+return [$i, $j, "=", $i = $j, "<", $i < $j, "<=", $i <= $j, ">", $i > $j, ">=", $i >= $j]
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue55.aql b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue55.aql
new file mode 100644
index 0000000..b4e4572
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/open-closed/query-issue55.aql
@@ -0,0 +1,11 @@
+/*
+ * Description  : This test case is to verify the fix for issue55 query 2
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=55
+ * Expected Res : Success
+ * Date         : 26th November 2012
+ */
+
+write output to nc1:"rttest/open-closed_query-issue55.adm";
+
+for $x in [[1,3],[4,5,2],[-1,-3,0],["a"]]
+return $x
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue201.aql b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue201.aql
new file mode 100644
index 0000000..0505179
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/user-defined-functions/query-issue201.aql
@@ -0,0 +1,12 @@
+/*
+ * Description  : This test case is to verify the fix for issue201
+ 				: https://code.google.com/p/asterixdb/issues/detail?id=201
+ * Expected Res : Success
+ * Date         : 26th November 2012
+ */
+ 
+write output to nc1:"rttest/user-defined-functions_query-issue201.adm";
+
+let $x:=range(1,100)
+for $i in $x
+return $i
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/dml/query-issue205.adm b/asterix-app/src/test/resources/runtimets/results/dml/query-issue205.adm
new file mode 100644
index 0000000..ea80112
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/dml/query-issue205.adm
@@ -0,0 +1 @@
+{ "id": "5678", "stat": { "age": 40, "salary": 100000 }, "deptCode": 16 }
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue134.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue134.adm
new file mode 100644
index 0000000..0d06066
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue134.adm
@@ -0,0 +1 @@
+{{ [ 1, 2, 3, 4, 5 ], [ 6, 5, 3, 8, 9 ], [ 44, 22, 66, -1, 0, 99.9d ] }}
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue166.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue166.adm
new file mode 100644
index 0000000..b5897af
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue166.adm
@@ -0,0 +1 @@
+[ 4, 5, 6, 7 ]
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue208.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue208.adm
new file mode 100644
index 0000000..14c1c18
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue208.adm
@@ -0,0 +1,6 @@
+{ "count": 1, "user": "RollandEckhard#500" }
+{ "count": 2, "user": "RollandEckhardstein#211" }
+{ "count": 1, "user": "RollandEckhardstein#221" }
+{ "count": 1, "user": "RollandEcstein#211" }
+{ "count": 1, "user": "Rolldstein#211" }
+{ "count": 1, "user": "Rolltein#211" }
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue29.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue29.adm
new file mode 100644
index 0000000..3d2819e
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue29.adm
@@ -0,0 +1 @@
+{{ { "tweetid": "1023", "user": { "screen-name": "dflynn24", "lang": "en", "friends_count": 46, "statuses_count": 987, "name": "danielle flynn", "followers_count": 47 }, "sender-location": "40.904177,-72.958996", "send-time": "2010-02-21T11:56:02-05:00", "referred-topics": {{ "verizon" }}, "message-text": "i need a #verizon phone like nowwwww! :(" }, { "tweetid": "1024", "user": { "screen-name": "miriamorous", "lang": "en", "friends_count": 69, "statuses_count": 1068, "name": "Miriam Songco", "followers_count": 78 }, "send-time": "2010-02-21T11:11:43-08:00", "referred-topics": {{ "commercials", "verizon", "att" }}, "message-text": "#verizon & #att #commercials, so competitive" }, { "tweetid": "1025", "user": { "screen-name": "dj33", "lang": "en", "friends_count": 96, "statuses_count": 1696, "name": "Don Jango", "followers_count": 22 }, "send-time": "2010-02-21T12:38:44-05:00", "referred-topics": {{ "charlotte" }}, "message-text": "Chillin at dca waiting for 900am flight to #charlotte and from there to providenciales" }, { "tweetid": "1026", "user": { "screen-name": "reallyleila", "lang": "en", "friends_count": 106, "statuses_count": 107, "name": "Leila Samii", "followers_count": 52 }, "send-time": "2010-02-21T21:31:57-06:00", "referred-topics": {{ "verizon", "at&t", "iphone" }}, "message-text": "I think a switch from #verizon to #at&t may be in my near future... my smartphone is like a land line compared to the #iphone!" } }}
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue55-1.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue55-1.adm
new file mode 100644
index 0000000..adf0f33
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue55-1.adm
@@ -0,0 +1,49 @@
+[ 1.1f, 1.1f, "=", true, "<", false, "<=", true, ">", false, ">=", true ]
+[ 1.1f, 1.0f, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.1f, 1.2f, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1.1f, 0.9d, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.1f, 1.3d, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1.1f, 1, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.1f, 2, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1.0f, 1.1f, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1.0f, 1.0f, "=", true, "<", false, "<=", true, ">", false, ">=", true ]
+[ 1.0f, 1.2f, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1.0f, 0.9d, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.0f, 1.3d, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1.0f, 1, "=", true, "<", false, "<=", true, ">", false, ">=", true ]
+[ 1.0f, 2, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1.2f, 1.1f, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.2f, 1.0f, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.2f, 1.2f, "=", true, "<", false, "<=", true, ">", false, ">=", true ]
+[ 1.2f, 0.9d, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.2f, 1.3d, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1.2f, 1, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.2f, 2, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 0.9d, 1.1f, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 0.9d, 1.0f, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 0.9d, 1.2f, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 0.9d, 0.9d, "=", true, "<", false, "<=", true, ">", false, ">=", true ]
+[ 0.9d, 1.3d, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 0.9d, 1, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 0.9d, 2, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1.3d, 1.1f, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.3d, 1.0f, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.3d, 1.2f, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.3d, 0.9d, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.3d, 1.3d, "=", true, "<", false, "<=", true, ">", false, ">=", true ]
+[ 1.3d, 1, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1.3d, 2, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1, 1.1f, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1, 1.0f, "=", true, "<", false, "<=", true, ">", false, ">=", true ]
+[ 1, 1.2f, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1, 0.9d, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 1, 1.3d, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 1, 1, "=", true, "<", false, "<=", true, ">", false, ">=", true ]
+[ 1, 2, "=", false, "<", true, "<=", true, ">", false, ">=", false ]
+[ 2, 1.1f, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 2, 1.0f, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 2, 1.2f, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 2, 0.9d, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 2, 1.3d, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 2, 1, "=", false, "<", false, "<=", false, ">", true, ">=", true ]
+[ 2, 2, "=", true, "<", false, "<=", true, ">", false, ">=", true ]
diff --git a/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue55.adm b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue55.adm
new file mode 100644
index 0000000..d384bce
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/open-closed/query-issue55.adm
@@ -0,0 +1,4 @@
+[ 1, 3 ]
+[ 4, 5, 2 ]
+[ -1, -3, 0 ]
+[ "a" ]
diff --git a/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-issue201.adm b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-issue201.adm
new file mode 100644
index 0000000..190423f
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/user-defined-functions/query-issue201.adm
@@ -0,0 +1,100 @@
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+21
+22
+23
+24
+25
+26
+27
+28
+29
+30
+31
+32
+33
+34
+35
+36
+37
+38
+39
+40
+41
+42
+43
+44
+45
+46
+47
+48
+49
+50
+51
+52
+53
+54
+55
+56
+57
+58
+59
+60
+61
+62
+63
+64
+65
+66
+67
+68
+69
+70
+71
+72
+73
+74
+75
+76
+77
+78
+79
+80
+81
+82
+83
+84
+85
+86
+87
+88
+89
+90
+91
+92
+93
+94
+95
+96
+97
+98
+99
+100
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index 2d740be..1e2f235 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -739,6 +739,11 @@
   </test-group>
   <test-group name="dml">
     <test-case FilePath="dml">
+      <compilation-unit name="query-issue205">
+        <output-file compare="Text">query-issue205.adm</output-file>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="dml">
       <compilation-unit name="delete-from-loaded-dataset-with-index">
         <output-file compare="Text">delete-from-loaded-dataset-with-index.adm</output-file>
       </compilation-unit>
@@ -2369,6 +2374,31 @@
         <output-file compare="Text">open-closed-14.adm</output-file>
       </compilation-unit>
     </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue134">
+        <output-file compare="Text">query-issue134.adm</output-file>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue55">
+        <output-file compare="Text">query-issue55.adm</output-file>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue55-1">
+        <output-file compare="Text">query-issue55-1.adm</output-file>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue166">
+        <output-file compare="Text">query-issue166.adm</output-file>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="open-closed">
+      <compilation-unit name="query-issue208">
+        <output-file compare="Text">query-issue208.adm</output-file>
+      </compilation-unit>
+    </test-case>
     <!--
     <test-case FilePath="open-closed">
       <compilation-unit name="open-closed-15">
@@ -3632,6 +3662,11 @@
   </test-group>
   <test-group name="user-defined-functions">
     <test-case FilePath="user-defined-functions">
+      <compilation-unit name="query-issue201">
+        <output-file compare="Text">query-issue201.adm</output-file>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="user-defined-functions">
       <compilation-unit name="udf01">
         <output-file compare="Text">udf01.adm</output-file>
       </compilation-unit>
diff --git a/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/dataset/adapter/HDFSAdapter.java b/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/dataset/adapter/HDFSAdapter.java
index f7a79cd..744683c 100644
--- a/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/dataset/adapter/HDFSAdapter.java
+++ b/asterix-external-data/src/main/java/edu/uci/ics/asterix/external/dataset/adapter/HDFSAdapter.java
@@ -40,7 +40,7 @@
 import org.apache.hadoop.mapred.TextInputFormat;
 
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.runtime.util.AsterixRuntimeUtil;
+import edu.uci.ics.asterix.om.util.AsterixRuntimeUtil;
 import edu.uci.ics.hyracks.algebricks.common.constraints.AlgebricksAbsolutePartitionConstraint;
 import edu.uci.ics.hyracks.algebricks.common.constraints.AlgebricksCountPartitionConstraint;
 import edu.uci.ics.hyracks.api.context.IHyracksTaskContext;
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ABooleanPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ABooleanPrinter.java
index 60ee1fe..33866df 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ABooleanPrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ABooleanPrinter.java
@@ -8,7 +8,6 @@
 
 public class ABooleanPrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final ABooleanPrinter INSTANCE = new ABooleanPrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ACirclePrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ACirclePrinter.java
index 812d93b..e2da96e 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ACirclePrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ACirclePrinter.java
@@ -8,7 +8,6 @@
 
 public class ACirclePrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final ACirclePrinter INSTANCE = new ACirclePrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADatePrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADatePrinter.java
index 24aa144..918e286 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADatePrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADatePrinter.java
@@ -9,11 +9,10 @@
 
 public class ADatePrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     private static long CHRONON_OF_DAY = 24 * 60 * 60 * 1000;
     public static final ADatePrinter INSTANCE = new ADatePrinter();
     private static final GregorianCalendarSystem gCalInstance = GregorianCalendarSystem.getInstance();
-    
+
     @Override
     public void init() {
 
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADateTimePrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADateTimePrinter.java
index d856a20..96df767 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADateTimePrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADateTimePrinter.java
@@ -9,7 +9,6 @@
 
 public class ADateTimePrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final ADateTimePrinter INSTANCE = new ADateTimePrinter();
     private static final GregorianCalendarSystem gCalInstance = GregorianCalendarSystem.getInstance();
 
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADoublePrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADoublePrinter.java
index 3a255c6..ae50d0e 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADoublePrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADoublePrinter.java
@@ -8,7 +8,6 @@
 
 public class ADoublePrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final ADoublePrinter INSTANCE = new ADoublePrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADurationPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADurationPrinter.java
index a59b9ac..061d372 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADurationPrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ADurationPrinter.java
@@ -12,7 +12,6 @@
 
 public class ADurationPrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final ADurationPrinter INSTANCE = new ADurationPrinter();
     private static final GregorianCalendarSystem gCalInstance = GregorianCalendarSystem.getInstance();
 
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AFloatPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AFloatPrinter.java
index 27d2e2c..96c9ee9 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AFloatPrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AFloatPrinter.java
@@ -8,7 +8,6 @@
 
 public class AFloatPrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final AFloatPrinter INSTANCE = new AFloatPrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt16Printer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt16Printer.java
index 8961013..1687fb1 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt16Printer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt16Printer.java
@@ -13,8 +13,6 @@
 
 public class AInt16Printer implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
-
     private static final String SUFFIX_STRING = "i16";
     private static byte[] _suffix;
     private static int _suffix_count;
@@ -23,6 +21,7 @@
         DataOutput dout = new DataOutputStream(interm);
         try {
             dout.writeUTF(SUFFIX_STRING);
+            interm.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt32Printer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt32Printer.java
index a5c3245..b20db34 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt32Printer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt32Printer.java
@@ -10,7 +10,6 @@
 
 public class AInt32Printer implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final AInt32Printer INSTANCE = new AInt32Printer();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt64Printer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt64Printer.java
index 318828e..312802a 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt64Printer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt64Printer.java
@@ -13,8 +13,6 @@
 
 public class AInt64Printer implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
-
     private static final String SUFFIX_STRING = "i64";
     private static byte[] _suffix;
     private static int _suffix_count;
@@ -23,6 +21,7 @@
         DataOutput dout = new DataOutputStream(interm);
         try {
             dout.writeUTF(SUFFIX_STRING);
+            interm.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt8Printer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt8Printer.java
index bd4139d..a5a421d 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt8Printer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AInt8Printer.java
@@ -13,8 +13,6 @@
 
 public class AInt8Printer implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
-
     private static final String SUFFIX_STRING = "i8";
     private static byte[] _suffix;
     private static int _suffix_count;
@@ -23,6 +21,7 @@
         DataOutput dout = new DataOutputStream(interm);
         try {
             dout.writeUTF(SUFFIX_STRING);
+            interm.close();
         } catch (IOException e) {
             e.printStackTrace();
         }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ALinePrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ALinePrinter.java
index f99727a..40f405c 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ALinePrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ALinePrinter.java
@@ -8,7 +8,6 @@
 
 public class ALinePrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final ALinePrinter INSTANCE = new ALinePrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ANullPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ANullPrinter.java
index 6abc0e1..daea967 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ANullPrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ANullPrinter.java
@@ -5,11 +5,8 @@
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
 import edu.uci.ics.hyracks.algebricks.data.IPrinter;
 
-
-
 public class ANullPrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final ANullPrinter INSTANCE = new ANullPrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AObjectPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AObjectPrinter.java
index aff30f7..edcfc8a 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AObjectPrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AObjectPrinter.java
@@ -10,7 +10,6 @@
 
 public class AObjectPrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final AObjectPrinter INSTANCE = new AObjectPrinter();
 
     private IPrinter recordPrinter = new ARecordPrinterFactory(null).createPrinter();
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AOrderedlistPrinterFactory.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AOrderedlistPrinterFactory.java
index 563b14c..7d04ebc 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AOrderedlistPrinterFactory.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AOrderedlistPrinterFactory.java
@@ -2,22 +2,21 @@
 
 import java.io.PrintStream;
 
-import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.dataflow.data.nontagged.serde.AInt32SerializerDeserializer;
-import edu.uci.ics.asterix.formats.nontagged.AqlPrinterFactoryProvider;
+import edu.uci.ics.asterix.om.pointables.PointableAllocator;
+import edu.uci.ics.asterix.om.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.pointables.printer.APrintVisitor;
 import edu.uci.ics.asterix.om.types.AOrderedListType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
-import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
 import edu.uci.ics.hyracks.algebricks.data.IPrinter;
 import edu.uci.ics.hyracks.algebricks.data.IPrinterFactory;
 
 public class AOrderedlistPrinterFactory implements IPrinterFactory {
 
     private static final long serialVersionUID = 1L;
-
     private AOrderedListType orderedlistType;
 
     public AOrderedlistPrinterFactory(AOrderedListType orderedlistType) {
@@ -27,77 +26,29 @@
     @Override
     public IPrinter createPrinter() {
 
-        return new IPrinter() {
+        PointableAllocator allocator = new PointableAllocator();
+        final IAType inputType = orderedlistType == null ? DefaultOpenFieldType
+                .getDefaultOpenFieldType(ATypeTag.ORDEREDLIST) : orderedlistType;
+        final IVisitablePointable listAccessor = allocator.allocateListValue(inputType);
+        final APrintVisitor printVisitor = new APrintVisitor();
+        final Pair<PrintStream, ATypeTag> arg = new Pair<PrintStream, ATypeTag>(null, null);
 
-            private IPrinter itemPrinter;
-            private IAType itemType;
-            private ATypeTag itemTag;
-            private boolean typedItemList = false;
+        return new IPrinter() {
 
             @Override
             public void init() throws AlgebricksException {
-
-                if (orderedlistType != null && orderedlistType.getItemType() != null) {
-                    itemType = orderedlistType.getItemType();
-                    if (itemType.getTypeTag() == ATypeTag.ANY) {
-                        this.typedItemList = false;
-                        this.itemPrinter = AObjectPrinterFactory.INSTANCE.createPrinter();
-                    } else {
-                        this.typedItemList = true;
-                        itemPrinter = AqlPrinterFactoryProvider.INSTANCE.getPrinterFactory(itemType).createPrinter();
-                        itemTag = orderedlistType.getItemType().getTypeTag();
-                    }
-                } else {
-                    this.typedItemList = false;
-                    this.itemPrinter = AObjectPrinterFactory.INSTANCE.createPrinter();
-                }
-                itemPrinter.init();
-
+                arg.second = inputType.getTypeTag();
             }
 
             @Override
-            public void print(byte[] b, int s, int l, PrintStream ps) throws AlgebricksException {
-                ps.print("[ ");
-                int numberOfitems = AInt32SerializerDeserializer.getInt(b, s + 6);
-                int itemOffset;
-                if (typedItemList) {
-                    switch (itemTag) {
-                        case STRING:
-                        case RECORD:
-                        case ORDEREDLIST:
-                        case UNORDEREDLIST:
-                        case ANY:
-                            itemOffset = s + 10 + (numberOfitems * 4);
-                            break;
-                        default:
-                            itemOffset = s + 10;
-                    }
-                } else
-                    itemOffset = s + 10 + (numberOfitems * 4);
-                int itemLength = 0;
+            public void print(byte[] b, int start, int l, PrintStream ps) throws AlgebricksException {
                 try {
-                    if (typedItemList) {
-                        for (int i = 0; i < numberOfitems; i++) {
-                            itemLength = NonTaggedFormatUtil.getFieldValueLength(b, itemOffset, itemTag, false);
-                            itemPrinter.print(b, itemOffset - 1, itemLength, ps);
-                            itemOffset += itemLength;
-                            if (i + 1 < numberOfitems)
-                                ps.print(", ");
-                        }
-                    } else {
-                        for (int i = 0; i < numberOfitems; i++) {
-                            itemTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(b[itemOffset]);
-                            itemLength = NonTaggedFormatUtil.getFieldValueLength(b, itemOffset, itemTag, true) + 1;
-                            itemPrinter.print(b, itemOffset, itemLength, ps);
-                            itemOffset += itemLength;
-                            if (i + 1 < numberOfitems)
-                                ps.print(", ");
-                        }
-                    }
-                } catch (AsterixException e) {
-                    throw new AlgebricksException(e);
+                    listAccessor.set(b, start, l);
+                    arg.first = ps;
+                    listAccessor.accept(printVisitor, arg);
+                } catch (Exception ioe) {
+                    throw new AlgebricksException(ioe);
                 }
-                ps.print(" ]");
             }
         };
     }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APoint3DPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APoint3DPrinter.java
index 699cb77..7394dc5 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APoint3DPrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APoint3DPrinter.java
@@ -8,7 +8,6 @@
 
 public class APoint3DPrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final APoint3DPrinter INSTANCE = new APoint3DPrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APointPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APointPrinter.java
index 50fbb0b..7c50905 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APointPrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APointPrinter.java
@@ -8,7 +8,6 @@
 
 public class APointPrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final APointPrinter INSTANCE = new APointPrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APolygonPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APolygonPrinter.java
index 794dae7..bc1e553 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APolygonPrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/APolygonPrinter.java
@@ -9,7 +9,6 @@
 
 public class APolygonPrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final APolygonPrinter INSTANCE = new APolygonPrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARecordPrinterFactory.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARecordPrinterFactory.java
index fafe320..e4b2676 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARecordPrinterFactory.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARecordPrinterFactory.java
@@ -2,23 +2,21 @@
 
 import java.io.PrintStream;
 
-import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.dataflow.data.nontagged.serde.AInt32SerializerDeserializer;
-import edu.uci.ics.asterix.formats.nontagged.AqlPrinterFactoryProvider;
+import edu.uci.ics.asterix.om.pointables.PointableAllocator;
+import edu.uci.ics.asterix.om.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.pointables.printer.APrintVisitor;
 import edu.uci.ics.asterix.om.types.ARecordType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
-import edu.uci.ics.asterix.om.types.AUnionType;
-import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
 import edu.uci.ics.hyracks.algebricks.data.IPrinter;
 import edu.uci.ics.hyracks.algebricks.data.IPrinterFactory;
 
 public class ARecordPrinterFactory implements IPrinterFactory {
 
     private static final long serialVersionUID = 1L;
-
     private final ARecordType recType;
 
     public ARecordPrinterFactory(ARecordType recType) {
@@ -28,137 +26,29 @@
     @Override
     public IPrinter createPrinter() {
 
-        return new IPrinter() {
+        PointableAllocator allocator = new PointableAllocator();
+        final IAType inputType = recType == null ? DefaultOpenFieldType.getDefaultOpenFieldType(ATypeTag.RECORD)
+                : recType;
+        final IVisitablePointable recAccessor = allocator.allocateRecordValue(inputType);
+        final APrintVisitor printVisitor = new APrintVisitor();
+        final Pair<PrintStream, ATypeTag> arg = new Pair<PrintStream, ATypeTag>(null, null);
 
-            private IPrinter[] fieldPrinters;
-            private IAType[] fieldTypes;
-            private int[] fieldOffsets;
-            private int numberOfSchemaFields, numberOfOpenFields, openPartOffset, fieldOffset, offsetArrayOffset,
-                    fieldValueLength, nullBitMapOffset, recordOffset;
-            private boolean isExpanded, hasNullableFields;
-            private ATypeTag tag;
+        return new IPrinter() {
 
             @Override
             public void init() throws AlgebricksException {
-
-                numberOfSchemaFields = 0;
-                if (recType != null) {
-                    numberOfSchemaFields = recType.getFieldNames().length;
-                    fieldPrinters = new IPrinter[numberOfSchemaFields];
-                    fieldTypes = new IAType[numberOfSchemaFields];
-                    fieldOffsets = new int[numberOfSchemaFields];
-                    for (int i = 0; i < numberOfSchemaFields; i++) {
-                        fieldTypes[i] = recType.getFieldTypes()[i];
-                        if (fieldTypes[i].getTypeTag() == ATypeTag.UNION
-                                && NonTaggedFormatUtil.isOptionalField((AUnionType) fieldTypes[i]))
-                            fieldPrinters[i] = (AqlPrinterFactoryProvider.INSTANCE
-                                    .getPrinterFactory(((AUnionType) fieldTypes[i]).getUnionList().get(
-                                            NonTaggedFormatUtil.OPTIONAL_TYPE_INDEX_IN_UNION_LIST))).createPrinter();
-                        else
-                            fieldPrinters[i] = (AqlPrinterFactoryProvider.INSTANCE.getPrinterFactory(fieldTypes[i]))
-                                    .createPrinter();
-                        fieldPrinters[i].init();
-                    }
-                }
+                arg.second = inputType.getTypeTag();
             }
 
             @Override
             public void print(byte[] b, int start, int l, PrintStream ps) throws AlgebricksException {
-                ps.print("{ ");
-                isExpanded = false;
-                openPartOffset = 0;
-                int s = start;
-                recordOffset = s;
-                if (recType == null) {
-                    openPartOffset = s + AInt32SerializerDeserializer.getInt(b, s + 6);
-                    s += 8;
-                    isExpanded = true;
-                } else {
-                    if (recType.isOpen()) {
-                        isExpanded = b[s + 5] == 1 ? true : false;
-                        if (isExpanded) {
-                            openPartOffset = s + AInt32SerializerDeserializer.getInt(b, s + 6);
-                            s += 10;
-                        } else
-                            s += 6;
-                    } else
-                        s += 5;
-                }
                 try {
-                    if (numberOfSchemaFields > 0) {
-                        s += 4;
-                        nullBitMapOffset = 0;
-                        hasNullableFields = NonTaggedFormatUtil.hasNullableField(recType);
-                        if (hasNullableFields) {
-                            nullBitMapOffset = s;
-                            offsetArrayOffset = s
-                                    + (this.numberOfSchemaFields % 8 == 0 ? numberOfSchemaFields / 8
-                                            : numberOfSchemaFields / 8 + 1);
-                        } else {
-                            offsetArrayOffset = s;
-                        }
-                        for (int i = 0; i < numberOfSchemaFields; i++) {
-                            fieldOffsets[i] = AInt32SerializerDeserializer.getInt(b, offsetArrayOffset) + recordOffset;
-                            offsetArrayOffset += 4;
-                        }
-                        for (int fieldNumber = 0; fieldNumber < numberOfSchemaFields; fieldNumber++) {
-                            if (fieldNumber != 0) {
-                                ps.print(", ");
-                            }
-                            ps.print("\"");
-                            ps.print(recType.getFieldNames()[fieldNumber]);
-                            ps.print("\": ");
-                            if (hasNullableFields) {
-                                byte b1 = b[nullBitMapOffset + fieldNumber / 8];
-                                int p = 1 << (7 - (fieldNumber % 8));
-                                if ((b1 & p) == 0) {
-                                    ps.print("null");
-                                    continue;
-                                }
-                            }
-                            if (fieldTypes[fieldNumber].getTypeTag() == ATypeTag.UNION) {
-                                if (NonTaggedFormatUtil.isOptionalField((AUnionType) fieldTypes[fieldNumber])) {
-                                    tag = ((AUnionType) fieldTypes[fieldNumber]).getUnionList()
-                                            .get(NonTaggedFormatUtil.OPTIONAL_TYPE_INDEX_IN_UNION_LIST).getTypeTag();
-                                    fieldValueLength = NonTaggedFormatUtil.getFieldValueLength(b,
-                                            fieldOffsets[fieldNumber], tag, false);
-                                    fieldPrinters[fieldNumber].print(b, fieldOffsets[fieldNumber] - 1,
-                                            fieldValueLength, ps);
-                                }
-                            } else {
-                                tag = fieldTypes[fieldNumber].getTypeTag();
-                                fieldValueLength = NonTaggedFormatUtil.getFieldValueLength(b,
-                                        fieldOffsets[fieldNumber], tag, false);
-                                fieldPrinters[fieldNumber]
-                                        .print(b, fieldOffsets[fieldNumber] - 1, fieldValueLength, ps);
-                            }
-                        }
-                        if (isExpanded)
-                            ps.print(", ");
-                    }
-                    if (isExpanded) {
-                        numberOfOpenFields = AInt32SerializerDeserializer.getInt(b, openPartOffset);
-                        fieldOffset = openPartOffset + 4 + (8 * numberOfOpenFields);
-                        for (int i = 0; i < numberOfOpenFields; i++) {
-                            // we print the field name
-                            fieldValueLength = NonTaggedFormatUtil.getFieldValueLength(b, fieldOffset, ATypeTag.STRING,
-                                    false);
-                            AStringPrinter.INSTANCE.print(b, fieldOffset - 1, fieldValueLength, ps);
-                            fieldOffset += fieldValueLength;
-                            ps.print(": ");
-                            // now we print the value
-                            tag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(b[fieldOffset]);
-                            fieldValueLength = NonTaggedFormatUtil.getFieldValueLength(b, fieldOffset, tag, true) + 1;
-                            AObjectPrinter.INSTANCE.print(b, fieldOffset, fieldValueLength, ps);
-                            fieldOffset += fieldValueLength;
-                            if (i + 1 < numberOfOpenFields)
-                                ps.print(", ");
-                        }
-                    }
-                } catch (AsterixException e) {
-                    throw new AlgebricksException(e);
+                    recAccessor.set(b, start, l);
+                    arg.first = ps;
+                    recAccessor.accept(printVisitor, arg);
+                } catch (Exception ioe) {
+                    throw new AlgebricksException(ioe);
                 }
-                ps.print(" }");
             }
         };
     }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARectanglePrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARectanglePrinter.java
index 17caa53..0618f28 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARectanglePrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ARectanglePrinter.java
@@ -8,7 +8,6 @@
 
 public class ARectanglePrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final ARectanglePrinter INSTANCE = new ARectanglePrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AStringPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AStringPrinter.java
index 6f5b629..d491777 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AStringPrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AStringPrinter.java
@@ -9,7 +9,6 @@
 
 public class AStringPrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final AStringPrinter INSTANCE = new AStringPrinter();
 
     @Override
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ATimePrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ATimePrinter.java
index 72a6e37..b77ee64 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ATimePrinter.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/ATimePrinter.java
@@ -9,10 +9,9 @@
 
 public class ATimePrinter implements IPrinter {
 
-    private static final long serialVersionUID = 1L;
     public static final ATimePrinter INSTANCE = new ATimePrinter();
     private static final GregorianCalendarSystem gCalInstance = GregorianCalendarSystem.getInstance();
-    
+
     @Override
     public void init() {
 
@@ -21,7 +20,7 @@
     @Override
     public void print(byte[] b, int s, int l, PrintStream ps) throws AlgebricksException {
         int time = AInt32SerializerDeserializer.getInt(b, s + 1);
-        
+
         ps.print("time(\"");
         ps.append(String.format("%02d", gCalInstance.getHourOfDay(time))).append(":")
                 .append(String.format("%02d", gCalInstance.getMinOfHour(time))).append(":")
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AUnorderedlistPrinterFactory.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AUnorderedlistPrinterFactory.java
index 59af365..f779919 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AUnorderedlistPrinterFactory.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/printers/AUnorderedlistPrinterFactory.java
@@ -2,22 +2,21 @@
 
 import java.io.PrintStream;
 
-import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.dataflow.data.nontagged.serde.AInt32SerializerDeserializer;
-import edu.uci.ics.asterix.formats.nontagged.AqlPrinterFactoryProvider;
+import edu.uci.ics.asterix.om.pointables.PointableAllocator;
+import edu.uci.ics.asterix.om.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.pointables.printer.APrintVisitor;
 import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.AUnorderedListType;
-import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
 import edu.uci.ics.hyracks.algebricks.data.IPrinter;
 import edu.uci.ics.hyracks.algebricks.data.IPrinterFactory;
 
 public class AUnorderedlistPrinterFactory implements IPrinterFactory {
 
     private static final long serialVersionUID = 1L;
-
     private AUnorderedListType unorderedlistType;
 
     public AUnorderedlistPrinterFactory(AUnorderedListType unorderedlistType) {
@@ -27,77 +26,29 @@
     @Override
     public IPrinter createPrinter() {
 
-        return new IPrinter() {
+        PointableAllocator allocator = new PointableAllocator();
+        final IAType inputType = unorderedlistType == null ? DefaultOpenFieldType
+                .getDefaultOpenFieldType(ATypeTag.UNORDEREDLIST) : unorderedlistType;
+        final IVisitablePointable listAccessor = allocator.allocateListValue(inputType);
+        final APrintVisitor printVisitor = new APrintVisitor();
+        final Pair<PrintStream, ATypeTag> arg = new Pair<PrintStream, ATypeTag>(null, null);
 
-            private IPrinter itemPrinter;
-            private IAType itemType;
-            private ATypeTag itemTag;
-            private boolean typedItemList = false;
+        return new IPrinter() {
 
             @Override
             public void init() throws AlgebricksException {
-
-                if (unorderedlistType != null && unorderedlistType.getItemType() != null) {
-                    itemType = unorderedlistType.getItemType();
-                    if (itemType.getTypeTag() == ATypeTag.ANY) {
-                        this.typedItemList = false;
-                        this.itemPrinter = AObjectPrinterFactory.INSTANCE.createPrinter();
-                    } else {
-                        this.typedItemList = true;
-                        itemPrinter = AqlPrinterFactoryProvider.INSTANCE.getPrinterFactory(itemType).createPrinter();
-                        itemTag = unorderedlistType.getItemType().getTypeTag();
-                    }
-                } else {
-                    this.typedItemList = false;
-                    this.itemPrinter = AObjectPrinterFactory.INSTANCE.createPrinter();
-                }
-                itemPrinter.init();
+                arg.second = inputType.getTypeTag();
             }
 
             @Override
-            public void print(byte[] b, int s, int l, PrintStream ps) throws AlgebricksException {
-                ps.print("{{ ");
-                int numberOfitems = AInt32SerializerDeserializer.getInt(b, s + 6);
-                int itemOffset;
-                if (typedItemList) {
-                    switch (itemTag) {
-                        case STRING:
-                        case RECORD:
-                        case ORDEREDLIST:
-                        case UNORDEREDLIST:
-                        case ANY:
-                            itemOffset = s + 10 + (numberOfitems * 4);
-                            break;
-                        default:
-                            itemOffset = s + 10;
-                    }
-                } else
-                    itemOffset = s + 10 + (numberOfitems * 4);
-                int itemLength;
-
+            public void print(byte[] b, int start, int l, PrintStream ps) throws AlgebricksException {
                 try {
-                    if (typedItemList) {
-                        for (int i = 0; i < numberOfitems; i++) {
-                            itemLength = NonTaggedFormatUtil.getFieldValueLength(b, itemOffset, itemTag, false);
-                            itemPrinter.print(b, itemOffset - 1, itemLength, ps);
-                            itemOffset += itemLength;
-                            if (i + 1 < numberOfitems)
-                                ps.print(", ");
-                        }
-                    } else {
-                        for (int i = 0; i < numberOfitems; i++) {
-                            itemTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(b[itemOffset]);
-                            itemLength = NonTaggedFormatUtil.getFieldValueLength(b, itemOffset, itemTag, true) + 1;
-                            itemPrinter.print(b, itemOffset, itemLength, ps);
-                            itemOffset += itemLength;
-                            if (i + 1 < numberOfitems)
-                                ps.print(", ");
-                        }
-                    }
-                } catch (AsterixException e) {
-                    throw new AlgebricksException(e);
+                    listAccessor.set(b, start, l);
+                    arg.first = ps;
+                    listAccessor.accept(printVisitor, arg);
+                } catch (Exception ioe) {
+                    throw new AlgebricksException(ioe);
                 }
-                ps.print(" }}");
             }
         };
     }
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/serde/AOrderedListSerializerDeserializer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/serde/AOrderedListSerializerDeserializer.java
index 38262d3..4d115d7 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/serde/AOrderedListSerializerDeserializer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/dataflow/data/nontagged/serde/AOrderedListSerializerDeserializer.java
@@ -25,9 +25,9 @@
     public static final AOrderedListSerializerDeserializer SCHEMALESS_INSTANCE = new AOrderedListSerializerDeserializer();
 
     private IAType itemType;
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings("rawtypes")
     private ISerializerDeserializer serializer;
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings("rawtypes")
     private ISerializerDeserializer deserializer;
     private AOrderedListType orderedlistType;
 
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/formats/nontagged/AqlSerializerDeserializerProvider.java b/asterix-om/src/main/java/edu/uci/ics/asterix/formats/nontagged/AqlSerializerDeserializerProvider.java
index d4d4fb6..5492c5c 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/formats/nontagged/AqlSerializerDeserializerProvider.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/formats/nontagged/AqlSerializerDeserializerProvider.java
@@ -48,7 +48,7 @@
     private AqlSerializerDeserializerProvider() {
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings("rawtypes")
     @Override
     public ISerializerDeserializer getSerializerDeserializer(Object typeInfo) {
         IAType aqlType = (IAType) typeInfo;
@@ -63,7 +63,7 @@
         }
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings("rawtypes")
     public ISerializerDeserializer getNonTaggedSerializerDeserializer(IAType aqlType) {
         switch (aqlType.getTypeTag()) {
             case CIRCLE: {
@@ -139,7 +139,7 @@
         }
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings("rawtypes")
     private ISerializerDeserializer addTag(final ISerializerDeserializer nonTaggedSerde, final ATypeTag typeTag) {
         return new ISerializerDeserializer<IAObject>() {
 
@@ -148,13 +148,15 @@
             @Override
             public IAObject deserialize(DataInput in) throws HyracksDataException {
                 try {
-                    ATypeTag typeTag = SerializerDeserializerUtil.deserializeTag(in);
+                    //deserialize the tag to move the  input cursor forward 
+                    SerializerDeserializerUtil.deserializeTag(in);
                 } catch (IOException e) {
                     throw new HyracksDataException(e);
                 }
                 return (IAObject) nonTaggedSerde.deserialize(in);
             }
 
+            @SuppressWarnings("unchecked")
             @Override
             public void serialize(IAObject instance, DataOutput out) throws HyracksDataException {
                 SerializerDeserializerUtil.serializeTag(instance, out);
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AFlatValuePointable.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/AFlatValuePointable.java
similarity index 89%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AFlatValuePointable.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/AFlatValuePointable.java
index a3547a4..445e2a2 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AFlatValuePointable.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/AFlatValuePointable.java
@@ -13,13 +13,13 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables;
+package edu.uci.ics.asterix.om.pointables;
 
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.pointables.visitor.IVisitablePointableVisitor;
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
-import edu.uci.ics.asterix.runtime.pointables.visitor.IVisitablePointableVisitor;
-import edu.uci.ics.asterix.runtime.util.container.IObjectFactory;
+import edu.uci.ics.asterix.om.util.container.IObjectFactory;
 import edu.uci.ics.hyracks.data.std.api.IValueReference;
 
 /**
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AListPointable.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/AListPointable.java
similarity index 91%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AListPointable.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/AListPointable.java
index 6f85694..ab32b6b 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AListPointable.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/AListPointable.java
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables;
+package edu.uci.ics.asterix.om.pointables;
 
 import java.io.DataOutputStream;
 import java.util.ArrayList;
@@ -21,21 +21,21 @@
 
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 import edu.uci.ics.asterix.dataflow.data.nontagged.serde.AInt32SerializerDeserializer;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.pointables.visitor.IVisitablePointableVisitor;
+import edu.uci.ics.asterix.om.types.AOrderedListType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.AbstractCollectionType;
 import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
 import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
-import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
-import edu.uci.ics.asterix.runtime.pointables.visitor.IVisitablePointableVisitor;
-import edu.uci.ics.asterix.runtime.util.ResettableByteArrayOutputStream;
-import edu.uci.ics.asterix.runtime.util.container.IObjectFactory;
+import edu.uci.ics.asterix.om.util.ResettableByteArrayOutputStream;
+import edu.uci.ics.asterix.om.util.container.IObjectFactory;
 
 /**
  * This class interprets the binary data representation of a list, one can
  * call getItems and getItemTags to get pointable objects for items and item
  * type tags.
- * 
  */
 public class AListPointable extends AbstractVisitablePointable {
 
@@ -59,6 +59,7 @@
     private IAType itemType;
     private ATypeTag itemTag;
     private boolean typedItemList = false;
+    private boolean ordered = false;
 
     /**
      * private constructor, to prevent constructing it arbitrarily
@@ -66,6 +67,9 @@
      * @param inputType
      */
     private AListPointable(AbstractCollectionType inputType) {
+        if (inputType instanceof AOrderedListType) {
+            ordered = true;
+        }
         if (inputType != null && inputType.getItemType() != null) {
             itemType = inputType.getItemType();
             if (itemType.getTypeTag() == ATypeTag.ANY) {
@@ -136,7 +140,7 @@
                     itemTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(b[itemOffset]);
                     itemLength = NonTaggedFormatUtil.getFieldValueLength(b, itemOffset, itemTag, true) + 1;
                     IVisitablePointable tag = allocator.allocateEmpty();
-                    IVisitablePointable item = allocator.allocateFieldValue(itemType);
+                    IVisitablePointable item = allocator.allocateFieldValue(itemTag);
 
                     // set item type tag
                     int start = dataBos.size();
@@ -168,4 +172,8 @@
     public List<IVisitablePointable> getItemTags() {
         return itemTags;
     }
+
+    public boolean ordered() {
+        return ordered;
+    }
 }
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/ARecordPointable.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/ARecordPointable.java
similarity index 96%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/ARecordPointable.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/ARecordPointable.java
index 5904170..7e097af 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/ARecordPointable.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/ARecordPointable.java
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables;
+package edu.uci.ics.asterix.om.pointables;
 
 import java.io.DataOutputStream;
 import java.io.IOException;
@@ -23,16 +23,16 @@
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 import edu.uci.ics.asterix.dataflow.data.nontagged.AqlNullWriterFactory;
 import edu.uci.ics.asterix.dataflow.data.nontagged.serde.AInt32SerializerDeserializer;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.pointables.visitor.IVisitablePointableVisitor;
 import edu.uci.ics.asterix.om.types.ARecordType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
 import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
-import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
-import edu.uci.ics.asterix.runtime.pointables.visitor.IVisitablePointableVisitor;
-import edu.uci.ics.asterix.runtime.util.ResettableByteArrayOutputStream;
-import edu.uci.ics.asterix.runtime.util.container.IObjectFactory;
+import edu.uci.ics.asterix.om.util.ResettableByteArrayOutputStream;
+import edu.uci.ics.asterix.om.util.container.IObjectFactory;
 import edu.uci.ics.hyracks.api.dataflow.value.INullWriter;
 
 /**
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AbstractVisitablePointable.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/AbstractVisitablePointable.java
similarity index 89%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AbstractVisitablePointable.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/AbstractVisitablePointable.java
index 8cfe661..943a967 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/AbstractVisitablePointable.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/AbstractVisitablePointable.java
@@ -13,21 +13,20 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables;
+package edu.uci.ics.asterix.om.pointables;
 
-import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
 import edu.uci.ics.hyracks.data.std.api.IValueReference;
 
 /**
  * This class implements several "routine" methods in IVisitablePointable
  * interface, so that subclasses do not need to repeat the same code.
- * 
  */
 public abstract class AbstractVisitablePointable implements IVisitablePointable {
 
     private byte[] data;
-    private int start;
-    private int len;
+    private int start = -1;
+    private int len = -1;
 
     @Override
     public byte[] getByteArray() {
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/PointableAllocator.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/PointableAllocator.java
similarity index 90%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/PointableAllocator.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/PointableAllocator.java
index b6116be..5ad2ccb 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/PointableAllocator.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/PointableAllocator.java
@@ -13,14 +13,14 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables;
+package edu.uci.ics.asterix.om.pointables;
 
+import edu.uci.ics.asterix.om.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
 import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.runtime.pointables.base.DefaultOpenFieldType;
-import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
-import edu.uci.ics.asterix.runtime.util.container.IObjectPool;
-import edu.uci.ics.asterix.runtime.util.container.ListObjectPool;
+import edu.uci.ics.asterix.om.util.container.IObjectPool;
+import edu.uci.ics.asterix.om.util.container.ListObjectPool;
 
 /**
  * This class is the ONLY place to create IVisitablePointable object instances,
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/DefaultOpenFieldType.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/base/DefaultOpenFieldType.java
similarity index 97%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/DefaultOpenFieldType.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/base/DefaultOpenFieldType.java
index 987ac02..9184616 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/DefaultOpenFieldType.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/base/DefaultOpenFieldType.java
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables.base;
+package edu.uci.ics.asterix.om.pointables.base;
 
 import edu.uci.ics.asterix.om.types.AOrderedListType;
 import edu.uci.ics.asterix.om.types.ARecordType;
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/IVisitablePointable.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/base/IVisitablePointable.java
similarity index 88%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/IVisitablePointable.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/base/IVisitablePointable.java
index 28c61c2..7456ee8 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/base/IVisitablePointable.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/base/IVisitablePointable.java
@@ -13,10 +13,10 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables.base;
+package edu.uci.ics.asterix.om.pointables.base;
 
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.runtime.pointables.visitor.IVisitablePointableVisitor;
+import edu.uci.ics.asterix.om.pointables.visitor.IVisitablePointableVisitor;
 import edu.uci.ics.hyracks.data.std.api.IPointable;
 
 /**
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ACastVisitor.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ACastVisitor.java
similarity index 80%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ACastVisitor.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ACastVisitor.java
index 822e37c..9fccf4a 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ACastVisitor.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ACastVisitor.java
@@ -13,31 +13,31 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables.cast;
+package edu.uci.ics.asterix.om.pointables.cast;
 
 import java.util.HashMap;
 import java.util.Map;
 
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
+import edu.uci.ics.asterix.om.pointables.AFlatValuePointable;
+import edu.uci.ics.asterix.om.pointables.AListPointable;
+import edu.uci.ics.asterix.om.pointables.ARecordPointable;
+import edu.uci.ics.asterix.om.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.pointables.visitor.IVisitablePointableVisitor;
 import edu.uci.ics.asterix.om.types.ARecordType;
+import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.AbstractCollectionType;
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.runtime.pointables.AFlatValuePointable;
-import edu.uci.ics.asterix.runtime.pointables.AListPointable;
-import edu.uci.ics.asterix.runtime.pointables.ARecordPointable;
-import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
-import edu.uci.ics.asterix.runtime.pointables.visitor.IVisitablePointableVisitor;
 import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
 
 /**
  * This class is a IVisitablePointableVisitor implementation which recursively
  * visit a given record, list or flat value of a given type, and cast it to a
  * specified type. For example:
- * 
  * A record { "hobby": {{"music", "coding"}}, "id": "001", "name":
  * "Person Three"} which confirms to closed type ( id: string, name: string,
  * hobby: {{string}}? ) can be casted to a open type (id: string )
- * 
  * Since the open/closed part of a record has a completely different underlying
  * memory/storage layout, the visitor will change the layout as specified at
  * runtime.
@@ -56,6 +56,9 @@
             laccessorToCaster.put(accessor, caster);
         }
         try {
+            if (arg.second.getTypeTag().equals(ATypeTag.ANY)) {
+                arg.second = DefaultOpenFieldType.NESTED_OPEN_AUNORDERED_LIST_TYPE;
+            }
             caster.castList(accessor, arg.first, (AbstractCollectionType) arg.second, this);
         } catch (Exception e) {
             throw new AsterixException(e);
@@ -72,6 +75,9 @@
             raccessorToCaster.put(accessor, caster);
         }
         try {
+            if (arg.second.getTypeTag().equals(ATypeTag.ANY)) {
+                arg.second = DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
+            }
             caster.castRecord(accessor, arg.first, (ARecordType) arg.second, this);
         } catch (Exception e) {
             throw new AsterixException(e);
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/AListCaster.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/AListCaster.java
similarity index 90%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/AListCaster.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/AListCaster.java
index e10fb72..c65b3eb 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/AListCaster.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/AListCaster.java
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables.cast;
+package edu.uci.ics.asterix.om.pointables.cast;
 
 import java.io.DataOutput;
 import java.io.DataOutputStream;
@@ -23,17 +23,17 @@
 import edu.uci.ics.asterix.builders.OrderedListBuilder;
 import edu.uci.ics.asterix.builders.UnorderedListBuilder;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
+import edu.uci.ics.asterix.om.pointables.AListPointable;
+import edu.uci.ics.asterix.om.pointables.PointableAllocator;
+import edu.uci.ics.asterix.om.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
 import edu.uci.ics.asterix.om.types.AOrderedListType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.AUnorderedListType;
 import edu.uci.ics.asterix.om.types.AbstractCollectionType;
 import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
-import edu.uci.ics.asterix.runtime.pointables.AListPointable;
-import edu.uci.ics.asterix.runtime.pointables.PointableAllocator;
-import edu.uci.ics.asterix.runtime.pointables.base.DefaultOpenFieldType;
-import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
-import edu.uci.ics.asterix.runtime.util.ResettableByteArrayOutputStream;
+import edu.uci.ics.asterix.om.util.ResettableByteArrayOutputStream;
 import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
 
 /**
@@ -64,9 +64,11 @@
             AbstractCollectionType reqType, ACastVisitor visitor) throws IOException, AsterixException {
         if (reqType.getTypeTag().equals(ATypeTag.UNORDEREDLIST)) {
             unOrderedListBuilder.reset((AUnorderedListType) reqType);
+            reqItemType = reqType.getItemType();
         }
         if (reqType.getTypeTag().equals(ATypeTag.ORDEREDLIST)) {
             orderedListBuilder.reset((AOrderedListType) reqType);
+            reqItemType = reqType.getItemType();
         }
         dataBos.reset();
 
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ARecordCaster.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ARecordCaster.java
similarity index 96%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ARecordCaster.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ARecordCaster.java
index 8ef2ef0..fbad7a7 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/cast/ARecordCaster.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/cast/ARecordCaster.java
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables.cast;
+package edu.uci.ics.asterix.om.pointables.cast;
 
 import java.io.DataOutput;
 import java.io.DataOutputStream;
@@ -24,17 +24,17 @@
 import edu.uci.ics.asterix.builders.RecordBuilder;
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
 import edu.uci.ics.asterix.dataflow.data.nontagged.AqlNullWriterFactory;
+import edu.uci.ics.asterix.om.pointables.ARecordPointable;
+import edu.uci.ics.asterix.om.pointables.PointableAllocator;
+import edu.uci.ics.asterix.om.pointables.base.DefaultOpenFieldType;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
 import edu.uci.ics.asterix.om.types.ARecordType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
 import edu.uci.ics.asterix.om.types.AUnionType;
 import edu.uci.ics.asterix.om.types.EnumDeserializer;
 import edu.uci.ics.asterix.om.types.IAType;
 import edu.uci.ics.asterix.om.util.NonTaggedFormatUtil;
-import edu.uci.ics.asterix.runtime.pointables.ARecordPointable;
-import edu.uci.ics.asterix.runtime.pointables.PointableAllocator;
-import edu.uci.ics.asterix.runtime.pointables.base.DefaultOpenFieldType;
-import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
-import edu.uci.ics.asterix.runtime.util.ResettableByteArrayOutputStream;
+import edu.uci.ics.asterix.om.util.ResettableByteArrayOutputStream;
 import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
 import edu.uci.ics.hyracks.api.dataflow.value.IBinaryComparator;
 import edu.uci.ics.hyracks.api.dataflow.value.INullWriter;
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/printer/AListPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/printer/AListPrinter.java
new file mode 100644
index 0000000..283c916
--- /dev/null
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/printer/AListPrinter.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2009-2010 by The Regents of the University of California
+ * Licensed 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 from
+ * 
+ *     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 edu.uci.ics.asterix.om.pointables.printer;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.List;
+
+import edu.uci.ics.asterix.common.exceptions.AsterixException;
+import edu.uci.ics.asterix.om.pointables.AListPointable;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.asterix.om.types.EnumDeserializer;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
+
+/**
+ * This class is to print the content of a list. It is ONLY visible to
+ * APrintVisitor.
+ */
+class AListPrinter {
+    private static String LEFT_PAREN = "{{ ";
+    private static String RIGHT_PAREN = " }}";
+    private static String LEFT_PAREN_ORDERED = "[ ";
+    private static String RIGHT_PAREN_ORDERED = " ]";
+    private static String COMMA = ", ";
+
+    private final Pair<PrintStream, ATypeTag> itemVisitorArg = new Pair<PrintStream, ATypeTag>(null, null);
+    private String leftParen = LEFT_PAREN;
+    private String rightParen = RIGHT_PAREN;
+
+    public AListPrinter(boolean ordered) {
+        if (ordered) {
+            leftParen = LEFT_PAREN_ORDERED;
+            rightParen = RIGHT_PAREN_ORDERED;
+        }
+    }
+
+    public void printList(AListPointable listAccessor, PrintStream ps, APrintVisitor visitor) throws IOException,
+            AsterixException {
+        List<IVisitablePointable> itemTags = listAccessor.getItemTags();
+        List<IVisitablePointable> items = listAccessor.getItems();
+        itemVisitorArg.first = ps;
+
+        // print the beginning part
+        ps.print(leftParen);
+
+        // print item 0 to n-2
+        for (int i = 0; i < items.size() - 1; i++) {
+            printItem(visitor, itemTags, items, i);
+            // print the comma
+            ps.print(COMMA);
+        }
+
+        // print item n-1
+        if (items.size() > 0) {
+            printItem(visitor, itemTags, items, items.size() - 1);
+        }
+
+        // print the end part
+        ps.print(rightParen);
+    }
+
+    private void printItem(APrintVisitor visitor, List<IVisitablePointable> itemTags, List<IVisitablePointable> items,
+            int i) throws AsterixException {
+        IVisitablePointable itemTypeTag = itemTags.get(i);
+        IVisitablePointable item = items.get(i);
+        ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(itemTypeTag.getByteArray()[itemTypeTag
+                .getStartOffset()]);
+        itemVisitorArg.second = item.getLength() <= 1 ? ATypeTag.NULL : typeTag;
+        item.accept(visitor, itemVisitorArg);
+    }
+}
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/printer/APrintVisitor.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/printer/APrintVisitor.java
new file mode 100644
index 0000000..3664804
--- /dev/null
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/printer/APrintVisitor.java
@@ -0,0 +1,185 @@
+/*
+ * Copyright 2009-2010 by The Regents of the University of California
+ * Licensed 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 from
+ * 
+ *     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 edu.uci.ics.asterix.om.pointables.printer;
+
+import java.io.PrintStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import edu.uci.ics.asterix.common.exceptions.AsterixException;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.ABooleanPrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.ACirclePrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.ADatePrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.ADateTimePrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.ADoublePrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.ADurationPrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.AFloatPrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.AInt16Printer;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.AInt32Printer;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.AInt64Printer;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.AInt8Printer;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.ALinePrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.ANullPrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.APoint3DPrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.APointPrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.APolygonPrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.ARectanglePrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.AStringPrinter;
+import edu.uci.ics.asterix.dataflow.data.nontagged.printers.ATimePrinter;
+import edu.uci.ics.asterix.om.pointables.AFlatValuePointable;
+import edu.uci.ics.asterix.om.pointables.AListPointable;
+import edu.uci.ics.asterix.om.pointables.ARecordPointable;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.pointables.visitor.IVisitablePointableVisitor;
+import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.hyracks.algebricks.common.exceptions.NotImplementedException;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
+
+/**
+ * This class is a IVisitablePointableVisitor implementation which recursively
+ * visit a given record, list or flat value of a given type, and print it to a
+ * PrintStream in adm format.
+ */
+public class APrintVisitor implements IVisitablePointableVisitor<Void, Pair<PrintStream, ATypeTag>> {
+
+    private final Map<IVisitablePointable, ARecordPrinter> raccessorToPrinter = new HashMap<IVisitablePointable, ARecordPrinter>();
+    private final Map<IVisitablePointable, AListPrinter> laccessorToPrinter = new HashMap<IVisitablePointable, AListPrinter>();
+
+    @Override
+    public Void visit(AListPointable accessor, Pair<PrintStream, ATypeTag> arg) throws AsterixException {
+        AListPrinter printer = laccessorToPrinter.get(accessor);
+        if (printer == null) {
+            printer = new AListPrinter(accessor.ordered());
+            laccessorToPrinter.put(accessor, printer);
+        }
+        try {
+            printer.printList(accessor, arg.first, this);
+        } catch (Exception e) {
+            throw new AsterixException(e);
+        }
+        return null;
+    }
+
+    @Override
+    public Void visit(ARecordPointable accessor, Pair<PrintStream, ATypeTag> arg) throws AsterixException {
+        ARecordPrinter printer = raccessorToPrinter.get(accessor);
+        if (printer == null) {
+            printer = new ARecordPrinter();
+            raccessorToPrinter.put(accessor, printer);
+        }
+        try {
+            printer.printRecord(accessor, arg.first, this);
+        } catch (Exception e) {
+            throw new AsterixException(e);
+        }
+        return null;
+    }
+
+    @Override
+    public Void visit(AFlatValuePointable accessor, Pair<PrintStream, ATypeTag> arg) {
+        try {
+            byte[] b = accessor.getByteArray();
+            int s = accessor.getStartOffset();
+            int l = accessor.getLength();
+            PrintStream ps = arg.first;
+            ATypeTag typeTag = arg.second;
+            switch (typeTag) {
+                case INT8: {
+                    AInt8Printer.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case INT16: {
+                    AInt16Printer.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case INT32: {
+                    AInt32Printer.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case INT64: {
+                    AInt64Printer.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case NULL: {
+                    ANullPrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case BOOLEAN: {
+                    ABooleanPrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case FLOAT: {
+                    AFloatPrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case DOUBLE: {
+                    ADoublePrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case DATE: {
+                    ADatePrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case TIME: {
+                    ATimePrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case DATETIME: {
+                    ADateTimePrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case DURATION: {
+                    ADurationPrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case POINT: {
+                    APointPrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case POINT3D: {
+                    APoint3DPrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case LINE: {
+                    ALinePrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case POLYGON: {
+                    APolygonPrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case CIRCLE: {
+                    ACirclePrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case RECTANGLE: {
+                    ARectanglePrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                case STRING: {
+                    AStringPrinter.INSTANCE.print(b, s, l, ps);
+                    break;
+                }
+                default: {
+                    throw new NotImplementedException("No printer for type " + typeTag);
+                }
+            }
+            return null;
+        } catch (Exception e) {
+            throw new IllegalStateException(e);
+        }
+    }
+}
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/printer/ARecordPrinter.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/printer/ARecordPrinter.java
new file mode 100644
index 0000000..55f9447
--- /dev/null
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/printer/ARecordPrinter.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2009-2010 by The Regents of the University of California
+ * Licensed 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 from
+ * 
+ *     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 edu.uci.ics.asterix.om.pointables.printer;
+
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.List;
+
+import edu.uci.ics.asterix.common.exceptions.AsterixException;
+import edu.uci.ics.asterix.om.pointables.ARecordPointable;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.types.ATypeTag;
+import edu.uci.ics.asterix.om.types.EnumDeserializer;
+import edu.uci.ics.hyracks.algebricks.common.utils.Pair;
+
+/**
+ * This class is to print the content of a record. It is ONLY visible to
+ * APrintVisitor.
+ */
+class ARecordPrinter {
+    private static String LEFT_PAREN = "{ ";
+    private static String RIGHT_PAREN = " }";
+    private static String COMMA = ", ";
+    private static String COLON = ": ";
+
+    private final Pair<PrintStream, ATypeTag> nameVisitorArg = new Pair<PrintStream, ATypeTag>(null, ATypeTag.STRING);
+    private final Pair<PrintStream, ATypeTag> itemVisitorArg = new Pair<PrintStream, ATypeTag>(null, null);
+
+    public ARecordPrinter() {
+
+    }
+
+    public void printRecord(ARecordPointable recordAccessor, PrintStream ps, APrintVisitor visitor) throws IOException,
+            AsterixException {
+        List<IVisitablePointable> fieldNames = recordAccessor.getFieldNames();
+        List<IVisitablePointable> fieldTags = recordAccessor.getFieldTypeTags();
+        List<IVisitablePointable> fieldValues = recordAccessor.getFieldValues();
+
+        nameVisitorArg.first = ps;
+        itemVisitorArg.first = ps;
+
+        // print the beginning part
+        ps.print(LEFT_PAREN);
+
+        // print field 0 to n-2
+        for (int i = 0; i < fieldNames.size() - 1; i++) {
+            printField(ps, visitor, fieldNames, fieldTags, fieldValues, i);
+            // print the comma
+            ps.print(COMMA);
+        }
+
+        // print field n-1
+        if (fieldValues.size() > 0) {
+            printField(ps, visitor, fieldNames, fieldTags, fieldValues, fieldValues.size() - 1);
+        }
+
+        // print the end part
+        ps.print(RIGHT_PAREN);
+    }
+
+    private void printField(PrintStream ps, APrintVisitor visitor, List<IVisitablePointable> fieldNames,
+            List<IVisitablePointable> fieldTags, List<IVisitablePointable> fieldValues, int i) throws AsterixException {
+        IVisitablePointable itemTypeTag = fieldTags.get(i);
+        IVisitablePointable item = fieldValues.get(i);
+        ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(itemTypeTag.getByteArray()[itemTypeTag
+                .getStartOffset()]);
+        itemVisitorArg.second = item.getLength() <= 1 ? ATypeTag.NULL : typeTag;
+
+        // print field name
+        fieldNames.get(i).accept(visitor, nameVisitorArg);
+        ps.print(COLON);
+        // print field value
+        item.accept(visitor, itemVisitorArg);
+    }
+}
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/visitor/IVisitablePointableVisitor.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/visitor/IVisitablePointableVisitor.java
similarity index 82%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/visitor/IVisitablePointableVisitor.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/visitor/IVisitablePointableVisitor.java
index 669e3fc..bcb4b34 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/pointables/visitor/IVisitablePointableVisitor.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/pointables/visitor/IVisitablePointableVisitor.java
@@ -13,12 +13,12 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.pointables.visitor;
+package edu.uci.ics.asterix.om.pointables.visitor;
 
 import edu.uci.ics.asterix.common.exceptions.AsterixException;
-import edu.uci.ics.asterix.runtime.pointables.AFlatValuePointable;
-import edu.uci.ics.asterix.runtime.pointables.AListPointable;
-import edu.uci.ics.asterix.runtime.pointables.ARecordPointable;
+import edu.uci.ics.asterix.om.pointables.AFlatValuePointable;
+import edu.uci.ics.asterix.om.pointables.AListPointable;
+import edu.uci.ics.asterix.om.pointables.ARecordPointable;
 
 /**
  * This interface is a visitor for all the three different IVisitablePointable
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/NonTaggedFieldAccessByNameResultType.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/NonTaggedFieldAccessByNameResultType.java
index a0c35b6..db9d932 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/NonTaggedFieldAccessByNameResultType.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/typecomputer/impl/NonTaggedFieldAccessByNameResultType.java
@@ -2,6 +2,7 @@
 
 import edu.uci.ics.asterix.om.base.AString;
 import edu.uci.ics.asterix.om.constants.AsterixConstantValue;
+import edu.uci.ics.asterix.om.pointables.base.DefaultOpenFieldType;
 import edu.uci.ics.asterix.om.typecomputer.base.IResultTypeComputer;
 import edu.uci.ics.asterix.om.types.ARecordType;
 import edu.uci.ics.asterix.om.types.ATypeTag;
@@ -60,7 +61,7 @@
                 return (ARecordType) type0;
             }
             case ANY: {
-                return null;
+                return DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
             }
             case UNION: {
                 AUnionType u = (AUnionType) type0;
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/AsterixRuntimeUtil.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/AsterixRuntimeUtil.java
similarity index 97%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/AsterixRuntimeUtil.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/util/AsterixRuntimeUtil.java
index ab57288..624d7eb 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/AsterixRuntimeUtil.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/AsterixRuntimeUtil.java
@@ -12,7 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package edu.uci.ics.asterix.runtime.util;
+package edu.uci.ics.asterix.om.util;
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/ResettableByteArrayOutputStream.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/ResettableByteArrayOutputStream.java
similarity index 88%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/ResettableByteArrayOutputStream.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/util/ResettableByteArrayOutputStream.java
index 968c3f2..e47d417 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/ResettableByteArrayOutputStream.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/ResettableByteArrayOutputStream.java
@@ -1,4 +1,4 @@
-package edu.uci.ics.asterix.runtime.util;
+package edu.uci.ics.asterix.om.util;
 
 import edu.uci.ics.hyracks.data.std.util.ByteArrayAccessibleOutputStream;
 
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectFactory.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/container/IObjectFactory.java
similarity index 93%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectFactory.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/util/container/IObjectFactory.java
index 85f0fa3..bf11b76 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectFactory.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/container/IObjectFactory.java
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.util.container;
+package edu.uci.ics.asterix.om.util.container;
 
 /**
  * A factory interface to create objects.
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectPool.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/container/IObjectPool.java
similarity index 94%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectPool.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/util/container/IObjectPool.java
index dd2c3bf..4969cd5 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/IObjectPool.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/container/IObjectPool.java
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.util.container;
+package edu.uci.ics.asterix.om.util.container;
 
 /**
  * A reusable object pool interface.
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/container/ListObjectPool.java
similarity index 97%
rename from asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java
rename to asterix-om/src/main/java/edu/uci/ics/asterix/om/util/container/ListObjectPool.java
index b8f8e3a..8d9057c 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/util/container/ListObjectPool.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/util/container/ListObjectPool.java
@@ -13,7 +13,7 @@
  * limitations under the License.
  */
 
-package edu.uci.ics.asterix.runtime.util.container;
+package edu.uci.ics.asterix.om.util.container;
 
 import java.util.ArrayList;
 import java.util.BitSet;
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/CastRecordDescriptor.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/CastRecordDescriptor.java
index 4c0a201..8bb4d49 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/CastRecordDescriptor.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/evaluators/functions/CastRecordDescriptor.java
@@ -5,12 +5,12 @@
 import edu.uci.ics.asterix.om.functions.AsterixBuiltinFunctions;
 import edu.uci.ics.asterix.om.functions.IFunctionDescriptor;
 import edu.uci.ics.asterix.om.functions.IFunctionDescriptorFactory;
+import edu.uci.ics.asterix.om.pointables.PointableAllocator;
+import edu.uci.ics.asterix.om.pointables.base.IVisitablePointable;
+import edu.uci.ics.asterix.om.pointables.cast.ACastVisitor;
 import edu.uci.ics.asterix.om.types.ARecordType;
 import edu.uci.ics.asterix.om.types.IAType;
 import edu.uci.ics.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
-import edu.uci.ics.asterix.runtime.pointables.PointableAllocator;
-import edu.uci.ics.asterix.runtime.pointables.base.IVisitablePointable;
-import edu.uci.ics.asterix.runtime.pointables.cast.ACastVisitor;
 import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
 import edu.uci.ics.hyracks.algebricks.common.utils.Triple;
 import edu.uci.ics.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;