1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.store.access.SortObserver; 25 import org.apache.derby.iapi.services.io.Storable; 26 27 import org.apache.derby.iapi.types.UserDataValue; 28 29 import org.apache.derby.iapi.types.CloneableObject; 30 31 import org.apache.derby.iapi.services.io.Storable; 32 import org.apache.derby.iapi.services.sanity.SanityManager; 33 34 import org.apache.derby.iapi.error.StandardException; 35 36 import org.apache.derby.iapi.sql.execute.ExecRow; 37 38 import org.apache.derby.iapi.types.DataValueDescriptor; 39 40 41 import java.util.Vector ; 42 43 48 public class AggregateSortObserver extends BasicSortObserver 49 { 50 51 protected GenericAggregator[] aggsToProcess; 52 protected GenericAggregator[] aggsToInitialize; 53 54 private int firstAggregatorColumn; 55 56 82 public AggregateSortObserver(boolean doClone, GenericAggregator[] aggsToProcess, 83 GenericAggregator[] aggsToInitialize, 84 ExecRow execRow) 85 { 86 super(doClone, false, execRow, true); 87 this.aggsToProcess = aggsToProcess; 88 this.aggsToInitialize = aggsToInitialize; 89 90 96 if (SanityManager.DEBUG) 97 { 98 SanityManager.ASSERT(aggsToInitialize != null, "aggsToInitialize argument to AggregateSortObserver is null"); 99 SanityManager.ASSERT(aggsToProcess != null, "aggsToProcess argument to AggregateSortObserver is null"); 100 } 101 102 if (aggsToInitialize.length > 0) 103 { 104 firstAggregatorColumn = aggsToInitialize[0].aggregatorColumnId; 105 } 106 } 107 108 121 public DataValueDescriptor[] insertNonDuplicateKey(DataValueDescriptor[] insertRow) 122 throws StandardException 123 { 124 DataValueDescriptor[] returnRow = 125 super.insertNonDuplicateKey(insertRow); 126 127 131 if (aggsToInitialize.length > 0 && 132 ((Storable)returnRow[firstAggregatorColumn]).isNull()) 133 { 134 for (int i = 0; i < aggsToInitialize.length; i++) 135 { 136 GenericAggregator aggregator = aggsToInitialize[i]; 137 UserDataValue wrapper = ((UserDataValue)returnRow[aggregator.aggregatorColumnId]); 138 if (SanityManager.DEBUG) 139 { 140 if (!wrapper.isNull()) 141 { 142 SanityManager.THROWASSERT("during aggregate "+ 143 "initialization, all wrappers expected to be empty; "+ 144 "however, the wrapper for the following aggregate " + 145 "was not empty:" +aggregator+". The value stored is "+ 146 wrapper.getObject()); 147 } 148 } 149 wrapper.setValue(aggregator.getAggregatorInstance()); 150 aggregator.accumulate(returnRow, returnRow); 151 } 152 } 153 154 return returnRow; 155 156 } 157 170 public DataValueDescriptor[] insertDuplicateKey(DataValueDescriptor[] insertRow, DataValueDescriptor[] existingRow) 171 throws StandardException 172 { 173 if (aggsToProcess.length == 0) 174 { 175 return null; 176 } 177 178 183 for (int i = 0; i < aggsToProcess.length; i++) 184 { 185 GenericAggregator aggregator = aggsToProcess[i]; 186 if (((Storable)insertRow[aggregator.getColumnId()]).isNull()) 187 { 188 aggregator.accumulate(insertRow, existingRow); 189 } 190 else 191 { 192 aggregator.merge(insertRow, existingRow); 193 } 194 } 195 return null; 196 } 197 } 198 | Popular Tags |