Skip to content

Commit

Permalink
Fix issue with assertion of DATE types
Browse files Browse the repository at this point in the history
  • Loading branch information
matriv committed Sep 8, 2019
1 parent eabbe2e commit 4b119dc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}
}
18 changes: 9 additions & 9 deletions x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions x-pack/plugin/sql/qa/src/main/resources/docs/docs.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -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
;

Expand Down

0 comments on commit 4b119dc

Please sign in to comment.