-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
TestCaseSupplier.java
1505 lines (1374 loc) · 55.7 KB
/
TestCaseSupplier.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
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.esql.expression.function;
import org.apache.lucene.document.InetAddressPoint;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.geo.GeometryTestUtils;
import org.elasticsearch.geo.ShapeTestUtils;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.logging.Logger;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.AbstractConvertFunction;
import org.elasticsearch.xpack.esql.type.EsqlDataTypes;
import org.elasticsearch.xpack.ql.expression.Expression;
import org.elasticsearch.xpack.ql.expression.Literal;
import org.elasticsearch.xpack.ql.tree.Source;
import org.elasticsearch.xpack.ql.type.DataType;
import org.elasticsearch.xpack.ql.type.DataTypes;
import org.elasticsearch.xpack.ql.util.NumericUtils;
import org.elasticsearch.xpack.versionfield.Version;
import org.hamcrest.Matcher;
import java.math.BigInteger;
import java.time.Duration;
import java.time.Instant;
import java.time.Period;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.DoubleFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.LongFunction;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import static org.elasticsearch.xpack.ql.util.SpatialCoordinateTypes.CARTESIAN;
import static org.elasticsearch.xpack.ql.util.SpatialCoordinateTypes.GEO;
import static org.hamcrest.Matchers.equalTo;
/**
* This class exists to give a human-readable string representation of the test case.
*/
public record TestCaseSupplier(String name, List<DataType> types, Supplier<TestCase> supplier)
implements
Supplier<TestCaseSupplier.TestCase> {
private static final Logger logger = LogManager.getLogger(TestCaseSupplier.class);
/**
* Build a test case without types.
*
* @deprecated Supply types
*/
@Deprecated
public TestCaseSupplier(String name, Supplier<TestCase> supplier) {
this(name, null, supplier);
}
/**
* Build a test case named after the types it takes.
*/
public TestCaseSupplier(List<DataType> types, Supplier<TestCase> supplier) {
this(nameFromTypes(types), types, supplier);
}
public static String nameFromTypes(List<DataType> types) {
return types.stream().map(t -> "<" + t.typeName() + ">").collect(Collectors.joining(", "));
}
public static List<TestCaseSupplier> stringCases(
BinaryOperator<Object> expected,
BiFunction<DataType, DataType, String> evaluatorToString,
List<String> warnings,
DataType expectedType
) {
List<TypedDataSupplier> lhsSuppliers = new ArrayList<>();
List<TypedDataSupplier> rhsSuppliers = new ArrayList<>();
List<TestCaseSupplier> suppliers = new ArrayList<>();
for (DataType type : AbstractConvertFunction.STRING_TYPES) {
lhsSuppliers.addAll(stringCases(type));
rhsSuppliers.addAll(stringCases(type));
casesCrossProduct(
expected,
lhsSuppliers,
rhsSuppliers,
evaluatorToString,
(lhs, rhs) -> warnings,
suppliers,
expectedType,
true
);
}
return suppliers;
}
@Override
public TestCase get() {
TestCase supplied = supplier.get();
if (types != null) {
for (int i = 0; i < types.size(); i++) {
if (supplied.getData().get(i).type() != types.get(i)) {
throw new IllegalStateException(
name + ": supplier/data type mismatch " + supplied.getData().get(i).type() + "/" + types.get(i)
);
}
}
}
return supplied;
}
@Override
public String toString() {
return name;
}
/**
* Generate positive test cases for unary functions that operate on an {@code numeric}
* fields by casting them to {@link DataTypes#DOUBLE}s.
*/
public static List<TestCaseSupplier> forUnaryCastingToDouble(
String name,
String argName,
UnaryOperator<Double> expected,
Double min,
Double max,
List<String> warnings
) {
String read = "Attribute[channel=0]";
String eval = name + "[" + argName + "=";
List<TestCaseSupplier> suppliers = new ArrayList<>();
forUnaryInt(
suppliers,
eval + castToDoubleEvaluator(read, DataTypes.INTEGER) + "]",
DataTypes.DOUBLE,
i -> expected.apply(Double.valueOf(i)),
min.intValue(),
max.intValue(),
warnings
);
forUnaryLong(
suppliers,
eval + castToDoubleEvaluator(read, DataTypes.LONG) + "]",
DataTypes.DOUBLE,
i -> expected.apply(Double.valueOf(i)),
min.longValue(),
max.longValue(),
warnings
);
forUnaryUnsignedLong(
suppliers,
eval + castToDoubleEvaluator(read, DataTypes.UNSIGNED_LONG) + "]",
DataTypes.DOUBLE,
ul -> expected.apply(ul.doubleValue()),
BigInteger.valueOf((int) Math.ceil(min)),
BigInteger.valueOf((int) Math.floor(max)),
warnings
);
forUnaryDouble(suppliers, eval + read + "]", DataTypes.DOUBLE, expected::apply, min, max, warnings);
return suppliers;
}
/**
* Generate positive test cases for binary functions that operate on an {@code numeric}
* fields by casting them to {@link DataTypes#DOUBLE}s.
*/
public static List<TestCaseSupplier> forBinaryCastingToDouble(
String name,
String lhsName,
String rhsName,
BinaryOperator<Double> expected,
Double lhsMin,
Double lhsMax,
Double rhsMin,
Double rhsMax,
List<String> warnings
) {
List<TypedDataSupplier> lhsSuppliers = castToDoubleSuppliersFromRange(lhsMin, lhsMax);
List<TypedDataSupplier> rhsSuppliers = castToDoubleSuppliersFromRange(rhsMin, rhsMax);
return forBinaryCastingToDouble(name, lhsName, rhsName, expected, lhsSuppliers, rhsSuppliers, warnings);
}
public static List<TestCaseSupplier> forBinaryCastingToDouble(
String name,
String lhsName,
String rhsName,
BinaryOperator<Double> expected,
List<TypedDataSupplier> lhsSuppliers,
List<TypedDataSupplier> rhsSuppliers,
List<String> warnings
) {
List<TestCaseSupplier> suppliers = new ArrayList<>();
casesCrossProduct(
(l, r) -> expected.apply(((Number) l).doubleValue(), ((Number) r).doubleValue()),
lhsSuppliers,
rhsSuppliers,
(lhsType, rhsType) -> name
+ "["
+ lhsName
+ "="
+ castToDoubleEvaluator("Attribute[channel=0]", lhsType)
+ ", "
+ rhsName
+ "="
+ castToDoubleEvaluator("Attribute[channel=1]", rhsType)
+ "]",
(lhs, rhs) -> warnings,
suppliers,
DataTypes.DOUBLE,
false
);
return suppliers;
}
public static void casesCrossProduct(
BinaryOperator<Object> expected,
List<TypedDataSupplier> lhsSuppliers,
List<TypedDataSupplier> rhsSuppliers,
BiFunction<DataType, DataType, String> evaluatorToString,
BiFunction<TypedData, TypedData, List<String>> warnings,
List<TestCaseSupplier> suppliers,
DataType expectedType,
boolean symmetric
) {
for (TypedDataSupplier lhsSupplier : lhsSuppliers) {
for (TypedDataSupplier rhsSupplier : rhsSuppliers) {
suppliers.add(testCaseSupplier(lhsSupplier, rhsSupplier, evaluatorToString, expectedType, expected, warnings));
if (symmetric) {
suppliers.add(testCaseSupplier(rhsSupplier, lhsSupplier, evaluatorToString, expectedType, expected, warnings));
}
}
}
}
public static TestCaseSupplier testCaseSupplier(
TypedDataSupplier lhsSupplier,
TypedDataSupplier rhsSupplier,
BiFunction<DataType, DataType, String> evaluatorToString,
DataType expectedType,
BinaryOperator<Object> expectedValue
) {
return testCaseSupplier(lhsSupplier, rhsSupplier, evaluatorToString, expectedType, expectedValue, (lhs, rhs) -> List.of());
}
private static TestCaseSupplier testCaseSupplier(
TypedDataSupplier lhsSupplier,
TypedDataSupplier rhsSupplier,
BiFunction<DataType, DataType, String> evaluatorToString,
DataType expectedType,
BinaryOperator<Object> expectedValue,
BiFunction<TypedData, TypedData, List<String>> warnings
) {
String caseName = lhsSupplier.name() + ", " + rhsSupplier.name();
return new TestCaseSupplier(caseName, List.of(lhsSupplier.type(), rhsSupplier.type()), () -> {
TypedData lhsTyped = lhsSupplier.get();
TypedData rhsTyped = rhsSupplier.get();
TestCase testCase = new TestCase(
List.of(lhsTyped, rhsTyped),
evaluatorToString.apply(lhsSupplier.type(), rhsSupplier.type()),
expectedType,
equalTo(expectedValue.apply(lhsTyped.getValue(), rhsTyped.getValue()))
);
for (String warning : warnings.apply(lhsTyped, rhsTyped)) {
testCase = testCase.withWarning(warning);
}
return testCase;
});
}
public static List<TypedDataSupplier> castToDoubleSuppliersFromRange(Double Min, Double Max) {
List<TypedDataSupplier> suppliers = new ArrayList<>();
suppliers.addAll(intCases(Min.intValue(), Max.intValue(), true));
suppliers.addAll(longCases(Min.longValue(), Max.longValue(), true));
suppliers.addAll(ulongCases(BigInteger.valueOf((long) Math.ceil(Min)), BigInteger.valueOf((long) Math.floor(Max)), true));
suppliers.addAll(doubleCases(Min, Max, true));
return suppliers;
}
public record NumericTypeTestConfig<T>(Number min, Number max, BiFunction<Number, Number, T> expected, String evaluatorName) {}
public record NumericTypeTestConfigs<T>(
NumericTypeTestConfig<T> intStuff,
NumericTypeTestConfig<T> longStuff,
NumericTypeTestConfig<T> doubleStuff
) {
public NumericTypeTestConfig<T> get(DataType type) {
if (type == DataTypes.INTEGER) {
return intStuff;
}
if (type == DataTypes.LONG) {
return longStuff;
}
if (type == DataTypes.DOUBLE) {
return doubleStuff;
}
throw new IllegalArgumentException("bogus numeric type [" + type + "]");
}
}
public static DataType widen(DataType lhs, DataType rhs) {
if (lhs == rhs) {
return lhs;
}
if (lhs == DataTypes.DOUBLE || rhs == DataTypes.DOUBLE) {
return DataTypes.DOUBLE;
}
if (lhs == DataTypes.LONG || rhs == DataTypes.LONG) {
return DataTypes.LONG;
}
throw new IllegalArgumentException("Invalid numeric widening lhs: [" + lhs + "] rhs: [" + rhs + "]");
}
public static List<TypedDataSupplier> getSuppliersForNumericType(DataType type, Number min, Number max, boolean includeZero) {
if (type == DataTypes.INTEGER) {
return intCases(NumericUtils.saturatingIntValue(min), NumericUtils.saturatingIntValue(max), includeZero);
}
if (type == DataTypes.LONG) {
return longCases(min.longValue(), max.longValue(), includeZero);
}
if (type == DataTypes.UNSIGNED_LONG) {
return ulongCases(
min instanceof BigInteger ? (BigInteger) min : BigInteger.valueOf(Math.max(min.longValue(), 0L)),
max instanceof BigInteger ? (BigInteger) max : BigInteger.valueOf(Math.max(max.longValue(), 0L)),
includeZero
);
}
if (type == DataTypes.DOUBLE) {
return doubleCases(min.doubleValue(), max.doubleValue(), includeZero);
}
throw new IllegalArgumentException("bogus numeric type [" + type + "]");
}
public static List<TestCaseSupplier> forBinaryComparisonWithWidening(
NumericTypeTestConfigs<Boolean> typeStuff,
String lhsName,
String rhsName,
BiFunction<TypedData, TypedData, List<String>> warnings,
boolean allowRhsZero
) {
List<TestCaseSupplier> suppliers = new ArrayList<>();
List<DataType> numericTypes = List.of(DataTypes.INTEGER, DataTypes.LONG, DataTypes.DOUBLE);
for (DataType lhsType : numericTypes) {
for (DataType rhsType : numericTypes) {
DataType expected = widen(lhsType, rhsType);
NumericTypeTestConfig<Boolean> expectedTypeStuff = typeStuff.get(expected);
BiFunction<DataType, DataType, String> evaluatorToString = (lhs, rhs) -> expectedTypeStuff.evaluatorName()
+ "["
+ lhsName
+ "="
+ getCastEvaluator("Attribute[channel=0]", lhs, expected)
+ ", "
+ rhsName
+ "="
+ getCastEvaluator("Attribute[channel=1]", rhs, expected)
+ "]";
casesCrossProduct(
(l, r) -> expectedTypeStuff.expected().apply((Number) l, (Number) r),
getSuppliersForNumericType(lhsType, expectedTypeStuff.min(), expectedTypeStuff.max(), allowRhsZero),
getSuppliersForNumericType(rhsType, expectedTypeStuff.min(), expectedTypeStuff.max(), allowRhsZero),
evaluatorToString,
warnings,
suppliers,
DataTypes.BOOLEAN,
true
);
}
}
return suppliers;
}
public static List<TestCaseSupplier> forBinaryWithWidening(
NumericTypeTestConfigs<Number> typeStuff,
String lhsName,
String rhsName,
BiFunction<TypedData, TypedData, List<String>> warnings,
boolean allowRhsZero
) {
List<TestCaseSupplier> suppliers = new ArrayList<>();
List<DataType> numericTypes = List.of(DataTypes.INTEGER, DataTypes.LONG, DataTypes.DOUBLE);
for (DataType lhsType : numericTypes) {
for (DataType rhsType : numericTypes) {
DataType expected = widen(lhsType, rhsType);
NumericTypeTestConfig<Number> expectedTypeStuff = typeStuff.get(expected);
BiFunction<DataType, DataType, String> evaluatorToString = (lhs, rhs) -> expectedTypeStuff.evaluatorName()
+ "["
+ lhsName
+ "="
+ getCastEvaluator("Attribute[channel=0]", lhs, expected)
+ ", "
+ rhsName
+ "="
+ getCastEvaluator("Attribute[channel=1]", rhs, expected)
+ "]";
casesCrossProduct(
(l, r) -> expectedTypeStuff.expected().apply((Number) l, (Number) r),
getSuppliersForNumericType(lhsType, expectedTypeStuff.min(), expectedTypeStuff.max(), true),
getSuppliersForNumericType(rhsType, expectedTypeStuff.min(), expectedTypeStuff.max(), allowRhsZero),
evaluatorToString,
warnings,
suppliers,
expected,
false
);
}
}
return suppliers;
}
public static List<TestCaseSupplier> forBinaryNotCasting(
String name,
String lhsName,
String rhsName,
BinaryOperator<Object> expected,
DataType expectedType,
List<TypedDataSupplier> lhsSuppliers,
List<TypedDataSupplier> rhsSuppliers,
List<String> warnings,
boolean symmetric
) {
return forBinaryNotCasting(
name,
lhsName,
rhsName,
expected,
expectedType,
lhsSuppliers,
rhsSuppliers,
(lhs, rhs) -> warnings,
symmetric
);
}
public static List<TestCaseSupplier> forBinaryNotCasting(
String name,
String lhsName,
String rhsName,
BinaryOperator<Object> expected,
DataType expectedType,
List<TypedDataSupplier> lhsSuppliers,
List<TypedDataSupplier> rhsSuppliers,
BiFunction<TypedData, TypedData, List<String>> warnings,
boolean symmetric
) {
List<TestCaseSupplier> suppliers = new ArrayList<>();
casesCrossProduct(
expected,
lhsSuppliers,
rhsSuppliers,
(lhsType, rhsType) -> name + "[" + lhsName + "=Attribute[channel=0], " + rhsName + "=Attribute[channel=1]]",
warnings,
suppliers,
expectedType,
symmetric
);
return suppliers;
}
/**
* Generate positive test cases for a unary function operating on an {@link DataTypes#INTEGER}.
*/
public static void forUnaryInt(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
IntFunction<Object> expectedValue,
int lowerBound,
int upperBound,
Function<Number, List<String>> expectedWarnings
) {
unaryNumeric(
suppliers,
expectedEvaluatorToString,
intCases(lowerBound, upperBound, true),
expectedType,
n -> expectedValue.apply(n.intValue()),
n -> expectedWarnings.apply(n.intValue())
);
}
public static void forUnaryInt(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
IntFunction<Object> expectedValue,
int lowerBound,
int upperBound,
List<String> warnings
) {
forUnaryInt(suppliers, expectedEvaluatorToString, expectedType, expectedValue, lowerBound, upperBound, unused -> warnings);
}
/**
* Generate positive test cases for a unary function operating on an {@link DataTypes#LONG}.
*/
public static void forUnaryLong(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
LongFunction<Object> expectedValue,
long lowerBound,
long upperBound,
Function<Number, List<String>> expectedWarnings
) {
unaryNumeric(
suppliers,
expectedEvaluatorToString,
longCases(lowerBound, upperBound, true),
expectedType,
n -> expectedValue.apply(n.longValue()),
expectedWarnings
);
}
public static void forUnaryLong(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
LongFunction<Object> expectedValue,
long lowerBound,
long upperBound,
List<String> warnings
) {
forUnaryLong(suppliers, expectedEvaluatorToString, expectedType, expectedValue, lowerBound, upperBound, unused -> warnings);
}
/**
* Generate positive test cases for a unary function operating on an {@link DataTypes#UNSIGNED_LONG}.
*/
public static void forUnaryUnsignedLong(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<BigInteger, Object> expectedValue,
BigInteger lowerBound,
BigInteger upperBound,
Function<BigInteger, List<String>> expectedWarnings
) {
unaryNumeric(
suppliers,
expectedEvaluatorToString,
ulongCases(lowerBound, upperBound, true),
expectedType,
n -> expectedValue.apply((BigInteger) n),
n -> expectedWarnings.apply((BigInteger) n)
);
}
public static void forUnaryUnsignedLong(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<BigInteger, Object> expectedValue,
BigInteger lowerBound,
BigInteger upperBound,
List<String> warnings
) {
forUnaryUnsignedLong(suppliers, expectedEvaluatorToString, expectedType, expectedValue, lowerBound, upperBound, unused -> warnings);
}
/**
* Generate positive test cases for a unary function operating on an {@link DataTypes#DOUBLE}.
*/
public static void forUnaryDouble(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
DoubleFunction<Object> expectedValue,
double lowerBound,
double upperBound,
List<String> warnings
) {
forUnaryDouble(suppliers, expectedEvaluatorToString, expectedType, expectedValue, lowerBound, upperBound, unused -> warnings);
}
public static void forUnaryDouble(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
DoubleFunction<Object> expectedValue,
double lowerBound,
double upperBound,
DoubleFunction<List<String>> expectedWarnings
) {
unaryNumeric(
suppliers,
expectedEvaluatorToString,
doubleCases(lowerBound, upperBound, true),
expectedType,
n -> expectedValue.apply(n.doubleValue()),
n -> expectedWarnings.apply(n.doubleValue())
);
}
/**
* Generate positive test cases for a unary function operating on an {@link DataTypes#BOOLEAN}.
*/
public static void forUnaryBoolean(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<Boolean, Object> expectedValue,
List<String> warnings
) {
unary(suppliers, expectedEvaluatorToString, booleanCases(), expectedType, v -> expectedValue.apply((Boolean) v), warnings);
}
/**
* Generate positive test cases for a unary function operating on an {@link DataTypes#DATETIME}.
*/
public static void forUnaryDatetime(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<Instant, Object> expectedValue,
List<String> warnings
) {
unaryNumeric(
suppliers,
expectedEvaluatorToString,
dateCases(),
expectedType,
n -> expectedValue.apply(Instant.ofEpochMilli(n.longValue())),
warnings
);
}
/**
* Generate positive test cases for a unary function operating on an {@link EsqlDataTypes#GEO_POINT}.
*/
public static void forUnaryGeoPoint(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<BytesRef, Object> expectedValue,
List<String> warnings
) {
unary(suppliers, expectedEvaluatorToString, geoPointCases(), expectedType, n -> expectedValue.apply((BytesRef) n), warnings);
}
/**
* Generate positive test cases for a unary function operating on an {@link EsqlDataTypes#CARTESIAN_POINT}.
*/
public static void forUnaryCartesianPoint(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<BytesRef, Object> expectedValue,
List<String> warnings
) {
unary(suppliers, expectedEvaluatorToString, cartesianPointCases(), expectedType, n -> expectedValue.apply((BytesRef) n), warnings);
}
/**
* Generate positive test cases for a unary function operating on an {@link EsqlDataTypes#GEO_SHAPE}.
*/
public static void forUnaryGeoShape(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<BytesRef, Object> expectedValue,
List<String> warnings
) {
unary(suppliers, expectedEvaluatorToString, geoShapeCases(), expectedType, n -> expectedValue.apply((BytesRef) n), warnings);
}
/**
* Generate positive test cases for a unary function operating on an {@link EsqlDataTypes#CARTESIAN_SHAPE}.
*/
public static void forUnaryCartesianShape(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<BytesRef, Object> expectedValue,
List<String> warnings
) {
unary(suppliers, expectedEvaluatorToString, cartesianShapeCases(), expectedType, n -> expectedValue.apply((BytesRef) n), warnings);
}
/**
* Generate positive test cases for a unary function operating on an {@link DataTypes#IP}.
*/
public static void forUnaryIp(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<BytesRef, Object> expectedValue,
List<String> warnings
) {
unary(suppliers, expectedEvaluatorToString, ipCases(), expectedType, v -> expectedValue.apply((BytesRef) v), warnings);
}
/**
* Generate positive test cases for a unary function operating on an {@link DataTypes#KEYWORD} and {@link DataTypes#TEXT}.
*/
public static void forUnaryStrings(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<BytesRef, Object> expectedValue,
Function<BytesRef, List<String>> expectedWarnings
) {
for (DataType type : AbstractConvertFunction.STRING_TYPES) {
unary(
suppliers,
expectedEvaluatorToString,
stringCases(type),
expectedType,
v -> expectedValue.apply((BytesRef) v),
v -> expectedWarnings.apply((BytesRef) v)
);
}
}
public static void forUnaryStrings(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<BytesRef, Object> expectedValue,
List<String> warnings
) {
forUnaryStrings(suppliers, expectedEvaluatorToString, expectedType, expectedValue, unused -> warnings);
}
/**
* Generate positive test cases for a unary function operating on an {@link DataTypes#VERSION}.
*/
public static void forUnaryVersion(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
DataType expectedType,
Function<Version, Object> expectedValue,
List<String> warnings
) {
unary(
suppliers,
expectedEvaluatorToString,
versionCases(""),
expectedType,
v -> expectedValue.apply(new Version((BytesRef) v)),
warnings
);
}
private static void unaryNumeric(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
List<TypedDataSupplier> valueSuppliers,
DataType expectedOutputType,
Function<Number, Object> expectedValue,
Function<Number, List<String>> expectedWarnings
) {
unary(
suppliers,
expectedEvaluatorToString,
valueSuppliers,
expectedOutputType,
v -> expectedValue.apply((Number) v),
v -> expectedWarnings.apply((Number) v)
);
}
private static void unaryNumeric(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
List<TypedDataSupplier> valueSuppliers,
DataType expectedOutputType,
Function<Number, Object> expected,
List<String> warnings
) {
unaryNumeric(suppliers, expectedEvaluatorToString, valueSuppliers, expectedOutputType, expected, unused -> warnings);
}
public static void unary(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
List<TypedDataSupplier> valueSuppliers,
DataType expectedOutputType,
Function<Object, Object> expectedValue,
Function<Object, List<String>> expectedWarnings
) {
for (TypedDataSupplier supplier : valueSuppliers) {
suppliers.add(new TestCaseSupplier(supplier.name(), List.of(supplier.type()), () -> {
TypedData typed = supplier.get();
Object value = typed.getValue();
logger.info("Value is " + value + " of type " + value.getClass());
logger.info("expectedValue is " + expectedValue.apply(value));
TestCase testCase = new TestCase(
List.of(typed),
expectedEvaluatorToString,
expectedOutputType,
equalTo(expectedValue.apply(value))
);
for (String warning : expectedWarnings.apply(value)) {
testCase = testCase.withWarning(warning);
}
return testCase;
}));
}
}
public static void unary(
List<TestCaseSupplier> suppliers,
String expectedEvaluatorToString,
List<TypedDataSupplier> valueSuppliers,
DataType expectedOutputType,
Function<Object, Object> expected,
List<String> warnings
) {
unary(suppliers, expectedEvaluatorToString, valueSuppliers, expectedOutputType, expected, unused -> warnings);
}
public static List<TypedDataSupplier> intCases(int min, int max, boolean includeZero) {
List<TypedDataSupplier> cases = new ArrayList<>();
if (0 <= max && 0 >= min && includeZero) {
cases.add(new TypedDataSupplier("<0 int>", () -> 0, DataTypes.INTEGER));
}
int lower = Math.max(min, 1);
int upper = Math.min(max, Integer.MAX_VALUE);
if (lower < upper) {
cases.add(new TypedDataSupplier("<positive int>", () -> ESTestCase.randomIntBetween(lower, upper), DataTypes.INTEGER));
} else if (lower == upper) {
cases.add(new TypedDataSupplier("<" + lower + " int>", () -> lower, DataTypes.INTEGER));
}
int lower1 = Math.max(min, Integer.MIN_VALUE);
int upper1 = Math.min(max, -1);
if (lower1 < upper1) {
cases.add(new TypedDataSupplier("<negative int>", () -> ESTestCase.randomIntBetween(lower1, upper1), DataTypes.INTEGER));
} else if (lower1 == upper1) {
cases.add(new TypedDataSupplier("<" + lower1 + " int>", () -> lower1, DataTypes.INTEGER));
}
return cases;
}
public static List<TypedDataSupplier> longCases(long min, long max, boolean includeZero) {
List<TypedDataSupplier> cases = new ArrayList<>();
if (0L <= max && 0L >= min && includeZero) {
cases.add(new TypedDataSupplier("<0 long>", () -> 0L, DataTypes.LONG));
}
long lower = Math.max(min, 1);
long upper = Math.min(max, Long.MAX_VALUE);
if (lower < upper) {
cases.add(new TypedDataSupplier("<positive long>", () -> ESTestCase.randomLongBetween(lower, upper), DataTypes.LONG));
} else if (lower == upper) {
cases.add(new TypedDataSupplier("<" + lower + " long>", () -> lower, DataTypes.LONG));
}
long lower1 = Math.max(min, Long.MIN_VALUE);
long upper1 = Math.min(max, -1);
if (lower1 < upper1) {
cases.add(new TypedDataSupplier("<negative long>", () -> ESTestCase.randomLongBetween(lower1, upper1), DataTypes.LONG));
} else if (lower1 == upper1) {
cases.add(new TypedDataSupplier("<" + lower1 + " long>", () -> lower1, DataTypes.LONG));
}
return cases;
}
public static List<TypedDataSupplier> ulongCases(BigInteger min, BigInteger max, boolean includeZero) {
List<TypedDataSupplier> cases = new ArrayList<>();
// Zero
if (BigInteger.ZERO.compareTo(max) <= 0 && BigInteger.ZERO.compareTo(min) >= 0 && includeZero) {
cases.add(new TypedDataSupplier("<0 unsigned long>", () -> BigInteger.ZERO, DataTypes.UNSIGNED_LONG));
}
// small values, less than Long.MAX_VALUE
BigInteger lower1 = min.max(BigInteger.ONE);
BigInteger upper1 = max.min(BigInteger.valueOf(Long.MAX_VALUE));
if (lower1.compareTo(upper1) < 0) {
cases.add(
new TypedDataSupplier(
"<small unsigned long>",
() -> ESTestCase.randomUnsignedLongBetween(lower1, upper1),
DataTypes.UNSIGNED_LONG
)
);
} else if (lower1.compareTo(upper1) == 0) {
cases.add(new TypedDataSupplier("<small unsigned long>", () -> lower1, DataTypes.UNSIGNED_LONG));
}
// Big values, greater than Long.MAX_VALUE
BigInteger lower2 = min.max(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE));
BigInteger upper2 = max.min(ESTestCase.UNSIGNED_LONG_MAX);
if (lower2.compareTo(upper2) < 0) {
cases.add(
new TypedDataSupplier(
"<big unsigned long>",
() -> ESTestCase.randomUnsignedLongBetween(lower2, upper2),
DataTypes.UNSIGNED_LONG
)
);
} else if (lower2.compareTo(upper2) == 0) {
cases.add(new TypedDataSupplier("<big unsigned long>", () -> lower2, DataTypes.UNSIGNED_LONG));
}
return cases;
}
public static List<TypedDataSupplier> doubleCases(double min, double max, boolean includeZero) {
List<TypedDataSupplier> cases = new ArrayList<>();
// Zeros
if (0d <= max && 0d >= min && includeZero) {
cases.add(new TypedDataSupplier("<0 double>", () -> 0.0d, DataTypes.DOUBLE));
cases.add(new TypedDataSupplier("<-0 double>", () -> -0.0d, DataTypes.DOUBLE));
}
// Positive small double
double lower1 = Math.max(0d, min);
double upper1 = Math.min(1d, max);
if (lower1 < upper1) {
cases.add(
new TypedDataSupplier(
"<small positive double>",
() -> ESTestCase.randomDoubleBetween(lower1, upper1, true),
DataTypes.DOUBLE
)
);
} else if (lower1 == upper1) {
cases.add(new TypedDataSupplier("<small positive double>", () -> lower1, DataTypes.DOUBLE));
}
// Negative small double
double lower2 = Math.max(-1d, min);
double upper2 = Math.min(0d, max);
if (lower2 < upper2) {
cases.add(
new TypedDataSupplier(
"<small negative double>",
() -> ESTestCase.randomDoubleBetween(lower2, upper2, true),
DataTypes.DOUBLE
)
);
} else if (lower2 == upper2) {
cases.add(new TypedDataSupplier("<small negative double>", () -> lower2, DataTypes.DOUBLE));
}
// Positive big double
double lower3 = Math.max(1d, min); // start at 1 (inclusive) because the density of values between 0 and 1 is very high
double upper3 = Math.min(Double.MAX_VALUE, max);
if (lower3 < upper3) {
cases.add(
new TypedDataSupplier("<big positive double>", () -> ESTestCase.randomDoubleBetween(lower3, upper3, true), DataTypes.DOUBLE)
);
} else if (lower3 == upper3) {
cases.add(new TypedDataSupplier("<big positive double>", () -> lower3, DataTypes.DOUBLE));
}
// Negative big double
// note: Double.MIN_VALUE is the smallest non-zero positive double, not the smallest non-infinite negative double.
double lower4 = Math.max(-Double.MAX_VALUE, min);
double upper4 = Math.min(-1, max); // because again, the interval from -1 to 0 is very high density
if (lower4 < upper4) {
cases.add(
new TypedDataSupplier("<big negative double>", () -> ESTestCase.randomDoubleBetween(lower4, upper4, true), DataTypes.DOUBLE)
);
} else if (lower4 == upper4) {
cases.add(new TypedDataSupplier("<big negative double>", () -> lower4, DataTypes.DOUBLE));
}
return cases;
}
public static List<TypedDataSupplier> booleanCases() {
return List.of(
new TypedDataSupplier("<true>", () -> true, DataTypes.BOOLEAN),
new TypedDataSupplier("<false>", () -> false, DataTypes.BOOLEAN)
);
}
public static List<TypedDataSupplier> dateCases() {
return List.of(
new TypedDataSupplier("<1970-01-01T00:00:00Z>", () -> 0L, DataTypes.DATETIME),
new TypedDataSupplier(
"<date>",
() -> ESTestCase.randomLongBetween(0, 10 * (long) 10e11), // 1970-01-01T00:00:00Z - 2286-11-20T17:46:40Z
DataTypes.DATETIME
),
new TypedDataSupplier(
"<far future date>",
// 2286-11-20T17:46:40Z - +292278994-08-17T07:12:55.807Z
() -> ESTestCase.randomLongBetween(10 * (long) 10e11, Long.MAX_VALUE),
DataTypes.DATETIME
),
new TypedDataSupplier(
"<near the end of time>",
// very close to +292278994-08-17T07:12:55.807Z, the maximum supported millis since epoch