KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > NestedLoopJoinStrategy


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.NestedLoopJoinStrategy
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.sql.compile.CostEstimate;
25 import org.apache.derby.iapi.sql.compile.ExpressionClassBuilderInterface;
26 import org.apache.derby.iapi.sql.compile.JoinStrategy;
27 import org.apache.derby.iapi.sql.compile.Optimizable;
28 import org.apache.derby.iapi.sql.compile.Optimizer;
29 import org.apache.derby.iapi.sql.compile.OptimizablePredicateList;
30 import org.apache.derby.iapi.sql.compile.OptimizablePredicate;
31
32 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
33 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor;
34
35 import org.apache.derby.iapi.store.access.StoreCostController;
36 import org.apache.derby.iapi.store.access.TransactionController;
37
38 import org.apache.derby.iapi.services.compiler.MethodBuilder;
39
40 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
41
42 import org.apache.derby.iapi.error.StandardException;
43
44 import org.apache.derby.iapi.services.sanity.SanityManager;
45
46 public class NestedLoopJoinStrategy extends BaseJoinStrategy {
47     public NestedLoopJoinStrategy() {
48     }
49
50
51     /**
52      * @see JoinStrategy#feasible
53      *
54      * @exception StandardException Thrown on error
55      */

56     public boolean feasible(Optimizable innerTable,
57                             OptimizablePredicateList predList,
58                             Optimizer optimizer
59                             )
60                     throws StandardException
61     {
62         /* Nested loop is feasible, except in the corner case
63          * where innerTable is a VTI that cannot be materialized
64          * (because it has a join column as a parameter) and
65          * it cannot be instantiated multiple times.
66          * RESOLVE - Actually, the above would work if all of
67          * the tables outer to innerTable were 1 row tables, but
68          * we don't have that info yet, and it should probably
69          * be hidden in inner table somewhere.
70          * NOTE: A derived table that is correlated with an outer
71          * query block is not materializable, but it can be
72          * "instantiated" multiple times because that only has
73          * meaning for VTIs.
74          */

75         if (innerTable.isMaterializable())
76         {
77             return true;
78         }
79         if (innerTable.supportsMultipleInstantiations())
80         {
81             return true;
82         }
83         return false;
84     }
85
86     /** @see JoinStrategy#multiplyBaseCostByOuterRows */
87     public boolean multiplyBaseCostByOuterRows() {
88         return true;
89     }
90
91     /**
92      * @see JoinStrategy#getBasePredicates
93      *
94      * @exception StandardException Thrown on error
95      */

96     public OptimizablePredicateList getBasePredicates(
97                                     OptimizablePredicateList predList,
98                                     OptimizablePredicateList basePredicates,
99                                     Optimizable innerTable)
100                             throws StandardException {
101         if (SanityManager.DEBUG) {
102             SanityManager.ASSERT(basePredicates == null ||
103                                  basePredicates.size() == 0,
104                 "The base predicate list should be empty.");
105         }
106
107         if (predList != null) {
108             predList.transferAllPredicates(basePredicates);
109             basePredicates.classify(innerTable,
110                 innerTable.getCurrentAccessPath().getConglomerateDescriptor());
111         }
112
113         return basePredicates;
114     }
115
116     /** @see JoinStrategy#nonBasePredicateSelectivity */
117     public double nonBasePredicateSelectivity(
118                                         Optimizable innerTable,
119                                         OptimizablePredicateList predList) {
120         /*
121         ** For nested loop, all predicates are base predicates, so there
122         ** is no extra selectivity.
123         */

124         return 1.0;
125     }
126     
127     /**
128      * @see JoinStrategy#putBasePredicates
129      *
130      * @exception StandardException Thrown on error
131      */

132     public void putBasePredicates(OptimizablePredicateList predList,
133                                     OptimizablePredicateList basePredicates)
134                     throws StandardException {
135         for (int i = basePredicates.size() - 1; i >= 0; i--) {
136             OptimizablePredicate pred = basePredicates.getOptPredicate(i);
137
138             predList.addOptPredicate(pred);
139             basePredicates.removeOptPredicate(i);
140         }
141     }
142
143     /* @see JoinStrategy#estimateCost */
144     public void estimateCost(Optimizable innerTable,
145                              OptimizablePredicateList predList,
146                              ConglomerateDescriptor cd,
147                              CostEstimate outerCost,
148                              Optimizer optimizer,
149                              CostEstimate costEstimate) {
150         costEstimate.multiply(outerCost.rowCount(), costEstimate);
151
152         optimizer.trace(Optimizer.COST_OF_N_SCANS, innerTable.getTableNumber(), 0, outerCost.rowCount(),
153                         costEstimate);
154     }
155
156     /** @see JoinStrategy#maxCapacity */
157     public int maxCapacity( int userSpecifiedCapacity,
158                             int maxMemoryPerTable,
159                             double perRowUsage) {
160         return Integer.MAX_VALUE;
161     }
162
163     /** @see JoinStrategy#getName */
164     public String JavaDoc getName() {
165         return "NESTEDLOOP";
166     }
167
168     /** @see JoinStrategy#scanCostType */
169     public int scanCostType() {
170         return StoreCostController.STORECOST_SCAN_NORMAL;
171     }
172
173     /** @see JoinStrategy#resultSetMethodName */
174     public String JavaDoc resultSetMethodName(boolean bulkFetch) {
175         if (bulkFetch)
176             return "getBulkTableScanResultSet";
177         else
178             return "getTableScanResultSet";
179     }
180
181     /** @see JoinStrategy#joinResultSetMethodName */
182     public String JavaDoc joinResultSetMethodName() {
183         return "getNestedLoopJoinResultSet";
184     }
185
186     /** @see JoinStrategy#halfOuterJoinResultSetMethodName */
187     public String JavaDoc halfOuterJoinResultSetMethodName() {
188         return "getNestedLoopLeftOuterJoinResultSet";
189     }
190
191     /**
192      * @see JoinStrategy#getScanArgs
193      *
194      * @exception StandardException Thrown on error
195      */

196     public int getScanArgs(
197                             TransactionController tc,
198                             MethodBuilder mb,
199                             Optimizable innerTable,
200                             OptimizablePredicateList storeRestrictionList,
201                             OptimizablePredicateList nonStoreRestrictionList,
202                             ExpressionClassBuilderInterface acbi,
203                             int bulkFetch,
204                             MethodBuilder resultRowAllocator,
205                             int colRefItem,
206                             int indexColItem,
207                             int lockMode,
208                             boolean tableLocked,
209                             int isolationLevel,
210                             int maxMemoryPerTable
211                             )
212                         throws StandardException {
213         ExpressionClassBuilder acb = (ExpressionClassBuilder) acbi;
214         int numArgs;
215
216         if (SanityManager.DEBUG) {
217             if (nonStoreRestrictionList.size() != 0) {
218                 SanityManager.THROWASSERT(
219                     "nonStoreRestrictionList should be empty for " +
220                     "nested loop join strategy, but it contains " +
221                     nonStoreRestrictionList.size() +
222                     " elements");
223             }
224         }
225
226         if (bulkFetch > 1)
227         {
228             numArgs = 25;
229         }
230         else
231         {
232             numArgs = 24 ;
233         }
234
235         fillInScanArgs1(tc, mb,
236                                         innerTable,
237                                         storeRestrictionList,
238                                         acb,
239                                         resultRowAllocator);
240
241         fillInScanArgs2(mb,
242                         innerTable,
243                         bulkFetch,
244                         colRefItem,
245                         indexColItem,
246                         lockMode,
247                         tableLocked,
248                         isolationLevel);
249
250         return numArgs;
251     }
252
253     /**
254      * @see JoinStrategy#divideUpPredicateLists
255      *
256      * @exception StandardException Thrown on error
257      */

258     public void divideUpPredicateLists(
259                     Optimizable innerTable,
260                     OptimizablePredicateList originalRestrictionList,
261                     OptimizablePredicateList storeRestrictionList,
262                     OptimizablePredicateList nonStoreRestrictionList,
263                     OptimizablePredicateList requalificationRestrictionList,
264                     DataDictionary dd
265                     ) throws StandardException
266     {
267         /*
268         ** All predicates are store predicates. No requalification is
269         ** necessary for non-covering index scans.
270         */

271         originalRestrictionList.setPredicatesAndProperties(storeRestrictionList);
272     }
273
274     /**
275      * @see JoinStrategy#doesMaterialization
276      */

277     public boolean doesMaterialization()
278     {
279         return false;
280     }
281
282     public String JavaDoc toString() {
283         return getName();
284     }
285
286     /**
287      * Can this join strategy be used on the
288      * outermost table of a join.
289      *
290      * @return Whether or not this join strategy
291      * can be used on the outermose table of a join.
292      */

293     protected boolean validForOutermostTable()
294     {
295         return true;
296     }
297 }
298
Popular Tags