KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > plan > table > QuerySpecificationPlan


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.serversystem.*;
7 import com.daffodilwoods.daffodildb.server.sql99.common.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*;
11 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*;
12 import com.daffodilwoods.daffodildb.server.sql99.dql.queryexpression.
13     queryspecification.*;
14 import com.daffodilwoods.daffodildb.server.sql99.dql.semanticchecker.*;
15 import com.daffodilwoods.daffodildb.server.sql99.dql.tableexpression.*;
16 import com.daffodilwoods.daffodildb.server.sql99.expression.
17     booleanvalueexpression.*;
18 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
19 import com.daffodilwoods.daffodildb.utils.field.*;
20 import com.daffodilwoods.database.resource.*;
21 import com.daffodilwoods.database.sqlinitiator.*;
22 import com.daffodilwoods.database.utility.P;
23 import com.daffodilwoods.daffodildb.server.sql99.expression.
24
    booleanvalueexpression.predicates.inpredicate;
25 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.
26
    BVEPlanMerger;
27 import com.daffodilwoods.daffodildb.server.sql99.utils._VariableValues;
28 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary.
29
    subquery;
30 import com.daffodilwoods.daffodildb.client.QueryPlan;
31 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator._Iterator;
32
33 /**
34  *
35  * <p>Title: QuerySpecificationPlan</p>
36  * <p>Description:
37  * This class represents the plan of simple SelectQuery, simple means the select
38  * query excluding Set Operators. It includes select list, from clause, where
39  * clause, group by clause, having clause.
40  * This class delegates all the function call of _tablePlan to underlying plan,
41  * and just making the provision of order present in this class.
42  * </p>
43  * <p>Copyright: Copyright (c) 2003</p>
44  * <p>Company: Daffodil S/W Ltd.</p>
45  * @author unascribed
46  * @version 1.0
47  */

