-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathPrestoAggregateFunction.java
More file actions
713 lines (629 loc) · 30.8 KB
/
PrestoAggregateFunction.java
File metadata and controls
713 lines (629 loc) · 30.8 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
package sqlancer.presto.ast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import sqlancer.Randomly;
import sqlancer.presto.PrestoSchema;
import sqlancer.presto.PrestoSchema.PrestoCompositeDataType;
import sqlancer.presto.PrestoSchema.PrestoDataType;
import sqlancer.presto.gen.PrestoTypedExpressionGenerator;
public enum PrestoAggregateFunction implements PrestoFunction {
// General Aggregate Functions
// arbitrary(x) → [same as input]
// Returns an arbitrary non-null value of x, if one exists.
ARBITRARY("arbitrary", null) {
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
return true;
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { returnType.getPrimitiveDataType() };
}
@Override
public PrestoDataType getReturnType() {
return PrestoDataType.getRandomWithoutNull();
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoCompositeDataType returnType, boolean orderable) {
PrestoCompositeDataType returnTypeLocal = Objects.requireNonNullElseGet(returnType,
() -> PrestoCompositeDataType.fromDataType(getReturnType()));
return super.getArgumentsForReturnType(gen, depth, returnTypeLocal, orderable);
}
},
// TODO:
//
// array_agg(x) → array<[same as input]>#
// Returns an array created from the input x elements.
// avg(x) → double
// Returns the average (arithmetic mean) of all input values.
AVG("avg", PrestoDataType.FLOAT) {
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] {
Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT, PrestoDataType.DECIMAL) };
}
},
// avg(time interval type) → time interval type#
// Returns the average interval length of all input values.
AVG_INTERVAL_YM("avg", PrestoDataType.INTERVAL_YEAR_TO_MONTH, PrestoDataType.INTERVAL_YEAR_TO_MONTH),
AVG_INTERVAL_DS("avg", PrestoDataType.INTERVAL_DAY_TO_SECOND, PrestoDataType.INTERVAL_DAY_TO_SECOND),
// bool_and(boolean) → boolean#
// Returns TRUE if every input value is TRUE, otherwise FALSE.
BOOL_AND("bool_and", PrestoDataType.BOOLEAN, PrestoDataType.BOOLEAN),
// bool_or(boolean) → boolean#
// Returns TRUE if any input value is TRUE, otherwise FALSE.
BOOL_OR("bool_or", PrestoDataType.BOOLEAN, PrestoDataType.BOOLEAN),
// checksum(x) → varbinary#
// Returns an order-insensitive checksum of the given values.
CHECKSUM("checksum", PrestoDataType.VARBINARY) {
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { Randomly.fromList(PrestoDataType.getComparableTypes()) };
}
},
// count(*) → bigint#
// Returns the number of input rows.
COUNT_ALL("count(*)", PrestoDataType.INT),
// count(x) → bigint#
// Returns the number of non-null input values.
COUNT_NOARGS("count", PrestoDataType.INT), COUNT("count", PrestoDataType.INT) {
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { Randomly.fromOptions(PrestoDataType.getRandomWithoutNull()) };
}
},
// count_if(x) → bigint#
// Returns the number of TRUE input values. This function is equivalent to count(CASE WHEN x THEN 1 END).
COUNT_IF("count_if", PrestoDataType.INT) {
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { Randomly.fromOptions(PrestoDataType.getRandomWithoutNull()) };
}
},
// every(boolean) → boolean#
// This is an alias for bool_and().
EVERY("every", PrestoDataType.BOOLEAN, PrestoDataType.BOOLEAN),
// geometric_mean(x) → double#
// Returns the geometric mean of all input values.
GEOMETRIC_MEAN("geometric_mean", PrestoDataType.FLOAT) {
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] {
Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT, PrestoDataType.DECIMAL) };
}
},
// max_by(x, y) → [same as x]#
// Returns the value of x associated with the maximum value of y over all input values.
MAX_BY("max_by", null) {
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
return true;
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { returnType.getPrimitiveDataType(),
Randomly.fromList(PrestoDataType.getOrderableTypes()) };
}
@Override
public PrestoDataType getReturnType() {
return Randomly.fromList(PrestoDataType.getOrderableTypes());
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoCompositeDataType returnType, boolean orderable) {
PrestoCompositeDataType returnTypeLocal = Objects.requireNonNullElseGet(returnType,
() -> PrestoCompositeDataType.fromDataType(getReturnType()));
return super.getArgumentsForReturnType(gen, depth, returnTypeLocal, orderable);
}
},
// TODO:
//
// max_by(x, y, n) → array<[same as x]>#
// Returns n values of x associated with the n largest of all input values of y in descending order of y.
// min_by(x, y) → [same as x]#
// Returns the value of x associated with the minimum value of y over all input values.
MIN_BY("min_by", null) {
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
return true;
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { returnType.getPrimitiveDataType(),
Randomly.fromList(PrestoDataType.getOrderableTypes()) };
}
@Override
public PrestoDataType getReturnType() {
return Randomly.fromList(PrestoDataType.getOrderableTypes());
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoCompositeDataType returnType, boolean orderable) {
PrestoCompositeDataType returnTypeLocal = Objects.requireNonNullElseGet(returnType,
() -> PrestoCompositeDataType.fromDataType(getReturnType()));
return super.getArgumentsForReturnType(gen, depth, returnTypeLocal, orderable);
}
},
// TODO:
//
// min_by(x, y, n) → array<[same as x]>
// Returns n values of x associated with the n smallest of all input values of y in ascending order of y.
// max(x) → [same as input]
// Returns the maximum value of all input values.
MAX("max", null) {
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
boolean isCompatible = PrestoDataType.getOrderableTypes().contains(returnType.getPrimitiveDataType());
if (returnType.getPrimitiveDataType() == PrestoDataType.ARRAY && returnType.toString().contains("JSON")) {
isCompatible = false;
}
return isCompatible;
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { returnType.getPrimitiveDataType() };
}
@Override
public PrestoDataType getReturnType() {
return Randomly.fromList(PrestoDataType.getOrderableTypes());
}
@Override
public PrestoCompositeDataType getCompositeReturnType() {
PrestoDataType dataType = Randomly.fromList(PrestoDataType.getOrderableTypes());
PrestoCompositeDataType returnType;
do {
returnType = PrestoCompositeDataType.fromDataType(dataType);
} while (!isCompatibleWithReturnType(returnType));
return returnType;
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoCompositeDataType returnType, boolean orderable) {
PrestoCompositeDataType returnTypeLocal = Objects.requireNonNullElseGet(returnType,
() -> PrestoCompositeDataType.fromDataType(getReturnType()));
return super.getArgumentsForReturnType(gen, depth, returnTypeLocal, true);
}
},
// TODO:
//
// max(x, n) → array<[same as x]>#
// Returns n largest values of all input values of x.
// min(x) → [same as input]#
// Returns the minimum value of all input values.
MIN("min", null) {
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
boolean orderable = PrestoDataType.getOrderableTypes().contains(returnType.getPrimitiveDataType());
if (returnType.getPrimitiveDataType() == PrestoDataType.ARRAY && returnType.toString().contains("JSON")) {
orderable = false;
}
return orderable;
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { returnType.getPrimitiveDataType() };
}
@Override
public PrestoDataType getReturnType() {
return Randomly.fromList(PrestoDataType.getOrderableTypes());
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoCompositeDataType returnType, boolean orderable) {
PrestoCompositeDataType returnTypeLocal = Objects.requireNonNullElseGet(returnType,
() -> PrestoCompositeDataType.fromDataType(getReturnType()));
return super.getArgumentsForReturnType(gen, depth, returnTypeLocal, orderable);
}
},
// TODO:
//
// min(x, n) → array<[same as x]>#
// Returns n smallest values of all input values of x.
// TODO:
//
// reduce_agg(inputValue T, initialState S, inputFunction(S, T, S), combineFunction(S, S, S)) → S#
// Reduces all input values into a single value. inputFunction will be invoked for each input value. In addition to
// taking the input value, inputFunction takes the current state, initially initialState, and returns the new state.
// combineFunction will be invoked to combine two states into a new state. The final state is returned:
//
// SELECT id, reduce_agg(value, (a, b) -> a + b, (a, b) -> a + b)
// FROM (
// VALUES
// (1, 2),
// (1, 3),
// (1, 4),
// (2, 20),
// (2, 30),
// (2, 40)
// ) AS t(id, value)
// GROUP BY id;
// -- (1, 9)
// -- (2, 90)
//
// SELECT id, reduce_agg(value, (a, b) -> a * b, (a, b) -> a * b)
// FROM (
// VALUES
// (1, 2),
// (1, 3),
// (1, 4),
// (2, 20),
// (2, 30),
// (2, 40)
// ) AS t(id, value)
// GROUP BY id;
// -- (1, 24)
// -- (2, 24000)
// The state type must be a boolean, integer, floating-point, or date/time/interval.
// TODO:
//
// set_agg(x) → array<[same as input]>#
// Returns an array created from the distinct input x elements.
// TODO:
//
// set_union(array(T)) -> array(T)#
// Returns an array of all the distinct values contained in each array of the input
//
// Example:
//
// SELECT set_union(elements)
// FROM (
// VALUES
// ARRAY[1, 3],
// ARRAY[2, 4]
// ) AS t(elements);
// Returns ARRAY[1, 3, 4]
// sum(x) → [same as input]#
// Returns the sum of all input values.
SUM("sum", null) {
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
return List.of(PrestoDataType.INT, PrestoDataType.FLOAT, PrestoDataType.DECIMAL)
.contains(returnType.getPrimitiveDataType());
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { returnType.getPrimitiveDataType() };
}
@Override
public PrestoDataType getReturnType() {
return Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT, PrestoDataType.DECIMAL);
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoCompositeDataType returnType, boolean orderable) {
PrestoCompositeDataType returnTypeLocal = Objects.requireNonNullElseGet(returnType,
() -> PrestoCompositeDataType.fromDataType(getReturnType()));
return super.getArgumentsForReturnType(gen, depth, returnTypeLocal, orderable);
}
},
// sum(time interval type) → time interval type#
// Returns the average interval length of all input values.
SUM_INTERVAL_YM("sum", PrestoDataType.INTERVAL_YEAR_TO_MONTH, PrestoDataType.INTERVAL_YEAR_TO_MONTH),
SUM_INTERVAL_DS("sum", PrestoDataType.INTERVAL_DAY_TO_SECOND, PrestoDataType.INTERVAL_DAY_TO_SECOND),
// Bitwise Aggregate Functions#
// bitwise_and_agg(x) → bigint#
// Returns the bitwise AND of all input values in 2’s complement representation.
BITWISE_AND_AGG("bitwise_and_agg", PrestoDataType.INT, PrestoDataType.INT),
// bitwise_or_agg(x) → bigint#
// Returns the bitwise OR of all input values in 2’s complement representation.
BITWISE_OR_AGG("bitwise_or_agg", PrestoDataType.INT, PrestoDataType.INT),
// TODO:
//
// Map Aggregate Functions
// histogram(x)#
// Returns a map containing the count of the number of times each input value occurs.
//
// map_agg(key, value)#
// Returns a map created from the input key / value pairs.
//
// map_union(x(K, V)) -> map(K, V)#
// Returns the union of all the input maps. If a key is found in multiple input maps, that key’s value in the
// resulting map comes from an arbitrary input map.
//
// map_union_sum(x(K, V)) -> map(K, V)#
// Returns the union of all the input maps summing the values of matching keys in all the maps. All null values in
// the original maps are coalesced to 0.
//
// multimap_agg(key, value)#
// Returns a multimap created from the input key / value pairs. Each key can be associated with multiple values.
// Approximate Aggregate Functions#
// approx_distinct(x) → bigint#
// Returns the approximate number of distinct input values. This function provides an approximation of
// count(DISTINCT x).
// Zero is returned if all input values are null.
// This function should produce a standard error of 2.3%, which is the standard deviation of the (approximately
// normal)
// error distribution over all possible sets. It does not guarantee an upper bound on the error for any specific
// input set.
APPROX_DISTINCT("approx_distinct", PrestoDataType.INT) {
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { Randomly.fromList(PrestoDataType.getOrderableTypes()) };
}
},
//
// approx_distinct(x, e) → bigint#
// Returns the approximate number of distinct input values. This function provides an approximation of
// count(DISTINCT x). Zero is returned if all input values are null.
//
// This function should produce a standard error of no more than e, which is the standard deviation of the
// (approximately normal) error distribution over all possible sets. It does not guarantee an upper bound on the
// error for any specific input set. The current implementation of this function requires that e be in the range of
// [0.0040625, 0.26000].
APPROX_DISTINCT_2("approx_distinct", PrestoDataType.INT) {
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { Randomly.fromList(PrestoDataType.getOrderableTypes()), PrestoDataType.FLOAT };
}
},
// approx_percentile(x, percentage) → [same as x]#
// Returns the approximate percentile for all input values of x at the given percentage.
// The value of percentage must be between zero and one and must be constant for all input rows.
APPROX_PERCENTILE("approx_percentile", null) {
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
return List.of(PrestoDataType.INT, PrestoDataType.FLOAT).contains(returnType.getPrimitiveDataType());
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT),
PrestoDataType.FLOAT };
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoDataType[] argumentTypes2, PrestoCompositeDataType returnType2) {
List<PrestoExpression> arguments = new ArrayList<>();
arguments.add(gen.generateExpression(returnType2, depth + 1));
arguments.add(new PrestoConstant.PrestoFloatConstant(Randomly.getPercentage()));
return arguments;
}
@Override
public PrestoDataType getReturnType() {
return Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT);
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoCompositeDataType returnType, boolean orderable) {
PrestoCompositeDataType returnTypeLocal = Objects.requireNonNullElseGet(returnType,
() -> PrestoCompositeDataType.fromDataType(getReturnType()));
return super.getArgumentsForReturnType(gen, depth, returnTypeLocal, orderable);
}
},
// approx_percentile(x, percentage, accuracy) → [same as x]#
// As approx_percentile(x, percentage), but with a maximum rank error of accuracy.
// The value of accuracy must be between zero and one (exclusive) and must be constant for all input rows.
// Note that a lower “accuracy” is really a lower error threshold, and thus more accurate. The default accuracy is
// 0.01.
APPROX_PERCENTILE_ACCURACY("approx_percentile", null) {
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
return List.of(PrestoDataType.INT, PrestoDataType.FLOAT).contains(returnType.getPrimitiveDataType());
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT),
PrestoDataType.FLOAT, PrestoDataType.FLOAT };
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoDataType[] argumentTypes2, PrestoCompositeDataType returnType2) {
List<PrestoExpression> arguments = new ArrayList<>();
arguments.add(gen.generateExpression(returnType2, depth + 1));
arguments.add(new PrestoConstant.PrestoFloatConstant(Randomly.getPercentage()));
if (Randomly.getBooleanWithRatherLowProbability()) {
arguments.add(new PrestoConstant.PrestoFloatConstant(0.01D));
} else {
arguments.add(new PrestoConstant.PrestoFloatConstant(Randomly.getPercentage()));
}
return arguments;
}
@Override
public PrestoDataType getReturnType() {
return Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT);
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoCompositeDataType returnType, boolean orderable) {
PrestoCompositeDataType returnTypeLocal = Objects.requireNonNullElseGet(returnType,
() -> PrestoCompositeDataType
.fromDataType(Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT)));
return super.getArgumentsForReturnType(gen, depth, returnTypeLocal, orderable);
}
},
// TODO:
//
// approx_percentile(x, percentages) → array<[same as x]>#
// Returns the approximate percentile for all input values of x at each of the specified percentages. Each element
// of the percentages array must be between zero and one, and the array must be constant for all input rows.
//
// approx_percentile(x, percentages, accuracy) → array<[same as x]>#
// As approx_percentile(x, percentages), but with a maximum rank error of accuracy.
// approx_percentile(x, w, percentage) → [same as x]#
// Returns the approximate weighed percentile for all input values of x using the per-item weight w at the
// percentage p.
// The weight must be an integer value of at least one.
// It is effectively a replication count for the value x in the percentile set.
// The value of p must be between zero and one and must be constant for all input rows.
APPROX_PERCENTILE_WEIGHT("approx_percentile", null) {
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
return List.of(PrestoDataType.INT, PrestoDataType.FLOAT).contains(returnType.getPrimitiveDataType());
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT),
PrestoDataType.INT, PrestoDataType.FLOAT };
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoDataType[] argumentTypes2, PrestoCompositeDataType returnType2) {
List<PrestoExpression> arguments = new ArrayList<>();
arguments.add(gen.generateExpression(returnType2, depth + 1));
arguments.add(new PrestoConstant.PrestoFloatConstant(Randomly.getPercentage()));
if (Randomly.getBooleanWithRatherLowProbability()) {
arguments.add(new PrestoConstant.PrestoIntConstant(1));
} else {
arguments.add(new PrestoConstant.PrestoIntConstant(Randomly.smallNumber()));
}
return arguments;
}
@Override
public PrestoDataType getReturnType() {
return Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT);
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoCompositeDataType returnType, boolean orderable) {
PrestoCompositeDataType returnTypeLocal = Objects.requireNonNullElseGet(returnType,
() -> PrestoCompositeDataType
.fromDataType(Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT)));
return super.getArgumentsForReturnType(gen, depth, returnTypeLocal, orderable);
}
},
// approx_percentile(x, w, percentage, accuracy) → [same as x]#
// As approx_percentile(x, w, percentage), but with a maximum rank error of accuracy.
APPROX_PERCENTILE_PERCENTAGE_ACCURACY("approx_percentile", null) {
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
return List.of(PrestoDataType.INT, PrestoDataType.FLOAT).contains(returnType.getPrimitiveDataType());
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return new PrestoDataType[] { Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT),
PrestoDataType.INT, PrestoDataType.FLOAT, PrestoDataType.FLOAT };
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoDataType[] argumentTypes2, PrestoCompositeDataType returnType2) {
List<PrestoExpression> arguments = new ArrayList<>();
arguments.add(gen.generateExpression(returnType2, depth + 1));
if (Randomly.getBooleanWithRatherLowProbability()) {
arguments.add(new PrestoConstant.PrestoIntConstant(1));
} else {
arguments.add(new PrestoConstant.PrestoIntConstant(Randomly.smallNumber()));
}
arguments.add(new PrestoConstant.PrestoFloatConstant(Randomly.getPercentage()));
if (Randomly.getBooleanWithRatherLowProbability()) {
arguments.add(new PrestoConstant.PrestoFloatConstant(0.01D));
} else {
arguments.add(new PrestoConstant.PrestoFloatConstant(Randomly.getPercentage()));
}
return arguments;
}
@Override
public PrestoDataType getReturnType() {
return Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT);
}
@Override
public List<PrestoExpression> getArgumentsForReturnType(PrestoTypedExpressionGenerator gen, int depth,
PrestoCompositeDataType returnType, boolean orderable) {
PrestoCompositeDataType returnTypeLocal = Objects.requireNonNullElseGet(returnType,
() -> PrestoCompositeDataType
.fromDataType(Randomly.fromOptions(PrestoDataType.INT, PrestoDataType.FLOAT)));
return super.getArgumentsForReturnType(gen, depth, returnTypeLocal, orderable);
}
};
// TODO:
//
// approx_percentile(x, w, percentages) → array<[same as x]>#
// Returns the approximate weighed percentile for all input values of x using the per-item weight w at each of the
// given percentages specified in the array. The weight must be an integer value of at least one. It is effectively
// a replication count for the value x in the percentile set. Each element of the array must be between zero and
// one, and the array must be constant for all input rows.
//
// approx_percentile(x, w, percentages, accuracy) → array<[same as x]>#
// As approx_percentile(x, w, percentages), but with a maximum rank error of accuracy.
//
// approx_set(x) → HyperLogLog
// See HyperLogLog Functions.
//
// merge(x) → HyperLogLog
// See HyperLogLog Functions.
//
// khyperloglog_agg(x) → KHyperLogLog
// See KHyperLogLog Functions.
// TODO:
//
// merge(qdigest(T)) -> qdigest(T)
// See Quantile Digest Functions.
//
// qdigest_agg(x) → qdigest<[same as x]>
// See Quantile Digest Functions.
//
// qdigest_agg(x, w) → qdigest<[same as x]>
// See Quantile Digest Functions.
//
// qdigest_agg(x, w, accuracy) → qdigest<[same as x]>
// See Quantile Digest Functions.
//
// numeric_histogram(buckets, value, weight) → map<double, double>#
// Computes an approximate histogram with up to buckets number of buckets for all values with a per-item weight of
// weight.
// The keys of the returned map are roughly the center of the bin, and the entry is the total weight of the bin.
// The algorithm is based loosely on [BenHaimTomTov2010].
//
// buckets must be a bigint. value and weight must be numeric.
//
// numeric_histogram(buckets, value) → map<double, double>#
// Computes an approximate histogram with up to buckets number of buckets for all values. This function is
// equivalent to the variant of numeric_histogram() that takes a weight, with a per-item weight of 1. In this case,
// the total weight in the returned map is the count of items in the bin.
private final PrestoDataType returnType;
private final PrestoDataType[] argumentTypes;
private final String functionName;
PrestoAggregateFunction(String functionName, PrestoDataType returnType) {
this.functionName = functionName;
this.returnType = returnType;
this.argumentTypes = new PrestoDataType[0];
}
PrestoAggregateFunction(String functionName, PrestoDataType returnType, PrestoDataType... argumentTypes) {
this.functionName = functionName;
this.returnType = returnType;
this.argumentTypes = argumentTypes.clone();
}
public static PrestoAggregateFunction getRandomMetamorphicOracle() {
return Randomly.fromOptions(ARBITRARY, AVG, AVG_INTERVAL_YM, AVG_INTERVAL_DS, BOOL_AND, BOOL_OR, CHECKSUM,
COUNT_ALL, COUNT_NOARGS, COUNT, COUNT_IF, EVERY, GEOMETRIC_MEAN, MAX_BY, MIN_BY, MAX, MIN, SUM,
SUM_INTERVAL_YM, SUM_INTERVAL_DS, BITWISE_AND_AGG, BITWISE_OR_AGG);
}
public static PrestoAggregateFunction getRandom() {
return Randomly.fromOptions(values());
}
public static List<PrestoAggregateFunction> getFunctionsCompatibleWith(PrestoCompositeDataType returnType) {
return Stream.of(values()).filter(f -> f.isCompatibleWithReturnType(returnType)).collect(Collectors.toList());
}
@Override
public String getFunctionName() {
return functionName;
}
@Override
public boolean isCompatibleWithReturnType(PrestoCompositeDataType returnType) {
return this.returnType == returnType.getPrimitiveDataType();
}
@Override
public PrestoDataType[] getArgumentTypes(PrestoCompositeDataType returnType) {
return argumentTypes.clone();
}
@Override
public int getNumberOfArguments() {
return 1;
}
public List<PrestoSchema.PrestoDataType> getReturnTypes(PrestoSchema.PrestoDataType dataType) {
return Collections.singletonList(dataType);
}
public PrestoDataType getReturnType() {
if (returnType == null) {
return PrestoDataType.getRandomWithoutNull();
}
return returnType;
}
public PrestoCompositeDataType getCompositeReturnType() {
PrestoDataType dataType = getReturnType();
return PrestoCompositeDataType.fromDataType(dataType);
}
}