-
Notifications
You must be signed in to change notification settings - Fork 984
/
ParquetGroupScanStatistics.java
222 lines (203 loc) · 9.11 KB
/
ParquetGroupScanStatistics.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
/*
* 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.drill.exec.store.parquet;
import org.apache.commons.lang3.mutable.MutableLong;
import org.apache.drill.common.expression.SchemaPath;
import org.apache.drill.common.types.TypeProtos;
import org.apache.drill.exec.physical.impl.statistics.Statistic;
import org.apache.drill.exec.record.metadata.ColumnMetadata;
import org.apache.drill.exec.record.metadata.SchemaPathUtils;
import org.apache.drill.metastore.BaseMetadata;
import org.apache.hadoop.fs.Path;
import org.apache.drill.metastore.ColumnStatistics;
import org.apache.drill.metastore.ColumnStatisticsKind;
import org.apache.drill.metastore.LocationProvider;
import org.apache.drill.metastore.TableStatisticsKind;
import org.apache.drill.shaded.guava.com.google.common.collect.HashBasedTable;
import org.apache.drill.shaded.guava.com.google.common.collect.Table;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Holds common statistics about data in parquet group scan,
* including information about total row count, columns counts, partition columns.
*/
public class ParquetGroupScanStatistics<T extends BaseMetadata & LocationProvider> {
// map from file names to maps of column name to partition value mappings
private Table<Path, SchemaPath, Object> partitionValueMap;
// only for partition columns : value is unique for each partition
private Map<SchemaPath, TypeProtos.MajorType> partitionColTypeMap;
// total number of non-null value for each column in parquet files
private Map<SchemaPath, MutableLong> columnValueCounts;
// total number of rows (obtained from parquet footer)
private long rowCount;
public ParquetGroupScanStatistics(List<T> rowGroupInfos) {
collect(rowGroupInfos);
}
@SuppressWarnings("unchecked")
public ParquetGroupScanStatistics(ParquetGroupScanStatistics that) {
this.partitionValueMap = HashBasedTable.create(that.partitionValueMap);
this.partitionColTypeMap = new HashMap<>(that.partitionColTypeMap);
this.columnValueCounts = new HashMap<>(that.columnValueCounts);
this.rowCount = that.rowCount;
}
public long getColumnValueCount(SchemaPath column) {
MutableLong count = columnValueCounts.get(column);
return count != null ? count.getValue() : 0;
}
public List<SchemaPath> getPartitionColumns() {
return new ArrayList<>(partitionColTypeMap.keySet());
}
public TypeProtos.MajorType getTypeForColumn(SchemaPath schemaPath) {
return partitionColTypeMap.get(schemaPath);
}
public long getRowCount() {
return rowCount;
}
public Object getPartitionValue(Path path, SchemaPath column) {
Object partitionValue = partitionValueMap.get(path, column);
if (partitionValue == BaseParquetMetadataProvider.NULL_VALUE) {
return null;
}
return partitionValue;
}
public Map<Path, Object> getPartitionPaths(SchemaPath column) {
return partitionValueMap.column(column);
}
public void collect(List<T> metadataList) {
resetHolders();
boolean first = true;
for (T metadata : metadataList) {
long localRowCount = (long) TableStatisticsKind.ROW_COUNT.getValue(metadata);
for (Map.Entry<SchemaPath, ColumnStatistics> columnsStatistics : metadata.getColumnsStatistics().entrySet()) {
SchemaPath schemaPath = columnsStatistics.getKey();
ColumnStatistics statistics = columnsStatistics.getValue();
MutableLong emptyCount = new MutableLong();
MutableLong previousCount = columnValueCounts.putIfAbsent(schemaPath, emptyCount);
if (previousCount == null) {
previousCount = emptyCount;
}
Long nullsNum = (Long) statistics.getStatistic(ColumnStatisticsKind.NULLS_COUNT);
if (previousCount.longValue() != Statistic.NO_COLUMN_STATS && nullsNum != null && nullsNum != Statistic.NO_COLUMN_STATS) {
previousCount.add(localRowCount - nullsNum);
} else {
previousCount.setValue(Statistic.NO_COLUMN_STATS);
}
ColumnMetadata columnMetadata = SchemaPathUtils.getColumnMetadata(schemaPath, metadata.getSchema());
TypeProtos.MajorType majorType = columnMetadata != null ? columnMetadata.majorType() : null;
boolean partitionColumn = checkForPartitionColumn(statistics, first, localRowCount, majorType, schemaPath);
if (partitionColumn) {
Object value = partitionValueMap.get(metadata.getLocation(), schemaPath);
Object currentValue = statistics.getStatistic(ColumnStatisticsKind.MAX_VALUE);
if (value != null && value != BaseParquetMetadataProvider.NULL_VALUE) {
if (value != currentValue) {
partitionColTypeMap.remove(schemaPath);
}
} else {
// the value of a column with primitive type can not be null,
// so checks that there are really null value and puts it to the map
if (localRowCount == (long) statistics.getStatistic(ColumnStatisticsKind.NULLS_COUNT)) {
partitionValueMap.put(metadata.getLocation(), schemaPath, BaseParquetMetadataProvider.NULL_VALUE);
} else {
partitionValueMap.put(metadata.getLocation(), schemaPath, currentValue);
}
}
} else {
partitionColTypeMap.remove(schemaPath);
}
}
this.rowCount += localRowCount;
first = false;
}
}
/**
* Re-init holders eigther during first instance creation or statistics update based on updated list of row groups.
*/
private void resetHolders() {
this.partitionValueMap = HashBasedTable.create();
this.partitionColTypeMap = new HashMap<>();
this.columnValueCounts = new HashMap<>();
this.rowCount = 0;
}
/**
* When reading the very first footer, any column is a potential partition column. So for the first footer, we check
* every column to see if it is single valued, and if so, add it to the list of potential partition columns. For the
* remaining footers, we will not find any new partition columns, but we may discover that what was previously a
* potential partition column now no longer qualifies, so it needs to be removed from the list.
*
* @param columnStatistics column metadata
* @param first if columns first appears in row group
* @param rowCount row count
* @return whether column is a potential partition column
*/
private boolean checkForPartitionColumn(ColumnStatistics columnStatistics,
boolean first,
long rowCount,
TypeProtos.MajorType type,
SchemaPath schemaPath) {
if (first) {
if (hasSingleValue(columnStatistics, rowCount)) {
partitionColTypeMap.put(schemaPath, type);
return true;
} else {
return false;
}
} else {
if (!partitionColTypeMap.keySet().contains(schemaPath)) {
return false;
} else {
if (!hasSingleValue(columnStatistics, rowCount)) {
partitionColTypeMap.remove(schemaPath);
return false;
}
if (!partitionColTypeMap.get(schemaPath).equals(type)) {
partitionColTypeMap.remove(schemaPath);
return false;
}
}
}
return true;
}
/**
* Checks that the column chunk has a single value.
* ColumnMetadata will have a non-null value iff the minValue and
* the maxValue for the rowgroup are the same.
*
* @param columnStatistics metadata to check
* @param rowCount rows count in column chunk
* @return true if column has single value
*/
private boolean hasSingleValue(ColumnStatistics columnStatistics, long rowCount) {
return columnStatistics != null && isSingleVal(columnStatistics, rowCount);
}
private boolean isSingleVal(ColumnStatistics columnStatistics, long rowCount) {
Long numNulls = (Long) columnStatistics.getStatistic(ColumnStatisticsKind.NULLS_COUNT);
if (numNulls != null && numNulls != Statistic.NO_COLUMN_STATS) {
Object min = columnStatistics.getStatistic(ColumnStatisticsKind.MIN_VALUE);
Object max = columnStatistics.getStatistic(ColumnStatisticsKind.MAX_VALUE);
if (min != null) {
return (numNulls == 0 || numNulls == rowCount) && Objects.deepEquals(min, max);
} else {
return numNulls == rowCount && max == null;
}
}
return false;
}
}