Fix for issue 715 - changed null value serialization policy in lists
Fix for issue 760/756 - changed nullable fields type inference in listify
Fix for issue 761 - changed type inference for records with multiple lists of non-primitive types

Change-Id: I700f0af27d8b9c2d8bf0c0a2906d2ffe4d0ee900
Reviewed-on: http://fulliautomatix.ics.uci.edu:8443/156
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Keren-Audrey Ouaknine <kereno@gmail.com>
diff --git a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/TypeTranslator.java b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/TypeTranslator.java
index 5abbd91..33ae101 100644
--- a/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/TypeTranslator.java
+++ b/asterix-algebra/src/main/java/edu/uci/ics/asterix/translator/TypeTranslator.java
@@ -266,7 +266,7 @@
             String defaultDataverse) {
         String typeName = tre.getIdent().getValue();
         TypeSignature typeSignature = new TypeSignature(defaultDataverse, typeName);
-        List<AbstractCollectionType> typeList = incompleteItemTypes.get(typeName);
+        List<AbstractCollectionType> typeList = incompleteItemTypes.get(typeSignature);
         if (typeList == null) {
             typeList = new LinkedList<AbstractCollectionType>();
             incompleteItemTypes.put(typeSignature, typeList);
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/builders/AbstractListBuilder.java b/asterix-om/src/main/java/edu/uci/ics/asterix/builders/AbstractListBuilder.java
index 21b032a..c067507 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/builders/AbstractListBuilder.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/builders/AbstractListBuilder.java
@@ -76,7 +76,7 @@
     @Override
     public void addItem(IValueReference item) throws HyracksDataException {
         try {
-            if (!fixedSize)
+            if (!fixedSize && (item.getByteArray()[0] != serNullTypeTag || itemTypeTag == ATypeTag.ANY))
                 this.offsets.add((short) outputStorage.getLength());
             if (itemTypeTag == ATypeTag.ANY
                     || (itemTypeTag == ATypeTag.NULL && item.getByteArray()[0] == serNullTypeTag)) {
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/formats/NonTaggedDataFormat.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/formats/NonTaggedDataFormat.java
index 4704ae1..28ad34b 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/formats/NonTaggedDataFormat.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/formats/NonTaggedDataFormat.java
@@ -60,6 +60,7 @@
 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.aggregates.collections.ListifyAggregateDescriptor;
 import edu.uci.ics.asterix.runtime.aggregates.scalar.ScalarAvgAggregateDescriptor;
 import edu.uci.ics.asterix.runtime.aggregates.scalar.ScalarCountAggregateDescriptor;
@@ -788,9 +789,13 @@
                 ((ListifyAggregateDescriptor) fd).reset(new AOrderedListType(null, null));
             } else {
                 IAType itemType = (IAType) context.getType(f.getArguments().get(0).getValue());
-                // Convert UNION types into ANY.
                 if (itemType instanceof AUnionType) {
-                    itemType = BuiltinType.ANY;
+                    if (((AUnionType) itemType).isNullableType())
+                        itemType = ((AUnionType) itemType).getUnionList().get(
+                                NonTaggedFormatUtil.OPTIONAL_TYPE_INDEX_IN_UNION_LIST);
+                    else
+                        // Convert UNION types into ANY.
+                        itemType = BuiltinType.ANY;
                 }
                 ((ListifyAggregateDescriptor) fd).reset(new AOrderedListType(itemType, null));
             }
@@ -934,7 +939,8 @@
     }
 
     @Override
-    public ITupleParserFactory createTupleParser(ARecordType recType, boolean delimitedFormat, char delimiter, char quote) {
+    public ITupleParserFactory createTupleParser(ARecordType recType, boolean delimitedFormat, char delimiter,
+            char quote) {
         if (delimitedFormat) {
             int n = recType.getFieldTypes().length;
             IValueParserFactory[] fieldParserFactories = new IValueParserFactory[n];
@@ -946,7 +952,8 @@
                 }
                 fieldParserFactories[i] = vpf;
             }
-            return new NtDelimitedDataTupleParserFactory(recType, fieldParserFactories, delimiter, quote, false, -1, null);
+            return new NtDelimitedDataTupleParserFactory(recType, fieldParserFactories, delimiter, quote, false, -1,
+                    null);
         } else {
             return new AdmSchemafullRecordParserFactory(recType, false, -1, null);
         }