1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.sql.compile.C_NodeTypes; 25 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 26 27 import org.apache.derby.iapi.sql.compile.TypeCompiler; 28 import org.apache.derby.iapi.types.DataValueDescriptor; 29 import org.apache.derby.iapi.types.TypeId; 30 import org.apache.derby.iapi.types.DataTypeDescriptor; 31 32 import org.apache.derby.iapi.reference.SQLState; 33 import org.apache.derby.iapi.reference.ClassName; 34 import org.apache.derby.iapi.error.StandardException; 35 36 import org.apache.derby.iapi.services.sanity.SanityManager; 37 38 import org.apache.derby.impl.sql.compile.ActivationClassBuilder; 39 40 import java.util.Vector ; 41 import java.sql.Types ; 42 43 49 50 public abstract class BinaryComparisonOperatorNode extends BinaryOperatorNode 51 { 52 private boolean forQueryRewrite; 54 private boolean betweenSelectivity; 55 56 65 66 public void init( 67 Object leftOperand, 68 Object rightOperand, 69 Object operator, 70 Object methodName) 71 { 72 super.init(leftOperand, rightOperand, operator, methodName, 73 ClassName.DataValueDescriptor, ClassName.DataValueDescriptor); 74 } 75 76 81 public void setForQueryRewrite(boolean val) 82 { 83 forQueryRewrite=val; 84 } 85 86 91 public boolean getForQueryRewrite() 92 { 93 return forQueryRewrite; 94 } 95 96 99 void setBetweenSelectivity() 100 { 101 betweenSelectivity = true; 102 } 103 104 109 boolean getBetweenSelectivity() 110 { 111 return betweenSelectivity; 112 } 113 114 115 128 129 public ValueNode bindExpression( 130 FromList fromList, SubqueryList subqueryList, 131 Vector aggregateVector) 132 throws StandardException 133 { 134 super.bindExpression(fromList, subqueryList, aggregateVector); 135 136 TypeCompiler leftTC = leftOperand.getTypeCompiler(); 138 TypeCompiler rightTC = rightOperand.getTypeCompiler(); 139 TypeId leftTypeId = leftOperand.getTypeId(); 140 TypeId rightTypeId = rightOperand.getTypeId(); 141 142 152 if (! leftTypeId.isStringTypeId() && rightTypeId.isStringTypeId()) 153 { 154 DataTypeDescriptor rightTypeServices = rightOperand.getTypeServices(); 155 156 rightOperand = (ValueNode) 157 getNodeFactory().getNode( 158 C_NodeTypes.CAST_NODE, 159 rightOperand, 160 new DataTypeDescriptor( 161 rightTypeId, 162 true, 163 rightTypeServices.getMaximumWidth()), 164 getContextManager()); 165 ((CastNode) rightOperand).bindCastNodeOnly(); 166 } 167 else if (! rightTypeId.isStringTypeId() && leftTypeId.isStringTypeId()) 168 { 169 DataTypeDescriptor leftTypeServices = leftOperand.getTypeServices(); 170 171 leftOperand = (ValueNode) 172 getNodeFactory().getNode( 173 C_NodeTypes.CAST_NODE, 174 leftOperand, 175 new DataTypeDescriptor( 176 leftTypeId, 177 true, 178 leftTypeServices.getMaximumWidth()), 179 getContextManager()); 180 ((CastNode) leftOperand).bindCastNodeOnly(); 181 } 182 186 else if (! leftTypeId.isNationalStringTypeId() && 187 rightTypeId.isNationalStringTypeId()) 188 { 189 leftOperand = (ValueNode) 190 getNodeFactory().getNode( 191 C_NodeTypes.CAST_NODE, 192 leftOperand, 193 DataTypeDescriptor.getBuiltInDataTypeDescriptor(leftTC.getMatchingNationalCharTypeName(), 194 leftTC.getCastToCharWidth( 195 leftOperand.getTypeServices())), 196 getContextManager()); 197 ((CastNode) leftOperand).bindCastNodeOnly(); 198 } 199 else if (! rightTypeId.isNationalStringTypeId() && 200 leftTypeId.isNationalStringTypeId()) 201 { 202 rightOperand = (ValueNode) 203 getNodeFactory().getNode( 204 C_NodeTypes.CAST_NODE, 205 rightOperand, 206 DataTypeDescriptor.getBuiltInDataTypeDescriptor(rightTC.getMatchingNationalCharTypeName(), 207 rightTC.getCastToCharWidth( 208 rightOperand.getTypeServices())), 209 getContextManager()); 210 ((CastNode) rightOperand).bindCastNodeOnly(); 211 } 212 213 214 bindComparisonOperator(); 215 216 return this; 217 } 218 219 220 227 public void bindComparisonOperator() 228 throws StandardException 229 { 230 TypeId leftType; 231 TypeId rightType; 232 boolean nullableResult; 233 234 leftType = leftOperand.getTypeId(); 235 rightType = rightOperand.getTypeId(); 236 237 238 242 boolean forEquals = operator.equals("=") || operator.equals("<>"); 243 244 boolean cmp = leftOperand.getTypeCompiler().comparable(rightType, 245 forEquals, 246 getClassFactory()); 247 if (!cmp && !forQueryRewrite) { 250 throw StandardException.newException(SQLState.LANG_NOT_COMPARABLE, 251 leftType.getSQLTypeName(), 252 rightType.getSQLTypeName() 253 ); 254 } 255 256 257 264 nullableResult = leftOperand.getTypeServices().isNullable() || 265 rightOperand.getTypeServices().isNullable(); 266 setType(new DataTypeDescriptor(TypeId.BOOLEAN_ID, nullableResult)); 267 268 269 } 270 271 286 public ValueNode preprocess(int numTables, 287 FromList outerFromList, 288 SubqueryList outerSubqueryList, 289 PredicateList outerPredicateList) 290 throws StandardException 291 { 292 leftOperand = leftOperand.preprocess(numTables, 293 outerFromList, outerSubqueryList, 294 outerPredicateList); 295 296 313 if ((rightOperand instanceof SubqueryNode) && 314 !((SubqueryNode) rightOperand).getPreprocessed()) 315 { 316 ((SubqueryNode) rightOperand).setParentComparisonOperator(this); 317 return rightOperand.preprocess(numTables, 318 outerFromList, outerSubqueryList, 319 outerPredicateList); 320 } 321 else 322 { 323 rightOperand = rightOperand.preprocess(numTables, 324 outerFromList, outerSubqueryList, 325 outerPredicateList); 326 return this; 327 } 328 } 329 330 346 ValueNode eliminateNots(boolean underNotNode) 347 throws StandardException 348 { 349 if (! underNotNode) 350 { 351 return this; 352 } 353 354 355 return getNegation(leftOperand, rightOperand); 356 } 357 358 368 BinaryOperatorNode getNegation(ValueNode leftOperand, 369 ValueNode rightOperand) 370 throws StandardException 371 { 372 375 if (SanityManager.DEBUG) 376 SanityManager.ASSERT(false, 377 "expected to call getNegation() for subclass " + 378 getClass().toString()); 379 return this; 380 } 381 382 410 public ValueNode changeToCNF(boolean underTopAndNode) 411 throws StandardException 412 { 413 417 if (underTopAndNode && (rightOperand instanceof SubqueryNode)) 418 { 419 rightOperand = rightOperand.changeToCNF(underTopAndNode); 420 } 421 422 return this; 423 } 424 425 426 public ValueNode genSQLJavaSQLTree() throws StandardException 427 { 428 TypeId leftTypeId = leftOperand.getTypeId(); 429 430 433 if (leftTypeId.userType()) 434 { 435 if (leftOperand.getTypeCompiler().comparable(leftTypeId, false, 436 getClassFactory())) 437 return this; 438 439 leftOperand = leftOperand.genSQLJavaSQLTree(); 440 } 441 442 TypeId rightTypeId = rightOperand.getTypeId(); 443 444 if (rightTypeId.userType()) 445 { 446 if (rightOperand.getTypeCompiler().comparable(rightTypeId, false, 447 getClassFactory())) 448 return this; 449 450 rightOperand = rightOperand.genSQLJavaSQLTree(); 451 } 452 453 return this; 454 } 455 } 456 | Popular Tags |