phoenix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From an...@apache.org
Subject phoenix git commit: PHOENIX-2845 Timestamp ignores nanos in literal expressions(Sergey Soldatov)
Date Thu, 05 May 2016 06:17:44 GMT
Repository: phoenix
Updated Branches:
  refs/heads/4.x-HBase-1.1 7b1db0516 -> ffb481040


PHOENIX-2845 Timestamp ignores nanos in literal expressions(Sergey Soldatov)


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

Branch: refs/heads/4.x-HBase-1.1
Commit: ffb481040132af39d0690df17cf7990ad01cfdd4
Parents: 7b1db05
Author: Ankit Singhal <ankitsinghal59@gmail.com>
Authored: Thu May 5 11:47:37 2016 +0530
Committer: Ankit Singhal <ankitsinghal59@gmail.com>
Committed: Thu May 5 11:47:37 2016 +0530

----------------------------------------------------------------------
 .../org/apache/phoenix/end2end/DateTimeIT.java  | 22 +++++++++++++++++
 .../apache/phoenix/end2end/UpsertValuesIT.java  |  1 +
 .../java/org/apache/phoenix/util/DateUtil.java  | 17 ++++++++++++-
 .../org/apache/phoenix/util/DateUtilTest.java   | 25 ++++++++++++++++++++
 4 files changed, 64 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/phoenix/blob/ffb48104/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 af8f0c1..7e407bc 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
@@ -693,4 +693,26 @@ public class DateTimeIT extends BaseHBaseManagedTimeIT {
         long actualTime = rs.getDate(1).getTime();
         assertTrue(Math.abs(actualTime - expectedTime) < MILLIS_IN_DAY);
     }
+    @Test
+    public void testSelectBetweenNanos() throws Exception {
+        String ddl =
+                "CREATE TABLE IF NOT EXISTS N1 (k1 INTEGER NOT NULL PRIMARY KEY, ts " +
+                        "TIMESTAMP(3))";
+        conn.createStatement().execute(ddl);
+        String dml = "UPSERT INTO N1 VALUES (1, TIMESTAMP'2015-01-01 00:00:00.111111111')";
+        conn.createStatement().execute(dml);
+        dml = "UPSERT INTO N1 VALUES (2, TIMESTAMP'2015-01-01 00:00:00.111111115')";
+        conn.createStatement().execute(dml);
+        dml = "UPSERT INTO N1 VALUES (3, TIMESTAMP'2015-01-01 00:00:00.111111113')";
+        conn.createStatement().execute(dml);
+        conn.commit();
+
+        ResultSet rs = conn.createStatement().executeQuery("SELECT k1,ts from N1 where ts
between" +
+                " TIMESTAMP'2015-01-01 00:00:00.111111112' AND TIMESTAMP'2015-01-01 00:00:00"
+
+                ".111111114'");
+        assertTrue(rs.next());
+        assertEquals(3, rs.getInt(1));
+        assertEquals(111111113, rs.getTimestamp(2).getNanos());
+        assertFalse(rs.next());
+    }
 }

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ffb48104/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpsertValuesIT.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpsertValuesIT.java b/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpsertValuesIT.java
index a7e70c1..5cefd7d 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpsertValuesIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/end2end/UpsertValuesIT.java
@@ -632,6 +632,7 @@ public class UpsertValuesIT extends BaseClientManagedTimeIT {
 
         Date date = toDate(dateStr);
         Timestamp timeStamp = new Timestamp(toDate(timeStampStr).getTime());
+        timeStamp.setNanos(Timestamp.valueOf(timeStampStr).getNanos());
         Time time = new Time(toDate(timeStr).getTime());
         props.setProperty(PhoenixRuntime.CURRENT_SCN_ATTRIB, Long.toString(ts + 4));
         try {

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ffb48104/phoenix-core/src/main/java/org/apache/phoenix/util/DateUtil.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/main/java/org/apache/phoenix/util/DateUtil.java b/phoenix-core/src/main/java/org/apache/phoenix/util/DateUtil.java
index 4d7c27c..557d8ed 100644
--- a/phoenix-core/src/main/java/org/apache/phoenix/util/DateUtil.java
+++ b/phoenix-core/src/main/java/org/apache/phoenix/util/DateUtil.java
@@ -32,6 +32,7 @@ import java.util.List;
 import java.util.TimeZone;
 
 import org.apache.commons.lang.time.FastDateFormat;
+import org.apache.hadoop.hbase.exceptions.IllegalArgumentIOException;
 import org.apache.phoenix.schema.IllegalDataException;
 import org.apache.phoenix.schema.types.PDataType;
 import org.joda.time.DateTimeZone;
@@ -172,7 +173,21 @@ public class DateUtil {
     }
 
     public static Timestamp parseTimestamp(String timestampValue) {
-        return new Timestamp(parseDateTime(timestampValue));
+        Timestamp timestamp = new Timestamp(parseDateTime(timestampValue));
+        int period = timestampValue.indexOf('.');
+        if (period > 0) {
+            String nanosStr = timestampValue.substring(period + 1);
+            if (nanosStr.length() > 9)
+                throw new IllegalDataException("nanos > 999999999 or < 0");
+            if(nanosStr.length() > 3 ) {
+                int nanos = Integer.parseInt(nanosStr);
+                for (int i = 0; i < 9 - nanosStr.length(); i++) {
+                    nanos *= 10;
+                }
+                timestamp.setNanos(nanos);
+            }
+        }
+        return timestamp;
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/phoenix/blob/ffb48104/phoenix-core/src/test/java/org/apache/phoenix/util/DateUtilTest.java
----------------------------------------------------------------------
diff --git a/phoenix-core/src/test/java/org/apache/phoenix/util/DateUtilTest.java b/phoenix-core/src/test/java/org/apache/phoenix/util/DateUtilTest.java
index ec0bc01..9ecce29 100644
--- a/phoenix-core/src/test/java/org/apache/phoenix/util/DateUtilTest.java
+++ b/phoenix-core/src/test/java/org/apache/phoenix/util/DateUtilTest.java
@@ -167,6 +167,31 @@ public class DateUtilTest {
         assertEquals(10123L, DateUtil.parseTimestamp("1970-01-01 00:00:10.123").getTime());
     }
 
+    @Test
+    public void testParseTimestamp_WithNanos() {
+        assertEquals(123000000, DateUtil.parseTimestamp("1970-01-01 00:00:10.123").getNanos());
+
+        assertEquals(123456780, DateUtil.parseTimestamp("1970-01-01 00:00:10.12345678").getNanos
+                ());
+        assertEquals(999999999, DateUtil.parseTimestamp("1970-01-01 00:00:10.999999999").getNanos
+                ());
+
+    }
+
+    @Test(expected=IllegalDataException.class)
+    public void testParseTimestamp_tooLargeNanos() {
+        DateUtil.parseTimestamp("1970-01-01 00:00:10.9999999999");
+    }
+
+    @Test(expected=IllegalDataException.class)
+    public void testParseTimestamp_missingNanos() {
+        DateUtil.parseTimestamp("1970-01-01 00:00:10.");
+    }
+    @Test(expected=IllegalDataException.class)
+    public void testParseTimestamp_negativeNanos() {
+        DateUtil.parseTimestamp("1970-01-01 00:00:10.-1");
+    }
+
     @Test(expected=IllegalDataException.class)
     public void testParseTimestamp_InvalidTimestamp() {
         DateUtil.parseTimestamp("not-a-timestamp");


Mime
View raw message