/
HistogramAggregatorFactory.java
278 lines (242 loc) · 7.33 KB
/
HistogramAggregatorFactory.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
/*
* 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.druid.query.aggregation;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.segment.ColumnSelectorFactory;
import org.apache.druid.segment.ColumnValueSelector;
import org.apache.druid.segment.column.ColumnType;
import javax.annotation.Nullable;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class HistogramAggregatorFactory extends AggregatorFactory
{
public static final ColumnType TYPE = ColumnType.ofComplex("histogram");
// todo: this isn't registered with serde, is it a lie? should we just report null as the type name? or register a serde?
public static final ColumnType TYPE_VISUAL = ColumnType.ofComplex("histogramVisual");
private final String name;
private final String fieldName;
private final List<Float> breaksList;
private final float[] breaks;
@JsonCreator
public HistogramAggregatorFactory(
@JsonProperty("name") String name,
@JsonProperty("fieldName") final String fieldName,
@JsonProperty("breaks") final List<Float> breaksList
)
{
Preconditions.checkNotNull(name, "Must have a valid, non-null aggregator name");
Preconditions.checkNotNull(fieldName, "Must have a valid, non-null fieldName");
this.name = name;
this.fieldName = fieldName;
this.breaksList = (breaksList == null) ? new ArrayList<>() : breaksList;
this.breaks = new float[this.breaksList.size()];
for (int i = 0; i < this.breaksList.size(); ++i) {
this.breaks[i] = this.breaksList.get(i);
}
}
@Override
public Aggregator factorize(ColumnSelectorFactory metricFactory)
{
return new HistogramAggregator(metricFactory.makeColumnValueSelector(fieldName), breaks);
}
@Override
public BufferAggregator factorizeBuffered(ColumnSelectorFactory metricFactory)
{
return new HistogramBufferAggregator(metricFactory.makeColumnValueSelector(fieldName), breaks);
}
@Override
public Comparator getComparator()
{
return HistogramAggregator.COMPARATOR;
}
@Override
public Object combine(Object lhs, Object rhs)
{
return HistogramAggregator.combineHistograms(lhs, rhs);
}
@Override
public AggregateCombiner makeAggregateCombiner()
{
// HistogramAggregatorFactory.combine() delegates to HistogramAggregator.combineHistograms() and it doesn't check
// for nulls, so this AggregateCombiner neither.
return new ObjectAggregateCombiner<Histogram>()
{
private Histogram combined;
@Override
public void reset(ColumnValueSelector selector)
{
Histogram first = (Histogram) selector.getObject();
if (combined == null) {
combined = new Histogram(first);
} else {
combined.copyFrom(first);
}
}
@Override
public void fold(ColumnValueSelector selector)
{
Histogram other = (Histogram) selector.getObject();
combined.fold(other);
}
@Override
public Class<Histogram> classOfObject()
{
return Histogram.class;
}
@Nullable
@Override
public Histogram getObject()
{
return combined;
}
};
}
@Override
public AggregatorFactory getCombiningFactory()
{
return new HistogramAggregatorFactory(name, name, breaksList);
}
@Override
public Object deserialize(Object object)
{
if (object instanceof byte[]) {
return Histogram.fromBytes((byte[]) object);
} else if (object instanceof ByteBuffer) {
return Histogram.fromBytes((ByteBuffer) object);
} else if (object instanceof String) {
byte[] bytes = StringUtils.decodeBase64(StringUtils.toUtf8((String) object));
return Histogram.fromBytes(bytes);
}
return object;
}
@Nullable
@Override
public Object finalizeComputation(@Nullable Object object)
{
return object == null ? null : ((Histogram) object).asVisual();
}
@Override
@JsonProperty
public String getName()
{
return name;
}
@JsonProperty
public String getFieldName()
{
return fieldName;
}
@JsonProperty
public List<Float> getBreaks()
{
return breaksList;
}
@Override
public List<String> requiredFields()
{
return Collections.singletonList(fieldName);
}
@Override
public byte[] getCacheKey()
{
byte[] fieldNameBytes = StringUtils.toUtf8(fieldName);
ByteBuffer buf = ByteBuffer
.allocate(1 + fieldNameBytes.length + Float.BYTES * breaks.length)
.put(AggregatorUtil.HIST_CACHE_TYPE_ID)
.put(fieldNameBytes)
.put((byte) 0xFF);
buf.asFloatBuffer().put(breaks);
return buf.array();
}
/**
* actual type is {@link Histogram}
*/
@Override
public ColumnType getIntermediateType()
{
return TYPE;
}
/**
* actual type is {@link HistogramVisual}
*/
@Override
public ColumnType getResultType()
{
return TYPE_VISUAL;
}
@Override
public int getMaxIntermediateSize()
{
return Long.BYTES * (breaks.length + 1) + Float.BYTES * 2;
}
@Override
public AggregatorFactory withName(String newName)
{
return new HistogramAggregatorFactory(newName, fieldName, breaksList);
}
@Override
public String toString()
{
return "HistogramAggregatorFactory{" +
"name='" + name + '\'' +
", fieldName='" + fieldName + '\'' +
", breaks=" + Arrays.toString(breaks) +
'}';
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
HistogramAggregatorFactory that = (HistogramAggregatorFactory) o;
if (!Arrays.equals(breaks, that.breaks)) {
return false;
}
if (breaksList != null ? !breaksList.equals(that.breaksList) : that.breaksList != null) {
return false;
}
if (fieldName != null ? !fieldName.equals(that.fieldName) : that.fieldName != null) {
return false;
}
if (name != null ? !name.equals(that.name) : that.name != null) {
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (fieldName != null ? fieldName.hashCode() : 0);
result = 31 * result + (breaksList != null ? breaksList.hashCode() : 0);
result = 31 * result + (breaks != null ? Arrays.hashCode(breaks) : 0);
return result;
}
}