KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > plan > set > SetOperatorPlan


1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.set;
2
3 import com.daffodilwoods.daffodildb.client.*;
4 import com.daffodilwoods.daffodildb.server.serversystem.*;
5 import com.daffodilwoods.daffodildb.server.sql99.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.set.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*;
10 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*;
11 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*;
12 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
13 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
14 import com.daffodilwoods.daffodildb.utils.comparator.*;
15 import com.daffodilwoods.database.resource.*;
16 import com.daffodilwoods.database.sqlinitiator.*;
17 import com.daffodilwoods.database.utility.*;
18
19 /**
20  *
21  * <p>Title: SetOperatorPlan</p>
22  * <p>Description:
23  * This class represents the plan for SetOperators like Union, Intersection,
24  * Except with two options All and Distinct. This plan is needed to obtain
25  * the resultset of set operator in optimal way.
26  * If Union/Except is mentioned then distinct is implicit.
27  * i) Let R be a row that is a duplicate of some row in T1 or of some row in T2 or both. Let
28  * m be the number of duplicates of R in T1 and let n be the number of duplicates of R in
29  * T2 where m >=0 and n >= 0
30  * ii) If DISTINCT is specified or implicit, then
31  * Case:
32  * 1) If UNION is specified, then
33  * Case:
34  * A) If m > 0 or n > 0, then T contains exactly one duplicate of R.
35  * B) Otherwise, T contains no duplicate of R.
36  * 2) If EXCEPT is specified, then
37  * Case:
38  * A) If m > 0 and n = 0, then T contains exactly one duplicate of R.
39  * B) Otherwise, T contains no duplicate of R.
40  * 3) If INTERSECT is specified, then
41  * Case:
42  * A) If m > 0 and n > 0, then T contains exactly one duplicate of R.
43  * B) Otherwise, T contains no duplicates of R.
44  * iii) If ALL is specified, then
45  * Case:
46  * 1) If UNION is specified, then the number of duplicates of R that T contains is (m+ n).
47  * 2) If EXCEPT is specified, then the number of duplicates of R that T contains is the
48  * maximum of (m - n ) and 0(Zero)
49  * 3) If INTERSECT is specified, then the number of duplicates of R that T
50  * contains is the minimum of m and n.
51  * </p>
52  * <p>Copyright: Copyright (c) 2003</p>
53  * <p>Company: Daffodil S/W Ltd.</p>
54  * @author SelectTeam
55  * @version 1.0
56  */

