a72b8820f90e4b81c9d672523d0e8ec28169eaff
[hive.git] / ql / src / gen / vectorization / ExpressionTemplates / FilterStringGroupColumnCompareStringGroupColumn.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.gen;
20
21 import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression;
22 import org.apache.hadoop.hive.ql.exec.vector.expressions.StringExpr;
23 import org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;
24 import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
25 import org.apache.hadoop.hive.ql.exec.vector.VectorExpressionDescriptor;
26
27 /**
28  * Filter the rows in a batch by comparing one string column to another. 
29  * This code is generated from a template.
30  */
31 public class <ClassName> extends VectorExpression {
32
33   private static final long serialVersionUID = 1L;
34
35   private int colNum1;
36   private int colNum2;
37
38   public <ClassName>(int colNum1, int colNum2) { 
39     this.colNum1 = colNum1;
40     this.colNum2 = colNum2;
41   }
42
43   public <ClassName>() {
44   }
45
46   @Override
47   public void evaluate(VectorizedRowBatch batch) {
48
49     if (childExpressions != null) {
50       super.evaluateChildren(batch);
51     }
52
53     BytesColumnVector inputColVector1 = (BytesColumnVector) batch.cols[colNum1];
54     BytesColumnVector inputColVector2 = (BytesColumnVector) batch.cols[colNum2];
55     int[] sel = batch.selected;
56     boolean[] nullPos1 = inputColVector1.isNull;
57     boolean[] nullPos2 = inputColVector2.isNull;
58     int n = batch.size;
59     byte[][] vector1 = inputColVector1.vector;
60     byte[][] vector2 = inputColVector2.vector;
61     int[] start1 = inputColVector1.start;
62     int[] start2 = inputColVector2.start;
63     int[] length1 = inputColVector1.length;
64     int[] length2 = inputColVector2.length;
65     
66     // return immediately if batch is empty
67     if (n == 0) {
68       return;
69     }
70     
71     // handle case where neither input has nulls
72     if (inputColVector1.noNulls && inputColVector2.noNulls) {
73       if (inputColVector1.isRepeating && inputColVector2.isRepeating) {
74       
75         /* Either all must remain selected or all will be eliminated.
76          * Repeating property will not change.
77          */
78         if (!(<CompareOrEqual>(vector1[0], start1[0], length1[0], 
79                                  vector2[0], start2[0], length2[0])<OptionalCompare>)) {
80           batch.size = 0;
81         }      
82       } else if (inputColVector1.isRepeating) {
83         if (batch.selectedInUse) {
84           int newSize = 0;
85           for(int j = 0; j != n; j++) {
86             int i = sel[j];
87             if (<CompareOrEqual>(vector1[0], start1[0], length1[0], 
88                                    vector2[i], start2[i], length2[i])<OptionalCompare>) {
89               sel[newSize++] = i;
90             }
91           }
92           batch.size = newSize;
93         } else {
94           int newSize = 0;
95           for(int i = 0; i != n; i++) {
96             if (<CompareOrEqual>(vector1[0], start1[0], length1[0], 
97                                    vector2[i], start2[i], length2[i])<OptionalCompare>) {
98               sel[newSize++] = i;
99             }
100           }
101           if (newSize < batch.size) {
102             batch.size = newSize;
103             batch.selectedInUse = true;
104           }
105         }
106       } else if (inputColVector2.isRepeating) {
107         if (batch.selectedInUse) {
108           int newSize = 0;
109           for(int j = 0; j != n; j++) {
110             int i = sel[j];
111             if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
112                                    vector2[0], start2[0], length2[0])<OptionalCompare>) {
113               sel[newSize++] = i;
114             }
115           }
116           batch.size = newSize;
117         } else {
118           int newSize = 0;
119           for(int i = 0; i != n; i++) {
120             if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
121                                    vector2[0], start2[0], length2[0])<OptionalCompare>) {
122               sel[newSize++] = i;
123             }
124           }
125           if (newSize < batch.size) {
126             batch.size = newSize;
127             batch.selectedInUse = true;
128           }
129         }
130       } else if (batch.selectedInUse) {
131         int newSize = 0;
132         for(int j = 0; j != n; j++) {
133           int i = sel[j];
134           if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
135                                  vector2[i], start2[i], length2[i])<OptionalCompare>) {
136             sel[newSize++] = i;
137           }
138         }
139         batch.size = newSize;
140       } else {
141         int newSize = 0;
142         for(int i = 0; i != n; i++) {
143           if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
144                                  vector2[i], start2[i], length2[i])<OptionalCompare>) {
145             sel[newSize++] = i;
146           }
147         }
148         if (newSize < batch.size) {
149           batch.size = newSize;
150           batch.selectedInUse = true;
151         }
152       }
153     
154     // handle case where only input 2 has nulls
155     } else if (inputColVector1.noNulls) { 
156       if (inputColVector1.isRepeating && inputColVector2.isRepeating) {
157         if (nullPos2[0] ||
158             !(<CompareOrEqual>(vector1[0], start1[0], length1[0], 
159                                vector2[0], start2[0], length2[0])<OptionalCompare>)) {
160           batch.size = 0; 
161         } 
162       } else if (inputColVector1.isRepeating) {
163          
164          // no need to check for nulls in input 1
165          if (batch.selectedInUse) {
166           int newSize = 0;
167           for(int j = 0; j != n; j++) {
168             int i = sel[j];
169             if (!nullPos2[i]) {
170               if (<CompareOrEqual>(vector1[0], start1[0], length1[0], 
171                                      vector2[i], start2[i], length2[i])<OptionalCompare>) {
172                 sel[newSize++] = i;
173               }
174             }
175           }
176           batch.size = newSize;
177         } else {
178           int newSize = 0;
179           for(int i = 0; i != n; i++) {
180             if (!nullPos2[i]) {
181               if (<CompareOrEqual>(vector1[0], start1[0], length1[0], 
182                                      vector2[i], start2[i], length2[i])<OptionalCompare>) {
183                 sel[newSize++] = i;
184               }
185             }
186           }
187           if (newSize < batch.size) {
188             batch.size = newSize;
189             batch.selectedInUse = true;
190           }
191         }
192       } else if (inputColVector2.isRepeating) {
193         if (nullPos2[0]) {
194         
195           // no values will qualify because every comparison will be with NULL
196           batch.size = 0;
197           return;
198         }
199         if (batch.selectedInUse) {
200           int newSize = 0;
201           for(int j = 0; j != n; j++) {
202             int i = sel[j];
203             if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
204                                    vector2[0], start2[0], length2[0])<OptionalCompare>) {
205               sel[newSize++] = i;
206             }
207           }
208           batch.size = newSize;
209         } else {
210           int newSize = 0;
211           for(int i = 0; i != n; i++) {
212             if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
213                                    vector2[0], start2[0], length2[0])<OptionalCompare>) {
214               sel[newSize++] = i;
215             }
216           }
217           if (newSize < batch.size) {
218             batch.size = newSize;
219             batch.selectedInUse = true;
220           }
221         }
222       } else { // neither input is repeating
223         if (batch.selectedInUse) {
224           int newSize = 0;
225           for(int j = 0; j != n; j++) {
226             int i = sel[j];
227             if (!nullPos2[i]) {
228               if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
229                                      vector2[i], start2[i], length2[i])<OptionalCompare>) {
230                 sel[newSize++] = i;
231               }
232             }
233           }
234           batch.size = newSize;
235         } else {
236           int newSize = 0;
237           for(int i = 0; i != n; i++) {
238             if (!nullPos2[i]) {
239               if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
240                                      vector2[i], start2[i], length2[i])<OptionalCompare>) {
241                 sel[newSize++] = i;
242               }
243             }
244           }
245           if (newSize < batch.size) {
246             batch.size = newSize;
247             batch.selectedInUse = true;
248           }
249         }      
250       }
251       
252     // handle case where only input 1 has nulls
253     } else if (inputColVector2.noNulls) {
254       if (inputColVector1.isRepeating && inputColVector2.isRepeating) {
255         if (nullPos1[0] ||
256             !(<CompareOrEqual>(vector1[0], start1[0], length1[0], 
257                                vector2[0], start2[0], length2[0])<OptionalCompare>)) {
258           batch.size = 0; 
259           return;
260         } 
261       } else if (inputColVector1.isRepeating) {
262         if (nullPos1[0]) {
263         
264           // if repeating value is null then every comparison will fail so nothing qualifies
265           batch.size = 0;
266           return; 
267         }
268         if (batch.selectedInUse) {
269           int newSize = 0;
270           for(int j = 0; j != n; j++) {
271             int i = sel[j];
272             if (<CompareOrEqual>(vector1[0], start1[0], length1[0], 
273                                    vector2[i], start2[i], length2[i])<OptionalCompare>) {
274               sel[newSize++] = i;
275             }
276           }
277           batch.size = newSize;
278         } else {
279           int newSize = 0;
280           for(int i = 0; i != n; i++) {
281             if (<CompareOrEqual>(vector1[0], start1[0], length1[0], 
282                                    vector2[i], start2[i], length2[i])<OptionalCompare>) {
283               sel[newSize++] = i;
284             }
285           }
286           if (newSize < batch.size) {
287             batch.size = newSize;
288             batch.selectedInUse = true;
289           }
290         }
291       } else if (inputColVector2.isRepeating) {
292          if (batch.selectedInUse) {
293           int newSize = 0;
294           for(int j = 0; j != n; j++) {
295             int i = sel[j];
296             if (!nullPos1[i]) {
297               if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
298                                      vector2[0], start2[0], length2[0])<OptionalCompare>) {
299                 sel[newSize++] = i;
300               }
301             }
302           }
303           batch.size = newSize;
304         } else {
305           int newSize = 0;
306           for(int i = 0; i != n; i++) {
307             if (!nullPos1[i]) {
308               if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
309                                      vector2[0], start2[0], length2[0])<OptionalCompare>) {
310                 sel[newSize++] = i;
311               }
312             }
313           }
314           if (newSize < batch.size) {
315             batch.size = newSize;
316             batch.selectedInUse = true;
317           }
318         }
319       } else { // neither input is repeating
320          if (batch.selectedInUse) {
321           int newSize = 0;
322           for(int j = 0; j != n; j++) {
323             int i = sel[j];
324             if (!nullPos1[i]) {
325               if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
326                                      vector2[i], start2[i], length2[i])<OptionalCompare>) {
327                 sel[newSize++] = i;
328               }
329             }
330           }
331           batch.size = newSize;
332         } else {
333           int newSize = 0;
334           for(int i = 0; i != n; i++) {
335             if (!nullPos1[i]) {
336               if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
337                                      vector2[i], start2[i], length2[i])<OptionalCompare>) {
338                 sel[newSize++] = i;
339               }
340             }
341           }
342           if (newSize < batch.size) {
343             batch.size = newSize;
344             batch.selectedInUse = true;
345           }
346         }      
347       }
348             
349     // handle case where both inputs have nulls
350     } else {
351       if (inputColVector1.isRepeating && inputColVector2.isRepeating) {
352         if (nullPos1[0] || nullPos2[0] ||
353             !(<CompareOrEqual>(vector1[0], start1[0], length1[0], 
354                                vector2[0], start2[0], length2[0])<OptionalCompare>)) {
355           batch.size = 0; 
356         } 
357       } else if (inputColVector1.isRepeating) {
358          if (nullPos1[0]) {
359            batch.size = 0;
360            return;
361          }
362          if (batch.selectedInUse) {
363           int newSize = 0;
364           for(int j = 0; j != n; j++) {
365             int i = sel[j];
366             if (!nullPos2[i]) {
367               if (<CompareOrEqual>(vector1[0], start1[0], length1[0], 
368                                      vector2[i], start2[i], length2[i])<OptionalCompare>) {
369                 sel[newSize++] = i;
370               }
371             }
372           }
373           batch.size = newSize;
374         } else {
375           int newSize = 0;
376           for(int i = 0; i != n; i++) {
377             if (!nullPos2[i]) {
378               if (<CompareOrEqual>(vector1[0], start1[0], length1[0], 
379                                      vector2[i], start2[i], length2[i])<OptionalCompare>) {
380                 sel[newSize++] = i;
381               }
382             }
383           }
384           if (newSize < batch.size) {
385             batch.size = newSize;
386             batch.selectedInUse = true;
387           }
388         }
389       } else if (inputColVector2.isRepeating) {
390         if (nullPos2[0]) {
391           batch.size = 0;
392           return;
393         }
394         if (batch.selectedInUse) {
395           int newSize = 0;
396           for(int j = 0; j != n; j++) {
397             int i = sel[j];
398             if (!nullPos1[i]) {
399               if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
400                                      vector2[0], start2[0], length2[0])<OptionalCompare>) {
401                 sel[newSize++] = i;
402               }
403             }
404           }
405           batch.size = newSize;
406         } else {
407           int newSize = 0;
408           for(int i = 0; i != n; i++) {
409             if (!nullPos1[i]) {
410               if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
411                                      vector2[0], start2[0], length2[0])<OptionalCompare>) {
412                 sel[newSize++] = i;
413               }
414             }
415           }
416           if (newSize < batch.size) {
417             batch.size = newSize;
418             batch.selectedInUse = true;
419           }
420         }
421       } else { // neither input is repeating
422          if (batch.selectedInUse) {
423           int newSize = 0;
424           for(int j = 0; j != n; j++) {
425             int i = sel[j];
426             if (!nullPos1[i] && !nullPos2[i]) {
427               if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
428                                      vector2[i], start2[i], length2[i])<OptionalCompare>) {
429                 sel[newSize++] = i;
430               }
431             }
432           }
433           batch.size = newSize;
434         } else {
435           int newSize = 0;
436           for(int i = 0; i != n; i++) {
437             if (!nullPos1[i] && !nullPos2[i]) {
438               if (<CompareOrEqual>(vector1[i], start1[i], length1[i], 
439                                      vector2[i], start2[i], length2[i])<OptionalCompare>) {
440                 sel[newSize++] = i;
441               }
442             }
443           }
444           if (newSize < batch.size) {
445             batch.size = newSize;
446             batch.selectedInUse = true;
447           }
448         }      
449       } 
450     }
451   }
452
453   @Override
454   public String getOutputType() {
455     return "boolean";
456   }
457
458   @Override
459   public int getOutputColumn() {
460     return -1;
461   }
462   
463   public int getColNum1() {
464     return colNum1;
465   }
466
467   public void setColNum1(int colNum1) {
468     this.colNum1 = colNum1;
469   }
470
471   public int getColNum2() {
472     return colNum2;
473   }
474
475   public void setColNum2(int colNum2) {
476     this.colNum2 = colNum2;
477   }
478
479   @Override
480   public VectorExpressionDescriptor.Descriptor getDescriptor() {
481     return (new VectorExpressionDescriptor.Builder())
482         .setMode(
483             VectorExpressionDescriptor.Mode.FILTER)
484         .setNumArguments(2)
485         .setArgumentTypes(
486             VectorExpressionDescriptor.ArgumentType.STRING_FAMILY,
487             VectorExpressionDescriptor.ArgumentType.STRING_FAMILY)
488         .setInputExpressionTypes(
489             VectorExpressionDescriptor.InputExpressionType.COLUMN,
490             VectorExpressionDescriptor.InputExpressionType.COLUMN).build();
491   }
492 }