1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.types.NumberDataValue; 25 import org.apache.derby.iapi.types.UserDataValue; 26 27 import org.apache.derby.iapi.reference.SQLState; 28 29 import org.apache.derby.iapi.sql.execute.ExecRow; 30 import org.apache.derby.iapi.store.access.TransactionController; 31 import org.apache.derby.iapi.services.io.Storable; 32 33 import org.apache.derby.iapi.types.DataValueDescriptor; 34 import org.apache.derby.iapi.types.DataValueDescriptor; 35 import org.apache.derby.iapi.sql.ResultDescription; 36 37 import org.apache.derby.iapi.services.sanity.SanityManager; 38 import org.apache.derby.iapi.services.loader.ClassFactory; 39 40 import org.apache.derby.iapi.sql.execute.ExecAggregator; 41 import org.apache.derby.iapi.sql.execute.NoPutResultSet; 42 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 43 44 import org.apache.derby.iapi.error.StandardException; 45 import org.apache.derby.iapi.jdbc.ConnectionContext; 46 import java.sql.Statement ; 47 52 class GenericAggregator 53 { 54 AggregatorInfo aggInfo; 55 int aggregatorColumnId; 56 private int inputColumnId; 57 private int resultColumnId; 58 private ResultDescription inputColumnResultDescription; 59 60 private ExecRow[] rowArray; 61 private TemporaryRowHolderResultSet execResultSet; 62 private final ClassFactory cf; 63 64 68 private ExecAggregator cachedAggregator; 69 70 76 GenericAggregator 77 ( 78 AggregatorInfo aggInfo, 79 ClassFactory cf 80 ) 81 { 82 this.aggInfo = aggInfo; 83 aggregatorColumnId = aggInfo.getAggregatorColNum(); 84 inputColumnId = aggInfo.getInputColNum(); 85 resultColumnId = aggInfo.getOutputColNum(); 86 int[] colArray = new int[1]; 87 inputColumnResultDescription = aggInfo.getResultDescription(); 88 this.cf = cf; 89 } 90 91 92 99 void initialize(ExecRow row) 100 throws StandardException 101 { 102 if (SanityManager.DEBUG) 103 { 104 SanityManager.ASSERT(row != null, "row is null"); 105 } 106 107 UserDataValue aggregatorColumn = (UserDataValue) row.getColumn(aggregatorColumnId + 1); 108 109 ExecAggregator ua = (ExecAggregator) aggregatorColumn.getObject(); 110 if (ua == null) 111 { 112 ua = getAggregatorInstance(); 113 aggregatorColumn.setValue(ua); 114 } 115 } 116 117 127 void accumulate(ExecRow inputRow, 128 ExecRow accumulateRow) 129 throws StandardException 130 { 131 DataValueDescriptor inputColumn = null; 132 133 if (SanityManager.DEBUG) 134 { 135 SanityManager.ASSERT((inputRow != null) && (accumulateRow != null), "bad accumulate call"); 136 } 137 138 DataValueDescriptor aggregatorColumn = accumulateRow.getColumn(aggregatorColumnId + 1); 139 140 inputColumn = inputRow.getColumn(inputColumnId + 1); 141 142 accumulate(inputColumn, aggregatorColumn); 143 } 144 145 155 void accumulate(Object [] inputRow, 156 Object [] accumulateRow) 157 throws StandardException 158 { 159 DataValueDescriptor inputColumn = null; 160 161 if (SanityManager.DEBUG) 162 { 163 SanityManager.ASSERT((inputRow != null) && (accumulateRow != null), "bad accumulate call"); 164 } 165 166 DataValueDescriptor aggregatorColumn = (DataValueDescriptor) accumulateRow[aggregatorColumnId]; 167 inputColumn = (DataValueDescriptor) inputRow[inputColumnId]; 168 169 accumulate(inputColumn, aggregatorColumn); 170 } 171 172 182 void accumulate(DataValueDescriptor inputColumn, 183 DataValueDescriptor aggregatorColumn) 184 throws StandardException 185 { 186 ExecAggregator ua; 187 188 if (SanityManager.DEBUG) 189 { 190 194 if (!(aggregatorColumn instanceof UserDataValue)) 195 { 196 SanityManager.THROWASSERT("accumlator column is not a UserDataValue as "+ 197 "expected, it is a "+aggregatorColumn.getClass().getName()); 198 } 199 } 200 ua = (ExecAggregator) aggregatorColumn.getObject(); 201 202 207 if (ua == null) 208 { 209 ua = getAggregatorInstance(); 210 } 211 212 ua.accumulate(inputColumn, this); 213 } 214 215 225 void merge(ExecRow inputRow, 226 ExecRow mergeRow) 227 throws StandardException 228 { 229 230 DataValueDescriptor mergeColumn = mergeRow.getColumn(aggregatorColumnId + 1); 231 DataValueDescriptor inputColumn = inputRow.getColumn(aggregatorColumnId + 1); 232 233 merge(inputColumn, mergeColumn); 234 } 235 236 246 void merge(Object [] inputRow, 247 Object [] mergeRow) 248 throws StandardException 249 { 250 DataValueDescriptor mergeColumn = (DataValueDescriptor) mergeRow[aggregatorColumnId]; 251 DataValueDescriptor inputColumn = (DataValueDescriptor) inputRow[aggregatorColumnId]; 252 253 merge(inputColumn, mergeColumn); 254 } 255 256 264 boolean finish(ExecRow row) 265 throws StandardException 266 { 267 DataValueDescriptor outputColumn = row.getColumn(resultColumnId + 1); 268 DataValueDescriptor aggregatorColumn = row.getColumn(aggregatorColumnId + 1); 269 273 if (SanityManager.DEBUG) 274 { 275 SanityManager.ASSERT(aggregatorColumn != null, "aggregatorColumn is null"); 276 SanityManager.ASSERT(outputColumn != null, "otuputColumn is null"); 277 SanityManager.ASSERT(aggregatorColumn instanceof UserDataValue, 278 "accumlator column is not a UserDataValue as expected"); 279 } 280 281 ExecAggregator ua = (ExecAggregator) aggregatorColumn.getObject(); 282 283 288 if (ua == null) 289 { 290 ua = getAggregatorInstance(); 291 } 292 293 302 303 DataValueDescriptor result = ua.getResult(); 304 if (result == null) 305 outputColumn.setToNull(); 306 else 307 outputColumn.setValue(result); 308 309 return ua.didEliminateNulls(); 310 } 311 312 319 ExecAggregator getAggregatorInstance() 320 throws StandardException 321 { 322 ExecAggregator aggregatorInstance; 323 if (cachedAggregator == null) 324 { 325 try 326 { 327 Class aggregatorClass = cf.loadApplicationClass(aggInfo.getAggregatorClassName()); 328 Object agg = aggregatorClass.newInstance(); 329 aggregatorInstance = (ExecAggregator)agg; 330 cachedAggregator = aggregatorInstance; 331 aggregatorInstance.setup(aggInfo.getAggregateName()); 332 333 } catch (Exception e) 334 { 335 throw StandardException.unexpectedUserException(e); 336 } 337 } 338 else 339 { 340 aggregatorInstance = cachedAggregator.newAggregator(); 341 } 342 343 344 return aggregatorInstance; 345 } 346 347 351 354 int getColumnId() 355 { 356 return aggregatorColumnId; 358 } 359 360 361 367 void merge(Storable aggregatorColumnIn, 368 Storable aggregatorColumnOut) 369 throws StandardException 370 { 371 ExecAggregator uaIn; 372 ExecAggregator uaOut; 373 374 if (SanityManager.DEBUG) 375 { 376 380 if (!(aggregatorColumnIn instanceof UserDataValue)) 381 { 382 SanityManager.THROWASSERT("aggregatorColumnOut column is not "+ 383 "a UserAggreator as expected, "+ 384 "it is a "+aggregatorColumnIn.getClass().getName()); 385 } 386 if (!(aggregatorColumnOut instanceof UserDataValue)) 387 { 388 SanityManager.THROWASSERT("aggregatorColumnIn column is not"+ 389 " a UserAggreator as expected, "+ 390 "it is a "+aggregatorColumnOut.getClass().getName()); 391 } 392 } 393 uaIn = (ExecAggregator)(((UserDataValue) aggregatorColumnIn).getObject()); 394 uaOut = (ExecAggregator)(((UserDataValue) aggregatorColumnOut).getObject()); 395 396 uaOut.merge(uaIn); 397 } 398 399 AggregatorInfo getAggregatorInfo() 405 { 406 return aggInfo; 407 } 408 409 410 } 411 | Popular Tags |