Search in sources :

Example 96 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class ScheduledQueryAnalyzer method parseTimeStamp.

private Timestamp parseTimeStamp(Tree offsetNode) {
    if (offsetNode == null) {
        return new Timestamp();
    }
    List<String> s = new ArrayList<>();
    s.add(TimestampParser.ISO_8601_FORMAT_STR);
    s.add(TimestampParser.RFC_1123_FORMAT_STR);
    s.add("HH:mm:ss");
    s.add("H:mm:ss");
    s.add("HH:mm");
    TimestampParser p = new TimestampParser(s);
    return p.parseTimestamp(unescapeSQLString(offsetNode.getText()));
}
Also used : TimestampParser(org.apache.hive.common.util.TimestampParser) ArrayList(java.util.ArrayList) Timestamp(org.apache.hadoop.hive.common.type.Timestamp)

Example 97 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class VectorUDFDatetimeLegacyHybridCalendarTimestamp method func.

protected void func(TimestampColumnVector outputColVector, TimestampColumnVector inputColVector, int i) {
    String adjustedTimestampString = SIMPLE_DATE_FORMAT_THREAD_LOCAL.get().format(new java.sql.Timestamp(inputColVector.time[i]));
    Timestamp adjustedTimestamp = Timestamp.valueOf(adjustedTimestampString);
    outputColVector.time[i] = adjustedTimestamp.toEpochMilli();
    // Nanos don't change
    outputColVector.nanos[i] = inputColVector.nanos[i];
}
Also used : Timestamp(org.apache.hadoop.hive.common.type.Timestamp)

Example 98 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class GenericUDFCastFormat method convert.

private Object convert(Object o) throws HiveException {
    Object input;
    switch(inputOI.getPrimitiveCategory()) {
        case STRING:
            input = ((StringObjectInspector) inputOI).getPrimitiveJavaObject(o);
            break;
        case CHAR:
            input = ((HiveCharObjectInspector) inputOI).getPrimitiveJavaObject(o).getStrippedValue();
            break;
        case VARCHAR:
            input = ((HiveVarcharObjectInspector) inputOI).getPrimitiveJavaObject(o).toString();
            break;
        case TIMESTAMP:
            input = ((TimestampObjectInspector) inputOI).getPrimitiveWritableObject(o).getTimestamp();
            break;
        case DATE:
            input = ((DateObjectInspector) inputOI).getPrimitiveWritableObject(o).get();
            break;
        default:
            throw new HiveException("Input type " + inputOI.getPrimitiveCategory() + " not valid");
    }
    // format here
    Object formattedOutput = null;
    if (inputOI.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.DATE || inputOI.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.TIMESTAMP) {
        if (inputOI.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.DATE) {
            try {
                formattedOutput = formatter.format((Date) input);
            } catch (IllegalArgumentException e) {
                return null;
            }
        } else {
            try {
                formattedOutput = formatter.format((Timestamp) input);
            } catch (IllegalArgumentException e) {
                return null;
            }
        }
        if (formattedOutput == null) {
            return null;
        }
    }
    // parse and create Writables
    switch(outputOI.getPrimitiveCategory()) {
        case STRING:
            return new Text((String) formattedOutput);
        case CHAR:
            return ((SettableHiveCharObjectInspector) outputOI).create(new HiveChar((String) formattedOutput, -1));
        case VARCHAR:
            return ((SettableHiveVarcharObjectInspector) outputOI).create(new HiveVarchar((String) formattedOutput, -1));
        case TIMESTAMP:
            try {
                Timestamp t = formatter.parseTimestamp((String) input);
                if (t == null) {
                    return null;
                }
                return ((SettableTimestampObjectInspector) outputOI).create(t);
            } catch (IllegalArgumentException e) {
                return null;
            }
        case DATE:
            try {
                Date d = formatter.parseDate((String) input);
                if (d == null) {
                    return null;
                }
                return ((SettableDateObjectInspector) outputOI).create(d);
            } catch (IllegalArgumentException e) {
                return null;
            }
        default:
            throw new HiveException("Output type " + outputOI.getPrimitiveCategory() + " not valid");
    }
}
Also used : DateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.DateObjectInspector) SettableDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableDateObjectInspector) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) Text(org.apache.hadoop.io.Text) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) Date(org.apache.hadoop.hive.common.type.Date) SettableTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableTimestampObjectInspector) TimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.TimestampObjectInspector) HiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveVarcharObjectInspector) SettableHiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveVarcharObjectInspector) SettableTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableTimestampObjectInspector) SettableHiveVarcharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveVarcharObjectInspector) HiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveCharObjectInspector) SettableHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveCharObjectInspector) SettableDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableDateObjectInspector) SettableHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableHiveCharObjectInspector)

