--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.sql.compile.SumAvgAggregateDefinition\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.compile;\r
+\r
+import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;\r
+import org.apache.derby.iapi.services.context.ContextService;\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import org.apache.derby.impl.sql.execute.SumAggregator;\r
+import org.apache.derby.impl.sql.execute.AvgAggregator;\r
+\r
+import org.apache.derby.catalog.TypeDescriptor;\r
+import org.apache.derby.iapi.types.TypeId;\r
+import org.apache.derby.iapi.types.DataTypeDescriptor;\r
+\r
+import org.apache.derby.iapi.sql.compile.TypeCompiler;\r
+import org.apache.derby.iapi.sql.compile.TypeCompilerFactory;\r
+\r
+import org.apache.derby.iapi.sql.compile.CompilerContext;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+import org.apache.derby.iapi.reference.ClassName;\r
+\r
+/**\r
+ * Defintion for the SUM()/AVG() aggregates.\r
+ *\r
+ */\r
+public class SumAvgAggregateDefinition\r
+ implements AggregateDefinition \r
+{\r
+ private boolean isSum;\r
+ /**\r
+ * Niladic constructor. Does nothing. For ease\r
+ * Of use, only.\r
+ */\r
+ public SumAvgAggregateDefinition() { super(); }\r
+\r
+ /**\r
+ * Determines the result datatype. Accept NumberDataValues\r
+ * only. \r
+ * <P>\r
+ * <I>Note</I>: In the future you should be able to do\r
+ * a sum user data types. One option would be to run\r
+ * sum on anything that implements plus(). In which\r
+ * case avg() would need divide().\r
+ *\r
+ * @param inputType the input type, either a user type or a java.lang object\r
+ *\r
+ * @return the output Class (null if cannot operate on\r
+ * value expression of this type.\r
+ */\r
+ public final DataTypeDescriptor getAggregator(DataTypeDescriptor inputType, \r
+ StringBuffer aggregatorClass) \r
+ {\r
+ try\r
+ {\r
+ TypeId compType = inputType.getTypeId();\r
+ \r
+ CompilerContext cc = (CompilerContext)\r
+ ContextService.getContext(CompilerContext.CONTEXT_ID);\r
+ TypeCompilerFactory tcf = cc.getTypeCompilerFactory();\r
+ TypeCompiler tc = tcf.getTypeCompiler(compType);\r
+ \r
+ /*\r
+ ** If the class implements NumberDataValue, then we\r
+ ** are in business. Return type is same as input\r
+ ** type.\r
+ */\r
+ if (compType.isNumericTypeId())\r
+ {\r
+ aggregatorClass.append(getAggregatorClassName());\r
+\r
+ DataTypeDescriptor outDts = tc.resolveArithmeticOperation( \r
+ inputType, inputType, getOperator());\r
+ /*\r
+ ** SUM and AVG may return null\r
+ */\r
+ return outDts.getNullabilityType(true);\r
+ }\r
+ }\r
+ catch (StandardException e)\r
+ {\r
+ if (SanityManager.DEBUG)\r
+ {\r
+ SanityManager.THROWASSERT("Unexpected exception", e);\r
+ }\r
+ }\r
+\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Return the aggregator class. \r
+ *\r
+ * @return SumAggregator.CLASS_NAME/AvgAggregator.CLASS_NAME\r
+ */\r
+ private String getAggregatorClassName()\r
+ {\r
+ if ( isSum )\r
+ return ClassName.SumAggregator;\r
+ else\r
+ return ClassName.AvgAggregator;\r
+ }\r
+\r
+ /**\r
+ * Return the arithmetic operator corresponding\r
+ * to this operation.\r
+ *\r
+ * @return TypeCompiler.SUM_OP /TypeCompiler.AVG_OP\r
+ */\r
+ protected String getOperator()\r
+ {\r
+ if ( isSum )\r
+ return TypeCompiler.SUM_OP;\r
+ else\r
+ return TypeCompiler.AVG_OP;\r
+ }\r
+\r
+ /**\r
+ * This is set by the parser.\r
+ */\r
+ public final void setSumOrAvg(boolean isSum)\r
+ {\r
+ this.isSum = isSum;\r
+ }\r
+\r
+}\r