-
Notifications
You must be signed in to change notification settings - Fork 984
/
ComparisonFunctions.java
418 lines (335 loc) · 15.8 KB
/
ComparisonFunctions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
NOTE: ComparisonFunctions.java does not contain/generate all comparison
functions. DecimalFunctions.java and DateIntervalFunctions.java contain
some.
*/
<#-- TODO: Refactor comparison code from DecimalFunctions.java and
DateIntervalFunctions.java into here (to eliminate duplicate template code
and so that ComparisonFunctions actually has all comparison functions. -->
<@pp.dropOutputFile />
<#macro intervalCompareBlock leftType rightType leftMonths leftDays leftMillis rightMonths rightDays rightMillis output>
org.joda.time.MutableDateTime leftDate =
new org.joda.time.MutableDateTime(1970, 1, 1, 0, 0, 0, 0, org.joda.time.DateTimeZone.UTC);
org.joda.time.MutableDateTime rightDate =
new org.joda.time.MutableDateTime(1970, 1, 1, 0, 0, 0, 0, org.joda.time.DateTimeZone.UTC);
// Left and right date have the same starting point (epoch), add the interval period and compare the two
leftDate.addMonths(${leftMonths});
leftDate.addDays(${leftDays});
leftDate.add(${leftMillis});
rightDate.addMonths(${rightMonths});
rightDate.addDays(${rightDays});
rightDate.add(${rightMillis});
long leftMS = leftDate.getMillis();
long rightMS = rightDate.getMillis();
${output} = leftMS < rightMS ? -1 : (leftMS > rightMS ? 1 : 0);
</#macro>
<#macro compareNullsSubblock leftType rightType output breakTarget nullCompare nullComparesHigh>
<#if nullCompare>
<#if nullComparesHigh>
<#assign leftNullResult = 1> <#-- if only L is null and nulls are high, then "L > R" (1) -->
<#assign rightNullResult = -1>
<#else>
<#assign leftNullResult = -1> <#-- if only L is null and nulls are low, then "L < R" (-1) -->
<#assign rightNullResult = 1>
</#if>
<#if leftType?starts_with("Nullable")>
<#if rightType?starts_with("Nullable")>
<#-- Both are nullable. -->
if ( left.isSet == 0 ) {
if ( right.isSet == 0 ) {
<#-- Both are null--result is "L = R". -->
${output} = 0;
break ${breakTarget};
} else {
<#-- Only left is null--result is "L < R" or "L > R" per null ordering. -->
${output} = ${leftNullResult};
break ${breakTarget};
}
} else if ( right.isSet == 0 ) {
<#-- Only right is null--result is "L > R" or "L < R" per null ordering. -->
${output} = ${rightNullResult};
break ${breakTarget};
}
<#else>
<#-- Left is nullable but right is not. -->
if ( left.isSet == 0 ) {
<#-- Only left is null--result is "L < R" or "L > R" per null ordering. -->
${output} = ${leftNullResult};
break ${breakTarget};
}
</#if>
<#elseif rightType?starts_with("Nullable")>
<#-- Left is not nullable but right is. -->
if ( right.isSet == 0 ) {
<#-- Only right is null--result is "L > R" or "L < R" per null ordering. -->
${output} = ${rightNullResult};
break ${breakTarget};
}
</#if>
</#if>
</#macro>
<#-- macro compareBlock: Generates block handling comparison, including NULL. -->
<#-- Parameters: >
<#-- - mode: selects case of comparison code -->
<#-- - leftType: name of left argument's type (e.g., NullableFloat4) -->
<#-- - rightType: name of right argument's type -->
<#-- - output: output variable name -->
<#-- - nullCompare: whether to generate null-comparison code -->
<#-- - nullComparesHigh: whether NULL compares as the highest value or the lowest
value -->
<#macro compareBlock mode leftType rightType output nullCompare nullComparesHigh>
outside:
{
<@compareNullsSubblock leftType=leftType rightType=rightType
output="out.value" breakTarget="outside" nullCompare=true nullComparesHigh=nullComparesHigh />
<#if mode == "primitive">
${output} = left.value < right.value ? -1 : (left.value == right.value ? 0 : 1);
<#elseif mode == "varString">
${output} = org.apache.drill.exec.expr.fn.impl.ByteFunctionHelpers.compare(
left.buffer, left.start, left.end, right.buffer, right.start, right.end );
<#elseif mode == "intervalNameThis">
<@intervalCompareBlock leftType=leftType rightType=rightType
leftMonths ="left.months" leftDays ="left.days" leftMillis ="left.milliseconds"
rightMonths="right.months" rightDays="right.days" rightMillis="right.milliseconds"
output="${output}"/>
<#elseif mode == "intervalDay">
<@intervalCompareBlock leftType=leftType rightType=rightType
leftMonths ="0" leftDays ="left.days" leftMillis ="left.milliseconds"
rightMonths="0" rightDays="right.days" rightMillis="right.milliseconds"
output="${output}"/>
<#-- TODO: Refactor other comparison code to here. -->
<#else>
${mode_HAS_BAD_VALUE}
</#if>
} // outside
</#macro>
<#-- 1. For each group of cross-comparable types: -->
<#list comparisonTypesMain.typeGroups as typeGroup>
<#-- 2. For each pair of (cross-comparable) types in group: -->
<#list typeGroup.comparables as leftTypeBase>
<#list typeGroup.comparables as rightTypeBase>
<#-- Generate one file for each pair of base types (includes Nullable cases). -->
<@pp.changeOutputFile name="/org/apache/drill/exec/expr/fn/impl/GCompare${leftTypeBase}Vs${rightTypeBase}.java" />
<#include "/@includes/license.ftl" />
package org.apache.drill.exec.expr.fn.impl;
import org.apache.drill.exec.expr.DrillSimpleFunc;
import org.apache.drill.exec.expr.annotations.FunctionTemplate;
import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
import org.apache.drill.exec.expr.annotations.Output;
import org.apache.drill.exec.expr.annotations.Param;
import org.apache.drill.exec.expr.fn.FunctionGenerationHelper;
import org.apache.drill.exec.expr.fn.impl.ByteFunctionHelpers;
import org.apache.drill.exec.expr.holders.*;
import org.apache.drill.exec.record.RecordBatch;
import javax.inject.Inject;
import io.netty.buffer.DrillBuf;
@SuppressWarnings("unused")
public class GCompare${leftTypeBase}Vs${rightTypeBase} {
<#-- 3. For each combination of Nullable vs. not (of given non-nullable types): -->
<#list ["${leftTypeBase}", "Nullable${leftTypeBase}"] as leftType >
<#list ["${rightTypeBase}", "Nullable${rightTypeBase}"] as rightType >
<#-- Comparison function for sorting and grouping relational operators
(not for comparison expression operators (=, <, etc.)). -->
@FunctionTemplate(name = FunctionGenerationHelper.COMPARE_TO_NULLS_HIGH,
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.INTERNAL)
public static class GCompare${leftType}Vs${rightType}NullHigh implements DrillSimpleFunc {
@Param ${leftType}Holder left;
@Param ${rightType}Holder right;
@Output IntHolder out;
public void setup() {}
public void eval() {
<@compareBlock mode=typeGroup.mode leftType=leftType rightType=rightType
output="out.value" nullCompare=true nullComparesHigh=true />
}
}
<#-- Comparison function for sorting and grouping relational operators
(not for comparison expression operators (=, <, etc.)). -->
@FunctionTemplate(name = FunctionGenerationHelper.COMPARE_TO_NULLS_LOW,
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.INTERNAL)
public static class GCompare${leftType}Vs${rightType}NullLow implements DrillSimpleFunc {
@Param ${leftType}Holder left;
@Param ${rightType}Holder right;
@Output IntHolder out;
public void setup() {}
public void eval() {
<@compareBlock mode=typeGroup.mode leftType=leftType rightType=rightType
output="out.value" nullCompare=true nullComparesHigh=false />
}
}
</#list>
</#list> <#-- 3. Nullable combinations -->
<#-- Comparison function for comparison expression operator (=, <, etc.),
not for sorting and grouping relational operators. -->
@FunctionTemplate(names = {"less_than", "<"},
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.NULL_IF_NULL)
public static class LessThan${leftTypeBase}Vs${rightTypeBase} implements DrillSimpleFunc {
@Param ${leftTypeBase}Holder left;
@Param ${rightTypeBase}Holder right;
@Output BitHolder out;
public void setup() {}
public void eval() {
<#if typeGroup.mode == "primitive">
out.value = left.value < right.value ? 1 : 0;
<#elseif typeGroup.mode == "varString"
|| typeGroup.mode == "intervalNameThis" || typeGroup.mode == "intervalDay" >
int cmp;
<@compareBlock mode=typeGroup.mode leftType=leftTypeBase rightType=rightTypeBase
output="cmp" nullCompare=false nullComparesHigh=false />
out.value = cmp == -1 ? 1 : 0;
<#-- TODO: Refactor other comparison code to here. -->
<#else>
${mode_HAS_BAD_VALUE}
</#if>
}
}
<#-- Comparison function for comparison expression operator (=, <, etc.),
not for sorting and grouping relational operators. -->
@FunctionTemplate(names = {"less_than_or_equal_to", "<="},
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.NULL_IF_NULL)
public static class LessThanEq${leftTypeBase}Vs${rightTypeBase} implements DrillSimpleFunc {
@Param ${leftTypeBase}Holder left;
@Param ${rightTypeBase}Holder right;
@Output BitHolder out;
public void setup() {}
public void eval() {
<#if typeGroup.mode == "primitive">
out.value = left.value <= right.value ? 1 : 0;
<#elseif typeGroup.mode == "varString"
|| typeGroup.mode == "intervalNameThis" || typeGroup.mode == "intervalDay" >
int cmp;
<@compareBlock mode=typeGroup.mode leftType=leftTypeBase rightType=rightTypeBase
output="cmp" nullCompare=false nullComparesHigh=false />
out.value = cmp < 1 ? 1 : 0;
<#-- TODO: Refactor other comparison code to here. -->
<#else>
${mode_HAS_BAD_VALUE}
</#if>
}
}
<#-- Comparison function for comparison expression operator (=, <, etc.),
not for sorting and grouping relational operators. -->
@FunctionTemplate(names = {"greater_than", ">"},
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.NULL_IF_NULL)
public static class GreaterThan${leftTypeBase}Vs${rightTypeBase} implements DrillSimpleFunc {
@Param ${leftTypeBase}Holder left;
@Param ${rightTypeBase}Holder right;
@Output BitHolder out;
public void setup() {}
public void eval() {
<#if typeGroup.mode == "primitive">
out.value = left.value > right.value ? 1 : 0;
<#elseif typeGroup.mode == "varString"
|| typeGroup.mode == "intervalNameThis" || typeGroup.mode == "intervalDay" >
int cmp;
<@compareBlock mode=typeGroup.mode leftType=leftTypeBase rightType=rightTypeBase
output="cmp" nullCompare=false nullComparesHigh=false />
out.value = cmp == 1 ? 1 : 0;
<#-- TODO: Refactor other comparison code to here. -->
<#else>
${mode_HAS_BAD_VALUE}
</#if>
}
}
<#-- Comparison function for comparison expression operator (=, <, etc.),
not for sorting and grouping relational operators. -->
@FunctionTemplate(names = {"greater_than_or_equal_to", ">="},
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.NULL_IF_NULL)
public static class GreaterThanEq${leftTypeBase}Vs${rightTypeBase} implements DrillSimpleFunc {
@Param ${leftTypeBase}Holder left;
@Param ${rightTypeBase}Holder right;
@Output BitHolder out;
public void setup() {}
public void eval() {
<#if typeGroup.mode == "primitive">
out.value = left.value >= right.value ? 1 : 0;
<#elseif typeGroup.mode == "varString"
|| typeGroup.mode == "intervalNameThis" || typeGroup.mode == "intervalDay" >
int cmp;
<@compareBlock mode=typeGroup.mode leftType=leftTypeBase rightType=rightTypeBase
output="cmp" nullCompare=false nullComparesHigh=false />
out.value = cmp > -1 ? 1 : 0;
<#-- TODO: Refactor other comparison code to here. -->
<#else>
${mode_HAS_BAD_VALUE}
</#if>
}
}
<#-- Comparison function for comparison expression operator (=, <, etc.),
not for sorting and grouping relational operators. -->
@FunctionTemplate(names = {"equal", "==", "="},
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.NULL_IF_NULL)
public static class Equals${leftTypeBase}Vs${rightTypeBase} implements DrillSimpleFunc {
@Param ${leftTypeBase}Holder left;
@Param ${rightTypeBase}Holder right;
@Output BitHolder out;
public void setup() {}
public void eval() {
<#if typeGroup.mode == "primitive">
out.value = left.value == right.value ? 1 : 0;
<#elseif typeGroup.mode == "varString" >
out.value = org.apache.drill.exec.expr.fn.impl.ByteFunctionHelpers.equal(
left.buffer, left.start, left.end, right.buffer, right.start, right.end);
<#elseif typeGroup.mode == "intervalNameThis" || typeGroup.mode == "intervalDay" >
int cmp;
<@compareBlock mode=typeGroup.mode leftType=leftTypeBase rightType=rightTypeBase
output="cmp" nullCompare=false nullComparesHigh=false />
out.value = cmp == 0 ? 1 : 0;
<#-- TODO: Refactor other comparison code to here. -->
<#else>
${mode_HAS_BAD_VALUE}
</#if>
}
}
<#-- Comparison function for comparison expression operator (=, <, etc.),
not for sorting and grouping relational operators. -->
@FunctionTemplate(names = {"not_equal", "<>", "!="},
scope = FunctionTemplate.FunctionScope.SIMPLE,
nulls = NullHandling.NULL_IF_NULL)
public static class NotEquals${leftTypeBase}Vs${rightTypeBase} implements DrillSimpleFunc {
@Param ${leftTypeBase}Holder left;
@Param ${rightTypeBase}Holder right;
@Output BitHolder out;
public void setup() {}
public void eval() {
<#if typeGroup.mode == "primitive">
out.value = left.value != right.value ? 1 : 0;
<#elseif typeGroup.mode == "varString"
|| typeGroup.mode == "intervalNameThis" || typeGroup.mode == "intervalDay" >
int cmp;
<@compareBlock mode=typeGroup.mode leftType=leftTypeBase rightType=rightTypeBase
output="cmp" nullCompare=false nullComparesHigh=false />
out.value = cmp == 0 ? 0 : 1;
<#-- TODO: Refactor other comparison code to here. -->
<#else>
${mode_HAS_BAD_VALUE}
</#if>
}
}
}
</#list> <#-- 2. Pair of types-->
</#list>
</#list> <#-- 1. Group -->