Skip to content

Commit cee50b5

Browse files
committed
Fixes date range query using epoch with timezone (#21542)
This change fixes the rnage query so that an exception is always thrown if the range query uses epoch time together with a time zone. Since epoch time is always UTC it should not be used with a time zone. Closes #21501
1 parent 6815b8a commit cee50b5

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

core/src/main/java/org/elasticsearch/index/mapper/DateFieldMapper.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,6 @@ public Relation isFieldWithinQuery(IndexReader reader,
321321
dateParser = this.dateMathParser;
322322
}
323323

324-
if (PointValues.size(reader, name()) == 0) {
325-
// no points, so nothing matches
326-
return Relation.DISJOINT;
327-
}
328-
329-
long minValue = LongPoint.decodeDimension(PointValues.getMinPackedValue(reader, name()), 0);
330-
long maxValue = LongPoint.decodeDimension(PointValues.getMaxPackedValue(reader, name()), 0);
331-
332324
long fromInclusive = Long.MIN_VALUE;
333325
if (from != null) {
334326
fromInclusive = parseToMilliseconds(from, !includeLower, timeZone, dateParser, context);
@@ -351,6 +343,17 @@ public Relation isFieldWithinQuery(IndexReader reader,
351343
}
352344
}
353345

346+
// This check needs to be done after fromInclusive and toInclusive
347+
// are resolved so we can throw an exception if they are invalid
348+
// even if there are no points in the shard
349+
if (PointValues.size(reader, name()) == 0) {
350+
// no points, so nothing matches
351+
return Relation.DISJOINT;
352+
}
353+
354+
long minValue = LongPoint.decodeDimension(PointValues.getMinPackedValue(reader, name()), 0);
355+
long maxValue = LongPoint.decodeDimension(PointValues.getMaxPackedValue(reader, name()), 0);
356+
354357
if (minValue >= fromInclusive && maxValue <= toInclusive) {
355358
return Relation.WITHIN;
356359
} else if (maxValue < fromInclusive || minValue > toInclusive) {

core/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ public void modify(MappedFieldType ft) {
7373
}
7474

7575
public void testIsFieldWithinQueryEmptyReader() throws IOException {
76+
QueryRewriteContext context = new QueryRewriteContext(null, null, null, null, null, null, null, () -> nowInMillis);
7677
IndexReader reader = new MultiReader();
7778
DateFieldType ft = new DateFieldType();
7879
ft.setName("my_date");
7980
assertEquals(Relation.DISJOINT, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03",
80-
randomBoolean(), randomBoolean(), null, null, null));
81+
randomBoolean(), randomBoolean(), null, null, context));
8182
}
8283

8384
private void doTestIsFieldWithinQuery(DateFieldType ft, DirectoryReader reader,
@@ -128,7 +129,9 @@ public void testIsFieldWithinQuery() throws IOException {
128129
// Fields with no value indexed.
129130
DateFieldType ft2 = new DateFieldType();
130131
ft2.setName("my_date2");
131-
assertEquals(Relation.DISJOINT, ft2.isFieldWithinQuery(reader, "2015-10-09", "2016-01-02", false, false, null, null, null));
132+
133+
QueryRewriteContext context = new QueryRewriteContext(null, null, null, null, null, null, null, () -> nowInMillis);
134+
assertEquals(Relation.DISJOINT, ft2.isFieldWithinQuery(reader, "2015-10-09", "2016-01-02", false, false, null, null, context));
132135
IOUtils.close(reader, w, dir);
133136
}
134137

core/src/test/java/org/elasticsearch/search/query/SearchQueryIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,6 @@ public void testDateProvidedAsNumber() throws ExecutionException, InterruptedExc
19041904
assertHitCount(client().prepareSearch("test").setSize(0).setQuery(rangeQuery("field").lte(-999999999999L)).get(), 3);
19051905
}
19061906

1907-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/21501")
19081907
public void testRangeQueryWithTimeZone() throws Exception {
19091908
assertAcked(prepareCreate("test")
19101909
.addMapping("type1", "date", "type=date", "num", "type=integer"));

0 commit comments

Comments
 (0)