DRILL-1051: Casting timestamp as date gives wrong result for dates earlier than 1883
- Fix DateAccessor's, TimestampAccessor's and TimeAccessor's converting joda time to java.sql close apache/drill#915 Project: http://git-wip-us.apache.org/repos/asf/drill/repo Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/406dd2a5 Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/406dd2a5 Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/406dd2a5 Branch: refs/heads/master Commit: 406dd2a58d2168faf47636fcd9829f29a93175bd Parents: 33f17aa Author: Vitalii Diravka <vitalii.dira...@gmail.com> Authored: Tue Aug 15 17:51:10 2017 +0000 Committer: Aman Sinha <asi...@maprtech.com> Committed: Sat Sep 2 13:38:02 2017 -0700 ---------------------------------------------------------------------- .../main/codegen/templates/SqlAccessors.java | 28 +++++++++----------- .../apache/drill/jdbc/test/TestJdbcQuery.java | 25 ++++++++++++++++- 2 files changed, 36 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/drill/blob/406dd2a5/exec/java-exec/src/main/codegen/templates/SqlAccessors.java ---------------------------------------------------------------------- diff --git a/exec/java-exec/src/main/codegen/templates/SqlAccessors.java b/exec/java-exec/src/main/codegen/templates/SqlAccessors.java index 49a91c2..888d1c6 100644 --- a/exec/java-exec/src/main/codegen/templates/SqlAccessors.java +++ b/exec/java-exec/src/main/codegen/templates/SqlAccessors.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -264,9 +264,10 @@ public class ${name}Accessor extends AbstractSqlAccessor { return null; } </#if> - org.joda.time.DateTime date = new org.joda.time.DateTime(ac.get(index), org.joda.time.DateTimeZone.UTC); - date = date.withZoneRetainFields(org.joda.time.DateTimeZone.getDefault()); - return new Date(date.getMillis()); + org.joda.time.LocalDate date = new org.joda.time.LocalDate(ac.get(index), org.joda.time.DateTimeZone.UTC); + // Use "toDate()" to get java.util.Date object with exactly the same year the same year, month and day as Joda date. + // See more in Javadoc for "LocalDate#toDate()" + return new Date(date.toDate().getTime()); } <#elseif minor.class == "TimeStamp"> @@ -293,9 +294,10 @@ public class ${name}Accessor extends AbstractSqlAccessor { return null; } </#if> - org.joda.time.DateTime date = new org.joda.time.DateTime(ac.get(index), org.joda.time.DateTimeZone.UTC); - date = date.withZoneRetainFields(org.joda.time.DateTimeZone.getDefault()); - return new Timestamp(date.getMillis()); + org.joda.time.LocalDateTime dateTime = new org.joda.time.LocalDateTime(ac.get(index), org.joda.time.DateTimeZone.UTC); + // use "toDate()" to get java.util.Date object with exactly the same fields as this Joda date-time. + // See more in Javadoc for "LocalDateTime#toDate()" + return new Timestamp(dateTime.toDate().getTime()); } <#elseif minor.class == "Time"> @@ -317,9 +319,9 @@ public class ${name}Accessor extends AbstractSqlAccessor { return null; } </#if> - org.joda.time.DateTime time = new org.joda.time.DateTime(ac.get(index), org.joda.time.DateTimeZone.UTC); - time = time.withZoneRetainFields(org.joda.time.DateTimeZone.getDefault()); - return new TimePrintMillis(time.getMillis()); + org.joda.time.LocalTime time = new org.joda.time.LocalTime(ac.get(index), org.joda.time.DateTimeZone.UTC); + // use "toDateTimeToday()" and "getMillis()" to get the local milliseconds from the Java epoch of 1970-01-01T00:00:00 + return new TimePrintMillis(time.toDateTimeToday().getMillis()); } <#else> @@ -330,7 +332,6 @@ public class ${name}Accessor extends AbstractSqlAccessor { } </#if> - <#if minor.class == "Bit" > public boolean getBoolean(int index) { <#if mode == "Nullable"> @@ -341,13 +342,8 @@ public class ${name}Accessor extends AbstractSqlAccessor { return 1 == ac.get(index); } </#if> - - </#if> <#-- not VarLen --> - } - - </#list> </#list> </#list> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/drill/blob/406dd2a5/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestJdbcQuery.java ---------------------------------------------------------------------- diff --git a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestJdbcQuery.java b/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestJdbcQuery.java index bff620e..1dd172f 100644 --- a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestJdbcQuery.java +++ b/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestJdbcQuery.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -367,4 +367,27 @@ public class TestJdbcQuery extends JdbcTestQueryBase { "TIME_INT_ADD=6\n" ); } + + @Test // DRILL-1051 + public void testOldDateTimeJulianCalendar() throws Exception { + // Should be work with any timezone + JdbcAssert.withNoDefaultSchema() + .sql("select cast(to_timestamp('1581-12-01 23:32:01', 'yyyy-MM-dd HH:mm:ss') as date) as `DATE`, " + + "to_timestamp('1581-12-01 23:32:01', 'yyyy-MM-dd HH:mm:ss') as `TIMESTAMP`, " + + "cast(to_timestamp('1581-12-01 23:32:01', 'yyyy-MM-dd HH:mm:ss') as time) as `TIME` " + + "from (VALUES(1))") + .returns("DATE=1581-12-01; TIMESTAMP=1581-12-01 23:32:01.0; TIME=23:32:01"); + } + + @Test // DRILL-1051 + public void testOldDateTimeLocalMeanTime() throws Exception { + // Should be work with any timezone + JdbcAssert.withNoDefaultSchema() + .sql("select cast(to_timestamp('1883-11-16 01:32:01', 'yyyy-MM-dd HH:mm:ss') as date) as `DATE`, " + + "to_timestamp('1883-11-16 01:32:01', 'yyyy-MM-dd HH:mm:ss') as `TIMESTAMP`, " + + "cast(to_timestamp('1883-11-16 01:32:01', 'yyyy-MM-dd HH:mm:ss') as time) as `TIME` " + + "from (VALUES(1))") + .returns("DATE=1883-11-16; TIMESTAMP=1883-11-16 01:32:01.0; TIME=01:32:01"); + } + }