use of org.apache.hadoop.hive.common.type.TimestampTZ in project hive by apache.
the class GenericUDFToUnixTimeStamp method evaluate.
@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
if (arguments[0].get() == null) {
return null;
}
if (inputTextConverter != null) {
String textVal = (String) inputTextConverter.convert(arguments[0].get());
if (textVal == null) {
return null;
}
if (patternConverter != null) {
if (arguments[1].get() == null) {
return null;
}
String patternVal = (String) patternConverter.convert(arguments[1].get());
if (patternVal == null) {
return null;
}
if (!patternVal.equals(lasPattern)) {
formatter.applyPattern(patternVal);
lasPattern = patternVal;
}
}
try {
retValue.set(formatter.parse(textVal).getTime() / 1000);
return retValue;
} catch (ParseException e) {
return null;
}
} else if (inputDateOI != null) {
retValue.set(inputDateOI.getPrimitiveWritableObject(arguments[0].get()).getTimeInSeconds());
return retValue;
} else if (inputTimestampLocalTzOI != null) {
TimestampTZ timestampTZ = inputTimestampLocalTzOI.getPrimitiveJavaObject(arguments[0].get());
retValue.set(timestampTZ.getEpochSecond());
return retValue;
}
Timestamp timestamp = inputTimestampOI.getPrimitiveJavaObject(arguments[0].get());
setValueFromTs(retValue, timestamp);
return retValue;
}
use of org.apache.hadoop.hive.common.type.TimestampTZ in project hive by apache.
the class DruidSerDe method deserialize.
@Override
public Object deserialize(Writable writable) throws SerDeException {
final DruidWritable input = (DruidWritable) writable;
final List<Object> output = Lists.newArrayListWithExpectedSize(columns.length);
for (int i = 0; i < columns.length; i++) {
final Object value = input.getValue().get(columns[i]);
if (value == null) {
output.add(null);
continue;
}
switch(types[i].getPrimitiveCategory()) {
case TIMESTAMP:
output.add(new TimestampWritable(Timestamp.valueOf(ZonedDateTime.ofInstant(Instant.ofEpochMilli(((Number) value).longValue()), tsTZTypeInfo.timeZone()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toString())));
break;
case TIMESTAMPLOCALTZ:
output.add(new TimestampLocalTZWritable(new TimestampTZ(ZonedDateTime.ofInstant(Instant.ofEpochMilli(((Number) value).longValue()), ((TimestampLocalTZTypeInfo) types[i]).timeZone()))));
break;
case BYTE:
output.add(new ByteWritable(((Number) value).byteValue()));
break;
case SHORT:
output.add(new ShortWritable(((Number) value).shortValue()));
break;
case INT:
output.add(new IntWritable(((Number) value).intValue()));
break;
case LONG:
output.add(new LongWritable(((Number) value).longValue()));
break;
case FLOAT:
output.add(new FloatWritable(((Number) value).floatValue()));
break;
case DOUBLE:
output.add(new DoubleWritable(((Number) value).doubleValue()));
break;
case DECIMAL:
output.add(new HiveDecimalWritable(HiveDecimal.create(((Number) value).doubleValue())));
break;
case CHAR:
output.add(new HiveCharWritable(new HiveChar(value.toString(), ((CharTypeInfo) types[i]).getLength())));
break;
case VARCHAR:
output.add(new HiveVarcharWritable(new HiveVarchar(value.toString(), ((VarcharTypeInfo) types[i]).getLength())));
break;
case STRING:
output.add(new Text(value.toString()));
break;
case BOOLEAN:
output.add(new BooleanWritable(Boolean.valueOf(value.toString())));
break;
default:
throw new SerDeException("Unknown type: " + types[i].getPrimitiveCategory());
}
}
return output;
}
use of org.apache.hadoop.hive.common.type.TimestampTZ in project hive by apache.
the class MultiValueBoundaryScanner method isEqual.
@Override
public boolean isEqual(Object v1, Object v2) {
if (v1 != null && v2 != null) {
TimestampTZ l1 = PrimitiveObjectInspectorUtils.getTimestampLocalTZ(v1, (PrimitiveObjectInspector) expressionDef.getOI(), null);
TimestampTZ l2 = PrimitiveObjectInspectorUtils.getTimestampLocalTZ(v2, (PrimitiveObjectInspector) expressionDef.getOI(), null);
return l1.equals(l2);
}
// True if both are null
return v1 == null && v2 == null;
}
use of org.apache.hadoop.hive.common.type.TimestampTZ in project hive by apache.
the class TestUDFDateFormatGranularity method testTimestampWithLocalTZGranularity.
@Test
public void testTimestampWithLocalTZGranularity() throws Exception {
// Running example
// Friday 30th August 1985 02:47:02 AM
final TimestampLocalTZWritable t = new TimestampLocalTZWritable(new TimestampTZ(Instant.ofEpochMilli(494243222000L).atZone(ZoneId.of("America/Los_Angeles"))));
UDFDateFloor g;
// Year granularity
// Tuesday 1st January 1985 12:00:00 AM
g = new UDFDateFloorYear();
TimestampLocalTZWritable i1 = g.evaluate(t);
assertEquals(new TimestampTZ(Instant.ofEpochMilli(473414400000L).atZone(ZoneId.of("America/Los_Angeles"))), i1.getTimestampTZ());
// Quarter granularity
// Monday 1st July 1985 12:00:00 AM
g = new UDFDateFloorQuarter();
TimestampLocalTZWritable i2 = g.evaluate(t);
assertEquals(new TimestampTZ(Instant.ofEpochMilli(489049200000L).atZone(ZoneId.of("America/Los_Angeles"))), i2.getTimestampTZ());
// Month granularity
// Thursday 1st August 1985 12:00:00 AM
g = new UDFDateFloorMonth();
TimestampLocalTZWritable i3 = g.evaluate(t);
assertEquals(new TimestampTZ(Instant.ofEpochMilli(491727600000L).atZone(ZoneId.of("America/Los_Angeles"))), i3.getTimestampTZ());
// Week granularity
// Monday 26th August 1985 12:00:00 AM
g = new UDFDateFloorWeek();
TimestampLocalTZWritable i4 = g.evaluate(t);
assertEquals(new TimestampTZ(Instant.ofEpochMilli(493887600000L).atZone(ZoneId.of("America/Los_Angeles"))), i4.getTimestampTZ());
// Day granularity
// Friday 30th August 1985 12:00:00 AM
g = new UDFDateFloorDay();
TimestampLocalTZWritable i5 = g.evaluate(t);
assertEquals(new TimestampTZ(Instant.ofEpochMilli(494233200000L).atZone(ZoneId.of("America/Los_Angeles"))), i5.getTimestampTZ());
// Hour granularity
// Friday 30th August 1985 02:00:00 AM
g = new UDFDateFloorHour();
TimestampLocalTZWritable i6 = g.evaluate(t);
assertEquals(new TimestampTZ(Instant.ofEpochMilli(494240400000L).atZone(ZoneId.of("America/Los_Angeles"))), i6.getTimestampTZ());
// Minute granularity
// Friday 30th August 1985 02:47:00 AM
g = new UDFDateFloorMinute();
TimestampLocalTZWritable i7 = g.evaluate(t);
assertEquals(new TimestampTZ(Instant.ofEpochMilli(494243220000L).atZone(ZoneId.of("America/Los_Angeles"))), i7.getTimestampTZ());
// Second granularity
// Friday 30th August 1985 02:47:02 AM
g = new UDFDateFloorSecond();
TimestampLocalTZWritable i8 = g.evaluate(t);
assertEquals(new TimestampTZ(Instant.ofEpochMilli(494243222000L).atZone(ZoneId.of("America/Los_Angeles"))), i8.getTimestampTZ());
}
use of org.apache.hadoop.hive.common.type.TimestampTZ in project hive by apache.
the class LazyTimestampLocalTZObjectInspector method getPrimitiveJavaObject.
@Override
public TimestampTZ getPrimitiveJavaObject(Object o) {
if (o == null) {
return null;
}
TimestampTZ t = ((LazyTimestampLocalTZ) o).getWritableObject().getTimestampTZ();
TimestampLocalTZTypeInfo timestampTZTypeInfo = (TimestampLocalTZTypeInfo) typeInfo;
if (!t.getZonedDateTime().getZone().equals(timestampTZTypeInfo.timeZone())) {
t.setZonedDateTime(t.getZonedDateTime().withZoneSameInstant(timestampTZTypeInfo.timeZone()));
}
return t;
}
Aggregations