Skip to content

Commit b50164f

Browse files
authored
Support optional parsers in any order with DateMathParser and roundup (#46654)
Currently DateMathParser with roundUp = true is relying on the DateFormatter build with combined optional sub parsers with defaulted fields (depending on the formatter). That means that for yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS Java.time implementation expects optional parsers in order from most specific to least specific (reverse in the example above). It is causing a problem because the first parsing succeeds but does not consume the full input. The second parser should be used. We can work around this with keeping a list of RoundUpParsers and iterate over them choosing the one that parsed full input. The same approach we used for regular (non date math) in relates #40100 The jdk is not considering this to be a bug https://bugs.openjdk.java.net/browse/JDK-8188771 Those below will expect this change first relates #46242 relates #45284
1 parent 237b238 commit b50164f

File tree

8 files changed

+147
-59
lines changed

8 files changed

+147
-59
lines changed

server/src/main/java/org/elasticsearch/common/time/DateFormatter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,6 @@ static DateFormatter forPattern(String input) {
149149
return formatters.get(0);
150150
}
151151

152-
return DateFormatters.merge(input, formatters);
152+
return JavaDateFormatter.combined(input, formatters);
153153
}
154154
}

server/src/main/java/org/elasticsearch/common/time/DateFormatters.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import java.time.temporal.TemporalAdjusters;
4141
import java.time.temporal.TemporalQueries;
4242
import java.time.temporal.WeekFields;
43-
import java.util.ArrayList;
44-
import java.util.List;
4543

4644
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
4745
import static java.time.temporal.ChronoField.DAY_OF_WEEK;
@@ -1045,7 +1043,7 @@ public class DateFormatters {
10451043
new DateTimeFormatterBuilder().appendValue(WeekFields.ISO.weekBasedYear()).toFormatter(IsoLocale.ROOT));
10461044

10471045
/*
1048-
* Returns a formatter for a four digit weekyear. (uuuu)
1046+
* Returns a formatter for a four digit year. (uuuu)
10491047
*/
10501048
private static final DateFormatter YEAR = new JavaDateFormatter("year",
10511049
new DateTimeFormatterBuilder().appendValue(ChronoField.YEAR).toFormatter(IsoLocale.ROOT));
@@ -1440,7 +1438,7 @@ public class DateFormatters {
14401438
.appendValue(WeekFields.ISO.dayOfWeek())
14411439
.toFormatter(IsoLocale.ROOT)
14421440
);
1443-
1441+
14441442

14451443
/////////////////////////////////////////
14461444
//
@@ -1628,26 +1626,7 @@ static DateFormatter forPattern(String input) {
16281626
}
16291627
}
16301628

1631-
static JavaDateFormatter merge(String pattern, List<DateFormatter> formatters) {
1632-
assert formatters.size() > 0;
1633-
1634-
List<DateTimeFormatter> dateTimeFormatters = new ArrayList<>(formatters.size());
1635-
DateTimeFormatterBuilder roundupBuilder = new DateTimeFormatterBuilder();
1636-
DateTimeFormatter printer = null;
1637-
for (DateFormatter formatter : formatters) {
1638-
assert formatter instanceof JavaDateFormatter;
1639-
JavaDateFormatter javaDateFormatter = (JavaDateFormatter) formatter;
1640-
if (printer == null) {
1641-
printer = javaDateFormatter.getPrinter();
1642-
}
1643-
dateTimeFormatters.addAll(javaDateFormatter.getParsers());
1644-
roundupBuilder.appendOptional(javaDateFormatter.getRoundupParser());
1645-
}
1646-
DateTimeFormatter roundUpParser = roundupBuilder.toFormatter(IsoLocale.ROOT);
16471629

1648-
return new JavaDateFormatter(pattern, printer, builder -> builder.append(roundUpParser),
1649-
dateTimeFormatters.toArray(new DateTimeFormatter[0]));
1650-
}
16511630

16521631
private static final LocalDate LOCALDATE_EPOCH = LocalDate.of(1970, 1, 1);
16531632

