Skip to content

Commit a0818d3

Browse files
committed
Split regular histograms from date histograms. #19551
Currently both aggregations really share the same implementation. This commit splits the implementations so that regular histograms can support decimal intervals/offsets and compute correct buckets for negative decimal values. However the response API is still the same. So for intance both regular histograms and date histograms will produce an `org.elasticsearch.search.aggregations.bucket.histogram.Histogram` aggregation. The optimization to compute an identifier of the rounded value and the rounded value itself has been removed since it was only used by regular histograms, which now do the rounding themselves instead of relying on the Rounding abstraction. Closes #8082 Closes #4847
1 parent f6aeb35 commit a0818d3

File tree

53 files changed

+1685
-1120
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1685
-1120
lines changed

core/src/main/java/org/elasticsearch/common/rounding/Rounding.java

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,9 @@ public abstract class Rounding implements Streamable {
3535
public abstract byte id();
3636

3737
/**
38-
* Given a value, compute a key that uniquely identifies the rounded value although it is not necessarily equal to the rounding value itself.
38+
* Rounds the given value.
3939
*/
40-
public abstract long roundKey(long value);
41-
42-
/**
43-
* Compute the rounded value given the key that identifies it.
44-
*/
45-
public abstract long valueForKey(long key);
46-
47-
/**
48-
* Rounds the given value, equivalent to calling <code>roundValue(roundKey(value))</code>.
49-
*
50-
* @param value The value to round.
51-
* @return The rounded value.
52-
*/
53-
public final long round(long value) {
54-
return valueForKey(roundKey(value));
55-
}
40+
public abstract long round(long value);
5641

5742
/**
5843
* Given the rounded value (which was potentially generated by {@link #round(long)}, returns the next rounding value. For example, with
@@ -112,13 +97,8 @@ public static long roundValue(long key, long interval) {
11297
}
11398

11499
@Override
115-
public long roundKey(long value) {
116-
return roundKey(value, interval);
117-
}
118-
119-
@Override
120-
public long valueForKey(long key) {
121-
return key * interval;
100+
public long round(long value) {
101+
return roundKey(value, interval) * interval;
122102
}
123103

124104
@Override
@@ -179,13 +159,8 @@ public byte id() {
179159
}
180160

181161
@Override
182-
public long roundKey(long utcMillis) {
183-
return rounding.roundKey((long) (factor * utcMillis));
184-
}
185-
186-
@Override
187-
public long valueForKey(long key) {
188-
return rounding.valueForKey(key);
162+
public long round(long utcMillis) {
163+
return rounding.round((long) (factor * utcMillis));
189164
}
190165

191166
@Override
@@ -248,13 +223,8 @@ public byte id() {
248223
}
249224

250225
@Override
251-
public long roundKey(long value) {
252-
return rounding.roundKey(value - offset);
253-
}
254-
255-
@Override
256-
public long valueForKey(long key) {
257-
return offset + rounding.valueForKey(key);
226+
public long round(long value) {
227+
return rounding.round(value - offset) + offset;
258228
}
259229

260230
@Override

core/src/main/java/org/elasticsearch/common/rounding/TimeZoneRounding.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
import java.util.Objects;
3232

3333
/**
34+
* A rounding strategy for dates. It is typically used to group together dates
35+
* that are part of the same hour/day/month, taking into account time zones and
36+
* daylight saving times.
3437
*/
3538
public abstract class TimeZoneRounding extends Rounding {
3639
public static final ParseField INTERVAL_FIELD = new ParseField("interval");
@@ -125,7 +128,7 @@ public byte id() {
125128
}
126129

127130
@Override
128-
public long roundKey(long utcMillis) {
131+
public long round(long utcMillis) {
129132
long rounded = field.roundFloor(utcMillis);
130133
if (timeZone.isFixed() == false && timeZone.getOffset(utcMillis) != timeZone.getOffset(rounded)) {
131134
// in this case, we crossed a time zone transition. In some edge cases this will
@@ -138,20 +141,14 @@ public long roundKey(long utcMillis) {
138141
return rounded;
139142
}
140143

141-
@Override
142-
public long valueForKey(long time) {
143-
assert roundKey(time) == time;
144-
return time;
145-
}
146-
147144
@Override
148145
public long nextRoundingValue(long utcMillis) {
149-
long floor = roundKey(utcMillis);
146+
long floor = round(utcMillis);
150147
// add one unit and round to get to next rounded value
151-
long next = roundKey(field.add(floor, 1));
148+
long next = round(field.add(floor, 1));
152149
if (next == floor) {
153150
// in rare case we need to add more than one unit
154-
next = roundKey(field.add(floor, 2));
151+
next = round(field.add(floor, 2));
155152
}
156153
return next;
157154
}
@@ -216,7 +213,7 @@ public byte id() {
216213
}
217214

218215
@Override
219-
public long roundKey(long utcMillis) {
216+
public long round(long utcMillis) {
220217
long timeLocal = timeZone.convertUTCToLocal(utcMillis);
221218
long rounded = Rounding.Interval.roundValue(Rounding.Interval.roundKey(timeLocal, interval), interval);
222219
long roundedUTC;
@@ -225,7 +222,7 @@ public long roundKey(long utcMillis) {
225222
// check if we crossed DST transition, in this case we want the last rounded value before the transition
226223
long transition = timeZone.previousTransition(utcMillis);
227224
if (transition != utcMillis && transition > roundedUTC) {
228-
roundedUTC = roundKey(transition - 1);
225+
roundedUTC = round(transition - 1);
229226
}
230227
} else {
231228
/*
@@ -276,12 +273,6 @@ private boolean isInDSTGap(long instantLocal) {
276273
return false;
277274
}
278275

279-
@Override
280-
public long valueForKey(long time) {
281-
assert roundKey(time) == time;
282-
return time;
283-
}
284-
285276
@Override
286277
public long nextRoundingValue(long time) {
287278
long timeLocal = time;

core/src/main/java/org/elasticsearch/search/SearchModule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramParser;
117117
import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder;
118118
import org.elasticsearch.search.aggregations.bucket.histogram.HistogramParser;
119+
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram;
119120
import org.elasticsearch.search.aggregations.bucket.histogram.InternalHistogram;
120121
import org.elasticsearch.search.aggregations.bucket.missing.InternalMissing;
121122
import org.elasticsearch.search.aggregations.bucket.missing.MissingAggregationBuilder;
@@ -546,7 +547,7 @@ private void registerBuiltinAggregations() {
546547
registerAggregation(new AggregationSpec(HistogramAggregationBuilder::new, new HistogramParser(),
547548
HistogramAggregationBuilder.AGGREGATION_NAME_FIELD).addResultReader(InternalHistogram::new));
548549
registerAggregation(new AggregationSpec(DateHistogramAggregationBuilder::new, new DateHistogramParser(),
549-
DateHistogramAggregationBuilder.AGGREGATION_NAME_FIELD));
550+
DateHistogramAggregationBuilder.AGGREGATION_NAME_FIELD).addResultReader(InternalDateHistogram::new));
550551
registerAggregation(new AggregationSpec(GeoDistanceAggregationBuilder::new, new GeoDistanceParser(),
551552
GeoDistanceAggregationBuilder.AGGREGATION_NAME_FIELD).addResultReader(InternalGeoDistance::new));
552553
registerAggregation(new AggregationSpec(GeoGridAggregationBuilder::new, new GeoHashGridParser(),

core/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AbstractHistogramAggregatorFactory.java

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)