@Override public Object getWritableConstantValue() { if (value==null) { return null; } return new TimestampWritableV2(value); } }
public Timestamp getPrimitiveJavaObject(Object o) { return o == null ? null : ((TimestampWritableV2) o).getTimestamp(); }
public DoubleWritable evaluate(TimestampWritableV2 i) { if (i == null) { return null; } else { try { doubleWritable.set(i.getDouble()); return doubleWritable; } catch (NumberFormatException e) { // MySQL returns 0 if the string is not a well-formed numeric value. // But we decided to return NULL instead, which is more conservative. return null; } } }
public void set(TimestampWritableV2 t) { if (t.bytesEmpty) { set(t.getTimestamp()); return; } if (t.currentBytes == t.externalBytes) { set(t.currentBytes, t.offset); } else { set(t.currentBytes, 0); } }
@Override public int hashCode() { long seconds = getSeconds(); seconds <<= 30; // the nanosecond part fits in 30 bits seconds |= getNanos(); return (int) ((seconds >>> 32) ^ seconds); }
public HiveDecimal getHiveDecimal() { if (timestampEmpty) { populateTimestamp(); } return getHiveDecimal(timestamp); }
@Test public void testIntervalYearMonthPlusTimestamp() throws Exception { GenericUDFOPPlus udf = new GenericUDFOPPlus(); HiveIntervalYearMonthWritable left = new HiveIntervalYearMonthWritable(HiveIntervalYearMonth.valueOf("2-2")); TimestampWritableV2 right = new TimestampWritableV2(Timestamp.valueOf("2001-11-15 01:02:03.123456789")); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableHiveIntervalYearMonthObjectInspector, PrimitiveObjectInspectorFactory.writableTimestampObjectInspector }; DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right), }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.timestampTypeInfo, oi.getTypeInfo()); TimestampWritableV2 res = (TimestampWritableV2) udf.evaluate(args); Assert.assertEquals(Timestamp.valueOf("2004-01-15 01:02:03.123456789"), res.getTimestamp()); }
@Override public void writeTimestamp(Timestamp v) throws IOException { beginPrimitive(); if (timestampWritable == null) { timestampWritable = new TimestampWritableV2(); } timestampWritable.set(v); LazyTimestamp.writeUTF8(output, timestampWritable); finishPrimitive(); }
@Override public void writeTimestamp(Timestamp v) throws IOException { beginElement(); if (timestampWritable == null) { timestampWritable = new TimestampWritableV2(); } timestampWritable.set(v); timestampWritable.writeToByteStream(output); finishElement(); }
protected TimestampWritableV2 handleTimestampResult(Timestamp result) { if (result == null) { return null; } timestampResult.set(result); return timestampResult; }
/** * @return byte[] representation of TimestampWritableV2 that is binary * sortable (7 bytes for seconds, 4 bytes for nanoseconds) */ public byte[] getBinarySortable() { byte[] b = new byte[BINARY_SORTABLE_LENGTH]; int nanos = getNanos(); // We flip the highest-order bit of the seven-byte representation of seconds to make negative // values come before positive ones. long seconds = getSeconds() ^ SEVEN_BYTE_LONG_SIGN_FLIP; sevenByteLongToBytes(seconds, b, 0); intToBytes(nanos, b, 7); return b; }
public LongWritable evaluate(TimestampWritableV2 i) { if (i == null) { return null; } else { longWritable.set(i.getSeconds()); return longWritable; } }
public static void setTimestamp(Timestamp t, byte[] bytes, int offset) { long seconds = getSeconds(bytes, offset); int nanos; if (hasDecimalOrSecondVInt(bytes[offset])) { nanos = getNanos(bytes, offset + 4); } else { nanos = 0; } t.setTimeInSeconds(seconds, nanos); }
public int compareTo(TimestampWritableV2 t) { checkBytes(); long s1 = this.getSeconds(); long s2 = t.getSeconds(); if (s1 == s2) { int n1 = this.getNanos(); int n2 = t.getNanos(); if (n1 == n2) { return 0; } return n1 - n2; } else { return s1 < s2 ? -1 : 1; } }
private VectorizedRowBatch getBatchDecimalTimestamp(double[] doubleValues) { VectorizedRowBatch b = new VectorizedRowBatch(2); DecimalColumnVector dv; b.cols[0] = dv = new DecimalColumnVector(doubleValues.length, HiveDecimal.SYSTEM_DEFAULT_PRECISION, HiveDecimal.SYSTEM_DEFAULT_SCALE); b.cols[1] = new TimestampColumnVector(doubleValues.length); dv.noNulls = true; Random r = new Random(94830); for (int i = 0; i < doubleValues.length; i++) { long millis = RandomTypeUtil.randomMillis(r); Timestamp ts = new Timestamp(millis); int nanos = RandomTypeUtil.randomNanos(r); ts.setNanos(nanos); TimestampWritableV2 tsw = new TimestampWritableV2( org.apache.hadoop.hive.common.type.Timestamp.ofEpochMilli(ts.getTime(), ts.getNanos())); double asDouble = tsw.getDouble(); doubleValues[i] = asDouble; HiveDecimal hiveDecimal = HiveDecimal.create(new BigDecimal(asDouble)); dv.set(i, hiveDecimal); } b.size = doubleValues.length; return b; }
Timestamp ts = new Timestamp(millis); ts.setNanos(optionalNanos); TimestampWritableV2 tsw = new TimestampWritableV2( org.apache.hadoop.hive.common.type.Timestamp.ofEpochMilli(ts.getTime(), ts.getNanos())); hiveDecimalValues[i] = tsw.getHiveDecimal();
/** * * @return nanoseconds in this TimestampWritableV2 */ public int getNanos() { if (!timestampEmpty) { return timestamp.getNanos(); } else if (!bytesEmpty) { return hasDecimalOrSecondVInt() ? TimestampWritableV2.getNanos(currentBytes, offset + 4) : 0; } else { throw new IllegalStateException("Both timestamp and bytes are empty"); } }
private void testWriterTimestamp(TypeInfo type) throws HiveException { Timestamp[] timestampValues = new Timestamp[vectorSize]; TimestampColumnVector tcv = VectorizedRowGroupGenUtil.generateTimestampColumnVector(true, false, vectorSize, new Random(10), timestampValues); tcv.isNull[3] = true; VectorExpressionWriter vew = getWriter(type); for (int i = 0; i < vectorSize; i++) { Writable w = (Writable) vew.writeValue(tcv, i); if (w != null) { Writable expected = getWritableValue(type, timestampValues[i]); TimestampWritableV2 t1 = (TimestampWritableV2) expected; TimestampWritableV2 t2 = (TimestampWritableV2) w; Assert.assertTrue(t1.equals(t2)); } else { Assert.assertTrue(tcv.isNull[i]); } } }
Object row = rows.next(null); Timestamp tlistTimestamp = tslist.get(idx++); if (tlistTimestamp.getNanos() != ((TimestampWritableV2) row).getNanos()) { assertTrue(false);
@Test public void testIntervalDayTimePlusTimestamp() throws Exception { GenericUDFOPPlus udf = new GenericUDFOPPlus(); HiveIntervalDayTimeWritable left = new HiveIntervalDayTimeWritable(HiveIntervalDayTime.valueOf("1 2:3:4.567")); TimestampWritableV2 right = new TimestampWritableV2(Timestamp.valueOf("2001-01-01 00:00:00")); ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableHiveIntervalDayTimeObjectInspector, PrimitiveObjectInspectorFactory.writableTimestampObjectInspector }; DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right), }; PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs); Assert.assertEquals(TypeInfoFactory.timestampTypeInfo, oi.getTypeInfo()); TimestampWritableV2 res = (TimestampWritableV2) udf.evaluate(args); Assert.assertEquals(Timestamp.valueOf("2001-01-02 2:3:4.567"), res.getTimestamp()); }