Search in sources :

Example 56 with Date

use of org.apache.hadoop.hive.common.type.Date 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 57 with Date

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

the class GenericUDFCurrentDate method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 0) {
        throw new UDFArgumentLengthException("The function CURRENT_DATE does not take any arguments, but found " + arguments.length);
    }
    if (currentDate == null) {
        SessionState ss = SessionState.get();
        ZonedDateTime dateTime = ss.getQueryCurrentTimestamp().atZone(ss.getConf().getLocalTimeZone());
        Date dateVal = Date.valueOf(dateTime.toString().substring(0, 10));
        currentDate = new DateWritableV2(dateVal);
    }
    return PrimitiveObjectInspectorFactory.writableDateObjectInspector;
}
Also used : SessionState(org.apache.hadoop.hive.ql.session.SessionState) ZonedDateTime(java.time.ZonedDateTime) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) DateWritableV2(org.apache.hadoop.hive.serde2.io.DateWritableV2) Date(org.apache.hadoop.hive.common.type.Date)

Example 58 with Date

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

the class GenericUDFLastDay method evaluate.

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    Date d = getDateValue(arguments, 0, converters);
    if (d == null) {
        return null;
    }
    lastDay(d);
    output.set(date.toString());
    return output;
}
Also used : Date(org.apache.hadoop.hive.common.type.Date)

Example 59 with Date

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

the class DateTimeMath method add.

public Date add(HiveIntervalYearMonth interval, Date dt) {
    if (dt == null || interval == null) {
        return null;
    }
    Date dtResult = new Date();
    add(interval, dt, dtResult);
    return dtResult;
}
Also used : Date(org.apache.hadoop.hive.common.type.Date)

Example 60 with Date

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

the class DateTimeMath method subtract.

public Date subtract(Date left, HiveIntervalYearMonth right) {
    if (left == null || right == null) {
        return null;
    }
    Date dtResult = new Date();
    subtract(left, right, dtResult);
    return dtResult;
}
Also used : Date(org.apache.hadoop.hive.common.type.Date)

Aggregations

Date (org.apache.hadoop.hive.common.type.Date)71 Timestamp (org.apache.hadoop.hive.common.type.Timestamp)26 DateWritableV2 (org.apache.hadoop.hive.serde2.io.DateWritableV2)21 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)18 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)18 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)17 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)15 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)14 Text (org.apache.hadoop.io.Text)14 Test (org.junit.Test)14 HiveIntervalYearMonth (org.apache.hadoop.hive.common.type.HiveIntervalYearMonth)13 BytesWritable (org.apache.hadoop.io.BytesWritable)12 HiveIntervalDayTime (org.apache.hadoop.hive.common.type.HiveIntervalDayTime)11 TimestampWritableV2 (org.apache.hadoop.hive.serde2.io.TimestampWritableV2)11 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)11 List (java.util.List)10 LongWritable (org.apache.hadoop.io.LongWritable)10 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)9 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)9 HiveCharWritable (org.apache.hadoop.hive.serde2.io.HiveCharWritable)9