Example 99 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class GenericUDFDateDiff method evaluate.

@Override
public IntWritable evaluate(DeferredObject[] arguments) throws HiveException {
    Timestamp ts1 = getTimestampValue(arguments, 0, tsConverters);
    Timestamp ts2 = getTimestampValue(arguments, 1, tsConverters);
    if (ts1 == null || ts2 == null) {
        return null;
    }
    output.set(DateWritableV2.millisToDays(ts1.toEpochMilli()) - DateWritableV2.millisToDays(ts2.toEpochMilli()));
    return output;
}
Also used : Timestamp(org.apache.hadoop.hive.common.type.Timestamp)

Example 100 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class ColumnVectorGenUtil method generateTimestampColumnVector.

public static TimestampColumnVector generateTimestampColumnVector(boolean nulls, boolean repeating, int size, Random rand, Timestamp[] timestampValues) {
    TimestampColumnVector tcv = new TimestampColumnVector(size);
    tcv.noNulls = !nulls;
    tcv.isRepeating = repeating;
    Timestamp repeatingTimestamp = RandomTypeUtil.getRandTimestamp(rand);
    int nullFrequency = generateNullFrequency(rand);
    for (int i = 0; i < size; i++) {
        if (nulls && (repeating || i % nullFrequency == 0)) {
            tcv.isNull[i] = true;
            tcv.setNullValue(i);
            timestampValues[i] = null;
        } else {
            tcv.isNull[i] = false;
            if (!repeating) {
                Timestamp randomTimestamp = RandomTypeUtil.getRandTimestamp(rand);
                tcv.set(i, randomTimestamp.toSqlTimestamp());
                timestampValues[i] = randomTimestamp;
            } else {
                tcv.set(i, repeatingTimestamp.toSqlTimestamp());
                timestampValues[i] = repeatingTimestamp;
            }
        }
    }
    return tcv;
}
Also used : TimestampColumnVector(org.apache.hadoop.hive.ql.exec.vector.TimestampColumnVector) Timestamp(org.apache.hadoop.hive.common.type.Timestamp)

Aggregations

Timestamp (org.apache.hadoop.hive.common.type.Timestamp)116 Test (org.junit.Test)36 TimestampWritableV2 (org.apache.hadoop.hive.serde2.io.TimestampWritableV2)32 Date (org.apache.hadoop.hive.common.type.Date)27 BytesWritable (org.apache.hadoop.io.BytesWritable)25 LongWritable (org.apache.hadoop.io.LongWritable)25 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)24 Text (org.apache.hadoop.io.Text)22 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)21 IntWritable (org.apache.hadoop.io.IntWritable)21 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)20 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)20 BooleanWritable (org.apache.hadoop.io.BooleanWritable)19 FloatWritable (org.apache.hadoop.io.FloatWritable)19 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)18 ArrayList (java.util.ArrayList)17 DateWritableV2 (org.apache.hadoop.hive.serde2.io.DateWritableV2)17 HiveIntervalDayTime (org.apache.hadoop.hive.common.type.HiveIntervalDayTime)16 List (java.util.List)15 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)12