1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.table; 2 3 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*; 4 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*; 5 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 6 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates.*; 7 import com.daffodilwoods.database.resource.*; 8 import com.daffodilwoods.database.sqlinitiator.*; 9 10 11 21 22 public class CostCalculator implements CostFactorConstants { 23 24 35 36 public static Object getCostForIndexedPredicate(AllColumnPredicates indexPredicates, long estimatedRows) throws DException { 37 if (indexPredicates == null) { 38 return null; 39 } 40 _SingleColumnPredicate[] singleColumnPredicate = indexPredicates.getSingleColumnPredicates(); 41 double ETROW = estimatedRows; 42 double cost = 0; 43 for (int i = 0, length = singleColumnPredicate.length; i < length; ++i) { 44 if (singleColumnPredicate[i] != null) { 45 predicate pred = singleColumnPredicate[i].getPredicate(); 46 int predicateType = pred.getPredicateType(); 47 cost += ETROW * INDEXPREDICATE / 100; ETROW = pred.getEstimatedRows( (long) ETROW); 49 } 50 } 51 return new Object [] {new Double (cost), new Double (ETROW)}; 52 } 53 54 71 public static double computeCostForIndex(IndexPredicate indexPredicate, Object indexTable, _Order order, String [] queryColumns, _IndexInformation indexInformation, int indexPosition) throws DException { 72 double cost = indexPredicate.getCost(indexTable); double ETROW = indexPredicate.getEstimatedRow(); int num = TableReferenceMerger.checkForOrderColumns(order, indexInformation); 75 boolean checkForOrderSolvableByIndex = num != -1; 76 if (!checkForOrderSolvableByIndex) { cost = cost + getCostForTemporaryOrder(ETROW); 78 indexPredicate.setOrder(order); 79 } else { 80 if (order != null) { 81 indexPredicate.setIndex(indexPosition); 82 order.setIndexIsSameOrReverse(num == 0); 83 } 84 } 85 boolean checkForQueryColumnsSolvableByIndex = TableReferenceMerger.checkForQueryColumns(indexInformation.getColumns(), queryColumns); 86 if (!checkForQueryColumnsSolvableByIndex) { cost = cost + getCostForQueryColumns(ETROW); 88 } 89 return cost; 90 } 91 92 103 104 public static Object getCostForNonIndexedPredicate(AllColumnPredicates nonIndexPredicates, double estimatedRows) throws DException { 105 if (nonIndexPredicates == null) { 106 return null; 107 } 108 booleanvalueexpression cond = nonIndexPredicates.getNonIndexedCondition(); 109 double cost = cond.getCost( (long) estimatedRows, false); 110 estimatedRows = (double) cond.getEstimatedRows( (long) estimatedRows); 111 return new Object [] {new Double (cost), new Double (estimatedRows)}; 112 } 113 114 120 121 public static double getCostForTemporaryOrder(double estimatedRows) throws DException { 122 return estimatedRows * ORDER / 100; 123 } 124 125 133 134 public static double getCostForQueryColumns(double estimatedRows) throws DException { 135 return estimatedRows * TABLESCAN / 100; 136 } 137 138 } 139 | Popular Tags |