1 package com.daffodilwoods.daffodildb.server.sql99.dql.execution; 2 3 import java.text.*; 4 5 import com.daffodilwoods.daffodildb.server.sql99.common.*; 6 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 7 import com.daffodilwoods.daffodildb.server.sql99.expression.*; 8 import com.daffodilwoods.daffodildb.server.sql99.utils.*; 9 import com.daffodilwoods.daffodildb.utils.*; 10 import com.daffodilwoods.daffodildb.utils.comparator.*; 11 import com.daffodilwoods.daffodildb.utils.field.*; 12 import com.daffodilwoods.database.resource.*; 13 import com.daffodilwoods.daffodildb.server.sql99.expression.valueexpression; 14 import com.daffodilwoods.database.utility.P; 15 16 26 public class AggregateMin implements _Aggregate, Datatypes { 27 28 32 private FieldBase result; 33 37 private valueexpression column; 38 42 private SuperComparator comparator; 43 46 private int dataType; 47 48 private boolean forQuestion; 49 58 public AggregateMin(valueexpression column0) throws DException { 59 column = column0; 60 if (column0.getCardinality() > 1) { 61 throw new DException("DSE4119", new Object [] {"AGGREGATE FUNCTION MIN"}); 62 } 63 if(column0.getCardinality()==-1){ 64 if (column0.getColumnDetails()[0].getQuestion()) 65 column0.getColumnDetails()[0].setDatatype(16); 66 return; 67 } 68 69 70 ColumnDetails[] cd = column0.getColumnDetails(); 71 _Reference[] ref1 =null ; 72 for (int i = 0; i < cd.length; i++) { 73 if(cd[i].getType() != TypeConstants.CONSTANT ){ 74 ref1 = column0.getReferences(new TableDetails[] {column0.getColumnDetails()[i].getTable()}); 75 if(ref1!=null){ 76 for (int j = 0; j < ref1.length; j++) { 77 if (ref1[i].getReferenceType() == SimpleConstants.VARIABLECOLUMN ) { 78 dataType = Datatypes.BIGDECIMAL; 79 return; 80 } 81 }} 82 } 83 } 84 85 86 87 88 GeneralPurposeStaticClass.checkForValidColumnsInAggregatesMaxMinAndCount(column0.getColumnDetails(), "MIN"); 89 ByteComparison byteComparison = column0.getByteComparison(null); 90 91 dataType = column0.getColumnDetails()[0].getDatatype(); 92 forQuestion=true; 93 } 94 95 99 public void initialize() throws DException { 100 result = FieldUtility.getField(dataType, FieldUtility.NULLBUFFERRANGE); 101 } 102 103 110 public Object getResult() throws DException { 111 return result == null ? FieldUtility.NULLFIELDBASE : (FieldBase) result; 112 } 113 114 private FieldBase reinitializeBufferRangeAndObject(FieldBase newObject) throws DException { 115 if (dataType == DEC || dataType == NUMERIC || dataType == DECIMAL || dataType == BIGDECIMAL) { 116 result = FieldUtility.convertToAppropriateType(newObject, newObject.getDatatype(), 38, 5, newObject.getCollator()); } else if ( (dataType == CHAR || dataType == CHARACTER) && column.getColumnDetails()[0].getSize() < 0) { 118 result = FieldUtility.convertToAppropriateType( ( (FieldBase) newObject), VARCHAR, 4192, newObject.getCollator()); 119 } else 120 result = FieldUtility.convertToAppropriateType(newObject, newObject.getDatatype(), newObject.getSize(), newObject.getCollator()); 121 return result; 122 } 123 124 130 private void initializeFirstValue(FieldBase newObject) throws DException { 131 if (dataType == -1) { 132 result = FieldUtility.convertToAppropriateType( ( (FieldBase) newObject), ( (FieldBase) newObject).getDatatype(), column.getColumnDetails()[0].getSize(), newObject.getCollator()); 133 } else { 134 if (dataType == DEC || dataType == NUMERIC || dataType == DECIMAL || dataType == BIGDECIMAL) { 135 result = FieldUtility.convertToAppropriateType( ( (FieldBase) newObject), ( (FieldBase) newObject).getDatatype(), 38, 5, newObject.getCollator()); }else if ( (dataType == CHAR || dataType == CHARACTER)) 137 138 { 139 result = FieldUtility.convertToAppropriateType( ( (FieldBase) newObject), VARCHAR, 4192, newObject.getCollator()); 140 } 141 else{ 142 ColumnDetails cd = column.getColumnDetails()[0]; 143 if (!forQuestion) { 144 result = FieldUtility.convertToAppropriateType( ( (FieldBase) newObject), cd.getDatatype(), cd.getSize(), newObject.getCollator()); 145 } 146 else { 147 result = FieldUtility.convertToAppropriateType( ( (FieldBase)newObject), dataType, newObject.getSize(), newObject.getCollator()); 148 } 149 } 150 151 } 152 } 153 154 161 public void releaseResource() throws DException { 162 comparator = null; 163 } 164 165 public valueexpression getValueExpression() { 166 return column; 167 } 168 169 181 182 public void addRecord(Object newObject0) throws DException{ 183 FieldBase newObject=(FieldBase)newObject0; 184 185 if(!forQuestion){ 186 forQuestion = true; 187 dataType = newObject.getDatatype(); 188 if (dataType == -1){ 189 newObject.getBytes(); 190 191 } 192 dataType=newObject.getDatatype(); 193 } 194 195 if (result == null || result.getNull()) { 196 initializeFirstValue(newObject); 197 return; 198 } 199 200 if (newObject.isNull()) { 201 return; 202 } 203 try { 204 205 if (newObject.bufferRange == null) 206 newObject.getBufferRange(); 207 result = comparator.compare( (Object ) result, (Object ) newObject) > 0 ? reinitializeBufferRangeAndObject(newObject) : result; 208 } catch (NullPointerException ex) { 209 ColumnDetails[] cd = column.getColumnDetails(); 210 Collator collator = null; 211 for (int i = 0, length = cd.length; i < length && collator == null; i++) { 212 if (cd[i].getType() == cd[i].REFERENCE) { 213 collator = cd[i].getTable().getColumnCharacteristics().getCollator(); 214 } 215 } 216 comparator = dataType == -1 ? new CPckfduDpnqbsbups() : GetByteComparator.getComparator(dataType, false, collator); 217 result = comparator.compare( (Object ) result, (Object ) newObject) > 0 ? reinitializeBufferRangeAndObject(newObject) : result; 218 } 219 220 } 221 222 } 223 | Popular Tags |