Repository: prestodb/presto
Issue: array_agg should not ignore nulls

```
presto> select array_agg(x) from (values 1,2,3,null) t(x);

   _col0   
-----------
 [1, 2, 3] 
```

The expected result is `[1, 2, 3, null]`.

From the spec:

> ii) Otherwise, the result of `<array aggregate function>` is an array of N elements such that for all i, 1 (one) ≤ i ≤ N, the value of the i-th element is the value of the  first column of Ri.
> 
>    NOTE 331 — **Null values are not eliminated when computing `<array aggregate function>`**. This, plus the optional `<sort specification list>`, sets `<array aggregate function>` apart from `<general set function>`s.


Changed files:
presto-main/src/main/java/com/facebook/presto/operator/aggregation/ArrayAggregationFunction.java
presto-main/src/main/java/com/facebook/presto/operator/aggregation/state/ArrayAggregationStateSerializer.java
presto-main/src/test/java/com/facebook/presto/operator/aggregation/TestArrayAggregation.java

diff --git a/presto-main/src/main/java/com/facebook/presto/operator/aggregation/ArrayAggregationFunction.java b/presto-main/src/main/java/com/facebook/presto/operator/aggregation/ArrayAggregationFunction.java
index 93540af632..b317a917e2 100644
--- a/presto-main/src/main/java/com/facebook/presto/operator/aggregation/ArrayAggregationFunction.java
+++ b/presto-main/src/main/java/com/facebook/presto/operator/aggregation/ArrayAggregationFunction.java
@@ -37,7 +37,7 @@ import java.util.List;
 
 import static com.facebook.presto.metadata.Signature.typeVariable;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.BLOCK_INDEX;
-import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.BLOCK_INPUT_CHANNEL;
+import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.NULLABLE_BLOCK_INPUT_CHANNEL;
 import static com.facebook.presto.operator.aggregation.AggregationMetadata.ParameterMetadata.ParameterType.STATE;
 import static com.facebook.presto.operator.aggregation.AggregationUtils.generateAggregationName;
 import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
@@ -111,7 +111,7 @@ public class ArrayAggregationFunction
 
     private static List<ParameterMetadata> createInputParameterMetadata(Type value)
     {
-        return ImmutableList.of(new ParameterMetadata(STATE), new ParameterMetadata(BLOCK_INPUT_CHANNEL, value), new ParameterMetadata(BLOCK_INDEX));
+        return ImmutableList.of(new ParameterMetadata(STATE), new ParameterMetadata(NULLABLE_BLOCK_INPUT_CHANNEL, value), new ParameterMetadata(BLOCK_INDEX));
     }
 
     public static void input(Type type, ArrayAggregationState state, Block value, int position)
diff --git a/presto-main/src/main/java/com/facebook/presto/operator/aggregation/state/ArrayAggregationStateSerializer.java b/presto-main/src/main/java/com/facebook/presto/operator/aggregation/state/ArrayAggregationStateSerializer.java
index 0f7aa2f876..f22ec14ec4 100644
--- a/presto-main/src/main/java/com/facebook/presto/operator/aggregation/state/ArrayAggregationStateSerializer.java
+++ b/presto-main/src/main/java/com/facebook/presto/operator/aggregation/state/ArrayAggregationStateSerializer.java
@@ -56,8 +56,7 @@ public class ArrayAggregationStateSerializer
         int positionCount = stateBlock.getPositionCount();
         BlockBuilder blockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), positionCount);
         for (int i = 0; i < positionCount; i++) {
-            stateBlock.writePositionTo(i, blockBuilder);
-            blockBuilder.closeEntry();
+            elementType.appendTo(stateBlock, i, blockBuilder);
         }
         state.setBlockBuilder(blockBuilder);
     }
diff --git a/presto-main/src/test/java/com/facebook/presto/operator/aggregation/TestArrayAggregation.java b/presto-main/src/test/java/com/facebook/presto/operator/aggregation/TestArrayAggregation.java
index b62d350320..e6503be30f 100644
--- a/presto-main/src/test/java/com/facebook/presto/operator/aggregation/TestArrayAggregation.java
+++ b/presto-main/src/test/java/com/facebook/presto/operator/aggregation/TestArrayAggregation.java
@@ -58,7 +58,7 @@ public class TestArrayAggregation
         assertAggregation(
                 bigIntAgg,
                 1.0,
-                null,
+                Arrays.asList(null, null, null),
                 createLongsBlock(new Long[] {null, null, null}));
     }
 
@@ -71,7 +71,7 @@ public class TestArrayAggregation
         assertAggregation(
                 bigIntAgg,
                 1.0,
-                Arrays.asList(2L, 3L),
+                Arrays.asList(null, 2L, null, 3L, null),
                 createLongsBlock(new Long[] {null, 2L, null, 3L, null}));
     }