From 79e26510a91a640d4645421aa14f47c514747620 Mon Sep 17 00:00:00 2001 From: Stas Malyshev Date: Tue, 10 Sep 2024 16:54:56 -0600 Subject: [PATCH] Improve DateTime error handling and add some bad date tests --- .../common/time/JavaDateMathParser.java | 4 +-- .../index/mapper/DateFieldTypeTests.java | 32 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java b/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java index f48e4725af956..62dc65ce0ab19 100644 --- a/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java +++ b/server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java @@ -11,13 +11,13 @@ import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.common.Strings; +import java.time.DateTimeException; import java.time.DayOfWeek; import java.time.Instant; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; -import java.time.format.DateTimeParseException; import java.time.temporal.ChronoField; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAdjusters; @@ -219,7 +219,7 @@ private Instant parseDateTime(String value, ZoneId timeZone, boolean roundUpIfNo return DateFormatters.from(accessor).withZoneSameLocal(timeZone).toInstant(); } - } catch (IllegalArgumentException | DateTimeParseException e) { + } catch (IllegalArgumentException | DateTimeException e) { throw new ElasticsearchParseException( "failed to parse date field [{}] with format [{}]: [{}]", e, diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java index c06fe5d8a89d2..46ebb7fccbbf8 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DateFieldTypeTests.java @@ -21,6 +21,7 @@ import org.apache.lucene.search.IndexSortSortedNumericDocValuesRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.store.Directory; +import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.cluster.metadata.IndexMetadata; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.time.DateFormatter; @@ -142,6 +143,37 @@ private void doTestIsFieldWithinQuery(DateFieldType ft, DirectoryReader reader, assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", false, false, zone, null, context)); assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", false, true, zone, null, context)); assertEquals(Relation.INTERSECTS, ft.isFieldWithinQuery(reader, "2015-10-12", "2016-04-03", true, false, zone, null, context)); + // Bad dates + assertThrows( + ElasticsearchParseException.class, + () -> ft.isFieldWithinQuery(reader, "2015-00-01", "2016-04-03", randomBoolean(), randomBoolean(), zone, null, context) + ); + assertThrows( + ElasticsearchParseException.class, + () -> ft.isFieldWithinQuery(reader, "2015-01-01", "2016-04-00", randomBoolean(), randomBoolean(), zone, null, context) + ); + assertThrows( + ElasticsearchParseException.class, + () -> ft.isFieldWithinQuery(reader, "2015-22-01", "2016-04-00", randomBoolean(), randomBoolean(), zone, null, context) + ); + assertThrows( + ElasticsearchParseException.class, + () -> ft.isFieldWithinQuery(reader, "2015-01-01", "2016-04-45", randomBoolean(), randomBoolean(), zone, null, context) + ); + assertThrows( + ElasticsearchParseException.class, + () -> ft.isFieldWithinQuery(reader, "2015-01-01", "2016-04-01T25:00:00", randomBoolean(), randomBoolean(), zone, null, context) + ); + if (ft.resolution().equals(Resolution.NANOSECONDS)) { + assertThrows( + IllegalArgumentException.class, + () -> ft.isFieldWithinQuery(reader, "-2016-04-01", "2016-04-01", randomBoolean(), randomBoolean(), zone, null, context) + ); + assertThrows( + IllegalArgumentException.class, + () -> ft.isFieldWithinQuery(reader, "9223372036854775807", "2016-04-01", randomBoolean(), randomBoolean(), zone, null, context) + ); + } } public void testValueFormat() {