PHOENIX-2734 Literal expressions for UNSIGNED_DATE/UNSIGNED_TIME/etc (Sergey 
Soldatov)


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

Branch: refs/heads/4.x-HBase-1.0
Commit: c636504ed53f24a4aa2bef9def14d6fd439d66d1
Parents: 308e282
Author: James Taylor <jtay...@salesforce.com>
Authored: Mon Mar 28 08:22:37 2016 -0700
Committer: James Taylor <jtay...@salesforce.com>
Committed: Mon Mar 28 09:56:57 2016 -0700

----------------------------------------------------------------------
 .../org/apache/phoenix/end2end/DateTimeIT.java  | 20 ++++++++++++++++++++
 .../phoenix/expression/LiteralExpression.java   | 12 ++++--------
 2 files changed, 24 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/c636504e/phoenix-core/src/it/java/org/apache/phoenix/end2end/DateTimeIT.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/DateTimeIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/DateTimeIT.java
index e87d8d4..af8f0c1 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/DateTimeIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/DateTimeIT.java
@@ -513,6 +513,26 @@ public class DateTimeIT extends BaseHBaseManagedTimeIT {
     }
 
     @Test
+    public void testUnsignedTimeDateWithLiteral() throws Exception {
+        String ddl =
+                "CREATE TABLE IF NOT EXISTS UT (k1 INTEGER NOT NULL," +
+                        "unsignedDates UNSIGNED_DATE, unsignedTimestamps 
UNSIGNED_TIMESTAMP, unsignedTimes UNSIGNED_TIME CONSTRAINT pk PRIMARY KEY 
(k1))";
+        conn.createStatement().execute(ddl);
+        String dml = "UPSERT INTO UT VALUES (1, " +
+                "'2010-06-20 12:00:00', '2012-07-28 12:00:00', '2015-12-25 
12:00:00')";
+        conn.createStatement().execute(dml);
+        conn.commit();
+
+        ResultSet rs = conn.createStatement().executeQuery("SELECT k1, 
unsignedDates, " +
+                "unsignedTimestamps, unsignedTimes FROM UT where k1 = 1");
+        assertTrue(rs.next());
+        assertEquals(DateUtil.parseDate("2010-06-20 12:00:00"), rs.getDate(2));
+        assertEquals(DateUtil.parseTimestamp("2012-07-28 12:00:00"), 
rs.getTimestamp(3));
+        assertEquals(DateUtil.parseTime("2015-12-25 12:00:00"), rs.getTime(4));
+        assertFalse(rs.next());
+    }
+
+    @Test
     public void testSecondFuncAgainstColumns() throws Exception {
         String ddl =
                 "CREATE TABLE IF NOT EXISTS T1 (k1 INTEGER NOT NULL, dates 
DATE, timestamps TIMESTAMP, times TIME, " +

http://git-wip-us.apache.org/repos/asf/phoenix/blob/c636504e/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
----------------------------------------------------------------------
diff --git 
a/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
 
b/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
index ad1c7c0..90882a2 100644
--- 
a/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
+++ 
b/phoenix-core/src/main/java/org/apache/phoenix/expression/LiteralExpression.java
@@ -25,6 +25,7 @@ import java.sql.SQLException;
 import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
 import org.apache.hadoop.io.WritableUtils;
 import org.apache.phoenix.expression.visitor.ExpressionVisitor;
+import org.apache.phoenix.schema.IllegalDataException;
 import org.apache.phoenix.schema.SortOrder;
 import org.apache.phoenix.schema.TypeMismatchException;
 import org.apache.phoenix.schema.tuple.Tuple;
@@ -182,16 +183,11 @@ public class LiteralExpression extends 
BaseTerminalExpression {
             return getBooleanLiteralExpression((Boolean)value, determinism);
         }
         PDataType actualType = PDataType.fromLiteral(value);
-        // For array we should check individual element in it?
-        // It would be costly though!!!!!
-        // UpsertStatement can try to cast varchar to date type but PVarchar 
can't CoercibleTo Date or Timestamp
-        // otherwise TO_NUMBER like functions will fail
-        if (!actualType.isCoercibleTo(type, value) &&
-                (!actualType.equals(PVarchar.INSTANCE) ||
-                        !(type.equals(PDate.INSTANCE) || 
type.equals(PTimestamp.INSTANCE) || type.equals(PTime.INSTANCE)))) {
+        try {
+            value = type.toObject(value, actualType);
+        } catch (IllegalDataException e) {
             throw TypeMismatchException.newException(type, actualType, 
value.toString());
         }
-        value = type.toObject(value, actualType);
         byte[] b = type.isArrayType() ? ((PArrayDataType)type).toBytes(value, 
PArrayDataType.arrayBaseType(type), sortOrder, rowKeyOrderOptimizable) :
                 type.toBytes(value, sortOrder);
         if (type == PVarchar.INSTANCE || type == PChar.INSTANCE) {

Reply via email to