01: /*
02:
03: Derby - Class org.apache.derby.impl.sql.execute.SumAggregator
04:
05: Licensed to the Apache Software Foundation (ASF) under one or more
06: contributor license agreements. See the NOTICE file distributed with
07: this work for additional information regarding copyright ownership.
08: The ASF licenses this file to you under the Apache License, Version 2.0
09: (the "License"); you may not use this file except in compliance with
10: the License. You may obtain a copy of the License at
11:
12: http://www.apache.org/licenses/LICENSE-2.0
13:
14: Unless required by applicable law or agreed to in writing, software
15: distributed under the License is distributed on an "AS IS" BASIS,
16: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: See the License for the specific language governing permissions and
18: limitations under the License.
19:
20: */
21:
22: package org.apache.derby.impl.sql.execute;
23:
24: import org.apache.derby.iapi.services.sanity.SanityManager;
25: import org.apache.derby.iapi.types.NumberDataValue;
26: import org.apache.derby.iapi.error.StandardException;
27: import org.apache.derby.iapi.sql.execute.ExecAggregator;
28: import org.apache.derby.iapi.types.DataValueDescriptor;
29:
30: import org.apache.derby.iapi.services.io.StoredFormatIds;
31:
32: /**
33: * Aggregator for SUM(). Defers most of its work
34: * to OrderableAggregator.
35: *
36: * @author jamie
37: */
38: public class SumAggregator extends OrderableAggregator {
39: /**
40: * Accumulate
41: *
42: * @param addend value to be added in
43: *
44: * @exception StandardException on error
45: *
46: * @see ExecAggregator#accumulate
47: */
48: protected void accumulate(DataValueDescriptor addend)
49: throws StandardException {
50:
51: /*
52: ** If we don't have any value yet, just clone
53: ** the addend.
54: */
55: if (value == null) {
56: /* NOTE: We need to call getClone() since value gets
57: * reused underneath us
58: */
59: value = addend.getClone();
60: } else {
61: NumberDataValue input = (NumberDataValue) addend;
62: NumberDataValue nv = (NumberDataValue) value;
63:
64: value = nv.plus(input, // addend 1
65: nv, // addend 2
66: nv); // result
67: }
68: }
69:
70: /**
71: * @return ExecAggregator the new aggregator
72: */
73: public ExecAggregator newAggregator() {
74: return new SumAggregator();
75: }
76:
77: ////////////////////////////////////////////////////////////
78: //
79: // FORMATABLE INTERFACE
80: //
81: /////////////////////////////////////////////////////////////
82: /**
83: * Get the formatID which corresponds to this class.
84: *
85: * @return the formatID of this class
86: */
87: public int getTypeFormatId() {
88: return StoredFormatIds.AGG_SUM_V01_ID;
89: }
90: }
|