57
58 public class SetOperatorPlan extends PlanExceptionAbstract implements _TablePlanAdditional {
59
60    /**
61     * Represents the plan of left side query of set operator.
62     */

63
64    private _TablePlan queryExpressionBodyPlan;
65
66    /**
67     * Represents the plan of right side query of set operator.
68     */

69
70    private _TablePlan queryTermPlan;
71
72    /**
73     * Represents the cloned column of left side query of set operator.
74     */

75
76    private ColumnDetails[] columnDetails1;
77
78    /**
79     * Represents the cloned column of right side query of set operator.
80     */

81
82    private ColumnDetails[] columnDetails2;
83
84    /**
85     * Represents the selected column of left side query adjusted according to
86     * order.
87     */

88
89    private ColumnDetails[] orderLeftCD;
90
91    /**
92     * Represents the selected column of right side query adjusted according to
93     * order.
94     */

95
96    private ColumnDetails[] orderRightCD;
97
98    /**
99     * Represents the option(distinct/all) present with set operator.
100     */

101
102    private int distinctOrAll;
103
104    /**
105     * Represents the type(union/intersection/except) of set operator.
106     */

107
108    private int union_int_except;
109
110    /**
111     * Represents the object whose value is to be provide by user of this set
112     * operator.
113     */

114
115    private _Reference[] references;
116
117    /**
118     * Represents the comparator to compare the order of both queries involved in
119     * set operator.
120     */

121
122    private SuperComparator superComparator;
123
124    /**
125     * Represents the array of maximum data types for corresponding columns of
126     * both queries involved in set operator.
127     */

128
129    private int[] appropriateDataTypes;
130
131    /**
132     * Represents the array of maximum sizes for corresponding columns of
133     * both queries involved in set operator.
134     */

135
136
137    private int[] appropriateSizes;
138
139    /**
140     * Represents the actual selected column of left side query of set operator.
141     */

142
143    private ColumnDetails[] leftOriginal;
144
145    /**
146     * Represents the actual selected column of right side query of set operator.
147     */

148
149    private ColumnDetails[] rightOriginal;
150
151    /**
152     * Represents the order of left query.
153     */

154
155    private _Order leftOrder;
156
157    /**
158     * Represents the order of right query.
159     */

160
161    private _Order rightOrder;
162
163    public SetOperatorPlan(_TablePlan tablePlanLeft0, _TablePlan tablePlanRight0, int distinctOrAll0, int union_int_except0, _Reference[] references0, SuperComparator superComparator0, ColumnDetails[] columnDetailsLeft0, ColumnDetails[] columnDetailsRight0, int[] appropriateDataTypes0, int[] appropriateSizes0, ColumnDetails[] orderLeftCD0, ColumnDetails[] orderRightCD0) {
164       queryExpressionBodyPlan = tablePlanLeft0;
165       queryTermPlan = tablePlanRight0;
166       distinctOrAll = distinctOrAll0;
167       union_int_except = union_int_except0;
168       references = references0;
169       superComparator = superComparator0;
170       columnDetails1 = columnDetailsLeft0;
171       columnDetails2 = columnDetailsRight0;
172       orderLeftCD = orderLeftCD0;
173       orderRightCD = orderRightCD0;
174       childPlans = new _TablePlan[] {queryExpressionBodyPlan, queryTermPlan};
175       appropriateDataTypes = appropriateDataTypes0;
176       appropriateSizes = appropriateSizes0;
177    }
178
179    /**
180     * This method is used to Set the actual columns and order of both queries
181     * involved in set operator.
182     * @param leftOriginal0
183     * @param rightOriginal0
184     * @param leftOrder0
185     * @param rightOrder0
186     */

187
188    public void setOriginalColumnDetails(ColumnDetails[] leftOriginal0, ColumnDetails[] rightOriginal0, _Order leftOrder0, _Order rightOrder0) {
189       leftOriginal = leftOriginal0;
190       rightOriginal = rightOriginal0;
191       leftOrder = leftOrder0;
192       rightOrder = rightOrder0;
193    }
194
195    /**
196     * It is used to get the type of plan for merging purpose. It is not required
197     * for this plan, as it is never merged with some other plan.
198     * @return
199     * @throws DException
200     */

201
202    public int getType() throws DException {
203       throw new UnsupportedOperationException JavaDoc("MEthod not supported");
204    }
205
206    /**
207     * It allows user to obtain the estimate number of rows that will result
208     * after solving this plan.
209     * @param serverSession
210     * @return
211     * @throws DException
212     */

213
214    public long getRowCount(_ServerSession serverSession) throws DException {
215       long rowOfqueryexpressionbody = queryExpressionBodyPlan.getRowCount(serverSession);
216       long rowOfqueryprimary = queryTermPlan.getRowCount(serverSession);
217       long returnValue = 0;
218       switch (union_int_except) {
219          case SetOperatorConstants.UNION:
220             if (distinctOrAll == SetOperatorConstants.DISTINCT) {
221                returnValue = rowOfqueryexpressionbody > rowOfqueryprimary ? rowOfqueryexpressionbody : rowOfqueryprimary;
222             } else {
223                return rowOfqueryexpressionbody + rowOfqueryprimary;
224             }
225          case SetOperatorConstants.EXCEPT:
226          case SetOperatorConstants.INTERSECT:
227             returnValue = rowOfqueryexpressionbody > rowOfqueryprimary ? rowOfqueryprimary : rowOfqueryexpressionbody;
228       }
229       return returnValue;
230    }
231
232    /**
233     * It is used to check the plan of this set operator.
234     * @return
235     * @throws DException
236     */

237
238    public String JavaDoc getVerifier() throws DException {
239       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
240       inc();
241       buffer.append(tabW("[ SET_OPERATOR_PALN ]"));
242       buffer.append("\n" + queryTermPlan.getVerifier());
243       buffer.append("\n" + queryExpressionBodyPlan.getVerifier());
244       inc();
245       buffer.append(tab("[Distinct Check = " + superComparator + "]"));
246       buffer.append(tab("[ColumnDetailsLeft = " + P.print(columnDetails1)) + "]");
247       buffer.append(tab("[ColumnDetailsRight = " + P.print(columnDetails2)) + "]");
248       dec();
249       buffer.append(tab("[/ SET_OPERATOR_PALN ]"));
250       dec();
251       return buffer.toString();
252    }
253
254    /**
255     * Returns the plan of this set opearator.
256     * @usage Browser.
257     * @return
258     * @throws DException
259     */

260
261    public _QueryPlan getQueryPlan() throws DException {
262       _QueryPlan cplan = queryExpressionBodyPlan.getQueryPlan();
263       _QueryPlan[] cplans = cplan == null ? null : new _QueryPlan[] {cplan};
264       return new QueryPlan("QueryExpressionBodySRESERVEDWORD1206543922OptSetQuantifierOptCorrespondingSpecQueryTerm", cplans, "" + null, "" + null);
265    }
266
267    /**
268     * Returns the order present on this plan.
269     * @return
270     * @throws DException
271     */

272
273    public _Order getOrder() throws DException {
274       return null;
275    }
276
277    /**
278     * It is required to obtain the resultset of Union set operator. There may be
279     * three cases
280     * 1. When DISTINCT is present then UnionDistinct is returned
281     * 2. When ALL is present and order is not available then UnionAll is returned
282     * 3. When ALL is present and order is available then UnionAllOrdered is
283     * returned.
284     * @param session
285     * @param distinctOrAll
286     * @return
287     * @throws DException
288     */

289
290    private _SelectIterator getUnionSetOperatorObject(_ServerSession session, int distinctOrAll) throws DException {
291       _Iterator leftIterator = (_Iterator) queryExpressionBodyPlan.execute(session);
292       _Iterator rightIterator = (_Iterator) queryTermPlan.execute(session);
293       UnionAllOrderedIterator iterator = null;
294       if (distinctOrAll == SetOperatorConstants.DISTINCT) {
295          iterator = new UnionDistinctIterator(leftIterator, rightIterator, (_Reference[]) columnDetails1, (_Reference[]) columnDetails2, superComparator, appropriateDataTypes, appropriateSizes, (_Reference[]) orderLeftCD, (_Reference[]) orderRightCD, leftOrder.getComparator(), rightOrder.getComparator());
296       } else if (superComparator == null) {
297          iterator = new UnionAllIterator(leftIterator, rightIterator, (_Reference[]) columnDetails1, (_Reference[]) columnDetails2, appropriateDataTypes, appropriateSizes, (_Reference[]) orderLeftCD, (_Reference[]) orderRightCD);
298       } else {
299          iterator = new UnionAllOrderedIterator(leftIterator, rightIterator, (_Reference[]) columnDetails1, (_Reference[]) columnDetails2, superComparator, appropriateDataTypes, appropriateSizes, (_Reference[]) orderLeftCD, (_Reference[]) orderRightCD);
300       }
301       iterator.setOriginalColumnDetails(leftOriginal, rightOriginal);
302       return new SelectIterator(columnDetails1, iterator);
303     }
304
305    /**
306     * It is used to obtain the resultset of 'Except' set operator. It is present
307     * either with ALL or with DISTINCT option. Order will always be present for
308     * this set operator.
309     * @param session
310     * @param distinctOrAll
311     * @return
312     * @throws DException
313     */

314
315    private _SelectIterator getExceptSetOperatorObject(_ServerSession session, int distinctOrAll) throws DException {
316       _Iterator leftIterator = (_Iterator) queryExpressionBodyPlan.execute(session);
317       _Iterator rightIterator = (_Iterator) queryTermPlan.execute(session);
318       if (distinctOrAll == SetOperatorConstants.DISTINCT) {
319          return new SelectIterator(columnDetails1, new ExceptDistinctIterator(leftIterator, rightIterator, (_Reference[]) columnDetails1, (_Reference[]) columnDetails2, superComparator, orderLeftCD, orderRightCD, leftOrder.getComparator(), rightOrder.getComparator()));
320       }
321       return new SelectIterator(columnDetails1, new ExceptAllIterator(leftIterator, rightIterator, (_Reference[]) columnDetails1, (_Reference[]) columnDetails2, superComparator, orderLeftCD, orderRightCD, leftOrder.getComparator(), rightOrder.getComparator()));
322
323    }
324
325    /**
326     * It is used to obtain the resultset of 'Intersect' set operator. It is present
327     * either with ALL or with DISTINCT option. Order will always be present for
328     * this set operator.
329     * @param session
330     * @param distinctOrAll
331     * @return
332     * @throws DException
333     */

334
335    private _SelectIterator getIntersectSetOperatorObject(_ServerSession session, int distinctOrAll) throws DException {
336       _Iterator leftIterator = (_Iterator) queryExpressionBodyPlan.execute(session);
337       _Iterator rightIterator = (_Iterator) queryTermPlan.execute(session);
338       _SelectIterator selectIterator = distinctOrAll == SetOperatorConstants.DISTINCT ?
339           new SelectIterator(columnDetails1, new IntersectDistinctIterator(leftIterator, rightIterator, (_Reference[]) columnDetails1, (_Reference[]) columnDetails2, superComparator, orderLeftCD, orderRightCD)) :
340           new SelectIterator(columnDetails1, new IntersectAllIterator(leftIterator, rightIterator, (_Reference[]) columnDetails1, (_Reference[]) columnDetails2, superComparator, orderLeftCD, orderRightCD));
341       return selectIterator;
342    }
343
344    /**
345     * Returns all the children plans.
346     * @return
347     * @throws DException
348     */

349
350    public _TablePlan[] getChildTablePlans() throws DException {
351       childPlans = new _TablePlan[] {queryExpressionBodyPlan, queryTermPlan};
352       return new _TablePlan[] {this};
353    }
354
355    /**
356     * This method returns the cumulative cost of underlying query.
357     * @param session
358     * @return
359     * @throws DException
360     */

361
362    public double getCost(_ServerSession session) throws DException {
363       return queryExpressionBodyPlan.getCost(session) + queryTermPlan.getCost(session);
364    }
365
366    /**
367     * Returns the cost calculated. It includes the cost of underlying plan, cost
368     * of solving condition.
369     * @param session
370     * @param condition
371     * @param estimatedRows
372     * @return
373     * @throws DException
374     */

375
376    public double getCost(_ServerSession session, booleanvalueexpression condition, long estimatedRows) throws DException {
377       long rows = getRowCount(session);
378       double conditionCost = condition.getCost(rows, false);
379       return conditionCost + (queryTermPlan.getCost(session) + queryExpressionBodyPlan.getCost(session));
380
381    }
382
383    /**
384     * It allows user to obtain resultset based on the type of set operator.
385     * @param session
386     * @return
387     * @throws DException
388     */

389
390    public _Iterator execute(_ServerSession session) throws DException {
391       _SelectIterator selIter =
392           union_int_except == SetOperatorConstants.UNION
393           ? getUnionSetOperatorObject(session, distinctOrAll)
394           : union_int_except == SetOperatorConstants.EXCEPT ?
395           getExceptSetOperatorObject(session, distinctOrAll) :
396           getIntersectSetOperatorObject(session, distinctOrAll);
397       _Iterator iterator = (_Iterator) selIter;
398       return iterator;
399    }
400
401    /**
402     * It is needed to obtain resultset of this plan with passed condition. As
403     * condition is solved at this level, so firstly underlying resultset is
404     * obtained and after that condition is applied to that resultset.
405     * @param session
406     * @param condition
407     * @return
408     * @throws DException
409     */

410
411    public _Iterator execute(_ServerSession session, booleanvalueexpression condition) throws DException {
412       return getNonIndexedFilteredIterator(execute(session), session, condition);
413    }
414
415    /**
416     * The following methods delegate their call to their corresponding method,
417     * in which order is taken into consideration. It is done because order is
418     * needed to get the result properly.
419     */

420
421    public double getCostWithoutOrder(_ServerSession session, booleanvalueexpression condition, long estimatedRowCount) throws DException {
422       return getCost(session, condition, estimatedRowCount);
423    }
424
425    public double getCostWithoutOrder(_ServerSession session) throws DException {
426       return getCost(session);
427    }
428
429    public _Iterator executeWithOutOrder(_ServerSession session, booleanvalueexpression condition) throws DException {
430       return execute(session, condition);
431    }
432
433    public _Iterator executeWithoutOrder(_ServerSession session) throws DException {
434       return execute(session);
435    }
436    /**
437     * Returns the estimated number of rows which results from solving passed
438     * condition. Condition is evaluated on as many rows as equal to
439     * mulitiplication of passed row and estimated rows of this plan.
440     * @param conditionArg
441     * @param estimatedRowCount
442     * @param serverSession
443     * @return
444     * @throws DException
445     */

446
447    public long getEstimatedRows(booleanvalueexpression conditionArg, long estimatedRowCount, _ServerSession serverSession) throws DException {
448       return conditionArg.getEstimatedRows(estimatedRowCount*getRowCount(serverSession));
449    }
450
451    /**
452     * All the following methods belong to _TablePlanAdditional. For
453     * documentation of these methods, refer to documentation of
454     * _TablePlanAdditional. They deligate their calls to their corresponding
455     * execute methods.
456     */

457
458    public _Iterator executeWithOutOrderPlan(_ServerSession session, booleanvalueexpression condition) throws DException {
459       return executeWithOutOrder(session,condition);
460    }
461
462    public _Iterator executeWithoutOrderPlan(_ServerSession session) throws DException {
463       return executeWithoutOrder(session);
464    }
465
466    public _Iterator executePlan(_ServerSession session) throws DException {
467       return execute(session);
468    }
469
470    public _Iterator executePlan(_ServerSession session, booleanvalueexpression condition) throws DException {
471       return execute(session,condition);
472    }
473
474 }
475
Popular Tags