KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > sql99 > dql > execution > AggregateMax


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 /**
17  * <p>Title: AggregateMax </p>
18  * <p>Description:
19  * This class is responsible for computing MAXIMUM VALUE out of the values
20  * from the records belonging to a particular group. </p>
21  * <p>Copyright: Copyright (c) 2004</p>
22  * <p>Company: </p>
23  * @author not attributable
24  * @version 1.0
25  */

26 public class AggregateMax
27     implements _Aggregate, Datatypes {
28
29   /**
30    * Value representing the maximum value out of all the values
31    * corresponding to a particular group.
32    */

33   private FieldBase result;
34
35   /**
36    * Instance variable representing the expression/column involed
37    * in the avg function argument.
38    */

39   private valueexpression column;
40
41   /**
42    * Comparator i.e. used to compare the two different numeric
43    * values corresponding to two rows of a particular group.
44    */

45   private SuperComparator comparator;
46
47   /**
48    * data type of the maximum value.
49    */

50   private int dataType;
51
52
53   private boolean forQuestion;
54
55   /**
56    * Initializes the value expression and its data type.
57    * @param column0 represents the value expression passed as argument to
58    * MAX function
59    * @throws DException (a) throws an exception when aggregate function is
60    * performed on column of data type BLOB, CLOB and Boolean data type.
61    * (b) throws an exception when the cardinality of the value expression
62    * passed as a argument to MAX function is more than one.
63    */

64   public AggregateMax(valueexpression column0) throws DException {
65     column = column0;
66     if (column0.getCardinality() > 1) {
67       throw new DException("DSE4119", new Object JavaDoc[] {"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 /*done by vibha to solve prob of ? in agg function on 13-08-2004*/
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   /**
105    * Initializes the MAXIMUM value to NULL when a particular group starts.
106    * @throws DException
107    */

108   public void initialize() throws DException {
109     result = FieldUtility.getField(dataType, FieldUtility.NULLBUFFERRANGE);
110   }
111
112   /**
113    * This method is required to retrieve the MAXIMUM, out of all the values
114    * of a particular group.
115    * @return null if there are no records in table otherwise,
116    * maximum value in the group.
117    * @throws DException
118    */

119   public Object JavaDoc getResult() throws DException { // null check for aggregate case when one record has to be shown in case of no records
120
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           /*column.getColumnDetails()[0].getSize()*/5, newObject.getCollator());// commented by Sandeep to solve bug-12544 as suggested by Parveen Sir on 18/01/2005
130
}
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   /**
143    * This method is required to initialize the first non- null value in the
144    * group to MAXIMUM value.
145    * @param newObject
146    * @throws DException
147    */

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             /*column.getColumnDetails()[0].getSize()*/ 5, newObject.getCollator()); // commented by Sandeep to solve bug-12544 as suggested by Parveen Sir on 18/01/2005
160
}
161       else if ( (dataType == CHAR || dataType == CHARACTER))
162       {
163     /* Check is removed by vibha on 17-08-2004 to solve NP in case of char when data entered from shell*/
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   /**
183    * This method is used to release the numeric comparator hold by this class.
184    * whenever a new value having a different type as compared to previous
185    * value, comparator is initialized according to data types of value passed
186    * after releasing the previously hold comaprator.
187    * @throws DException
188    */

189   public void releaseResource() throws DException {
190     comparator = null;
191   }
192
193   public valueexpression getValueExpression() {
194     return column;
195   }
196
197   /**
198    * This method is responsible to add a new record in to a group and to update
199    * maximum value correspondingly. For first non-null value in the group,
200    * maximum value is initialized to first value. NULL values are ignored in max
201    * function computation.
202    * The comaprator may be null due to values of distinct data type passed, it
203    * will throw the nullpointerexception, which is catched to reinitialize the
204    * comparator.
205    * @param iterator group by iterator retriving the values of columns
206    * involved in aggregate computation.
207    * @throws DException
208    */

209
210   public void addRecord(Object JavaDoc 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      /* Done by Manoj Kr. for Bug. No. 12462*/
230        if(newObject.bufferRange == null)
231            newObject.getBufferRange();
232       result = comparator.compare( (Object JavaDoc) result, (Object JavaDoc) newObject) < 0 ?
233           reinitializeBufferRangeAndObject(newObject) : result;
234     }
235     catch (NullPointerException JavaDoc 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 JavaDoc) result, (Object JavaDoc) newObject) < 0 ?
246           reinitializeBufferRangeAndObject(newObject) : result;
247     }
248   }
249 }
250
Popular Tags