KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.IsNullNode
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.reference.ClassName;
25
26 import org.apache.derby.iapi.services.compiler.MethodBuilder;
27 import org.apache.derby.iapi.services.sanity.SanityManager;
28
29 import org.apache.derby.iapi.error.StandardException;
30
31 import org.apache.derby.iapi.sql.compile.C_NodeTypes;
32 import org.apache.derby.iapi.sql.compile.Optimizable;
33
34 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
35
36 import org.apache.derby.iapi.store.access.ScanController;
37
38 import org.apache.derby.iapi.types.DataTypeDescriptor;
39 import org.apache.derby.iapi.types.DataValueDescriptor;
40 import org.apache.derby.iapi.types.DataValueFactory;
41 import org.apache.derby.iapi.types.TypeId;
42
43 import org.apache.derby.iapi.types.Orderable;
44
45 import org.apache.derby.impl.sql.compile.ExpressionClassBuilder;
46
47 import java.sql.Types JavaDoc;
48
49 /**
50  * This node represents either a unary
51  * IS NULL or IS NOT NULL comparison operator
52  *
53  * @author Jerry Brenner
54  */

55
56 public final class IsNullNode extends UnaryComparisonOperatorNode
57                         implements RelationalOperator
58 {
59
60     Object JavaDoc nullValue = null;
61
62     public void setNodeType(int nodeType)
63     {
64         String JavaDoc operator;
65         String JavaDoc methodName;
66
67         if (nodeType == C_NodeTypes.IS_NULL_NODE)
68         {
69             /* By convention, the method name for the is null operator is "isNull" */
70             operator = "is null";
71             methodName = "isNullOp";
72         }
73         else
74         {
75             if (SanityManager.DEBUG)
76             {
77                 if (nodeType != C_NodeTypes.IS_NOT_NULL_NODE)
78                 {
79                     SanityManager.THROWASSERT(
80                         "Unexpected nodeType = " + nodeType);
81                 }
82             }
83             /* By convention, the method name for the is not null operator is
84              * "isNotNull"
85              */

86             operator = "is not null";
87             methodName = "isNotNull";
88         }
89         setOperator(operator);
90         setMethodName(methodName);
91         super.setNodeType(nodeType);
92     }
93
94     /**
95      * Negate the comparison.
96      *
97      * @param operand The operand of the operator
98      *
99      * @return UnaryOperatorNode The negated expression
100      *
101      * @exception StandardException Thrown on error
102      */

103     UnaryOperatorNode getNegation(ValueNode operand)
104                 throws StandardException
105     {
106         UnaryOperatorNode negation;
107
108         if (SanityManager.DEBUG)
109         {
110             SanityManager.ASSERT(dataTypeServices != null,
111                         "dataTypeServices is expected to be non-null");
112         }
113
114         if (isNullNode())
115         {
116             setNodeType(C_NodeTypes.IS_NOT_NULL_NODE);
117         }
118         else
119         {
120             if (SanityManager.DEBUG)
121             {
122                 if (! isNotNullNode())
123                 {
124                     SanityManager.THROWASSERT(
125                         "Unexpected nodeType = " + getNodeType());
126                 }
127             }
128             setNodeType(C_NodeTypes.IS_NULL_NODE);
129         }
130         return this;
131     }
132
133     /**
134      * Bind a ? parameter operand of the IS [NOT] NULL predicate.
135      *
136      * @exception StandardException Thrown on error
137      */

138
139     void bindParameter()
140             throws StandardException
141     {
142         /*
143         ** If IS [NOT] NULL has a ? operand, we assume
144         ** its type is varchar with the implementation-defined maximum length
145         ** for a varchar.
146         */

147
148         operand.setType(new DataTypeDescriptor(TypeId.getBuiltInTypeId(Types.VARCHAR), true));
149     }
150
151     /* RelationalOperator interface */
152
153     /** @see RelationalOperator#usefulStartKey */
154     public boolean usefulStartKey(Optimizable optTable)
155     {
156         // IS NULL is start/stop key, IS NOT NULL is not
157
return (isNullNode());
158     }
159
160     /** @see RelationalOperator#usefulStopKey */
161     public boolean usefulStopKey(Optimizable optTable)
162     {
163         // IS NULL is start/stop key, IS NOT NULL is not
164
return (isNullNode());
165     }
166
167     /** @see RelationalOperator#getStartOperator */
168     public int getStartOperator(Optimizable optTable)
169     {
170         if (SanityManager.DEBUG)
171         {
172             if (! isNullNode())
173             {
174                 SanityManager.THROWASSERT(
175                     "getNodeType() not expected to return " + getNodeType());
176             }
177         }
178         return ScanController.GE;
179     }
180
181     /** @see RelationalOperator#getStopOperator */
182     public int getStopOperator(Optimizable optTable)
183     {
184         if (SanityManager.DEBUG)
185         {
186             if (! isNullNode())
187             {
188                 SanityManager.THROWASSERT(
189                     "getNodeType() not expected to return " + getNodeType());
190             }
191         }
192         return ScanController.GT;
193     }
194
195     /** @see RelationalOperator#generateOperator */
196     public void generateOperator(MethodBuilder mb,
197                                         Optimizable optTable)
198     {
199         mb.push(Orderable.ORDER_OP_EQUALS);
200     }
201
202     /** @see RelationalOperator#generateNegate */
203     public void generateNegate(MethodBuilder mb,
204                                         Optimizable optTable)
205     {
206         mb.push(isNotNullNode());
207     }
208
209     /** @see RelationalOperator#getOperator */
210     public int getOperator()
211     {
212         int operator;
213         if (isNullNode())
214         {
215             operator = IS_NULL_RELOP;
216         }
217         else
218         {
219             if (SanityManager.DEBUG)
220             {
221                 if (! isNotNullNode())
222                 {
223                     SanityManager.THROWASSERT(
224                         "Unexpected nodeType = " + getNodeType());
225                 }
226             }
227             operator = IS_NOT_NULL_RELOP;
228         }
229
230         return operator;
231     }
232
233     /** @see RelationalOperator#compareWithKnownConstant */
234     public boolean compareWithKnownConstant(Optimizable optTable, boolean considerParameters)
235     {
236         return true;
237     }
238
239     /**
240      * @see RelationalOperator#getCompareValue
241      *
242      * @exception StandardException Thrown on error
243      */

244     public DataValueDescriptor getCompareValue(Optimizable optTable)
245                     throws StandardException
246     {
247         if (nullValue == null)
248         {
249             nullValue = operand.getTypeId().getNull();
250         }
251
252         return (DataValueDescriptor) nullValue;
253     }
254
255     /** @see RelationalOperator#equalsComparisonWithConstantExpression */
256     public boolean equalsComparisonWithConstantExpression(Optimizable optTable)
257     {
258         boolean retval = false;
259
260         // Always return false for NOT NULL
261
if (isNotNullNode())
262         {
263             return false;
264         }
265
266         /*
267         ** Is the operand a column in the given table?
268         */

269         if (operand instanceof ColumnReference)
270         {
271             int tabNum = ((ColumnReference) operand).getTableNumber();
272             if (optTable.hasTableNumber() &&
273                 (optTable.getTableNumber() == tabNum))
274             {
275                 retval = true;
276             }
277         }
278
279         return retval;
280     }
281
282     /**
283      * @see RelationalOperator#getTransitiveSearchClause
284      *
285      * @exception StandardException thrown on error
286      */

287     public RelationalOperator getTransitiveSearchClause(ColumnReference otherCR)
288         throws StandardException
289     {
290         return (RelationalOperator) getNodeFactory().getNode(
291                                     getNodeType(),
292                                     otherCR,
293                                     getContextManager());
294     }
295
296     /**
297      * null operators are defined on DataValueDescriptor.
298      * Overrides method in UnaryOperatorNode for code generation purposes.
299      */

300     public String JavaDoc getReceiverInterfaceName() {
301         return ClassName.DataValueDescriptor;
302     }
303
304     /** IS NULL is like =, so should have the same selectivity */
305     public double selectivity(Optimizable optTable)
306     {
307         if (isNullNode())
308         {
309             return 0.1d;
310         }
311         else
312         {
313             if (SanityManager.DEBUG)
314             {
315                 if (! isNotNullNode())
316                 {
317                     SanityManager.THROWASSERT(
318                         "Unexpected nodeType = " + getNodeType());
319                 }
320             }
321             /* IS NOT NULL is like <>, so should have same selectivity */
322             return 0.9d;
323         }
324     }
325
326     private boolean isNullNode()
327     {
328         return getNodeType() == C_NodeTypes.IS_NULL_NODE;
329     }
330
331     private boolean isNotNullNode()
332     {
333         return getNodeType() == C_NodeTypes.IS_NOT_NULL_NODE;
334     }
335     
336     /** @see ValueNode#isRelationalOperator */
337     public boolean isRelationalOperator()
338     {
339         return true;
340     }
341
342     /** @see ValueNode#optimizableEqualityNode */
343     public boolean optimizableEqualityNode(Optimizable optTable,
344                                            int columnNumber,
345                                            boolean isNullOkay)
346     {
347         if (!isNullNode() || !isNullOkay)
348             return false;
349         
350         ColumnReference cr = getColumnOperand(optTable,
351                                               columnNumber);
352         if (cr == null)
353         {
354             return false;
355         }
356
357         return true;
358     }
359
360 }
361
Popular Tags