7468c2fddb19ee6804c533caf3001c2eb2662311
[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
81     @Override
82     public VectorExpression inputExpression() {
83       return inputExpression;
84     }
85
86     private transient VectorExpressionWriter resultWriter;
87     
88     public <ClassName>(VectorExpression inputExpression) {
89       this();
90       this.inputExpression = inputExpression;
91     }
92
93     public <ClassName>() {
94       super();
95     }
96     
97     @Override
98     public void init(AggregationDesc desc) throws HiveException {
99       resultWriter = VectorExpressionWriterFactory.genVectorExpressionWritable(
100           desc.getParameters().get(0));
101     }
102     
103     private Aggregation getCurrentAggregationBuffer(
104         VectorAggregationBufferRow[] aggregationBufferSets,
105         int aggregrateIndex,
106         int row) {
107       VectorAggregationBufferRow mySet = aggregationBufferSets[row];
108       Aggregation myagg = (Aggregation) mySet.getAggregationBuffer(aggregrateIndex);
109       return myagg;
110     }
111     
112     @Override
113     public void aggregateInputSelection(
114       VectorAggregationBufferRow[] aggregationBufferSets,
115       int aggregrateIndex, 
116       VectorizedRowBatch batch) throws HiveException {
117       
118       int batchSize = batch.size;
119       
120       if (batchSize == 0) {
121         return;
122       }
123       
124       inputExpression.evaluate(batch);
125       
126       <InputColumnVectorType> inputVector = (<InputColumnVectorType>)batch.
127         cols[this.inputExpression.getOutputColumn()];
128       <ValueType>[] vector = inputVector.vector;
129
130       if (inputVector.noNulls) {
131         if (inputVector.isRepeating) {
132           iterateNoNullsRepeatingWithAggregationSelection(
133             aggregationBufferSets, aggregrateIndex,
134             vector[0], batchSize);
135         } else {
136           if (batch.selectedInUse) {
137             iterateNoNullsSelectionWithAggregationSelection(
138               aggregationBufferSets, aggregrateIndex,
139               vector, batch.selected, batchSize);
140           } else {
141             iterateNoNullsWithAggregationSelection(
142               aggregationBufferSets, aggregrateIndex,
143               vector, batchSize);
144           }
145         }
146       } else {
147         if (inputVector.isRepeating) {
148           if (batch.selectedInUse) {
149             iterateHasNullsRepeatingSelectionWithAggregationSelection(
150               aggregationBufferSets, aggregrateIndex,
151               vector[0], batchSize, batch.selected, inputVector.isNull);
152           } else {
153             iterateHasNullsRepeatingWithAggregationSelection(
154               aggregationBufferSets, aggregrateIndex,
155               vector[0], batchSize, inputVector.isNull);
156           }
157         } else {
158           if (batch.selectedInUse) {
159             iterateHasNullsSelectionWithAggregationSelection(
160               aggregationBufferSets, aggregrateIndex,
161               vector, batchSize, batch.selected, inputVector.isNull);
162           } else {
163             iterateHasNullsWithAggregationSelection(
164               aggregationBufferSets, aggregrateIndex,
165               vector, batchSize, inputVector.isNull);
166           }
167         }
168       }
169     }
170
171     private void iterateNoNullsRepeatingWithAggregationSelection(
172       VectorAggregationBufferRow[] aggregationBufferSets,
173       int aggregrateIndex,
174       <ValueType> value,
175       int batchSize) {
176
177       for (int i=0; i < batchSize; ++i) {
178         Aggregation myagg = getCurrentAggregationBuffer(
179           aggregationBufferSets, 
180           aggregrateIndex,
181           i);
182         myagg.checkValue(value);
183       }
184     } 
185
186     private void iterateNoNullsSelectionWithAggregationSelection(
187       VectorAggregationBufferRow[] aggregationBufferSets,
188       int aggregrateIndex,
189       <ValueType>[] values,
190       int[] selection,
191       int batchSize) {
192       
193       for (int i=0; i < batchSize; ++i) {
194         Aggregation myagg = getCurrentAggregationBuffer(
195           aggregationBufferSets, 
196           aggregrateIndex,
197           i);
198         myagg.checkValue(values[selection[i]]);
199       }
200     }
201
202     private void iterateNoNullsWithAggregationSelection(
203       VectorAggregationBufferRow[] aggregationBufferSets,
204       int aggregrateIndex,
205       <ValueType>[] values,
206       int batchSize) {
207       for (int i=0; i < batchSize; ++i) {
208         Aggregation myagg = getCurrentAggregationBuffer(
209           aggregationBufferSets, 
210           aggregrateIndex,
211           i);
212         myagg.checkValue(values[i]);
213       }
214     }
215
216     private void iterateHasNullsRepeatingSelectionWithAggregationSelection(
217       VectorAggregationBufferRow[] aggregationBufferSets,
218       int aggregrateIndex,
219       <ValueType> value,
220       int batchSize,
221       int[] selection,
222       boolean[] isNull) {
223
224       if (isNull[0]) {
225         return;
226       }
227       
228       for (int i=0; i < batchSize; ++i) {
229         Aggregation myagg = getCurrentAggregationBuffer(
230           aggregationBufferSets,
231           aggregrateIndex,
232           i);
233         myagg.checkValue(value);
234       }
235       
236     }
237
238     private void iterateHasNullsRepeatingWithAggregationSelection(
239       VectorAggregationBufferRow[] aggregationBufferSets,
240       int aggregrateIndex,
241       <ValueType> value,
242       int batchSize,
243       boolean[] isNull) {
244
245       if (isNull[0]) {
246         return;
247       }
248
249       for (int i=0; i < batchSize; ++i) {
250         Aggregation myagg = getCurrentAggregationBuffer(
251           aggregationBufferSets,
252           aggregrateIndex,
253           i);
254         myagg.checkValue(value);
255       }
256     }
257
258     private void iterateHasNullsSelectionWithAggregationSelection(
259       VectorAggregationBufferRow[] aggregationBufferSets,
260       int aggregrateIndex,
261       <ValueType>[] values,
262       int batchSize,
263       int[] selection,
264       boolean[] isNull) {
265
266       for (int j=0; j < batchSize; ++j) {
267         int i = selection[j];
268         if (!isNull[i]) {
269           Aggregation myagg = getCurrentAggregationBuffer(
270             aggregationBufferSets, 
271             aggregrateIndex,
272             j);
273           myagg.checkValue(values[i]);
274         }
275       }
276    }
277
278     private void iterateHasNullsWithAggregationSelection(
279       VectorAggregationBufferRow[] aggregationBufferSets,
280       int aggregrateIndex,
281       <ValueType>[] values,
282       int batchSize,
283       boolean[] isNull) {
284
285       for (int i=0; i < batchSize; ++i) {
286         if (!isNull[i]) {
287           Aggregation myagg = getCurrentAggregationBuffer(
288             aggregationBufferSets, 
289             aggregrateIndex,
290             i);
291           myagg.checkValue(values[i]);
292         }
293       }
294    }
295     
296     @Override
297     public void aggregateInput(AggregationBuffer agg, VectorizedRowBatch batch) 
298       throws HiveException {
299         
300         inputExpression.evaluate(batch);
301         
302         <InputColumnVectorType> inputVector = (<InputColumnVectorType>)batch.
303             cols[this.inputExpression.getOutputColumn()];
304         
305         int batchSize = batch.size;
306         
307         if (batchSize == 0) {
308           return;
309         }
310         
311         Aggregation myagg = (Aggregation)agg;
312   
313         <ValueType>[] vector = inputVector.vector;
314         
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];
320           }
321           return;
322         }
323         
324         if (!batch.selectedInUse && inputVector.noNulls) {
325           iterateNoSelectionNoNulls(myagg, vector, batchSize);
326         }
327         else if (!batch.selectedInUse) {
328           iterateNoSelectionHasNulls(myagg, vector, batchSize, inputVector.isNull);
329         }
330         else if (inputVector.noNulls){
331           iterateSelectionNoNulls(myagg, vector, batchSize, batch.selected);
332         }
333         else {
334           iterateSelectionHasNulls(myagg, vector, batchSize, inputVector.isNull, batch.selected);
335         }
336     }
337   
338     private void iterateSelectionHasNulls(
339         Aggregation myagg, 
340         <ValueType>[] vector, 
341         int batchSize,
342         boolean[] isNull, 
343         int[] selected) {
344       
345       for (int j=0; j< batchSize; ++j) {
346         int i = selected[j];
347         if (!isNull[i]) {
348           <ValueType> value = vector[i];
349           if (myagg.isNull) {
350             myagg.isNull = false;
351             myagg.value = value;
352           }
353           else if (value <OperatorSymbol> myagg.value) {
354             myagg.value = value;
355           }
356         }
357       }
358     }
359
360     private void iterateSelectionNoNulls(
361         Aggregation myagg, 
362         <ValueType>[] vector, 
363         int batchSize, 
364         int[] selected) {
365       
366       if (myagg.isNull) {
367         myagg.value = vector[selected[0]];
368         myagg.isNull = false;
369       }
370       
371       for (int i=0; i< batchSize; ++i) {
372         <ValueType> value = vector[selected[i]];
373         if (value <OperatorSymbol> myagg.value) {
374           myagg.value = value;
375         }
376       }
377     }
378
379     private void iterateNoSelectionHasNulls(
380         Aggregation myagg, 
381         <ValueType>[] vector, 
382         int batchSize,
383         boolean[] isNull) {
384       
385       for(int i=0;i<batchSize;++i) {
386         if (!isNull[i]) {
387           <ValueType> value = vector[i];
388           if (myagg.isNull) { 
389             myagg.value = value;
390             myagg.isNull = false;
391           }
392           else if (value <OperatorSymbol> myagg.value) {
393             myagg.value = value;
394           }
395         }
396       }
397     }
398
399     private void iterateNoSelectionNoNulls(
400         Aggregation myagg, 
401         <ValueType>[] vector, 
402         int batchSize) {
403       if (myagg.isNull) {
404         myagg.value = vector[0];
405         myagg.isNull = false;
406       }
407       
408       for (int i=0;i<batchSize;++i) {
409         <ValueType> value = vector[i];
410         if (value <OperatorSymbol> myagg.value) {
411           myagg.value = value;
412         }
413       }
414     }
415
416     @Override
417     public AggregationBuffer getNewAggregationBuffer() throws HiveException {
418       return new Aggregation();
419     }
420
421     @Override
422     public void reset(AggregationBuffer agg) throws HiveException {
423       Aggregation myAgg = (Aggregation) agg;
424       myAgg.reset();
425     }
426
427     @Override
428     public Object evaluateOutput(
429         AggregationBuffer agg) throws HiveException {
430     Aggregation myagg = (Aggregation) agg;
431       if (myagg.isNull) {
432         return null;
433       }
434       else {
435         return resultWriter.writeValue(myagg.value);
436       }
437     }
438     
439     @Override
440     public ObjectInspector getOutputObjectInspector() {
441       return resultWriter.getObjectInspector();
442     }
443
444     @Override
445     public int getAggregationBufferFixedSize() {
446     JavaDataModel model = JavaDataModel.get();
447     return JavaDataModel.alignUp(
448       model.object() +
449       model.primitive2(),
450       model.memoryAlign());
451   }
452
453   public VectorExpression getInputExpression() {
454     return inputExpression;
455   }
456
457   public void setInputExpression(VectorExpression inputExpression) {
458     this.inputExpression = inputExpression;
459   }
460 }
461