Repository: phoenix
Updated Branches:
  refs/heads/4.3 236f40785 -> 362e37264


PHOENIX-1744 CAST from UNSIGNED_LONG (_INT) to * TIMESTAMP is not supported 
(Dave Hacker)


Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo
Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/362e3726
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/362e3726
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/362e3726

Branch: refs/heads/4.3
Commit: 362e37264d9998928b2051ec917e7251ad48456d
Parents: 236f407
Author: Samarth <samarth.j...@salesforce.com>
Authored: Tue Mar 24 09:56:30 2015 -0700
Committer: Samarth <samarth.j...@salesforce.com>
Committed: Tue Mar 24 09:56:30 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/362e3726/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/362e3726/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 a0ead11..005e2c4 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
@@ -94,6 +94,11 @@ public class PUnsignedLong extends PDataType<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);

Reply via email to