From 4b119dc99bc374e40186f043a26f90434e866d13 Mon Sep 17 00:00:00 2001 From: Marios Trivyzas Date: Mon, 9 Sep 2019 00:48:04 +0300 Subject: [PATCH] Fix issue with assertion of DATE types --- .../xpack/sql/qa/jdbc/JdbcAssert.java | 15 ++++++++++---- .../xpack/sql/qa/jdbc/JdbcTestUtils.java | 5 +++++ .../qa/src/main/resources/datetime.csv-spec | 18 ++++++++--------- .../qa/src/main/resources/docs/docs.csv-spec | 20 +++++++++---------- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcAssert.java b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcAssert.java index ec9386d2d6e12..37ca10c9988d0 100644 --- a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcAssert.java +++ b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcAssert.java @@ -37,6 +37,9 @@ import static java.sql.Types.REAL; import static java.sql.Types.SMALLINT; import static java.sql.Types.TINYINT; +import static org.elasticsearch.xpack.sql.qa.jdbc.JdbcTestUtils.convertDateToSystemTimezone; +import static org.elasticsearch.xpack.sql.qa.jdbc.JdbcTestUtils.logResultSetMetadata; +import static org.elasticsearch.xpack.sql.qa.jdbc.JdbcTestUtils.resultSetCurrentData; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.instanceOf; import static org.junit.Assert.assertEquals; @@ -107,7 +110,7 @@ public static void assertResultSetMetadata(ResultSet expected, ResultSet actual, ResultSetMetaData actualMeta = actual.getMetaData(); if (logger != null) { - JdbcTestUtils.logResultSetMetadata(actual, logger); + logResultSetMetadata(actual, logger); } if (expectedMeta.getColumnCount() != actualMeta.getColumnCount()) { @@ -210,7 +213,7 @@ private static void doAssertResultSetData(ResultSet expected, ResultSet actual, assertTrue("Expected more data but no more entries found after [" + count + "]", actual.next()); if (logger != null) { - logger.info(JdbcTestUtils.resultSetCurrentData(actual)); + logger.info(resultSetCurrentData(actual)); } for (int column = 1; column <= columns; column++) { @@ -264,6 +267,10 @@ private static void doAssertResultSetData(ResultSet expected, ResultSet actual, else if (type == Types.TIMESTAMP || type == Types.TIMESTAMP_WITH_TIMEZONE) { assertEquals(msg, expected.getTimestamp(column), actual.getTimestamp(column)); } + // then date + else if (type == Types.DATE) { + assertEquals(msg, convertDateToSystemTimezone(expected.getDate(column)), actual.getDate(column)); + } // and floats/doubles else if (type == Types.DOUBLE) { assertEquals(msg, (double) expectedObject, (double) actualObject, lenientFloatingNumbers ? 1d : 0.0d); @@ -301,14 +308,14 @@ else if (type == Types.VARCHAR && actualObject instanceof TemporalAmount) { } catch (AssertionError ae) { if (logger != null && actual.next()) { logger.info("^^^ Assertion failure ^^^"); - logger.info(JdbcTestUtils.resultSetCurrentData(actual)); + logger.info(resultSetCurrentData(actual)); } throw ae; } if (actual.next()) { fail("Elasticsearch [" + actual + "] still has data after [" + count + "] entries:\n" - + JdbcTestUtils.resultSetCurrentData(actual)); + + resultSetCurrentData(actual)); } } diff --git a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcTestUtils.java b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcTestUtils.java index 123f22073ae57..d625192e63fce 100644 --- a/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcTestUtils.java +++ b/x-pack/plugin/sql/qa/src/main/java/org/elasticsearch/xpack/sql/qa/jdbc/JdbcTestUtils.java @@ -38,6 +38,7 @@ import java.util.jar.JarInputStream; import java.util.zip.ZipEntry; +import static java.time.ZoneOffset.UTC; import static org.elasticsearch.xpack.sql.action.BasicFormatter.FormatOption.CLI; final class JdbcTestUtils { @@ -240,4 +241,8 @@ static Time asTime(long millis, ZoneId zoneId) { return new Time(ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), zoneId) .toLocalTime().atDate(JdbcTestUtils.EPOCH).atZone(zoneId).toInstant().toEpochMilli()); } + + static Date convertDateToSystemTimezone(Date date) { + return new Date(date.toLocalDate().atStartOfDay(UTC).toInstant().toEpochMilli()); + } } diff --git a/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec b/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec index b6d5905df21ed..217994fea9c77 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec @@ -133,15 +133,15 @@ DATE_TRUNC('mcs', '2019-09-04T11:22:33.123Z'::datetime)::string as dt_micro, DAT ; selectDateTruncWithDate -schema::dt_mil:s|dt_cent:s|dt_dec:s|dt_year:s|dt_quarter:s|dt_month:s|dt_week:s|dt_day:s -SELECT DATE_TRUNC('millennia', '2019-09-04'::date)::string as dt_mil, DATE_TRUNC('century', '2019-09-04'::date)::string as dt_cent, -DATE_TRUNC('decades', '2019-09-04'::date)::string as dt_dec, DATE_TRUNC('year', '2019-09-04'::date)::string as dt_year, -DATE_TRUNC('quarter', '2019-09-04'::date)::string as dt_quarter, DATE_TRUNC('month', '2019-09-04'::date)::string as dt_month, -DATE_TRUNC('week', '2019-09-04'::date)::string as dt_week, DATE_TRUNC('day', '2019-09-04'::date)::string as dt_day; - - dt_mil | dt_cent | dt_dec | dt_year | dt_quarter | dt_month | dt_week | dt_day --------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+--------------------------+------------------------- -2000-01-01T00:00:00.000Z | 2000-01-01T00:00:00.000Z | 2010-01-01T00:00:00.000Z | 2019-01-01T00:00:00.000Z | 2019-07-01T00:00:00.000Z | 2019-09-01T00:00:00.000Z | 2019-09-02T00:00:00.000Z | 2019-09-04T00:00:00.000Z +schema::dt_mil:date|dt_cent:date|dt_dec:date|dt_year:date|dt_quarter:date|dt_month:date|dt_week:date|dt_day:date +SELECT DATE_TRUNC('millennia', '2019-09-04'::date) as dt_mil, DATE_TRUNC('century', '2019-09-04'::date) as dt_cent, +DATE_TRUNC('decades', '2019-09-04'::date) as dt_dec, DATE_TRUNC('year', '2019-09-04'::date) as dt_year, +DATE_TRUNC('quarter', '2019-09-04'::date) as dt_quarter, DATE_TRUNC('month', '2019-09-04'::date) as dt_month, +DATE_TRUNC('week', '2019-09-04'::date) as dt_week, DATE_TRUNC('day', '2019-09-04'::date) as dt_day; + + dt_mil | dt_cent | dt_dec | dt_year | dt_quarter | dt_month | dt_week | dt_day +-----------+------------+------------+------------+-------------+------------+------------+----------- +2000-01-01 | 2000-01-01 | 2010-01-01 | 2019-01-01 | 2019-07-01 | 2019-09-01 | 2019-09-02 | 2019-09-04 ; selectDateTruncWithField diff --git a/x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec b/x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec index 36f55bd8f6169..024cad379ff3a 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec @@ -2444,24 +2444,24 @@ SELECT DATE_TRUNC('mi', '2019-09-04T11:22:33.123Z'::datetime) AS mins; ; truncateDateDecades -schema::decades:s +schema::decades:date // tag::truncateDateDecades -SELECT DATE_TRUNC('decade', CAST('2019-09-04' AS DATE))::string AS decades; +SELECT DATE_TRUNC('decade', CAST('2019-09-04' AS DATE)) AS decades; - decades ------------------------- -2010-01-01T00:00:00.000Z + decades +---------- +2010-01-01 // end::truncateDateDecades ; truncateDateQuarter -schema::quarter:s +schema::quarter:date // tag::truncateDateQuarter -SELECT DATE_TRUNC('quarters', CAST('2019-09-04' AS DATE))::string AS quarter; +SELECT DATE_TRUNC('quarters', CAST('2019-09-04' AS DATE)) AS quarter; - quarter ------------------------- -2019-07-01T00:00:00.000Z + quarter +---------- +2019-07-01 // end::truncateDateQuarter ;