1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.iapi.error.StandardException; 27 28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 29 30 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 31 32 import org.apache.derby.iapi.types.DataTypeDescriptor; 33 import org.apache.derby.iapi.types.DataValueDescriptor; 34 import org.apache.derby.iapi.types.TypeId; 35 36 import org.apache.derby.iapi.sql.compile.TypeCompiler; 37 38 import org.apache.derby.iapi.reference.SQLState; 39 40 import org.apache.derby.iapi.store.access.Qualifier; 41 42 import org.apache.derby.iapi.util.JBitSet; 43 44 import java.util.Vector ; 45 46 53 54 public class ValueNodeList extends QueryTreeNodeVector 55 { 56 57 63 64 public void printSubNodes(int depth) 65 { 66 if (SanityManager.DEBUG) 67 { 68 super.printSubNodes(depth); 69 70 for (int index = 0; index < size(); index++) 71 { 72 ValueNode valueNode; 73 valueNode = (ValueNode) elementAt(index); 74 valueNode.treePrint(depth + 1); 75 } 76 } 77 } 78 79 84 public void setClause(int clause) 85 { 86 int size = size(); 87 88 for (int index = 0; index < size; index++) 89 { 90 ValueNode valueNode; 91 valueNode = (ValueNode) elementAt(index); 92 valueNode.setClause(clause); 93 } 94 } 95 96 103 104 public void addValueNode(ValueNode valueNode) throws StandardException 105 { 106 addElement(valueNode); 107 } 108 109 120 public void bindExpression(FromList fromList, 121 SubqueryList subqueryList, 122 Vector aggregateVector) 123 throws StandardException 124 { 125 int size = size(); 126 127 for (int index = 0; index < size; index++) 128 { 129 ValueNode vn = (ValueNode) elementAt(index); 130 vn = vn.bindExpression(fromList, subqueryList, 131 aggregateVector); 132 133 setElementAt(vn, index); 134 } 135 } 136 137 138 146 public void genSQLJavaSQLTrees() 147 throws StandardException 148 { 149 int size = size(); 150 151 for (int index = 0; index < size; index++) 152 { 153 ValueNode valueNode = (ValueNode) elementAt(index); 154 155 if (valueNode.getTypeId().userType()) 156 { 157 setElementAt(valueNode.genSQLJavaSQLTree(), index); 158 } 159 } 160 } 161 162 169 public DataTypeDescriptor getDominantTypeServices() throws StandardException 170 { 171 DataTypeDescriptor dominantDTS = null; 172 173 for (int index = 0; index < size(); index++) 174 { 175 ValueNode valueNode; 176 177 valueNode = (ValueNode) elementAt(index); 178 if (valueNode.requiresTypeFromContext()) 179 continue; 180 DataTypeDescriptor valueNodeDTS = valueNode.getTypeServices(); 181 182 if (dominantDTS == null) 183 { 184 dominantDTS = valueNodeDTS; 185 } 186 else 187 { 188 dominantDTS = dominantDTS.getDominantType(valueNodeDTS, getClassFactory()); 189 } 190 } 191 192 return dominantDTS; 193 } 194 195 202 public DataTypeDescriptor getTypeServices() throws StandardException 203 { 204 DataTypeDescriptor firstDTS = null; 205 int size = size(); 206 207 for (int index = 0; index < size; index++) 208 { 209 ValueNode valueNode; 210 211 valueNode = (ValueNode) elementAt(index); 212 DataTypeDescriptor valueNodeDTS = valueNode.getTypeServices(); 213 214 if ((firstDTS == null) && (valueNodeDTS != null)) 215 { 216 firstDTS = valueNodeDTS; 217 break; 218 } 219 } 220 221 return firstDTS; 222 } 223 224 233 boolean allSamePrecendence(int precedence) 234 throws StandardException 235 { 236 boolean allSame = true; 237 int size = size(); 238 239 for (int index = 0; index < size; index++) 240 { 241 ValueNode valueNode; 242 243 valueNode = (ValueNode) elementAt(index); 244 DataTypeDescriptor valueNodeDTS = valueNode.getTypeServices(); 245 246 if (valueNodeDTS == null) 247 { 248 return false; 249 } 250 251 if (precedence != valueNodeDTS.getTypeId().typePrecedence()) 252 { 253 return false; 254 } 255 } 256 257 return allSame; 258 } 259 260 261 267 public void compatible(ValueNode leftOperand) throws StandardException 268 { 269 int size = size(); 270 TypeId leftType; 271 ValueNode valueNode; 272 TypeCompiler leftTC; 273 274 leftType = leftOperand.getTypeId(); 275 leftTC = leftOperand.getTypeCompiler(); 276 277 for (int index = 0; index < size; index++) 278 { 279 valueNode = (ValueNode) elementAt(index); 280 if (valueNode.requiresTypeFromContext()) 281 continue; 282 283 284 287 if (! leftTC.compatible(valueNode.getTypeId())) 288 { 289 throw StandardException.newException(SQLState.LANG_DB2_COALESCE_DATATYPE_MISMATCH, 290 leftType.getSQLTypeName(), 291 valueNode.getTypeId().getSQLTypeName() 292 ); 293 } 294 } 295 } 296 297 306 public void comparable(ValueNode leftOperand) throws StandardException 307 { 308 int size = size(); 309 TypeId leftType; 310 ValueNode valueNode; 311 TypeCompiler leftTC; 312 313 leftType = leftOperand.getTypeId(); 314 leftTC = leftOperand.getTypeCompiler(); 315 316 for (int index = 0; index < size; index++) 317 { 318 valueNode = (ValueNode) elementAt(index); 319 320 324 if (! leftTC.comparable(valueNode.getTypeId(), 325 false, 326 getClassFactory())) 327 { 328 throw StandardException.newException(SQLState.LANG_NOT_COMPARABLE, 329 leftType.getSQLTypeName(), 330 valueNode.getTypeId().getSQLTypeName() 331 ); 332 } 333 } 334 } 335 336 342 public boolean isNullable() 343 throws StandardException 344 { 345 int size = size(); 346 347 for (int index = 0; index < size; index++) 348 { 349 if (((ValueNode) elementAt(index)).getTypeServices().isNullable()) 350 { 351 return true; 352 } 353 } 354 return false; 355 } 356 357 362 public boolean containsParameterNode() 363 { 364 int size = size(); 365 366 for (int index = 0; index < size; index++) 367 { 368 if (((ValueNode) elementAt(index)).requiresTypeFromContext()) 369 { 370 return true; 371 } 372 } 373 return false; 374 } 375 376 381 public boolean containsAllParameterNodes() 382 { 383 int size = size(); 384 385 for (int index = 0; index < size; index++) 386 { 387 if (! (((ValueNode) elementAt(index)).requiresTypeFromContext())) 388 { 389 return false; 390 } 391 } 392 return true; 393 } 394 395 400 public boolean containsAllConstantNodes() 401 { 402 int size = size(); 403 404 for (int index = 0; index < size; index++) 405 { 406 if (! ((ValueNode) elementAt(index) instanceof ConstantNode)) 407 { 408 return false; 409 } 410 } 411 return true; 412 } 413 414 422 void sortInAscendingOrder(DataValueDescriptor judgeODV) 423 throws StandardException 424 { 425 int size = size(); 426 427 if (SanityManager.DEBUG) 428 { 429 SanityManager.ASSERT(size > 0, 430 "size() expected to be non-zero"); 431 } 432 433 436 boolean continueSort = true; 437 438 while (continueSort) 439 { 440 continueSort = false; 441 442 for (int index = 1; index < size; index++) 443 { 444 ConstantNode currCN = (ConstantNode) elementAt(index); 445 DataValueDescriptor currODV = 446 currCN.getValue(); 447 ConstantNode prevCN = (ConstantNode) elementAt(index - 1); 448 DataValueDescriptor prevODV = 449 prevCN.getValue(); 450 451 452 if ((judgeODV == null && (prevODV.compare(currODV)) > 0) || 453 (judgeODV != null && judgeODV.greaterThan(prevODV, currODV).equals(true))) 454 { 455 setElementAt(currCN, index - 1); 456 setElementAt(prevCN, index); 457 continueSort = true; 458 } 459 } 460 } 461 } 462 463 470 public void setParameterDescriptor(DataTypeDescriptor descriptor) 471 throws StandardException 472 { 473 int size = size(); 474 ValueNode valueNode; 475 476 for (int index = 0; index < size; index++) 477 { 478 valueNode = (ValueNode) elementAt(index); 479 if (valueNode.requiresTypeFromContext()) 480 { 481 valueNode.setType(descriptor); 482 } 483 } 484 } 485 486 497 public void preprocess(int numTables, 498 FromList outerFromList, 499 SubqueryList outerSubqueryList, 500 PredicateList outerPredicateList) 501 throws StandardException 502 { 503 int size = size(); 504 ValueNode valueNode; 505 506 for (int index = 0; index < size; index++) 507 { 508 valueNode = (ValueNode) elementAt(index); 509 valueNode.preprocess(numTables, 510 outerFromList, outerSubqueryList, 511 outerPredicateList); 512 } 513 } 514 515 523 public ValueNodeList remapColumnReferencesToExpressions() 524 throws StandardException 525 { 526 int size = size(); 527 528 for (int index = 0; index < size; index++) 529 { 530 setElementAt( 531 ((ValueNode) elementAt(index)).remapColumnReferencesToExpressions(), 532 index); 533 } 534 return this; 535 } 536 537 542 public boolean isConstantExpression() 543 { 544 int size = size(); 545 546 for (int index = 0; index < size; index++) 547 { 548 boolean retcode; 549 550 retcode = ((ValueNode) elementAt(index)).isConstantExpression(); 551 if (! retcode) 552 { 553 return retcode; 554 } 555 } 556 557 return true; 558 } 559 560 561 public boolean constantExpression(PredicateList whereClause) 562 { 563 int size = size(); 564 565 for (int index = 0; index < size; index++) 566 { 567 boolean retcode; 568 569 retcode = 570 ((ValueNode) elementAt(index)).constantExpression(whereClause); 571 if (! retcode) 572 { 573 return retcode; 574 } 575 } 576 577 return true; 578 } 579 580 605 public boolean categorize(JBitSet referencedTabs, boolean simplePredsOnly) 606 throws StandardException 607 { 608 612 boolean pushable = true; 613 int size = size(); 614 615 for (int index = 0; index < size; index++) 616 { 617 pushable = ((ValueNode) elementAt(index)).categorize(referencedTabs, simplePredsOnly) && 618 pushable; 619 } 620 621 return pushable; 622 } 623 624 637 protected int getOrderableVariantType() throws StandardException 638 { 639 int listType = Qualifier.CONSTANT; 640 int size = size(); 641 642 647 for (int index = 0; index < size; index++) 648 { 649 int curType = ((ValueNode) elementAt(index)).getOrderableVariantType(); 650 listType = Math.min(listType, curType); 651 } 652 653 return listType; 654 } 655 } 656 | Popular Tags |