Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL: Fix casting from date to numeric type to use millis #37869

Merged
merged 1 commit into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions x-pack/plugin/sql/qa/src/main/resources/datetime.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,32 @@ SELECT CONVERT(birth_date, DOUBLE) AS date FROM test_emp GROUP BY date ORDER BY
date:d
---------------
null
-5.631552E8
-5.586624E8
-5.56416E8
-5.539104E8
-5.517504E8
-5.492448E8
-5.406912E8
-5.371488E8
-5.359392E8
-5.631552E11
-5.586624E11
-5.56416E11
-5.539104E11
-5.517504E11
-5.492448E11
-5.406912E11
-5.371488E11
-5.359392E11
;

castedDateTimeWithGroupBy2
SELECT CAST(hire_date AS INTEGER) AS date FROM test_emp GROUP BY date ORDER BY date LIMIT 10;
SELECT CAST(hire_date AS LONG) AS date FROM test_emp GROUP BY date ORDER BY date LIMIT 10;

date:i
date:l
---------------
477532800
478051200
484790400
489715200
495763200
498096000
498614400
501206400
501292800
501379200
477532800000
478051200000
484790400000
489715200000
495763200000
498096000000
498614400000
501206400000
501292800000
501379200000
;

dateTimeAggByIsoDayOfWeekWithFilter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ private static Function<Object, Object> fromBool(Function<Boolean, Object> conve
}

private static Function<Object, Object> fromDateTime(Function<Long, Object> converter) {
return l -> converter.apply(((ZonedDateTime) l).toEpochSecond());
return l -> converter.apply(((ZonedDateTime) l).toInstant().toEpochMilli());
}

