forked from sqlancer/sqlancer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClickHouseType.java
More file actions
939 lines (790 loc) · 24.6 KB
/
Copy pathClickHouseType.java
File metadata and controls
939 lines (790 loc) · 24.6 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
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
package sqlancer.clickhouse;
import java.util.Objects;
import java.util.Optional;
import com.clickhouse.data.ClickHouseDataType;
public sealed interface ClickHouseType permits ClickHouseType.Primitive, ClickHouseType.FixedString, ClickHouseType.Decimal, ClickHouseType.DateTime64Type, ClickHouseType.Array, ClickHouseType.Tuple, ClickHouseType.Map, ClickHouseType.Enum, ClickHouseType.Time, ClickHouseType.Time64, ClickHouseType.Point, ClickHouseType.Ring, ClickHouseType.Polygon, ClickHouseType.MultiPolygon, ClickHouseType.Nested, ClickHouseType.JSON, ClickHouseType.Variant, ClickHouseType.Dynamic, ClickHouseType.IntervalType, ClickHouseType.AggregateFunctionType, ClickHouseType.SimpleAggregateFunctionType, ClickHouseType.Nullable, ClickHouseType.LowCardinality, ClickHouseType.Unknown {
boolean isNumeric();
boolean supportsLiteralEmission();
boolean hasNullSemantics();
enum Kind {
Int8, Int16, Int32, Int64, Int128, Int256, UInt8, UInt16, UInt32, UInt64, UInt128, UInt256, Float32, Float64,
Bool, String, UUID, Date, Date32, DateTime, IPv4, IPv6;
public ClickHouseDataType toClickHouseDataType() {
switch (this) {
case Int8:
return ClickHouseDataType.Int8;
case Int16:
return ClickHouseDataType.Int16;
case Int32:
return ClickHouseDataType.Int32;
case Int64:
return ClickHouseDataType.Int64;
case Int128:
return ClickHouseDataType.Int128;
case Int256:
return ClickHouseDataType.Int256;
case UInt8:
return ClickHouseDataType.UInt8;
case UInt16:
return ClickHouseDataType.UInt16;
case UInt32:
return ClickHouseDataType.UInt32;
case UInt64:
return ClickHouseDataType.UInt64;
case UInt128:
return ClickHouseDataType.UInt128;
case UInt256:
return ClickHouseDataType.UInt256;
case Float32:
return ClickHouseDataType.Float32;
case Float64:
return ClickHouseDataType.Float64;
case Bool:
return ClickHouseDataType.Bool;
case String:
return ClickHouseDataType.String;
case UUID:
return ClickHouseDataType.UUID;
case Date:
return ClickHouseDataType.Date;
case Date32:
return ClickHouseDataType.Date32;
case DateTime:
return ClickHouseDataType.DateTime;
case IPv4:
return ClickHouseDataType.IPv4;
case IPv6:
return ClickHouseDataType.IPv6;
default:
throw new AssertionError(this);
}
}
public static Optional<Kind> fromClickHouseDataType(ClickHouseDataType type) {
if (type == null) {
return Optional.empty();
}
switch (type) {
case Int8:
return Optional.of(Int8);
case Int16:
return Optional.of(Int16);
case Int32:
return Optional.of(Int32);
case Int64:
return Optional.of(Int64);
case Int128:
return Optional.of(Int128);
case Int256:
return Optional.of(Int256);
case UInt8:
return Optional.of(UInt8);
case UInt16:
return Optional.of(UInt16);
case UInt32:
return Optional.of(UInt32);
case UInt64:
return Optional.of(UInt64);
case UInt128:
return Optional.of(UInt128);
case UInt256:
return Optional.of(UInt256);
case Float32:
return Optional.of(Float32);
case Float64:
return Optional.of(Float64);
case Bool:
return Optional.of(Bool);
case String:
return Optional.of(String);
case UUID:
return Optional.of(UUID);
case Date:
return Optional.of(Date);
case Date32:
return Optional.of(Date32);
case DateTime:
case DateTime32:
return Optional.of(DateTime);
case IPv4:
return Optional.of(IPv4);
case IPv6:
return Optional.of(IPv6);
default:
return Optional.empty();
}
}
}
default ClickHouseType unwrap() {
if (this instanceof Nullable n) {
return n.inner().unwrap();
}
if (this instanceof LowCardinality lc) {
return lc.inner().unwrap();
}
return this;
}
record Primitive(Kind kind) implements ClickHouseType {
public Primitive {
Objects.requireNonNull(kind, "kind");
}
@Override
public boolean isNumeric() {
switch (kind) {
case Int8:
case Int16:
case Int32:
case Int64:
case Int128:
case Int256:
case UInt8:
case UInt16:
case UInt32:
case UInt64:
case UInt128:
case UInt256:
case Float32:
case Float64:
return true;
default:
return false;
}
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return kind.name();
}
}
record FixedString(int length) implements ClickHouseType {
public FixedString {
if (length < 1 || length > 256) {
throw new IllegalArgumentException("FixedString length out of range: " + length);
}
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "FixedString(" + length + ")";
}
}
record Decimal(int precision, int scale) implements ClickHouseType {
public Decimal {
if (precision < 1 || precision > 76 || scale < 0 || scale > precision) {
throw new IllegalArgumentException("Decimal out of range: P=" + precision + " S=" + scale);
}
}
@Override
public boolean isNumeric() {
return true;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "Decimal(" + precision + ", " + scale + ")";
}
}
record DateTime64Type(int precision) implements ClickHouseType {
public DateTime64Type {
if (precision < 0 || precision > 9) {
throw new IllegalArgumentException("DateTime64 precision out of range: " + precision);
}
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "DateTime64(" + precision + ")";
}
}
record Array(ClickHouseType inner) implements ClickHouseType {
public Array {
Objects.requireNonNull(inner, "inner");
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return inner.supportsLiteralEmission();
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "Array(" + inner + ")";
}
public static boolean canWrap(ClickHouseType type) {
if (type instanceof Array || type instanceof Unknown) {
return false;
}
return type.supportsLiteralEmission();
}
}
record Tuple(java.util.List<ClickHouseType> elements) implements ClickHouseType {
public Tuple {
Objects.requireNonNull(elements, "elements");
if (elements.isEmpty() || elements.size() > 8) {
throw new IllegalArgumentException("Tuple arity out of range: " + elements.size());
}
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
for (ClickHouseType e : elements) {
if (!e.supportsLiteralEmission()) {
return false;
}
}
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Tuple(");
for (int i = 0; i < elements.size(); i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(elements.get(i));
}
sb.append(")");
return sb.toString();
}
}
record Enum(int width, java.util.List<EnumEntry> entries) implements ClickHouseType {
public Enum {
if (width != 8 && width != 16) {
throw new IllegalArgumentException("Enum width must be 8 or 16, got " + width);
}
Objects.requireNonNull(entries, "entries");
if (entries.isEmpty() || entries.size() > 16) {
throw new IllegalArgumentException("Enum entry count out of range: " + entries.size());
}
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Enum");
sb.append(width).append("(");
for (int i = 0; i < entries.size(); i++) {
if (i > 0) {
sb.append(", ");
}
EnumEntry e = entries.get(i);
sb.append("'").append(e.name()).append("' = ").append(e.value());
}
sb.append(")");
return sb.toString();
}
}
record Time() implements ClickHouseType {
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "Time";
}
}
record Time64(int precision) implements ClickHouseType {
public Time64 {
if (precision < 0 || precision > 9) {
throw new IllegalArgumentException("Time64 precision out of range: " + precision);
}
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "Time64(" + precision + ")";
}
}
record Map(ClickHouseType keyType, ClickHouseType valueType) implements ClickHouseType {
public Map {
Objects.requireNonNull(keyType, "keyType");
Objects.requireNonNull(valueType, "valueType");
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return keyType.supportsLiteralEmission() && valueType.supportsLiteralEmission();
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "Map(" + keyType + ", " + valueType + ")";
}
public static boolean isValidKey(ClickHouseType t) {
ClickHouseType u = t.unwrap();
if (u instanceof Primitive p) {
switch (p.kind()) {
case Int8:
case Int16:
case Int32:
case Int64:
case UInt8:
case UInt16:
case UInt32:
case UInt64:
case String:
case UUID:
case Date:
case DateTime:
return true;
default:
return false;
}
}
return u instanceof FixedString;
}
}
record Point() implements ClickHouseType {
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "Point";
}
}
record Ring() implements ClickHouseType {
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "Ring";
}
}
record Polygon() implements ClickHouseType {
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "Polygon";
}
}
record MultiPolygon() implements ClickHouseType {
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "MultiPolygon";
}
}
record Nested(java.util.List<NestedField> fields) implements ClickHouseType {
public Nested {
Objects.requireNonNull(fields, "fields");
if (fields.isEmpty() || fields.size() > 6) {
throw new IllegalArgumentException("Nested field count out of range: " + fields.size());
}
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return false;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Nested(");
for (int i = 0; i < fields.size(); i++) {
if (i > 0) {
sb.append(", ");
}
NestedField f = fields.get(i);
sb.append(f.name()).append(" ").append(f.type());
}
sb.append(")");
return sb.toString();
}
}
record NestedField(String name, ClickHouseType type) {
public NestedField {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(type, "type");
}
}
record JSON() implements ClickHouseType {
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "JSON";
}
}
record Variant(java.util.List<ClickHouseType> alternatives) implements ClickHouseType {
public Variant {
Objects.requireNonNull(alternatives, "alternatives");
if (alternatives.isEmpty() || alternatives.size() > 6) {
throw new IllegalArgumentException("Variant alternative count out of range: " + alternatives.size());
}
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("Variant(");
for (int i = 0; i < alternatives.size(); i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(alternatives.get(i));
}
sb.append(")");
return sb.toString();
}
}
record Dynamic() implements ClickHouseType {
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "Dynamic";
}
}
enum IntervalKind {
Nanosecond, Microsecond, Millisecond, Second, Minute, Hour, Day, Week, Month, Quarter, Year
}
record IntervalType(IntervalKind kind) implements ClickHouseType {
public IntervalType {
Objects.requireNonNull(kind, "kind");
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return true;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "Interval" + kind.name();
}
}
record AggregateFunctionType(String functionName, java.util.List<ClickHouseType> args) implements ClickHouseType {
public AggregateFunctionType {
Objects.requireNonNull(functionName, "functionName");
Objects.requireNonNull(args, "args");
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return false;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("AggregateFunction(");
sb.append(functionName);
for (ClickHouseType a : args) {
sb.append(", ").append(a);
}
sb.append(")");
return sb.toString();
}
}
record SimpleAggregateFunctionType(String functionName, ClickHouseType arg) implements ClickHouseType {
public SimpleAggregateFunctionType {
Objects.requireNonNull(functionName, "functionName");
Objects.requireNonNull(arg, "arg");
}
@Override
public boolean isNumeric() {
return arg.isNumeric();
}
@Override
public boolean supportsLiteralEmission() {
return arg.supportsLiteralEmission();
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "SimpleAggregateFunction(" + functionName + ", " + arg + ")";
}
}
record EnumEntry(String name, int value) {
public EnumEntry {
Objects.requireNonNull(name, "name");
if (name.isEmpty() || name.length() > 32) {
throw new IllegalArgumentException("Enum entry name length out of range: " + name.length());
}
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (c == '\'' || c == '\\' || c < 0x20) {
throw new IllegalArgumentException("Enum entry name contains forbidden character: " + name);
}
}
}
}
record Nullable(ClickHouseType inner) implements ClickHouseType {
public Nullable {
Objects.requireNonNull(inner, "inner");
}
@Override
public boolean isNumeric() {
return inner.isNumeric();
}
@Override
public boolean supportsLiteralEmission() {
return inner.supportsLiteralEmission();
}
@Override
public boolean hasNullSemantics() {
return true;
}
@Override
public String toString() {
return "Nullable(" + inner + ")";
}
public static boolean canWrap(ClickHouseType type) {
return type instanceof Primitive || type instanceof FixedString || type instanceof Decimal
|| type instanceof DateTime64Type || type instanceof Enum || type instanceof Time
|| type instanceof Time64;
}
}
record LowCardinality(ClickHouseType inner) implements ClickHouseType {
public LowCardinality {
Objects.requireNonNull(inner, "inner");
}
@Override
public boolean isNumeric() {
return inner.isNumeric();
}
@Override
public boolean supportsLiteralEmission() {
return inner.supportsLiteralEmission();
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return "LowCardinality(" + inner + ")";
}
public static boolean canWrap(ClickHouseType type) {
if (type instanceof Nullable n) {
return canWrap(n.inner());
}
if (type instanceof FixedString) {
return true;
}
if (type instanceof Primitive p) {
switch (p.kind()) {
case Int8:
case Int16:
case Int32:
case Int64:
case Int128:
case Int256:
case UInt8:
case UInt16:
case UInt32:
case UInt64:
case UInt128:
case UInt256:
case Float32:
case Float64:
case String:
case Date:
case Date32:
case DateTime:
return true;
default:
return false;
}
}
return false;
}
}
record Unknown(String raw) implements ClickHouseType {
public Unknown {
Objects.requireNonNull(raw, "raw");
}
@Override
public boolean isNumeric() {
return false;
}
@Override
public boolean supportsLiteralEmission() {
return false;
}
@Override
public boolean hasNullSemantics() {
return false;
}
@Override
public String toString() {
return raw;
}
}
}