46d66bddf27a732fcc463884d33df68b2c6dad17
[hive.git] / ql / src / gen / vectorization / UDAFTemplates / VectorUDAFMinMax.txt
1 /**
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
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 package org.apache.hadoop.hive.ql.exec.vector.expressions.aggregates.gen;
20
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;
34
35 /**
36 * <ClassName>. Vectorized implementation for MIN/MAX aggregates. 
37 */
38 @Description(name = "<DescriptionName>", 
39     value = "<DescriptionValue>")
40 public class <ClassName> extends VectorAggregateExpression {
41    
42     private static final long serialVersionUID = 1L;
43     
44     /** 
45      * class for storing the current aggregate value.
46      */
47     static private final class Aggregation implements AggregationBuffer {
48
49       private static final long serialVersionUID = 1L;
50
51       transient private <ValueType> value;
52
53       /**
54       * Value is explicitly (re)initialized in reset()
55       */
56       transient private boolean isNull = true;
57
58       public void checkValue(<ValueType> value) {
59         if (isNull) {
60           isNull = false;
61           this.value = value;
62         } else if (value <OperatorSymbol> this.value) {
63           this.value = value;
64         }
65       }
66
67       @Override
68       public int getVariableSize() {
69         throw new UnsupportedOperationException();
70       }
71
72       @Override
73       public void reset () {
74         isNull = true;
75         value = 0;
76       }
77     }
78     
79     private VectorExpression inputExpression;
80     private transient VectorExpressionWriter resultWriter;
81     
82     public <ClassName>(VectorExpression inputExpression) {
83       this();
84       this.inputExpression = inputExpression;
85     }
86
87     public <ClassName>() {
88       super();
89     }
90     
91     @Override
92     public void init(AggregationDesc desc) throws HiveException {
93       resultWriter = VectorExpressionWriterFactory.genVectorExpressionWritable(
94           desc.getParameters().get(0));
95     }
96     
97     private Aggregation getCurrentAggregationBuffer(
98         VectorAggregationBufferRow[] aggregationBufferSets,
99         int aggregrateIndex,
100         int row) {
101       VectorAggregationBufferRow mySet = aggregationBufferSets[row];
102       Aggregation myagg = (Aggregation) mySet.getAggregationBuffer(aggregrateIndex);
103       return myagg;
104     }
105     
106     @Override
107     public void aggregateInputSelection(
108       VectorAggregationBufferRow[] aggregationBufferSets,
109       int aggregrateIndex, 
110       VectorizedRowBatch batch) throws HiveException {
111       
112       int batchSize = batch.size;
113       
114       if (batchSize == 0) {
115         return;
116       }
117       
118       inputExpression.evaluate(batch);
119       
120       <InputColumnVectorType> inputVector = (<InputColumnVectorType>)batch.
121         cols[this.inputExpression.getOutputColumn()];
122       <ValueType>[] vector = inputVector.vector;
123
124       if (inputVector.noNulls) {
125         if (inputVector.isRepeating) {
126           iterateNoNullsRepeatingWithAggregationSelection(
127             aggregationBufferSets, aggregrateIndex,
128             vector[0], batchSize);
129         } else {
130           if (batch.selectedInUse) {
131             iterateNoNullsSelectionWithAggregationSelection(
132               aggregationBufferSets, aggregrateIndex,
133               vector, batch.selected, batchSize);
134           } else {
135             iterateNoNullsWithAggregationSelection(
136               aggregationBufferSets, aggregrateIndex,
137               vector, batchSize);
138           }
139         }
140       } else {
141         if (inputVector.isRepeating) {
142           if (batch.selectedInUse) {
143             iterateHasNullsRepeatingSelectionWithAggregationSelection(
144               aggregationBufferSets, aggregrateIndex,
145               vector[0], batchSize, batch.selected, inputVector.isNull);
146           } else {
147             iterateHasNullsRepeatingWithAggregationSelection(
148               aggregationBufferSets, aggregrateIndex,
149               vector[0], batchSize, inputVector.isNull);
150           }
151         } else {
152           if (batch.selectedInUse) {
153             iterateHasNullsSelectionWithAggregationSelection(
154               aggregationBufferSets, aggregrateIndex,
155               vector, batchSize, batch.selected, inputVector.isNull);
156           } else {
157             iterateHasNullsWithAggregationSelection(
158               aggregationBufferSets, aggregrateIndex,
159               vector, batchSize, inputVector.isNull);
160           }
161         }
162       }
163     }
164
165     private void iterateNoNullsRepeatingWithAggregationSelection(
166       VectorAggregationBufferRow[] aggregationBufferSets,
167       int aggregrateIndex,
168       <ValueType> value,
169       int batchSize) {
170
171       for (int i=0; i < batchSize; ++i) {
172         Aggregation myagg = getCurrentAggregationBuffer(
173           aggregationBufferSets, 
174           aggregrateIndex,
175           i);
176         myagg.checkValue(value);
177       }
178     } 
179
180     private void iterateNoNullsSelectionWithAggregationSelection(
181       VectorAggregationBufferRow[] aggregationBufferSets,
182       int aggregrateIndex,
183       <ValueType>[] values,
184       int[] selection,
185       int batchSize) {
186       
187       for (int i=0; i < batchSize; ++i) {
188         Aggregation myagg = getCurrentAggregationBuffer(
189           aggregationBufferSets, 
190           aggregrateIndex,
191           i);
192         myagg.checkValue(values[selection[i]]);
193       }
194     }
195
196     private void iterateNoNullsWithAggregationSelection(
197       VectorAggregationBufferRow[] aggregationBufferSets,
198       int aggregrateIndex,
199       <ValueType>[] values,
200       int batchSize) {
201       for (int i=0; i < batchSize; ++i) {
202         Aggregation myagg = getCurrentAggregationBuffer(
203           aggregationBufferSets, 
204           aggregrateIndex,
205           i);
206         myagg.checkValue(values[i]);
207       }
208     }
209
210     private void iterateHasNullsRepeatingSelectionWithAggregationSelection(
211       VectorAggregationBufferRow[] aggregationBufferSets,
212       int aggregrateIndex,
213       <ValueType> value,
214       int batchSize,
215       int[] selection,
216       boolean[] isNull) {
217
218       if (isNull[0]) {
219         return;
220       }
221       
222       for (int i=0; i < batchSize; ++i) {
223         Aggregation myagg = getCurrentAggregationBuffer(
224           aggregationBufferSets,
225           aggregrateIndex,
226           i);
227         myagg.checkValue(value);
228       }
229       
230     }
231
232     private void iterateHasNullsRepeatingWithAggregationSelection(
233       VectorAggregationBufferRow[] aggregationBufferSets,
234       int aggregrateIndex,
235       <ValueType> value,
236       int batchSize,
237       boolean[] isNull) {
238
239       if (isNull[0]) {
240         return;
241       }
242
243       for (int i=0; i < batchSize; ++i) {
244         Aggregation myagg = getCurrentAggregationBuffer(
245           aggregationBufferSets,
246           aggregrateIndex,
247           i);
248         myagg.checkValue(value);
249       }
250     }
251
252     private void iterateHasNullsSelectionWithAggregationSelection(
253       VectorAggregationBufferRow[] aggregationBufferSets,
254       int aggregrateIndex,
255       <ValueType>[] values,
256       int batchSize,
257       int[] selection,
258       boolean[] isNull) {
259
260       for (int j=0; j < batchSize; ++j) {
261         int i = selection[j];
262         if (!isNull[i]) {
263           Aggregation myagg = getCurrentAggregationBuffer(
264             aggregationBufferSets, 
265             aggregrateIndex,
266             j);
267           myagg.checkValue(values[i]);
268         }
269       }
270    }
271
272     private void iterateHasNullsWithAggregationSelection(
273       VectorAggregationBufferRow[] aggregationBufferSets,
274       int aggregrateIndex,
275       <ValueType>[] values,
276       int batchSize,
277       boolean[] isNull) {
278
279       for (int i=0; i < batchSize; ++i) {
280         if (!isNull[i]) {
281           Aggregation myagg = getCurrentAggregationBuffer(
282             aggregationBufferSets, 
283             aggregrateIndex,
284             i);
285           myagg.checkValue(values[i]);
286         }
287       }
288    }
289     
290     @Override
291     public void aggregateInput(AggregationBuffer agg, VectorizedRowBatch batch) 
292       throws HiveException {
293         
294         inputExpression.evaluate(batch);
295         
296         <InputColumnVectorType> inputVector = (<InputColumnVectorType>)batch.
297             cols[this.inputExpression.getOutputColumn()];
298         
299         int batchSize = batch.size;
300         
301         if (batchSize == 0) {
302           return;
303         }
304         
305         Aggregation myagg = (Aggregation)agg;
306   
307         <ValueType>[] vector = inputVector.vector;
308         
309         if (inputVector.isRepeating) {
310           if (inputVector.noNulls &&
311             (myagg.isNull || (vector[0] <OperatorSymbol> myagg.value))) {
312             myagg.isNull = false;
313             myagg.value = vector[0];
314           }
315           return;
316         }
317         
318         if (!batch.selectedInUse && inputVector.noNulls) {
319           iterateNoSelectionNoNulls(myagg, vector, batchSize);
320         }
321         else if (!batch.selectedInUse) {
322           iterateNoSelectionHasNulls(myagg, vector, batchSize, inputVector.isNull);
323         }
324         else if (inputVector.noNulls){
325           iterateSelectionNoNulls(myagg, vector, batchSize, batch.selected);
326         }
327         else {
328           iterateSelectionHasNulls(myagg, vector, batchSize, inputVector.isNull, batch.selected);
329         }
330     }
331   
332     private void iterateSelectionHasNulls(
333         Aggregation myagg, 
334         <ValueType>[] vector, 
335         int batchSize,
336         boolean[] isNull, 
337         int[] selected) {
338       
339       for (int j=0; j< batchSize; ++j) {
340         int i = selected[j];
341         if (!isNull[i]) {
342           <ValueType> value = vector[i];
343           if (myagg.isNull) {
344             myagg.isNull = false;
345             myagg.value = value;
346           }
347           else if (value <OperatorSymbol> myagg.value) {
348             myagg.value = value;
349           }
350         }
351       }
352     }
353
354     private void iterateSelectionNoNulls(
355         Aggregation myagg, 
356         <ValueType>[] vector, 
357         int batchSize, 
358         int[] selected) {
359       
360       if (myagg.isNull) {
361         myagg.value = vector[selected[0]];
362         myagg.isNull = false;
363       }
364       
365       for (int i=0; i< batchSize; ++i) {
366         <ValueType> value = vector[selected[i]];
367         if (value <OperatorSymbol> myagg.value) {
368           myagg.value = value;
369         }
370       }
371     }
372
373     private void iterateNoSelectionHasNulls(
374         Aggregation myagg, 
375         <ValueType>[] vector, 
376         int batchSize,
377         boolean[] isNull) {
378       
379       for(int i=0;i<batchSize;++i) {
380         if (!isNull[i]) {
381           <ValueType> value = vector[i];
382           if (myagg.isNull) { 
383             myagg.value = value;
384             myagg.isNull = false;
385           }
386           else if (value <OperatorSymbol> myagg.value) {
387             myagg.value = value;
388           }
389         }
390       }
391     }
392
393     private void iterateNoSelectionNoNulls(
394         Aggregation myagg, 
395         <ValueType>[] vector, 
396         int batchSize) {
397       if (myagg.isNull) {
398         myagg.value = vector[0];
399         myagg.isNull = false;
400       }
401       
402       for (int i=0;i<batchSize;++i) {
403         <ValueType> value = vector[i];
404         if (value <OperatorSymbol> myagg.value) {
405           myagg.value = value;
406         }
407       }
408     }
409
410     @Override
411     public AggregationBuffer getNewAggregationBuffer() throws HiveException {
412       return new Aggregation();
413     }
414
415     @Override
416     public void reset(AggregationBuffer agg) throws HiveException {
417       Aggregation myAgg = (Aggregation) agg;
418       myAgg.reset();
419     }
420
421     @Override
422     public Object evaluateOutput(
423         AggregationBuffer agg) throws HiveException {
424     Aggregation myagg = (Aggregation) agg;
425       if (myagg.isNull) {
426         return null;
427       }
428       else {
429         return resultWriter.writeValue(myagg.value);
430       }
431     }
432     
433     @Override
434     public ObjectInspector getOutputObjectInspector() {
435       return resultWriter.getObjectInspector();
436     }
437
438     @Override
439     public int getAggregationBufferFixedSize() {
440     JavaDataModel model = JavaDataModel.get();
441     return JavaDataModel.alignUp(
442       model.object() +
443       model.primitive2(),
444       model.memoryAlign());
445   }
446
447   public VectorExpression getInputExpression() {
448     return inputExpression;
449   }
450
451   public void setInputExpression(VectorExpression inputExpression) {
452     this.inputExpression = inputExpression;
453   }
454 }
455