Simplified the aggregate step functions with better init values.

Project: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/commit/c813b097
Tree: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/tree/c813b097
Diff: http://git-wip-us.apache.org/repos/asf/incubator-vxquery/diff/c813b097

Branch: refs/heads/master
Commit: c813b097f8179771a934a9aac0cce0824edf5986
Parents: 903576e
Author: Preston Carman <[email protected]>
Authored: Tue Apr 1 21:57:19 2014 -0700
Committer: Preston Carman <[email protected]>
Committed: Tue Apr 1 21:57:19 2014 -0700

----------------------------------------------------------------------
 .../AvgLocalAggregateEvaluatorFactory.java      | 23 ++++------
 .../FnSumAggregateEvaluatorFactory.java         | 45 ++++++--------------
 2 files changed, 21 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-vxquery/blob/c813b097/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/aggregate/AvgLocalAggregateEvaluatorFactory.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/aggregate/AvgLocalAggregateEvaluatorFactory.java
 
b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/aggregate/AvgLocalAggregateEvaluatorFactory.java
index 45414dc..6763a67 100644
--- 
a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/aggregate/AvgLocalAggregateEvaluatorFactory.java
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/aggregate/AvgLocalAggregateEvaluatorFactory.java
@@ -17,13 +17,11 @@
 package org.apache.vxquery.runtime.functions.aggregate;
 
 import java.io.DataOutput;
-import java.io.IOException;
 
 import org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
 import org.apache.vxquery.datamodel.builders.sequence.SequenceBuilder;
 import org.apache.vxquery.datamodel.values.ValueTag;
 import org.apache.vxquery.datamodel.values.XDMConstants;
-import org.apache.vxquery.exceptions.ErrorCode;
 import org.apache.vxquery.exceptions.SystemException;
 import org.apache.vxquery.runtime.functions.arithmetic.AddOperation;
 import 
org.apache.vxquery.runtime.functions.base.AbstractTaggedValueArgumentAggregateEvaluator;
@@ -62,6 +60,14 @@ public class AvgLocalAggregateEvaluatorFactory extends 
AbstractTaggedValueArgume
             @Override
             public void init() throws AlgebricksException {
                 count = 0;
+                try {
+                    abvsSum.reset();
+                    dOutSum.write(ValueTag.XS_INTEGER_TAG);
+                    dOutSum.writeLong(0);
+                    tvpSum.set(abvsSum);
+                } catch (Exception e) {
+                    throw new AlgebricksException(e);
+                }
             }
 
             @Override
@@ -92,18 +98,7 @@ public class AvgLocalAggregateEvaluatorFactory extends 
AbstractTaggedValueArgume
             @Override
             protected void step(TaggedValuePointable[] args) throws 
SystemException {
                 TaggedValuePointable tvp = args[0];
-                if (count == 0) {
-                    // Init.
-                    try {
-                        abvsSum.reset();
-                        dOutSum.write(tvp.getByteArray(), 
tvp.getStartOffset(), tvp.getLength());
-                        tvpSum.set(abvsSum);
-                    } catch (IOException e) {
-                        throw new SystemException(ErrorCode.SYSE0001, 
e.toString());
-                    }
-                } else {
-                    FunctionHelper.arithmeticOperation(aOp, dCtx, tvp, tvpSum, 
tvpSum);
-                }
+                FunctionHelper.arithmeticOperation(aOp, dCtx, tvp, tvpSum, 
tvpSum);
                 count++;
             }
         };

http://git-wip-us.apache.org/repos/asf/incubator-vxquery/blob/c813b097/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/aggregate/FnSumAggregateEvaluatorFactory.java
----------------------------------------------------------------------
diff --git 
a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/aggregate/FnSumAggregateEvaluatorFactory.java
 
b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/aggregate/FnSumAggregateEvaluatorFactory.java
index 8572a11..8a55f3c 100644
--- 
a/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/aggregate/FnSumAggregateEvaluatorFactory.java
+++ 
b/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/aggregate/FnSumAggregateEvaluatorFactory.java
@@ -21,7 +21,6 @@ import java.io.IOException;
 
 import org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
 import org.apache.vxquery.datamodel.values.ValueTag;
-import org.apache.vxquery.exceptions.ErrorCode;
 import org.apache.vxquery.exceptions.SystemException;
 import org.apache.vxquery.runtime.functions.arithmetic.AddOperation;
 import 
org.apache.vxquery.runtime.functions.base.AbstractTaggedValueArgumentAggregateEvaluator;
@@ -44,56 +43,36 @@ public class FnSumAggregateEvaluatorFactory extends 
AbstractTaggedValueArgumentA
 
     @Override
     protected IAggregateEvaluator createEvaluator(IScalarEvaluator[] args) 
throws AlgebricksException {
-        final ArrayBackedValueStorage abvsCount = new 
ArrayBackedValueStorage();
-        final DataOutput dOutCount = abvsCount.getDataOutput();
         final ArrayBackedValueStorage abvsSum = new ArrayBackedValueStorage();
         final DataOutput dOutSum = abvsSum.getDataOutput();
         final AddOperation aOp = new AddOperation();
 
         return new AbstractTaggedValueArgumentAggregateEvaluator(args) {
-            long count;
             TaggedValuePointable tvpSum = (TaggedValuePointable) 
TaggedValuePointable.FACTORY.createPointable();
 
+            // TODO Check if the second argument is supplied as the zero value.
+
             @Override
             public void init() throws AlgebricksException {
-                count = 0;
+                try {
+                    abvsSum.reset();
+                    dOutSum.write(ValueTag.XS_INTEGER_TAG);
+                    dOutSum.writeLong(0);
+                    tvpSum.set(abvsSum);
+                } catch (IOException e) {
+                    throw new AlgebricksException(e.toString());
+                }
             }
 
             @Override
             public void finish(IPointable result) throws AlgebricksException {
-                // TODO What is returned when step is never called. Since the 
second argument is the zero value.
-                if (count == 0) {
-                    // No argument return an integer.
-                    try {
-                        abvsCount.reset();
-                        dOutCount.write(ValueTag.XS_INTEGER_TAG);
-                        dOutCount.writeLong(0);
-                        result.set(abvsCount);
-                    } catch (Exception e) {
-                        
-                        throw new AlgebricksException(e);
-                    }
-                } else {
-                    result.set(tvpSum);
-                }
+                result.set(tvpSum);
             }
 
             @Override
             protected void step(TaggedValuePointable[] args) throws 
SystemException {
                 TaggedValuePointable tvp = args[0];
-                if (count == 0) {
-                    // Init.
-                    try {
-                        abvsSum.reset();
-                        dOutSum.write(tvp.getByteArray(), 
tvp.getStartOffset(), tvp.getLength());
-                        tvpSum.set(abvsSum);
-                    } catch (IOException e) {
-                        throw new SystemException(ErrorCode.SYSE0001, 
e.toString());
-                    }
-                } else {
-                    FunctionHelper.arithmeticOperation(aOp, dCtx, tvp, tvpSum, 
tvpSum);
-                }
-                count++;
+                FunctionHelper.arithmeticOperation(aOp, dCtx, tvp, tvpSum, 
tvpSum);
             }
         };
     }

Reply via email to