| 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 AggregateMax 27 implements _Aggregate, Datatypes { 28 29 33 private FieldBase result; 34 35 39 private valueexpression column; 40 41 45 private SuperComparator comparator; 46 47 50 private int dataType; 51 52 53 private boolean forQuestion; 54 55 64 public AggregateMax(valueexpression column0) throws DException { 65 column = column0; 66 if (column0.getCardinality() > 1) { 67 throw new DException("DSE4119", new Object [] {"AGGREGATE FUNCTION MAX"}); 68 } 69 70 if (column0.getCardinality() == -1) { 71 if (column0.getColumnDetails()[0].getQuestion()){ 72 column0.getColumnDetails()[0].setDatatype(16); 73 } 74 return; 75 } 76 77 78 79 80 ColumnDetails[] cd = column0.getColumnDetails(); 81 _Reference[] ref1 =null ; 82 for (int i = 0; i < cd.length; i++) { 83 if(cd[i].getType() != TypeConstants.CONSTANT ){ 84 ref1 = column0.getReferences(new TableDetails[] {column0.getColumnDetails()[i].getTable()}); 85 if(ref1!=null){ 86 for (int j = 0; j < ref1.length; j++) { 87 if (ref1[i].getReferenceType() == SimpleConstants.VARIABLECOLUMN ) { 88 dataType = Datatypes.BIGDECIMAL; 89 return; 90 } 91 }} 92 } 93 } 94 95 96 GeneralPurposeStaticClass.checkForValidColumnsInAggregatesMaxMinAndCount( 97 column0.getColumnDetails(), "MAX"); 98 ByteComparison byteComparison = column0.getByteComparison(null); 99 dataType = column0.getColumnDetails()[0].getDatatype(); 100 101 forQuestion = true; 102 } 103 104 108 public void initialize() throws DException { 109 result = FieldUtility.getField(dataType, FieldUtility.NULLBUFFERRANGE); 110 } 111 112 119 public Object getResult() throws DException { return result == null ? FieldUtility.NULLFIELDBASE : (FieldBase) result; 121 } 122 123 private FieldBase reinitializeBufferRangeAndObject(FieldBase newObject) throws 124 DException { 125 if (dataType == DEC || dataType == NUMERIC || dataType == DECIMAL || 126 dataType == BIGDECIMAL) { 127 result = FieldUtility.convertToAppropriateType( ( (FieldBase) newObject), 128 ( (FieldBase) newObject).getDatatype(), 38, 129 5, newObject.getCollator()); } 131 else if ( (dataType == CHAR || dataType == CHARACTER) && 132 column.getColumnDetails()[0].getSize() < 0) { 133 result = FieldUtility.convertToAppropriateType( ( (FieldBase) newObject), 134 VARCHAR, 4192, newObject.getCollator()); 135 } 136 else 137 result = FieldUtility.convertToAppropriateType(newObject, 138 newObject.getDatatype(), newObject.getSize(), newObject.getCollator()); 139 return result; 140 } 141 142 148 private void initializeFirstValue(FieldBase newObject) throws DException { 149 if (dataType == -1) { 150 result = FieldUtility.convertToAppropriateType( ( (FieldBase) newObject), 151 newObject.getDatatype(), column.getColumnDetails()[0].getSize(), 152 newObject.getCollator()); 153 } 154 else { 155 if (dataType == DEC || dataType == NUMERIC || dataType == DECIMAL || 156 dataType == BIGDECIMAL) { 157 result = FieldUtility.convertToAppropriateType( ( (FieldBase) newObject), 158 ( (FieldBase) newObject).getDatatype(), 38, 159 5, newObject.getCollator()); } 161 else if ( (dataType == CHAR || dataType == CHARACTER)) 162 { 163 164 result = FieldUtility.convertToAppropriateType( ( (FieldBase) newObject), 165 VARCHAR, 4192, newObject.getCollator()); 166 } 167 168 else { 169 ColumnDetails cd = column.getColumnDetails()[0]; 170 if(!forQuestion){ 171 result = FieldUtility.convertToAppropriateType( ( (FieldBase) 172 newObject), cd.getDatatype(), cd.getSize(), newObject.getCollator()); 173 } 174 else { 175 result = FieldUtility.convertToAppropriateType( ( (FieldBase) 176 newObject), dataType, newObject.getSize(), newObject.getCollator()); 177 } 178 } 179 } 180 } 181 182 189 public void releaseResource() throws DException { 190 comparator = null; 191 } 192 193 public valueexpression getValueExpression() { 194 return column; 195 } 196 197 209 210 public void addRecord(Object newObject0) throws DException { 211 FieldBase newObject = (FieldBase) newObject0; 212 213 if (!forQuestion) { 214 forQuestion = true; 215 dataType = newObject.getDatatype(); 216 if (dataType == -1) 217 newObject.getBytes(); 218 dataType = newObject.getDatatype(); 219 } 220 if (result == null || result.getNull()) { 221 initializeFirstValue(newObject); 222 return; 223 } 224 225 if (newObject.isNull()) { 226 return; 227 } 228 try { 229 230 if(newObject.bufferRange == null) 231 newObject.getBufferRange(); 232 result = comparator.compare( (Object ) result, (Object ) newObject) < 0 ? 233 reinitializeBufferRangeAndObject(newObject) : result; 234 } 235 catch (NullPointerException ex) { 236 ColumnDetails[] cd = column.getColumnDetails(); 237 Collator collator = null; 238 for (int i = 0, length = cd.length; i < length && collator == null; i++) { 239 if (cd[i].getType() == cd[i].REFERENCE) { 240 collator = cd[i].getTable().getColumnCharacteristics().getCollator(); 241 } 242 } 243 comparator = dataType == -1 ? new CPckfduDpnqbsbups() : 244 GetByteComparator.getComparator(dataType, false, collator); 245 result = comparator.compare( (Object ) result, (Object ) newObject) < 0 ? 246 reinitializeBufferRangeAndObject(newObject) : result; 247 } 248 } 249 } 250 | Popular Tags |