1 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 ; 48 49 55 56 public final class IsNullNode extends UnaryComparisonOperatorNode 57 implements RelationalOperator 58 { 59 60 Object nullValue = null; 61 62 public void setNodeType(int nodeType) 63 { 64 String operator; 65 String methodName; 66 67 if (nodeType == C_NodeTypes.IS_NULL_NODE) 68 { 69 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 86 operator = "is not null"; 87 methodName = "isNotNull"; 88 } 89 setOperator(operator); 90 setMethodName(methodName); 91 super.setNodeType(nodeType); 92 } 93 94 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 138 139 void bindParameter() 140 throws StandardException 141 { 142 147 148 operand.setType(new DataTypeDescriptor(TypeId.getBuiltInTypeId(Types.VARCHAR), true)); 149 } 150 151 152 153 154 public boolean usefulStartKey(Optimizable optTable) 155 { 156 return (isNullNode()); 158 } 159 160 161 public boolean usefulStopKey(Optimizable optTable) 162 { 163 return (isNullNode()); 165 } 166 167 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 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 196 public void generateOperator(MethodBuilder mb, 197 Optimizable optTable) 198 { 199 mb.push(Orderable.ORDER_OP_EQUALS); 200 } 201 202 203 public void generateNegate(MethodBuilder mb, 204 Optimizable optTable) 205 { 206 mb.push(isNotNullNode()); 207 } 208 209 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 234 public boolean compareWithKnownConstant(Optimizable optTable, boolean considerParameters) 235 { 236 return true; 237 } 238 239 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 256 public boolean equalsComparisonWithConstantExpression(Optimizable optTable) 257 { 258 boolean retval = false; 259 260 if (isNotNullNode()) 262 { 263 return false; 264 } 265 266 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 287 public RelationalOperator getTransitiveSearchClause(ColumnReference otherCR) 288 throws StandardException 289 { 290 return (RelationalOperator) getNodeFactory().getNode( 291 getNodeType(), 292 otherCR, 293 getContextManager()); 294 } 295 296 300 public String getReceiverInterfaceName() { 301 return ClassName.DataValueDescriptor; 302 } 303 304 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 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 337 public boolean isRelationalOperator() 338 { 339 return true; 340 } 341 342 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 |