[ASTERIXDB-2690][FUN] Agg Sum: skip and warn for invalid values

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
- Currently, the aggregate sum function throws an exception
  when an invalid (non-numeric but not null or missing) value
  is encountered, this patch changes the behavior to skip
  the invalid value (treat it as null) and issue a warning.

Change-Id: Ia7cde094302331d8f37670b4e6c6ce894d549154
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/4603
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: Hussain Towaileb <hussainht@gmail.com>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/serial_sum_mixed/serial_sum_mixed.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/serial_sum_mixed/serial_sum_mixed.3.query.sqlpp
index 36d1701..47613b7 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/serial_sum_mixed/serial_sum_mixed.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/serial_sum_mixed/serial_sum_mixed.3.query.sqlpp
@@ -22,6 +22,8 @@
  * Date         : March 5th 2018
  */
 
+-- param max-warnings:json=1000
+
 use test;
 
 select gid, array_sum((select value g.valplus from g)) as sum
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/sum_mixed/sum_mixed.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/sum_mixed/sum_mixed.3.query.sqlpp
index fed4cc9..1be93de 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/sum_mixed/sum_mixed.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate-sql/sum_mixed/sum_mixed.3.query.sqlpp
@@ -22,6 +22,8 @@
 * Date         : Feb 7th 2014
 */
 
