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