server/src/main/java/org/elasticsearch/common/time/JavaDateFormatter.java

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.time.temporal.ChronoField;
3030
import java.time.temporal.TemporalAccessor;
3131
import java.time.temporal.TemporalField;
32+
import java.util.ArrayList;
3233
import java.util.Arrays;
3334
import java.util.Collection;
3435
import java.util.Collections;
@@ -57,19 +58,33 @@ class JavaDateFormatter implements DateFormatter {
5758
private final String format;
5859
private final DateTimeFormatter printer;
5960
private final List<DateTimeFormatter> parsers;
60-
private final DateTimeFormatter roundupParser;
61+
private final JavaDateFormatter roundupParser;
6162

62-
private JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter roundupParser, List<DateTimeFormatter> parsers) {
63-
this.format = format;
64-
this.printer = printer;
65-
this.roundupParser = roundupParser;
66-
this.parsers = parsers;
63+
static class RoundUpFormatter extends JavaDateFormatter{
64+
65+
RoundUpFormatter(String format, List<DateTimeFormatter> roundUpParsers) {
66+
super(format, firstFrom(roundUpParsers),null, roundUpParsers);
67+
}
68+
69+
private static DateTimeFormatter firstFrom(List<DateTimeFormatter> roundUpParsers) {
70+
return roundUpParsers.get(0);
71+
}
72+
73+
@Override
74+
JavaDateFormatter getRoundupParser() {
75+
throw new UnsupportedOperationException("RoundUpFormatter does not have another roundUpFormatter");
76+
}
6777
}
78+
79+
// named formatters use default roundUpParser
6880
JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeFormatter... parsers) {
6981
this(format, printer, builder -> ROUND_UP_BASE_FIELDS.forEach(builder::parseDefaulting), parsers);
7082
}
7183

