/
StatisticsFilter.java
283 lines (236 loc) · 9.6 KB
/
StatisticsFilter.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
package parquet.filter2.statisticslevel;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import parquet.column.statistics.Statistics;
import parquet.common.schema.ColumnPath;
import parquet.filter2.predicate.FilterPredicate;
import parquet.filter2.predicate.Operators.And;
import parquet.filter2.predicate.Operators.Column;
import parquet.filter2.predicate.Operators.Eq;
import parquet.filter2.predicate.Operators.Gt;
import parquet.filter2.predicate.Operators.GtEq;
import parquet.filter2.predicate.Operators.LogicalNotUserDefined;
import parquet.filter2.predicate.Operators.Lt;
import parquet.filter2.predicate.Operators.LtEq;
import parquet.filter2.predicate.Operators.Not;
import parquet.filter2.predicate.Operators.NotEq;
import parquet.filter2.predicate.Operators.Or;
import parquet.filter2.predicate.Operators.UserDefined;
import parquet.filter2.predicate.UserDefinedPredicate;
import parquet.hadoop.metadata.ColumnChunkMetaData;
import static parquet.Preconditions.checkArgument;
import static parquet.Preconditions.checkNotNull;
/**
* Applies a {@link parquet.filter2.predicate.FilterPredicate} to statistics about a group of
* records.
*
* Note: the supplied predicate must not contain any instances of the not() operator as this is not
* supported by this filter.
*
* the supplied predicate should first be run through {@link parquet.filter2.predicate.LogicalInverseRewriter} to rewrite it
* in a form that doesn't make use of the not() operator.
*
* the supplied predicate should also have already been run through
* {@link parquet.filter2.predicate.SchemaCompatibilityValidator}
* to make sure it is compatible with the schema of this file.
*
* Returns true if all the records represented by the statistics in the provided column metadata can be dropped.
* false otherwise (including when it is not known, which is often the case).
*/
// TODO: this belongs in the parquet-column project, but some of the classes here need to be moved too
// TODO: (https://issues.apache.org/jira/browse/PARQUET-38)
public class StatisticsFilter implements FilterPredicate.Visitor<Boolean> {
public static boolean canDrop(FilterPredicate pred, List<ColumnChunkMetaData> columns) {
checkNotNull(pred, "pred");
checkNotNull(columns, "columns");
return pred.accept(new StatisticsFilter(columns));
}
private final Map<ColumnPath, ColumnChunkMetaData> columns = new HashMap<ColumnPath, ColumnChunkMetaData>();
private StatisticsFilter(List<ColumnChunkMetaData> columnsList) {
for (ColumnChunkMetaData chunk : columnsList) {
columns.put(chunk.getPath(), chunk);
}
}
private ColumnChunkMetaData getColumnChunk(ColumnPath columnPath) {
ColumnChunkMetaData c = columns.get(columnPath);
checkArgument(c != null, "Column " + columnPath.toDotString() + " not found in schema!");
return c;
}
// is this column chunk composed entirely of nulls?
// assumes the column chunk's statistics is not empty
private boolean isAllNulls(ColumnChunkMetaData column) {
return column.getStatistics().getNumNulls() == column.getValueCount();
}
// are there any nulls in this column chunk?
// assumes the column chunk's statistics is not empty
private boolean hasNulls(ColumnChunkMetaData column) {
return column.getStatistics().getNumNulls() > 0;
}
@Override
public <T extends Comparable<T>> Boolean visit(Eq<T> eq) {
Column<T> filterColumn = eq.getColumn();
T value = eq.getValue();
ColumnChunkMetaData columnChunk = getColumnChunk(filterColumn.getColumnPath());
Statistics<T> stats = columnChunk.getStatistics();
if (stats.isEmpty()) {
// we have no statistics available, we cannot drop any chunks
return false;
}
if (value == null) {
// we are looking for records where v eq(null)
// so drop if there are no nulls in this chunk
return !hasNulls(columnChunk);
}
if (isAllNulls(columnChunk)) {
// we are looking for records where v eq(someNonNull)
// and this is a column of all nulls, so drop it
return true;
}
// drop if value < min || value > max
return value.compareTo(stats.genericGetMin()) < 0 || value.compareTo(stats.genericGetMax()) > 0;
}
@Override
public <T extends Comparable<T>> Boolean visit(NotEq<T> notEq) {
Column<T> filterColumn = notEq.getColumn();
T value = notEq.getValue();
ColumnChunkMetaData columnChunk = getColumnChunk(filterColumn.getColumnPath());
Statistics<T> stats = columnChunk.getStatistics();
if (stats.isEmpty()) {
// we have no statistics available, we cannot drop any chunks
return false;
}
if (value == null) {
// we are looking for records where v notEq(null)
// so, if this is a column of all nulls, we can drop it
return isAllNulls(columnChunk);
}
if (hasNulls(columnChunk)) {
// we are looking for records where v notEq(someNonNull)
// but this chunk contains nulls, we cannot drop it
return false;
}
// drop if this is a column where min = max = value
return value.compareTo(stats.genericGetMin()) == 0 && value.compareTo(stats.genericGetMax()) == 0;
}
@Override
public <T extends Comparable<T>> Boolean visit(Lt<T> lt) {
Column<T> filterColumn = lt.getColumn();
T value = lt.getValue();
ColumnChunkMetaData columnChunk = getColumnChunk(filterColumn.getColumnPath());
Statistics<T> stats = columnChunk.getStatistics();
if (stats.isEmpty()) {
// we have no statistics available, we cannot drop any chunks
return false;
}
if (isAllNulls(columnChunk)) {
// we are looking for records where v < someValue
// this chunk is all nulls, so we can drop it
return true;
}
// drop if value <= min
return value.compareTo(stats.genericGetMin()) <= 0;
}
@Override
public <T extends Comparable<T>> Boolean visit(LtEq<T> ltEq) {
Column<T> filterColumn = ltEq.getColumn();
T value = ltEq.getValue();
ColumnChunkMetaData columnChunk = getColumnChunk(filterColumn.getColumnPath());
Statistics<T> stats = columnChunk.getStatistics();
if (stats.isEmpty()) {
// we have no statistics available, we cannot drop any chunks
return false;
}
if (isAllNulls(columnChunk)) {
// we are looking for records where v <= someValue
// this chunk is all nulls, so we can drop it
return true;
}
// drop if value < min
return value.compareTo(stats.genericGetMin()) < 0;
}
@Override
public <T extends Comparable<T>> Boolean visit(Gt<T> gt) {
Column<T> filterColumn = gt.getColumn();
T value = gt.getValue();
ColumnChunkMetaData columnChunk = getColumnChunk(filterColumn.getColumnPath());
Statistics<T> stats = columnChunk.getStatistics();
if (stats.isEmpty()) {
// we have no statistics available, we cannot drop any chunks
return false;
}
if (isAllNulls(columnChunk)) {
// we are looking for records where v > someValue
// this chunk is all nulls, so we can drop it
return true;
}
// drop if value >= max
return value.compareTo(stats.genericGetMax()) >= 0;
}
@Override
public <T extends Comparable<T>> Boolean visit(GtEq<T> gtEq) {
Column<T> filterColumn = gtEq.getColumn();
T value = gtEq.getValue();
ColumnChunkMetaData columnChunk = getColumnChunk(filterColumn.getColumnPath());
Statistics<T> stats = columnChunk.getStatistics();
if (stats.isEmpty()) {
// we have no statistics available, we cannot drop any chunks
return false;
}
if (isAllNulls(columnChunk)) {
// we are looking for records where v >= someValue
// this chunk is all nulls, so we can drop it
return true;
}
// drop if value >= max
return value.compareTo(stats.genericGetMax()) > 0;
}
@Override
public Boolean visit(And and) {
return and.getLeft().accept(this) && and.getRight().accept(this);
}
@Override
public Boolean visit(Or or) {
// seems unintuitive to put an && not an || here
// but we can only drop a chunk of records if we know that
// both the left and right predicates agree that no matter what
// we don't need this chunk.
return or.getLeft().accept(this) && or.getRight().accept(this);
}
@Override
public Boolean visit(Not not) {
throw new IllegalArgumentException(
"This predicate contains a not! Did you forget to run this predicate through LogicalInverseRewriter? " + not);
}
private <T extends Comparable<T>, U extends UserDefinedPredicate<T>> Boolean visit(UserDefined<T, U> ud, boolean inverted) {
Column<T> filterColumn = ud.getColumn();
ColumnChunkMetaData columnChunk = getColumnChunk(filterColumn.getColumnPath());
U udp = ud.getUserDefinedPredicate();
Statistics<T> stats = columnChunk.getStatistics();
if (stats.isEmpty()) {
// we have no statistics available, we cannot drop any chunks
return false;
}
if (isAllNulls(columnChunk)) {
// there is no min max, there is nothing
// else we can say about this chunk, we
// cannot drop it.
return false;
}
parquet.filter2.predicate.Statistics<T> udpStats =
new parquet.filter2.predicate.Statistics<T>(stats.genericGetMin(), stats.genericGetMax());
if (inverted) {
return udp.inverseCanDrop(udpStats);
} else {
return udp.canDrop(udpStats);
}
}
@Override
public <T extends Comparable<T>, U extends UserDefinedPredicate<T>> Boolean visit(UserDefined<T, U> ud) {
return visit(ud, false);
}
@Override
public <T extends Comparable<T>, U extends UserDefinedPredicate<T>> Boolean visit(LogicalNotUserDefined<T, U> lnud) {
return visit(lnud.getUserDefined(), true);
}
}