checkpoint: added more aggregate test cases
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null/agg_null.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null/agg_null.1.ddl.aql
new file mode 100644
index 0000000..84bd013
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null/agg_null.1.ddl.aql
@@ -0,0 +1,5 @@
+/*
+* Description  : Run aggregates over both ordered list and unordered list with only null items.
+* Expected Res : Success
+* Date         : Jun 2nd 2013
+*/
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null/agg_null.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null/agg_null.2.update.aql
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null/agg_null.2.update.aql
@@ -0,0 +1 @@
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null/agg_null.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null/agg_null.3.query.aql
new file mode 100644
index 0000000..72e70b3
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null/agg_null.3.query.aql
@@ -0,0 +1,9 @@
+/*
+* Description  : Run aggregates over both ordered list and unordered list with only null items.
+* Expected Res : Success
+* Date         : Jun 2nd 2013
+*/
+
+let $l1 := [null]
+let $l2 := {{null, null}}
+return { "count1": count($l1), "average1": avg($l1), "sum1": sum($l1), "min1": min($l1), "max1": max($l1), "count2": count($l2), "average2": avg($l2), "sum2": sum($l2), "min2": min($l2), "max2": max($l2) } 
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null_rec/agg_null_rec.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null_rec/agg_null_rec.1.ddl.aql
new file mode 100644
index 0000000..da8a4a2
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null_rec/agg_null_rec.1.ddl.aql
@@ -0,0 +1,16 @@
+/*
+* Description  : Run aggregates over records, with only null items for the aggregating fields.
+* Expected Res : Success
+* Date         : Jun 2nd 2013
+*/
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type TestType as open {
+  id: int32,
+  val: double
+}
+
+create dataset Test(TestType) primary key id;
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null_rec/agg_null_rec.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null_rec/agg_null_rec.2.update.aql
new file mode 100644
index 0000000..fbe9098
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null_rec/agg_null_rec.2.update.aql
@@ -0,0 +1,11 @@
+/*
+* Description  : Run aggregates over records, with only null items for the aggregating fields.
+* Expected Res : Success
+* Date         : Jun 2nd 2013
+*/
+
+use dataverse test;
+
+insert into dataset Test ({"id": 0, "val": 4.32, "valplus": 2});
+insert into dataset Test ({"id": 1, "val": 5.32});
+insert into dataset Test ({"id": 2, "val": 6.32, "valplus": 38473827484738239});
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null_rec/agg_null_rec.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null_rec/agg_null_rec.3.query.aql
new file mode 100644
index 0000000..bdab44e
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_null_rec/agg_null_rec.3.query.aql
@@ -0,0 +1,10 @@
+/*
+* Description  : Run aggregates over records, with only null items for the aggregating fields.
+* Expected Res : Success
+* Date         : Jun 2nd 2013
+*/
+
+use dataverse test;
+
+let $l := for $t in dataset Test return $t.valplus
+return { "count": count($l), "average": avg($l), "sum": sum($l), "min": min($l), "max": max($l) }  
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number/agg_number.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number/agg_number.1.ddl.aql
new file mode 100644
index 0000000..343be89
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number/agg_number.1.ddl.aql
@@ -0,0 +1,6 @@
+/*
+* Description  : Run aggregates over an ordered list with numbers of different types
+* Expected Res : Success
+* Date         : Jun 2nd 2013
+*/
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number/agg_number.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number/agg_number.2.update.aql
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number/agg_number.2.update.aql
@@ -0,0 +1 @@
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number/agg_number.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number/agg_number.3.query.aql
new file mode 100644
index 0000000..3e9c1ae
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number/agg_number.3.query.aql
@@ -0,0 +1,15 @@
+/*
+* Description  : Run aggregates over an ordered list with numbers of different types
+* Expected Res : Success
+* Date         : Jun 2nd 2013
+*/
+
+let $l1 := [float("2.0"), double("3.0"), 93847382783847382, 1]
+let $l2 := {{float("2.0"), double("3.0"), 93847382783847382, 1}}
+let $a1 := count($l2)
+let $a2 := avg($l2)
+let $a3 := sum($l2)
+let $a4 := min($l2)
+let $a5 := max($l2)
+return { "count1": count($l1), "average1": avg($l1), "sum1": sum($l1), "min1": min($l1), "max1": max($l1), "count2": $a1, "average2": $a2, "sum2": $a3, "min2": $a4, "max2": $a5 }
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number_rec/agg_number_rec.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number_rec/agg_number_rec.1.ddl.aql
new file mode 100644
index 0000000..3b6f853a
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number_rec/agg_number_rec.1.ddl.aql
@@ -0,0 +1,16 @@
+/*
+* Description  : Run aggregates over records, with different numeric typed items for the aggregating fields.
+* Expected Res : Success
+* Date         : Jun 2nd 2013
+*/
+
+drop dataverse test if exists;
+create dataverse test;
+use dataverse test;
+
+create type TestType as open {
+  id: int32,
+  val: double
+}
+
+create dataset Test(TestType) primary key id;
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number_rec/agg_number_rec.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number_rec/agg_number_rec.2.update.aql
new file mode 100644
index 0000000..86aeb85
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number_rec/agg_number_rec.2.update.aql
@@ -0,0 +1,11 @@
+/*
+* Description  : Run aggregates over records, with different numeric typed items for the aggregating fields.
+* Expected Res : Success
+* Date         : Jun 2nd 2013
+*/
+
+use dataverse test;
+
+insert into dataset Test ({"id": 0, "val": 4.32, "valplus": 2});
+insert into dataset Test ({"id": 1, "val": 5.32, "valplus": 32.98});
+insert into dataset Test ({"id": 2, "val": 6.32, "valplus": 38473827484738239});
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number_rec/agg_number_rec.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number_rec/agg_number_rec.3.query.aql
new file mode 100644
index 0000000..256be92
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/agg_number_rec/agg_number_rec.3.query.aql
@@ -0,0 +1,10 @@
+/*
+* Description  : Run aggregates over records, with different numeric typed items for the aggregating fields.
+* Expected Res : Success
+* Date         : Jun 2nd 2013
+*/
+
+use dataverse test;
+
+let $l := for $t in dataset Test return $t.valplus
+return { "count": count($l), "average": avg($l), "sum": sum($l), "min": min($l), "max": max($l) }  
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/avg_mixed/avg_mixed.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/avg_mixed/avg_mixed.1.ddl.aql
new file mode 100644
index 0000000..9270281
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/avg_mixed/avg_mixed.1.ddl.aql
@@ -0,0 +1,5 @@
+/*
+* Description  : Run avg over an ordered list with mixed types
+* Expected Res : Failure
+* Date         : Jun 2nd 2013
+*/
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/avg_mixed/avg_mixed.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/avg_mixed/avg_mixed.2.update.aql
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/avg_mixed/avg_mixed.2.update.aql
@@ -0,0 +1 @@
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/avg_mixed/avg_mixed.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/avg_mixed/avg_mixed.3.query.aql
new file mode 100644
index 0000000..05d42c2
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/avg_mixed/avg_mixed.3.query.aql
@@ -0,0 +1,11 @@
+/*
+* Description  : Run avg over an ordered list with mixed types
+* Expected Res : Failure
+* Date         : Jun 2nd 2013
+*/
+
+avg(
+ for $x in [float("2.0"), "hello world", 93847382783847382, date("2013-01-01")]
+ return $x
+)
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/min_mixed/min_mixed.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/min_mixed/min_mixed.1.ddl.aql
new file mode 100644
index 0000000..b6ea924
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/min_mixed/min_mixed.1.ddl.aql
@@ -0,0 +1,5 @@
+/*
+* Description  : Run min over an ordered list with mixed types
+* Expected Res : Failure
+* Date         : Jun 2nd 2013
+*/
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/min_mixed/min_mixed.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/min_mixed/min_mixed.2.update.aql
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/min_mixed/min_mixed.2.update.aql
@@ -0,0 +1 @@
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/min_mixed/min_mixed.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/min_mixed/min_mixed.3.query.aql
new file mode 100644
index 0000000..f66d3c2
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/min_mixed/min_mixed.3.query.aql
@@ -0,0 +1,11 @@
+/*
+* Description  : Run min over an ordered list with mixed types
+* Expected Res : Failure
+* Date         : Jun 2nd 2013
+*/
+
+min(
+ for $x in [float("2.0"), "hello world", 93847382783847382, date("2013-01-01")]
+ return $x
+)
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_mixed/sum_mixed.1.ddl.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_mixed/sum_mixed.1.ddl.aql
new file mode 100644
index 0000000..8e691a3
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_mixed/sum_mixed.1.ddl.aql
@@ -0,0 +1,5 @@
+/*
+* Description  : Run sum over an ordered list with mixed types
+* Expected Res : Failure
+* Date         : Jun 2nd 2013
+*/
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_mixed/sum_mixed.2.update.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_mixed/sum_mixed.2.update.aql
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_mixed/sum_mixed.2.update.aql
@@ -0,0 +1 @@
+
diff --git a/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_mixed/sum_mixed.3.query.aql b/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_mixed/sum_mixed.3.query.aql
new file mode 100644
index 0000000..7b4e530
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/queries/aggregate/sum_mixed/sum_mixed.3.query.aql
@@ -0,0 +1,11 @@
+/*
+* Description  : Run sum over an ordered list with mixed types
+* Expected Res : Failure
+* Date         : Jun 2nd 2013
+*/
+
+sum(
+ for $x in [float("2.0"), "hello world", 93847382783847382, date("2013-01-01")]
+ return $x
+)
+
diff --git a/asterix-app/src/test/resources/runtimets/results/aggregate/agg_null/agg_null.1.adm b/asterix-app/src/test/resources/runtimets/results/aggregate/agg_null/agg_null.1.adm
new file mode 100644
index 0000000..9150c3f
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/aggregate/agg_null/agg_null.1.adm
@@ -0,0 +1 @@
+{ "count1": 1, "average1": null, "sum1": null, "min1": null, "max1": null, "count2": 2, "average2": null, "sum2": null, "min2": null, "max2": null }
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/aggregate/agg_null_rec/agg_null_rec.1.adm b/asterix-app/src/test/resources/runtimets/results/aggregate/agg_null_rec/agg_null_rec.1.adm
new file mode 100644
index 0000000..77d99fc
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/aggregate/agg_null_rec/agg_null_rec.1.adm
@@ -0,0 +1 @@
+{ "count": 1, "average": null, "sum": null, "min": null, "max": null }
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/aggregate/agg_number/agg_number.1.adm b/asterix-app/src/test/resources/runtimets/results/aggregate/agg_number/agg_number.1.adm
new file mode 100644
index 0000000..48ba7ee
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/aggregate/agg_number/agg_number.1.adm
@@ -0,0 +1 @@
+{ "count1": 4, "count2": 4, "average1": 2.3461845695961844E16d, "sum1": 9.3847382783847376E16, "min1": 1.0d, "max1": 9.3847382783847376E16, "average2": 2.3461845695961844E16d, "sum2": 9.3847382783847376E16, "min2": 1.0d, "max2": 9.3847382783847376E16 }
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/aggregate/agg_number_rec/agg_number_rec.1.adm b/asterix-app/src/test/resources/runtimets/results/aggregate/agg_number_rec/agg_number_rec.1.adm
new file mode 100644
index 0000000..77d99fc
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/aggregate/agg_number_rec/agg_number_rec.1.adm
@@ -0,0 +1 @@
+{ "count": 1, "average": null, "sum": null, "min": null, "max": null }
\ No newline at end of file
diff --git a/asterix-app/src/test/resources/runtimets/results/aggregate/avg_mixed/avg_mixed.1.adm b/asterix-app/src/test/resources/runtimets/results/aggregate/avg_mixed/avg_mixed.1.adm
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/aggregate/avg_mixed/avg_mixed.1.adm
diff --git a/asterix-app/src/test/resources/runtimets/results/aggregate/min_mixed/min_mixed.1.adm b/asterix-app/src/test/resources/runtimets/results/aggregate/min_mixed/min_mixed.1.adm
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/aggregate/min_mixed/min_mixed.1.adm
diff --git a/asterix-app/src/test/resources/runtimets/results/aggregate/sum_mixed/sum_mixed.1.adm b/asterix-app/src/test/resources/runtimets/results/aggregate/sum_mixed/sum_mixed.1.adm
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/asterix-app/src/test/resources/runtimets/results/aggregate/sum_mixed/sum_mixed.1.adm
diff --git a/asterix-app/src/test/resources/runtimets/testsuite.xml b/asterix-app/src/test/resources/runtimets/testsuite.xml
index 1b9946e..a8cdf5e 100644
--- a/asterix-app/src/test/resources/runtimets/testsuite.xml
+++ b/asterix-app/src/test/resources/runtimets/testsuite.xml
@@ -1,6 +1,39 @@
 <test-suite xmlns="urn:xml.testframework.asterix.ics.uci.edu" ResultOffsetPath="results" QueryOffsetPath="queries" QueryFileExtension=".aql">
   <test-group name="aggregate">
     <test-case FilePath="aggregate">
