Return-Path: X-Original-To: apmail-hive-commits-archive@www.apache.org Delivered-To: apmail-hive-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0DD47108FF for ; Wed, 11 Dec 2013 21:32:27 +0000 (UTC) Received: (qmail 54616 invoked by uid 500); 11 Dec 2013 21:32:26 -0000 Delivered-To: apmail-hive-commits-archive@hive.apache.org Received: (qmail 54573 invoked by uid 500); 11 Dec 2013 21:32:26 -0000 Mailing-List: contact commits-help@hive.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: hive-dev@hive.apache.org Delivered-To: mailing list commits@hive.apache.org Received: (qmail 54559 invoked by uid 99); 11 Dec 2013 21:32:26 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Dec 2013 21:32:26 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Dec 2013 21:32:24 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 8DB932388868; Wed, 11 Dec 2013 21:32:04 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1550270 - in /hive/trunk/ql/src: java/org/apache/hadoop/hive/ql/exec/vector/TimestampUtils.java test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorExpressionWriters.java Date: Wed, 11 Dec 2013 21:32:04 -0000 To: commits@hive.apache.org From: hashutosh@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131211213204.8DB932388868@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: hashutosh Date: Wed Dec 11 21:32:04 2013 New Revision: 1550270 URL: http://svn.apache.org/r1550270 Log: HIVE-5979. Failure in cast to timestamps. (Jitendra Pandey) Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/TimestampUtils.java hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorExpressionWriters.java Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/TimestampUtils.java URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/TimestampUtils.java?rev=1550270&r1=1550269&r2=1550270&view=diff ============================================================================== --- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/TimestampUtils.java (original) +++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/TimestampUtils.java Wed Dec 11 21:32:04 2013 @@ -22,9 +22,27 @@ import java.sql.Timestamp; public final class TimestampUtils { + /** + * Store the given timestamp in nanoseconds into the timestamp object. + * @param timeInNanoSec Given timestamp in nanoseconds + * @param t The timestamp object + */ public static void assignTimeInNanoSec(long timeInNanoSec, Timestamp t) { - t.setTime((timeInNanoSec)/1000000); - t.setNanos((int)((t.getNanos()) + (timeInNanoSec % 1000000))); + /* + * java.sql.Timestamp consists of a long variable to store milliseconds and an integer variable for nanoseconds. + * The long variable is used to store only the full seconds converted to millis. For example for 1234 milliseconds, + * 1000 is stored in the long variable, and 234000000 (234 converted to nanoseconds) is stored as nanoseconds. + * The negative timestamps are also supported, but nanoseconds must be positive therefore millisecond part is + * reduced by one second. + */ + long integralSecInMillis = (timeInNanoSec / 1000000000) * 1000; // Full seconds converted to millis. + long nanos = timeInNanoSec % 1000000000; // The nanoseconds. + if (nanos < 0) { + nanos = 1000000000 + nanos; // The positive nano-part that will be added to milliseconds. + integralSecInMillis = ((timeInNanoSec / 1000000000) - 1) * 1000; // Reduce by one second. + } + t.setTime(integralSecInMillis); + t.setNanos((int) nanos); } public static long getTimeNanoSec(Timestamp t) { Modified: hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorExpressionWriters.java URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorExpressionWriters.java?rev=1550270&r1=1550269&r2=1550270&view=diff ============================================================================== --- hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorExpressionWriters.java (original) +++ hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorExpressionWriters.java Wed Dec 11 21:32:04 2013 @@ -418,4 +418,62 @@ public class TestVectorExpressionWriters public void testVectorExpressionSetterBinary() throws HiveException { testSetterText(TypeInfoFactory.binaryTypeInfo); } + + @Test + public void testTimeStampUtils(){ + Timestamp ts = new Timestamp(0); + + // Convert positive nanoseconds to timestamp object. + TimestampUtils.assignTimeInNanoSec(1234567891, ts); + Assert.assertEquals(234567891, ts.getNanos()); + Assert.assertEquals(1234567891, TimestampUtils.getTimeNanoSec(ts)); + + // Test negative nanoseconds + TimestampUtils.assignTimeInNanoSec(-1234567891, ts); + Assert.assertEquals((1000000000-234567891), ts.getNanos()); + Assert.assertEquals(-1234567891, TimestampUtils.getTimeNanoSec(ts)); + + // Test positive value smaller than a second. + TimestampUtils.assignTimeInNanoSec(234567891, ts); + Assert.assertEquals(234567891, ts.getNanos()); + Assert.assertEquals(234567891, TimestampUtils.getTimeNanoSec(ts)); + + // Test negative value smaller than a second. + TimestampUtils.assignTimeInNanoSec(-234567891, ts); + Assert.assertEquals((1000000000-234567891), ts.getNanos()); + Assert.assertEquals(-234567891, TimestampUtils.getTimeNanoSec(ts)); + + // Test a positive long timestamp + long big = 152414813551296L; + TimestampUtils.assignTimeInNanoSec(big, ts); + Assert.assertEquals(big % 1000000000, ts.getNanos()); + Assert.assertEquals(big, TimestampUtils.getTimeNanoSec(ts)); + + // Test a negative long timestamp + big = -152414813551296L; + TimestampUtils.assignTimeInNanoSec(big, ts); + Assert.assertEquals((1000000000 + (big % 1000000000)), ts.getNanos()); + Assert.assertEquals(big, TimestampUtils.getTimeNanoSec(ts)); + + // big/1000000 will yield zero nanoseconds + big = -1794750230000828416L; + ts = new Timestamp(0); + TimestampUtils.assignTimeInNanoSec(big, ts); + Assert.assertEquals((1000000000 + big % 1000000000), ts.getNanos()); + Assert.assertEquals(big, TimestampUtils.getTimeNanoSec(ts)); + + // Very small nanosecond part + big = 1700000000000000016L; + ts = new Timestamp(0); + TimestampUtils.assignTimeInNanoSec(big, ts); + Assert.assertEquals(big % 1000000000, ts.getNanos()); + Assert.assertEquals(big, TimestampUtils.getTimeNanoSec(ts)); + + // Very small nanosecond part + big = -1700000000000000016L; + ts = new Timestamp(0); + TimestampUtils.assignTimeInNanoSec(big, ts); + Assert.assertEquals((1000000000 + big % 1000000000), ts.getNanos()); + Assert.assertEquals(big, TimestampUtils.getTimeNanoSec(ts)); + } }