1717import org .elasticsearch .xpack .esql .core .type .DataType ;
1818import org .elasticsearch .xpack .esql .expression .function .TestCaseSupplier ;
1919import org .elasticsearch .xpack .esql .expression .function .scalar .AbstractConfigurationFunctionTestCase ;
20+ import org .elasticsearch .xpack .esql .expression .function .scalar .date .matchers .DateMillisMatcher ;
21+ import org .elasticsearch .xpack .esql .expression .function .scalar .date .matchers .DateNanosMatcher ;
2022import org .elasticsearch .xpack .esql .session .Configuration ;
2123import org .hamcrest .BaseMatcher ;
2224import org .hamcrest .Matchers ;
@@ -62,10 +64,6 @@ public ZoneId zoneId() {
6264 public long inputDateAsMillis () {
6365 return Instant .parse (inputDate ).toEpochMilli ();
6466 }
65-
66- public long expectedDateAsMillis () {
67- return Instant .parse (expectedDate ).toEpochMilli ();
68- }
6967 }
7068
7169 public record DurationTestCaseData (Duration duration , String inputDate , @ Nullable String zoneIdString , String expectedDate ) {
@@ -76,10 +74,6 @@ public ZoneId zoneId() {
7674 public long inputDateAsMillis () {
7775 return Instant .parse (inputDate ).toEpochMilli ();
7876 }
79-
80- public long expectedDateAsMillis () {
81- return Instant .parse (expectedDate ).toEpochMilli ();
82- }
8377 }
8478
8579 public static List <DurationTestCaseData > makeTruncDurationTestCases () {
@@ -192,7 +186,7 @@ private static List<TestCaseSupplier> ofDatePeriod(PeriodTestCaseData data) {
192186 ),
193187 Matchers .startsWith ("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding[" ),
194188 DataType .DATETIME ,
195- new DateMillisMatcher (data .expectedDate ()) // equalTo(data.expectedDateAsMillis())
189+ new DateMillisMatcher (data .expectedDate ())
196190 ).withConfiguration (TEST_SOURCE , configurationForTimezone (data .zoneId ()))
197191 ),
198192 new TestCaseSupplier (
@@ -205,7 +199,7 @@ private static List<TestCaseSupplier> ofDatePeriod(PeriodTestCaseData data) {
205199 ),
206200 Matchers .startsWith ("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding[" ),
207201 DataType .DATE_NANOS ,
208- new DateNanosMatcher (data .expectedDate ()) // equalTo(toNanos(data.expectedDate()))
202+ new DateNanosMatcher (data .expectedDate ())
209203 ).withConfiguration (TEST_SOURCE , configurationForTimezone (data .zoneId ()))
210204 )
211205 );
@@ -223,7 +217,7 @@ private static List<TestCaseSupplier> ofDuration(DurationTestCaseData data) {
223217 ),
224218 Matchers .startsWith ("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding[" ),
225219 DataType .DATETIME ,
226- new DateMillisMatcher (data .expectedDate ()) // equalTo(data.expectedDateAsMillis())
220+ new DateMillisMatcher (data .expectedDate ())
227221 ).withConfiguration (TEST_SOURCE , configurationForTimezone (data .zoneId ()))
228222 ),
229223 new TestCaseSupplier (
@@ -236,7 +230,7 @@ private static List<TestCaseSupplier> ofDuration(DurationTestCaseData data) {
236230 ),
237231 Matchers .startsWith ("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding[" ),
238232 DataType .DATE_NANOS ,
239- new DateNanosMatcher (data .expectedDate ()) // equalTo(toNanos(data.expectedDate()))
233+ new DateNanosMatcher (data .expectedDate ())
240234 ).withConfiguration (TEST_SOURCE , configurationForTimezone (data .zoneId ()))
241235 )
242236 );
@@ -275,68 +269,8 @@ private static long toMillis(String timestamp) {
275269 return Instant .parse (timestamp ).toEpochMilli ();
276270 }
277271
278- private static long toNanos (String timestamp ) {
279- return DateUtils .toLong (Instant .parse (timestamp ));
280- }
281-
282272 @ Override
283273 protected Expression buildWithConfiguration (Source source , List <Expression > args , Configuration configuration ) {
284274 return new DateTrunc (source , args .get (0 ), args .get (1 ), configuration );
285275 }
286-
287- public static class DateMillisMatcher extends BaseMatcher <Long > {
288- private final long timeMillis ;
289-
290- public DateMillisMatcher (String date ) {
291- this .timeMillis = toMillis (date );
292- }
293-
294- @ Override
295- public boolean matches (Object item ) {
296- return item instanceof Long && timeMillis == (Long ) item ;
297- }
298-
299- @ Override
300- public void describeMismatch (Object item , org .hamcrest .Description description ) {
301- description .appendText ("was " );
302- if (item instanceof Long l ) {
303- description .appendValue (DEFAULT_DATE_TIME_FORMATTER .formatMillis (l ));
304- } else {
305- description .appendValue (item );
306- }
307- }
308-
309- @ Override
310- public void describeTo (org .hamcrest .Description description ) {
311- description .appendText (DEFAULT_DATE_TIME_FORMATTER .formatMillis (timeMillis ));
312- }
313- }
314-
315- public static class DateNanosMatcher extends BaseMatcher <Long > {
316- private final long timeNanos ;
317-
318- public DateNanosMatcher (String date ) {
319- this .timeNanos = toNanos (date );
320- }
321-
322- @ Override
323- public boolean matches (Object item ) {
324- return item instanceof Long && timeNanos == (Long ) item ;
325- }
326-
327- @ Override
328- public void describeMismatch (Object item , org .hamcrest .Description description ) {
329- description .appendText ("was " );
330- if (item instanceof Long l ) {
331- description .appendValue (DEFAULT_DATE_TIME_FORMATTER .formatNanos (l ));
332- } else {
333- description .appendValue (item );
334- }
335- }
336-
337- @ Override
338- public void describeTo (org .hamcrest .Description description ) {
339- description .appendText (DEFAULT_DATE_TIME_FORMATTER .formatNanos (timeNanos ));
340- }
341- }
342276}
0 commit comments