001: /*
002:
003: Derby - Class org.apache.derby.impl.sql.execute.AggregatorInfo
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.impl.sql.execute;
023:
024: import org.apache.derby.iapi.sql.ResultDescription;
025: import org.apache.derby.iapi.services.io.StoredFormatIds;
026: import org.apache.derby.iapi.services.io.FormatIdUtil;
027: import org.apache.derby.iapi.services.io.Formatable;
028: import org.apache.derby.iapi.services.sanity.SanityManager;
029:
030: import java.io.ObjectOutput;
031: import java.io.ObjectInput;
032: import java.io.IOException;
033:
034: /**
035: * This is a simple class used to store the run time information
036: * needed to invoke an aggregator. This class is serializable
037: * because it is stored with the plan. It is serializable rather
038: * than externalizable because it isn't particularly complicated
039: * and presumbably we don't need version control on plans.
040: *
041: * @author jamie
042: */
043: public class AggregatorInfo implements Formatable {
044: /********************************************************
045: **
046: ** This class implements Formatable. That means that it
047: ** can write itself to and from a formatted stream. If
048: ** you add more fields to this class, make sure that you
049: ** also write/read them with the writeExternal()/readExternal()
050: ** methods.
051: **
052: ** If, inbetween releases, you add more fields to this class,
053: ** then you should bump the version number emitted by the getTypeFormatId()
054: ** method. OR, since this is something that is used
055: ** in stored prepared statements, it is ok to change it
056: ** if you make sure that stored prepared statements are
057: ** invalidated across releases.
058: **
059: ********************************************************/
060:
061: /*
062: ** See the constructor for the meaning of these fields
063: */
064: String aggregateName;
065: int inputColumn;
066: int outputColumn;
067: int aggregatorColumn;
068: String aggregatorClassName;
069: boolean isDistinct;
070: ResultDescription rd;
071:
072: /**
073: * Niladic constructor for Formattable
074: */
075: public AggregatorInfo() {
076: }
077:
078: /**
079: * Consructor
080: *
081: * @param aggregateName the name of the aggregate. Not
082: * actually used anywhere except diagnostics. Should
083: * be the names as found in the language (e.g. MAX).
084: * @param aggregatorClassName the name of the aggregator
085: * used to process this aggregate. Aggregator expected
086: * to have a null arg constructor and implement
087: * Aggregator.
088: * @param inputColNum the input column number
089: * @param outputColNum the output column number
090: * @param aggregatorColNum the column number in which the
091: * aggregator is stored.
092: * @param isDistinct if it is a distinct aggregate
093: * @param rd the result description
094: *
095: */
096: public AggregatorInfo(String aggregateName,
097: String aggregatorClassName, int inputColNum,
098: int outputColNum, int aggregatorColNum, boolean isDistinct,
099: ResultDescription rd) {
100: this .aggregateName = aggregateName;
101: this .aggregatorClassName = aggregatorClassName;
102: this .inputColumn = inputColNum;
103: this .outputColumn = outputColNum;
104: this .aggregatorColumn = aggregatorColNum;
105: this .isDistinct = isDistinct;
106: this .rd = rd;
107: }
108:
109: /**
110: * Get the name of the aggergate (e.g. MAX)
111: *
112: * @return the aggeregate name
113: */
114: public String getAggregateName() {
115: return aggregateName;
116: }
117:
118: /**
119: * Get the name of the class that implements the user
120: * aggregator for this class.
121: *
122: * @return the aggeregator class name
123: */
124: public String getAggregatorClassName() {
125: return aggregatorClassName;
126: }
127:
128: /**
129: * Get the column number for the aggregator
130: * column.
131: *
132: * @return the aggeregator colid
133: */
134: public int getAggregatorColNum() {
135: return aggregatorColumn;
136: }
137:
138: /**
139: * Get the column number for the input
140: * (addend) column.
141: *
142: * @return the aggeregator colid
143: */
144: public int getInputColNum() {
145: return inputColumn;
146: }
147:
148: /**
149: * Get the column number for the output
150: * (result) column.
151: *
152: * @return the aggeregator colid
153: */
154: public int getOutputColNum() {
155: return outputColumn;
156: }
157:
158: /**
159: * Is the aggergate distinct
160: *
161: * @return whether it is distinct
162: */
163: public boolean isDistinct() {
164: return isDistinct;
165: }
166:
167: /**
168: * Get the result description for the input value
169: * to this aggregate.
170: *
171: * @return the rd
172: */
173: public ResultDescription getResultDescription() {
174: return rd;
175: }
176:
177: /**
178: * Get a string for the object
179: *
180: * @return string
181: */
182: public String toString() {
183: if (SanityManager.DEBUG) {
184: return "AggregatorInfo = Name: " + aggregateName
185: + "\n\tClass: " + aggregatorClassName
186: + "\n\tInputColNum: " + inputColumn
187: + "\n\tOutputColNum: " + outputColumn
188: + "\n\tAggregatorColNum: " + aggregatorColumn
189: + "\n\tDistinct: " + isDistinct + "\n" + rd;
190: } else {
191: return "";
192: }
193: }
194:
195: //////////////////////////////////////////////
196: //
197: // FORMATABLE
198: //
199: //////////////////////////////////////////////
200: /**
201: * Write this object out
202: *
203: * @param out write bytes here
204: *
205: * @exception IOException thrown on error
206: */
207: public void writeExternal(ObjectOutput out) throws IOException {
208: out.writeObject(aggregateName);
209: out.writeInt(inputColumn);
210: out.writeInt(outputColumn);
211: out.writeInt(aggregatorColumn);
212: out.writeObject(aggregatorClassName);
213: out.writeBoolean(isDistinct);
214: out.writeObject(rd);
215: }
216:
217: /**
218: * Read this object from a stream of stored objects.
219: *
220: * @param in read this.
221: *
222: * @exception IOException thrown on error
223: * @exception ClassNotFoundException thrown on error
224: */
225: public void readExternal(ObjectInput in) throws IOException,
226: ClassNotFoundException {
227: aggregateName = (String) in.readObject();
228: inputColumn = in.readInt();
229: outputColumn = in.readInt();
230: aggregatorColumn = in.readInt();
231: aggregatorClassName = (String) in.readObject();
232: isDistinct = in.readBoolean();
233: rd = (ResultDescription) in.readObject();
234: }
235:
236: /**
237: * Get the formatID which corresponds to this class.
238: *
239: * @return the formatID of this class
240: */
241: public int getTypeFormatId() {
242: return StoredFormatIds.AGG_INFO_V01_ID;
243: }
244: }
|