Search in sources :

Example 26 with TimestampWritableV2

use of org.apache.hadoop.hive.serde2.io.TimestampWritableV2 in project hive by apache.

the class TestETypeConverter method testGetInt64NanosAdjustedToUTCTimestampConverter.

@Test
public void testGetInt64NanosAdjustedToUTCTimestampConverter() throws Exception {
    ZoneId zone = ZoneId.systemDefault();
    Timestamp timestamp = Timestamp.valueOf("2018-07-15 15:12:20.11223344");
    PrimitiveType primitiveType = createInt64TimestampType(true, TimeUnit.NANOS);
    long time = timestamp.toEpochSecond() * 1000000000 + timestamp.getNanos();
    Writable writable = getWritableFromPrimitiveConverter(null, primitiveType, time);
    TimestampWritableV2 timestampWritable = (TimestampWritableV2) writable;
    timestamp = Timestamp.ofEpochSecond(timestamp.toEpochSecond(), timestamp.getNanos(), zone);
    assertEquals(timestamp.toEpochMilli(), timestampWritable.getTimestamp().toEpochMilli());
    assertEquals(timestamp.getNanos(), timestampWritable.getNanos());
}
Also used : ZoneId(java.time.ZoneId) Writable(org.apache.hadoop.io.Writable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) LongWritable(org.apache.hadoop.io.LongWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) IntWritable(org.apache.hadoop.io.IntWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) FloatWritable(org.apache.hadoop.io.FloatWritable) PrimitiveType(org.apache.parquet.schema.PrimitiveType) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) Test(org.junit.Test)

Example 27 with TimestampWritableV2

use of org.apache.hadoop.hive.serde2.io.TimestampWritableV2 in project hive by apache.

the class TestETypeConverter method testGetInt64MillisTimestampProlepticConverter.

@Test
public void testGetInt64MillisTimestampProlepticConverter() throws Exception {
    Timestamp timestamp = Timestamp.valueOf("1572-07-15 15:12:20.112");
    PrimitiveType primitiveType = createInt64TimestampType(false, TimeUnit.MILLIS);
    Writable writable = getWritableFromPrimitiveConverter(null, primitiveType, timestamp.toEpochMilli());
    TimestampWritableV2 timestampWritable = (TimestampWritableV2) writable;
    assertEquals(timestamp.toEpochMilli(), timestampWritable.getTimestamp().toEpochMilli());
}
Also used : Writable(org.apache.hadoop.io.Writable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) LongWritable(org.apache.hadoop.io.LongWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) IntWritable(org.apache.hadoop.io.IntWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) FloatWritable(org.apache.hadoop.io.FloatWritable) PrimitiveType(org.apache.parquet.schema.PrimitiveType) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) Test(org.junit.Test)

Example 28 with TimestampWritableV2

use of org.apache.hadoop.hive.serde2.io.TimestampWritableV2 in project hive by apache.

the class ColumnStatisticsObjTranslator method unpackTimestampStats.

private static void unpackTimestampStats(ObjectInspector oi, Object o, ColumnStatsField csf, ColumnStatisticsObj statsObj) {
    switch(csf) {
        case COUNT_NULLS:
            long cn = ((LongObjectInspector) oi).get(o);
            statsObj.getStatsData().getTimestampStats().setNumNulls(cn);
            break;
        case MIN:
            TimestampWritableV2 min = ((TimestampObjectInspector) oi).getPrimitiveWritableObject(o);
            statsObj.getStatsData().getTimestampStats().setLowValue(new Timestamp(min.getSeconds()));
            break;
        case MAX:
            TimestampWritableV2 max = ((TimestampObjectInspector) oi).getPrimitiveWritableObject(o);
            statsObj.getStatsData().getTimestampStats().setHighValue(new Timestamp(max.getSeconds()));
            break;
        case NDV:
            long ndv = ((LongObjectInspector) oi).get(o);
            statsObj.getStatsData().getTimestampStats().setNumDVs(ndv);
            break;
        case BITVECTOR:
            PrimitiveObjectInspector poi = (PrimitiveObjectInspector) oi;
            byte[] buf = ((BinaryObjectInspector) poi).getPrimitiveJavaObject(o);
            statsObj.getStatsData().getTimestampStats().setBitVectors(buf);
            break;
        default:
            throw new RuntimeException("Unsupported column stat for TIMESTAMP : " + csf);
    }
}
Also used : LongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.LongObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) BinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) Timestamp(org.apache.hadoop.hive.metastore.api.Timestamp)

Example 29 with TimestampWritableV2

use of org.apache.hadoop.hive.serde2.io.TimestampWritableV2 in project hive by apache.