72-
JavaDateFormatter(String format, DateTimeFormatter printer, Consumer<DateTimeFormatterBuilder> roundupParserConsumer,
84+
// subclasses override roundUpParser
85+
JavaDateFormatter(String format,
86+
DateTimeFormatter printer,
87+
Consumer<DateTimeFormatterBuilder> roundupParserConsumer,
7388
DateTimeFormatter... parsers) {
7489
if (printer == null) {
7590
throw new IllegalArgumentException("printer may not be null");
@@ -90,20 +105,51 @@ private JavaDateFormatter(String format, DateTimeFormatter printer, DateTimeForm
90105
} else {
91106
this.parsers = Arrays.asList(parsers);
92107
}
108+
//this is when the RoundUp Formatter is created. In further merges (with ||) it will only append this one to a list.
109+
List<DateTimeFormatter> roundUp = createRoundUpParser(format, roundupParserConsumer);
110+
this.roundupParser = new RoundUpFormatter(format, roundUp) ;
111+
}
93112

94-
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
113+
private List<DateTimeFormatter> createRoundUpParser(String format,
114+
Consumer<DateTimeFormatterBuilder> roundupParserConsumer) {
95115
if (format.contains("||") == false) {
116+
DateTimeFormatterBuilder builder = new DateTimeFormatterBuilder();
96117
builder.append(this.parsers.get(0));
118+
roundupParserConsumer.accept(builder);
119+
return Arrays.asList(builder.toFormatter(locale()));
97120
}
98-
roundupParserConsumer.accept(builder);
99-
DateTimeFormatter roundupFormatter = builder.toFormatter(locale());
100-
if (printer.getZone() != null) {
101-
roundupFormatter = roundupFormatter.withZone(zone());
121+
return null;
122+
}
123+
124+
public static DateFormatter combined(String input, List<DateFormatter> formatters) {
125+
assert formatters.size() > 0;
126+
127+
List<DateTimeFormatter> parsers = new ArrayList<>(formatters.size());
128+
List<DateTimeFormatter> roundUpParsers = new ArrayList<>(formatters.size());
129+
130+
DateTimeFormatter printer = null;
131+
for (DateFormatter formatter : formatters) {
132+
assert formatter instanceof JavaDateFormatter;
133+
JavaDateFormatter javaDateFormatter = (JavaDateFormatter) formatter;
134+
if (printer == null) {
135+
printer = javaDateFormatter.getPrinter();
136+
}
137+
parsers.addAll(javaDateFormatter.getParsers());
138+
roundUpParsers.addAll(javaDateFormatter.getRoundupParser().getParsers());
102139
}
103-
this.roundupParser = roundupFormatter;
140+
141+
return new JavaDateFormatter(input, printer, roundUpParsers, parsers);
142+
}
143+
144+
private JavaDateFormatter(String format, DateTimeFormatter printer, List<DateTimeFormatter> roundUpParsers,
145+
List<DateTimeFormatter> parsers) {
146+
this.format = format;
147+
this.printer = printer;
148+
this.roundupParser = roundUpParsers != null ? new RoundUpFormatter(format, roundUpParsers ) : null;
149+
this.parsers = parsers;
104150
}
105151

106-
DateTimeFormatter getRoundupParser() {
152+
JavaDateFormatter getRoundupParser() {
107153
return roundupParser;
108154
}
109155

@@ -162,8 +208,12 @@ public DateFormatter withZone(ZoneId zoneId) {
162208
if (zoneId.equals(zone())) {
163209
return this;
164210
}
165-
return new JavaDateFormatter(format, printer.withZone(zoneId), getRoundupParser().withZone(zoneId),
166-
parsers.stream().map(p -> p.withZone(zoneId)).collect(Collectors.toList()));
211+
List<DateTimeFormatter> parsers = this.parsers.stream().map(p -> p.withZone(zoneId)).collect(Collectors.toList());
212+
List<DateTimeFormatter> roundUpParsers = this.roundupParser.getParsers()
213+
.stream()
214+
.map(p -> p.withZone(zoneId))
215+
.collect(Collectors.toList());
216+
return new JavaDateFormatter(format, printer.withZone(zoneId), roundUpParsers, parsers);
167217
}
168218

169219
@Override
@@ -172,8 +222,12 @@ public DateFormatter withLocale(Locale locale) {
172222
if (locale.equals(locale())) {
173223
return this;
174224
}
175-
return new JavaDateFormatter(format, printer.withLocale(locale), getRoundupParser().withLocale(locale),
176-
parsers.stream().map(p -> p.withLocale(locale)).collect(Collectors.toList()));
225+
List<DateTimeFormatter> parsers = this.parsers.stream().map(p -> p.withLocale(locale)).collect(Collectors.toList());
226+
List<DateTimeFormatter> roundUpParsers = this.roundupParser.getParsers()
227+
.stream()
228+
.map(p -> p.withLocale(locale))
229+
.collect(Collectors.toList());
230+
return new JavaDateFormatter(format, printer.withLocale(locale), roundUpParsers, parsers);
177231
}
178232

179233
@Override

server/src/main/java/org/elasticsearch/common/time/JavaDateMathParser.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@
2828
import java.time.ZoneId;
2929
import java.time.ZoneOffset;
3030
import java.time.ZonedDateTime;
31-
import java.time.format.DateTimeFormatter;
3231
import java.time.format.DateTimeParseException;
3332
import java.time.temporal.ChronoField;
3433
import java.time.temporal.TemporalAccessor;
3534
import java.time.temporal.TemporalAdjusters;
3635
import java.time.temporal.TemporalQueries;
3736
import java.util.Objects;
38-
import java.util.function.Function;
3937
import java.util.function.LongSupplier;
4038

4139
/**
@@ -48,14 +46,14 @@
4846
public class JavaDateMathParser implements DateMathParser {
4947

5048
private final JavaDateFormatter formatter;
51-
private final DateTimeFormatter roundUpFormatter;
5249
private final String format;
50+
private final JavaDateFormatter roundupParser;
5351

54-
JavaDateMathParser(String format, JavaDateFormatter formatter, DateTimeFormatter roundUpFormatter) {
52+
JavaDateMathParser(String format, JavaDateFormatter formatter, JavaDateFormatter roundupParser) {
5553
this.format = format;
54+
this.roundupParser = roundupParser;
5655
Objects.requireNonNull(formatter);
5756
this.formatter = formatter;
58-
this.roundUpFormatter = roundUpFormatter;
5957
}
6058

6159
@Override
@@ -217,12 +215,12 @@ private Instant parseDateTime(String value, ZoneId timeZone, boolean roundUpIfNo
217215
throw new ElasticsearchParseException("cannot parse empty date");
218216
}
219217

220-
Function<String,TemporalAccessor> formatter = roundUpIfNoTime ? this.roundUpFormatter::parse : this.formatter::parse;
218+
DateFormatter formatter = roundUpIfNoTime ? this.roundupParser : this.formatter;
221219
try {
222220
if (timeZone == null) {
223-
return DateFormatters.from(formatter.apply(value)).toInstant();
221+
return DateFormatters.from(formatter.parse(value)).toInstant();
224222
} else {
225-
TemporalAccessor accessor = formatter.apply(value);
223+
TemporalAccessor accessor = formatter.parse(value);
226224
ZoneId zoneId = TemporalQueries.zone().queryFrom(accessor);
227225
if (zoneId != null) {
228226
timeZone = zoneId;

server/src/test/java/org/elasticsearch/common/joda/JavaJodaTimeDuellingTests.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919

2020
package org.elasticsearch.common.joda;
2121

22+
import org.elasticsearch.ElasticsearchParseException;
2223
import org.elasticsearch.bootstrap.JavaVersion;
2324
import org.elasticsearch.common.time.DateFormatter;
2425
import org.elasticsearch.common.time.DateFormatters;
26+
import org.elasticsearch.common.time.DateMathParser;
2527
import org.elasticsearch.test.ESTestCase;
2628
import org.joda.time.DateTime;
2729
import org.joda.time.DateTimeZone;
2830
import org.joda.time.format.ISODateTimeFormat;
2931

3032
import java.time.LocalDateTime;
33+
import java.time.ZoneId;
3134
import java.time.ZoneOffset;
3235
import java.time.ZonedDateTime;
3336
import java.time.format.DateTimeFormatter;
@@ -44,6 +47,35 @@ protected boolean enableWarningsCheck() {
4447
return false;
4548
}
4649

50+
public void testCompositeDateMathParsing(){
51+
//in all these examples the second pattern will be used
52+
assertDateMathEquals("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SSS");
53+
assertDateMathEquals("2014-06-06T12:01:02.123", "strictDateTimeNoMillis||yyyy-MM-dd'T'HH:mm:ss.SSS");
54+
assertDateMathEquals("2014-06-06T12:01:02.123", "yyyy-MM-dd'T'HH:mm:ss+HH:MM||yyyy-MM-dd'T'HH:mm:ss.SSS");
55+
}
56+
57+
public void testExceptionWhenCompositeParsingFailsDateMath(){
58+
//both parsing failures should contain pattern and input text in exception
59+
//both patterns fail parsing the input text due to only 2 digits of millis. Hence full text was not parsed.
60+
String pattern = "yyyy-MM-dd'T'HH:mm:ss||yyyy-MM-dd'T'HH:mm:ss.SS";
61+
String text = "2014-06-06T12:01:02.123";
62+
ElasticsearchParseException e1 = expectThrows(ElasticsearchParseException.class,
63+
() -> dateMathToMillis(text, DateFormatter.forPattern(pattern)));
64+
assertThat(e1.getMessage(), containsString(pattern));
65+
assertThat(e1.getMessage(), containsString(text));
66+
67+
ElasticsearchParseException e2 = expectThrows(ElasticsearchParseException.class,
68+
() -> dateMathToMillis(text, Joda.forPattern(pattern)));
69+
assertThat(e2.getMessage(), containsString(pattern));
70+
assertThat(e2.getMessage(), containsString(text));
71+
}
72+
73+
private long dateMathToMillis(String text, DateFormatter dateFormatter) {
74+
DateFormatter javaFormatter = dateFormatter.withLocale(randomLocale(random()));
75+
DateMathParser javaDateMath = javaFormatter.toDateMathParser();
76+
return javaDateMath.parse(text, () -> 0, true, (ZoneId) null).toEpochMilli();
77+
}
78+
4779
public void testDayOfWeek() {
4880
//7 (ok joda) vs 1 (java by default) but 7 with customized org.elasticsearch.common.time.IsoLocale.ISO8601
4981
ZonedDateTime now = LocalDateTime.of(2009,11,15,1,32,8,328402)
@@ -851,4 +883,11 @@ private void assertJavaTimeParseException(String input, String format) {
851883
assertThat(e.getMessage(), containsString(input));
852884
assertThat(e.getMessage(), containsString(format));
853885
}
886+
887+
private void assertDateMathEquals(String text, String pattern) {
888+
long gotMillisJava = dateMathToMillis(text, DateFormatter.forPattern(pattern));
889+
long gotMillisJoda = dateMathToMillis(text, Joda.forPattern(pattern));
890+
891+
assertEquals(gotMillisJoda, gotMillisJava);
892+
}
854893
}

server/src/test/java/org/elasticsearch/common/joda/JodaDateMathParserTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.time.Instant;
2929
import java.time.ZoneId;
30+
import java.time.ZoneOffset;
3031
import java.util.concurrent.atomic.AtomicBoolean;
3132
import java.util.function.LongSupplier;
3233

@@ -59,6 +60,19 @@ void assertDateEquals(long gotMillis, String original, String expected) {
5960
}
6061
}
6162

63+
public void testOverridingLocaleOrZoneAndCompositeRoundUpParser() {
64+
//the pattern has to be composite and the match should not be on the first one
65+
DateFormatter formatter = Joda.forPattern("date||epoch_millis").withLocale(randomLocale(random()));
66+
DateMathParser parser = formatter.toDateMathParser();
67+
long gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli();
68+
assertDateEquals(gotMillis, "297276785531", "297276785531");
69+
70+
formatter = Joda.forPattern("date||epoch_millis").withZone(ZoneOffset.UTC);
71+
parser = formatter.toDateMathParser();
72+
gotMillis = parser.parse("297276785531", () -> 0, true, (ZoneId) null).toEpochMilli();
73+
assertDateEquals(gotMillis, "297276785531", "297276785531");
74+
}
75+
6276
public void testBasicDates() {
6377
assertDateMathEquals("2014", "2014-01-01T00:00:00.000");
6478
assertDateMathEquals("2014-05", "2014-05-01T00:00:00.000");

0 commit comments

Comments
 (0)