+-- param max-warnings:json=1000
+
 select element array_sum((
     select element x
     from  [float('2.0'),'hello world',93847382783847382,date('2013-01-01')] as x
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/serial_sum_mixed/serial_sum_mixed.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/serial_sum_mixed/serial_sum_mixed.3.query.sqlpp
index 96551e2..a1e0951 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/serial_sum_mixed/serial_sum_mixed.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/serial_sum_mixed/serial_sum_mixed.3.query.sqlpp
@@ -22,6 +22,8 @@
  * Date         : March 5th 2018
  */
 
+-- param max-warnings:json=1000
+
 use test;
 
 select gid, strict_sum((select value g.valplus from g)) as sum
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/sum_mixed/sum_mixed.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/sum_mixed/sum_mixed.3.query.sqlpp
index 3c20a2c..ff48896 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/sum_mixed/sum_mixed.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/aggregate/sum_mixed/sum_mixed.3.query.sqlpp
@@ -22,6 +22,8 @@
 * Date         : Jun 2nd 2013
 */
 
+-- param max-warnings:json=1000
+
 select element strict_sum((
     select element x
     from  [float('2.0'),'hello world',93847382783847382,date('2013-01-01')] as x
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.3.query.sqlpp
index 9a03c50..b20900a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.3.query.sqlpp
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/group-by/policy-05/policy-05.3.query.sqlpp
@@ -17,6 +17,8 @@
  * under the License.
  */
 
+-- param max-warnings:json=1000
+
 USE gby;
 
 FROM policies p
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/serial_sum_mixed/serial_sum_mixed.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/serial_sum_mixed/serial_sum_mixed.1.adm
index eb5581f..ad13028 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/serial_sum_mixed/serial_sum_mixed.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/serial_sum_mixed/serial_sum_mixed.1.adm
@@ -1 +1 @@
-{ "gid": 1, "sum": null }
+{ "gid": 1, "sum": 5.0 }
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/sum_mixed/sum_mixed.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/sum_mixed/sum_mixed.1.adm
index e3b97f5..799c1ac 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/sum_mixed/sum_mixed.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate-sql/sum_mixed/sum_mixed.1.adm
@@ -1 +1 @@
-[  ]
+9.3847382783847376E16
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/sum_mixed/sum_mixed.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/sum_mixed/sum_mixed.1.adm
index e3b97f5..19765bd 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/sum_mixed/sum_mixed.1.adm
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/aggregate/sum_mixed/sum_mixed.1.adm
@@ -1 +1 @@
-[  ]
+null
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/policy-05/policy-05.1.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/policy-05/policy-05.1.adm
new file mode 100644
index 0000000..2448312
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/group-by/policy-05/policy-05.1.adm
@@ -0,0 +1,3 @@
+{ "state": "AZ", "risk": 0.0 }
+{ "state": "CA", "risk": 0.0 }
+{ "state": "UT", "risk": 0.0 }
\ No newline at end of file
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
index cc5d18d..206474a 100644
--- a/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/testsuite_sqlpp.xml
@@ -338,16 +338,16 @@
         <expected-error>Type incompatibility: function agg-stddev_pop gets incompatible input values: string and float</expected-error>
       </compilation-unit>
     </test-case>
-    <test-case FilePath="aggregate">
+    <test-case FilePath="aggregate" check-warnings="true">
       <compilation-unit name="sum_mixed">
         <output-dir compare="Text">sum_mixed</output-dir>
-        <expected-error>Invalid item type: function agg-sum cannot process item type string in an input array</expected-error>
+        <expected-warn>Unsupported type: agg-sum cannot process input type string (in line 27, at column 16)</expected-warn>
       </compilation-unit>
     </test-case>
-    <test-case FilePath="aggregate">
+    <test-case FilePath="aggregate" check-warnings="true">
       <compilation-unit name="serial_sum_mixed">
         <output-dir compare="Text">serial_sum_mixed</output-dir>
-        <expected-error>Invalid item type: function agg-sum cannot process item type string in an input array</expected-error>
+        <expected-warn>Unsupported type: agg-sum cannot process input type string (in line 29, at column 39)</expected-warn>
       </compilation-unit>
     </test-case>
     <test-case FilePath="aggregate">
@@ -1532,16 +1532,16 @@
         <expected-error>Type incompatibility: function agg-stddev_pop gets incompatible input values: string and float</expected-error>
       </compilation-unit>
     </test-case>
-    <test-case FilePath="aggregate-sql">
+    <test-case FilePath="aggregate-sql" check-warnings="true">
       <compilation-unit name="sum_mixed">
         <output-dir compare="Text">sum_mixed</output-dir>
-        <expected-error>Invalid item type: function agg-sum cannot process item type string in an input array</expected-error>
+        <expected-warn>Unsupported type: agg-sum cannot process input type string (in line 27, at column 16)</expected-warn>
       </compilation-unit>
     </test-case>
-    <test-case FilePath="aggregate-sql">
+    <test-case FilePath="aggregate-sql" check-warnings="true">
       <compilation-unit name="serial_sum_mixed">
         <output-dir compare="Text">serial_sum_mixed</output-dir>
-        <expected-error>Invalid item type: function agg-sum cannot process item type string in an input array</expected-error>
+        <expected-warn>Unsupported type: agg-sum cannot process input type string (in line 29, at column 38)</expected-warn>
       </compilation-unit>
     </test-case>
     <test-case FilePath="aggregate-sql">
@@ -5233,10 +5233,10 @@
         <output-dir compare="Text">policy-04</output-dir>
       </compilation-unit>
     </test-case>
-    <test-case FilePath="group-by">
+    <test-case FilePath="group-by" check-warnings="true">
       <compilation-unit name="policy-05">
-        <output-dir compare="Text">none</output-dir>
-        <expected-error>Invalid item type: function agg-sum cannot process item type object in an input array (or multiset)</expected-error>
+        <output-dir compare="Text">policy-05</output-dir>
+        <expected-warn>Unsupported type: agg-sum cannot process input type object (in line 29, at column 23)</expected-warn>
       </compilation-unit>
     </test-case>
     <test-case FilePath="group-by">
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/serializable/std/AbstractSerializableSumAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/serializable/std/AbstractSerializableSumAggregateFunction.java
index cb174ee..bedfeb0 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/serializable/std/AbstractSerializableSumAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/serializable/std/AbstractSerializableSumAggregateFunction.java
@@ -30,12 +30,12 @@
 import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider;
 import org.apache.asterix.om.base.AMutableDouble;
 import org.apache.asterix.om.base.AMutableInt64;
+import org.apache.asterix.om.exceptions.ExceptionUtil;
 import org.apache.asterix.om.functions.BuiltinFunctions;
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.BuiltinType;
 import org.apache.asterix.om.types.EnumDeserializer;
 import org.apache.asterix.runtime.exceptions.OverflowException;
-import org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException;
 import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
 import org.apache.hyracks.algebricks.runtime.base.IEvaluatorContext;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator;
@@ -49,6 +49,11 @@
 
 public abstract class AbstractSerializableSumAggregateFunction extends AbstractSerializableAggregateFunction {
 
+    private final IEvaluatorContext context;
+
+    // Warning flag to warn only once in case of non-numeric data
+    private boolean isWarned = false;
+
     // Handles evaluating and storing/passing serialized data
     protected static final int AGG_TYPE_OFFSET = 0;
     private static final int SUM_OFFSET = 1;
@@ -77,6 +82,7 @@
     public AbstractSerializableSumAggregateFunction(IScalarEvaluatorFactory[] args, IEvaluatorContext context,
             SourceLocation sourceLoc) throws HyracksDataException {
         super(sourceLoc);
+        this.context = context;
         eval = args[0].createScalarEvaluator(context);
     }
 
@@ -97,6 +103,7 @@
         try {
             state.writeByte(ATypeTag.SERIALIZED_SYSTEM_NULL_TYPE_TAG);
             state.writeLong(0);
+            isWarned = false;
         } catch (IOException e) {
             throw HyracksDataException.create(e);
         }
@@ -114,8 +121,6 @@
         eval.evaluate(tuple, inputVal);
         byte[] bytes = inputVal.getByteArray();
         int offset = inputVal.getStartOffset();
-
-        // Get the data type tag
         ATypeTag typeTag = EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(bytes[offset]);
 
         // Handle MISSING and NULL values
@@ -162,7 +167,12 @@
                 processSystemNull();
                 break;
             default:
-                throw new UnsupportedItemTypeException(sourceLoc, BuiltinFunctions.SUM, bytes[offset]);
+                // Issue warning only once and treat current tuple as null
+                if (!isWarned) {
+                    isWarned = true;
+                    ExceptionUtil.warnUnsupportedType(context, sourceLoc, getIdentifier().getName(), typeTag);
+                }
+                processNull(state, start);
         }
     }
 
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/AbstractSumAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/AbstractSumAggregateFunction.java
index 7e78aad..c560255 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/AbstractSumAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/AbstractSumAggregateFunction.java
@@ -29,12 +29,12 @@
 import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider;
 import org.apache.asterix.om.base.AMutableDouble;
 import org.apache.asterix.om.base.AMutableInt64;
+import org.apache.asterix.om.exceptions.ExceptionUtil;
 import org.apache.asterix.om.functions.BuiltinFunctions;
 import org.apache.asterix.om.types.ATypeTag;
 import org.apache.asterix.om.types.BuiltinType;
 import org.apache.asterix.om.types.EnumDeserializer;
 import org.apache.asterix.runtime.exceptions.OverflowException;
-import org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException;
 import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
 import org.apache.hyracks.algebricks.runtime.base.IEvaluatorContext;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluator;
@@ -49,6 +49,11 @@
 
 public abstract class AbstractSumAggregateFunction extends AbstractAggregateFunction {
 
+    private final IEvaluatorContext context;
+
+    // Warning flag to warn only once in case of non-numeric data
+    private boolean isWarned = false;
+
     // Handles evaluating and storing/passing serialized data
     protected ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
     private IPointable inputVal = new VoidPointable();
@@ -78,6 +83,7 @@
     public AbstractSumAggregateFunction(IScalarEvaluatorFactory[] args, IEvaluatorContext context,
             SourceLocation sourceLoc) throws HyracksDataException {
         super(sourceLoc);
+        this.context = context;
         eval = args[0].createScalarEvaluator(context);
     }
 
@@ -98,6 +104,7 @@
         aggType = ATypeTag.SYSTEM_NULL;
         sumInt64 = 0;
         sumDouble = 0.0;
+        isWarned = false;
     }
 
     // Called for each incoming tuple
@@ -165,7 +172,12 @@
                 break;
             }
             default: {
-                throw new UnsupportedItemTypeException(sourceLoc, BuiltinFunctions.SUM, typeTag.serialize());
+                // Issue warning only once and treat current tuple as null
+                if (!isWarned) {
+                    isWarned = true;
+                    ExceptionUtil.warnUnsupportedType(context, sourceLoc, getIdentifier().getName(), typeTag);
+                }
+                processNull();
             }
         }
     }