the class GenericUDFTrunc method evaluateDate.

private Object evaluateDate(DeferredObject[] arguments) throws UDFArgumentLengthException, HiveException, UDFArgumentTypeException, UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentLengthException("trunc() requires 2 argument, got " + arguments.length);
    }
    if (arguments[0].get() == null || arguments[1].get() == null) {
        return null;
    }
    if (textConverter2 != null) {
        fmtInput = textConverter2.convert(arguments[1].get()).toString();
    }
    Date d;
    switch(inputType1) {
        case STRING:
            String dateString = textConverter1.convert(arguments[0].get()).toString();
            d = DateParser.parseDate(dateString);
            if (d == null) {
                return null;
            }
            break;
        case TIMESTAMP:
            Timestamp ts = ((TimestampWritableV2) timestampConverter.convert(arguments[0].get())).getTimestamp();
            d = Date.ofEpochMilli(ts.toEpochMilli());
            break;
        case DATE:
            DateWritableV2 dw = (DateWritableV2) dateWritableConverter.convert(arguments[0].get());
            d = dw.get();
            break;
        default:
            throw new UDFArgumentTypeException(0, "TRUNC() only takes STRING/TIMESTAMP/DATEWRITABLE types, got " + inputType1);
    }
    if (evalDate(d) == null) {
        return null;
    }
    output.set(date.toString());
    return output;
}
Also used : UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) DateWritableV2(org.apache.hadoop.hive.serde2.io.DateWritableV2) TruncDateFromString(org.apache.hadoop.hive.ql.exec.vector.expressions.TruncDateFromString) TruncDateFromTimestamp(org.apache.hadoop.hive.ql.exec.vector.expressions.TruncDateFromTimestamp) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) Date(org.apache.hadoop.hive.common.type.Date) TruncDateFromDate(org.apache.hadoop.hive.ql.exec.vector.expressions.TruncDateFromDate)

Example 30 with TimestampWritableV2

use of org.apache.hadoop.hive.serde2.io.TimestampWritableV2 in project hive by apache.

the class TestVectorGroupByOperator method testKeyTypeAggregate.

