KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > tableexpression > fromclause > NaturalJoinPredicate


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 /**
19  * This class represents condition,which form in case of natural join using common
20  * column of table involved in natural join.this condition remain on On Clause of
21  * Natural Join.
22  * <p>Title: </p>
23  * <p>Description: </p>
24  * <p>Copyright: Copyright (c) 2003</p>
25  * <p>Company: </p>
26  * @author not attributable
27  * @version 1.0
28  */

29
30 public class NaturalJoinPredicate extends PredicateAbstract implements predicate, OperatorConstants {
31   /**
32    * It represents common column detail of left table.
33    */

34   private ColumnDetails columnLeft;
35   /**
36    * It represents common column detail of right table
37    */

38   private ColumnDetails columnRight;
39   /**
40    * It represents all column detail of left and right table.
41    */

42   private ColumnDetails[] columnDetails;
43   /**
44    * It represents type of operator used in predicate which is either equal ,
45    * greaterthan lessthan etc. In case of natural join predicate it will be equals
46    */

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    /**
58     * this method is used to get operator used in this predicate.
59     * @return
60     * @throws DException
61     */

62    public int getPredicateType() throws DException {
63       return operatorType;
64    }
65    /**
66     *This method is used to get estimated number of rows
67     * @param noOfRows
68     * @return
69     * @throws DException
70     */

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 JavaDoc leftValue = variableValueOperations.getColumnValues(columnLeft);
77       Object JavaDoc rightValue = variableValueOperations.getColumnValues(columnRight);
78       if (leftValue == null && rightValue != null && ! (rightValue instanceof IgnoreValue)) {
79          ( (VariableValueOperations) variableValueOperations).addColumnValues(new _Reference[] {columnLeft}
80              , new Object JavaDoc[] {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 JavaDoc[] {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 JavaDoc 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 JavaDoc run(Object JavaDoc object) throws DException {
148       _VariableValues variableValues = (_VariableValues) object;
149       Object JavaDoc leftValue = variableValues.getColumnValues(columnLeft);
150       Object JavaDoc rightValue = variableValues.getColumnValues(columnRight);
151       int cmp = -1;
152       try {
153          cmp = comparator.compare(leftValue, rightValue);
154       } catch (NullPointerException JavaDoc 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 JavaDoc clone() throws CloneNotSupportedException JavaDoc {
162       return new NaturalJoinPredicate( (ColumnDetails) columnLeft.clone(), (ColumnDetails) columnRight.clone());
163    }
164
165    public Object JavaDoc[] getParameters(Object JavaDoc 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 JavaDoc colName = columnDetails[index].getAppropriateColumn(); //this line has been added to set
185
singleColumnPredicate.setPredicate(joinPredicate);
186          singleColumnPredicate.setColumnName(colName);
187          allColumnPredicates.addSinglePredicate(new SingleColumnPredicate[] {singleColumnPredicate});
188          return;
189       }
190    }
191
192    /**
193     * Returns index of column involved in the condition whose table name matches
194     * with the passed table otheriwse -1 is returned.
195     * @param tableName
196     * @return
197     * @throws DException
198     */

199    private int checkForJoinPredicate(String JavaDoc tableName) throws DException {
200       for (int i = 0; i < columnDetails.length; i++) {
201          if (columnDetails[i].getTableDetails().getAppropriateOnlyTableName().
202              equalsIgnoreCase(tableName)) { //CHANGED BY SV
203
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 JavaDoc toString() {
232       try {
233          return columnLeft.getAppropriateQualifiedName() + " = " +
234              columnRight.getAppropriateQualifiedName();
235       } catch (DException ex) {
236          return null;
237       }
238    }
239 }
240
Popular Tags