KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > expression > booleanvalueexpression > booleantermSRESERVEDWORD1206543922booleanfactor


1 package com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*;
6 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
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.token.*;
14 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
15 import com.daffodilwoods.database.resource.*;
16 import com.daffodilwoods.database.sqlinitiator.*;
17
18 /**
19  * <p>Title: BooleanTermAndBooleanFactor</p>
20  * <p>Description:
21  * This class represents AND condition with two children boolean term and boolean
22  * factor. Boolean term in turn can be BTANDBF/BF/ParenthesizedBVE/TRUE/FALSE.
23  * Boolean factor in turn can be Predicate/ParenthesizedBVE/TRUE/FALSE.
24  * It provides the functionality of Bve Execution Plan and execution of the
25  * condition.
26  * </p>
27  * <p>Copyright: Copyright (c) 2003</p>
28  * <p>Company: Daffodil S/W Ltd.</p>
29  * @author SelectTeam
30  * @version 1.0
31  */

32
33 public class booleantermSRESERVEDWORD1206543922booleanfactor extends AbstractBooleanValueExpression implements booleanterm, ExecutionPlanConstants, JoinConditionTableGetter ,_SingleTableCondition{
34    public booleanfactor _booleanfactor0;
35    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439221;
36    public booleanterm _booleanterm2;
37
38    public booleantermSRESERVEDWORD1206543922booleanfactor() {
39    }
40
41    public booleantermSRESERVEDWORD1206543922booleanfactor(booleanterm obj1, booleanfactor obj2, SRESERVEDWORD1206543922 reservedWord) {
42       _booleanfactor0 = (booleanfactor) obj2;
43       _booleanterm2 = (booleanterm) obj1;
44       _SRESERVEDWORD12065439221 = reservedWord;
45    }
46
47    public booleantermSRESERVEDWORD1206543922booleanfactor(booleanterm obj1, booleanfactor obj2) {
48       _booleanfactor0 = (booleanfactor) obj2;
49       _booleanterm2 = (booleanterm) obj1;
50    }
51
52    /**
53     * Returns the cost of evaluating this condition on passed number of rows, by
54     * considering the factor of index.
55     * @param rowCount
56     * @param index
57     * @return cost
58     * @throws DException
59     */

60
61    public double getCost(long rowCount, boolean index) throws DException {
62       long rowsReduced = _booleanterm2.getEstimatedRows(rowCount);
63       return _booleanterm2.getCost(rowCount, index) + _booleanfactor0.getCost(rowsReduced, index);
64    }
65
66    /**
67     * This method is called from qualified join. Passed TableDetails are of
68     * Right plans in case of LOJ and of Left Table Plans in case of ROJ.
69     * With the help of this method we identify whether 'on condition' is solvable
70     * on SingleTablePlan or on Qualified Join Level.
71     * Takes the QualifiedBVE from the booleanterm and the booleanfactor. If there is a
72     * match of TableName from the SingleTablePlans of the QualifiedBVEs obtained from the BT and BF,
73     * then the SingletablePlans' condition is ANDed.
74     * The OnConditions obtained from the BT and BF are also ANDed.
75     * The QualifiedBVE with the singleTablePlans ,if any , and the ANDed ONCondition is returned.
76     * @param tablePlan
77     * @return
78     * @throws DException
79     */

80
81    public _QualifiedBVE getQualifiedBVE(TableDetails[] tableDetails0) throws DException {
82       _QualifiedBVE qBve1 = _booleanterm2.getQualifiedBVE(tableDetails0);
83       _QualifiedBVE qBve2 = _booleanfactor0.getQualifiedBVE(tableDetails0);
84       _BVESingleTablePlan[] bveSingleTablePlan1 = (BVESingleTablePlan[]) qBve1.getBVESingleTablePlans();
85       _BVESingleTablePlan[] bveSingleTablePlan2 = (BVESingleTablePlan[]) qBve2.getBVESingleTablePlans();
86       booleanvalueexpression resultantOnCondition = BVEPlanMerger.addAndConditions(qBve1.getOnCondition(), qBve2.getOnCondition());
87       if (bveSingleTablePlan1 == null) {
88          return (bveSingleTablePlan2 == null) ? new QualifiedBVE(null, this) : new QualifiedBVE(bveSingleTablePlan2, resultantOnCondition);
89       }
90       if (bveSingleTablePlan2 == null) {
91          return new QualifiedBVE(bveSingleTablePlan1, resultantOnCondition);
92       }
93       int length1 = bveSingleTablePlan1.length;
94       int length2 = bveSingleTablePlan2.length;
95       ArrayList aList = new ArrayList(length2);
96       for (int i = 0; i < length2; i++) {
97          boolean found = false;
98          _BVESingleTablePlan bveSingleTablePlan = bveSingleTablePlan2[i];
99          TableDetails tableDetails = bveSingleTablePlan.getTableDetails()[0];
100          for (int j = 0; j < length1; j++) {
101             if (tableDetails == bveSingleTablePlan1[j].getTableDetails()[0]) {
102                bveSingleTablePlan1[j] = BVEPlanMerger.mergeSameSinglePlanWithAnd(bveSingleTablePlan1[j], bveSingleTablePlan);
103                found = true;
104                break;
105             }
106          }
107          if (!found) {
108             aList.add(bveSingleTablePlan);
109          }
110       }
111       int size = aList.size();
112       QualifiedBVE resultant = null;
113       if (size != 0) {
114          BVESingleTablePlan[] stp = new BVESingleTablePlan[bveSingleTablePlan1.length + size];
115          BVESingleTablePlan[] stp1 = (BVESingleTablePlan[]) aList.toArray(new BVESingleTablePlan[aList.size()]);
116          System.arraycopy(bveSingleTablePlan1, 0, stp, 0, bveSingleTablePlan1.length);
117          System.arraycopy(stp1, 0, stp, bveSingleTablePlan1.length, stp1.length);
118          resultant = new QualifiedBVE(stp, resultantOnCondition);
119       } else {
120          resultant = new QualifiedBVE(bveSingleTablePlan1, resultantOnCondition);
121       }
122       return resultant;
123    }
124
125    /**
126     * This method tells whether this condition can be used for seek or not.
127     * The criteria for seek is -
128     * Individual predicates is Equal Predicate with one column of type
129     * Reference on each side of Equal Predicate.
130     * @return
131     * @throws DException
132     */

133
134    public int canUseForSeek() throws DException {
135    int leftSeekableValue = _booleanterm2.canUseForSeek();
136    int rightseekableValue = _booleanfactor0.canUseForSeek();
137    switch(leftSeekableValue){
138       case TypeConstants.BOTHSIDESEEKABLE:
139          switch(rightseekableValue){
140             case TypeConstants.BOTHSIDESEEKABLE:
141             case TypeConstants.LEFTSIDESEEKABLE:
142             case TypeConstants.RIGHTSIDESEEKABLE:
143             case TypeConstants.NOSEEK:
144                return TypeConstants.BOTHSIDESEEKABLE;
145             default:
146                 throw new DException("DSE0",new Object JavaDoc[] {"IN CanUseForSeek Invalid Case"}); //To be Asked }
147
}
148
149       case TypeConstants.LEFTSIDESEEKABLE:
150          switch(rightseekableValue){
151             case TypeConstants.BOTHSIDESEEKABLE:
152             case TypeConstants.LEFTSIDESEEKABLE:
153             case TypeConstants.RIGHTSIDESEEKABLE:
154                return TypeConstants.BOTHSIDESEEKABLE;
155             case TypeConstants.NOSEEK:
156                return TypeConstants.LEFTSIDESEEKABLE;
157             default:
158                throw new DException("DSE0",new Object JavaDoc[] {"IN CanUseForSeek Invalid Case"}); //To be Asked
159
}
160       case TypeConstants.RIGHTSIDESEEKABLE:
161          switch(rightseekableValue){
162             case TypeConstants.BOTHSIDESEEKABLE:
163             case TypeConstants.LEFTSIDESEEKABLE:
164             case TypeConstants.RIGHTSIDESEEKABLE:
165                return TypeConstants.BOTHSIDESEEKABLE;
166             case TypeConstants.NOSEEK:
167                return TypeConstants.RIGHTSIDESEEKABLE;
168             default:
169               throw new DException("DSE0",new Object JavaDoc[] {"IN CanUseForSeek Invalid Case"}); //To be Asked
170
}
171        case TypeConstants.NOSEEK:
172           return TypeConstants.NOSEEK;
173       default:
174          throw new DException("DSE0",new Object JavaDoc[] {"IN CanUseForSeek Invalid Case"}); //To be Asked
175

176    }
177 }
178
179 /**
180   public int canUseForSeek() throws DException {
181       int leftSeekableValue = _booleanterm2.canUseForSeek();
182       int rightseekableValue = _booleanfactor0.canUseForSeek();
183       switch(leftSeekableValue){
184          case TypeConstants.BOTHSIDESEEKABLE:
185             switch(rightseekableValue){
186                case TypeConstants.BOTHSIDESEEKABLE:
187                   return TypeConstants.BOTHSIDESEEKABLE;
188                case TypeConstants.LEFTSIDESEEKABLE:
189                   return TypeConstants.LEFTSIDESEEKABLE;
190                case TypeConstants.RIGHTSIDESEEKABLE:
191                   return TypeConstants.RIGHTSIDESEEKABLE;
192                case TypeConstants.NOSEEK:
193                   return TypeConstants.NOSEEK;
194                default:
195                    throw new DException("DSE0",new Object[] {"IN CanUseForSeek Invalid Case"}); //To be Asked }
196             }
197
198          case TypeConstants.LEFTSIDESEEKABLE:
199             switch(rightseekableValue){
200                case TypeConstants.BOTHSIDESEEKABLE:
201                   return TypeConstants.LEFTSIDESEEKABLE;
202                case TypeConstants.LEFTSIDESEEKABLE:
203                   return TypeConstants.LEFTSIDESEEKABLE;
204                case TypeConstants.RIGHTSIDESEEKABLE:
205                   return TypeConstants.NOSEEK;
206                case TypeConstants.NOSEEK:
207                   return TypeConstants.LEFTSIDESEEKABLE;
208                default:
209                   throw new DException("DSE0",new Object[] {"IN CanUseForSeek Invalid Case"}); //To be Asked
210             }
211          case TypeConstants.RIGHTSIDESEEKABLE:
212             switch(rightseekableValue){
213                case TypeConstants.BOTHSIDESEEKABLE:
214                   return TypeConstants.RIGHTSIDESEEKABLE;
215                case TypeConstants.LEFTSIDESEEKABLE:
216                   return TypeConstants.NOSEEK;
217                case TypeConstants.RIGHTSIDESEEKABLE:
218                   return TypeConstants.RIGHTSIDESEEKABLE;
219                case TypeConstants.NOSEEK:
220                   return TypeConstants.RIGHTSIDESEEKABLE;
221                default:
222                  throw new DException("DSE0",new Object[] {"IN CanUseForSeek Invalid Case"}); //To be Asked
223             }
224           case TypeConstants.NOSEEK:
225              return TypeConstants.NOSEEK;
226          default:
227             throw new DException("DSE0",new Object[] {"IN CanUseForSeek Invalid Case"}); //To be Asked
228
229       }
230    }
231
232 **/

233
234    /**
235     * This method is called from top level condition class. And that top level
236     * class represents the condition of SingleTable. Now this method separates the
237     * condition of individual predicates based on the columnName as well as
238     * the condition which can't be used in index like not predicate etc.
239     * @param allColumnPredicates
240     * @throws DException
241     */

242
243
244
245    public void setColumnPredicates(_AllColumnPredicates allColumnPredicates) throws DException {
246       _booleanterm2.setColumnPredicates(allColumnPredicates);
247       _booleanfactor0.setColumnPredicates(allColumnPredicates);
248    }
249
250    /**
251     * This condition represents the condition of SingleTable.
252     * This method returns the cost of this condition based on following criteria -
253     * 1. Condition is solvable on index
254     * A. Is Order solvable on that index.
255     * a. Are Query columns solvable on that index.
256     * 2. Condition is not solvable on index
257     * A. Order is solvable on index.
258     * a. Are Query columns solvable on that index.
259     *
260     * For achieving that purpose we initialize AllColumnPredicate in which we
261     * separate out the individual predicates based on the column name as well
262     * as the predicates which can't be solved on index.
263     * Then We check cost of AllColumnPredicates for each index available on
264     * that table.
265     * @param order
266     * @param queryColumns
267     * @param session
268     * @param tableDetail
269     * @param aggregateColumns
270     * @return
271     * @throws DException
272     */

273
274    public _IndexPredicateInterface getCost(_Order order, String JavaDoc[] queryColumns, Object JavaDoc session, TableDetails tableDetail, ColumnDetails[] aggregateColumns) throws DException {
275       AllColumnPredicates allColumnPredicate = initializeAllColumnPredicate(tableDetail);
276       return calculateCost(order, queryColumns, session, tableDetail, allColumnPredicate, aggregateColumns);
277    }
278
279    private IndexPredicate calculateCost(_Order order, String JavaDoc[] queryColumns, Object JavaDoc session, TableDetails tableDetail, AllColumnPredicates allColumnPredicate, ColumnDetails[] aggregateColumns) throws DException {
280       IndexPredicate resultantIndexPredicate = null;
281       _IndexInformation[] indexInformation = ( (_IndexTable) session).getIndexInformations();
282       ColumnDetails aggColumn = aggregateColumns == null ? null : aggregateColumns[0].getExistingColumnDetails()[0];
283       for (int i = 0; i < indexInformation.length; ++i) {
284          IndexPredicate indexPredicate = (IndexPredicate) allColumnPredicate.splitForIndex(indexInformation[i], i);
285          indexPredicate.setTableDetails(tableDetail);
286          if (TableReferenceMerger.checkForAggregateColumn(aggColumn, indexInformation[i])) {
287             double cost = CostCalculator.computeCostForIndex(indexPredicate, session, order, queryColumns, indexInformation[i], i);
288             indexPredicate.setCost(cost);
289             if (aggColumn != null) {
290                indexPredicate.setIndex(i);
291             }
292             resultantIndexPredicate = checkForMinimum(indexPredicate, resultantIndexPredicate);
293          }
294       }
295       if (resultantIndexPredicate.getOrder() == null && order != null) {
296          order.setSolvableByIndex(true);
297       }
298       return resultantIndexPredicate;
299    }
300
301    private AllColumnPredicates initializeAllColumnPredicate(TableDetails tableDetail) throws DException {
302       AllColumnPredicates allColumnPredicate = new AllColumnPredicates();
303       allColumnPredicate.setTableDetails(tableDetail);
304       _booleanterm2.setColumnPredicates(allColumnPredicate);
305       _booleanfactor0.setColumnPredicates(allColumnPredicate);
306       return allColumnPredicate;
307    }
308
309    private IndexPredicate checkForMinimum(IndexPredicate indexPredicate1, IndexPredicate indexPredicate2) throws DException {
310       if (indexPredicate2 == null) {
311          return indexPredicate1;
312       }
313       double comingCost = indexPredicate1.getCost();
314       double inCost = indexPredicate2.getCost();
315       return inCost > comingCost
316           ? indexPredicate1 : indexPredicate2;
317    }
318
319    public int getConditionType() throws DException {
320       return BTANDBF;
321    }
322
323    /**
324     * This method returns the ExecutionPlan of the condition. Execution Plan
325     * contains the following information -
326     * 1. Array of BveSingleTablePlans, the condition which can be solved on
327     * single table level.
328     * 2. BveAllTablePlan which contains the condition in which two tables are
329     * involved(join condition) and it also contains remaining condition
330     * 3. BveAggregatePlan which contains the condition of aggregate columns.
331     * This condition can involve in having clause only.
332     * @return
333     * @throws DException
334     */

335
336    public _BVEPlan getExecutionPlan() throws DException {
337       _BVEPlan tablePlan1 = _booleanterm2.getExecutionPlan();
338       _BVEPlan tablePlan2 = _booleanfactor0.getExecutionPlan();
339       _BVEPlan resultantPlan = BVEPlanMerger.mergeTablePlansWithAnd(tablePlan1, tablePlan2);
340       return resultantPlan;
341    }
342
343    /**
344     * This method is called on execution time, to get the result.
345     * Passed Object is variableValues which contains -
346     * 1. Reference-Value mapping
347     * 2. Iterator.
348     * This variable values helps in retrieving the result of underlying
349     * predicates.
350     * @param object
351     * @return This method returns the result of anding of the result of
352     * underlying predicates -
353     * Zero means condition is satisfied.
354     * Non Zero means condition is not satisfied.
355     * if both underlying results are zero then zero is returned
356     * else non zero is returned.
357     * @throws DException
358     */

359
360    public Object JavaDoc run(Object JavaDoc object) throws DException {
361       Object JavaDoc result1 = _booleanterm2.run(object);
362       if (result1.hashCode() == 0) {
363          return _booleanfactor0.run(object);
364       }
365       Object JavaDoc result2 = null;
366       if (result1.hashCode() == -2) { //cases like (!(U and F))
367
result2 = _booleanfactor0.run(object);
368          return result2.hashCode() == 0 ? result1 : predicate.boolResultWithLeftSign[result2.hashCode() + 2][1];
369       } else if (result1.hashCode() == 2) {
370          result2 = _booleanfactor0.run(object);
371          return result2.hashCode() == 0 ? result1 : predicate.boolResultWithLeftSign[result2.hashCode() + 2][0];
372       }
373       return result1;
374    }
375
376    /**
377     * This method is called from Browser, when Parameterized query is fired from Browser.
378     * With the help of ParameterInfo Browser open ups daialog box(labelled by its corresponding name)
379     * for each question mark to put the values of question mark.
380     * @return
381     * @throws DException
382     */

383
384    public ParameterInfo[] getParameterInfo() throws DException {
385       ParameterInfo[] parameterInfo1 = _booleanterm2.getParameterInfo();
386       ParameterInfo[] parameterInfo2 = _booleanfactor0.getParameterInfo();
387       return GeneralPurposeStaticClass.getCombinedParameterInfoArray(parameterInfo1, parameterInfo2);
388    }
389
390    /**
391     * This method is needed in insert through resultset and is called from
392     * join condition of qualified join.
393     * @param td
394     * @return
395     * @throws com.daffodilwoods.database.resource.DException
396     */

397
398    public TableDetails[] getCorrespondingTablesInvolved(TableDetails[] td) throws com.daffodilwoods.database.resource.DException {
399       ArrayList toRet = new ArrayList();
400       boolean found = false;
401       TableDetails[] td1 = null, td2 = null;
402       if (_booleanterm2 instanceof JoinConditionTableGetter) {
403          td1 = ( (JoinConditionTableGetter) _booleanterm2).getCorrespondingTablesInvolved(td);
404          if (td1 != null) {
405             toRet.addAll(Arrays.asList(td1));
406             found = true;
407          }
408       }
409       if (_booleanfactor0 instanceof JoinConditionTableGetter) {
410          td2 = ( (JoinConditionTableGetter) _booleanfactor0).getCorrespondingTablesInvolved(td);
411          if (td2 != null) {
412             toRet.addAll(Arrays.asList(td2));
413             found = true;
414          }
415       }
416       if (found) {
417          return (TableDetails[]) toRet.toArray(new TableDetails[0]);
418       }
419       return null;
420    }
421
422    /**
423     * This method is used to verify the values given by the user
424     * corresponding to join-condition specified.
425     * If the values given mismatchs the join condition, exception is thrown.
426     */

427
428    public void verifyValues(_VariableValueOperations vv) throws DException {
429       if (_booleanfactor0 instanceof JoinConditionTableGetter) {
430          ( (JoinConditionTableGetter) _booleanfactor0).verifyValues(vv);
431       }
432       if (_booleanterm2 instanceof JoinConditionTableGetter) {
433          ( (JoinConditionTableGetter) _booleanfactor0).verifyValues(vv);
434       }
435    }
436
437    /**
438     * This method returns all the children of this class. It is needed in
439     * Abstract classes, so as to move common methods in Abstract class.
440     * @return
441     */

442
443    public AbstractRowValueExpression[] getChilds() {
444       AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] { (AbstractRowValueExpression) (_booleanterm2), (AbstractRowValueExpression) (_booleanfactor0)};
445       return childs;
446    }
447
448    /**
449     * This method tells whether null predicate is present in the condition or not.
450     * This is needed while shifting the condition to single table in case of
451     * Loj/Roj to identify whether to shift condition or not. As if null predicate
452     * is present in condition on right table of LOJ and left table of Roj, then
453     * we can't shift the condition to single table level.
454     * @return
455     * @throws DException
456     */

