Skip to content

Commit 56047f7

Browse files
authored
Fix auto_date_histogram serialization bug (#54447)
This fixes a serialization bug in `auto_date_histogram` that comes up in a cluster mixed between pre-7.3.0 and post-7.3.0. Includes #54429 to keep 7.x looking like master for simpler backports. Closes #54382
1 parent ed1edb4 commit 56047f7

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
lines changed

rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/330_auto_date_histogram.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,17 @@ setup:
2525

2626
---
2727
"basic":
28-
- skip:
29-
version: " - 7.7.99"
30-
reason: Tracked in https://github.com/elastic/elasticsearch/issues/54382
3128
- do:
3229
search:
30+
rest_total_hits_as_int: true
3331
body:
3432
size: 0
3533
aggs:
3634
histo:
3735
auto_date_histogram:
3836
field: date
3937
buckets: 2
40-
- match: { hits.total.value: 4 }
38+
- match: { hits.total: 4 }
4139
- length: { aggregations.histo.buckets: 2 }
4240
- match: { aggregations.histo.buckets.0.key_as_string: "2020-03-01T00:00:00.000Z" }
4341
- match: { aggregations.histo.buckets.0.doc_count: 2 }

server/src/main/java/org/elasticsearch/search/aggregations/bucket/histogram/AutoDateHistogramAggregationBuilder.java

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,6 @@ static RoundingInfo[] buildRoundings(ZoneId timeZone, String minimumInterval) {
113113

114114
private String minimumIntervalExpression;
115115

116-
public String getMinimumIntervalExpression() {
117-
return minimumIntervalExpression;
118-
}
119-
120-
public AutoDateHistogramAggregationBuilder setMinimumIntervalExpression(String minimumIntervalExpression) {
121-
if (minimumIntervalExpression != null && !ALLOWED_INTERVALS.containsValue(minimumIntervalExpression)) {
122-
throw new IllegalArgumentException(MINIMUM_INTERVAL_FIELD.getPreferredName() +
123-
" must be one of [" + ALLOWED_INTERVALS.values().toString() + "]");
124-
}
125-
this.minimumIntervalExpression = minimumIntervalExpression;
126-
return this;
127-
}
128-
129-
130116
/** Create a new builder with the given name. */
131117
public AutoDateHistogramAggregationBuilder(String name) {
132118
super(name, CoreValuesSourceType.NUMERIC, ValueType.DATE);
@@ -141,6 +127,14 @@ public AutoDateHistogramAggregationBuilder(StreamInput in) throws IOException {
141127
}
142128
}
143129

130+
@Override
131+
protected void innerWriteTo(StreamOutput out) throws IOException {
132+
out.writeVInt(numBuckets);
133+
if (out.getVersion().onOrAfter(Version.V_7_3_0)) {
134+
out.writeOptionalString(minimumIntervalExpression);
135+
}
136+
}
137+
144138
protected AutoDateHistogramAggregationBuilder(AutoDateHistogramAggregationBuilder clone, Builder factoriesBuilder,
145139
Map<String, Object> metaData) {
146140
super(clone, factoriesBuilder, metaData);
@@ -153,19 +147,24 @@ protected AggregationBuilder shallowCopy(Builder factoriesBuilder, Map<String, O
153147
return new AutoDateHistogramAggregationBuilder(this, factoriesBuilder, metaData);
154148
}
155149

156-
@Override
157-
protected void innerWriteTo(StreamOutput out) throws IOException {
158-
out.writeVInt(numBuckets);
159-
if (out.getVersion().onOrAfter(Version.V_7_3_0)) {
160-
out.writeOptionalString(minimumIntervalExpression);
161-
}
162-
}
163-
164150
@Override
165151
public String getType() {
166152
return NAME;
167153
}
168154

155+
public String getMinimumIntervalExpression() {
156+
return minimumIntervalExpression;
157+
}
158+
159+
public AutoDateHistogramAggregationBuilder setMinimumIntervalExpression(String minimumIntervalExpression) {
160+
if (minimumIntervalExpression != null && !ALLOWED_INTERVALS.containsValue(minimumIntervalExpression)) {
161+
throw new IllegalArgumentException(MINIMUM_INTERVAL_FIELD.getPreferredName() +
162+
" must be one of [" + ALLOWED_INTERVALS.values().toString() + "]");
163+
}
164+
this.minimumIntervalExpression = minimumIntervalExpression;
165+
return this;
166+
}
167+
169168
public AutoDateHistogramAggregationBuilder setNumBuckets(int numBuckets) {
170169
if (numBuckets <= 0) {
171170
throw new IllegalArgumentException(NUM_BUCKETS_FIELD.getPreferredName() + " must be greater than 0 for [" + name + "]");
@@ -262,7 +261,17 @@ public RoundingInfo(StreamInput in) throws IOException {
262261
roughEstimateDurationMillis = in.readVLong();
263262
innerIntervals = in.readIntArray();
264263
unitAbbreviation = in.readString();
265-
dateTimeUnit = in.readString();
264+
if (in.getVersion().onOrAfter(Version.V_7_3_0)) {
265+
dateTimeUnit = in.readString();
266+
} else {
267+
/*
268+
* This *should* be safe because we only deserialize RoundingInfo
269+
* when reading result and results don't actually use this at all.
270+
* We just set it to something non-null to line up with the normal
271+
* ctor. "seconds" is the smallest unit anyway.
272+
*/
273+
dateTimeUnit = "second";
274+
}
266275
}
267276

268277
@Override
@@ -271,7 +280,9 @@ public void writeTo(StreamOutput out) throws IOException {
271280
out.writeVLong(roughEstimateDurationMillis);
272281
out.writeIntArray(innerIntervals);
273282
out.writeString(unitAbbreviation);
274-
out.writeString(dateTimeUnit);
283+
if (out.getVersion().onOrAfter(Version.V_7_3_0)) {
284+
out.writeString(dateTimeUnit);
285+
}
275286
}
276287

277288
public int getMaximumInnerInterval() {

0 commit comments

Comments
 (0)