diff --git a/serde/src/java/org/apache/hadoop/hive/serde2/io/TimestampWritable.java b/serde/src/java/org/apache/hadoop/hive/serde2/io/TimestampWritable.java index ef7ad78..bbccc7f 100644 --- a/serde/src/java/org/apache/hadoop/hive/serde2/io/TimestampWritable.java +++ b/serde/src/java/org/apache/hadoop/hive/serde2/io/TimestampWritable.java @@ -554,7 +554,11 @@ public static Timestamp longToTimestamp(long time, boolean intToTimestampInSecon public static void setTimestamp(Timestamp t, byte[] bytes, int offset) { long seconds = getSeconds(bytes, offset); t.setTime(seconds * 1000); - t.setNanos(getNanos(bytes, offset + 4)); + if (hasDecimalOrSecondVInt(bytes[offset])) { + t.setNanos(getNanos(bytes, offset + 4)); + } else { + t.setNanos(0); + } } public static Timestamp createTimestamp(byte[] bytes, int offset) { diff --git a/serde/src/test/org/apache/hadoop/hive/serde2/io/TestTimestampWritable.java b/serde/src/test/org/apache/hadoop/hive/serde2/io/TestTimestampWritable.java index cd7a1b9..3c483cc 100644 --- a/serde/src/test/org/apache/hadoop/hive/serde2/io/TestTimestampWritable.java +++ b/serde/src/test/org/apache/hadoop/hive/serde2/io/TestTimestampWritable.java @@ -495,8 +495,21 @@ public void testBinarySortable() { @Test public void testSetTimestamp() { - // make sure we need a 2nd VInt - Timestamp t1 = new Timestamp((long) Integer.MAX_VALUE * 1000 + 1234); + // one VInt without nanos + verifySetTimestamp(1000); + + // one VInt with nanos + verifySetTimestamp(1001); + + // two VInt without nanos + verifySetTimestamp((long) Integer.MAX_VALUE * 1000 + 1000); + + // two VInt with nanos + verifySetTimestamp((long) Integer.MAX_VALUE * 1000 + 1234); + } + + private static void verifySetTimestamp(long time) { + Timestamp t1 = new Timestamp(time); TimestampWritable writable = new TimestampWritable(t1); byte[] bytes = writable.getBytes(); Timestamp t2 = new Timestamp(0);