public static HiveDecimal getHiveDecimal(Timestamp timestamp) { // The BigDecimal class recommends not converting directly from double to BigDecimal, // so we convert through a string... Double timestampDouble = TimestampUtils.getDouble(timestamp); HiveDecimal result = HiveDecimal.create(timestampDouble.toString()); return result; }
@Override protected void func(TimestampColumnVector outV, DecimalColumnVector inV, int i) { Timestamp timestamp = TimestampUtils.decimalToTimestamp( inV.vector[i], scratchHiveDecimalWritable1, scratchHiveDecimalWritable2); outV.set(i, timestamp); } }
private void setDouble(TimestampColumnVector timestampColVector, double[] vector, int elementNum) { timestampColVector.set(elementNum, TimestampUtils.doubleToTimestamp(vector[elementNum])); }
} else if (obj instanceof Timestamp) { return new HiveDecimalWritable(Double.toString( TimestampUtils.getDouble((Timestamp) obj))); return Double.valueOf(obj.toString()); } else if (obj instanceof Timestamp) { return TimestampUtils.getDouble((Timestamp) obj); } else if (obj instanceof HiveDecimal) { return ((HiveDecimal) obj).doubleValue(); return new Timestamp(((Number) obj).longValue()); } else if (obj instanceof Float) { return TimestampUtils.doubleToTimestamp(((Float) obj).doubleValue()); } else if (obj instanceof Double) { return TimestampUtils.doubleToTimestamp(((Double) obj).doubleValue()); } else if (obj instanceof HiveDecimal) { return TimestampUtils.decimalToTimestamp((HiveDecimal) obj); } else if (obj instanceof HiveDecimalWritable) { return TimestampUtils.decimalToTimestamp(((HiveDecimalWritable) obj).getHiveDecimal()); } else if (obj instanceof Date) { return new Timestamp(((Date) obj).getTime());
/** * Convert the timestamp to a double measured in seconds. * @return double representation of the timestamp, accurate to nanoseconds */ public static double getDouble(Timestamp ts) { long seconds = millisToSeconds(ts.getTime()); return seconds + ((double) ts.getNanos()) / 1000000000; }
} else if (obj instanceof Timestamp) { return new HiveDecimalWritable(Double.toString( TimestampUtils.getDouble((Timestamp) obj))); return Double.valueOf(obj.toString()); } else if (obj instanceof Timestamp) { return TimestampUtils.getDouble((Timestamp) obj); } else if (obj instanceof HiveDecimal) { return ((HiveDecimal) obj).doubleValue(); return new Timestamp(((Number) obj).longValue()); } else if (obj instanceof Float) { return TimestampUtils.doubleToTimestamp(((Float) obj).doubleValue()); } else if (obj instanceof Double) { return TimestampUtils.doubleToTimestamp(((Double) obj).doubleValue()); } else if (obj instanceof HiveDecimal) { return TimestampUtils.decimalToTimestamp((HiveDecimal) obj); } else if (obj instanceof HiveDecimalWritable) { return TimestampUtils.decimalToTimestamp(((HiveDecimalWritable) obj).getHiveDecimal()); } else if (obj instanceof Date) { return new Timestamp(((Date) obj).getTime());
/** * * @return seconds corresponding to this TimestampWritable */ public long getSeconds() { if (!timestampEmpty) { return TimestampUtils.millisToSeconds(timestamp.getTime()); } else if (!bytesEmpty) { return TimestampWritable.getSeconds(currentBytes, offset); } else { throw new IllegalStateException("Both timestamp and bytes are empty"); } }
@Override protected void func(DecimalColumnVector outV, TimestampColumnVector inV, int i) { Double timestampDouble = TimestampUtils.getDouble(inV.asScratchTimestamp(i)); HiveDecimal result = HiveDecimal.create(timestampDouble.toString()); outV.set(i, HiveDecimal.create(timestampDouble.toString())); } }
/** * * @return double representation of the timestamp, accurate to nanoseconds */ public double getDouble() { double seconds, nanos; if (bytesEmpty) { seconds = TimestampUtils.millisToSeconds(timestamp.getTime()); nanos = timestamp.getNanos(); } else { seconds = getSeconds(); nanos = getNanos(); } return seconds + nanos / 1000000000; }
private void setDouble(TimestampColumnVector timestampColVector, double[] vector, int elementNum) { timestampColVector.set(elementNum, TimestampUtils.doubleToTimestamp(vector[elementNum])); }
@Override protected void func(TimestampColumnVector outV, DecimalColumnVector inV, int i) { Timestamp timestamp = TimestampUtils.decimalToTimestamp( inV.vector[i], scratchHiveDecimalWritable1, scratchHiveDecimalWritable2); outV.set(i, timestamp); } }
@Test public void testCastDoubleToTimestamp() throws HiveException { VectorizedRowBatch b = TestVectorMathFunctions.getVectorizedRowBatchDoubleInTimestampOut(); TimestampColumnVector resultV = (TimestampColumnVector) b.cols[1]; b.cols[0].noNulls = true; VectorExpression expr = new CastDoubleToTimestamp(0, 1); expr.evaluate(b); Assert.assertEquals(0.0, TimestampUtils.getDouble(resultV.asScratchTimestamp(3))); Assert.assertEquals(0.5d, TimestampUtils.getDouble(resultV.asScratchTimestamp(4))); }
/** * Writes a Timestamp's serialized value to byte array b at the given offset * @param t to convert to bytes * @param b destination byte array * @param offset destination offset in the byte array */ public static void convertTimestampToBytes(Timestamp t, byte[] b, int offset) { long millis = t.getTime(); int nanos = t.getNanos(); long seconds = TimestampUtils.millisToSeconds(millis); boolean hasSecondVInt = seconds < 0 || seconds > Integer.MAX_VALUE; boolean hasDecimal = setNanosBytes(nanos, b, offset+4, hasSecondVInt); int firstInt = (int) seconds; if (hasDecimal || hasSecondVInt) { firstInt |= DECIMAL_OR_SECOND_VINT_FLAG; } else { firstInt &= LOWEST_31_BITS_OF_SEC_MASK; } intToBytes(firstInt, b, offset); if (hasSecondVInt) { LazyBinaryUtils.writeVLongToByteArray(b, offset + 4 + WritableUtils.decodeVIntSize(b[offset + 4]), seconds >> 31); } }
@Override public void setConvertVectorElement(int elementNum) { float floatValue = (float) doubleColVector.vector[elementNum]; Timestamp timestampValue = TimestampUtils.doubleToTimestamp(floatValue); // The TimestampColumnVector will set the entry to null when a null timestamp is passed in. timestampColVector.set(elementNum, timestampValue); }
@Override public void setConvertVectorElement(int elementNum) { Timestamp timestampValue = TimestampUtils.decimalToTimestamp( decimalColVector.vector[elementNum].getHiveDecimal()); // The TimestampColumnVector will set the entry to null when a null timestamp is passed in. timestampColVector.set(elementNum, timestampValue); }
@Test public void testCastDecimalToTimestamp() throws HiveException { double[] doubleValues = new double[500]; VectorizedRowBatch b = getBatchDecimalTimestamp(doubleValues); VectorExpression expr = new CastDecimalToTimestamp(0, 1); expr.evaluate(b); TimestampColumnVector r = (TimestampColumnVector) b.cols[1]; for (int i = 0; i < doubleValues.length; i++) { Timestamp timestamp = r.asScratchTimestamp(i); double asDouble = TimestampUtils.getDouble(timestamp); double expectedDouble = doubleValues[i]; if (expectedDouble != asDouble) { assertTrue(false); } } }
/** * Convert the timestamp to a double measured in seconds. * @return double representation of the timestamp, accurate to nanoseconds */ public static double getDouble(Timestamp ts) { long seconds = millisToSeconds(ts.getTime()); return seconds + ((double) ts.getNanos()) / 1000000000; }
@Override public void setConvertVectorElement(int elementNum) { double doubleValue = doubleColVector.vector[elementNum]; Timestamp timestampValue = TimestampUtils.doubleToTimestamp(doubleValue); // The TimestampColumnVector will set the entry to null when a null timestamp is passed in. timestampColVector.set(elementNum, timestampValue); }
@Override public void setConvertVectorElement(int elementNum) { Timestamp timestampValue = TimestampUtils.decimalToTimestamp( decimalColVector.vector[elementNum].getHiveDecimal()); // The TimestampColumnVector will set the entry to null when a null timestamp is passed in. timestampColVector.set(elementNum, timestampValue); }
@Test public void testCastTimestampToDouble() throws HiveException { double[] doubleValues = new double[500]; VectorizedRowBatch b = TestVectorMathFunctions.getVectorizedRowBatchTimestampInDoubleOut(doubleValues); TimestampColumnVector inV = (TimestampColumnVector) b.cols[0]; DoubleColumnVector resultV = (DoubleColumnVector) b.cols[1]; b.cols[0].noNulls = true; VectorExpression expr = new CastTimestampToDouble(0, 1); expr.evaluate(b); for (int i = 0; i < doubleValues.length; i++) { double actual = resultV.vector[i]; double doubleValue = TimestampUtils.getDouble(inV.asScratchTimestamp(i)); assertEquals(actual, doubleValue, 0.000000001F); } }