KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.predicates;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.sql99.common.*;
6 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.condition.*;
7 import com.daffodilwoods.daffodildb.server.sql99.dql.plan.table.*;
8 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
9 import com.daffodilwoods.daffodildb.server.sql99.expression.expressionprimary.*;
10 import com.daffodilwoods.daffodildb.server.sql99.expression.rowvalueexpression.*;
11 import com.daffodilwoods.daffodildb.server.sql99.token.*;
12 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
13 import com.daffodilwoods.daffodildb.utils.field.*;
14 import com.daffodilwoods.database.resource.*;
15 import com.daffodilwoods.database.utility.*;
16
17 /**
18  * Class represents Null Predicate
19  * <p>Title: </p>
20  * <p>Description: </p>
21  * <p>Copyright: Copyright (c) 20030/p>
22  * <p>Company: </p>
23  * @author unascribed
24  * @version 1.0
25  */

26 public class nullpredicate extends PredicateAbstract implements predicate, IntegerPool, ExecutionPlanConstants /*, _BooleanPrimary*/ {
27    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439220;
28    public SRESERVEDWORD1206543922 _OptSRESERVEDWORD12065439221;
29    public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439222;
30    public rowvalueexpressionwithoutboolean _rowvalueexpressionwithoutboolean3;
31
32    /**
33     * Methods Calculates cost of solving this predicate on the rows passed.
34     * @param rowCount, rows is the total no of rows on which predicate will be solved
35     * @param index, flag indicating Index is choosen or not.
36     * @return
37     * @throws DException
38     */

39    public double getCost(long rowCount, boolean index) throws DException {
40       return rowCount * CostFactorConstants.NONINDEXPREDICATE / 100;
41    }
42
43    /**
44     * Returns Qualified Condition in case of qualified join.
45     * TableDetails passed represents the tables involved on right side of LOJ and on left side of ROJ.
46     * Algo:
47     * 1) We extract unique tables involved in the predicate.
48     * 2) If tables involved are greater than 1 or less than 1, then it means predicate involves two tables
49     * or zero tables, means predicate is to be solved as onCondition in qualified join.
50     * 3) If tables involed are exactly one, then we check whether that table lies in the array of tables
51     * passed, if it lies then we can shift predicate to singleTableLevel otherwise not.
52     * 4) For predicate to shift at singleTable Level we make BVESingleTablePLan otheriwse we make onCondition
53     * For Example:
54     * A LOJ B on A.a1 is null
55     * In the above example, Table involved in predicate is one, i.e. A and it lies on the left side of LOJ,
56     * then we solve this condition above LOJ as onCondition
57     *
58     * A LOJ B on B.b1 is null
59     * In the above example, Table involved in predicate is one, i.e. B and it lies on the right side of LOJ,
60     * then we solve shift this condition to single Table Level
61     *
62     * A LOJ B on B.b1 + A.a1 is null
63     * In the above example, Table involved in predicate is two, then we solve this condition above LOJ as
64     * onCondition
65     * @param tableDetails
66     * @return
67     * @throws DException
68     */

69    public _QualifiedBVE getQualifiedBVE(TableDetails[] tableDetails) throws DException {
70      ArrayList uniqueTableDetailsList = getUniqueTableDetails(new ArrayList(),columnDetails);
71       if (uniqueTableDetailsList.size() != 1) {
72          return new QualifiedBVE(null, BVEPlanMerger.getBooleanFactor(this));
73       }
74       QualifiedBVE qualifiedBVE = getQualifiedBVEifBelongsToSingleTablePlan(tableDetails, uniqueTableDetailsList);
75       if (qualifiedBVE == null) {
76          qualifiedBVE = new QualifiedBVE(null, BVEPlanMerger.getBooleanFactor(this));
77       }
78       return qualifiedBVE;
79    }
80
81    /**
82     * Returns Qualified Plan in case when Predicate can be shifted to single Table Level, otherwise return
83     * null.
84     * Passed Tables belongs to Qualified Plan.
85     * Algo:
86     * Passed Plan Tables is matched against Tables involved in the predicate.
87     * If it matches then this predicate can be shifted to Single Table Level, otherwise predicate is solved
88     * as onCondition.
89     * @param planTableDeails
90     * @param uniqueTabldeDetailsLsit
91     * @return
92     * @throws DException
93     */

94    private QualifiedBVE getQualifiedBVEifBelongsToSingleTablePlan(TableDetails[] planTableDetails, ArrayList uniqueTableDetailsList) throws DException {
95       TableDetails conditionTableDetail = (TableDetails) uniqueTableDetailsList.get(0);
96       for (int j = 0, length1 = planTableDetails.length; j < length1; j++) {
97          if (planTableDetails[j] == conditionTableDetail) {
98             BVESingleTablePlan bveSingleTablePlan = new BVESingleTablePlan(BVEPlanMerger.getBooleanFactor(this), conditionTableDetail);
99             return new QualifiedBVE(new BVESingleTablePlan[] {bveSingleTablePlan}
100                                     , null);
101          }
102       }
103       return null;
104    }
105
106    /**
107     * Returns List of unique tables involved in the predicate.
108     * @return
109     * @throws DException
110
111    private ArrayList getUniqueTableDetailsList() throws DException {
112       int cLength = columnDetails.length;
113       ArrayList checkTableList = new ArrayList();
114       for (int i = 0; i < cLength; i++) {
115          int type = columnDetails[i].getType();
116          if (type == REFERENCE) {
117             TableDetails tableDetails = columnDetails[i].getTableDetails();
118             if (!checkTableList.contains(tableDetails)) {
119                checkTableList.add(tableDetails);
120             }
121          }
122          if (type == SCALARFUNCTION || type == CASEEXPRESSION) {
123             checkTableList.clear();
124             return checkTableList;
125          }
126       }
127       return checkTableList;
128    }
129 */

130
131    /**
132     * Returns Type of this predicate.
133     * @return
134     * @throws DException
135     */

136
137    public int getPredicateType() throws DException {
138       if (_OptSRESERVEDWORD12065439221 != null) {
139          return OperatorConstants.NOTNULL;
140       }
141       return OperatorConstants.NULLPREDICATE_IN_QUALIFIEDPLAN;
142    }
143
144    /**
145     * Method decides whether this predicate can be solved by Index or not.
146     * Cases for which Predicate can be solved as NonIndexed Condition :
147     * 1) If predicate involves subquery.
148     * 2) If NOT is present in the predicate
149     * 3) If predicate involves any expression like a + 1. In this case Columns
150     * present in predicate is greater than 2.
151     *
152     * Cases for which Predicate can be solved as Indexed Condition :
153     * If Predicate involves the column type REFERENCE then predicate can be choosen as Index Condition
154     * In case when predicate can be solved by index then we Make Range Predicate as solvable by Index
155     * and Predicate is choosen as NonIndex Condition
156     *
157     * In case when predicate can be solved by index then Predicate is choosen as NonIndex Condition.
158     *
159     * @param allColumnPredicates
160     * @throws DException
161     */

162
163    public void setColumnPredicates(_AllColumnPredicates allColumnPredicates) throws DException {
164       if (checkForSubQuery() || _OptSRESERVEDWORD12065439221 != null || checkForOtherCases()) {
165          allColumnPredicates.addToNonIndex(new booleanfactor(new booleantest(this)));
166          return;
167       }
168       SingleColumnPredicate singleColumnPredicate = new SingleColumnPredicate();
169       singleColumnPredicate.setColumnName(columnDetails[0].getColumn());
170       singleColumnPredicate.setPredicate(this);
171       allColumnPredicates.addSinglePredicate(new SingleColumnPredicate[] {singleColumnPredicate});
172    }
173
174    /**
175     * In this some cases are checked in which we can't use this predicate in
176     * indexing. These are -
177     * 1. When more than two columns are involved.
178     * 2. When Two Columns are involved then
179     * A. Both columns are constants
180     * B. One of the column is part of Outer query.
181     * @return
182     * @throws DException
183     */

184
185    private boolean checkForOtherCases() throws DException {
186       if (columnDetails.length != 1) {
187          return true;
188       }
189       return! (columnDetails[0].getType() == REFERENCE) || columnDetails[0].getUnderLyingReference();
190    }
191
192    /**
193     * ComplexInnerQuery means that subQuery contains the column of outer query.
194     * In this case this condition is treated as remaining condition.
195     * @return
196     * @throws DException
197     */

198
199    private boolean checkForComplexInnerQuery() throws DException {
200       _Reference[] references = getReferences(null);
201       int length = references.length;
202       for (int i = 0; i < length; i++) {
203          if (references[i].getReferenceType() == SimpleConstants.SUBQUERY) {
204             if ( ( (subquery) references[i]).getUnderlyingReferences() != null) {
205                return true;
206             }
207          }
208       }
209       return false;
210    }
211
212    /**
213     * Returns a BVEConstants member telling the BVEPlanType of instance on which
214     * this method is invoked.<p>
215     * If this method finds any column of GROUPING type then it returns
216     * BVEConstants.BVEAGGREGATE.<p>
217     * If it is not an aggregate plan, then it check for ALLTABLEPLAN
218     * type by checking existence of CASE Expression OR SubQuery and
219     * ComplexInnerQuery. If founds any then returns BVEConstants.BVEALLTABLEPLAN.<p>
220     * If both cases fail it check for table count for this predicate,<p>
221     * If table count is One, returns BVEConstants.BVESINGLETABLEPLAN;<p>
222     * If Two, returns BVEConstants.BVEJOINRELATION;<p>
223     * Otherwise BVEConstants.BVEALLTABLEPLAN.<p>
224     * @param tableList An empty ArrayList which is filled in this method with
225     * table names used in this predicate.
226     * @return BVEConstants.MEMBER.
227     * @throws DException
228     */

229
230    private int getBVEPlanType(ArrayList tableList) throws DException {
231       for (int i = 0; i < columnDetails.length; i++) {
232          int type = columnDetails[i].getType();
233          if (type == GROUPING) {
234             return BVEConstants.BVEAGGREGATEPLAN;
235          }
236        if (type == SCALARFUNCTION || type == CASEEXPRESSION || type == FUNCTIONAL || type == USERFUNCTION ) {
237             return BVEConstants.BVEALLTABLEPLAN;
238          }
239          if (type != CONSTANT) {
240             TableDetails tableDetail = columnDetails[i].getTableDetails();
241             if (tableDetail != null && !tableList.contains(tableDetail)) {
242                tableList.add(tableDetail);
243             }
244          }
245       }
246       if (checkForSubQuery() && checkForComplexInnerQuery()) {
247          return BVEConstants.BVEALLTABLEPLAN;
248       }
249       int size = tableList.size();
250       if (size == 1) {
251          return BVEConstants.BVESINGLETABLEPLAN;
252       } else if (size == 2) {
253          return BVEConstants.BVEJOINRELATION;
254       } else {
255          return BVEConstants.BVEALLTABLEPLAN;
256       }
257    }
258
259    /**
260     * Returns _BVEPlan based on the type of instance of this Object.<p>
261     * <pre>
262     * instance type returned plan
263     * -------------------------------------------------------------------------
264     * BVEConstants.BVEAGGREGATEPLAN BVEAggregatePlan
265     * BVEConstants.BVEALLTABLEPLAN BVEAllTablePlan
266     * BVEConstants.BVESINGLETABLEPLAN BVESingleTablePlan
267     * BVEConstants.BVEJOINRELATION BVEAllTablePlan
268     *
269     * <pre>
270     * In case of BVEJOINRELATION, a _JoinRelation is wrapped in
271     * AllTablesJoinRelation and AllTablesJoinRelation in BVEAllTablePlan.
272     * And AllTablesJoinRelation also contains the all SingleTablePlans also.
273     * @return
274     * @throws DException
275     */

276
277    public _BVEPlan getExecutionPlan() throws DException {
278       ArrayList tableList = new ArrayList();
279       int type = getBVEPlanType(tableList);
280
281       BVESingleTablePlan[] bveSTP = null;
282       int size = tableList.size();
283       if (size > 1) {
284          bveSTP = new BVESingleTablePlan[size];
285          for (int i = 0; i < size; i++) {
286             bveSTP[i] = new BVESingleTablePlan(BVEPlanMerger.getBooleanFactor(this), (TableDetails) tableList.get(i));
287          }
288       }
289
290       switch (type) {
291          case BVEConstants.BVEAGGREGATEPLAN:
292             return new BVEAggregatePlan(BVEPlanMerger.getBooleanFactor(this));
293          case BVEConstants.BVESINGLETABLEPLAN:
294             return new BVESingleTablePlan(BVEPlanMerger.getBooleanFactor(this), (TableDetails) tableList.get(0));
295          case BVEConstants.BVEJOINRELATION:
296             _JoinRelation sr = new SimpleRelation( (TableDetails[]) tableList.toArray(new TableDetails[0]), BVEPlanMerger.getBooleanFactor(this));
297             AllTablesJoinRelation atjr = new AllTablesJoinRelation(new _JoinRelation[] {sr});
298             return new BVEAllTablePlan(bveSTP, atjr, BVEPlanMerger.getBooleanFactor(this));
299          case BVEConstants.BVEALLTABLEPLAN:
300             return new BVEAllTablePlan(bveSTP, null, BVEPlanMerger.getBooleanFactor(this));
301       }
302       throw new DException("DSE3547", new Object JavaDoc[] {this.toString()});
303    }
304
305    /**
306     * Run Method returns value 0,-1 or 1 on the basis whether predicate solves on the current record or not.
307     * Steps:
308     * 1) On Calling run we retrieves values for _rowvalueexpressionwithoutboolean3.
309     * 2) Thereafter, we check for whether value is null or not.
310     * 3) If value is null, then flag nullValue is initialized to true otherwise false.
311     * 3) On the basis of flag from appropriate FiledBase is returned
312     * 4) if NOT presents and flag is true then 1 is returned otherwise 0 is returned
313     * @param object
314     * @return
315     * @throws com.daffodilwoods.database.resource.DException
316     */

317
318    public Object JavaDoc run(Object JavaDoc object) throws DException {
319       Object JavaDoc result = _rowvalueexpressionwithoutboolean3.run(object);
320       boolean nullValue = true;
321       try {
322          nullValue = ( (FieldBase) result).isNull();
323       } catch (ClassCastException JavaDoc ex) {
324          Object JavaDoc[] values = (Object JavaDoc[]) result;
325          FieldBase[] fieldBases = GeneralPurposeStaticClass.changeIntoFieldBase(values);
326          for (int i = 0, length = fieldBases.length; i < length; i++) {
327             if(_OptSRESERVEDWORD12065439221 != null && fieldBases[i].isNull()){
328                nullValue=true;
329                break;
330             }
331             nullValue &= fieldBases[i].isNull();
332          }
333       }
334       return _OptSRESERVEDWORD12065439221 == null ? nullValue ? IntegerPool.Integer0 : IntegerPool.Integer1
335           : nullValue ? IntegerPool.Integer1 : IntegerPool.Integer0;
336    }
337
338    public ParameterInfo[] getParameterInfo() throws DException {
339       return _rowvalueexpressionwithoutboolean3.getParameterInfo();
340    }
341
342    public AbstractRowValueExpression[] getChilds() {
343       AbstractRowValueExpression[] childs = new AbstractRowValueExpression[] { (AbstractRowValueExpression) (_rowvalueexpressionwithoutboolean3)};
344       return childs;
345    }
346
347    public ColumnDetails[] getColumnDetails() throws DException {
348       if (columnDetails == null) {
349          columnDetails = _rowvalueexpressionwithoutboolean3.getColumnDetails();
350          for (int i = 0, length = columnDetails.length; i < length; i++) {
351             columnDetails[i].setAsBelongToAllowedPredicateColumn();
352          }
353       }
354       return columnDetails;
355    }
356
357    public void setDefaultValues(_VariableValueOperations variableValueOperation) throws DException {
358    }
359
360    public com.daffodilwoods.daffodildb.server.sql99.utils._Reference[] checkSemantic(com.daffodilwoods.daffodildb.server.serversystem._ServerSession parent) throws DException {
361       return _rowvalueexpressionwithoutboolean3.checkSemantic(parent);
362    }
363
364    public String JavaDoc toString() {
365       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
366       sb.append(" ");
367       sb.append(_rowvalueexpressionwithoutboolean3);
368       sb.append(" ");
369       sb.append(_SRESERVEDWORD12065439222);
370       sb.append(" ");
371       if (_OptSRESERVEDWORD12065439221 != null) {
372          sb.append(_OptSRESERVEDWORD12065439221);
373       }
374       sb.append(" ");
375       sb.append(_SRESERVEDWORD12065439220);
376       return sb.toString();
377    }
378
379    public boolean isNullPredicate() throws DException {
380       return true;
381    }
382
383    public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
384       nullpredicate tempClass = new nullpredicate();
385       tempClass._SRESERVEDWORD12065439220 = (SRESERVEDWORD1206543922) _SRESERVEDWORD12065439220.clone();
386       if (_OptSRESERVEDWORD12065439221 != null) {
387          tempClass._OptSRESERVEDWORD12065439221 = (SRESERVEDWORD1206543922) _OptSRESERVEDWORD12065439221.clone();
388       }
389       tempClass._SRESERVEDWORD12065439222 = (SRESERVEDWORD1206543922) _SRESERVEDWORD12065439222.clone();
390       tempClass._rowvalueexpressionwithoutboolean3 = (rowvalueexpressionwithoutboolean) _rowvalueexpressionwithoutboolean3.clone();
391       try {
392          tempClass.getColumnDetails();
393       } catch (DException ex) {
394          throw new RuntimeException JavaDoc(ex.getMessage());
395       }
396       return tempClass;
397    }
398 }
399
Popular Tags