KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.dql.plan.table;
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.plan.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*;
9 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.order.*;
10 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
11 import com.daffodilwoods.database.resource.*;
12 import com.daffodilwoods.database.sqlinitiator.*;
13 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference;
14 import com.daffodilwoods.database.utility.P;
15
16 /**
17  *
18  * <p>Title: TableExpressionPlan</p>
19  * <p>Description:
20  * It represents the plan for condition and sorting needs of select query. It
21  * includes the following constructs of select qeury
22  * 1. From Clause, 2. Where Clause, 3. Group By Clause, 4. having clause,
23  * 5. order by clause.
24  * It provides the functionality of obtaining resultset.
25  * </p>
26  * <p>Copyright: Copyright (c) 2003</p>
27  * <p>Company: Daffodil S/W Ltd.</p>
28  * @author SelectTeam
29  * @version 1.0
30  */

31
32 public class TableExpressionPlan extends PlanExceptionAbstract {
33
34    /**
35     * Represents underlying plan.
36     */

37
38    private _TablePlan finalTablePlan;
39
40    /**
41     * Represents the order which will be applied on the resultset provides by
42     * this plan.
43     */

44
45    private _Order order;
46
47    /**
48     * Represents the plan of condition.
49     */

50
51    private _BVEPlan bveResultantPlan;
52
53
54    private _Reference[] whereClauseUnderlyingReferences;
55
56    private _Reference[] havingClauseUnderlyingReferences;
57
58    /**
59     * This constructor initializes the bveResultantPlan finalTablePlan
60     * if order is not null then
61     * it intializes order by calling the joinlevelorder of OrderPlan
62     * class.
63     * @param finalTablePlan0
64     * @param bveResultantPlan0
65     * @param orderPlan0
66     * @throws DException
67     */

68
69    public TableExpressionPlan(_TablePlan finalTablePlan0, _BVEPlan bveResultantPlan0, _OrderPlan orderPlan0,_Reference[] whereClauseUnderlyingReferences0,_Reference[] havingClauseUnderlyingReferences0) throws DException {
70       bveResultantPlan = bveResultantPlan0;
71       finalTablePlan = finalTablePlan0;
72       if (orderPlan0 != null) {
73          order = orderPlan0.getJoinLevelOrder();
74       }
75       whereClauseUnderlyingReferences=whereClauseUnderlyingReferences0;
76       havingClauseUnderlyingReferences=havingClauseUnderlyingReferences0;
77    }
78
79 /* public TableExpressionPlan(_TablePlan finalTablePlan0, _BVEPlan bveResultantPlan0, _OrderPlan orderPlan0) throws DException {
80       bveResultantPlan = bveResultantPlan0;
81       finalTablePlan = finalTablePlan0;
82       if (orderPlan0 != null) {
83          order = orderPlan0.getJoinLevelOrder();
84       }
85    }*/

86
87    /**
88     * This method is used to calculate the cost of obtaining resultset from this
89     * plan. Resultant cost includes the cost of underlying plan, cost of solving
90     * condition and cost of applying order.
91     * @param session
92     * @return
93     * @throws DException
94     */

95
96    public double getCost(_ServerSession session) throws DException {
97       return addCostForJoinOrder(addCostForRemainingCondition(finalTablePlan.getCost(session), session), session);
98    }
99
100    /**
101     * This method is used to calculate the cost of obtaining resultset from this
102     * plan based on the passed condition. Resultant cost includes the cost of
103     * underlying plan based on passed condition, cost of solving condition and
104     * cost of applying order.
105     * @param session
106     * @param condition
107     * @param estimatedRows
108     * @return
109     * @throws DException
110     */

111
112    public double getCost(_ServerSession session, booleanvalueexpression condition, long estimatedRows) throws DException {
113       return addCostForJoinOrder(addCostForRemainingCondition(finalTablePlan.getCost(session, condition, estimatedRows), estimatedRows), session);
114    }
115
116    /**
117     * This method is used to return estimated number of rows.
118     * @param serverSession
119     * @return
120     * @throws DException
121     */

122
123    public long getRowCount(_ServerSession serverSession) throws DException {
124       return finalTablePlan.getRowCount(serverSession);
125    }
126
127    /**
128     * This method is used to obtain resultset for this plan. If any condition is
129     * present on this plan, then filtered resultset is formed on underlying
130     * resultset. And if any order is present then sorted resultset is also
131     * formed and returned.
132     * @param session
133     * @return
134     * @throws DException
135     */

136
137    public _Iterator execute(_ServerSession session) throws DException {
138       _Iterator iterator = finalTablePlan.execute(session);
139       return makeIteratorForOrderAndRemainingCondition(session, iterator);
140    }
141
142
143    /**
144     * This method is used to obtain the resultset based on passed condition.
145     * This method execute finalTablePlan and take iterator then make iterator
146     * according to condition and Order present.
147     * @param session
148     * @param condition
149     * @return
150     * @throws DException
151     */

152
153    public _Iterator execute(_ServerSession session, booleanvalueexpression condition) throws DException {
154       _Iterator iterator = finalTablePlan.execute(session, condition);
155       return makeIteratorForOrderAndRemainingCondition(session, iterator);
156    }
157
158    public String JavaDoc getVerifier() throws DException {
159       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
160       inc();
161       buffer.append(tabW("[ TABLE_EXPRESSION_PLAN ]"));
162       buffer.append("\n" + finalTablePlan.getVerifier());
163       inc();
164       buffer.append(tab("[RemainingCondition =\"" + bveResultantPlan.getRemainingCondition() + "\"]"));
165       buffer.append(tab("[Order =\"" + order + "\"]"));
166       dec();
167       buffer.append(tab("[/ TABLE_EXPRESSION_PLAN ]"));
168       dec();
169       return buffer.toString();
170    }
171
172    /**
173     * Returns the plan for TableExpression.
174     * @return
175     * @throws DException
176     */

177
178    public _QueryPlan getQueryPlan() throws DException {
179       int length = 0; //tablePlans.length;
180
_QueryPlan[] cplans = new _QueryPlan[1];
181       cplans[0] = finalTablePlan.getQueryPlan();
182       String JavaDoc cond = bveResultantPlan.getRemainingCondition() == null ? null : "" + bveResultantPlan.getRemainingCondition();
183       String JavaDoc cond1 = bveResultantPlan.getAggregateCondition() == null ? null : "" + bveResultantPlan.getAggregateCondition();
184       String JavaDoc ord = order == null ? null : "" + order;
185       cond = cond == null ? cond1 : cond1 == null ? cond : cond + " <::> " + cond1;
186       return new QueryPlan("TableExpression " + this, cplans, cond, ord);
187    }
188
189    /**
190     * This method allows user to obtain Filtered resultset if any condition is
191     * present in this plan.
192     * @param session
193     * @param iterator
194     * @return
195     * @throws DException
196     */

197
198    private _Iterator executeWithBVE(_ServerSession session, _Iterator iterator) throws DException {
199
200       if( bveResultantPlan.getRemainingCondition() != null )
201          iterator = isRemainingConditionNotNull()==false? iterator : getNonIndexedFilteredIteratorWithSubQuery(iterator, session, bveResultantPlan.getRemainingCondition());
202       if( whereClauseUnderlyingReferences != null )
203          iterator.setSpecificUnderlyingReferences(whereClauseUnderlyingReferences);
204
205       if(bveResultantPlan.getRemainingCondition()!=null){
206          _Reference[] ref = GeneralPurposeStaticClass.getUnderLyingReferencesOnly(GeneralPurposeStaticClass.getAllReferences(bveResultantPlan.getRemainingCondition().getReferences(finalTablePlan.getTableDetails())), finalTablePlan.getTableDetails());
207          if(ref!=null)
208             iterator.setSpecificUnderlyingReferences(ref);
209       }
210       return iterator;
211    }
212
213    /**
214     * This method is used to check whether any condition is present in this plan
215     * or not.
216     * @return
217     * @throws DException
218     */

219
220    private boolean isRemainingConditionNotNull() throws DException {
221       return bveResultantPlan != null && bveResultantPlan.getRemainingCondition() != null;
222    }
223
224    /**
225     * This method add cost of solving remaining condition if resultant bveplan
226     * contains remaining condtion.
227     * @param cost
228     * @param serverSession
229     * @return
230     * @throws DException
231     */

232
233    private double addCostForRemainingCondition(double cost, _ServerSession serverSession) throws DException {
234       if (isRemainingConditionNotNull()) {
235          return addCostForRemainingCondition(cost, getRowCount(serverSession));
236       }
237       return cost;
238    }
239
240    /**
241     * This method is used to add cost, if any condition is present which has to
242     * be solved at this level.
243     * @param cost
244     * @param rows
245     * @return
246     * @throws DException
247     */

248
249    private double addCostForRemainingCondition(double cost, long rows) throws DException {
250       return isRemainingConditionNotNull()
251           ? bveResultantPlan.getRemainingCondition().getCost(rows, false) + cost
252           : cost;
253    }
254
255    /**
256     * This method is used to add the cost of applying order on the resultset.
257     * @param cost
258     * @param serverSession
259     * @return
260     * @throws DException
261     */

262
263    private double addCostForJoinOrder(double cost, _ServerSession serverSession) throws DException {
264       return order == null ? cost
265           : cost + getCostForOrder(getRowCount(serverSession));
266    }
267
268    /**
269     * this method add cost of TemporaryOrder if TemporaryOrder have to make to
270     * execute tableexpressionPlan.
271     * e.g Select * from A group by a1
272     * In this first we have to make temporary Order on a1 then grouping.now extra
273     * cost of making temporary order r to be added.
274     * @param rows
275     * @return
276     * @throws DException
277     */

278
279    private double getCostForOrder(long rows) throws DException {
280       return CostCalculator.getCostForTemporaryOrder(rows);
281    }
282
283    /**
284     * This method is used to obtain sorted resultset if any order has to be
285     * applied on this plan.
286     * @param session
287     * @param iterator
288     * @return
289     * @throws DException
290     */

291
292    private _Iterator makeIteratorForOrder(_ServerSession session, _Iterator iterator) throws DException {
293       if (order != null) {
294          return GeneralPurposeStaticClass.getTemporaryIndexIterator(iterator, finalTablePlan, session, order);
295       }
296       return iterator;
297    }
298
299    /**
300     * This is used to form resultset for condition and order present on this
301     * plan.
302     * @param session
303     * @param iterator
304     * @return
305     * @throws DException
306     */

307
308    private _Iterator makeIteratorForOrderAndRemainingCondition(_ServerSession session, _Iterator iterator) throws DException {
309       iterator = executeWithBVE(session, iterator);
310       iterator = makeIteratorForOrder(session, iterator);
311       return iterator;
312    }
313
314    /**
315     * Returns underlying plans.
316     * @return
317     * @throws DException
318     */

319
320    public _TablePlan[] getChildTablePlans() throws DException {
321       childPlans = finalTablePlan.getChildTablePlans();
322       initializeTableDetails();
323       return new _TablePlan[] {this};
324    }
325
326    /**
327     * This method is used to set whether aggregate columns can be solved at
328     * single table level or not in SingleTablePlan. It acts as intermediator to
329     * delegate the call to SingleTablePlan.
330     * @param columns
331     * @param session
332     * @return
333     * @throws DException
334     */

335
336    public boolean setAggregateColumnsForIndexing(ColumnDetails[] columns, _ServerSession session) throws DException {
337       return ( (SingleTablePlan) finalTablePlan).setAggregateColumnsForIndexing(columns, session);
338    }
339
340    /**
341     * This method is used to set whether 'For Update' option is present in
342     * Select Query or not in SingleTablePlan. It acts as intermediator to
343     * delegate the call to SingleTablePlan.
344     */

345
346    public void setForUpdate() {
347       ( (SingleTablePlan) finalTablePlan).setForUpdate();
348    }
349
350    /**
351     * This method will never be called for this plan.
352     * @return
353     * @throws DException
354     */

355
356    public int getType() throws DException {
357       throw new UnsupportedOperationException JavaDoc("Method not supported");
358    }
359
360    /**
361     * The following methods are same as corresponding counterparts execute
362     * methods. In these method executeWithoutOrder is called instead of execute
363     * methods.
364     */

365
366    public _Iterator executeWithoutOrder(_ServerSession session) throws DException {
367       _Iterator iterator = finalTablePlan.executeWithoutOrder(session);
368       return makeIteratorForOrderAndRemainingCondition(session, iterator);
369    }
370
371    public _Iterator executeWithOutOrder(_ServerSession session, booleanvalueexpression condition) throws DException {
372       _Iterator iterator = finalTablePlan.executeWithOutOrder(session, condition);
373       return executeWithBVE(session, iterator);
374    }
375
376    /**
377     * The following methods are same as corresponding counterparts getCost
378     * methods. In these method getCostWithoutOrder is called instead of getCost
379     * methods.
380     */

381
382    public double getCostWithoutOrder(_ServerSession session, booleanvalueexpression condition, long estimatedRowCount) throws DException {
383       return addCostForRemainingCondition(finalTablePlan.getCostWithoutOrder(session, condition, estimatedRowCount), estimatedRowCount);
384    }
385
386    public double getCostWithoutOrder(_ServerSession session) throws DException {
387       return addCostForRemainingCondition(finalTablePlan.getCostWithoutOrder(session), session);
388    }
389
390    /**
391     * For documentation of following methods refer to documentation of
392     * QuerySpecificationPlan.
393     */

394
395    public _TablePlan joinMissingLink() throws DException {
396       return finalTablePlan.joinMissingLink();
397    }
398
399    public _Order getOrder() throws DException {
400       _Order order1 = finalTablePlan.getOrder();
401       if (order != null && order1 != null) {
402          throw new DException("DSE3529", new Object JavaDoc[] {order, order1});
403       }
404       return order != null ? order : order1;
405    }
406 }
407
Popular Tags