Skip to content

Commit

Permalink
Improve DateTime error handling and add some bad date tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smalyshev committed Sep 11, 2024
1 parent 4ce661c commit 79e2651
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 79e2651

Please sign in to comment.