1 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 48 public class SumAvgAggregateDefinition 49 implements AggregateDefinition 50 { 51 private boolean isSum; 52 56 public SumAvgAggregateDefinition() { super(); } 57 58 72 public final TypeDescriptor getAggregator(TypeDescriptor inputType, 73 StringBuffer 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 93 if (compType.isNumericTypeId()) 94 { 95 aggregatorClass.append(getAggregatorClassName()); 96 97 DataTypeDescriptor outDts = tc.resolveArithmeticOperation( 98 dts, dts, getOperator()); 99 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 122 private String getAggregatorClassName() 123 { 124 if ( isSum ) 125 return ClassName.SumAggregator; 126 else 127 return ClassName.AvgAggregator; 128 } 129 130 136 protected String getOperator() 137 { 138 if ( isSum ) 139 return TypeCompiler.SUM_OP; 140 else 141 return TypeCompiler.AVG_OP; 142 } 143 144 147 public final void setSumOrAvg(boolean isSum) 148 { 149 this.isSum = isSum; 150 } 151 152 } 153 | Popular Tags |