1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.client.*; 6 import com.daffodilwoods.daffodildb.server.sql99.common.*; 7 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.listenerevents.*; 9 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 10 import com.daffodilwoods.daffodildb.utils.*; 11 import com.daffodilwoods.daffodildb.utils.field.*; 12 import com.daffodilwoods.database.resource.*; 13 import com.daffodilwoods.database.utility.*; 14 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 15 16 36 public class SemiJoinIterator extends AbstractQualifiedJoinIterator implements _HasRecordIterator { 37 38 41 _Reference[] leftColumnReferences; 43 46 private int rightCount = -1; 48 51 int rightKeysCount = -1; 52 61 public SemiJoinIterator(_Iterator leftIterator, _Iterator rightIterator, _Reference[] leftColumnReferences0, _KeyColumnInformation[] leftKeyColumnInformation0, _KeyColumnInformation[] rightKeyColumnInformation0, ColumnDetails[] hasRecordReferences0) { 62 super(leftIterator, rightIterator, leftKeyColumnInformation0, rightKeyColumnInformation0, hasRecordReferences0); 63 leftColumnReferences = leftColumnReferences0; 64 rightCount = rightKeyColumnInformation0.length; 65 try { 66 rightKeysCount = 0; 67 for (int i = 0; i < rightKeyColumnInformation0.length; i++) { 68 rightKeysCount += rightKeyColumnInformation0[i].getColumnDetails().getSize(); 69 70 } 71 } 72 catch (DException ex) { 73 } 74 } 75 76 110 public boolean first() throws DException { 111 rightNull = false; 112 if (!leftIterator.first()) { 113 state = AFTERLAST; 114 return false; 115 } 116 getAndSetConditionValues(); 117 rightNull = !rightIterator.first(); 118 state = VALIDSTATE; 119 return true; 120 } 121 122 155 public boolean last() throws DException { 156 rightNull = false; 157 if (!leftIterator.last()) { 158 state = BEFOREFIRST; 159 return false; 160 } 161 getAndSetConditionValues(); 162 rightNull = !rightIterator.last(); 163 state = VALIDSTATE; 164 return true; 165 } 166 167 220 221 public boolean next() throws DException { 222 switch (state) { 223 case INVALIDSTATE: 224 throw new DException("DSE4116", null); 225 case AFTERLAST: 226 return false; 227 case BEFOREFIRST: 228 return first(); 229 default: { 230 if (rightNull) { 231 if (!leftIterator.next()) { 232 state = AFTERLAST; 233 return false; 234 } 235 getAndSetConditionValues(); 236 rightNull = !rightIterator.first(); 237 } else { 238 if (!rightIterator.next()) { 239 if (!leftIterator.next()) { 240 state = AFTERLAST; 241 return false; 242 } 243 getAndSetConditionValues(); 244 rightNull = !rightIterator.first(); 245 } 246 } 247 state = VALIDSTATE; 248 return true; 249 } 250 } 251 } 252 253 304 305 public boolean previous() throws DException { 306 switch (state) { 307 case INVALIDSTATE: 308 throw new DException("DSE4117", null); 309 case AFTERLAST: 310 return last(); 311 case BEFOREFIRST: 312 return false; 313 default: { 314 if (rightNull) { 315 if (!leftIterator.previous()) { 316 state = BEFOREFIRST; 317 return false; 318 } 319 getAndSetConditionValues(); 320 rightNull = !rightIterator.last(); 321 } else { 322 if (!rightIterator.previous()) { 323 if (!leftIterator.previous()) { 324 state = BEFOREFIRST; 325 return false; 326 } 327 getAndSetConditionValues(); 328 rightNull = !rightIterator.last(); 329 } 330 } 331 state = VALIDSTATE; 332 return true; 333 } 334 } 335 } 336 337 343 public Object getKey() throws com.daffodilwoods.database.resource.DException { 344 ArrayList list = new ArrayList(); 345 Object [] leftKeys = (Object []) leftIterator.getKey(); 346 list.addAll(java.util.Arrays.asList(leftKeys)); 347 if (rightNull) { 348 for (int i = 0; i < rightCount; i++) { 349 list.add(null); 350 } 351 } else { 352 Object [] rightKeys = (Object []) rightIterator.getKey(); 353 list.addAll(java.util.Arrays.asList(rightKeys)); 354 } 355 return list.toArray(); 356 } 357 358 369 private void moveAcctoGroupKey(Object [] keys) throws DException { 370 if (leftKeyCount == 0) { 371 leftIterator.first(); 372 } else { 373 Object [] leftKeys = new Object [leftKeyCount]; 374 System.arraycopy(keys, 0, leftKeys, 0, leftKeyCount); 375 leftIterator.move(leftKeys); 376 } 377 getAndSetConditionValues(); 378 if (rightKeyCount == 0) { 379 rightNull = !rightIterator.first(); 380 } else { 381 int rightLength = keys.length - leftKeyCount; 382 Object [] rightKeys = new Object [rightLength]; 383 System.arraycopy(keys, leftKeyCount, rightKeys, 0, rightLength); 384 if (isKeyNull(rightKeys)) { 385 rightNull = true; 386 } else { 387 rightNull = false; 388 rightIterator.move(rightKeys); 389 } 390 } 391 state = VALIDSTATE; 392 } 393 394 408 public void move(Object keys) throws com.daffodilwoods.database.resource.DException { 409 if (leftKeyCount != 0 || rightKeyCount != 0) { 410 moveAcctoGroupKey( (Object []) keys); 411 return; 412 } 413 Object [] rightKeys = new Object [rightCount]; 414 int leftCount = ( (Object []) keys).length - rightCount; 415 Object [] leftKeys = new Object [leftCount]; 416 System.arraycopy(keys, 0, leftKeys, 0, leftCount); 417 System.arraycopy(keys, leftCount, rightKeys, 0, rightCount); 418 leftIterator.move(leftKeys); 419 getAndSetConditionValues(); 420 if (isKeyNull(rightKeys)) { 421 rightNull = true; 422 } else { 423 rightNull = false; 424 rightIterator.move(rightKeys); 425 } 426 state = 0; 427 } 428 429 437 446 public Object getColumnValues(_Reference[] columnReferences) throws DException { 447 int len = columnReferences.length; 448 Object [] values = new Object [len]; 449 try { 450 for (int i = 0; i < len; i++) { 451 values[i] = getColumnValues(columnReferences[i]); 452 } 453 } catch (DException E) { 454 throw E; 455 } 456 return values; 457 } 458 459 468 public Object getColumnValues() throws DException { 469 ArrayList aList = new ArrayList(); 470 Object obj1 = null; 471 try { 472 obj1 = leftIterator.getColumnValues(); 473 } catch (DException ex) { 474 throw ex; 475 } 476 aList.addAll(Arrays.asList( (Object []) obj1)); 477 Object [] obj2 = null; 478 if (rightNull) { 479 obj2=makeNullFieldLiteralArray(rightColumnCount); 480 } else { 481 obj2 = (Object []) rightIterator.getColumnValues(); 482 } 483 aList.addAll(Arrays.asList(obj2)); 484 return aList.toArray(); 485 } 486 487 500 public Object getColumnValues(_Reference references) throws DException { 501 Object values = null; 502 ColumnDetails refColumnDetail = (ColumnDetails) references; 503 TableDetails tDetails = refColumnDetail.getTable(); 504 int index = searchInMapping(tDetails); 505 if (index == -1) { 506 return GeneralPurposeStaticClass.getColumnValuesFromQualified(leftIterator, rightIterator, references, rightNull); 507 } 508 if (index == SimpleConstants.LEFT) { 509 values = leftIterator.getColumnValues(references); 510 } else if (rightNull) { 511 values = new FieldLiteral(null, ( (ColumnDetails) references).getDatatype()); 512 ( (FieldLiteral) values).setBufferRange(FieldUtility.NULLBUFFERRANGE); 513 } else { 514 values = rightIterator.getColumnValues(references); 515 } 516 return values; 517 } 518 519 527 public FieldBase field(_Reference references) throws com.daffodilwoods.database.resource.DException { 528 FieldBase values = null; 529 ColumnDetails refColumnDetail = (ColumnDetails) references; 530 TableDetails tDetails = refColumnDetail.getTable(); 531 int index = searchInMapping(tDetails); 532 if (index == -1) { 533 return GeneralPurposeStaticClass.getFieldFromQualified(leftIterator, rightIterator, references, rightNull); 534 } 535 if (index == SimpleConstants.LEFT) { 536 values = leftIterator.field(references); 537 } else if (rightNull) { 538 values = new FieldLiteral(null, ( (ColumnDetails) references).getDatatype()); 539 ( (FieldLiteral) values).setBufferRange(FieldUtility.NULLBUFFERRANGE); 540 } else { 541 values = rightIterator.field(references); 542 } 543 return values; 544 } 545 546 554 public FieldBase[] fields(_Reference[] columnReferences) throws com.daffodilwoods.database.resource.DException { 555 int len = columnReferences.length; 556 FieldBase[] values = new FieldBase[len]; 557 try { 558 for (int i = 0; i < len; i++) { 559 values[i] = field(columnReferences[i]); 560 } 561 } catch (DException E) { 562 throw E; 563 } 564 return values; 565 } 566 567 577 private void moveAcctoGroupByteKey(byte[] keys) throws DException { 578 int k = 0, index = 0; 579 int[] t = null; 580 if (leftKeyCount == 0) 581 throw new DException("DSE0", 582 new Object [] {"Left Key Count can not be null"}); 583 else { 584 index = moveBytes(leftTableDetails, leftIterator, index, keys); 585 } 586 getAndSetConditionValues(); 587 if (rightKeyCount == 0) 588 rightIterator.first(); 589 else { 590 index = moveBytes(rightTableDetails, rightIterator, index, keys); 591 } 592 state = VALIDSTATE; 593 } 594 595 604 private int moveBytes(TableDetails[] td ,_Iterator iter, int index, byte[] keys) throws 605 DException { 606 int length = 0; 607 for (int j = 0; j < td.length; j++) { 608 short len = CCzufDpowfsufs.getShortValue(keys,index); 609 length +=len; 610 index = length + 2; 611 } 612 byte[] resultantKeys = new byte[length]; 613 System.arraycopy(keys, 0, resultantKeys, 0, resultantKeys.length); 614 iter.move(resultantKeys); 615 rightNull = isKeyNull(resultantKeys); 616 return index; 617 } 618 619 626 public byte[] getByteKey() throws DException { 627 byte[] leftKeys = leftIterator.getByteKey(); 628 if (rightNull) { 629 byte[] result = new byte[leftKeys.length + rightKeysCount + 4]; 630 short leftLen = (short)leftKeys.length; 631 System.arraycopy(CCzufDpowfsufs.getBytes(leftLen),0,result,0,2); 632 System.arraycopy(leftKeys, 0, result, 2, leftKeys.length); 633 System.arraycopy(CCzufDpowfsufs.getBytes((short)-1),0,result,leftLen+2,2); 634 Arrays.fill(result,leftLen+4,result.length-1,(byte)-1); 635 return result; 636 } 637 else{ 638 byte[] rightKeys = rightIterator.getByteKey(); 639 byte[] resultantKeys = new byte[leftKeys.length + rightKeys.length + 4]; 640 short leftLen = (short)leftKeys.length; 641 System.arraycopy(CCzufDpowfsufs.getBytes(leftLen),0,resultantKeys,0,2); 642 System.arraycopy(leftKeys, 0, resultantKeys, 2, leftKeys.length); 643 short rightLen = (short)rightKeys.length; 644 System.arraycopy(CCzufDpowfsufs.getBytes(rightLen),0,resultantKeys,leftLen+2,2); 645 System.arraycopy(rightKeys, 0, resultantKeys, leftKeys.length + 4, 646 rightKeys.length); 647 return resultantKeys; 648 649 } 650 } 651 652 666 public void moveByteKey(byte[] key) throws DException { 667 if (leftKeyCount != 0 || rightKeyCount != 0) { 668 moveAcctoGroupByteKey(key); 669 return; 670 } 671 short leftLength = CCzufDpowfsufs.getShortValue(key,0); 672 byte[] leftKeys = new byte[leftLength]; 673 System.arraycopy(key, 2, leftKeys, 0, leftKeys.length); 674 leftIterator.moveByteKey(leftKeys); 675 getAndSetConditionValues(); 676 short rightLength =CCzufDpowfsufs.getShortValue(key,leftLength+2); 677 if (rightLength <0) 678 rightNull = true; 679 else { 680 rightNull = false; 681 byte[] rightKeys = new byte[rightLength]; 682 System.arraycopy(key, leftKeys.length + 4, rightKeys, 0, rightLength); 683 rightIterator.moveByteKey(rightKeys); 684 } 685 state = 0; 686 } 687 688 695 private int searchInMapping(TableDetails tDetails) throws DException { 696 for (int i = 0; i < tableDetailsMapping.length; i++) { 697 if (tableDetailsMapping[i][0] == tDetails) { 698 return ( (Integer ) tableDetailsMapping[i][1]).intValue(); 699 } 700 } 701 return -1; 702 } 703 704 707 708 718 public void getAndSetConditionValues() throws DException { 719 Object [] values = (Object []) leftIterator.getColumnValues(leftColumnReferences); 720 rightIterator.setConditionVariableValue(leftColumnReferences, values, 1); 721 } 722 723 734 public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException { 735 return this; 736 } 737 738 743 public _ExecutionPlan getExecutionPlan() throws DException { 744 _ExecutionPlan plan = leftIterator.getExecutionPlan(); 745 _ExecutionPlan plan1 = rightIterator.getExecutionPlan(); 746 _ExecutionPlan cplans[] = new _ExecutionPlan[] {plan, plan1}; 747 return new ExecutionPlan("SemiJoinIterator", cplans, null, null, null); 748 } 749 750 755 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 756 int length = 2; 757 ExecutionPlanForBrowser cplans[] = new ExecutionPlanForBrowser[length]; 758 cplans[0] = leftIterator.getExecutionPlanForBrowser(); 759 cplans[1] = rightIterator.getExecutionPlanForBrowser(); 760 String refer = leftColumnReferences == null ? "" : "" + Arrays.asList(leftColumnReferences); 761 refer = " Outer Reference " + refer; 762 return new ExecutionPlanForBrowser("Nested Join/Semi Join" + refer, "Semi Join Iterator", cplans, refer, null, null); 763 } 764 765 public String toString() { 766 return "SemiJoinIterator[" + leftIterator + "]\n\n\n[" + rightIterator + "]"; 767 } 768 769 } 770 | Popular Tags |