@@ -236,12 +248,11 @@
                 if (isUseInt64ForResult) {
                     aInt64.setValue(sumInt64);
                     aInt64Serde.serialize(aInt64, resultStorage.getDataOutput());
-                    result.set(resultStorage);
                 } else {
                     aDouble.setValue(sumDouble);
                     aDoubleSerde.serialize(aDouble, resultStorage.getDataOutput());
-                    result.set(resultStorage);
                 }
+                result.set(resultStorage);
             }
         } catch (IOException ex) {
             throw HyracksDataException.create(ex);
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/GlobalSqlSumAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/GlobalSqlSumAggregateFunction.java
index c854519..11f0a21 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/GlobalSqlSumAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/GlobalSqlSumAggregateFunction.java
@@ -18,9 +18,7 @@
  */
 package org.apache.asterix.runtime.aggregates.std;
 
-import java.io.IOException;
-
-import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.runtime.evaluators.functions.PointableHelper;
 import org.apache.hyracks.algebricks.runtime.base.IEvaluatorContext;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
@@ -67,15 +65,13 @@
 
     // Handle NULL finish
     @Override
-    protected void finishNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 
     // Handle SYSTEM_NULL finish
     @Override
-    protected void finishSystemNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishSystemNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 }
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/GlobalSumAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/GlobalSumAggregateFunction.java
index 79e264b..1437a10 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/GlobalSumAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/GlobalSumAggregateFunction.java
@@ -18,9 +18,8 @@
  */
 package org.apache.asterix.runtime.aggregates.std;
 
