private long truncate(Timestamp ts, HiveIntervalDayTime idt, Timestamp origin) { long intervalDurationMs = idt.getTotalSeconds() * 1000L + idt.getNanos() / 1000L; long offset = ts.toEpochMilli() % intervalDurationMs - origin.toEpochMilli() % intervalDurationMs; if (offset < 0) { offset += intervalDurationMs; } return ts.toEpochMilli() - offset; }
public java.sql.Timestamp toSqlTimestamp() { java.sql.Timestamp ts = new java.sql.Timestamp(toEpochMilli()); ts.setNanos(getNanos()); return ts; }
private Calendar addMonth(Timestamp ts, int numMonths) { calendar.setTimeInMillis(ts.toEpochMilli()); return addMonth(numMonths); }
public IntWritable evaluate(TimestampWritableV2 t) { if (t == null) { return null; } Timestamp ts = t.getTimestamp(); calendar.setTimeInMillis(ts.toEpochMilli()); result.set(calendar.get(Calendar.WEEK_OF_YEAR)); return result; }
public boolean subtract(Timestamp left, Timestamp right, HiveIntervalDayTime result) { if (left == null || right == null) { return false; } nanosResult.addNanos(left.getNanos(), -(right.getNanos())); long totalSeconds = TimeUnit.MILLISECONDS.toSeconds(left.toEpochMilli()) - TimeUnit.MILLISECONDS.toSeconds(right.toEpochMilli()) + nanosResult.seconds; result.set(totalSeconds, nanosResult.nanos); return true; }
private static long deserializeToMillis(Object value) { long numberOfMillis; if (value instanceof Number) { numberOfMillis = ((Number) value).longValue(); } else { // it is an extraction fn need to be parsed try { numberOfMillis = dateOptionalTimeParser().parseDateTime((String) value).getMillis(); } catch (IllegalArgumentException e) { // we may not be able to parse the date if it already comes in Hive format, // we retry and otherwise fail numberOfMillis = Timestamp.valueOf((String) value).toEpochMilli(); } } return numberOfMillis; }
@Override public boolean isDistanceGreater(Object v1, Object v2, int amt) { if (v1 != null && v2 != null) { long l1 = PrimitiveObjectInspectorUtils.getTimestamp(v1, (PrimitiveObjectInspector) expressionDef.getOI()).toEpochMilli(); long l2 = PrimitiveObjectInspectorUtils.getTimestamp(v2, (PrimitiveObjectInspector) expressionDef.getOI()).toEpochMilli(); return (double)(l1-l2)/1000 > amt; // TODO: lossy conversion, distance is considered in seconds } return v1 != null || v2 != null; // True if only one value is null }
public static NanoTime getNanoTime(Timestamp ts, boolean skipConversion) { Calendar calendar = getCalendar(skipConversion); calendar.setTimeInMillis(ts.toEpochMilli()); int year = calendar.get(Calendar.YEAR); if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) { year = 1 - year; } JDateTime jDateTime = new JDateTime(year, calendar.get(Calendar.MONTH) + 1, //java calendar index starting at 1. calendar.get(Calendar.DAY_OF_MONTH)); int days = jDateTime.getJulianDayNumber(); long hour = calendar.get(Calendar.HOUR_OF_DAY); long minute = calendar.get(Calendar.MINUTE); long second = calendar.get(Calendar.SECOND); long nanos = ts.getNanos(); long nanosOfDay = nanos + NANOS_PER_SECOND * second + NANOS_PER_MINUTE * minute + NANOS_PER_HOUR * hour; return new NanoTime(days, nanosOfDay); }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { Object a0 = arguments[0].get(); if (a0 == null) { return null; } result.set(tsOi == null ? tsWithLocalTzOi.getPrimitiveJavaObject(a0).getZonedDateTime().toInstant().toEpochMilli() : tsOi.getPrimitiveJavaObject(a0).toEpochMilli()); return result; }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { if (formatter == null) { return null; } // the function should support both short date and full timestamp format // time part of the timestamp should not be skipped Timestamp ts = getTimestampValue(arguments, 0, tsConverters); if (ts == null) { Date d = getDateValue(arguments, 0, dtInputTypes, dtConverters); if (d == null) { return null; } ts = Timestamp.ofEpochMilli(d.toEpochMilli()); } date.setTime(ts.toEpochMilli()); String res = formatter.format(date); if (res == null) { return null; } output.set(res); return output; }
public boolean add(Timestamp ts, HiveIntervalDayTime interval, Timestamp result) { if (ts == null || interval == null) { return false; } nanosResult.addNanos(ts.getNanos(), interval.getNanos()); long newMillis = ts.toEpochMilli() + TimeUnit.SECONDS.toMillis(interval.getTotalSeconds() + nanosResult.seconds); result.setTimeInMillis(newMillis, nanosResult.nanos); return true; }
public boolean add(HiveIntervalDayTime interval, Timestamp ts, Timestamp result) { if (ts == null || interval == null) { return false; } nanosResult.addNanos(ts.getNanos(), interval.getNanos()); long newMillis = ts.toEpochMilli() + TimeUnit.SECONDS.toMillis(interval.getTotalSeconds() + nanosResult.seconds); result.setTimeInMillis(newMillis, nanosResult.nanos); return true; }
public boolean add(Timestamp ts, HiveIntervalYearMonth interval, Timestamp result) { if (ts == null || interval == null) { return false; } long resultMillis = addMonthsToMillis(ts.toEpochMilli(), interval.getTotalMonths()); result.setTimeInMillis(resultMillis, ts.getNanos()); return true; }
public TimestampWritableV2 evaluate(TimestampWritableV2 t) { if (t == null) { return null; } final long originalTimestamp = t.getTimestamp().toEpochMilli(); final long newTimestamp = granularity.truncate(originalTimestamp); resultTS.set(Timestamp.ofEpochMilli(newTimestamp)); return resultTS; }
public boolean add(HiveIntervalYearMonth interval, Timestamp ts, Timestamp result) { if (ts == null || interval == null) { return false; } long resultMillis = addMonthsToMillis(ts.toEpochMilli(), interval.getTotalMonths()); result.setTimeInMillis(resultMillis, ts.getNanos()); return true; }
@Override public Object evaluate(GenericUDF.DeferredObject[] arguments) throws HiveException { switch (inputTypes[0]) { case INTERVAL_DAY_TIME: HiveIntervalDayTime intervalDayTime = getIntervalDayTimeValue(arguments, 0, inputTypes, converters); if (intervalDayTime == null) { return null; } output.set(intervalDayTime.getMinutes()); break; case STRING: case CHAR: case VARCHAR: case DATE: case TIMESTAMP: case TIMESTAMPLOCALTZ: case VOID: Timestamp ts = getTimestampValue(arguments, 0, converters); if (ts == null) { return null; } calendar.setTimeInMillis(ts.toEpochMilli()); output.set(calendar.get(Calendar.MINUTE)); } return output; }
@Override public Object evaluate(GenericUDF.DeferredObject[] arguments) throws HiveException { switch (inputTypes[0]) { case INTERVAL_DAY_TIME: HiveIntervalDayTime intervalDayTime = getIntervalDayTimeValue(arguments, 0, inputTypes, converters); if (intervalDayTime == null) { return null; } output.set(intervalDayTime.getSeconds()); break; case STRING: case CHAR: case VARCHAR: case DATE: case TIMESTAMP: case TIMESTAMPLOCALTZ: case VOID: Timestamp ts = getTimestampValue(arguments, 0, converters); if (ts == null) { return null; } calendar.setTimeInMillis(ts.toEpochMilli()); output.set(calendar.get(Calendar.SECOND)); } return output; }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { switch (inputTypes[0]) { case INTERVAL_DAY_TIME: HiveIntervalDayTime intervalDayTime = getIntervalDayTimeValue(arguments, 0, inputTypes, converters); if (intervalDayTime == null) { return null; } output.set(intervalDayTime.getHours()); break; case STRING: case CHAR: case VARCHAR: case DATE: case TIMESTAMP: case TIMESTAMPLOCALTZ: case VOID: Timestamp ts = getTimestampValue(arguments, 0, converters); if (ts == null) { return null; } calendar.setTimeInMillis(ts.toEpochMilli()); output.set(calendar.get(Calendar.HOUR_OF_DAY)); } return output; }
private void compareToUDFUnixTimeStampDate(long t, long y) { TimestampWritableV2 tsw = toTimestampWritable(t); LongWritable res = getLongWritable(tsw); if(res.get() != y) { System.out.printf("%d vs %d for %d, %d\n", res.get(), y, t, tsw.getTimestamp().toEpochMilli()/1000); } Assert.assertEquals(res.get(), y); }
static long getTimestampVal(IndexSearchCondition sc) throws IOException { long timestamp; try { ExprNodeConstantEvaluator eval = new ExprNodeConstantEvaluator(sc.getConstantDesc()); ObjectInspector inspector = eval.initialize(null); Object value = eval.evaluate(null); if (inspector instanceof LongObjectInspector) { timestamp = ((LongObjectInspector) inspector).get(value); } else { PrimitiveObjectInspector primitive = (PrimitiveObjectInspector) inspector; timestamp = PrimitiveObjectInspectorUtils.getTimestamp(value, primitive).toEpochMilli(); } } catch (HiveException e) { throw new IOException(e); } return timestamp; }