1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 25 import org.apache.derby.iapi.services.sanity.SanityManager; 26 import org.apache.derby.iapi.error.StandardException; 27 28 import org.apache.derby.iapi.sql.compile.NodeFactory; 29 import org.apache.derby.iapi.sql.compile.C_NodeTypes; 30 31 import java.util.Vector ; 32 33 public class AndNode extends BinaryLogicalOperatorNode 34 { 35 36 42 43 public void init(Object leftOperand, Object rightOperand) 44 { 45 super.init(leftOperand, rightOperand, "and"); 46 this.shortCircuitValue = false; 47 } 48 49 62 63 public ValueNode bindExpression( 64 FromList fromList, SubqueryList subqueryList, 65 Vector aggregateVector) 66 throws StandardException 67 { 68 super.bindExpression(fromList, subqueryList, aggregateVector); 69 70 postBindFixup(); 71 return this; 72 } 73 74 75 90 public ValueNode preprocess(int numTables, 91 FromList outerFromList, 92 SubqueryList outerSubqueryList, 93 PredicateList outerPredicateList) 94 throws StandardException 95 { 96 100 if (leftOperand instanceof OrNode) 101 { 102 ((OrNode) leftOperand).setFirstOr(); 103 } 104 leftOperand = leftOperand.preprocess(numTables, 105 outerFromList, outerSubqueryList, 106 outerPredicateList); 107 111 if (leftOperand instanceof AndNode) 112 { 113 changeToCNF(false); 114 } 115 rightOperand = rightOperand.preprocess(numTables, 116 outerFromList, outerSubqueryList, 117 outerPredicateList); 118 return this; 119 } 120 121 137 ValueNode eliminateNots(boolean underNotNode) 138 throws StandardException 139 { 140 leftOperand = leftOperand.eliminateNots(underNotNode); 141 rightOperand = rightOperand.eliminateNots(underNotNode); 142 if (! underNotNode) 143 { 144 return this; 145 } 146 147 148 ValueNode orNode; 149 150 orNode = (ValueNode) getNodeFactory().getNode( 151 C_NodeTypes.OR_NODE, 152 leftOperand, 153 rightOperand, 154 getContextManager()); 155 orNode.setType(dataTypeServices); 156 return orNode; 157 } 158 159 168 public ValueNode putAndsOnTop() 169 throws StandardException 170 { 171 if (SanityManager.DEBUG) 172 SanityManager.ASSERT(rightOperand != null, 173 "rightOperand is expected to be non-null"); 174 rightOperand = rightOperand.putAndsOnTop(); 175 176 return this; 177 } 178 179 185 public boolean verifyPutAndsOnTop() 186 { 187 boolean isValid = true; 188 189 if (SanityManager.ASSERT) 190 { 191 isValid = ((rightOperand instanceof AndNode) || 192 (rightOperand.isBooleanTrue())); 193 194 if (rightOperand instanceof AndNode) 195 { 196 isValid = rightOperand.verifyPutAndsOnTop(); 197 } 198 } 199 200 return isValid; 201 } 202 203 231 public ValueNode changeToCNF(boolean underTopAndNode) 232 throws StandardException 233 { 234 AndNode curAnd = this; 235 236 241 242 243 if (!(rightOperand instanceof AndNode) && 244 !(rightOperand.isBooleanTrue())) 245 { 246 BooleanConstantNode trueNode; 247 248 trueNode = (BooleanConstantNode) getNodeFactory().getNode( 249 C_NodeTypes.BOOLEAN_CONSTANT_NODE, 250 Boolean.TRUE, 251 getContextManager()); 252 curAnd.setRightOperand( 253 (ValueNode) getNodeFactory().getNode( 254 C_NodeTypes.AND_NODE, 255 curAnd.getRightOperand(), 256 trueNode, 257 getContextManager())); 258 ((AndNode) curAnd.getRightOperand()).postBindFixup(); 259 } 260 261 280 281 282 while (leftOperand instanceof AndNode) 283 { 284 ValueNode newLeft; 285 AndNode oldLeft; 286 AndNode newRight; 287 ValueNode oldRight; 288 289 290 newLeft = ((AndNode) leftOperand).getLeftOperand(); 291 oldLeft = (AndNode) leftOperand; 292 newRight = (AndNode) leftOperand; 293 oldRight = rightOperand; 294 295 296 leftOperand = newLeft; 297 rightOperand = newRight; 298 newRight.setLeftOperand(oldLeft.getRightOperand()); 299 newRight.setRightOperand(oldRight); 300 } 301 302 303 leftOperand = leftOperand.changeToCNF(underTopAndNode); 304 rightOperand = rightOperand.changeToCNF(underTopAndNode); 305 306 return this; 307 } 308 309 318 public boolean verifyChangeToCNF() 319 { 320 boolean isValid = true; 321 322 if (SanityManager.ASSERT) 323 { 324 isValid = ((rightOperand instanceof AndNode) || 325 (rightOperand.isBooleanTrue())); 326 if (rightOperand instanceof AndNode) 327 { 328 isValid = rightOperand.verifyChangeToCNF(); 329 } 330 if (leftOperand instanceof AndNode) 331 { 332 isValid = false; 333 } 334 else 335 { 336 isValid = isValid && leftOperand.verifyChangeToCNF(); 337 } 338 } 339 340 return isValid; 341 } 342 343 349 void postBindFixup() 350 throws StandardException 351 { 352 setType(resolveLogicalBinaryOperator( 353 leftOperand.getTypeServices(), 354 rightOperand.getTypeServices() 355 ) 356 ); 357 } 358 } 359 | Popular Tags |