| 1 package com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.fromclause; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.client.*; 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.sql99.common.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 9 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*; 11 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates.*; 12 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*; 13 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 14 import com.daffodilwoods.daffodildb.utils.*; 15 import com.daffodilwoods.daffodildb.utils.comparator.*; 16 import com.daffodilwoods.database.resource.*; 17 18 29 30 public class NaturalJoinPredicate extends PredicateAbstract implements predicate, OperatorConstants { 31 34 private ColumnDetails columnLeft; 35 38 private ColumnDetails columnRight; 39 42 private ColumnDetails[] columnDetails; 43 47 private int operatorType; 48 private SuperComparator comparator; 49 50 public NaturalJoinPredicate(ColumnDetails columnLeft0, ColumnDetails columnRight0) { 51 columnLeft = columnLeft0; 52 columnRight = columnRight0; 53 columnDetails = new ColumnDetails[] {columnLeft, columnRight}; 54 operatorType = EQUALTO; 55 } 56 57 62 public int getPredicateType() throws DException { 63 return operatorType; 64 } 65 71 public long getEstimatedRows(long noOfRows) throws DException { 72 return (long) Math.sqrt(noOfRows); 73 } 74 75 public void setDefaultValues(_VariableValueOperations variableValueOperations) throws DException { 76 Object leftValue = variableValueOperations.getColumnValues(columnLeft); 77 Object rightValue = variableValueOperations.getColumnValues(columnRight); 78 if (leftValue == null && rightValue != null && ! (rightValue instanceof IgnoreValue)) { 79 ( (VariableValueOperations) variableValueOperations).addColumnValues(new _Reference[] {columnLeft} 80 , new Object [] {rightValue} 81 , 1); 82 } 83 ( (VariableValueOperations) variableValueOperations).show(); 84 if (leftValue != null && rightValue == null && ! (leftValue instanceof IgnoreValue)) { 85 ( (VariableValueOperations) variableValueOperations).addColumnValues(new _Reference[] {columnRight} 86 , new Object [] {leftValue} 87 , 1); 88 } 89 } 90 91 public boolean checkForSubQuery() throws DException { 92 return false; 93 } 94 95 public _Reference[] getReferences(TableDetails[] tableDetails) throws DException { 96 ArrayList list = new ArrayList(); 97 if (!ifExist(tableDetails, columnLeft.getTable())) { 98 list.add( (_Reference) columnLeft); 99 } 100 if (!ifExist(tableDetails, columnRight.getTable())) { 101 list.add( (_Reference) columnRight); 102 } 103 return list.isEmpty() ? null : 104 (_Reference[]) list.toArray(new _Reference[list.size()]); 105 } 106 107 private boolean ifExist(TableDetails[] tableDetails, TableDetails table) throws DException { 108 for (int i = 0, length = tableDetails.length; i < length; i++) { 109 if (tableDetails[i] == table) { 110 return true; 111 } 112 } 113 return false; 114 } 115 116 public ColumnDetails[] getColumnDetails() throws DException { 117 return columnDetails; 118 } 119 120 public _Reference[] checkSemantic(_ServerSession parent) throws DException { 121 return null; 122 } 123 124 public int getCardinality() throws DException { 125 return 1; 126 } 127 128 public ParameterInfo[] getParameterInfo() throws DException { 129 return null; 130 } 131 132 public void getColumnsIncluded(ArrayList aList) throws DException { 133 aList.add(columnLeft.getColumnName()); 134 aList.add(columnRight.getColumnName()); 135 } 136 137 public void getTablesIncluded(ArrayList aList) throws DException { 138 } 139 140 public ByteComparison getByteComparison(Object object) throws DException { 141 return new ByteComparison(true, new int[] {columnLeft.getDatatype()}); 142 } 143 144 public void releaseResource() throws DException { 145 } 146 147 public Object run(Object object) throws DException { 148 _VariableValues variableValues = (_VariableValues) object; 149 Object leftValue = variableValues.getColumnValues(columnLeft); 150 Object rightValue = variableValues.getColumnValues(columnRight); 151 int cmp = -1; 152 try { 153 cmp = comparator.compare(leftValue, rightValue); 154 } catch (NullPointerException ex) { 155 comparator = GetByteComparator.getComparator(columnLeft.getDatatype(), false, columnLeft.getTable().cc.getCollator()); 156 cmp = comparator.compare(leftValue, rightValue); 157 } 158 return booleanResult[operatorType - 1][cmp + 2]; 159 } 160 161 public Object clone() throws CloneNotSupportedException { 162 return new NaturalJoinPredicate( (ColumnDetails) columnLeft.clone(), (ColumnDetails) columnRight.clone()); 163 } 164 165 public Object [] getParameters(Object object) throws DException { 166 return null; 167 } 168 169 public _QualifiedBVE getQualifiedBVE(TableDetails tableDetails[]) throws com.daffodilwoods.database.resource.DException { 170 return new QualifiedBVE(null, BVEPlanMerger.getBooleanFactor(this)); 171 } 172 173 public int canUseForSeek() throws com.daffodilwoods.database.resource.DException { 174 return TypeConstants.BOTHSIDESEEKABLE; 175 } 176 177 public void setColumnPredicates(_AllColumnPredicates allColumnPredicates) throws com.daffodilwoods.database.resource.DException { 178 SingleColumnPredicate singleColumnPredicate = new SingleColumnPredicate(); 179 int index = checkForJoinPredicate(allColumnPredicates.getTableName()); 180 if (index != -1) { 181 JoinPredicate joinPredicate = (index == 1) 182 ? new JoinPredicate(BVEPlanMerger.getBooleanFactor(new NaturalRightIndexed(this, operatorType))) 183 : new JoinPredicate(BVEPlanMerger.getBooleanFactor(this)); 184 String colName = columnDetails[index].getAppropriateColumn(); singleColumnPredicate.setPredicate(joinPredicate); 186 singleColumnPredicate.setColumnName(colName); 187 allColumnPredicates.addSinglePredicate(new SingleColumnPredicate[] {singleColumnPredicate}); 188 return; 189 } 190 } 191 192 199 private int checkForJoinPredicate(String tableName) throws DException { 200 for (int i = 0; i < columnDetails.length; i++) { 201 if (columnDetails[i].getTableDetails().getAppropriateOnlyTableName(). 202 equalsIgnoreCase(tableName)) { return i; 204 } 205 } 206 return -1; 207 } 208 209 public _BVEPlan getExecutionPlan() throws com.daffodilwoods.database.resource.DException { 210 _JoinRelation simpleRelation = new SimpleRelation(new TableDetails[] {columnLeft.getTable(), columnRight.getTable()} 211 , BVEPlanMerger.getBooleanFactor(this)); 212 _JoinRelation[] array = new _JoinRelation[] {simpleRelation}; 213 AllTablesJoinRelation allTablesJoinRelation = new AllTablesJoinRelation(array); 214 BVEAllTablePlan bveAllTablePlan = new BVEAllTablePlan(null, allTablesJoinRelation, null); 215 return bveAllTablePlan; 216 } 217 218 public boolean isNullPredicate() throws DException { 219 return false; 220 } 221 222 public double getCost(long rowCount, boolean index) throws DException { 223 return Math.sqrt(rowCount); 224 } 225 226 public AbstractRowValueExpression[] getChilds() { 227 AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] {}; 228 return childs; 229 } 230 231 public String toString() { 232 try { 233 return columnLeft.getAppropriateQualifiedName() + " = " + 234 columnRight.getAppropriateQualifiedName(); 235 } catch (DException ex) { 236 return null; 237 } 238 } 239 } 240 | Popular Tags |