1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.table; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.client.*; 6 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*; 7 import com.daffodilwoods.daffodildb.server.serversystem.*; 8 import com.daffodilwoods.daffodildb.server.sql99.common.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.common.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 11 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 12 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.order.*; 13 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table.*; 14 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 15 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*; 16 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 17 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 18 import com.daffodilwoods.database.resource.*; 19 import com.daffodilwoods.database.sqlinitiator.*; 20 import java.util.*; 21 import com.daffodilwoods.daffodildb.utils.byteconverter.CbCUsffWbmvfIboemfs; 22 import com.daffodilwoods.database.utility.P; 23 24 25 38 39 public class GroupByPlan extends PlanExceptionAbstract implements TableExpressionConstants, CostFactorConstants, SimpleConstants { 40 41 44 45 private _TablePlan tPlan; 47 50 51 private GroupByCondition groupByCondition; 53 56 57 private _Order order; 59 62 63 private ColumnDetails aggregateColumns[]; 65 68 69 private booleanvalueexpression aggregateCondition; 71 75 76 private boolean costCalculated; 77 78 82 83 private boolean indexUsedInAggregate; 84 85 private _Reference[] havinClauseUnderlyingReferences; 86 87 public GroupByPlan(_TablePlan tPlan0, GroupByCondition groupByCondition0, _Order order0, ColumnDetails[] aggregateColumnDetails0, booleanvalueexpression aggregateCondition0, boolean indexUsedInAggregate0,_Reference[] underlyingRef) throws DException { 88 aggregateCondition = aggregateCondition0; 89 tPlan = tPlan0; 90 groupByCondition = groupByCondition0; 91 order = order0; 92 aggregateColumns = aggregateColumnDetails0; 93 indexUsedInAggregate = indexUsedInAggregate0; 94 95 havinClauseUnderlyingReferences=underlyingRef; 96 } 97 98 103 104 public int getType() throws DException { 105 return TableExpressionConstants.GROUPBYPLAN; 106 } 107 108 116 117 public double getCost(_ServerSession session) throws DException { 118 costCalculated = true; 119 return tPlan.getCost(session) * (100.0 + CostFactorConstants.GROUP) / 100; 120 } 121 122 129 130 public long getRowCount(_ServerSession serverSession) throws DException { 131 return tPlan.getRowCount(serverSession) * GROUPROWREDUCED; 132 } 133 134 143 144 private _Iterator execute(_ServerSession session, _Iterator iterator) throws DException { 145 iterator = createGroupByIterator(session, iterator); 146 return createTemporaryIndexIterator(session, iterator); 147 } 148 149 155 private _Iterator createTemporaryIndexIterator(_ServerSession session, _Iterator iterator) throws DException { 156 if (order != null) { 157 TemporaryIndexIterator tempIndex = null; 158 if (session.getType() == IteratorConstants.UPDATABLE) { 159 _KeyColumnInformation[] keyColumns = getChangeKeyColumns(getKeyColumnInformation(order), iterator.getKeyColumnInformations()); 160 return GeneralPurposeStaticClass.getTemporaryIndexIterator(iterator, tPlan, session, order, keyColumns); 161 } 162 _Reference[] references = order.getReferences(getTableDetails()); 163 VariableValues variableValues = new VariableValues(references, session); 164 CbCUsffWbmvfIboemfs tempHandler = new TempIndexHandler(iterator); 165 variableValues.setIterator(iterator); 166 iterator = session.getIndexedIterator(iterator,new ExpressionOrderValues( GeneralPurposeStaticClass.getJoinOrdered(order,iterator.getDefaultOrder() ) ,variableValues),tempHandler) ; 167 return new TemporaryIndexWrapperIterator(iterator,iterator); 168 } 169 return iterator; 170 } 171 172 185 186 private _Iterator createGroupByIterator(_ServerSession session, _Iterator iterator) throws DException { 187 if (groupByCondition != null) { 188 groupByCondition.setIterator(iterator); 189 iterator = new GroupByIterator(iterator, groupByCondition, aggregateColumns,havinClauseUnderlyingReferences,session); 190 } else { 191 iterator = indexUsedInAggregate ? (_Iterator)new MaxMinIterator(iterator, aggregateColumns) : 192 new AggregateGroupByIterator(iterator, aggregateColumns,session); 193 } 194 iterator = solveCondition(session, iterator); 195 196 _Reference[] orderRef=order==null?null:order.getReferences(tPlan.getTableDetails()); 197 198 if(orderRef !=null) 199 for (int i = 0; i < orderRef.length; i++) { 200 if(orderRef[i].getReferenceType() == SimpleConstants.VARIABLECOLUMN){ 201 break; 202 } 203 havinClauseUnderlyingReferences=GeneralPurposeStaticClass.getJointReferences(havinClauseUnderlyingReferences,orderRef); 204 } 205 if (havinClauseUnderlyingReferences != null ) 206 iterator.setSpecificUnderlyingReferences(havinClauseUnderlyingReferences); 207 208 209 return iterator; 210 } 211 212 220 221 private _Iterator solveCondition(_ServerSession session, _Iterator iterator) throws DException { 222 if (aggregateCondition != null) { 223 return getNonIndexedFilteredIteratorWithSubQuery(iterator, session, aggregateCondition); 224 } 225 return iterator; 226 } 227 228 236 237 public _Iterator execute(_ServerSession session) throws DException { 238 if (!costCalculated) { 239 getCost(session); 240 } 241 _Iterator iterator = tPlan.execute(session); 242 return execute(session, iterator); 243 } 244 245 252 253 private _KeyColumnInformation[] getKeyColumnInformation(_Order order) throws DException { 254 boolean[] orders = order.getOrderOfColumns(); 255 ColumnDetails[] columnDetails = order.getKeyColumnDetails(); 256 int len = columnDetails.length; 257 _KeyColumnInformation[] columnInformations = new _KeyColumnInformation[len]; 258 for (int i = 0; i < len; i++) { 259 columnInformations[i] = new TableKeyColumnInformation(columnDetails[i], orders[i]); 260 columnInformations[i].setTableDetails(columnDetails[i].getTableDetails()); 261 } 262 return columnInformations; 263 } 264 265 273 274 private _KeyColumnInformation[] getChangeKeyColumns(_KeyColumnInformation[] orderColumnDetails, _KeyColumnInformation[] keyColumns) throws DException { 275 TreeSet set = new TreeSet(new Comparator() { 276 public int compare(Object obj1, Object obj2) { 277 _KeyColumnInformation first = (_KeyColumnInformation) obj1; 278 _KeyColumnInformation second = (_KeyColumnInformation) obj2; 279 try { 280 return first.getColumnDetails() == second.getColumnDetails() ? 0 : -1; 281 } catch (DException ex) { 282 throw new DRuntimeException("Dump Call", null); 283 } 284 } 285 }); 286 for (int i = 0, length = orderColumnDetails.length; i < length; i++) { 287 set.add(orderColumnDetails[i]); 288 } 289 for (int i = 0, length = keyColumns.length; i < length; i++) { 290 set.add(keyColumns[i]); 291 } 292 return (_KeyColumnInformation[]) set.toArray(new _KeyColumnInformation[set.size()]); 293 } 294 295 300 301 public _Order getOrder() throws DException { 302 return order; 303 } 304 305 316 317 public _Iterator execute(_ServerSession session, booleanvalueexpression condition) throws DException { 318 _Iterator iterator = tPlan.execute(session, condition); 319 return execute(session, iterator); 320 } 321 322 329 330 public long getEstimatedRows(booleanvalueexpression conditionArg, long estimatedRowCount, _ServerSession serverSession) throws DException { 331 return conditionArg.getEstimatedRows(getRowCount(serverSession)); 332 } 333 334 343 344 public double getCost(_ServerSession session, booleanvalueexpression condition, long estimatedRows) throws DException { 345 return tPlan.getCost(session, condition, estimatedRows) * CostFactorConstants.GROUP; 346 } 347 348 public String toString() { 349 String str = "GROUPBYPLAN["; 350 str += " TPLAN [" + tPlan + "]"; 351 str += " AGGREGATECONDITION [" + aggregateCondition + "]"; 352 str += "]"; 353 return str; 354 } 355 356 public String getVerifier() throws DException { 357 StringBuffer buffer = new StringBuffer (); 358 inc(); 359 buffer.append(tabW("[ GROUPBY_PLAN ]")); 360 buffer.append("\n" + tPlan.getVerifier()); 361 inc(); 362 buffer.append(tab("[GroupByCondition = \"" + groupByCondition + "\"]")); 363 buffer.append(tab("[AggregateCondition = \"" + aggregateCondition + "\"]")); 364 buffer.append(tab("[Order = \"" + order + "\"]")); 365 dec(); 366 buffer.append(tab("[/ GROUPBY_PLAN ]")); 367 dec(); 368 return buffer.toString(); 369 } 370 371 377 378 public _QueryPlan getQueryPlan() throws DException { 379 _QueryPlan[] cplans = new _QueryPlan[] {tPlan.getQueryPlan()}; 380 String cond = groupByCondition == null ? null : "" + groupByCondition; 381 String ord = order == null ? null : "" + order; 382 String cond1 = aggregateCondition == null ? null : "" + aggregateCondition; 383 cond = cond == null ? cond1 : (cond1 == null ? cond : cond + " <> " + cond1); 384 return new QueryPlan("GroupByPlan", cplans, cond, ord); 385 } 386 387 392 393 public _TablePlan[] getChildTablePlans() throws DException { 394 childPlans = tPlan.getChildTablePlans(); 395 initializeTableDetails(); 396 return new _TablePlan[] {this}; 397 } 398 403 404 public double getCostWithoutOrder(_ServerSession session, booleanvalueexpression condition, long estimatedRowCount) throws DException { 405 return getCost(session, condition, estimatedRowCount); 406 } 407 408 public double getCostWithoutOrder(_ServerSession session) throws DException { 409 return getCost(session); 410 } 411 412 public _Iterator executeWithOutOrder(_ServerSession session, booleanvalueexpression condition) throws DException { 413 return execute(session, condition); 414 } 415 416 public _Iterator executeWithoutOrder(_ServerSession session) throws DException { 417 return execute(session); 418 } 419 420 } 421 | Popular Tags |