KickJava   Java API By Example, From Geeks To Geeks.

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


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: AggregateMin </p>
18  * <p>Description:
19  * This class is responsible for computing MINIMUM 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 AggregateMin implements _Aggregate, Datatypes {
27
28   /**
29    * Value representing the minimum value out of all the values
30    * corresponding to a particular group.
31    */

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

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

42    private SuperComparator comparator;
43    /**
44     * data type of the minimum value.
45     */

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

58    public AggregateMin(valueexpression column0) throws DException {
59       column = column0;
60       if (column0.getCardinality() > 1) {
61          throw new DException("DSE4119", new Object JavaDoc[] {"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       /*done by vibha to solve prob of ? in agg function on 13-08-2004*/
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    /**
96     * Initializes the MINIMUM value to NULL when a particular group starts.
97     * @throws DException
98     */

99    public void initialize() throws DException {
100         result = FieldUtility.getField(dataType, FieldUtility.NULLBUFFERRANGE);
101    }
102
103    /**
104     * This method is required to retrieve the MINIMUM, out of all the values
105     * of a particular group.
106     * @return null if value of column involved in min is null for all the rows
107     * of group otherwise minimum value in the group.
108     * @throws DException
109     */

110    public Object JavaDoc 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, /*newObject.getSize()*/ 5, newObject.getCollator()); // commented by Sandeep to solve bug-12544 as suggested by Parveen Sir on 18/01/2005
117
} 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    /**
125     * This method is required to initialize the first non- null value in the
126     * group to MINIMUM value.
127     * @param newObject
128     * @throws DException
129     */

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,/* column.getColumnDetails()[0].getSize()*/ 5, newObject.getCollator());// commented by Sandeep to solve bug-12544 as suggested by Parveen Sir on 18/01/2005
136
}else if ( (dataType == CHAR || dataType == CHARACTER))
137 /*commented by vibha to solve NP in case of character on 17-08-2004*/
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    /**
155     * This method is used to release the numeric comparator hold by this class.
156     * whenever a new value having a different type as compared to previous
157     * value, comparator is initialized according to data types of value passed
158     * after releasing the previously hold comaprator.
159     * @throws DException
160     */

161    public void releaseResource() throws DException {
162       comparator = null;
163    }
164
165    public valueexpression getValueExpression() {
166       return column;
167    }
168
169    /**
170       * This method is responsible to add a new record in to a group and to update
171       * minimum value correspondingly. For first non-null value in the group,
172       * minimum value is initialized to first value. NULL values are ignored in
173       * minimum function computation.
174       * The comaprator may be null due to values of distinct data type passed, it
175       * will throw the nullpointerexception, which is catched to reinitialize the
176       * comparator.
177       * @param iterator group by iterator retriving the values of columns
178       * involved in aggregate computation.
179       * @throws DException
180       */

181
182    public void addRecord(Object JavaDoc 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          /* Done by Manoj Kr. for Bug. No. 12462*/
205          if (newObject.bufferRange == null)
206             newObject.getBufferRange();
207          result = comparator.compare( (Object JavaDoc) result, (Object JavaDoc) newObject) > 0 ? reinitializeBufferRangeAndObject(newObject) : result;
208       } catch (NullPointerException JavaDoc 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 JavaDoc) result, (Object JavaDoc) newObject) > 0 ? reinitializeBufferRangeAndObject(newObject) : result;
218       }
219
220   }
221
222 }
223
Popular Tags