/
Metadata.java
1740 lines (1450 loc) · 56 KB
/
Metadata.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
/**
* 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
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* 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.drill.exec.store.parquet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import org.apache.drill.common.expression.SchemaPath;
import org.apache.drill.common.util.DrillVersionInfo;
import org.apache.drill.exec.store.AbstractRecordReader;
import org.apache.drill.exec.store.TimedRunnable;
import org.apache.drill.exec.store.dfs.DrillPathFilter;
import org.apache.drill.exec.store.dfs.MetadataContext;
import org.apache.drill.exec.util.ImpersonationUtil;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.parquet.column.statistics.Statistics;
import org.apache.parquet.hadoop.ParquetFileReader;
import org.apache.parquet.hadoop.metadata.BlockMetaData;
import org.apache.parquet.hadoop.metadata.ColumnChunkMetaData;
import org.apache.parquet.hadoop.metadata.ParquetMetadata;
import org.apache.parquet.io.api.Binary;
import org.apache.parquet.schema.GroupType;
import org.apache.parquet.schema.MessageType;
import org.apache.parquet.schema.OriginalType;
import org.apache.parquet.schema.PrimitiveType;
import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName;
import org.apache.parquet.schema.Type;
import org.apache.commons.lang3.tuple.Pair;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonGenerator.Feature;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.KeyDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
public class Metadata {
static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Metadata.class);
public static final String[] OLD_METADATA_FILENAMES = {".drill.parquet_metadata.v2"};
public static final String METADATA_FILENAME = ".drill.parquet_metadata";
public static final String METADATA_DIRECTORIES_FILENAME = ".drill.parquet_metadata_directories";
private final FileSystem fs;
private final ParquetFormatConfig formatConfig;
private ParquetTableMetadataBase parquetTableMetadata;
private ParquetTableMetadataDirs parquetTableMetadataDirs;
/**
* Create the parquet metadata file for the directory at the given path, and for any subdirectories
*
* @param fs
* @param path
* @throws IOException
*/
public static void createMeta(FileSystem fs, String path, ParquetFormatConfig formatConfig) throws IOException {
Metadata metadata = new Metadata(fs, formatConfig);
metadata.createMetaFilesRecursively(path);
}
/**
* Get the parquet metadata for the parquet files in the given directory, including those in subdirectories
*
* @param fs
* @param path
* @return
* @throws IOException
*/
public static ParquetTableMetadata_v3 getParquetTableMetadata(FileSystem fs, String path, ParquetFormatConfig formatConfig)
throws IOException {
Metadata metadata = new Metadata(fs, formatConfig);
return metadata.getParquetTableMetadata(path);
}
/**
* Get the parquet metadata for a list of parquet files
*
* @param fs
* @param fileStatuses
* @return
* @throws IOException
*/
public static ParquetTableMetadata_v3 getParquetTableMetadata(FileSystem fs,
List<FileStatus> fileStatuses, ParquetFormatConfig formatConfig) throws IOException {
Metadata metadata = new Metadata(fs, formatConfig);
return metadata.getParquetTableMetadata(fileStatuses);
}
/**
* Get the parquet metadata for a directory by reading the metadata file
*
* @param fs
* @param path The path to the metadata file, located in the directory that contains the parquet files
* @return
* @throws IOException
*/
public static ParquetTableMetadataBase readBlockMeta(FileSystem fs, String path, MetadataContext metaContext, ParquetFormatConfig formatConfig) throws IOException {
Metadata metadata = new Metadata(fs, formatConfig);
metadata.readBlockMeta(path, false, metaContext);
return metadata.parquetTableMetadata;
}
public static ParquetTableMetadataDirs readMetadataDirs(FileSystem fs, String path, MetadataContext metaContext, ParquetFormatConfig formatConfig) throws IOException {
Metadata metadata = new Metadata(fs, formatConfig);
metadata.readBlockMeta(path, true, metaContext);
return metadata.parquetTableMetadataDirs;
}
private Metadata(FileSystem fs, ParquetFormatConfig formatConfig) {
this.fs = ImpersonationUtil.createFileSystem(ImpersonationUtil.getProcessUserName(), fs.getConf());
this.formatConfig = formatConfig;
}
/**
* Create the parquet metadata file for the directory at the given path, and for any subdirectories
*
* @param path
* @throws IOException
*/
private Pair<ParquetTableMetadata_v3, ParquetTableMetadataDirs>
createMetaFilesRecursively(final String path) throws IOException {
Stopwatch timer = Stopwatch.createStarted();
List<ParquetFileMetadata_v3> metaDataList = Lists.newArrayList();
List<String> directoryList = Lists.newArrayList();
ConcurrentHashMap<ColumnTypeMetadata_v3.Key, ColumnTypeMetadata_v3> columnTypeInfoSet =
new ConcurrentHashMap<>();
Path p = new Path(path);
FileStatus fileStatus = fs.getFileStatus(p);
assert fileStatus.isDirectory() : "Expected directory";
final List<FileStatus> childFiles = Lists.newArrayList();
for (final FileStatus file : fs.listStatus(p, new DrillPathFilter())) {
if (file.isDirectory()) {
ParquetTableMetadata_v3 subTableMetadata = (createMetaFilesRecursively(file.getPath().toString())).getLeft();
metaDataList.addAll(subTableMetadata.files);
directoryList.addAll(subTableMetadata.directories);
directoryList.add(file.getPath().toString());
// Merge the schema from the child level into the current level
//TODO: We need a merge method that merges two colums with the same name but different types
columnTypeInfoSet.putAll(subTableMetadata.columnTypeInfo);
} else {
childFiles.add(file);
}
}
ParquetTableMetadata_v3 parquetTableMetadata = new ParquetTableMetadata_v3(DrillVersionInfo.getVersion());
if (childFiles.size() > 0) {
List<ParquetFileMetadata_v3 > childFilesMetadata =
getParquetFileMetadata_v3(parquetTableMetadata, childFiles);
metaDataList.addAll(childFilesMetadata);
// Note that we do not need to merge the columnInfo at this point. The columnInfo is already added
// to the parquetTableMetadata.
}
parquetTableMetadata.directories = directoryList;
parquetTableMetadata.files = metaDataList;
//TODO: We need a merge method that merges two colums with the same name but different types
if (parquetTableMetadata.columnTypeInfo == null) {
parquetTableMetadata.columnTypeInfo = new ConcurrentHashMap<>();
}
parquetTableMetadata.columnTypeInfo.putAll(columnTypeInfoSet);
for (String oldname : OLD_METADATA_FILENAMES) {
fs.delete(new Path(p, oldname), false);
}
writeFile(parquetTableMetadata, new Path(p, METADATA_FILENAME));
if (directoryList.size() > 0 && childFiles.size() == 0) {
ParquetTableMetadataDirs parquetTableMetadataDirs = new ParquetTableMetadataDirs(directoryList);
writeFile(parquetTableMetadataDirs, new Path(p, METADATA_DIRECTORIES_FILENAME));
logger.info("Creating metadata files recursively took {} ms", timer.elapsed(TimeUnit.MILLISECONDS));
timer.stop();
return Pair.of(parquetTableMetadata, parquetTableMetadataDirs);
}
List<String> emptyDirList = Lists.newArrayList();
logger.info("Creating metadata files recursively took {} ms", timer.elapsed(TimeUnit.MILLISECONDS));
timer.stop();
return Pair.of(parquetTableMetadata, new ParquetTableMetadataDirs(emptyDirList));
}
/**
* Get the parquet metadata for the parquet files in a directory
*
* @param path the path of the directory
* @return
* @throws IOException
*/
private ParquetTableMetadata_v3 getParquetTableMetadata(String path) throws IOException {
Path p = new Path(path);
FileStatus fileStatus = fs.getFileStatus(p);
final Stopwatch watch = Stopwatch.createStarted();
List<FileStatus> fileStatuses = getFileStatuses(fileStatus);
logger.info("Took {} ms to get file statuses", watch.elapsed(TimeUnit.MILLISECONDS));
watch.reset();
watch.start();
ParquetTableMetadata_v3 metadata_v3 = getParquetTableMetadata(fileStatuses);
logger.info("Took {} ms to read file metadata", watch.elapsed(TimeUnit.MILLISECONDS));
return metadata_v3;
}
/**
* Get the parquet metadata for a list of parquet files
*
* @param fileStatuses
* @return
* @throws IOException
*/
private ParquetTableMetadata_v3 getParquetTableMetadata(List<FileStatus> fileStatuses)
throws IOException {
ParquetTableMetadata_v3 tableMetadata = new ParquetTableMetadata_v3();
List<ParquetFileMetadata_v3> fileMetadataList = getParquetFileMetadata_v3(tableMetadata, fileStatuses);
tableMetadata.files = fileMetadataList;
tableMetadata.directories = new ArrayList<String>();
return tableMetadata;
}
/**
* Get a list of file metadata for a list of parquet files
*
* @param fileStatuses
* @return
* @throws IOException
*/
private List<ParquetFileMetadata_v3> getParquetFileMetadata_v3(
ParquetTableMetadata_v3 parquetTableMetadata_v3, List<FileStatus> fileStatuses) throws IOException {
List<TimedRunnable<ParquetFileMetadata_v3>> gatherers = Lists.newArrayList();
for (FileStatus file : fileStatuses) {
gatherers.add(new MetadataGatherer(parquetTableMetadata_v3, file));
}
List<ParquetFileMetadata_v3> metaDataList = Lists.newArrayList();
metaDataList.addAll(TimedRunnable.run("Fetch parquet metadata", logger, gatherers, 16));
return metaDataList;
}
/**
* Recursively get a list of files
*
* @param fileStatus
* @return
* @throws IOException
*/
private List<FileStatus> getFileStatuses(FileStatus fileStatus) throws IOException {
List<FileStatus> statuses = Lists.newArrayList();
if (fileStatus.isDirectory()) {
for (FileStatus child : fs.listStatus(fileStatus.getPath(), new DrillPathFilter())) {
statuses.addAll(getFileStatuses(child));
}
} else {
statuses.add(fileStatus);
}
return statuses;
}
/**
* TimedRunnable that reads the footer from parquet and collects file metadata
*/
private class MetadataGatherer extends TimedRunnable<ParquetFileMetadata_v3> {
private FileStatus fileStatus;
private ParquetTableMetadata_v3 parquetTableMetadata;
public MetadataGatherer(ParquetTableMetadata_v3 parquetTableMetadata, FileStatus fileStatus) {
this.fileStatus = fileStatus;
this.parquetTableMetadata = parquetTableMetadata;
}
@Override
protected ParquetFileMetadata_v3 runInner() throws Exception {
return getParquetFileMetadata_v3(parquetTableMetadata, fileStatus);
}
@Override
protected IOException convertToIOException(Exception e) {
if (e instanceof IOException) {
return (IOException) e;
} else {
return new IOException(e);
}
}
}
private OriginalType getOriginalType(Type type, String[] path, int depth) {
if (type.isPrimitive()) {
return type.getOriginalType();
}
Type t = ((GroupType) type).getType(path[depth]);
return getOriginalType(t, path, depth + 1);
}
private ColTypeInfo getColTypeInfo(MessageType schema, Type type, String[] path, int depth) {
if (type.isPrimitive()) {
PrimitiveType primitiveType = (PrimitiveType) type;
int precision = 0;
int scale = 0;
if (primitiveType.getDecimalMetadata() != null) {
precision = primitiveType.getDecimalMetadata().getPrecision();
scale = primitiveType.getDecimalMetadata().getScale();
}
int repetitionLevel = schema.getMaxRepetitionLevel(path);
int definitionLevel = schema.getMaxDefinitionLevel(path);
return new ColTypeInfo(type.getOriginalType(), precision, scale, repetitionLevel, definitionLevel);
}
Type t = ((GroupType) type).getType(path[depth]);
return getColTypeInfo(schema, t, path, depth + 1);
}
private class ColTypeInfo {
public OriginalType originalType;
public int precision;
public int scale;
public int repetitionLevel;
public int definitionLevel;
public ColTypeInfo(OriginalType originalType, int precision, int scale, int repetitionLevel, int definitionLevel) {
this.originalType = originalType;
this.precision = precision;
this.scale = scale;
this.repetitionLevel = repetitionLevel;
this.definitionLevel = definitionLevel;
}
}
/**
* Get the metadata for a single file
*
* @param file
* @return
* @throws IOException
*/
private ParquetFileMetadata_v3 getParquetFileMetadata_v3(ParquetTableMetadata_v3 parquetTableMetadata,
FileStatus file) throws IOException {
ParquetMetadata metadata = ParquetFileReader.readFooter(fs.getConf(), file);
MessageType schema = metadata.getFileMetaData().getSchema();
// Map<SchemaPath, OriginalType> originalTypeMap = Maps.newHashMap();
Map<SchemaPath, ColTypeInfo> colTypeInfoMap = Maps.newHashMap();
schema.getPaths();
for (String[] path : schema.getPaths()) {
colTypeInfoMap.put(SchemaPath.getCompoundPath(path), getColTypeInfo(schema, schema, path, 0));
}
List<RowGroupMetadata_v3> rowGroupMetadataList = Lists.newArrayList();
ArrayList<SchemaPath> ALL_COLS = new ArrayList<>();
ALL_COLS.add(AbstractRecordReader.STAR_COLUMN);
boolean autoCorrectCorruptDates = formatConfig.autoCorrectCorruptDates;
ParquetReaderUtility.DateCorruptionStatus containsCorruptDates = ParquetReaderUtility.detectCorruptDates(metadata, ALL_COLS, autoCorrectCorruptDates);
if (logger.isDebugEnabled()) {
logger.debug(containsCorruptDates.toString());
}
for (BlockMetaData rowGroup : metadata.getBlocks()) {
List<ColumnMetadata_v3> columnMetadataList = Lists.newArrayList();
long length = 0;
for (ColumnChunkMetaData col : rowGroup.getColumns()) {
ColumnMetadata_v3 columnMetadata;
boolean statsAvailable = (col.getStatistics() != null && !col.getStatistics().isEmpty());
Statistics<?> stats = col.getStatistics();
String[] columnName = col.getPath().toArray();
SchemaPath columnSchemaName = SchemaPath.getCompoundPath(columnName);
ColTypeInfo colTypeInfo = colTypeInfoMap.get(columnSchemaName);
ColumnTypeMetadata_v3 columnTypeMetadata =
new ColumnTypeMetadata_v3(columnName, col.getType(), colTypeInfo.originalType,
colTypeInfo.precision, colTypeInfo.scale, colTypeInfo.repetitionLevel, colTypeInfo.definitionLevel);
if (parquetTableMetadata.columnTypeInfo == null) {
parquetTableMetadata.columnTypeInfo = new ConcurrentHashMap<>();
}
// Save the column schema info. We'll merge it into one list
parquetTableMetadata.columnTypeInfo
.put(new ColumnTypeMetadata_v3.Key(columnTypeMetadata.name), columnTypeMetadata);
if (statsAvailable) {
// Write stats when they are not null
Object minValue = null;
Object maxValue = null;
if (stats.genericGetMax() != null && stats.genericGetMin() != null ) {
minValue = stats.genericGetMin();
maxValue = stats.genericGetMax();
if (containsCorruptDates == ParquetReaderUtility.DateCorruptionStatus.META_SHOWS_CORRUPTION
&& columnTypeMetadata.originalType == OriginalType.DATE) {
minValue = ParquetReaderUtility.autoCorrectCorruptedDate((Integer) minValue);
maxValue = ParquetReaderUtility.autoCorrectCorruptedDate((Integer) maxValue);
}
}
columnMetadata =
new ColumnMetadata_v3(columnTypeMetadata.name, col.getType(), minValue, maxValue, stats.getNumNulls());
} else {
columnMetadata = new ColumnMetadata_v3(columnTypeMetadata.name, col.getType(), null, null, null);
}
columnMetadataList.add(columnMetadata);
length += col.getTotalSize();
}
// DRILL-5009: Skip the RowGroup if it is empty
// Note we still read the schema even if there are no values in the RowGroup
if (rowGroup.getRowCount() == 0) {
continue;
}
RowGroupMetadata_v3 rowGroupMeta =
new RowGroupMetadata_v3(rowGroup.getStartingPos(), length, rowGroup.getRowCount(),
getHostAffinity(file, rowGroup.getStartingPos(), length), columnMetadataList);
rowGroupMetadataList.add(rowGroupMeta);
}
String path = Path.getPathWithoutSchemeAndAuthority(file.getPath()).toString();
return new ParquetFileMetadata_v3(path, file.getLen(), rowGroupMetadataList);
}
/**
* Get the host affinity for a row group
*
* @param fileStatus the parquet file
* @param start the start of the row group
* @param length the length of the row group
* @return
* @throws IOException
*/
private Map<String, Float> getHostAffinity(FileStatus fileStatus, long start, long length)
throws IOException {
BlockLocation[] blockLocations = fs.getFileBlockLocations(fileStatus, start, length);
Map<String, Float> hostAffinityMap = Maps.newHashMap();
for (BlockLocation blockLocation : blockLocations) {
for (String host : blockLocation.getHosts()) {
Float currentAffinity = hostAffinityMap.get(host);
float blockStart = blockLocation.getOffset();
float blockEnd = blockStart + blockLocation.getLength();
float rowGroupEnd = start + length;
Float newAffinity = (blockLocation.getLength() - (blockStart < start ? start - blockStart : 0) -
(blockEnd > rowGroupEnd ? blockEnd - rowGroupEnd : 0)) / length;
if (currentAffinity != null) {
hostAffinityMap.put(host, currentAffinity + newAffinity);
} else {
hostAffinityMap.put(host, newAffinity);
}
}
}
return hostAffinityMap;
}
/**
* Serialize parquet metadata to json and write to a file
*
* @param parquetTableMetadata
* @param p
* @throws IOException
*/
private void writeFile(ParquetTableMetadata_v3 parquetTableMetadata, Path p) throws IOException {
JsonFactory jsonFactory = new JsonFactory();
jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
jsonFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
ObjectMapper mapper = new ObjectMapper(jsonFactory);
SimpleModule module = new SimpleModule();
module.addSerializer(ColumnMetadata_v3.class, new ColumnMetadata_v3.Serializer());
mapper.registerModule(module);
FSDataOutputStream os = fs.create(p);
mapper.writerWithDefaultPrettyPrinter().writeValue(os, parquetTableMetadata);
os.flush();
os.close();
}
private void writeFile(ParquetTableMetadataDirs parquetTableMetadataDirs, Path p) throws IOException {
JsonFactory jsonFactory = new JsonFactory();
jsonFactory.configure(Feature.AUTO_CLOSE_TARGET, false);
jsonFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
ObjectMapper mapper = new ObjectMapper(jsonFactory);
SimpleModule module = new SimpleModule();
mapper.registerModule(module);
FSDataOutputStream os = fs.create(p);
mapper.writerWithDefaultPrettyPrinter().writeValue(os, parquetTableMetadataDirs);
os.flush();
os.close();
}
/**
* Read the parquet metadata from a file
*
* @param path
* @return
* @throws IOException
*/
private void readBlockMeta(String path,
boolean dirsOnly,
MetadataContext metaContext) throws IOException {
Stopwatch timer = Stopwatch.createStarted();
Path p = new Path(path);
Path parentDir = p.getParent(); // parent directory of the metadata file
ObjectMapper mapper = new ObjectMapper();
final SimpleModule serialModule = new SimpleModule();
serialModule.addDeserializer(SchemaPath.class, new SchemaPath.De());
serialModule.addKeyDeserializer(ColumnTypeMetadata_v2.Key.class, new ColumnTypeMetadata_v2.Key.DeSerializer());
serialModule.addKeyDeserializer(ColumnTypeMetadata_v3.Key.class, new ColumnTypeMetadata_v3.Key.DeSerializer());
AfterburnerModule module = new AfterburnerModule();
module.setUseOptimizedBeanDeserializer(true);
mapper.registerModule(serialModule);
mapper.registerModule(module);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
FSDataInputStream is = fs.open(p);
boolean alreadyCheckedModification = false;
boolean newMetadata = false;
if (metaContext != null) {
alreadyCheckedModification = metaContext.getStatus(parentDir.toString());
}
if (dirsOnly) {
parquetTableMetadataDirs = mapper.readValue(is, ParquetTableMetadataDirs.class);
logger.info("Took {} ms to read directories from directory cache file", timer.elapsed(TimeUnit.MILLISECONDS));
timer.stop();
if (!alreadyCheckedModification && tableModified(parquetTableMetadataDirs.getDirectories(), p, parentDir, metaContext)) {
parquetTableMetadataDirs =
(createMetaFilesRecursively(Path.getPathWithoutSchemeAndAuthority(p.getParent()).toString())).getRight();
newMetadata = true;
}
} else {
parquetTableMetadata = mapper.readValue(is, ParquetTableMetadataBase.class);
logger.info("Took {} ms to read metadata from cache file", timer.elapsed(TimeUnit.MILLISECONDS));
timer.stop();
if (!alreadyCheckedModification && tableModified(parquetTableMetadata.getDirectories(), p, parentDir, metaContext)) {
parquetTableMetadata =
(createMetaFilesRecursively(Path.getPathWithoutSchemeAndAuthority(p.getParent()).toString())).getLeft();
newMetadata = true;
}
// DRILL-5009: Remove the RowGroup if it is empty
List<? extends ParquetFileMetadata> files = parquetTableMetadata.getFiles();
for (ParquetFileMetadata file : files) {
List<? extends RowGroupMetadata> rowGroups = file.getRowGroups();
for (Iterator<? extends RowGroupMetadata> iter = rowGroups.iterator(); iter.hasNext(); ) {
RowGroupMetadata r = iter.next();
if (r.getRowCount() == 0) {
iter.remove();
}
}
}
}
if (newMetadata && metaContext != null) {
// if new metadata files were created, invalidate the existing metadata context
metaContext.clear();
}
}
/**
* Check if the parquet metadata needs to be updated by comparing the modification time of the directories with
* the modification time of the metadata file
*
* @param directories
* @param metaFilePath
* @return
* @throws IOException
*/
private boolean tableModified(List<String> directories, Path metaFilePath,
Path parentDir,
MetadataContext metaContext)
throws IOException {
Stopwatch timer = Stopwatch.createStarted();
if (metaContext != null) {
metaContext.setStatus(parentDir.toString());
}
long metaFileModifyTime = fs.getFileStatus(metaFilePath).getModificationTime();
FileStatus directoryStatus = fs.getFileStatus(parentDir);
int numDirs = 1;
if (directoryStatus.getModificationTime() > metaFileModifyTime) {
logger.info("Directory {} was modified. Took {} ms to check modification time of {} directories", directoryStatus.getPath().toString(),
timer.elapsed(TimeUnit.MILLISECONDS),
numDirs);
timer.stop();
return true;
}
for (String directory : directories) {
numDirs++;
if (metaContext != null) {
metaContext.setStatus(directory);
}
directoryStatus = fs.getFileStatus(new Path(directory));
if (directoryStatus.getModificationTime() > metaFileModifyTime) {
logger.info("Directory {} was modified. Took {} ms to check modification time of {} directories", directoryStatus.getPath().toString(),
timer.elapsed(TimeUnit.MILLISECONDS),
numDirs);
timer.stop();
return true;
}
}
logger.info("No directories were modified. Took {} ms to check modification time of {} directories", timer.elapsed(TimeUnit.MILLISECONDS), numDirs);
timer.stop();
return false;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "metadata_version")
@JsonSubTypes({
@JsonSubTypes.Type(value = ParquetTableMetadata_v1.class, name="v1"),
@JsonSubTypes.Type(value = ParquetTableMetadata_v2.class, name="v2"),
@JsonSubTypes.Type(value = ParquetTableMetadata_v3.class, name="v3")
})
public static abstract class ParquetTableMetadataBase {
@JsonIgnore public abstract List<String> getDirectories();
@JsonIgnore public abstract List<? extends ParquetFileMetadata> getFiles();
@JsonIgnore public abstract void assignFiles(List<? extends ParquetFileMetadata> newFiles);
public abstract boolean hasColumnMetadata();
@JsonIgnore public abstract PrimitiveTypeName getPrimitiveType(String[] columnName);
@JsonIgnore public abstract OriginalType getOriginalType(String[] columnName);
@JsonIgnore public abstract Integer getRepetitionLevel(String[] columnName);
@JsonIgnore public abstract Integer getDefinitionLevel(String[] columnName);
@JsonIgnore public abstract boolean isRowGroupPrunable();
@JsonIgnore public abstract ParquetTableMetadataBase clone();
@JsonIgnore public abstract String getDrillVersion();
}
public static abstract class ParquetFileMetadata {
@JsonIgnore public abstract String getPath();
@JsonIgnore public abstract Long getLength();
@JsonIgnore public abstract List<? extends RowGroupMetadata> getRowGroups();
}
public static abstract class RowGroupMetadata {
@JsonIgnore public abstract Long getStart();
@JsonIgnore public abstract Long getLength();
@JsonIgnore public abstract Long getRowCount();
@JsonIgnore public abstract Map<String, Float> getHostAffinity();
@JsonIgnore public abstract List<? extends ColumnMetadata> getColumns();
}
public static abstract class ColumnMetadata {
public abstract String[] getName();
public abstract Long getNulls();
public abstract boolean hasSingleValue();
public abstract Object getMinValue();
public abstract Object getMaxValue();
/**
* Set the max value recorded in the parquet metadata statistics.
*
* This object would just be immutable, but due to Drill-4203 we need to correct
* date values that had been corrupted by earlier versions of Drill.
*/
public abstract void setMax(Object newMax);
/**
* Set the min value recorded in the parquet metadata statistics.
*
* This object would just be immutable, but due to Drill-4203 we need to correct
* date values that had been corrupted by earlier versions of Drill.
*/
public abstract void setMin(Object newMax);
public abstract PrimitiveTypeName getPrimitiveType();
public abstract OriginalType getOriginalType();
}
public static class ParquetTableMetadataDirs {
@JsonProperty List<String> directories;
public ParquetTableMetadataDirs() {
// default constructor needed for deserialization
}
public ParquetTableMetadataDirs(List<String> directories) {
this.directories = directories;
}
@JsonIgnore public List<String> getDirectories() {
return directories;
}
}
@JsonTypeName("v1")
public static class ParquetTableMetadata_v1 extends ParquetTableMetadataBase {
@JsonProperty List<ParquetFileMetadata_v1> files;
@JsonProperty List<String> directories;
public ParquetTableMetadata_v1() {
super();
}
public ParquetTableMetadata_v1(List<ParquetFileMetadata_v1> files, List<String> directories) {
this.files = files;
this.directories = directories;
}
@JsonIgnore @Override public List<String> getDirectories() {
return directories;
}
@JsonIgnore @Override public List<? extends ParquetFileMetadata> getFiles() {
return files;
}
@JsonIgnore @Override public void assignFiles(List<? extends ParquetFileMetadata> newFiles) {
this.files = (List<ParquetFileMetadata_v1>) newFiles;
}
@Override public boolean hasColumnMetadata() {
return false;
}
@JsonIgnore @Override public PrimitiveTypeName getPrimitiveType(String[] columnName) {
return null;
}
@JsonIgnore @Override public OriginalType getOriginalType(String[] columnName) {
return null;
}
@JsonIgnore @Override
public Integer getRepetitionLevel(String[] columnName) {
return null;
}
@JsonIgnore @Override
public Integer getDefinitionLevel(String[] columnName) {
return null;
}
@JsonIgnore @Override
public boolean isRowGroupPrunable() {
return false;
}
@JsonIgnore @Override public ParquetTableMetadataBase clone() {
return new ParquetTableMetadata_v1(files, directories);
}
@Override
public String getDrillVersion() {
return null;
}
}
/**
* Struct which contains the metadata for a single parquet file
*/
public static class ParquetFileMetadata_v1 extends ParquetFileMetadata {
@JsonProperty
public String path;
@JsonProperty
public Long length;
@JsonProperty
public List<RowGroupMetadata_v1> rowGroups;
public ParquetFileMetadata_v1() {
super();
}
public ParquetFileMetadata_v1(String path, Long length, List<RowGroupMetadata_v1> rowGroups) {
this.path = path;
this.length = length;
this.rowGroups = rowGroups;
}
@Override
public String toString() {
return String.format("path: %s rowGroups: %s", path, rowGroups);
}
@JsonIgnore @Override public String getPath() {
return path;
}
@JsonIgnore @Override public Long getLength() {
return length;
}
@JsonIgnore @Override public List<? extends RowGroupMetadata> getRowGroups() {
return rowGroups;
}
}
/**
* A struct that contains the metadata for a parquet row group
*/
public static class RowGroupMetadata_v1 extends RowGroupMetadata {
@JsonProperty
public Long start;
@JsonProperty
public Long length;
@JsonProperty
public Long rowCount;
@JsonProperty
public Map<String, Float> hostAffinity;
@JsonProperty
public List<ColumnMetadata_v1> columns;
public RowGroupMetadata_v1() {
super();
}
public RowGroupMetadata_v1(Long start, Long length, Long rowCount, Map<String, Float> hostAffinity,
List<ColumnMetadata_v1> columns) {
this.start = start;
this.length = length;
this.rowCount = rowCount;
this.hostAffinity = hostAffinity;
this.columns = columns;
}
@Override public Long getStart() {
return start;
}
@Override public Long getLength() {
return length;
}
@Override public Long getRowCount() {
return rowCount;
}
@Override public Map<String, Float> getHostAffinity() {
return hostAffinity;
}
@Override public List<? extends ColumnMetadata> getColumns() {
return columns;
}
}
/**
* A struct that contains the metadata for a column in a parquet file
*/
public static class ColumnMetadata_v1 extends ColumnMetadata {
@JsonProperty
public SchemaPath name;
@JsonProperty
public PrimitiveTypeName primitiveType;
@JsonProperty
public OriginalType originalType;
@JsonProperty
public Long nulls;
// JsonProperty for these are associated with the getters and setters
public Object max;
public Object min;
public ColumnMetadata_v1() {
super();
}
public ColumnMetadata_v1(SchemaPath name, PrimitiveTypeName primitiveType, OriginalType originalType,
Object max, Object min, Long nulls) {
this.name = name;
this.primitiveType = primitiveType;
this.originalType = originalType;
this.max = max;
this.min = min;
this.nulls = nulls;
}
@JsonProperty(value = "min")
public Object getMin() {
if (primitiveType == PrimitiveTypeName.BINARY && min != null) {
return new String(((Binary) min).getBytes());
}
return min;
}
@JsonProperty(value = "max")
public Object getMax() {
if (primitiveType == PrimitiveTypeName.BINARY && max != null) {
return new String(((Binary) max).getBytes());
}
return max;
}
@Override public PrimitiveTypeName getPrimitiveType() {
return primitiveType;
}
@Override public OriginalType getOriginalType() {
return originalType;
}
/**
* setter used during deserialization of the 'min' field of the metadata cache file.
*
* @param min
*/
@JsonProperty(value = "min")
public void setMin(Object min) {
this.min = min;
}
/**
* setter used during deserialization of the 'max' field of the metadata cache file.
*
* @param max
*/
@JsonProperty(value = "max")
public void setMax(Object max) {
this.max = max;
}
@Override public String[] getName() {
String[] s = new String[1];
String nameString = name.toString();
// Strip out the surrounding backticks.
s[0]=nameString.substring(1, nameString.length()-1);
return s;
}
@Override public Long getNulls() {
return nulls;
}
@Override public boolean hasSingleValue() {
return (max != null && min != null && max.equals(min));
}
@Override public Object getMinValue() {
return min;