private void testKeyTypeAggregate(String aggregateName, FakeVectorRowBatchFromObjectIterables data, Map<Object, Object> expected) throws HiveException {
    List<String> mapColumnNames = new ArrayList<String>();
    mapColumnNames.add("Key");
    mapColumnNames.add("Value");
    VectorizationContext ctx = new VectorizationContext("name", mapColumnNames);
    Set<Object> keys = new HashSet<Object>();
    AggregationDesc agg = buildAggregationDesc(ctx, aggregateName, GenericUDAFEvaluator.Mode.PARTIAL1, "Value", TypeInfoFactory.getPrimitiveTypeInfo(data.getTypes()[1]));
    ArrayList<AggregationDesc> aggs = new ArrayList<AggregationDesc>();
    aggs.add(agg);
    ArrayList<String> outputColumnNames = new ArrayList<String>();
    outputColumnNames.add("_col0");
    outputColumnNames.add("_col1");
    GroupByDesc desc = new GroupByDesc();
    VectorGroupByDesc vectorGroupByDesc = new VectorGroupByDesc();
    desc.setOutputColumnNames(outputColumnNames);
    desc.setAggregators(aggs);
    vectorGroupByDesc.setProcessingMode(ProcessingMode.HASH);
    ExprNodeDesc keyExp = buildColumnDesc(ctx, "Key", TypeInfoFactory.getPrimitiveTypeInfo(data.getTypes()[0]));
    ArrayList<ExprNodeDesc> keysDesc = new ArrayList<ExprNodeDesc>();
    keysDesc.add(keyExp);
    desc.setKeys(keysDesc);
    CompilationOpContext cCtx = new CompilationOpContext();
    Operator<? extends OperatorDesc> groupByOp = OperatorFactory.get(cCtx, desc);
    VectorGroupByOperator vgo = (VectorGroupByOperator) Vectorizer.vectorizeGroupByOperator(groupByOp, ctx, vectorGroupByDesc);
    if (vgo == null) {
        assertTrue(false);
    }
    FakeCaptureVectorToRowOutputOperator out = FakeCaptureVectorToRowOutputOperator.addCaptureOutputChild(cCtx, vgo);
    vgo.initialize(hconf, null);
    out.setOutputInspector(new FakeCaptureVectorToRowOutputOperator.OutputInspector() {

        private int rowIndex;

        private String aggregateName;

        private Map<Object, Object> expected;

        private Set<Object> keys;

        @Override
        public void inspectRow(Object row, int tag) throws HiveException {
            assertTrue(row instanceof Object[]);
            Object[] fields = (Object[]) row;
            assertEquals(2, fields.length);
            Object key = fields[0];
            Object keyValue = null;
            if (null == key) {
                keyValue = null;
            } else if (key instanceof ByteWritable) {
                ByteWritable bwKey = (ByteWritable) key;
                keyValue = bwKey.get();
            } else if (key instanceof ShortWritable) {
                ShortWritable swKey = (ShortWritable) key;
                keyValue = swKey.get();
            } else if (key instanceof IntWritable) {
                IntWritable iwKey = (IntWritable) key;
                keyValue = iwKey.get();
            } else if (key instanceof LongWritable) {
                LongWritable lwKey = (LongWritable) key;
                keyValue = lwKey.get();
            } else if (key instanceof TimestampWritableV2) {
                TimestampWritableV2 twKey = (TimestampWritableV2) key;
                keyValue = twKey.getTimestamp().toSqlTimestamp();
            } else if (key instanceof DoubleWritable) {
                DoubleWritable dwKey = (DoubleWritable) key;
                keyValue = dwKey.get();
            } else if (key instanceof FloatWritable) {
                FloatWritable fwKey = (FloatWritable) key;
                keyValue = fwKey.get();
            } else if (key instanceof BooleanWritable) {
                BooleanWritable bwKey = (BooleanWritable) key;
                keyValue = bwKey.get();
            } else if (key instanceof HiveDecimalWritable) {
                HiveDecimalWritable hdwKey = (HiveDecimalWritable) key;
                keyValue = hdwKey.getHiveDecimal();
            } else {
                Assert.fail(String.format("Not implemented key output type %s: %s", key.getClass().getName(), key));
            }
            String keyValueAsString = String.format("%s", keyValue);
            assertTrue(expected.containsKey(keyValue));
            Object expectedValue = expected.get(keyValue);
            Object value = fields[1];
            Validator validator = getValidator(aggregateName);
            validator.validate(keyValueAsString, expectedValue, new Object[] { value });
            keys.add(keyValue);
        }

        private FakeCaptureVectorToRowOutputOperator.OutputInspector init(String aggregateName, Map<Object, Object> expected, Set<Object> keys) {
            this.aggregateName = aggregateName;
            this.expected = expected;
            this.keys = keys;
            return this;
        }
    }.init(aggregateName, expected, keys));
    for (VectorizedRowBatch unit : data) {
        vgo.process(unit, 0);
    }
    vgo.close(false);
    List<Object> outBatchList = out.getCapturedRows();
    assertNotNull(outBatchList);
    assertEquals(expected.size(), outBatchList.size());
    assertEquals(expected.size(), keys.size());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc) LongWritable(org.apache.hadoop.io.LongWritable) VectorGroupByDesc(org.apache.hadoop.hive.ql.plan.VectorGroupByDesc) GroupByDesc(org.apache.hadoop.hive.ql.plan.GroupByDesc) ByteWritable(org.apache.hadoop.hive.serde2.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable) HashSet(java.util.HashSet) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) FloatWritable(org.apache.hadoop.io.FloatWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) CompilationOpContext(org.apache.hadoop.hive.ql.CompilationOpContext) VectorGroupByDesc(org.apache.hadoop.hive.ql.plan.VectorGroupByDesc) AggregationDesc(org.apache.hadoop.hive.ql.plan.AggregationDesc) FakeCaptureVectorToRowOutputOperator(org.apache.hadoop.hive.ql.exec.vector.util.FakeCaptureVectorToRowOutputOperator) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

TimestampWritableV2 (org.apache.hadoop.hive.serde2.io.TimestampWritableV2)99 IntWritable (org.apache.hadoop.io.IntWritable)44 Test (org.junit.Test)42 Timestamp (org.apache.hadoop.hive.common.type.Timestamp)36 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)36 BytesWritable (org.apache.hadoop.io.BytesWritable)31 Text (org.apache.hadoop.io.Text)31 DateWritableV2 (org.apache.hadoop.hive.serde2.io.DateWritableV2)30 LongWritable (org.apache.hadoop.io.LongWritable)30 BooleanWritable (org.apache.hadoop.io.BooleanWritable)26 FloatWritable (org.apache.hadoop.io.FloatWritable)26 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)25 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)21 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)21 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)21 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)20 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)20 HiveIntervalDayTimeWritable (org.apache.hadoop.hive.serde2.io.HiveIntervalDayTimeWritable)19 HiveVarcharWritable (org.apache.hadoop.hive.serde2.io.HiveVarcharWritable)18 ArrayList (java.util.ArrayList)16