Skip to content

Commit 2e26a5d

Browse files
author
Zachary Tong
authored
Throw exception if legacy interval cannot be parsed (#41972)
Due to the fallthrough logic, DateIntervalWrapper assumed that an otherwise unparsable interval was a legacy fixed millis interval. This could then NPE if the interval was just illegal ("foobar"). This commit correctly checks if the legacy millis parsing fails too, and throws an IllegalArgumentException at that point signaling the provided interval is bad.
1 parent 61c28bc commit 2e26a5d

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,15 @@ public Rounding createRounding(ZoneId timeZone) {
288288
} else {
289289
// We're not sure what the interval was originally (legacy) so use old behavior of assuming
290290
// calendar first, then fixed. Required because fixed/cal overlap in places ("1h")
291-
DateTimeUnit intervalAsUnit = tryIntervalAsCalendarUnit();
292-
if (intervalAsUnit != null) {
293-
tzRoundingBuilder = Rounding.builder(tryIntervalAsCalendarUnit());
291+
DateTimeUnit calInterval = tryIntervalAsCalendarUnit();
292+
TimeValue fixedInterval = tryIntervalAsFixedUnit();
293+
if (calInterval != null) {
294+
tzRoundingBuilder = Rounding.builder(calInterval);
295+
} else if (fixedInterval != null) {
296+
tzRoundingBuilder = Rounding.builder(fixedInterval);
294297
} else {
295-
tzRoundingBuilder = Rounding.builder(tryIntervalAsFixedUnit());
298+
// If we get here we have exhausted our options and are not able to parse this interval
299+
throw new IllegalArgumentException("Unable to parse interval [" + dateHistogramInterval + "]");
296300
}
297301
}
298302
if (timeZone != null) {

server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramAggregatorTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,16 @@ public void testLegacyThenNew() throws IOException {
10971097
assertWarnings("[interval] on [date_histogram] is deprecated, use [fixed_interval] or [calendar_interval] in the future.");
10981098
}
10991099

1100+
public void testIllegalInterval() throws IOException {
1101+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> testSearchCase(new MatchAllDocsQuery(),
1102+
Collections.emptyList(),
1103+
aggregation -> aggregation.dateHistogramInterval(new DateHistogramInterval("foobar")).field(DATE_FIELD),
1104+
histogram -> {}
1105+
));
1106+
assertThat(e.getMessage(), equalTo("Unable to parse interval [foobar]"));
1107+
assertWarnings("[interval] on [date_histogram] is deprecated, use [fixed_interval] or [calendar_interval] in the future.");
1108+
}
1109+
11001110
private void testSearchCase(Query query, List<String> dataset,
11011111
Consumer<DateHistogramAggregationBuilder> configure,
11021112
Consumer<InternalDateHistogram> verify) throws IOException {

x-pack/plugin/src/test/resources/rest-api-spec/test/rollup/rollup_search.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,9 +1246,7 @@ setup:
12461246

12471247
---
12481248
"Search error against live index":
1249-
- skip:
1250-
version: "all"
1251-
reason: "AwaitsFix: https://github.com/elastic/elasticsearch/issues/41970"
1249+
12521250
- do:
12531251
catch: bad_request
12541252
rollup.rollup_search:
@@ -1264,9 +1262,7 @@ setup:
12641262

12651263
---
12661264
"Search error against rollup and live index":
1267-
- skip:
1268-
version: "all"
1269-
reason: "AwaitsFix: https://github.com/elastic/elasticsearch/issues/41970"
1265+
12701266
- do:
12711267
catch: bad_request
12721268
rollup.rollup_search:

0 commit comments

Comments
 (0)