-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathTypeDescription.java
More file actions
1024 lines (940 loc) · 29.9 KB
/
TypeDescription.java
File metadata and controls
1024 lines (940 loc) · 29.9 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
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
/*
* 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.
*/
package org.apache.orc;
import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
import org.apache.orc.impl.ParserUtils;
import org.apache.orc.impl.TypeUtils;
import org.jetbrains.annotations.NotNull;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* This is the description of the types in an ORC file.
* @since 1.1.0
*/
public class TypeDescription
implements Comparable<TypeDescription>, Serializable, Cloneable {
private static final int MAX_PRECISION = 38;
private static final int MAX_SCALE = 38;
private static final int DEFAULT_PRECISION = 38;
private static final int DEFAULT_SCALE = 10;
public static final int MAX_DECIMAL64_PRECISION = 18;
public static final long MAX_DECIMAL64 = 999_999_999_999_999_999L;
public static final long MIN_DECIMAL64 = -MAX_DECIMAL64;
private static final int DEFAULT_LENGTH = 256;
private static final String DEFAULT_CRS = "OGC:CRS84";
static final Pattern UNQUOTED_NAMES = Pattern.compile("^[a-zA-Z0-9_]+$");
// type attributes
public static final String ENCRYPT_ATTRIBUTE = "encrypt";
public static final String MASK_ATTRIBUTE = "mask";
public enum EdgeInterpolationAlgorithm {
SPHERICAL("spherical"),
VINCENTY("vincenty"),
THOMAS("thomas"),
ANDOYER("andoyer"),
KARNEY("karney");
EdgeInterpolationAlgorithm(String name) {
this.name = name;
}
final String name;
}
private static final EdgeInterpolationAlgorithm DEFAULT_EDGE_INTERPOLATION_ALGORITHM
= EdgeInterpolationAlgorithm.SPHERICAL;
@Override
public int compareTo(TypeDescription other) {
if (this == other) {
return 0;
} else if (other == null) {
return -1;
} else {
int result = category.compareTo(other.category);
if (result == 0) {
switch (category) {
case CHAR:
case VARCHAR:
return maxLength - other.maxLength;
case DECIMAL:
if (precision != other.precision) {
return precision - other.precision;
}
return scale - other.scale;
case UNION:
case LIST:
case MAP:
if (children.size() != other.children.size()) {
return children.size() - other.children.size();
}
for(int c=0; result == 0 && c < children.size(); ++c) {
result = children.get(c).compareTo(other.children.get(c));
}
break;
case STRUCT:
if (children.size() != other.children.size()) {
return children.size() - other.children.size();
}
for(int c=0; result == 0 && c < children.size(); ++c) {
result = fieldNames.get(c).compareTo(other.fieldNames.get(c));
if (result == 0) {
result = children.get(c).compareTo(other.children.get(c));
}
}
break;
default:
// PASS
}
}
return result;
}
}
public enum Category {
BOOLEAN("boolean", true),
BYTE("tinyint", true),
SHORT("smallint", true),
INT("int", true),
LONG("bigint", true),
FLOAT("float", true),
DOUBLE("double", true),
STRING("string", true),
DATE("date", true),
TIMESTAMP("timestamp", true),
BINARY("binary", true),
DECIMAL("decimal", true),
VARCHAR("varchar", true),
CHAR("char", true),
LIST("array", false),
MAP("map", false),
STRUCT("struct", false),
UNION("uniontype", false),
TIMESTAMP_INSTANT("timestamp with local time zone", true),
Geometry("geometry", true),
Geography("geography", true);
Category(String name, boolean isPrimitive) {
this.name = name;
this.isPrimitive = isPrimitive;
}
final boolean isPrimitive;
final String name;
public boolean isPrimitive() {
return isPrimitive;
}
public String getName() {
return name;
}
}
public static TypeDescription createBoolean() {
return new TypeDescription(Category.BOOLEAN);
}
public static TypeDescription createByte() {
return new TypeDescription(Category.BYTE);
}
public static TypeDescription createShort() {
return new TypeDescription(Category.SHORT);
}
public static TypeDescription createInt() {
return new TypeDescription(Category.INT);
}
public static TypeDescription createLong() {
return new TypeDescription(Category.LONG);
}
public static TypeDescription createFloat() {
return new TypeDescription(Category.FLOAT);
}
public static TypeDescription createDouble() {
return new TypeDescription(Category.DOUBLE);
}
public static TypeDescription createString() {
return new TypeDescription(Category.STRING);
}
public static TypeDescription createDate() {
return new TypeDescription(Category.DATE);
}
public static TypeDescription createTimestamp() {
return new TypeDescription(Category.TIMESTAMP);
}
public static TypeDescription createTimestampInstant() {
return new TypeDescription(Category.TIMESTAMP_INSTANT);
}
public static TypeDescription createBinary() {
return new TypeDescription(Category.BINARY);
}
public static TypeDescription createDecimal() {
return new TypeDescription(Category.DECIMAL);
}
public static TypeDescription createGeometry() {
return new TypeDescription(Category.Geometry);
}
public static TypeDescription createGeography() {
return new TypeDescription(Category.Geography);
}
/**
* Parse TypeDescription from the Hive type names. This is the inverse
* of TypeDescription.toString()
* @param typeName the name of the type
* @return a new TypeDescription or null if typeName was null
* @throws IllegalArgumentException if the string is badly formed
*/
public static TypeDescription fromString(String typeName) {
if (typeName == null) {
return null;
}
ParserUtils.StringPosition source = new ParserUtils.StringPosition(typeName);
TypeDescription result = ParserUtils.parseType(source);
if (source.hasCharactersLeft()) {
throw new IllegalArgumentException("Extra characters at " + source);
}
return result;
}
/**
* For decimal types, set the precision.
* @param precision the new precision
* @return this
*/
public TypeDescription withPrecision(int precision) {
if (category != Category.DECIMAL) {
throw new IllegalArgumentException("precision is only allowed on decimal"+
" and not " + category.name);
} else if (precision < 1 || precision > MAX_PRECISION) {
throw new IllegalArgumentException(
"precision " + precision + " must be between 1 and " + MAX_PRECISION
);
} else if (scale > precision) {
throw new IllegalArgumentException("scale " + scale +
" must be less than or equal to precision " + precision);
}
this.precision = precision;
return this;
}
/**
* For decimal types, set the scale.
* @param scale the new scale
* @return this
*/
public TypeDescription withScale(int scale) {
if (category != Category.DECIMAL) {
throw new IllegalArgumentException("scale is only allowed on decimal"+
" and not " + category.name);
} else if (scale < 0 || scale > MAX_SCALE || scale > precision) {
throw new IllegalArgumentException("scale is out of range at " + scale);
}
this.scale = scale;
return this;
}
public TypeDescription withCRS(String crs) {
if (category != Category.Geometry &&
category != Category.Geography) {
throw new IllegalArgumentException("crs is only allowed on Geometry/Geography" +
" and not " + category.name);
}
this.crs = crs;
return this;
}
public TypeDescription withEdgeInterpolationAlgorithm(
EdgeInterpolationAlgorithm edgeInterpolationAlgorithm) {
if (category != Category.Geography) {
throw new IllegalArgumentException("edgeInterpolationAlgorithm is only allowed on Geography" +
" and not " + category.name);
}
this.edgeInterpolationAlgorithm = edgeInterpolationAlgorithm;
return this;
}
/**
* Set an attribute on this type.
* @param key the attribute name
* @param value the attribute value or null to clear the value
* @return this for method chaining
*/
public TypeDescription setAttribute(@NotNull String key,
String value) {
if (value == null) {
attributes.remove(key);
} else {
attributes.put(key, value);
}
return this;
}
/**
* Remove attribute on this type, if it is set.
* @param key the attribute name
* @return this for method chaining
*/
public TypeDescription removeAttribute(@NotNull String key) {
attributes.remove(key);
return this;
}
public static TypeDescription createVarchar() {
return new TypeDescription(Category.VARCHAR);
}
public static TypeDescription createChar() {
return new TypeDescription(Category.CHAR);
}
/**
* Set the maximum length for char and varchar types.
* @param maxLength the maximum value
* @return this
*/
public TypeDescription withMaxLength(int maxLength) {
if (category != Category.VARCHAR && category != Category.CHAR) {
throw new IllegalArgumentException("maxLength is only allowed on char" +
" and varchar and not " + category.name);
}
this.maxLength = maxLength;
return this;
}
public static TypeDescription createList(TypeDescription childType) {
TypeDescription result = new TypeDescription(Category.LIST);
result.children.add(childType);
childType.parent = result;
return result;
}
public static TypeDescription createMap(TypeDescription keyType,
TypeDescription valueType) {
TypeDescription result = new TypeDescription(Category.MAP);
result.children.add(keyType);
result.children.add(valueType);
keyType.parent = result;
valueType.parent = result;
return result;
}
public static TypeDescription createUnion() {
return new TypeDescription(Category.UNION);
}
public static TypeDescription createStruct() {
return new TypeDescription(Category.STRUCT);
}
/**
* Add a child to a union type.
* @param child a new child type to add
* @return the union type.
*/
public TypeDescription addUnionChild(TypeDescription child) {
if (category != Category.UNION) {
throw new IllegalArgumentException("Can only add types to union type" +
" and not " + category);
}
addChild(child);
return this;
}
/**
* Add a field to a struct type as it is built.
* @param field the field name
* @param fieldType the type of the field
* @return the struct type
*/
public TypeDescription addField(String field, TypeDescription fieldType) {
if (category != Category.STRUCT) {
throw new IllegalArgumentException("Can only add fields to struct type" +
" and not " + category);
}
fieldNames.add(field);
addChild(fieldType);
return this;
}
/**
* Get the id for this type.
* The first call will cause all of the the ids in tree to be assigned, so
* it should not be called before the type is completely built.
* @return the sequential id
*/
public int getId() {
// if the id hasn't been assigned, assign all of the ids from the root
if (id == -1) {
TypeDescription root = this;
while (root.parent != null) {
root = root.parent;
}
root.assignIds(0);
}
return id;
}
@Override
public TypeDescription clone() {
TypeDescription result = new TypeDescription(category);
result.maxLength = maxLength;
result.precision = precision;
result.scale = scale;
result.crs = crs;
result.edgeInterpolationAlgorithm = edgeInterpolationAlgorithm;
if (fieldNames != null) {
result.fieldNames.addAll(fieldNames);
}
if (children != null) {
for(TypeDescription child: children) {
TypeDescription clone = child.clone();
clone.parent = result;
result.children.add(clone);
}
}
for (Map.Entry<String,String> pair: attributes.entrySet()) {
result.attributes.put(pair.getKey(), pair.getValue());
}
return result;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + category.hashCode();
if (children != null) {
result = prime * result + children.hashCode();
}
result = prime * result + maxLength;
result = prime * result + precision;
result = prime * result + scale;
return result;
}
@Override
public boolean equals(Object other) {
return equals(other, true);
}
/**
* Determines whether the two object are equal.
* This function can either compare or ignore the type attributes as
* desired.
* @param other the reference object with which to compare.
* @param checkAttributes should the type attributes be considered?
* @return {@code true} if this object is the same as the other
* argument; {@code false} otherwise.
*/
public boolean equals(Object other, boolean checkAttributes) {
if (other == null || !(other instanceof TypeDescription castOther)) {
return false;
}
if (other == this) {
return true;
}
if (category != castOther.category ||
maxLength != castOther.maxLength ||
scale != castOther.scale ||
precision != castOther.precision) {
return false;
}
if (checkAttributes) {
// make sure the attributes are the same
List<String> attributeNames = getAttributeNames();
if (castOther.getAttributeNames().size() != attributeNames.size()) {
return false;
}
for (String attribute : attributeNames) {
if (!getAttributeValue(attribute).equals(castOther.getAttributeValue(attribute))) {
return false;
}
}
}
// check the children
if (children != null) {
if (children.size() != castOther.children.size()) {
return false;
}
for (int i = 0; i < children.size(); ++i) {
if (!children.get(i).equals(castOther.children.get(i), checkAttributes)) {
return false;
}
}
}
if (category == Category.STRUCT) {
for(int i=0; i < fieldNames.size(); ++i) {
if (!fieldNames.get(i).equals(castOther.fieldNames.get(i))) {
return false;
}
}
}
return true;
}
/**
* Get the maximum id assigned to this type or its children.
* The first call will cause all of the the ids in tree to be assigned, so
* it should not be called before the type is completely built.
* @return the maximum id assigned under this type
*/
public int getMaximumId() {
// if the id hasn't been assigned, assign all of the ids from the root
if (maxId == -1) {
TypeDescription root = this;
while (root.parent != null) {
root = root.parent;
}
root.assignIds(0);
}
return maxId;
}
/**
* Specify the version of the VectorizedRowBatch that the user desires.
*/
public enum RowBatchVersion {
ORIGINAL,
USE_DECIMAL64;
}
public VectorizedRowBatch createRowBatch(RowBatchVersion version, int size) {
VectorizedRowBatch result;
if (category == Category.STRUCT) {
result = new VectorizedRowBatch(children.size(), size);
for(int i=0; i < result.cols.length; ++i) {
result.cols[i] = TypeUtils.createColumn(children.get(i), version, size);
}
} else {
result = new VectorizedRowBatch(1, size);
result.cols[0] = TypeUtils.createColumn(this, version, size);
}
result.reset();
return result;
}
/**
* Create a VectorizedRowBatch that uses Decimal64ColumnVector for
* short (p ≤ 18) decimals.
* @return a new VectorizedRowBatch
*/
public VectorizedRowBatch createRowBatchV2() {
return createRowBatch(RowBatchVersion.USE_DECIMAL64,
VectorizedRowBatch.DEFAULT_SIZE);
}
/**
* Create a VectorizedRowBatch with the original ColumnVector types
* @param maxSize the maximum size of the batch
* @return a new VectorizedRowBatch
*/
public VectorizedRowBatch createRowBatch(int maxSize) {
return createRowBatch(RowBatchVersion.ORIGINAL, maxSize);
}
/**
* Create a VectorizedRowBatch with the original ColumnVector types
* @return a new VectorizedRowBatch
*/
public VectorizedRowBatch createRowBatch() {
return createRowBatch(RowBatchVersion.ORIGINAL,
VectorizedRowBatch.DEFAULT_SIZE);
}
/**
* Get the kind of this type.
* @return get the category for this type.
*/
public Category getCategory() {
return category;
}
/**
* Get the maximum length of the type. Only used for char and varchar types.
* @return the maximum length of the string type
*/
public int getMaxLength() {
return maxLength;
}
/**
* Get the precision of the decimal type.
* @return the number of digits for the precision.
*/
public int getPrecision() {
return precision;
}
/**
* Get the scale of the decimal type.
* @return the number of digits for the scale.
*/
public int getScale() {
return scale;
}
public String getCrs() {
return crs;
}
public EdgeInterpolationAlgorithm getEdgeInterpolationAlgorithm() {
return edgeInterpolationAlgorithm;
}
/**
* For struct types, get the list of field names.
* @return the list of field names.
*/
public List<String> getFieldNames() {
return Collections.unmodifiableList(fieldNames);
}
/**
* Get the list of attribute names defined on this type.
* @return a list of sorted attribute names
*/
public List<String> getAttributeNames() {
List<String> result = new ArrayList<>(attributes.keySet());
Collections.sort(result);
return result;
}
/**
* Get the value of a given attribute.
* @param attributeName the name of the attribute
* @return the value of the attribute or null if it isn't set
*/
public String getAttributeValue(String attributeName) {
return attributes.get(attributeName);
}
/**
* Get the parent of the current type
* @return null if root else parent
*/
public TypeDescription getParent() {
return parent;
}
/**
* Get the subtypes of this type.
* @return the list of children types
*/
public List<TypeDescription> getChildren() {
return children == null ? null : Collections.unmodifiableList(children);
}
/**
* Assign ids to all of the nodes under this one.
* @param startId the lowest id to assign
* @return the next available id
*/
private int assignIds(int startId) {
id = startId++;
if (children != null) {
for (TypeDescription child : children) {
startId = child.assignIds(startId);
}
}
maxId = startId - 1;
return startId;
}
/**
* Add a child to a type.
* @param child the child to add
*/
public void addChild(TypeDescription child) {
switch (category) {
case LIST:
if (children.size() >= 1) {
throw new IllegalArgumentException("Can't add more children to list");
}
case MAP:
if (children.size() >= 2) {
throw new IllegalArgumentException("Can't add more children to map");
}
case UNION:
case STRUCT:
children.add(child);
child.parent = this;
break;
default:
throw new IllegalArgumentException("Can't add children to " + category);
}
}
public TypeDescription(Category category) {
this.category = category;
if (category.isPrimitive) {
children = null;
} else {
children = new ArrayList<>();
}
if (category == Category.STRUCT) {
fieldNames = new ArrayList<>();
} else {
fieldNames = null;
}
}
private int id = -1;
private int maxId = -1;
private TypeDescription parent;
private final Category category;
private final List<TypeDescription> children;
private final List<String> fieldNames;
private final Map<String,String> attributes = new HashMap<>();
private int maxLength = DEFAULT_LENGTH;
private int precision = DEFAULT_PRECISION;
private int scale = DEFAULT_SCALE;
private String crs = DEFAULT_CRS;
private EdgeInterpolationAlgorithm edgeInterpolationAlgorithm
= DEFAULT_EDGE_INTERPOLATION_ALGORITHM;
static void printFieldName(StringBuilder buffer, String name) {
if (UNQUOTED_NAMES.matcher(name).matches()) {
buffer.append(name);
} else {
buffer.append('`');
buffer.append(name.replace("`", "``"));
buffer.append('`');
}
}
public void printToBuffer(StringBuilder buffer) {
buffer.append(category.name);
switch (category) {
case DECIMAL:
buffer.append('(');
buffer.append(precision);
buffer.append(',');
buffer.append(scale);
buffer.append(')');
break;
case CHAR:
case VARCHAR:
buffer.append('(');
buffer.append(maxLength);
buffer.append(')');
break;
case Geometry:
buffer.append('(');
buffer.append(crs);
buffer.append(')');
break;
case Geography:
buffer.append('(');
buffer.append(crs);
buffer.append(',');
buffer.append(edgeInterpolationAlgorithm.name());
buffer.append(')');
break;
case LIST:
case MAP:
case UNION:
buffer.append('<');
for(int i=0; i < children.size(); ++i) {
if (i != 0) {
buffer.append(',');
}
children.get(i).printToBuffer(buffer);
}
buffer.append('>');
break;
case STRUCT:
buffer.append('<');
for(int i=0; i < children.size(); ++i) {
if (i != 0) {
buffer.append(',');
}
printFieldName(buffer, fieldNames.get(i));
buffer.append(':');
children.get(i).printToBuffer(buffer);
}
buffer.append('>');
break;
default:
break;
}
}
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
printToBuffer(buffer);
return buffer.toString();
}
private void printJsonToBuffer(String prefix, StringBuilder buffer,
int indent) {
for(int i=0; i < indent; ++i) {
buffer.append(' ');
}
buffer.append(prefix);
buffer.append("{\"category\": \"");
buffer.append(category.name);
buffer.append("\", \"id\": ");
buffer.append(getId());
buffer.append(", \"max\": ");
buffer.append(maxId);
switch (category) {
case DECIMAL:
buffer.append(", \"precision\": ");
buffer.append(precision);
buffer.append(", \"scale\": ");
buffer.append(scale);
break;
case CHAR:
case VARCHAR:
buffer.append(", \"length\": ");
buffer.append(maxLength);
break;
case Geometry:
buffer.append(", \"crs\": ");
buffer.append(crs);
break;
case Geography:
buffer.append(", \"crs\": ");
buffer.append(crs);
buffer.append(", \"edge_interpolation_algorithm\": ");
buffer.append(edgeInterpolationAlgorithm.name());
break;
case LIST:
case MAP:
case UNION:
buffer.append(", \"children\": [");
for(int i=0; i < children.size(); ++i) {
buffer.append('\n');
children.get(i).printJsonToBuffer("", buffer, indent + 2);
if (i != children.size() - 1) {
buffer.append(',');
}
}
buffer.append("]");
break;
case STRUCT:
buffer.append(", \"fields\": [");
for(int i=0; i < children.size(); ++i) {
buffer.append('\n');
buffer.append('{');
children.get(i).printJsonToBuffer("\"" + fieldNames.get(i) + "\": ",
buffer, indent + 2);
buffer.append('}');
if (i != children.size() - 1) {
buffer.append(',');
}
}
buffer.append(']');
break;
default:
break;
}
buffer.append('}');
}
public String toJson() {
StringBuilder buffer = new StringBuilder();
printJsonToBuffer("", buffer, 0);
return buffer.toString();
}
/**
* Locate a subtype by its id.
* @param goal the column id to look for
* @return the subtype
*/
public TypeDescription findSubtype(int goal) {
ParserUtils.TypeFinder result = new ParserUtils.TypeFinder(this);
ParserUtils.findSubtype(this, goal, result);
return result.current;
}
/**
* Find a subtype of this schema by name.
* If the name is a simple integer, it will be used as a column number.
* Otherwise, this routine will recursively search for the name.
* <ul>
* <li>Struct fields are selected by name.</li>
* <li>List children are selected by "_elem".</li>
* <li>Map children are selected by "_key" or "_value".</li>
* <li>Union children are selected by number starting at 0.</li>
* </ul>
* Names are separated by '.'.
* @param columnName the name to search for
* @return the subtype
*/
public TypeDescription findSubtype(String columnName) {
return findSubtype(columnName, true);
}
public TypeDescription findSubtype(String columnName,
boolean isSchemaEvolutionCaseAware) {
ParserUtils.StringPosition source = new ParserUtils.StringPosition(columnName);
TypeDescription result = ParserUtils.findSubtype(this, source,
isSchemaEvolutionCaseAware);
if (source.hasCharactersLeft()) {
throw new IllegalArgumentException("Remaining text in parsing field name "
+ source);
}
return result;
}
/**
* Find a list of subtypes from a string, including the empty list.
*
* Each column name is separated by ','.
* @param columnNameList the list of column names
* @return the list of subtypes that correspond to the column names
*/
public List<TypeDescription> findSubtypes(String columnNameList) {
ParserUtils.StringPosition source = new ParserUtils.StringPosition(columnNameList);
List<TypeDescription> result = ParserUtils.findSubtypeList(this, source);
if (source.hasCharactersLeft()) {
throw new IllegalArgumentException("Remaining text in parsing field name "
+ source);
}
return result;
}
/**
* Annotate a schema with the encryption keys and masks.
* @param encryption the encryption keys and the fields
* @param masks the encryption masks and the fields
*/
public void annotateEncryption(String encryption, String masks) {
ParserUtils.StringPosition source = new ParserUtils.StringPosition(encryption);
ParserUtils.parseKeys(source, this);
if (source.hasCharactersLeft()) {
throw new IllegalArgumentException("Remaining text in parsing encryption keys "
+ source);
}
source = new ParserUtils.StringPosition(masks);
ParserUtils.parseMasks(source, this);
if (source.hasCharactersLeft()) {
throw new IllegalArgumentException("Remaining text in parsing encryption masks "
+ source);
}
}
/**
* Find the index of a given child object using == comparison.
* @param child The child type
* @return the index 0 to N-1 of the children.
*/
private int getChildIndex(TypeDescription child) {
for(int i=children.size() - 1; i >= 0; --i) {
if (children.get(i) == child) {
return i;
}
}
throw new IllegalArgumentException("Child not found");
}
/**
* For a complex type, get the partial name for this child. For structures,
* it returns the corresponding field name. For lists and maps, it uses the
* special names "_elem", "_key", and "_value". Unions use the integer index.
* @param child The desired child, which must be the same object (==)
* @return The name of the field for the given child.
*/
private String getPartialName(TypeDescription child) {
switch (category) {
case LIST:
return "_elem";
case MAP:
return getChildIndex(child) == 0 ? "_key" : "_value";
case STRUCT:
return fieldNames.get(getChildIndex(child));
case UNION:
return Integer.toString(getChildIndex(child));
default:
throw new IllegalArgumentException(
"Can't get the field name of a primitive type");
}
}
/**
* Get the full field name for the given type. For
* "struct<a:struct<list<struct<b:int,c:int>>>>" when
* called on c, would return "a._elem.c".
* @return A string that is the inverse of findSubtype
*/