Skip to content

Commit 52580b5

Browse files
authored
SQL: Fix parsing of dates with milliseconds (#30419)
Dates internally contain milliseconds (which appear when converting them to Strings) however parsing does not accept them (and is being strict). The parser has been changed so that Date is mandatory but the time (including its fractions such as millis) are optional. Fix #30002
1 parent 519768b commit 52580b5

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

docs/CHANGELOG.asciidoc

+8
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ Rollup::
115115
* Validate timezone in range queries to ensure they match the selected job when
116116
searching ({pull}30338[#30338])
117117

118+
SQL::
119+
* Fix parsing of Dates containing milliseconds ({pull}30419[#30419])
120+
118121
[float]
119122
=== Regressions
120123
Fail snapshot operations early when creating or deleting a snapshot on a repository that has been
@@ -201,6 +204,8 @@ Rollup::
201204
* Validate timezone in range queries to ensure they match the selected job when
202205
searching ({pull}30338[#30338])
203206

207+
SQL::
208+
* Fix parsing of Dates containing milliseconds ({pull}30419[#30419])
204209

205210
Allocation::
206211

@@ -241,6 +246,9 @@ Reduce the number of object allocations made by {security} when resolving the in
241246

242247
Respect accept header on requests with no handler ({pull}30383[#30383])
243248

249+
SQL::
250+
* Fix parsing of Dates containing milliseconds ({pull}30419[#30419])
251+
244252
//[float]
245253
//=== Regressions
246254

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/type/DataTypeConversion.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232
public abstract class DataTypeConversion {
3333

34-
private static final DateTimeFormatter UTC_DATE_FORMATTER = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC();
34+
private static final DateTimeFormatter UTC_DATE_FORMATTER = ISODateTimeFormat.dateOptionalTimeParser().withZoneUTC();
3535

3636
/**
3737
* Returns the type compatible with both left and right types

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/type/DataTypeConversionTests.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,15 @@ public void testConversionToDate() {
8282
Conversion conversion = DataTypeConversion.conversionFor(DataType.KEYWORD, to);
8383
assertNull(conversion.convert(null));
8484

85-
// TODO we'd like to be able to optionally parse millis here I think....
8685
assertEquals(new DateTime(1000L, DateTimeZone.UTC), conversion.convert("1970-01-01T00:00:01Z"));
8786
assertEquals(new DateTime(1483228800000L, DateTimeZone.UTC), conversion.convert("2017-01-01T00:00:00Z"));
8887
assertEquals(new DateTime(18000000L, DateTimeZone.UTC), conversion.convert("1970-01-01T00:00:00-05:00"));
88+
89+
// double check back and forth conversion
90+
DateTime dt = DateTime.now(DateTimeZone.UTC);
91+
Conversion forward = DataTypeConversion.conversionFor(DataType.DATE, DataType.KEYWORD);
92+
Conversion back = DataTypeConversion.conversionFor(DataType.KEYWORD, DataType.DATE);
93+
assertEquals(dt, back.convert(forward.convert(dt)));
8994
Exception e = expectThrows(SqlIllegalArgumentException.class, () -> conversion.convert("0xff"));
9095
assertEquals("cannot cast [0xff] to [Date]:Invalid format: \"0xff\" is malformed at \"xff\"", e.getMessage());
9196
}

0 commit comments

Comments
 (0)