From 28d10e615ada63386bfbdde6eebd4d122018350d Mon Sep 17 00:00:00 2001 From: Moritz Mack Date: Thu, 8 May 2025 10:04:57 +0200 Subject: [PATCH 1/2] Fix test to use date-time strings at corresponding precision for from and to. Fixes #86284 --- .../index/mapper/RangeFieldTypeTests.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java index 1a5f3fbcda6bb..527d62867ce4f 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java @@ -41,6 +41,8 @@ import java.time.Instant; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.ChronoUnit; import java.util.Collections; import java.util.List; import java.util.Map; @@ -86,10 +88,21 @@ public void testRangeQuery() throws Exception { ); } + private static String dateTimeString(long epochMillis, int increment, ChronoUnit precision) { + var dateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMillis), ZoneOffset.UTC) + .plus(increment, precision) + .truncatedTo(precision); + return switch (precision) { + case MILLIS -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(dateTime); + case SECONDS -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dateTime); + case MINUTES -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'").format(dateTime); + default -> dateTime.toString(); + }; + } + /** * test the queries are correct if from/to are adjacent and the range is exclusive of those values */ - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86284") public void testRangeQueryIntersectsAdjacentValues() throws Exception { SearchExecutionContext context = createContext(); ShapeRelation relation = randomFrom(ShapeRelation.values()); @@ -105,8 +118,9 @@ public void testRangeQueryIntersectsAdjacentValues() throws Exception { } case DATE -> { long fromValue = randomInt(); - from = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue), ZoneOffset.UTC); - to = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue + 1), ZoneOffset.UTC); + var precision = randomFrom(ChronoUnit.MILLIS, ChronoUnit.SECONDS, ChronoUnit.MINUTES); + from = dateTimeString(fromValue, 0, precision); + to = dateTimeString(fromValue, 1, precision); } case INTEGER -> { int fromValue = randomInt(); @@ -143,7 +157,6 @@ public void testRangeQueryIntersectsAdjacentValues() throws Exception { /** * check that we catch cases where the user specifies larger "from" than "to" value, not counting the include upper/lower settings */ - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86284") public void testFromLargerToErrors() throws Exception { SearchExecutionContext context = createContext(); RangeFieldType ft = createDefaultFieldType(); @@ -159,8 +172,9 @@ public void testFromLargerToErrors() throws Exception { } case DATE: { long fromValue = randomInt(); - from = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue), ZoneOffset.UTC); - to = ZonedDateTime.ofInstant(Instant.ofEpochMilli(fromValue - 1), ZoneOffset.UTC); + var precision = randomFrom(ChronoUnit.MILLIS, ChronoUnit.SECONDS, ChronoUnit.MINUTES); + from = dateTimeString(fromValue, 0, precision); + to = dateTimeString(fromValue, -1, precision); break; } case INTEGER: { From 17f4beff18759c820fdfcbb765a59502db61eafd Mon Sep 17 00:00:00 2001 From: Moritz Mack Date: Thu, 8 May 2025 14:37:00 +0200 Subject: [PATCH 2/2] fix checks --- .../elasticsearch/index/mapper/RangeFieldTypeTests.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java index 527d62867ce4f..0492dd1fcc7ce 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/RangeFieldTypeTests.java @@ -45,6 +45,7 @@ import java.time.temporal.ChronoUnit; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Map; import static org.hamcrest.Matchers.containsString; @@ -93,9 +94,9 @@ private static String dateTimeString(long epochMillis, int increment, ChronoUnit .plus(increment, precision) .truncatedTo(precision); return switch (precision) { - case MILLIS -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").format(dateTime); - case SECONDS -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").format(dateTime); - case MINUTES -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'").format(dateTime); + case MILLIS -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ROOT).format(dateTime); + case SECONDS -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ROOT).format(dateTime); + case MINUTES -> DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm'Z'", Locale.ROOT).format(dateTime); default -> dateTime.toString(); }; }