KickJava   Java API By Example, From Geeks To Geeks.

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


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.dql.iterator.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.*;
8 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
9 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
10 import com.daffodilwoods.database.resource.*;
11 import com.daffodilwoods.database.sqlinitiator.*;
12
13 /**
14  * DistinctPlan is a plan to obtain the resultset in which the values of all the
15  * columns of select list are distinct in each row.
16  * <p>Title: </p>
17  * <p>Description: </p>
18  * <p>Copyright: Copyright (c) 2003</p>
19  * <p>Company: </p>
20  * @author unascribed
21  * @version 1.0
22  */

23
24 public class DistinctPlan extends PlanExceptionAbstract implements CostFactorConstants {
25
26    /**
27     * Represents underlying plan on whose resultset, distinct will be applied.
28     */

29
30    private _TablePlan tablePlan;
31
32    /**
33     * Represents the columns of selectlist, whose values are distinct in each
34     * row of the resultset.
35     */

36
37    private _Reference[] selectReferences;
38
39    /**
40     * Represents those columns of select list whose values are to be provided by
41     * user of select query.
42     */

43
44    private _Reference[] selectListQuestionReferences;
45
46    /**
47     * boolean variable to indicate whether cost is calculated for this plan or
48     * not.
49     */

50
51    private boolean costCalculated;
52
53    public DistinctPlan(_TablePlan tPlan0, _Order order0, _Reference[] selectReferences0, _Reference[] selectListQuestionReferences0) {
54       tablePlan = tPlan0;
55       selectReferences = selectReferences0;
56       selectListQuestionReferences = selectListQuestionReferences0;
57    }
58
59    /**
60     * Returns the type of underlying plan.
61     * @return
62     * @throws DException
63     */

64
65    public int getType() throws DException {
66       return tablePlan.getType();
67    }
68
69    /**
70     * Calculates the cost of solving distinct on the underlying plan.
71     * @param session
72     * @return cost calculated
73     * @throws DException
74     */

75
76    public double getCost(_ServerSession session) throws DException {
77       costCalculated = true;
78       return DISTINCT * tablePlan.getCost(session);
79    }
80
81    /**
82     * Calculates the cost of solving distinct on the underlying plan on which
83     * passed condition will be applied.
84     * @param session
85     * @param condition
86     * @param estimatedRows
87     * @return
88     * @throws DException
89     */

90
91    public double getCost(_ServerSession session, booleanvalueexpression condition, long estimatedRows) throws DException {
92       return DISTINCT * tablePlan.getCost(session, condition, estimatedRows);
93    }
94
95    /**
96     * It allows user to obtain resultset in which values of selected columns are
97     * distinct for each row.
98     * @param session
99     * @return DistinctIterator
100     * @throws DException
101     */

102
103    public _Iterator execute(_ServerSession session) throws DException {
104       if (!costCalculated) {
105          getCost(session);
106       }
107       _Iterator iterator = tablePlan.execute(session);
108       iterator = new DistinctIterator(iterator, selectReferences, selectListQuestionReferences, session);
109       return iterator;
110    }
111
112    /**
113     * It allows user to obtain resultset in which values of selected columns are
114     * distinct for each row. As the passed condition will be solved by
115     * underlying plan, so we only need to obtain underlying resultset with
116     * condition.
117     * @param session
118     * @param condition
119     * @return
120     * @throws DException
121     */

122
123    public _Iterator execute(_ServerSession session, booleanvalueexpression condition) throws DException {
124       return new DistinctIterator(tablePlan.execute(session, condition), selectReferences, selectListQuestionReferences, session);
125    }
126
127    /**
128     * Returns the estimated number of rows for the resultset obtained from this
129     * plan.
130     * @param serverSession
131     * @return
132     * @throws DException
133     */

134
135    public long getRowCount(_ServerSession serverSession) throws DException {
136       return tablePlan.getRowCount(serverSession) / DISTINCT;
137    }
138
139    /**
140     * Returns the order present in underlying plan.
141     * @return
142     * @throws DException
143     */

144
145    public _Order getOrder() throws DException {
146       return tablePlan.getOrder();
147    }
148
149    public String JavaDoc getVerifier() throws DException {
150       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
151       buffer.append(tabW("[ DISTINCT_PLAN ]"));
152       buffer.append("\n" + tablePlan.getVerifier());
153       buffer.append(tab("[/DISTINCT_PLAN ]"));
154       return buffer.toString();
155    }
156
157    public String JavaDoc toString() {
158       return " DISTINCTPLAN [ " + tablePlan + "]";
159    }
160
161    /**
162     * Returns the plan for distinct.
163     * @usage Browser.
164     * @return
165     * @throws DException
166     */

167
168    public _QueryPlan getQueryPlan() throws DException {
169       _QueryPlan cplan = tablePlan.getQueryPlan();
170       _QueryPlan[] cplans = cplan == null ? null : new _QueryPlan[] {cplan};
171       return new QueryPlan("DistinctPlan", cplans, null, null);
172    }
173
174    /**
175     * Returns all the children plans.
176     * @return
177     * @throws DException
178     */

179
180    public _TablePlan[] getChildTablePlans() throws DException {
181       childPlans = tablePlan.getChildTablePlans();
182       initializeTableDetails();
183       return new _TablePlan[] {this};
184    }
185    /**
186     * The following methods delegate their call to their corresponding method,
187     * in which order is taken into consideration. It is done because order is
188     * needed to get the result properly.
189     */

190
191    public double getCostWithoutOrder(_ServerSession session, booleanvalueexpression condition, long estimatedRowCount) throws DException {
192       return getCost(session, condition, estimatedRowCount);
193    }
194
195    public double getCostWithoutOrder(_ServerSession session) throws DException {
196       return getCost(session);
197    }
198
199    public _Iterator executeWithOutOrder(_ServerSession session, booleanvalueexpression condition) throws DException {
200       return execute(session, condition);
201    }
202
203    public _Iterator executeWithoutOrder(_ServerSession session) throws DException {
204       return execute(session);
205    }
206
207 }
208
Popular Tags