forked from apache/parquet-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EncodingStats.java
157 lines (132 loc) · 4.85 KB
/
EncodingStats.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
/*
* 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.parquet.column;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import static org.apache.parquet.column.Encoding.PLAIN_DICTIONARY;
import static org.apache.parquet.column.Encoding.RLE_DICTIONARY;
/**
* EncodingStats track dictionary and data page encodings for a single column within a row group.
* These are used when filtering row groups. For example, to filter a row group based on a column's
* dictionary, all of the data pages in that column must be dictionary-encoded. This class provides
* convenience methods for those checks, like {@link #hasNonDictionaryEncodedPages()}.
*/
public class EncodingStats {
final Map<Encoding, Number> dictStats;
final Map<Encoding, Number> dataStats;
private final boolean usesV2Pages;
private EncodingStats(Map<Encoding, Number> dictStats,
Map<Encoding, Number> dataStats,
boolean usesV2Pages) {
this.dictStats = dictStats;
this.dataStats = dataStats;
this.usesV2Pages = usesV2Pages;
}
public Set<Encoding> getDictionaryEncodings() {
return dictStats.keySet();
}
public Set<Encoding> getDataEncodings() {
return dataStats.keySet();
}
public int getNumDictionaryPagesEncodedAs(Encoding enc) {
final Number pageCount = dictStats.get(enc);
return (pageCount == null) ? 0 : pageCount.intValue();
}
public int getNumDataPagesEncodedAs(Encoding enc) {
final Number pageCount = dataStats.get(enc);
return (pageCount == null) ? 0 : pageCount.intValue();
}
public boolean hasDictionaryPages() {
return !dictStats.isEmpty();
}
public boolean hasDictionaryEncodedPages() {
Set<Encoding> encodings = dataStats.keySet();
return (encodings.contains(RLE_DICTIONARY) || encodings.contains(PLAIN_DICTIONARY));
}
public boolean hasNonDictionaryEncodedPages() {
if (dataStats.isEmpty()) {
return false; // no pages
}
// this modifies the set, so copy it
Set<Encoding> encodings = new HashSet<>(dataStats.keySet());
if (!encodings.remove(RLE_DICTIONARY) &&
!encodings.remove(PLAIN_DICTIONARY)) {
return true; // not dictionary encoded
}
if (encodings.isEmpty()) {
return false;
}
// at least one non-dictionary encoding is present
return true;
}
public boolean usesV2Pages() {
return usesV2Pages;
}
/**
* Used to build {@link EncodingStats} from metadata or to accumulate stats as pages are written.
*/
public static class Builder {
private final Map<Encoding, AtomicInteger> dictStats = new LinkedHashMap<>();
private final Map<Encoding, AtomicInteger> dataStats = new LinkedHashMap<>();
private boolean usesV2Pages = false;
public Builder clear() {
this.usesV2Pages = false;
dictStats.clear();
dataStats.clear();
return this;
}
public Builder withV2Pages() {
this.usesV2Pages = true;
return this;
}
public Builder addDictEncoding(Encoding encoding) {
return addDictEncoding(encoding, 1);
}
public Builder addDictEncoding(Encoding encoding, int numPages) {
dictStats.computeIfAbsent(encoding, enc -> new AtomicInteger(0))
.addAndGet(numPages);
return this;
}
public Builder addDataEncodings(Collection<Encoding> encodings) {
for (Encoding encoding : encodings) {
addDataEncoding(encoding);
}
return this;
}
public Builder addDataEncoding(Encoding encoding) {
return addDataEncoding(encoding, 1);
}
public Builder addDataEncoding(Encoding encoding, int numPages) {
dataStats.computeIfAbsent(encoding, enc -> new AtomicInteger(0))
.addAndGet(numPages);
return this;
}
public EncodingStats build() {
return new EncodingStats(
Collections.unmodifiableMap(new LinkedHashMap<>(dictStats)),
Collections.unmodifiableMap(new LinkedHashMap<>(dataStats)),
usesV2Pages);
}
}
}