phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sama...@apache.org
Subject phoenix git commit: PHOENIX-1744 CAST from UNSIGNED_LONG (_INT) to * TIMESTAMP is not supported (Dave Hacker)
Date Tue, 24 Mar 2015 17:20:31 GMT
Repository: phoenix
Updated Branches:
  refs/heads/4.x-HBase-0.98 790d5e4d1 -> c92de2749


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/c92de274
Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/c92de274
Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/c92de274

Branch: refs/heads/4.x-HBase-0.98
Commit: c92de2749cc5d9954eb9381ecfa03affb615cc65
Parents: 790d5e4
Author: Samarth <samarth.jain@salesforce.com>
Authored: Tue Mar 24 10:20:39 2015 -0700
Committer: Samarth <samarth.jain@salesforce.com>
Committed: Tue Mar 24 10:20:39 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/c92de274/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/c92de274/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);


Mime
View raw message