48
49 public class QuerySpecificationPlan
50     extends PlanExceptionAbstract
51     implements _TablePlanAdditional {
52
53   /**
54    * Represents the underlying plan.
55    */

56
57   private _TablePlan tablePlan;
58
59   /**
60    * Represents the columns of select list.
61    */

62
63   private ColumnDetails[] selectListColumnDetails;
64
65   /**
66    * Represents those columns of select whose values are to be provided by user.
67    */

68
69   private _Reference[] selectReferences;
70
71   /**
72    * Represents the top function. It is optional.
73    */

74
75   private topfunction _Opttopfunction2;
76
77   /**
78    * Represents the object which is used for semantic checking.
79    */

80
81   private SemanticChecker semanticChecker;
82
83   /**
84    * Represents an object which represents from clause, where clause,
85    * group by clause, having clause.
86    */

87
88   private tableexpression tableExpression;
89
90   /**
91    * Represents the order which will be applied at the top of this plan.
92    */

93
94   private _Order order; // order can't be shifted to tableexpression Level
95

96   /**
97    * Represents those columns whose values are to be provided by the resulset
98    * of this plan.
99    */

100
101   private _Reference[] underlyingReferencesSolvableByCurrentIterator;
102
103   /**
104    * Represents the object to whom this plan belongs to.
105    */

106
107   private queryspecification qs;
108
109   private _Reference[] subqueryReferences;
110
111   public QuerySpecificationPlan(_TablePlan tPlan,
112                                 ColumnDetails[] selectListCD,
113                                 /* _ServerSession session0,*/
114                                 SemanticChecker semanticChecker0,
115                                 _Reference[] selectReferences0,
116                                 topfunction _Opttopfunction20,
117                                 tableexpression tableExpression0,
118                                 _Reference[]
119                                 underlyingReferencesSolvableByCurrentIterator0,
120                                 queryspecification qs) throws DException {
121     this.qs = qs;
122     tablePlan = tPlan; // underLyingPlan
123
selectListColumnDetails = selectListCD;
124     tableExpression = tableExpression0;
125     selectReferences = selectReferences0; // orderByReferences + selectListReferences
126
_Opttopfunction2 = _Opttopfunction20;
127     semanticChecker = semanticChecker0; // semanticChecker for countCD and sum, count column in case of avg.
128
underlyingReferencesSolvableByCurrentIterator =
129         underlyingReferencesSolvableByCurrentIterator0;
130     getChildTablePlans();
131
132   }
133
134   /**
135    * Returns the cost of this plan. Cost includes the cost of underlying plan
136    * as well as cost of applying order.
137    * @param session
138    * @return
139    * @throws DException
140    */

141
142   public double getCost(_ServerSession session) throws DException {
143     return addCostForOrder(tablePlan.getCost(session), session);
144   }
145
146   /**
147    * This method adds the cost of applying order to the passed cost.
148    * @param cost
149    * @param serverSession
150    * @return
151    * @throws DException
152    */

153
154   private double addCostForOrder(double cost, _ServerSession serverSession) throws
155       DException {
156     return order == null
157         ? cost
158         : cost + getCostForOrder(getRowCount(serverSession));
159   }
160
161   /**
162    * This method is used to obtain the cost of applying order on passed number
163    * of rows.
164    * @param rows
165    * @return
166    * @throws DException
167    */

168
169   private double getCostForOrder(double rows) throws DException {
170     return CostCalculator.getCostForTemporaryOrder(rows);
171   }
172
173   /**
174    * This method is used to obtain the estimate number of rows that are
175    * contained in the resultset of this plan.
176    * @param serverSession
177    * @return
178    * @throws DException
179    */

180
181   public long getRowCount(_ServerSession serverSession) throws DException {
182     return tablePlan.getRowCount(serverSession);
183   }
184
185   /**
186    * This method returns TemporaryIndexIterator if Order is present Else
187    * return iterator as such.
188    * @param session
189    * @param iterator
190    * @return
191    * @throws DException
192    */

193   private _Iterator getOrderedIterator(_ServerSession session,
194                                        _Iterator iterator,
195                                        _Reference[] subQueryRef,
196                                        _Iterator[] iterator1) throws DException {
197     return order == null
198         ? iterator
199         :
200         GeneralPurposeStaticClass.getTemporaryIndexIteratorForSubQuery(iterator,
201         tablePlan,
202         session, order, subQueryRef, iterator1);
203   }
204
205   /**
206    * This method is used to obtain the resultset for this plan. An sorted
207    * resultset is formed on the resultset of underlying plan. And finally
208    * resultset based on the type of RESULTSET is formed and returned.
209    * @param session
210    * @return
211    * @throws DException
212    */

213   public _Iterator execute(_ServerSession session) throws DException {
214     semanticChecker.checkSelectListDistinctSemantic();
215     _Iterator iter = tablePlan.execute(session);
216     _Iterator[] iter1 = getIteratorForSubQuery(iter, session);
217     iter = getOrderedIterator(session, iter, subqueryReferences, iter1);
218     return getAppropriateIterator(session, iter, false, iter1,
219                                   subqueryReferences);
220
221   }
222
223   /**
224    * This method is used to obtain the resultset for this plan based on the
225    * passed condition and number of rows. A resultset based on the type of
226    * RESULTSET is formed and returned. Order is not considered in this plan.
227    * @param session
228    * @param condition
229    * @return
230    * @throws DException
231    */

232
233   public _Iterator executeWithOutOrder(_ServerSession session,
234                                        booleanvalueexpression condition) throws
235       DException {
236     semanticChecker.checkSelectListDistinctSemantic();
237     _Iterator iter = tablePlan.executeWithOutOrder(session, condition);
238     _Iterator[] iter1 = getIteratorForSubQuery(iter, session);
239     return getAppropriateIterator(session, iter, false, iter1,
240                                   subqueryReferences);
241   }
242
243   /**
244    * This method is used to obtain the resultset for this plan . A resultset
245    * based on the type of RESULTSET is formed and returned. Order is not
246    * considered for this method.
247    * @param session
248    * @return
249    * @throws DException
250    */

251
252   public _Iterator executeWithoutOrder(_ServerSession session) throws
253       DException {
254     semanticChecker.checkSelectListDistinctSemantic();
255     _Iterator iter = tablePlan.executeWithoutOrder(session);
256     _Iterator[] iter1 = getIteratorForSubQuery(iter, session);
257     return getAppropriateIterator(session, iter, false, iter1,
258                                   subqueryReferences);
259   }
260
261   /**
262    * This method is used to obtain the resultset for this plan based on the
263    * passed condition and number of rows. A resultset based on the type of
264    * RESULTSET is formed and returned.
265    * @param session
266    * @param condition
267    * @return
268    * @throws DException
269    */

270
271   public _Iterator execute(_ServerSession session,
272                            booleanvalueexpression condition) throws DException {
273     semanticChecker.checkSelectListDistinctSemantic();
274     _Iterator iter = tablePlan.execute(session, condition);
275     _Iterator[] iter1 = getIteratorForSubQuery(iter, session);
276     iter = getOrderedIterator(session, iter, subqueryReferences, iter1);
277     return getAppropriateIterator(session, iter, false, iter1,
278                                   subqueryReferences);
279   }
280
281   /**
282    * This method is used to obtain the cost of this plan based on the passed
283    * condition and number of rows. Order is not considered in cost calculation.
284    * @param session
285    * @param condition
286    * @param estimatedRowCount
287    * @return
288    * @throws DException
289    */

290
291   public double getCostWithoutOrder(_ServerSession session,
292                                     booleanvalueexpression condition,
293                                     long estimatedRowCount) throws DException {
294     return tablePlan.getCostWithoutOrder(session, condition, estimatedRowCount);
295   }
296
297   /**
298    * This method is used to obtain the cost of this plan. Order is not
299    * considered in cost calculation.
300    * @param session
301    * @return
302    * @throws DException
303    */

304
305   public double getCostWithoutOrder(_ServerSession session) throws DException {
306     return tablePlan.getCostWithoutOrder(session);
307   }
308
309   /**
310    * This method is used to obtain the cost of this plan based on the passed
311    * condition and number of rows. Resultant cost also includes the cost of
312    * applying order.
313    * @param condition
314    * @param estimatedRows
315    * @return
316    * @throws DException
317    */

318
319   public double getCost(_ServerSession session,
320                         booleanvalueexpression condition, long estimatedRows) throws
321       DException {
322     return addCostForOrder(tablePlan.getCost(session, condition, estimatedRows),
323                            session);
324   }
325
326   public String JavaDoc getVerifier() throws DException {
327     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
328     inc();
329     buffer.append(tabW("[ QUERY_SPECIFICATION_PLAN ]"));
330     buffer.append("\n" + tablePlan.getVerifier()).toString();
331     buffer.append(tab("[/ QUERY_SPECIFICATION_PLAN ]"));
332     dec();
333     return buffer.toString();
334   }
335
336   /**
337    * Return the plan for QuerySpecification.
338    * @return
339    * @throws DException
340    */

341
342   public _QueryPlan getQueryPlan() throws DException {
343     _QueryPlan cplan = tablePlan.getQueryPlan();
344     _QueryPlan[] cplans = cplan == null ? null : new _QueryPlan[] {
345         cplan};
346     return new QueryPlan("QuerySpecification", cplans, "" + this, null);
347   }
348
349   /**
350    * Returns the indexes of hasRecord columns.
351    * @return
352    * @throws DException
353    */

354
355   public int[] getHasRecordIndexes() throws DException {
356     ArrayList list = new ArrayList();
357     for (int i = 0, length = selectListColumnDetails.length; i < length; i++) {
358       if (selectListColumnDetails[i].getType() == ColumnDetails.HAS_RECORD) {
359         list.add(new Integer JavaDoc(i));
360       }
361     }
362     return list.isEmpty() ? null : getIntFromList(list);
363   }
364
365   /**
366    * Coverts Integer object present in array list into array of int.
367    * @param list
368    * @return
369    * @throws DException
370    */

371
372   private int[] getIntFromList(ArrayList list) throws DException {
373     int size = list.size();
374     int[] indexes = new int[size];
375     for (int i = 0; i < size; i++) {
376       indexes[i] = list.get(i).hashCode();
377     }
378     return indexes;
379   }
380
381   /**
382    * This method is used to obtain the resultset based on the type of resultset.
383    * It may be -
384    * 1. SCROLLABLE, 2. NONSCROLLABLE, 3. UPDATABLE.
385    * This method check type of session
386    * if type is
387    * 1. NonScrollable:-Initialize Constructor of SelectedColumnIterator and return it
388    * 2. Scrollable:-Intialize Constructor of SelectColumnIterator with type
389    * and return it.
390    * 3. Updatable:- Intialize Constructor of UpdatableSelectIterator and return
391    * it.
392    *
393    *
394    * @param session
395    * @param iterator
396    * @param isView
397    * @return
398    * @throws DException
399    */

400
401   private _Iterator getAppropriateIterator(_ServerSession session,
402                                            _Iterator iterator, boolean isView,
403                                            _Iterator[] iter, _Reference[] ref) throws
404       DException {
405
406     int type = session.getType();
407     iterator = getIterator(iterator);
408     TableDetails[] tableDetails = tableExpression.getTableDetails();
409     switch (type) {
410       case IteratorConstants.NONSCROLLABLE:
411         return new SelectedColumnIterator(selectListColumnDetails, iterator,
412                                           selectReferences, tableDetails,
413                                           isView,
414                                           underlyingReferencesSolvableByCurrentIterator,
415                                           session, qs, ref, iter);
416       case IteratorConstants.SCROLLABLE:
417         return new SelectedColumnIterator(selectListColumnDetails, iterator,
418                                           selectReferences, tableDetails,
419                                           isView, type,
420                                           underlyingReferencesSolvableByCurrentIterator,
421                                           session, qs, ref, iter);
422       case IteratorConstants.UPDATABLE:
423         ArrayList list = new ArrayList();
424         semanticChecker.getGroupByPrimaryColumns(list);
425         String JavaDoc[] groupByPrimaryColumns = list.isEmpty() ? null :
426             (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
427         /*change select list column details according to the view's underlying table details */
428         ColumnDetails[] changedCD = changeSelectedColumnForView(
429             selectListColumnDetails, session);
430
431         return new UpdatableSelectIterator(changedCD, iterator,
432                                            selectReferences,
433                                            tableExpression.getAllTableDetails(),
434                                            tableExpression.getViewTableDetails(),
435                                            groupByPrimaryColumns, isView,
436                                            tableExpression, session,
437                                            underlyingReferencesSolvableByCurrentIterator,
438                                            qs, ref, iter);
439       default:
440         throw new DException("DSE3522", new Object JavaDoc[] {new Integer JavaDoc(type)});
441     }
442   }
443
444   /**
445    * this method check each columnDeatils in Selectlist.If column in selectlist
446    * of type constant and column is rownum then add to arraylist.
447    * and initialize counterIterator with these references If top function is
448    * present in selectlist we create wrapper of TopIterator on iterator else return
449    * iterator as such.
450    * @param iter
451    * @return
452    * @throws DException
453    */

454
455   private _Iterator getIterator(_Iterator iter) throws DException {
456     ArrayList list = new ArrayList();
457     for (int i = 0; i < selectListColumnDetails.length; i++) {
458       if (selectListColumnDetails[i].getType() == ColumnDetails.CONSTANT &&
459           selectListColumnDetails[i].getOriginalColumn().equalsIgnoreCase(
460           "rownum")) {
461         list.add(selectListColumnDetails[i]);
462       }
463     }
464     if (list.size() > 0) {
465       _Reference[] refs = (_Reference[]) list.toArray(new _Reference[list.size()]);
466       iter = new CounterIterator(iter, refs);
467     }
468     return _Opttopfunction2 != null ?
469         new TopIterator(iter, (FieldBase) _Opttopfunction2.run(null)) : iter;
470   }
471
472   /**
473    * Sets the order.
474    * @param order0
475    * @throws DException
476    */

477
478   public void setOrder(_Order order0) throws DException {
479     order = order0;
480   }
481
482   /**
483    * Returns the order present in this plan.
484    * @return
485    * @throws DException
486    */

487
488   public _Order getOrder() throws DException {
489     return order;
490   }
491
492   /**
493    * Returns the QuerySpecificationPlan.
494    * @return
495    * @throws DException
496    */

497
498   public _TablePlan[] getChildTablePlans() throws DException {
499     childPlans = tablePlan.getChildTablePlans();
500     initializeTableDetails();
501     return new _TablePlan[] {
502         this};
503   }
504
505   /**
506    * This method is called when 'For Update' option is present in select query.
507    * It initializes the flag in underlying plan which will be TableExpressionPlan.
508    */

509
510   public void setForUpdate() {
511     ( (TableExpressionPlan) tablePlan).setForUpdate();
512   }
513
514   /**
515    * The following methods are similar to their corresponding execute methods.
516    * For documentation of their purpose refer to documentation of
517    * _TablePlanAdditional.
518    */

519
520   public _Iterator executePlan(_ServerSession session) throws DException {
521     semanticChecker.checkSelectListDistinctSemantic();
522     _Iterator iter = tablePlan.execute(session);
523     _Iterator[] iter1 = getIteratorForSubQuery(iter, session);
524     iter = getOrderedIterator(session, iter, subqueryReferences, iter1);
525     return getAppropriateIterator(session, iter, true, iter1,
526                                   subqueryReferences);
527   }
528
529   public _Iterator executeWithOutOrderPlan(_ServerSession session,
530                                            booleanvalueexpression condition) throws
531       DException {
532     semanticChecker.checkSelectListDistinctSemantic();
533     _Iterator iter = tablePlan.executeWithOutOrder(session, condition);
534     _Iterator[] iter1 = getIteratorForSubQuery(iter, session);
535     return getAppropriateIterator(session, iter, true, iter1,
536                                   subqueryReferences);
537   }
538
539   public _Iterator executeWithoutOrderPlan(_ServerSession session) throws
540       DException {
541     semanticChecker.checkSelectListDistinctSemantic();
542     _Iterator iter = tablePlan.executeWithoutOrder(session);
543     _Iterator[] iter1 = getIteratorForSubQuery(iter, session);
544     return getAppropriateIterator(session, iter, true, iter1,
545                                   subqueryReferences);
546   }
547
548   public _Iterator executePlan(_ServerSession session,
549                                booleanvalueexpression condition) throws
550       DException {
551
552     semanticChecker.checkSelectListDistinctSemantic();
553     _Iterator iter = tablePlan.execute(session, condition);
554     _Iterator[] iter1 = getIteratorForSubQuery(iter, session);
555     iter = getOrderedIterator(session, iter, subqueryReferences, iter1);
556     return getAppropriateIterator(session, iter, true, iter1,
557                                   subqueryReferences);
558   }
559
560   /* changing the selected column details for view. */
561   public ColumnDetails[] changeSelectedColumnForView(ColumnDetails[]
562       selectListColumnDetails,
563       _ServerSession session) throws DException {
564     ColumnDetails[] newCD = new ColumnDetails[selectListColumnDetails.length];
565     for (int i = 0; i < selectListColumnDetails.length; i++) {
566       TableDetails td = selectListColumnDetails[i].getTableDetails();
567       if (td == null || td.getTableType() != TableDetails.VIEW)
568         newCD[i] = selectListColumnDetails[i];
569       else {
570         ViewObject viewObject = (ViewObject) session.getViewObject(td.
571             getQualifiedIdentifier(), false);
572         ColumnDetails[] cd = viewObject.getQueryExpression().getSelectedColumns();
573         String JavaDoc columnName = selectListColumnDetails[i].getColumn();
574         for (int j = 0; j < cd.length; j++) {
575           String JavaDoc viewColumnName = cd[j].getAppropriateColumn();
576           if (columnName.equalsIgnoreCase(viewColumnName)) {
577             newCD[i] = cd[j];
578             break;
579           }
580         }
581         if (newCD[i] == null)
582          ;//// Removed By Program ** System.out.println("column Name not found " + columnName);
583
}
584     }
585     return newCD;
586   }
587
588   public _Iterator[] getIteratorForSubQuery(_Iterator iterator,
589                                              _ServerSession serverSession) throws
590     DException {
591     ArrayList list = new ArrayList();
592     ArrayList al = new ArrayList();
593     TableDetails[] allTableDetails = tableExpression.getTableDetails();
594     if (selectReferences != null) {
595       for (int i = 0; i < selectReferences.length; i++) {
596         if (selectReferences[i].getReferenceType() == SimpleConstants.SUBQUERY) {
597           list.add(selectReferences[i]);
598           al.add( ( (subquery) selectReferences[i]).getSelectIterator(
599               serverSession));
600         }
601       }
602       if (list.size() > 0) {
603         subqueryReferences = (_Reference[]) list.toArray(new _Reference[list.
604             size()]);
605       }
606     }
607
608     /*-----------------FOR SCALARSUBQUERY IN SELCTLIST-------------*/
609     if (subqueryReferences != null) {
610       for (int i = 0; i < subqueryReferences.length; i++) {
611         _Iterator subQueryIterator = (_Iterator)al.get(i);
612         _Reference[] ref = GeneralPurposeStaticClass.getAllReferences(new
613             _Reference[] {subqueryReferences[i]});
614         ref = GeneralPurposeStaticClass.getUnderLyingReferencesOnly(ref,
615             allTableDetails);
616         if (ref != null) {
617           Object JavaDoc[] values = new Object JavaDoc[ref.length];
618           Arrays.fill(values, iterator);
619           subQueryIterator.setConditionVariableValue(ref, values, 0);
620         }
621       }
622     }
623     return (_Iterator[]) al.toArray(new _Iterator[al.size()]);
624   }
625 }
626
627
628
629
630
Popular Tags