-import java.io.IOException;
-
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.runtime.evaluators.functions.PointableHelper;
 import org.apache.hyracks.algebricks.runtime.base.IEvaluatorContext;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
@@ -67,15 +66,13 @@
 
     // Handle NULL finish
     @Override
-    protected void finishNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 
     // Handle SYSTEM_NULL finish
     @Override
-    protected void finishSystemNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishSystemNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 }
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/IntermediateSqlSumAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/IntermediateSqlSumAggregateFunction.java
index 94d934e..838a763 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/IntermediateSqlSumAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/IntermediateSqlSumAggregateFunction.java
@@ -21,6 +21,7 @@
 import java.io.IOException;
 
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.runtime.evaluators.functions.PointableHelper;
 import org.apache.hyracks.algebricks.runtime.base.IEvaluatorContext;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
@@ -67,9 +68,8 @@
 
     // Handle NULL finish
     @Override
-    protected void finishNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 
     // Handle SYSTEM_NULL finish
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/IntermediateSumAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/IntermediateSumAggregateFunction.java
index c280b33..9579cb3 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/IntermediateSumAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/IntermediateSumAggregateFunction.java
@@ -21,6 +21,7 @@
 import java.io.IOException;
 
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.runtime.evaluators.functions.PointableHelper;
 import org.apache.hyracks.algebricks.runtime.base.IEvaluatorContext;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
@@ -67,9 +68,8 @@
 
     // Handle NULL finish
     @Override
-    protected void finishNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 
     // Handle SYSTEM_NULL finish
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/LocalSqlSumAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/LocalSqlSumAggregateFunction.java
index eddf7cf..4cc3df4 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/LocalSqlSumAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/LocalSqlSumAggregateFunction.java
@@ -22,6 +22,7 @@
 
 import org.apache.asterix.om.functions.BuiltinFunctions;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.runtime.evaluators.functions.PointableHelper;
 import org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException;
 import org.apache.hyracks.algebricks.runtime.base.IEvaluatorContext;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
@@ -70,9 +71,8 @@
 
     // Handle NULL finish
     @Override
-    protected void finishNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 
     // Handle SYSTEM_NULL finish
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/LocalSumAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/LocalSumAggregateFunction.java
index f966742..09df733 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/LocalSumAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/LocalSumAggregateFunction.java
@@ -22,6 +22,7 @@
 
 import org.apache.asterix.om.functions.BuiltinFunctions;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.runtime.evaluators.functions.PointableHelper;
 import org.apache.asterix.runtime.exceptions.UnsupportedItemTypeException;
 import org.apache.hyracks.algebricks.runtime.base.IEvaluatorContext;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
@@ -70,9 +71,8 @@
 
     // Handle NULL finish
     @Override
-    protected void finishNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 
     // Handle SYSTEM_NULL finish
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/SqlSumAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/SqlSumAggregateFunction.java
index 02f5ac3..d0d4748 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/SqlSumAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/SqlSumAggregateFunction.java
@@ -18,9 +18,7 @@
  */
 package org.apache.asterix.runtime.aggregates.std;
 
-import java.io.IOException;
-
-import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.runtime.evaluators.functions.PointableHelper;
 import org.apache.hyracks.algebricks.runtime.base.IEvaluatorContext;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
@@ -67,15 +65,13 @@
 
     // Handle NULL finish
     @Override
-    protected void finishNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 
     // Handle SYSTEM_NULL finish
     @Override
-    protected void finishSystemNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishSystemNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 }
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/SumAggregateFunction.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/SumAggregateFunction.java
index 8833952..ba07d2a 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/SumAggregateFunction.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/aggregates/std/SumAggregateFunction.java
@@ -18,9 +18,8 @@
  */
 package org.apache.asterix.runtime.aggregates.std;
 
-import java.io.IOException;
-
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.runtime.evaluators.functions.PointableHelper;
 import org.apache.hyracks.algebricks.runtime.base.IEvaluatorContext;
 import org.apache.hyracks.algebricks.runtime.base.IScalarEvaluatorFactory;
 import org.apache.hyracks.api.exceptions.HyracksDataException;
@@ -67,15 +66,13 @@
 
     // Handle NULL finish
     @Override
-    protected void finishNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 
     // Handle SYSTEM_NULL finish
     @Override
-    protected void finishSystemNull(IPointable result) throws IOException {
-        resultStorage.getDataOutput().writeByte(ATypeTag.SERIALIZED_NULL_TYPE_TAG);
-        result.set(resultStorage);
+    protected void finishSystemNull(IPointable result) {
+        PointableHelper.setNull(result);
     }
 }