1 package com.daffodilwoods.daffodildb.server.sql99.dql.iterator.table; 2 3 import java.util.*; 4 import com.daffodilwoods.daffodildb.client.*; 5 import com.daffodilwoods.daffodildb.server.sql99.common.*; 6 import com.daffodilwoods.daffodildb.server.sql99.dql.execution.*; 7 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 8 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 9 import com.daffodilwoods.daffodildb.utils.field.*; 10 import com.daffodilwoods.database.resource.*; 11 import com.daffodilwoods.database.utility.P; 12 import com.daffodilwoods.daffodildb.utils.field.FieldBase; 13 import com.daffodilwoods.daffodildb.server.sql99.common.ColumnDetails; 14 import com.daffodilwoods.daffodildb.utils.FieldUtility; 15 import com.daffodilwoods.daffodildb.utils.BufferRange; 16 import com.daffodilwoods.daffodildb.server.sql99.expression.ValueExpressionSuper; 17 import com.daffodilwoods.daffodildb.server.sql99.expression.valueexpression; 18 import com.daffodilwoods.daffodildb.server.serversystem._ServerSession; 19 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.TableKeyColumnInformation; 20 21 34 35 public class GroupByIterator extends BaseSingleIterator implements _Iterator, SimpleConstants { 36 37 40 private GroupByCondition groupByCondition; 41 46 protected Object [][] mapping; 51 protected int state; 55 protected int status; 59 private ColumnDetails[] columnDetails; 60 64 protected _KeyColumnInformation[] keyColumnInformation; 65 68 private TableDetails[] tables; 69 70 public Object [] objectToMove; 71 72 private _VariableValues variableValues; 73 74 75 private Object [] getObjectArray() throws DException{ 76 if( keyColumnInformation ==null) 77 return null; 78 int len = keyColumnInformation.length; 79 FieldBase[] list = new FieldBase[len]; 80 for(int i=0;i<len;i++){ 81 int datatype = keyColumnInformation[i].getColumnDetails().getDatatype(); 82 list[i] = FieldUtility.getField(datatype,null) ; 83 } 84 return list; 85 } 86 87 96 public GroupByIterator(_Iterator iterator, GroupByCondition groupByCondition0, ColumnDetails[] aggregateColumnDetails0,_Reference[] aggregateColumnUnderlyingReferences0,_ServerSession serverSession) throws DException { 97 super(iterator); 98 status = INVALIDSTATE; 99 variableValues=new VariableValues(serverSession); 100 variableValues.setIterator(iterator); 101 groupByCondition = groupByCondition0; 102 if (aggregateColumnDetails0 != null) { 103 int len = aggregateColumnDetails0.length; 104 mapping = new Object [len][2]; 105 for (int i = 0; i < len; i++) { 106 mapping[i][0] = aggregateColumnDetails0[i]; 107 mapping[i][1] = aggregateColumnDetails0[i].getExecutable(); 108 } 109 } 110 if (groupByCondition != null) { 111 columnDetails = groupByCondition.getColumnDetails(); 112 keyColumnInformation = getKeyColumnInformations(); 113 tables = iterator.getTableDetails(); 114 Object [][] mapping = getMappingOfTableAndKey(); 115 iterator.setKeyCount(mapping); 116 } 117 else { 118 keyColumnInformation = getKeyColumnInformations(); 119 } 120 objectToMove = getObjectArray(); 121 } 122 123 129 private Object [][] getMappingOfTableAndKey() throws DException { 130 ArrayList list = getListOfTables(columnDetails); 131 ArrayList mapping = new ArrayList(5); 132 while (list.size() != 0) { 133 int count = 1; 134 TableDetails table = (TableDetails) list.remove(0); 135 for (int i = 0; i < list.size(); i++) { 136 TableDetails table1 = (TableDetails) list.get(i); 137 if (table == table1) { 138 ++count; 139 list.remove(i); 140 --i; 141 } 142 } 143 mapping.add(new Object [] {table, new Integer (count)}); 144 } 145 return (Object [][]) mapping.toArray(new Object [mapping.size()][2]); 146 } 147 154 private ArrayList getListOfTables(ColumnDetails[] columns) throws DException { 155 ArrayList list = new ArrayList(5); 156 for (int i = 0, length = columnDetails.length; i < length; i++) { 157 list.add(columnDetails[i].getTable()); 158 } 159 return list; 160 } 161 162 165 166 181 public boolean first() throws DException { if (iterator.first()) { 183 state = FORWARD; return (status = CheckForSameGroup() ? VALIDSTATE : AFTERLAST) == VALIDSTATE; 185 }else{ 186 if (mapping != null) { 187 for (int i = 0; i < mapping.length; i++) { 188 ( (_Aggregate) mapping[i][1]).initialize(); 189 } 190 } 191 } 192 status = AFTERLAST; 193 return false; 194 } 195 196 220 public boolean next() throws DException { 221 switch (status) { 222 case INVALIDSTATE: 223 throw new DException("DSE4116", null); 224 case BEFOREFIRST: 225 return first(); 226 case AFTERLAST: 227 return false; 228 default: { 229 if (state == BACKWARD) { 230 try { 231 skipGroupForward(); 232 } catch (DException checkDoc) { 233 checkDoc.printStackTrace(); 234 235 } 236 } 237 if (iterator.next()) { 238 state = FORWARD; 239 return (status = CheckForSameGroup() ? VALIDSTATE : AFTERLAST) == VALIDSTATE; 240 } 241 status = AFTERLAST; 242 return false; 243 } 244 } 245 } 246 247 256 257 private void skipGroupPrevious() throws DException { 258 if (groupByCondition != null) { 259 groupByCondition.setPreviousState(); 260 } while (groupByCondition == null || groupByCondition.compare()) { 261 if (!iterator.previous()) { 262 iterator.next(); 263 return; 264 } 265 } 266 iterator.next(); 267 } 268 277 private void skipGroupForward() throws DException { 278 if (groupByCondition != null) { 279 groupByCondition.setPreviousState(); 280 } while (groupByCondition == null || groupByCondition.compare()) { 281 if (!iterator.next()) { 282 iterator.previous(); 283 return; 284 } 285 } 286 iterator.previous(); 287 288 } 289 290 316 public boolean previous() throws com.daffodilwoods.database.resource.DException { 317 switch (status) { 318 case INVALIDSTATE: 319 throw new DException("DSE4117", null); 320 case BEFOREFIRST: 321 return false; 322 case AFTERLAST: 323 return last(); 324 default: { 325 if (state == FORWARD) { 326 try { 327 skipGroupPrevious(); 328 } catch (DException E) { 330 } 331 } 332 if (iterator.previous()) { 333 state = BACKWARD; 334 return (status = CheckForSameGroupPrevious() ? VALIDSTATE : BEFOREFIRST) == VALIDSTATE; 335 } 336 status = BEFOREFIRST; 337 return false; 338 } 339 } 340 } 341 342 357 public boolean last() throws DException { 358 if (iterator.last()) { 359 state = BACKWARD; return (status = CheckForSameGroupPrevious() ? VALIDSTATE : BEFOREFIRST) == VALIDSTATE; 361 }else{ 362 if (mapping != null) { 363 for (int i = 0; i < mapping.length; i++) { 364 ( (_Aggregate) mapping[i][1]).initialize(); 365 } 366 } 367 } 368 status = BEFOREFIRST; 369 return false; 370 } 371 372 384 protected boolean CheckForSameGroup() throws DException { 385 int len = 0; 386 if (mapping != null) { 387 len = mapping.length; 388 for (int i = 0; i < len; i++) { 389 ( (_Aggregate) mapping[i][1]).initialize(); 390 } 391 } 392 if (groupByCondition != null) { 393 groupByCondition.setPreviousState(); 394 } while (groupByCondition == null || groupByCondition.compare()) { 395 int cnt = 0; 396 if (mapping != null) { 397 evaluateAggregates(); 398 } 399 if (!iterator.next()) { 400 iterator.last(); 401 return true; 402 } 403 } 404 iterator.previous(); 405 return true; 406 } 407 408 420 protected boolean CheckForSameGroupPrevious() throws DException { 421 int len = 0; 422 if (mapping != null) { 423 len = mapping.length; 424 for (int i = 0; i < len; i++) { 425 ( (_Aggregate) mapping[i][1]).initialize(); 426 } 427 } 428 if (groupByCondition != null) { 429 groupByCondition.setPreviousState(); 430 } 431 432 while (groupByCondition == null || groupByCondition.compare()) { 433 if (mapping != null) { 434 evaluateAggregates(); 435 } 436 if (!iterator.previous()) { 437 iterator.first(); 438 return true; 439 } 440 } 441 iterator.next(); 442 return true; 443 } 444 445 453 public void move(Object keys) throws DException { 454 if (mapping != null && groupByCondition == null) { 455 first(); 456 return; 457 } 458 459 iterator.move(keys); 460 switch (state) { 461 case FORWARD: 462 463 skipGroupPrevious(); 464 CheckForSameGroup(); 465 break; 466 case BACKWARD: 467 skipGroupForward(); 468 CheckForSameGroupPrevious(); 469 break; 470 } 471 status = VALIDSTATE; 472 } 473 474 481 public Object getKey() throws DException { 482 if (keyColumnInformation == null) 483 return null; 484 int length = keyColumnInformation.length; 485 Object [] key = new Object [length]; 486 for (int i = 0; i < length; i++) { 487 key[i] = iterator.getColumnValues(keyColumnInformation[i]. 488 getColumnDetails()); 489 } 490 return key; 491 } 492 493 506 507 516 public Object getColumnValues(_Reference[] references) throws DException { 517 518 int len = references.length; 519 Object [] result = new Object [len]; 520 outerLoop:for (int j = 0; j < len; j++) { 521 if (mapping != null) { 522 int length = mapping.length; 523 for (int i = 0; i < length; i++) { 524 if ( ( (ColumnDetails) mapping[i][0]).getColumn().trim().equalsIgnoreCase(references[j].getColumn().trim())) { 525 result[j] = ( (_Aggregate) mapping[i][1]).getResult(); 526 continue outerLoop; 527 } 528 } 529 } 530 531 result[j] = ( (Object []) iterator.getColumnValues(new _Reference[] {references[j]}))[0]; 532 } 533 return result; 534 } 535 536 537 546 public Object getColumnValues(int[] parm1) throws com.daffodilwoods.database.resource.DException { 547 return iterator.getColumnValues(); 548 } 549 550 557 public Object getColumnValues() throws DException { 558 ArrayList list = new ArrayList(); 559 list.addAll(Arrays.asList( (Object []) iterator.getColumnValues())); 560 return list.toArray(new Object [list.size()]); 561 } 562 563 572 public Object getColumnValues(_Reference references) throws DException { 573 Object result = null; 574 if (mapping != null) { 575 int length = mapping.length; 576 for (int i = 0; i < length; i++) { 577 if ( ( (ColumnDetails) mapping[i][0]).getColumn().trim().equalsIgnoreCase(references.getColumn().trim())) { 578 result = ( (_Aggregate) mapping[i][1]).getResult(); 579 return result; 580 } 581 } 582 } 583 result = iterator.getColumnValues(references); 584 return result; 585 } 586 587 595 596 public FieldBase field(_Reference references) throws com.daffodilwoods.database.resource.DException { 597 FieldBase result = null; 598 if (mapping != null) { 599 int length = mapping.length; 600 for (int i = 0; i < length; i++) { 601 if ( ( (ColumnDetails) mapping[i][0]).getColumn().trim().equalsIgnoreCase(references.getColumn().trim())) { 602 result = (FieldBase) ( (_Aggregate) mapping[i][1]).getResult(); 603 return result; 604 } 605 } 606 } 607 result = iterator.field(references); 608 return result; 609 } 610 611 619 public FieldBase[] fields(_Reference[] references) throws com.daffodilwoods.database.resource.DException { 620 int len = references.length; 621 FieldBase[] result = new FieldBase[len]; 622 outerLoop:for (int j = 0; j < len; j++) { 623 if (mapping != null) { 624 int length = mapping.length; 625 for (int i = 0; i < length; i++) { 626 if ( ( (ColumnDetails) mapping[i][0]).getColumn().trim().equalsIgnoreCase(references[j].getColumn().trim())) { 627 result[j] = (FieldBase) ( (_Aggregate) mapping[i][1]).getResult(); 628 continue outerLoop; 629 } 630 } 631 } 632 result[j] = ( (FieldBase[]) iterator.fields(new _Reference[] {references[j]}))[0]; 633 } 634 return result; 635 } 636 637 public FieldBase[] fields(int[] columns) throws com.daffodilwoods.database.resource.DException { 638 throw new java.lang.UnsupportedOperationException ("Method fields() not yet implemented."); 639 } 640 641 644 645 651 public TableDetails[] getTableDetails() throws com.daffodilwoods.database.resource.DException { 652 return tables == null ? iterator.getTableDetails() : tables; 653 } 654 655 656 public byte[] getByteKey() throws DException { 657 int length = keyColumnInformation.length; 658 int resultantLength =0; 659 ArrayList KeyInfo = new ArrayList(5); 660 for(int i=0;i<length;i++){ 661 Object key = iterator.getColumnValues(keyColumnInformation[i]. 662 getColumnDetails()); 663 FieldBase fbKey1 = (FieldBase) key; 664 665 if (fbKey1.isNull()) { 666 KeyInfo.add(null); 667 } 668 else { 669 byte[] columnValues = fbKey1.getBufferRange().getBytes(); 670 KeyInfo.add(columnValues); 671 resultantLength += columnValues.length; 672 } 673 } 674 return getResultantKeyInfo(resultantLength,KeyInfo); 675 } 676 677 private byte[] getResultantKeyInfo(int resultantLen,ArrayList KeyInfo){ 678 byte[] resultantKeyInfo = new byte[resultantLen+KeyInfo.size()]; 679 for(int i=0,j=0;i<KeyInfo.size();i++){ 680 byte[] columnValues = (byte[]) KeyInfo.get(i); 681 682 if (columnValues == null) { 683 resultantKeyInfo[j] = 0; 684 j += 1; 685 } 686 else { 687 resultantKeyInfo[j] = (byte) columnValues.length; 688 System.arraycopy(columnValues, 0, resultantKeyInfo, j + 1, 689 columnValues.length); 690 j += columnValues.length + 1; 691 } 692 } 693 return resultantKeyInfo; 694 695 } 696 697 private byte[] getByte(int index, int lengthOfKey) throws DException { 698 int length = 0; 699 byte[] resultant = new byte[0]; 700 byte[] temp = null; 701 for (int i = index; i < index + lengthOfKey; i++) { 702 FieldBase fb = iterator.field(keyColumnInformation[i].getColumnDetails()); 703 byte[] keys = fb.getBufferRange().getBytes(); 704 length += keys.length; 705 temp = new byte[resultant.length]; 706 System.arraycopy(resultant, 0, temp, 0, temp.length); 707 resultant = new byte[length]; 708 System.arraycopy(temp, 0, resultant, 0, temp.length); 709 System.arraycopy(keys, 0, resultant, temp.length, keys.length); 710 711 } 712 return resultant; 713 714 } 715 716 717 public void moveByteKey(byte[] key) throws DException { 718 for(int i=0,j=0;i<key.length && j< objectToMove.length ;){ 719 720 BufferRange bf = key[i]==0?new BufferRange(true):new BufferRange(key,i+1,key[i]); 721 ((FieldBase)objectToMove[j]).setBufferRange(bf); 722 i+=key[i]+1; 723 j++; 724 } 725 try { 726 move(objectToMove); 727 } 728 catch (DException ex) { 729 throw ex; 730 } 731 } 732 733 734 739 public _ExecutionPlan getExecutionPlan() throws DException { 740 _ExecutionPlan plan = iterator.getExecutionPlan(); 741 _ExecutionPlan cplans[] = plan == null ? null : new _ExecutionPlan[] {plan}; 742 String aggColumns = getAggerigateColumns(); 743 return new ExecutionPlan("GroupByIterator", cplans, "" + groupByCondition, null, aggColumns); 744 } 745 746 751 public ExecutionPlanForBrowser getExecutionPlanForBrowser() throws DException { 752 ExecutionPlanForBrowser plan = iterator.getExecutionPlanForBrowser(); 753 ExecutionPlanForBrowser cplans[] = plan == null ? null : new ExecutionPlanForBrowser[] {plan}; 754 String aggColumns = getAggerigateColumns(); 755 return new ExecutionPlanForBrowser("Group By " + aggColumns, "GroupByIterator", cplans, "" + groupByCondition, null, null); 756 } 757 758 763 public void releaseResource() throws DException { 764 iterator.releaseResource(); 765 if (mapping != null) { 766 for (int i = 0; i < mapping.length; i++) { 767 _Aggregate aggregate = (_Aggregate) mapping[i][1]; 768 aggregate.releaseResource(); 769 } 770 } 771 } 772 773 779 public _KeyColumnInformation[] getKeyColumnInformations() throws DException { 780 781 if (columnDetails == null) 782 return iterator.getKeyColumnInformations(); 783 _KeyColumnInformation[] key = new _KeyColumnInformation[columnDetails. 784 length]; 785 for (int i = 0; i < columnDetails.length; i++) { 786 key[i] = new TableKeyColumnInformation(columnDetails[i], false); 787 } 788 return key; 789 790 } 791 792 812 813 820 private boolean matched(ColumnDetails cd) throws DException { 821 for (int i = 0, length = columnDetails.length; i < length; i++) { 822 823 if (columnDetails[i].getTable() == cd.getTable() && columnDetails[i].getAppropriateColumn().equalsIgnoreCase(cd.getAppropriateColumn())) { 824 return true; 825 } 826 } 827 return false; 828 } 829 830 835 private String getAggerigateColumns() throws DException { 836 if (mapping == null || mapping.length == 0) { 837 return null; 838 } 839 String name = ""; 840 int length = mapping.length - 1, i = 0; 841 for (; i < length; i++) { 842 name += "Aggregate[" + i + "] = " + mapping[i][0] + ","; 843 } 844 name += "Aggregate[" + i + "] =" + mapping[i][0]; 845 return name; 846 } 847 848 857 public _Iterator getBaseIterator(ColumnDetails column) throws com.daffodilwoods.database.resource.DException { 858 return this; 859 } 860 861 869 public void setConditionVariableValue(_Reference[] references, Object [] values, int priority) throws DException { 870 if(underlyingRef!=null){ 871 references = GeneralPurposeStaticClass.getJointReferences(references,underlyingRef); 872 values = GeneralPurposeStaticClass.getJointValues(this, values,underlyingRef.length); 873 } 874 variableValues. addReferences(references); 875 variableValues.setConditionVariableValue(references, values, priority); 876 877 iterator.setConditionVariableValue(references, values, priority); 878 if (groupByCondition != null) { 879 groupByCondition.setConditionVariableValue(references, values, priority); 880 } 881 } 882 883 884 889 890 public void setKeyCount(Object [][] tableAndKeyCount) throws DException { 891 } 892 893 public Object [][] getFunctionalColumnMapping() throws DException { 894 return iterator.getFunctionalColumnMapping(); 895 } 896 897 898 public String toString() { 899 String str = "GROUPBYITERATOR"; 900 try { 901 if (iterator != null) { 902 str += "[" + iterator.toString() + "]"; 903 } 904 } catch (Exception e) { 905 e.printStackTrace(); 906 } 907 return str; 908 } 909 protected void evaluateAggregates() throws DException{ 910 try { 911 int len = mapping.length; 912 Object obj[] = new Object [len]; 913 for (int i = 0; i < len; i++) { 914 valueexpression ve=( (_Aggregate) mapping[i][1]).getValueExpression(); 915 if(ve!=null){ 916 obj[i] = ve.run(variableValues); 917 }else 918 obj[i]=null; 919 } 920 for (int i = 0; i < len; i++) { 921 ( (_Aggregate) mapping[i][1]).addRecord(obj[i]); 922 } 923 } catch (DException ex) { 924 if(ex.getDseCode().equalsIgnoreCase("DSE2004")) 925 return; 926 throw ex; 927 } 928 } 929 } 930 | Popular Tags |