private static Function<Object, Object> toDateTime(Conversion conversion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ public void testConversionToLong() {
{
Conversion conversion = conversionFor(DATE, to);
assertNull(conversion.convert(null));
assertEquals(123379200L, conversion.convert(DateUtils.asDateOnly(123456789101L)));
assertEquals(-123465600L, conversion.convert(DateUtils.asDateOnly(-123456789101L)));
assertEquals(123379200000L, conversion.convert(DateUtils.asDateOnly(123456789101L)));
assertEquals(-123465600000L, conversion.convert(DateUtils.asDateOnly(-123456789101L)));
}
{
Conversion conversion = conversionFor(DATETIME, to);
assertNull(conversion.convert(null));
assertEquals(123456789L, conversion.convert(asDateTime(123456789101L)));
assertEquals(-123456790L, conversion.convert(asDateTime(-123456789101L)));
assertEquals(123456789101L, conversion.convert(asDateTime(123456789101L)));
assertEquals(-123456789101L, conversion.convert(asDateTime(-123456789101L)));
}
{
Conversion conversion = conversionFor(KEYWORD, to);
Expand Down Expand Up @@ -238,14 +238,14 @@ public void testConversionToDouble() {
{
Conversion conversion = conversionFor(DATE, to);
assertNull(conversion.convert(null));
assertEquals(1.233792E8, (double) conversion.convert(DateUtils.asDateOnly(123456789101L)), 0);
assertEquals(-1.234656E8, (double) conversion.convert(DateUtils.asDateOnly(-123456789101L)), 0);
assertEquals(1.233792E11, (double) conversion.convert(DateUtils.asDateOnly(123456789101L)), 0);
assertEquals(-1.234656E11, (double) conversion.convert(DateUtils.asDateOnly(-123456789101L)), 0);
}
{
Conversion conversion = conversionFor(DATETIME, to);
assertNull(conversion.convert(null));
assertEquals(1.23456789E8, (double) conversion.convert(asDateTime(123456789101L)), 0);
assertEquals(-1.2345679E8, (double) conversion.convert(asDateTime(-123456789101L)), 0);
assertEquals(1.23456789101E11, (double) conversion.convert(asDateTime(123456789101L)), 0);
assertEquals(-1.23456789101E11, (double) conversion.convert(asDateTime(-123456789101L)), 0);
}
{
Conversion conversion = conversionFor(KEYWORD, to);
Expand Down Expand Up @@ -340,39 +340,80 @@ public void testConversionToInt() {
{
Conversion conversion = conversionFor(DATE, to);
assertNull(conversion.convert(null));
assertEquals(123379200, conversion.convert(DateUtils.asDateOnly(123456789101L)));
assertEquals(-123465600, conversion.convert(DateUtils.asDateOnly(-123456789101L)));
assertEquals(0, conversion.convert(DateUtils.asDateOnly(12345678L)));
assertEquals(86400000, conversion.convert(DateUtils.asDateOnly(123456789L)));
assertEquals(172800000, conversion.convert(DateUtils.asDateOnly(223456789L)));
assertEquals(-172800000, conversion.convert(DateUtils.asDateOnly(-123456789L)));
Exception e = expectThrows(SqlIllegalArgumentException.class, () -> conversion.convert(DateUtils.asDateOnly(Long.MAX_VALUE)));
assertEquals("[9223372036828800000] out of [integer] range", e.getMessage());
}
{
Conversion conversion = conversionFor(DATETIME, to);
assertNull(conversion.convert(null));
assertEquals(123456789, conversion.convert(asDateTime(123456789101L)));
assertEquals(-123456790, conversion.convert(asDateTime(-123456789101L)));
assertEquals(12345678, conversion.convert(DateUtils.asDateTime(12345678L)));
assertEquals(223456789, conversion.convert(DateUtils.asDateTime(223456789L)));
assertEquals(-123456789, conversion.convert(DateUtils.asDateTime(-123456789L)));
Exception e = expectThrows(SqlIllegalArgumentException.class, () -> conversion.convert(DateUtils.asDateTime(Long.MAX_VALUE)));
assertEquals("[" + Long.MAX_VALUE + "] out of [integer] range", e.getMessage());
}
}

public void testConversionToShort() {
DataType to = SHORT;
{
Conversion conversion = conversionFor(DOUBLE, SHORT);
Conversion conversion = conversionFor(DOUBLE, to);
assertNull(conversion.convert(null));
assertEquals((short) 10, conversion.convert(10.0));
assertEquals((short) 10, conversion.convert(10.1));
assertEquals((short) 11, conversion.convert(10.6));
Exception e = expectThrows(SqlIllegalArgumentException.class, () -> conversion.convert(Integer.MAX_VALUE));
assertEquals("[" + Integer.MAX_VALUE + "] out of [short] range", e.getMessage());
}
{
Conversion conversion = conversionFor(DATE, to);
assertNull(conversion.convert(null));
assertEquals((short) 0, conversion.convert(DateUtils.asDateOnly(12345678L)));
Exception e = expectThrows(SqlIllegalArgumentException.class, () -> conversion.convert(DateUtils.asDateOnly(123456789L)));
assertEquals("[86400000] out of [short] range", e.getMessage());
}
{
Conversion conversion = conversionFor(DATETIME, to);
assertNull(conversion.convert(null));
assertEquals((short) 12345, conversion.convert(DateUtils.asDateTime(12345L)));
assertEquals((short) -12345, conversion.convert(DateUtils.asDateTime(-12345L)));
Exception e = expectThrows(SqlIllegalArgumentException.class,
() -> conversion.convert(DateUtils.asDateTime(Integer.MAX_VALUE)));
assertEquals("[" + Integer.MAX_VALUE + "] out of [short] range", e.getMessage());
}
}

public void testConversionToByte() {
DataType to = BYTE;
{
Conversion conversion = conversionFor(DOUBLE, BYTE);
Conversion conversion = conversionFor(DOUBLE, to);
assertNull(conversion.convert(null));
assertEquals((byte) 10, conversion.convert(10.0));
assertEquals((byte) 10, conversion.convert(10.1));
assertEquals((byte) 11, conversion.convert(10.6));
Exception e = expectThrows(SqlIllegalArgumentException.class, () -> conversion.convert(Short.MAX_VALUE));
assertEquals("[" + Short.MAX_VALUE + "] out of [byte] range", e.getMessage());
}
{
Conversion conversion = conversionFor(DATE, to);
assertNull(conversion.convert(null));
assertEquals((byte) 0, conversion.convert(DateUtils.asDateOnly(12345678L)));
Exception e = expectThrows(SqlIllegalArgumentException.class, () -> conversion.convert(DateUtils.asDateOnly(123456789L)));
assertEquals("[86400000] out of [byte] range", e.getMessage());
}
{
Conversion conversion = conversionFor(DATETIME, to);
assertNull(conversion.convert(null));
assertEquals((byte) 123, conversion.convert(DateUtils.asDateTime(123L)));
assertEquals((byte) -123, conversion.convert(DateUtils.asDateTime(-123L)));
Exception e = expectThrows(SqlIllegalArgumentException.class,
() -> conversion.convert(DateUtils.asDateTime(Integer.MAX_VALUE)));
assertEquals("[" + Integer.MAX_VALUE + "] out of [byte] range", e.getMessage());
}
}

public void testConversionToNull() {
Expand Down