PHOENIX-1744 Allow Integer, UnsignedInt and UnsignedLong to be Cast to TIMESTAMP (Dave Hacker)
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/250474de Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/250474de Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/250474de Branch: refs/heads/4.x-HBase-1.x Commit: 250474deb381b376d5ed442186470f65b36a8117 Parents: 7de8ee1 Author: David <dhac...@salesforce.com> Authored: Wed Mar 18 13:37:20 2015 -0700 Committer: Thomas <tdsi...@salesforce.com> Committed: Tue Mar 24 14:00:20 2015 -0700 ---------------------------------------------------------------------- .../phoenix/end2end/ToDateFunctionIT.java | 57 ++++++++++++++++++++ .../phoenix/schema/types/PUnsignedLong.java | 5 ++ 2 files changed, 62 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/250474de/phoenix-core/src/it/java/org/apache/phoenix/end2end/ToDateFunctionIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ToDateFunctionIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ToDateFunctionIT.java index bda4ea5..8de39b7 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/ToDateFunctionIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/ToDateFunctionIT.java @@ -33,6 +33,7 @@ import java.sql.Timestamp; import java.util.Properties; import org.apache.phoenix.query.QueryServices; +import org.apache.phoenix.schema.TypeMismatchException; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -176,4 +177,60 @@ public class ToDateFunctionIT extends BaseHBaseManagedTimeIT { callToDateFunction( customTimeZoneConn, "TO_DATE('1970-01-01', 'yyyy-MM-dd')").getTime()); } + + @Test + public void testTimestampCast() throws SQLException { + Properties props = new Properties(); + props.setProperty(QueryServices.DATE_FORMAT_TIMEZONE_ATTRIB, "GMT+1"); + Connection customTimeZoneConn = DriverManager.getConnection(getUrl(), props); + + assertEquals( + 1426188807198L, + callToDateFunction( + customTimeZoneConn, "CAST(1426188807198 AS TIMESTAMP)").getTime()); + + + try { + callToDateFunction( + customTimeZoneConn, "CAST(22005 AS TIMESTAMP)"); + fail(); + } catch (TypeMismatchException e) { + + } + } + + @Test + public void testUnsignedLongToTimestampCast() throws SQLException { + Properties props = new Properties(); + props.setProperty(QueryServices.DATE_FORMAT_TIMEZONE_ATTRIB, "GMT+1"); + Connection conn = DriverManager.getConnection(getUrl(), props); + conn.setAutoCommit(false); + try { + conn.prepareStatement( + "create table TT(" + + "a unsigned_int not null, " + + "b unsigned_int not null, " + + "ts unsigned_long not null " + + "constraint PK primary key (a, b, ts))").execute(); + conn.commit(); + + conn.prepareStatement("upsert into TT values (0, 22120, 1426188807198)").execute(); + conn.commit(); + + ResultSet rs = conn.prepareStatement("select a, b, ts, CAST(ts AS TIMESTAMP) from TT").executeQuery(); + assertTrue(rs.next()); + assertEquals(new Date(1426188807198L), rs.getObject(4)); + rs.close(); + + try { + rs = conn.prepareStatement("select a, b, ts, CAST(b AS TIMESTAMP) from TT").executeQuery(); + fail(); + } catch (TypeMismatchException e) { + + } + + } finally { + conn.close(); + } + } } http://git-wip-us.apache.org/repos/asf/phoenix/blob/250474de/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedLong.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedLong.java b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedLong.java index 67ae05a..a21ccc3 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedLong.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/schema/types/PUnsignedLong.java @@ -95,6 +95,11 @@ public class PUnsignedLong extends PWholeNumber<Long> { } @Override + public boolean isCastableTo(PDataType targetType) { + return super.isCastableTo(targetType) || targetType.isCoercibleTo(PTimestamp.INSTANCE); + } + + @Override public boolean isCoercibleTo(PDataType targetType) { return targetType == this || targetType == PUnsignedDouble.INSTANCE || PLong.INSTANCE .isCoercibleTo(targetType);