--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.execute.SumAggregator\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.sql.execute;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+import org.apache.derby.iapi.types.NumberDataValue;\r
+import org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.iapi.sql.execute.ExecAggregator;\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+\r
+import org.apache.derby.iapi.services.io.StoredFormatIds;\r
+\r
+/**\r
+ * Aggregator for SUM(). Defers most of its work\r
+ * to OrderableAggregator.\r
+ *\r
+ */\r
+public class SumAggregator \r
+ extends OrderableAggregator\r
+{\r
+ /**\r
+ * Accumulate\r
+ *\r
+ * @param addend value to be added in\r
+ *\r
+ * @exception StandardException on error\r
+ *\r
+ * @see ExecAggregator#accumulate\r
+ */\r
+ protected void accumulate(DataValueDescriptor addend) \r
+ throws StandardException\r
+ {\r
+\r
+ /*\r
+ ** If we don't have any value yet, just clone\r
+ ** the addend.\r
+ */\r
+ if (value == null)\r
+ { \r
+ /* NOTE: We need to call getClone() since value gets \r
+ * reused underneath us\r
+ */\r
+ value = addend.getClone();\r
+ }\r
+ else\r
+ {\r
+ NumberDataValue input = (NumberDataValue)addend;\r
+ NumberDataValue nv = (NumberDataValue) value;\r
+\r
+ value = nv.plus(\r
+ input, // addend 1\r
+ nv, // addend 2\r
+ nv); // result\r
+ }\r
+ }\r
+\r
+ /**\r
+ * @return ExecAggregator the new aggregator\r
+ */\r
+ public ExecAggregator newAggregator()\r
+ {\r
+ return new SumAggregator();\r
+ }\r
+\r
+ ////////////////////////////////////////////////////////////\r
+ // \r
+ // FORMATABLE INTERFACE\r
+ // \r
+ /////////////////////////////////////////////////////////////\r
+ /**\r
+ * Get the formatID which corresponds to this class.\r
+ *\r
+ * @return the formatID of this class\r
+ */\r
+ public int getTypeFormatId() { return StoredFormatIds.AGG_SUM_V01_ID; }\r
+}\r