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