+      <compilation-unit name="agg_null">
+        <output-dir compare="Text">agg_null</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate">
+      <compilation-unit name="agg_null_rec">
+        <output-dir compare="Text">agg_null_rec</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate">
+      <compilation-unit name="avg_mixed">
+        <output-dir compare="Text">avg_mixed</output-dir>
+        <expected-error>edu.uci.ics.asterix.common.exceptions.AsterixException</expected-error>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate">
+      <compilation-unit name="sum_mixed">
+        <output-dir compare="Text">sum_mixed</output-dir>
+        <expected-error>edu.uci.ics.asterix.common.exceptions.AsterixException</expected-error>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate">
+      <compilation-unit name="min_mixed">
+        <output-dir compare="Text">min_mixed</output-dir>
+        <expected-error>edu.uci.ics.asterix.common.exceptions.AsterixException</expected-error>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate">
+      <compilation-unit name="agg_number">
+        <output-dir compare="Text">agg_number</output-dir>
+      </compilation-unit>
+    </test-case>
+    <test-case FilePath="aggregate">
       <compilation-unit name="issue425_min_hetero_list_1">
         <output-dir compare="Text">issue425_min_hetero_list_1</output-dir>
       </compilation-unit>
diff --git a/asterix-om/src/main/java/edu/uci/ics/asterix/om/types/hierachy/IntegerToDoubleTypePromoteComputer.java b/asterix-om/src/main/java/edu/uci/ics/asterix/om/types/hierachy/IntegerToDoubleTypePromoteComputer.java
index 3c1f2be..a4b231c 100644
--- a/asterix-om/src/main/java/edu/uci/ics/asterix/om/types/hierachy/IntegerToDoubleTypePromoteComputer.java
+++ b/asterix-om/src/main/java/edu/uci/ics/asterix/om/types/hierachy/IntegerToDoubleTypePromoteComputer.java
@@ -32,11 +32,11 @@
     public void promote(byte[] data, int start, int length, IMutableValueStorage storageForPromotedValue)
             throws IOException {
         storageForPromotedValue.getDataOutput().writeByte(ATypeTag.DOUBLE.serialize());
-        double val = 0.0;
+        Long val = 0L;
         for (int i = 0; i < length; i++) {
-            val += (data[start + i] & 0xff) << (8 * (length - 1 - i));
+            val += ((long)(data[start + i] & 0xff)) << (8 * (length - 1 - i));
         }
-        DoubleSerializerDeserializer.INSTANCE.serialize(val, storageForPromotedValue.getDataOutput());
+        DoubleSerializerDeserializer.INSTANCE.serialize(val.doubleValue(), storageForPromotedValue.getDataOutput());
     }
 
 }
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/AvgAggregateDescriptor.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/AvgAggregateDescriptor.java
index ea5dcd0..7815606 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/AvgAggregateDescriptor.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/AvgAggregateDescriptor.java
@@ -150,7 +150,10 @@
                         } else if (typeTag != ATypeTag.SYSTEM_NULL && !ATypeHierarchy.isCompatible(typeTag, aggType)) {
                             throw new AlgebricksException("Unexpected type " + typeTag
                                     + " in aggregation input stream. Expected type " + aggType + ".");
+                        } else if (ATypeHierarchy.canPromote(aggType, typeTag)) {
+                            aggType = typeTag;
                         }