457
458    public boolean isNullPredicate() throws DException {
459       return _booleanterm2.isNullPredicate() || _booleanfactor0.isNullPredicate();
460    }
461
462    /**
463     * This method returns the estimate of the rows remaining after the execution
464     * of this condition on passed number of rows.
465     * @param noOfRows
466     * @return
467     * @throws DException
468     */

469
470    public long getEstimatedRows(long noOfRows) throws DException {
471       long rowCount = _booleanterm2.getEstimatedRows(noOfRows);
472       return _booleanfactor0.getEstimatedRows(rowCount);
473    }
474
475    public String JavaDoc toString() {
476       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
477       sb.append(" ");
478       sb.append(_booleanterm2);
479       sb.append(" ");
480       sb.append(_SRESERVEDWORD12065439221);
481       sb.append(" ");
482       sb.append(_booleanfactor0);
483       return sb.toString();
484    }
485
486    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
487       booleantermSRESERVEDWORD1206543922booleanfactor tempClass = new booleantermSRESERVEDWORD1206543922booleanfactor();
488       tempClass._booleanfactor0 = (booleanfactor) _booleanfactor0.clone();
489       tempClass._SRESERVEDWORD12065439221 = (SRESERVEDWORD1206543922) _SRESERVEDWORD12065439221.clone();
490       tempClass._booleanterm2 = (booleanterm) _booleanterm2.clone();
491       try {
492          tempClass.getColumnDetails();
493       } catch (DException ex) {
494          throw new RuntimeException JavaDoc(ex.getMessage());
495       }
496       return tempClass;
497    }
498
499    public booleanvalueexpression getSingleTableCondition() throws DException {
500       if (checkForMultipleTables()) {
501          booleanvalueexpression bve1 = null;
502          if (_booleanterm2 instanceof _SingleTableCondition) {
503            ((_SingleTableCondition)_booleanterm2).getSingleTableCondition();
504          } else {
505             throw new DException("DSE0", new Object JavaDoc[] {"Not handled the condition " + _booleanterm2.getClass()});
506          }
507          booleanvalueexpression bve2 = _booleanfactor0.getSingleTableCondition();
508          return BVEPlanMerger.addAndConditions(bve1, bve2);
509       }
510       return this;
511    }
512
513 }
514
Popular Tags