KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > execute > GenericAggregateResultSet


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.execute.GenericAggregateResultSet
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.execute;
23
24 import org.apache.derby.iapi.services.monitor.Monitor;
25
26 import org.apache.derby.iapi.services.sanity.SanityManager;
27
28 import org.apache.derby.iapi.services.stream.HeaderPrintWriter;
29 import org.apache.derby.iapi.services.stream.InfoStreams;
30
31 import org.apache.derby.iapi.services.io.Formatable;
32
33 import org.apache.derby.iapi.sql.execute.CursorResultSet;
34 import org.apache.derby.iapi.sql.ResultSet;
35 import org.apache.derby.iapi.sql.execute.ExecRow;
36 import org.apache.derby.iapi.sql.execute.ExecIndexRow;
37 import org.apache.derby.iapi.sql.execute.NoPutResultSet;
38
39 import org.apache.derby.iapi.sql.Activation;
40
41 import org.apache.derby.iapi.store.access.ColumnOrdering;
42 import org.apache.derby.iapi.store.access.SortObserver;
43 import org.apache.derby.iapi.store.access.TransactionController;
44 import org.apache.derby.iapi.store.access.ScanController;
45
46 import org.apache.derby.iapi.jdbc.ConnectionContext;
47
48 import org.apache.derby.iapi.services.loader.GeneratedMethod;
49 import org.apache.derby.iapi.services.loader.ClassFactory;
50
51 import org.apache.derby.iapi.sql.execute.ExecutionContext;
52 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
53
54 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
55 import org.apache.derby.iapi.error.StandardException;
56
57 import org.apache.derby.iapi.types.RowLocation;
58 import org.apache.derby.iapi.reference.SQLState;
59
60 import org.apache.derby.iapi.services.io.FormatableArrayHolder;
61 import org.apache.derby.impl.jdbc.EmbedSQLWarning;
62
63 import java.util.Properties JavaDoc;
64 import java.util.Vector JavaDoc;
65 import java.sql.SQLException JavaDoc;
66
67 /**
68  * Generic aggregation utilities.
69  *
70  * @author jamie
71  */

72 abstract class GenericAggregateResultSet extends NoPutResultSetImpl
73 {
74     protected GenericAggregator[] aggregates;
75     protected GeneratedMethod rowAllocator;
76     protected AggregatorInfoList aggInfoList;
77     public NoPutResultSet source;
78     protected NoPutResultSet originalSource; // used for run time stats only
79

80     /**
81      * Constructor
82      *
83      * @param a activation
84      * @param ra row allocator generated method
85      * @param resultSetNumber result set number
86      * @param optimizerEstimatedRowCount optimizer estimated row count
87      * @param optimizerEstimatedCost optimizer estimated cost
88      *
89      * @exception StandardException Thrown on error
90      */

91     GenericAggregateResultSet
92     (
93         NoPutResultSet s,
94         int aggregateItem,
95         Activation a,
96         GeneratedMethod ra,
97         int resultSetNumber,
98         double optimizerEstimatedRowCount,
99         double optimizerEstimatedCost
100     )
101         throws StandardException
102     {
103         super(a, resultSetNumber, optimizerEstimatedRowCount, optimizerEstimatedCost);
104         source = s;
105         originalSource = s;
106
107
108         rowAllocator = ra;
109
110         aggInfoList = (AggregatorInfoList) (a.getPreparedStatement().getSavedObject(aggregateItem));
111         aggregates = getSortAggregators(aggInfoList, false,
112                 a.getLanguageConnectionContext(), s);
113     }
114
115     /**
116      * For each AggregatorInfo in the list, generate a
117      * GenericAggregator and stick it in an array of
118      * GenericAggregators.
119      *
120      * @param list the list of aggregators to set up
121      * @param eliminateDistincts should distincts be ignored.
122      * Used to toss out distinct aggregates for a prelim
123      * sort.
124      * @param lcc the lcc
125      * @param inputResultSet the incoming result set
126      *
127      * @return the array of GenericAggregators
128      *
129      * @exception StandardException on error
130      */

131     protected final GenericAggregator[] getSortAggregators
132     (
133         AggregatorInfoList list,
134         boolean eliminateDistincts,
135         LanguageConnectionContext lcc,
136         NoPutResultSet inputResultSet
137     ) throws StandardException
138     {
139         GenericAggregator aggregators[];
140         Vector JavaDoc tmpAggregators = new Vector JavaDoc();
141         ClassFactory cf = lcc.getLanguageConnectionFactory().getClassFactory();
142
143         int count = list.size();
144         for (int i = 0; i < count; i++)
145         {
146             AggregatorInfo aggInfo = (AggregatorInfo) list.elementAt(i);
147             if (! (eliminateDistincts && aggInfo.isDistinct()))
148             // if (eliminateDistincts == aggInfo.isDistinct())
149
{
150                 tmpAggregators.addElement(new GenericAggregator(aggInfo, cf));
151             }
152         }
153
154
155
156         aggregators = new GenericAggregator[tmpAggregators.size()];
157         tmpAggregators.copyInto(aggregators);
158         // System.out.println("size of sort aggregates " + tmpAggregators.size());
159

160         return aggregators;
161     }
162
163     /**
164      * Finish the aggregation for the current row.
165      * Basically call finish() on each aggregator on
166      * this row. Called once per grouping on a vector
167      * aggregate or once per table on a scalar aggregate.
168      *
169      * If the input row is null, then rowAllocator is
170      * invoked to create a new row. That row is then
171      * initialized and used for the output of the aggregation.
172      *
173      * @param row the row to finish aggregation
174      *
175      * @return the result row. If the input row != null, then
176      * the result row == input row
177      *
178      * @exception StandardException Thrown on error
179      */

180     protected final ExecIndexRow finishAggregation(ExecIndexRow row)
181         throws StandardException
182     {
183         int size = aggregates.length;
184
185         /*
186         ** If the row in which we are to place the aggregate
187         ** result is null, then we have an empty input set.
188         ** So we'll have to create our own row and set it
189         ** up. Note: we needn't initialize in this case,
190         ** finish() will take care of it for us.
191         */

192         if (row == null)
193         {
194             row = getExecutionFactory().getIndexableRow((ExecRow) rowAllocator.invoke(activation));
195         }
196
197         setCurrentRow(row);
198         currentRow = row;
199
200         boolean eliminatedNulls = false;
201         for (int i = 0; i < size; i++)
202         {
203             GenericAggregator currAggregate = aggregates[i];
204             if (currAggregate.finish(row))
205                 eliminatedNulls = true;
206         }
207
208         if (eliminatedNulls)
209             addWarning(EmbedSQLWarning.newEmbedSQLWarning(SQLState.LANG_NULL_ELIMINATED_IN_SET_FUNCTION));
210     
211         return row;
212     }
213
214     public void finish() throws StandardException {
215         source.finish();
216         super.finish();
217     }
218
219 }
220
Popular Tags