+
                         if (typeTag != ATypeTag.SYSTEM_NULL) {
                             ++count;
                         }
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/LocalAvgAggregateDescriptor.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/LocalAvgAggregateDescriptor.java
index 224310f..5ba9c06 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/LocalAvgAggregateDescriptor.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/LocalAvgAggregateDescriptor.java
@@ -149,10 +149,14 @@
                         } else if (typeTag != ATypeTag.SYSTEM_NULL && !ATypeHierarchy.isCompatible(typeTag, aggType)) {
                             throw new AlgebricksException("Unexpected type " + typeTag
                                     + " in aggregation input stream. Expected type " + aggType + ".");
+                        } else if (ATypeHierarchy.canPromote(aggType, typeTag)) {
+                            aggType = typeTag;
                         }
+                        
                         if (typeTag != ATypeTag.SYSTEM_NULL) {
                             ++count;
                         }
+
                         switch (typeTag) {
                             case INT8: {
                                 byte val = AInt8SerializerDeserializer.getByte(inputVal.getByteArray(), 1);
diff --git a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/MinMaxAggregateFunction.java b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/MinMaxAggregateFunction.java
index bbc437b..59004d8 100644
--- a/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/MinMaxAggregateFunction.java
+++ b/asterix-runtime/src/main/java/edu/uci/ics/asterix/runtime/aggregates/std/MinMaxAggregateFunction.java
@@ -105,7 +105,7 @@
                     if (cmp.compare(tempValForCasting.getByteArray(), tempValForCasting.getStartOffset(),
                             tempValForCasting.getLength(), outputVal.getByteArray(), outputVal.getStartOffset(),
                             outputVal.getLength()) < 0) {
-                        outputVal.assign(inputVal);
+                        outputVal.assign(tempValForCasting);
                     }
                 } else {
                     if (cmp.compare(inputVal.getByteArray(), inputVal.getStartOffset(), inputVal.getLength(),