KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > SumAvgAggregateDefinition


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.SumAvgAggregateDefinition
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (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.compile;
23
24 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
25 import org.apache.derby.iapi.services.context.ContextService;
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27
28 import org.apache.derby.impl.sql.execute.SumAggregator;
29 import org.apache.derby.impl.sql.execute.AvgAggregator;
30
31 import org.apache.derby.catalog.TypeDescriptor;
32 import org.apache.derby.iapi.types.TypeId;
33 import org.apache.derby.iapi.types.DataTypeDescriptor;
34
35 import org.apache.derby.iapi.sql.compile.TypeCompiler;
36 import org.apache.derby.iapi.sql.compile.TypeCompilerFactory;
37
38 import org.apache.derby.iapi.sql.compile.CompilerContext;
39
40 import org.apache.derby.iapi.error.StandardException;
41 import org.apache.derby.iapi.reference.ClassName;
42
43 /**
44  * Defintion for the SUM()/AVG() aggregates.
45  *
46  * @author jamie
47  */

48 public class SumAvgAggregateDefinition
49         implements AggregateDefinition
50 {
51     private boolean isSum;
52     /**
53      * Niladic constructor. Does nothing. For ease
54      * Of use, only.
55      */

56     public SumAvgAggregateDefinition() { super(); }
57
58     /**
59      * Determines the result datatype. Accept NumberDataValues
60      * only.
61      * <P>
62      * <I>Note</I>: In the future you should be able to do
63      * a sum user data types. One option would be to run
64      * sum on anything that implements plus(). In which
65      * case avg() would need divide().
66      *
67      * @param inputType the input type, either a user type or a java.lang object
68      *
69      * @return the output Class (null if cannot operate on
70      * value expression of this type.
71      */

72     public final TypeDescriptor getAggregator(TypeDescriptor inputType,
73                 StringBuffer JavaDoc aggregatorClass)
74     {
75         try
76         {
77             LanguageConnectionContext lcc = (LanguageConnectionContext)
78                 ContextService.getContext(LanguageConnectionContext.CONTEXT_ID);
79
80             DataTypeDescriptor dts = new DataTypeDescriptor( (DataTypeDescriptor)inputType, inputType.isNullable());
81             TypeId compType = dts.getTypeId();
82         
83             CompilerContext cc = (CompilerContext)
84                 ContextService.getContext(CompilerContext.CONTEXT_ID);
85             TypeCompilerFactory tcf = cc.getTypeCompilerFactory();
86             TypeCompiler tc = tcf.getTypeCompiler(compType);
87         
88             /*
89             ** If the class implements NumberDataValue, then we
90             ** are in business. Return type is same as input
91             ** type.
92             */

93             if (compType.isNumericTypeId())
94             {
95                 aggregatorClass.append(getAggregatorClassName());
96
97                 DataTypeDescriptor outDts = tc.resolveArithmeticOperation(
98                                                             dts, dts, getOperator());
99                 /*
100                 ** SUM and AVG may return null
101                 */

102                 outDts.setNullability(true);
103                 return outDts;
104             }
105         }
106         catch (StandardException e)
107         {
108             if (SanityManager.DEBUG)
109             {
110                 SanityManager.THROWASSERT("Unexpected exception " + e);
111             }
112         }
113
114         return null;
115     }
116
117     /**
118      * Return the aggregator class.
119      *
120      * @return SumAggregator.CLASS_NAME/AvgAggregator.CLASS_NAME
121      */

122     private String JavaDoc getAggregatorClassName()
123     {
124         if ( isSum )
125                 return ClassName.SumAggregator;
126         else
127                 return ClassName.AvgAggregator;
128     }
129
130     /**
131      * Return the arithmetic operator corresponding
132      * to this operation.
133      *
134      * @return TypeCompiler.SUM_OP /TypeCompiler.AVG_OP
135      */

136     protected String JavaDoc getOperator()
137     {
138         if ( isSum )
139                 return TypeCompiler.SUM_OP;
140         else
141                 return TypeCompiler.AVG_OP;
142     }
143
144     /**
145      * This is set by the parser.
146      */

147     public final void setSumOrAvg(boolean isSum)
148     {
149         this.isSum = isSum;
150     }
151
152 }
153
Popular Tags