2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.apache.hadoop.hive.ql.exec.vector.expressions.aggregates.gen;
21 import org.apache.hadoop.hive.ql.exec.Description;
22 import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression;
23 import org.apache.hadoop.hive.ql.exec.vector.expressions.aggregates.VectorAggregateExpression;
24 import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpressionWriter;
25 import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpressionWriterFactory;
26 import org.apache.hadoop.hive.ql.exec.vector.VectorAggregationBufferRow;
27 import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
28 import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
29 import org.apache.hadoop.hive.ql.exec.vector.DoubleColumnVector;
30 import org.apache.hadoop.hive.ql.metadata.HiveException;
31 import org.apache.hadoop.hive.ql.plan.AggregationDesc;
32 import org.apache.hadoop.hive.ql.util.JavaDataModel;
33 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
36 * <ClassName>. Vectorized implementation for MIN/MAX aggregates.
38 @Description(name = "<DescriptionName>",
39 value = "<DescriptionValue>")
40 public class <ClassName> extends VectorAggregateExpression {
42 private static final long serialVersionUID = 1L;
45 * class for storing the current aggregate value.
47 static private final class Aggregation implements AggregationBuffer {
49 private static final long serialVersionUID = 1L;
51 transient private <ValueType> value;
54 * Value is explicitly (re)initialized in reset()
56 transient private boolean isNull = true;
58 public void checkValue(<ValueType> value) {
62 } else if (value <OperatorSymbol> this.value) {
68 public int getVariableSize() {
69 throw new UnsupportedOperationException();
73 public void reset () {
79 private VectorExpression inputExpression;
82 public VectorExpression inputExpression() {
83 return inputExpression;
86 private transient VectorExpressionWriter resultWriter;
88 public <ClassName>(VectorExpression inputExpression) {
90 this.inputExpression = inputExpression;
93 public <ClassName>() {
98 public void init(AggregationDesc desc) throws HiveException {
99 resultWriter = VectorExpressionWriterFactory.genVectorExpressionWritable(
100 desc.getParameters().get(0));
103 private Aggregation getCurrentAggregationBuffer(
104 VectorAggregationBufferRow[] aggregationBufferSets,
107 VectorAggregationBufferRow mySet = aggregationBufferSets[row];
108 Aggregation myagg = (Aggregation) mySet.getAggregationBuffer(aggregrateIndex);
113 public void aggregateInputSelection(
114 VectorAggregationBufferRow[] aggregationBufferSets,
116 VectorizedRowBatch batch) throws HiveException {
118 int batchSize = batch.size;
120 if (batchSize == 0) {
124 inputExpression.evaluate(batch);
126 <InputColumnVectorType> inputVector = (<InputColumnVectorType>)batch.
127 cols[this.inputExpression.getOutputColumn()];
128 <ValueType>[] vector = inputVector.vector;
130 if (inputVector.noNulls) {
131 if (inputVector.isRepeating) {
132 iterateNoNullsRepeatingWithAggregationSelection(
133 aggregationBufferSets, aggregrateIndex,
134 vector[0], batchSize);
136 if (batch.selectedInUse) {
137 iterateNoNullsSelectionWithAggregationSelection(
138 aggregationBufferSets, aggregrateIndex,
139 vector, batch.selected, batchSize);
141 iterateNoNullsWithAggregationSelection(
142 aggregationBufferSets, aggregrateIndex,
147 if (inputVector.isRepeating) {
148 if (batch.selectedInUse) {
149 iterateHasNullsRepeatingSelectionWithAggregationSelection(
150 aggregationBufferSets, aggregrateIndex,
151 vector[0], batchSize, batch.selected, inputVector.isNull);
153 iterateHasNullsRepeatingWithAggregationSelection(
154 aggregationBufferSets, aggregrateIndex,
155 vector[0], batchSize, inputVector.isNull);
158 if (batch.selectedInUse) {
159 iterateHasNullsSelectionWithAggregationSelection(
160 aggregationBufferSets, aggregrateIndex,
161 vector, batchSize, batch.selected, inputVector.isNull);
163 iterateHasNullsWithAggregationSelection(
164 aggregationBufferSets, aggregrateIndex,
165 vector, batchSize, inputVector.isNull);
171 private void iterateNoNullsRepeatingWithAggregationSelection(
172 VectorAggregationBufferRow[] aggregationBufferSets,
177 for (int i=0; i < batchSize; ++i) {
178 Aggregation myagg = getCurrentAggregationBuffer(
179 aggregationBufferSets,
182 myagg.checkValue(value);
186 private void iterateNoNullsSelectionWithAggregationSelection(
187 VectorAggregationBufferRow[] aggregationBufferSets,
189 <ValueType>[] values,
193 for (int i=0; i < batchSize; ++i) {
194 Aggregation myagg = getCurrentAggregationBuffer(
195 aggregationBufferSets,
198 myagg.checkValue(values[selection[i]]);
202 private void iterateNoNullsWithAggregationSelection(
203 VectorAggregationBufferRow[] aggregationBufferSets,
205 <ValueType>[] values,
207 for (int i=0; i < batchSize; ++i) {
208 Aggregation myagg = getCurrentAggregationBuffer(
209 aggregationBufferSets,
212 myagg.checkValue(values[i]);
216 private void iterateHasNullsRepeatingSelectionWithAggregationSelection(
217 VectorAggregationBufferRow[] aggregationBufferSets,
228 for (int i=0; i < batchSize; ++i) {
229 Aggregation myagg = getCurrentAggregationBuffer(
230 aggregationBufferSets,
233 myagg.checkValue(value);
238 private void iterateHasNullsRepeatingWithAggregationSelection(
239 VectorAggregationBufferRow[] aggregationBufferSets,
249 for (int i=0; i < batchSize; ++i) {
250 Aggregation myagg = getCurrentAggregationBuffer(
251 aggregationBufferSets,
254 myagg.checkValue(value);
258 private void iterateHasNullsSelectionWithAggregationSelection(
259 VectorAggregationBufferRow[] aggregationBufferSets,
261 <ValueType>[] values,
266 for (int j=0; j < batchSize; ++j) {
267 int i = selection[j];
269 Aggregation myagg = getCurrentAggregationBuffer(
270 aggregationBufferSets,
273 myagg.checkValue(values[i]);
278 private void iterateHasNullsWithAggregationSelection(
279 VectorAggregationBufferRow[] aggregationBufferSets,
281 <ValueType>[] values,
285 for (int i=0; i < batchSize; ++i) {
287 Aggregation myagg = getCurrentAggregationBuffer(
288 aggregationBufferSets,
291 myagg.checkValue(values[i]);
297 public void aggregateInput(AggregationBuffer agg, VectorizedRowBatch batch)
298 throws HiveException {
300 inputExpression.evaluate(batch);
302 <InputColumnVectorType> inputVector = (<InputColumnVectorType>)batch.
303 cols[this.inputExpression.getOutputColumn()];
305 int batchSize = batch.size;
307 if (batchSize == 0) {
311 Aggregation myagg = (Aggregation)agg;
313 <ValueType>[] vector = inputVector.vector;
315 if (inputVector.isRepeating) {
316 if (inputVector.noNulls &&
317 (myagg.isNull || (vector[0] <OperatorSymbol> myagg.value))) {
318 myagg.isNull = false;
319 myagg.value = vector[0];
324 if (!batch.selectedInUse && inputVector.noNulls) {
325 iterateNoSelectionNoNulls(myagg, vector, batchSize);
327 else if (!batch.selectedInUse) {
328 iterateNoSelectionHasNulls(myagg, vector, batchSize, inputVector.isNull);
330 else if (inputVector.noNulls){
331 iterateSelectionNoNulls(myagg, vector, batchSize, batch.selected);
334 iterateSelectionHasNulls(myagg, vector, batchSize, inputVector.isNull, batch.selected);
338 private void iterateSelectionHasNulls(
340 <ValueType>[] vector,
345 for (int j=0; j< batchSize; ++j) {
348 <ValueType> value = vector[i];
350 myagg.isNull = false;
353 else if (value <OperatorSymbol> myagg.value) {
360 private void iterateSelectionNoNulls(
362 <ValueType>[] vector,
367 myagg.value = vector[selected[0]];
368 myagg.isNull = false;
371 for (int i=0; i< batchSize; ++i) {
372 <ValueType> value = vector[selected[i]];
373 if (value <OperatorSymbol> myagg.value) {
379 private void iterateNoSelectionHasNulls(
381 <ValueType>[] vector,
385 for(int i=0;i<batchSize;++i) {
387 <ValueType> value = vector[i];
390 myagg.isNull = false;
392 else if (value <OperatorSymbol> myagg.value) {
399 private void iterateNoSelectionNoNulls(
401 <ValueType>[] vector,
404 myagg.value = vector[0];
405 myagg.isNull = false;
408 for (int i=0;i<batchSize;++i) {
409 <ValueType> value = vector[i];
410 if (value <OperatorSymbol> myagg.value) {
417 public AggregationBuffer getNewAggregationBuffer() throws HiveException {
418 return new Aggregation();
422 public void reset(AggregationBuffer agg) throws HiveException {
423 Aggregation myAgg = (Aggregation) agg;
428 public Object evaluateOutput(
429 AggregationBuffer agg) throws HiveException {
430 Aggregation myagg = (Aggregation) agg;
435 return resultWriter.writeValue(myagg.value);
440 public ObjectInspector getOutputObjectInspector() {
441 return resultWriter.getObjectInspector();
445 public int getAggregationBufferFixedSize() {
446 JavaDataModel model = JavaDataModel.get();
447 return JavaDataModel.alignUp(
450 model.memoryAlign());
453 public VectorExpression getInputExpression() {
454 return inputExpression;
457 public void setInputExpression(VectorExpression inputExpression) {
458 this.inputExpression = inputExpression;